Annotation of embedaddon/strongswan/src/libcharon/plugins/medcli/medcli_listener.c, revision 1.1.1.1
1.1 misho 1: /*
2: * Copyright (C) 2008 Martin Willi
3: * HSR 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: * Update connection status in the database
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: METHOD(listener_t, ike_state_change, bool,
61: private_medcli_listener_t *this, ike_sa_t *ike_sa, ike_sa_state_t state)
62: {
63: if (ike_sa)
64: {
65: switch (state)
66: {
67: case IKE_CONNECTING:
68: set_state(this, ike_sa->get_name(ike_sa), STATE_CONNECTING);
69: break;
70: case IKE_DESTROYING:
71: set_state(this, ike_sa->get_name(ike_sa), STATE_DOWN);
72: default:
73: break;
74: }
75: }
76: return TRUE;
77: }
78:
79: METHOD(listener_t, child_state_change, bool,
80: private_medcli_listener_t *this, ike_sa_t *ike_sa, child_sa_t *child_sa,
81: child_sa_state_t state)
82: {
83: if (ike_sa && child_sa)
84: {
85: switch (state)
86: {
87: case CHILD_INSTALLED:
88: set_state(this, child_sa->get_name(child_sa), STATE_UP);
89: break;
90: case CHILD_DESTROYING:
91: set_state(this, child_sa->get_name(child_sa), STATE_DOWN);
92: break;
93: default:
94: break;
95: }
96: }
97: return TRUE;
98: }
99:
100: METHOD(medcli_listener_t, destroy, void,
101: private_medcli_listener_t *this)
102: {
103: this->db->execute(this->db, NULL, "UPDATE Connection SET Status = ?",
104: DB_UINT, STATE_DOWN);
105: free(this);
106: }
107:
108: /**
109: * Described in header.
110: */
111: medcli_listener_t *medcli_listener_create(database_t *db)
112: {
113: private_medcli_listener_t *this;
114:
115: INIT(this,
116: .public = {
117: .listener = {
118: .ike_state_change = _ike_state_change,
119: .child_state_change = _child_state_change,
120: },
121: .destroy = _destroy,
122: },
123: .db = db,
124: );
125:
126: db->execute(db, NULL, "UPDATE Connection SET Status = ?",
127: DB_UINT, STATE_DOWN);
128:
129: return &this->public;
130: }
131:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>