refactoring of tnc-ifmap plugin
[strongswan.git] / src / libcharon / plugins / tnc_ifmap / tnc_ifmap_listener.c
1 /*
2 * Copyright (C) 2011 Andreas Steffen
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 "tnc_ifmap_listener.h"
17 #include "tnc_ifmap_soap.h"
18
19 #include <daemon.h>
20 #include <debug.h>
21
22 typedef struct private_tnc_ifmap_listener_t private_tnc_ifmap_listener_t;
23
24 /**
25 * Private data of an tnc_ifmap_listener_t object.
26 */
27 struct private_tnc_ifmap_listener_t {
28
29 /**
30 * Public tnc_ifmap_listener_t interface.
31 */
32 tnc_ifmap_listener_t public;
33
34 /**
35 * TNC IF-MAP 2.0 SOAP interface
36 */
37 tnc_ifmap_soap_t *ifmap;
38
39 };
40
41 /**
42 * Publish metadata of a single IKE_SA
43 */
44 static bool publish_ike_sa(private_tnc_ifmap_listener_t *this,
45 ike_sa_t *ike_sa, bool up)
46 {
47 u_int32_t ike_sa_id;
48 identification_t *id;
49 host_t *host;
50
51 ike_sa_id = ike_sa->get_unique_id(ike_sa);
52 id = ike_sa->get_other_id(ike_sa);
53 host = ike_sa->get_other_host(ike_sa);
54
55 return this->ifmap->publish(this->ifmap, ike_sa_id, id, host, up);
56 }
57
58 /**
59 * Publish all IKE_SA metadata
60 */
61 static bool reload_metadata(private_tnc_ifmap_listener_t *this)
62 {
63 enumerator_t *enumerator;
64 ike_sa_t *ike_sa;
65 bool success = TRUE;
66
67 enumerator = charon->controller->create_ike_sa_enumerator(
68 charon->controller, FALSE);
69 while (enumerator->enumerate(enumerator, &ike_sa))
70 {
71 if (ike_sa->get_state(ike_sa) != IKE_ESTABLISHED)
72 {
73 continue;
74 }
75 if (!publish_ike_sa(this, ike_sa, TRUE))
76 {
77 success = FALSE;
78 break;
79 }
80 }
81 enumerator->destroy(enumerator);
82
83 return success;
84 }
85
86 METHOD(listener_t, ike_updown, bool,
87 private_tnc_ifmap_listener_t *this, ike_sa_t *ike_sa, bool up)
88 {
89 publish_ike_sa(this, ike_sa, up);
90
91 return TRUE;
92 }
93
94 METHOD(tnc_ifmap_listener_t, destroy, void,
95 private_tnc_ifmap_listener_t *this)
96 {
97 DESTROY_IF(this->ifmap);
98 free(this);
99 }
100
101 /**
102 * See header
103 */
104 tnc_ifmap_listener_t *tnc_ifmap_listener_create(bool reload)
105 {
106 private_tnc_ifmap_listener_t *this;
107
108 INIT(this,
109 .public = {
110 .listener = {
111 .ike_updown = _ike_updown,
112 },
113 .destroy = _destroy,
114 },
115 .ifmap = tnc_ifmap_soap_create(),
116 );
117
118 if (!this->ifmap)
119 {
120 destroy(this);
121 return NULL;
122 }
123 if (!this->ifmap->newSession(this->ifmap))
124 {
125 destroy(this);
126 return NULL;
127 }
128 if (!this->ifmap->purgePublisher(this->ifmap))
129 {
130 destroy(this);
131 return NULL;
132 }
133 if (reload)
134 {
135 if (!reload_metadata(this))
136 {
137 destroy(this);
138 return NULL;
139 }
140 }
141
142 return &this->public;
143 }
144