]>
Commit | Line | Data |
---|---|---|
a5b8e697 MW |
1 | /* |
2 | * Copyright (C) 2008 Martin Willi | |
19ef2aec TB |
3 | * |
4 | * Copyright (C) secunet Security Networks AG | |
a5b8e697 MW |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2 of the License, or (at your | |
9 | * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
13 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | * for more details. | |
a5b8e697 MW |
15 | */ |
16 | ||
17 | #include "medcli_listener.h" | |
18 | ||
19 | #include <daemon.h> | |
20 | #include <library.h> | |
21 | ||
22 | typedef struct private_medcli_listener_t private_medcli_listener_t; | |
23 | typedef enum mediated_state_t mediated_state_t; | |
24 | ||
25 | /** | |
26 | * state of a mediated connection | |
27 | */ | |
28 | enum mediated_state_t { | |
29 | STATE_DOWN = 1, | |
30 | STATE_CONNECTING = 2, | |
31 | STATE_UP = 3, | |
32 | }; | |
33 | ||
34 | /** | |
35 | * Private data of an medcli_listener_t object | |
36 | */ | |
37 | struct private_medcli_listener_t { | |
38 | ||
39 | /** | |
40 | * Public part | |
41 | */ | |
42 | medcli_listener_t public; | |
7daf5226 | 43 | |
a5b8e697 MW |
44 | /** |
45 | * underlying database handle | |
46 | */ | |
47 | database_t *db; | |
48 | }; | |
49 | ||
50 | /** | |
b12734d8 | 51 | * Update connection status in the database |
a5b8e697 | 52 | */ |
4a537c90 MW |
53 | static void set_state(private_medcli_listener_t *this, char *alias, |
54 | mediated_state_t state) | |
a5b8e697 | 55 | { |
4a537c90 MW |
56 | this->db->execute(this->db, NULL, |
57 | "UPDATE Connection SET Status = ? WHERE Alias = ?", | |
58 | DB_UINT, state, DB_TEXT, alias); | |
59 | } | |
b12734d8 TB |
60 | |
61 | METHOD(listener_t, ike_state_change, bool, | |
62 | private_medcli_listener_t *this, ike_sa_t *ike_sa, ike_sa_state_t state) | |
4a537c90 MW |
63 | { |
64 | if (ike_sa) | |
a5b8e697 | 65 | { |
4a537c90 MW |
66 | switch (state) |
67 | { | |
68 | case IKE_CONNECTING: | |
69 | set_state(this, ike_sa->get_name(ike_sa), STATE_CONNECTING); | |
70 | break; | |
71 | case IKE_DESTROYING: | |
72 | set_state(this, ike_sa->get_name(ike_sa), STATE_DOWN); | |
73 | default: | |
74 | break; | |
75 | } | |
a5b8e697 | 76 | } |
4a537c90 MW |
77 | return TRUE; |
78 | } | |
a5b8e697 | 79 | |
b12734d8 TB |
80 | METHOD(listener_t, child_state_change, bool, |
81 | private_medcli_listener_t *this, ike_sa_t *ike_sa, child_sa_t *child_sa, | |
82 | child_sa_state_t state) | |
4a537c90 MW |
83 | { |
84 | if (ike_sa && child_sa) | |
a5b8e697 | 85 | { |
4a537c90 MW |
86 | switch (state) |
87 | { | |
88 | case CHILD_INSTALLED: | |
89 | set_state(this, child_sa->get_name(child_sa), STATE_UP); | |
90 | break; | |
91 | case CHILD_DESTROYING: | |
92 | set_state(this, child_sa->get_name(child_sa), STATE_DOWN); | |
93 | break; | |
94 | default: | |
95 | break; | |
96 | } | |
a5b8e697 | 97 | } |
a5b8e697 MW |
98 | return TRUE; |
99 | } | |
100 | ||
b12734d8 TB |
101 | METHOD(medcli_listener_t, destroy, void, |
102 | private_medcli_listener_t *this) | |
a5b8e697 MW |
103 | { |
104 | this->db->execute(this->db, NULL, "UPDATE Connection SET Status = ?", | |
105 | DB_UINT, STATE_DOWN); | |
4a537c90 | 106 | free(this); |
a5b8e697 MW |
107 | } |
108 | ||
109 | /** | |
110 | * Described in header. | |
111 | */ | |
112 | medcli_listener_t *medcli_listener_create(database_t *db) | |
113 | { | |
b12734d8 TB |
114 | private_medcli_listener_t *this; |
115 | ||
116 | INIT(this, | |
117 | .public = { | |
118 | .listener = { | |
119 | .ike_state_change = _ike_state_change, | |
120 | .child_state_change = _child_state_change, | |
121 | }, | |
122 | .destroy = _destroy, | |
123 | }, | |
124 | .db = db, | |
125 | ); | |
7daf5226 | 126 | |
a5b8e697 MW |
127 | db->execute(db, NULL, "UPDATE Connection SET Status = ?", |
128 | DB_UINT, STATE_DOWN); | |
7daf5226 | 129 | |
a5b8e697 MW |
130 | return &this->public; |
131 | } | |
132 |