2 * Copyright (C) 2010 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
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>.
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
16 #include "tnccs_manager.h"
19 #include <utils/linked_list.h>
20 #include <threading/rwlock.h>
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
;
27 * TNCCS constructor entry
29 struct tnccs_entry_t
{
37 * constructor function to create instance
39 tnccs_constructor_t constructor
;
43 * TNCCS connection entry
45 struct tnccs_connection_entry_t
{
57 /** TNCCS send message function
60 tnccs_send_message_t send_message
;
62 /** TNCS provide recommendation function
65 tnccs_provide_recommendation_t provide_recommendation
;
69 * private data of tnccs_manager
71 struct private_tnccs_manager_t
{
76 tnccs_manager_t
public;
79 * list of TNCCS protocol entries
81 linked_list_t
*protocols
;
84 * connection ID counter
86 TNC_ConnectionID connection_id
;
89 * list of TNCCS connection entries
91 linked_list_t
*connections
;
94 * rwlock to lock TNCCS protocol and connection entries
100 METHOD(tnccs_manager_t
, add_method
, void,
101 private_tnccs_manager_t
*this, tnccs_type_t type
,
102 tnccs_constructor_t constructor
)
104 tnccs_entry_t
*entry
= malloc_thing(tnccs_entry_t
);
107 entry
->constructor
= constructor
;
109 this->lock
->write_lock(this->lock
);
110 this->protocols
->insert_last(this->protocols
, entry
);
111 this->lock
->unlock(this->lock
);
114 METHOD(tnccs_manager_t
, remove_method
, void,
115 private_tnccs_manager_t
*this, tnccs_constructor_t constructor
)
117 enumerator_t
*enumerator
;
118 tnccs_entry_t
*entry
;
120 this->lock
->write_lock(this->lock
);
121 enumerator
= this->protocols
->create_enumerator(this->protocols
);
122 while (enumerator
->enumerate(enumerator
, &entry
))
124 if (constructor
== entry
->constructor
)
126 this->protocols
->remove_at(this->protocols
, enumerator
);
130 enumerator
->destroy(enumerator
);
131 this->lock
->unlock(this->lock
);
134 METHOD(tnccs_manager_t
, create_instance
, tnccs_t
*,
135 private_tnccs_manager_t
*this, tnccs_type_t type
, bool is_server
)
137 enumerator_t
*enumerator
;
138 tnccs_entry_t
*entry
;
139 tnccs_t
*protocol
= NULL
;
141 this->lock
->read_lock(this->lock
);
142 enumerator
= this->protocols
->create_enumerator(this->protocols
);
143 while (enumerator
->enumerate(enumerator
, &entry
))
145 if (type
== entry
->type
)
147 protocol
= entry
->constructor(is_server
);
154 enumerator
->destroy(enumerator
);
155 this->lock
->unlock(this->lock
);
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
)
163 tnccs_connection_entry_t
*entry
= malloc_thing(tnccs_connection_entry_t
);
165 entry
->id
= ++this->connection_id
;
166 entry
->tnccs
= tnccs
;
167 entry
->send_message
= send_message
;
169 this->lock
->write_lock(this->lock
);
170 this->connections
->insert_last(this->connections
, entry
);
171 this->lock
->unlock(this->lock
);
173 DBG1(DBG_TNC
, "assigned TNCCS Connection ID %u", entry
->id
);
177 METHOD(tnccs_manager_t
, remove_connection
, void,
178 private_tnccs_manager_t
*this, TNC_ConnectionID id
)
180 enumerator_t
*enumerator
;
181 tnccs_connection_entry_t
*entry
;
183 this->lock
->write_lock(this->lock
);
184 enumerator
= this->connections
->create_enumerator(this->connections
);
185 while (enumerator
->enumerate(enumerator
, &entry
))
189 this->connections
->remove_at(this->connections
, enumerator
);
191 DBG1(DBG_TNC
, "removed TNCCS Connection ID %u", id
);
194 enumerator
->destroy(enumerator
);
195 this->lock
->unlock(this->lock
);
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
)
204 enumerator_t
*enumerator
;
205 tnccs_connection_entry_t
*entry
;
206 tnccs_send_message_t send_message
= NULL
;
207 tnccs_t
*tnccs
= NULL
;
209 this->lock
->write_lock(this->lock
);
210 enumerator
= this->connections
->create_enumerator(this->connections
);
211 while (enumerator
->enumerate(enumerator
, &entry
))
215 tnccs
= entry
->tnccs
;
216 send_message
= entry
->send_message
;
220 enumerator
->destroy(enumerator
);
221 this->lock
->unlock(this->lock
);
223 if (tnccs
&& send_message
)
225 send_message(tnccs
, message
, message_len
, message_type
);
226 return TNC_RESULT_SUCCESS
;
228 return TNC_RESULT_FATAL
;
231 METHOD(tnccs_manager_t
, provide_recommendation
, TNC_Result
,
232 private_tnccs_manager_t
*this, TNC_IMVID imv_id
,
234 TNC_IMV_Action_Recommendation recommendation
,
235 TNC_IMV_Evaluation_Result evaluation
)
237 enumerator_t
*enumerator
;
238 tnccs_connection_entry_t
*entry
;
239 tnccs_provide_recommendation_t provide_recommendation
= NULL
;
240 tnccs_t
*tnccs
= NULL
;
242 this->lock
->write_lock(this->lock
);
243 enumerator
= this->connections
->create_enumerator(this->connections
);
244 while (enumerator
->enumerate(enumerator
, &entry
))
248 tnccs
= entry
->tnccs
;
249 provide_recommendation
= entry
->provide_recommendation
;
253 enumerator
->destroy(enumerator
);
254 this->lock
->unlock(this->lock
);
256 if (tnccs
&& provide_recommendation
)
258 provide_recommendation(tnccs
, imv_id
, recommendation
, evaluation
);
259 return TNC_RESULT_SUCCESS
;
261 return TNC_RESULT_FATAL
;
264 METHOD(tnccs_manager_t
, destroy
, void,
265 private_tnccs_manager_t
*this)
267 this->protocols
->destroy_function(this->protocols
, free
);
268 this->connections
->destroy_function(this->connections
, free
);
269 this->lock
->destroy(this->lock
);
276 tnccs_manager_t
*tnccs_manager_create()
278 private_tnccs_manager_t
*this;
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
,
291 .protocols
= linked_list_create(),
292 .connections
= linked_list_create(),
293 .lock
= rwlock_create(RWLOCK_TYPE_DEFAULT
),
296 return &this->public;