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
,
162 tnccs_provide_recommendation_t provide_recommendation
)
164 tnccs_connection_entry_t
*entry
= malloc_thing(tnccs_connection_entry_t
);
166 entry
->id
= ++this->connection_id
;
167 entry
->tnccs
= tnccs
;
168 entry
->send_message
= send_message
;
169 entry
->provide_recommendation
= provide_recommendation
;
171 this->lock
->write_lock(this->lock
);
172 this->connections
->insert_last(this->connections
, entry
);
173 this->lock
->unlock(this->lock
);
175 DBG1(DBG_TNC
, "assigned TNCCS Connection ID %u", entry
->id
);
179 METHOD(tnccs_manager_t
, remove_connection
, void,
180 private_tnccs_manager_t
*this, TNC_ConnectionID id
)
182 enumerator_t
*enumerator
;
183 tnccs_connection_entry_t
*entry
;
185 this->lock
->write_lock(this->lock
);
186 enumerator
= this->connections
->create_enumerator(this->connections
);
187 while (enumerator
->enumerate(enumerator
, &entry
))
191 this->connections
->remove_at(this->connections
, enumerator
);
193 DBG1(DBG_TNC
, "removed TNCCS Connection ID %u", id
);
196 enumerator
->destroy(enumerator
);
197 this->lock
->unlock(this->lock
);
200 METHOD(tnccs_manager_t
, send_message
, TNC_Result
,
201 private_tnccs_manager_t
*this, TNC_ConnectionID id
,
202 TNC_BufferReference message
,
203 TNC_UInt32 message_len
,
204 TNC_MessageType message_type
)
206 enumerator_t
*enumerator
;
207 tnccs_connection_entry_t
*entry
;
208 tnccs_send_message_t send_message
= NULL
;
209 tnccs_t
*tnccs
= NULL
;
211 this->lock
->write_lock(this->lock
);
212 enumerator
= this->connections
->create_enumerator(this->connections
);
213 while (enumerator
->enumerate(enumerator
, &entry
))
217 tnccs
= entry
->tnccs
;
218 send_message
= entry
->send_message
;
222 enumerator
->destroy(enumerator
);
223 this->lock
->unlock(this->lock
);
225 if (tnccs
&& send_message
)
227 send_message(tnccs
, message
, message_len
, message_type
);
228 return TNC_RESULT_SUCCESS
;
230 return TNC_RESULT_FATAL
;
233 METHOD(tnccs_manager_t
, provide_recommendation
, TNC_Result
,
234 private_tnccs_manager_t
*this, TNC_IMVID imv_id
,
236 TNC_IMV_Action_Recommendation recommendation
,
237 TNC_IMV_Evaluation_Result evaluation
)
239 enumerator_t
*enumerator
;
240 tnccs_connection_entry_t
*entry
;
241 tnccs_provide_recommendation_t provide_recommendation
= NULL
;
242 tnccs_t
*tnccs
= NULL
;
244 this->lock
->write_lock(this->lock
);
245 enumerator
= this->connections
->create_enumerator(this->connections
);
246 while (enumerator
->enumerate(enumerator
, &entry
))
250 tnccs
= entry
->tnccs
;
251 provide_recommendation
= entry
->provide_recommendation
;
255 enumerator
->destroy(enumerator
);
256 this->lock
->unlock(this->lock
);
258 if (tnccs
&& provide_recommendation
)
260 provide_recommendation(tnccs
, imv_id
, recommendation
, evaluation
);
261 return TNC_RESULT_SUCCESS
;
263 return TNC_RESULT_FATAL
;
266 METHOD(tnccs_manager_t
, destroy
, void,
267 private_tnccs_manager_t
*this)
269 this->protocols
->destroy_function(this->protocols
, free
);
270 this->connections
->destroy_function(this->connections
, free
);
271 this->lock
->destroy(this->lock
);
278 tnccs_manager_t
*tnccs_manager_create()
280 private_tnccs_manager_t
*this;
284 .add_method
= _add_method
,
285 .remove_method
= _remove_method
,
286 .create_instance
= _create_instance
,
287 .create_connection
= _create_connection
,
288 .remove_connection
= _remove_connection
,
289 .send_message
= _send_message
,
290 .provide_recommendation
= _provide_recommendation
,
293 .protocols
= linked_list_create(),
294 .connections
= linked_list_create(),
295 .lock
= rwlock_create(RWLOCK_TYPE_DEFAULT
),
298 return &this->public;