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