implemented provide_recommendation callback function
[strongswan.git] / src / libcharon / tnc / tnccs / tnccs_manager.c
1 /*
2 * Copyright (C) 2010 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 "tnccs_manager.h"
17
18 #include <debug.h>
19 #include <utils/linked_list.h>
20 #include <threading/rwlock.h>
21
22 typedef struct private_tnccs_manager_t private_tnccs_manager_t;
23 typedef struct tnccs_entry_t tnccs_entry_t;
24 typedef struct tnccs_connection_entry_t tnccs_connection_entry_t;
25
26 /**
27 * TNCCS constructor entry
28 */
29 struct tnccs_entry_t {
30
31 /**
32 * TNCCS protocol type
33 */
34 tnccs_type_t type;
35
36 /**
37 * constructor function to create instance
38 */
39 tnccs_constructor_t constructor;
40 };
41
42 /**
43 * TNCCS connection entry
44 */
45 struct tnccs_connection_entry_t {
46
47 /**
48 * TNCCS connection ID
49 */
50 TNC_ConnectionID id;
51
52 /**
53 * TNCCS instance
54 */
55 tnccs_t *tnccs;
56
57 /** TNCCS send message function
58 *
59 */
60 tnccs_send_message_t send_message;
61
62 /** TNCS provide recommendation function
63 *
64 */
65 tnccs_provide_recommendation_t provide_recommendation;
66 };
67
68 /**
69 * private data of tnccs_manager
70 */
71 struct private_tnccs_manager_t {
72
73 /**
74 * public functions
75 */
76 tnccs_manager_t public;
77
78 /**
79 * list of TNCCS protocol entries
80 */
81 linked_list_t *protocols;
82
83 /**
84 * connection ID counter
85 */
86 TNC_ConnectionID connection_id;
87
88 /**
89 * list of TNCCS connection entries
90 */
91 linked_list_t *connections;
92
93 /**
94 * rwlock to lock TNCCS protocol and connection entries
95 */
96 rwlock_t *lock;
97
98 };
99
100 METHOD(tnccs_manager_t, add_method, void,
101 private_tnccs_manager_t *this, tnccs_type_t type,
102 tnccs_constructor_t constructor)
103 {
104 tnccs_entry_t *entry = malloc_thing(tnccs_entry_t);
105
106 entry->type = type;
107 entry->constructor = constructor;
108
109 this->lock->write_lock(this->lock);
110 this->protocols->insert_last(this->protocols, entry);
111 this->lock->unlock(this->lock);
112 }
113
114 METHOD(tnccs_manager_t, remove_method, void,
115 private_tnccs_manager_t *this, tnccs_constructor_t constructor)
116 {
117 enumerator_t *enumerator;
118 tnccs_entry_t *entry;
119
120 this->lock->write_lock(this->lock);
121 enumerator = this->protocols->create_enumerator(this->protocols);
122 while (enumerator->enumerate(enumerator, &entry))
123 {
124 if (constructor == entry->constructor)
125 {
126 this->protocols->remove_at(this->protocols, enumerator);
127 free(entry);
128 }
129 }
130 enumerator->destroy(enumerator);
131 this->lock->unlock(this->lock);
132 }
133
134 METHOD(tnccs_manager_t, create_instance, tnccs_t*,
135 private_tnccs_manager_t *this, tnccs_type_t type, bool is_server)
136 {
137 enumerator_t *enumerator;
138 tnccs_entry_t *entry;
139 tnccs_t *protocol = NULL;
140
141 this->lock->read_lock(this->lock);
142 enumerator = this->protocols->create_enumerator(this->protocols);
143 while (enumerator->enumerate(enumerator, &entry))
144 {
145 if (type == entry->type)
146 {
147 protocol = entry->constructor(is_server);
148 if (protocol)
149 {
150 break;
151 }
152 }
153 }
154 enumerator->destroy(enumerator);
155 this->lock->unlock(this->lock);
156 return protocol;
157 }
158
159 METHOD(tnccs_manager_t, create_connection, TNC_ConnectionID,
160 private_tnccs_manager_t *this, tnccs_t *tnccs,
161 tnccs_send_message_t send_message)
162 {
163 tnccs_connection_entry_t *entry = malloc_thing(tnccs_connection_entry_t);
164
165 entry->id = ++this->connection_id;
166 entry->tnccs = tnccs;
167 entry->send_message = send_message;
168
169 this->lock->write_lock(this->lock);
170 this->connections->insert_last(this->connections, entry);
171 this->lock->unlock(this->lock);
172
173 DBG1(DBG_TNC, "assigned TNCCS Connection ID %u", entry->id);
174 return entry->id;
175 }
176
177 METHOD(tnccs_manager_t, remove_connection, void,
178 private_tnccs_manager_t *this, TNC_ConnectionID id)
179 {
180 enumerator_t *enumerator;
181 tnccs_connection_entry_t *entry;
182
183 this->lock->write_lock(this->lock);
184 enumerator = this->connections->create_enumerator(this->connections);
185 while (enumerator->enumerate(enumerator, &entry))
186 {
187 if (id == entry->id)
188 {
189 this->connections->remove_at(this->connections, enumerator);
190 free(entry);
191 DBG1(DBG_TNC, "removed TNCCS Connection ID %u", id);
192 }
193 }
194 enumerator->destroy(enumerator);
195 this->lock->unlock(this->lock);
196 }
197
198 METHOD(tnccs_manager_t, send_message, TNC_Result,
199 private_tnccs_manager_t *this, TNC_ConnectionID id,
200 TNC_BufferReference message,
201 TNC_UInt32 message_len,
202 TNC_MessageType message_type)
203 {
204 enumerator_t *enumerator;
205 tnccs_connection_entry_t *entry;
206 tnccs_send_message_t send_message = NULL;
207 tnccs_t *tnccs = NULL;
208
209 this->lock->write_lock(this->lock);
210 enumerator = this->connections->create_enumerator(this->connections);
211 while (enumerator->enumerate(enumerator, &entry))
212 {
213 if (id == entry->id)
214 {
215 tnccs = entry->tnccs;
216 send_message = entry->send_message;
217 break;
218 }
219 }
220 enumerator->destroy(enumerator);
221 this->lock->unlock(this->lock);
222
223 if (tnccs && send_message)
224 {
225 send_message(tnccs, message, message_len, message_type);
226 return TNC_RESULT_SUCCESS;
227 }
228 return TNC_RESULT_FATAL;
229 }
230
231 METHOD(tnccs_manager_t, provide_recommendation, TNC_Result,
232 private_tnccs_manager_t *this, TNC_IMVID imv_id,
233 TNC_ConnectionID id,
234 TNC_IMV_Action_Recommendation recommendation,
235 TNC_IMV_Evaluation_Result evaluation)
236 {
237 enumerator_t *enumerator;
238 tnccs_connection_entry_t *entry;
239 tnccs_provide_recommendation_t provide_recommendation = NULL;
240 tnccs_t *tnccs = NULL;
241
242 this->lock->write_lock(this->lock);
243 enumerator = this->connections->create_enumerator(this->connections);
244 while (enumerator->enumerate(enumerator, &entry))
245 {
246 if (id == entry->id)
247 {
248 tnccs = entry->tnccs;
249 provide_recommendation = entry->provide_recommendation;
250 break;
251 }
252 }
253 enumerator->destroy(enumerator);
254 this->lock->unlock(this->lock);
255
256 if (tnccs && provide_recommendation)
257 {
258 provide_recommendation(tnccs, imv_id, recommendation, evaluation);
259 return TNC_RESULT_SUCCESS;
260 }
261 return TNC_RESULT_FATAL;
262 }
263
264 METHOD(tnccs_manager_t, destroy, void,
265 private_tnccs_manager_t *this)
266 {
267 this->protocols->destroy_function(this->protocols, free);
268 this->connections->destroy_function(this->connections, free);
269 this->lock->destroy(this->lock);
270 free(this);
271 }
272
273 /*
274 * See header
275 */
276 tnccs_manager_t *tnccs_manager_create()
277 {
278 private_tnccs_manager_t *this;
279
280 INIT(this,
281 .public = {
282 .add_method = _add_method,
283 .remove_method = _remove_method,
284 .create_instance = _create_instance,
285 .create_connection = _create_connection,
286 .remove_connection = _remove_connection,
287 .send_message = _send_message,
288 .provide_recommendation = _provide_recommendation,
289 .destroy = _destroy,
290 },
291 .protocols = linked_list_create(),
292 .connections = linked_list_create(),
293 .lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
294 );
295
296 return &this->public;
297 }
298