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
;
64 * private data of tnccs_manager
66 struct private_tnccs_manager_t
{
71 tnccs_manager_t
public;
74 * list of TNCCS protocol entries
76 linked_list_t
*protocols
;
79 * connection ID counter
81 TNC_ConnectionID connection_id
;
84 * list of TNCCS connection entries
86 linked_list_t
*connections
;
89 * rwlock to lock TNCCS protocol and connection entries
95 METHOD(tnccs_manager_t
, add_method
, void,
96 private_tnccs_manager_t
*this, tnccs_type_t type
,
97 tnccs_constructor_t constructor
)
99 tnccs_entry_t
*entry
= malloc_thing(tnccs_entry_t
);
102 entry
->constructor
= constructor
;
104 this->lock
->write_lock(this->lock
);
105 this->protocols
->insert_last(this->protocols
, entry
);
106 this->lock
->unlock(this->lock
);
109 METHOD(tnccs_manager_t
, remove_method
, void,
110 private_tnccs_manager_t
*this, tnccs_constructor_t constructor
)
112 enumerator_t
*enumerator
;
113 tnccs_entry_t
*entry
;
115 this->lock
->write_lock(this->lock
);
116 enumerator
= this->protocols
->create_enumerator(this->protocols
);
117 while (enumerator
->enumerate(enumerator
, &entry
))
119 if (constructor
== entry
->constructor
)
121 this->protocols
->remove_at(this->protocols
, enumerator
);
125 enumerator
->destroy(enumerator
);
126 this->lock
->unlock(this->lock
);
129 METHOD(tnccs_manager_t
, create_instance
, tnccs_t
*,
130 private_tnccs_manager_t
*this, tnccs_type_t type
, bool is_server
)
132 enumerator_t
*enumerator
;
133 tnccs_entry_t
*entry
;
134 tnccs_t
*protocol
= NULL
;
136 this->lock
->read_lock(this->lock
);
137 enumerator
= this->protocols
->create_enumerator(this->protocols
);
138 while (enumerator
->enumerate(enumerator
, &entry
))
140 if (type
== entry
->type
)
142 protocol
= entry
->constructor(is_server
);
149 enumerator
->destroy(enumerator
);
150 this->lock
->unlock(this->lock
);
154 METHOD(tnccs_manager_t
, create_connection
, TNC_ConnectionID
,
155 private_tnccs_manager_t
*this, tnccs_t
*tnccs
,
156 tnccs_send_message_t send_message
)
158 tnccs_connection_entry_t
*entry
= malloc_thing(tnccs_connection_entry_t
);
160 entry
->id
= ++this->connection_id
;
161 entry
->tnccs
= tnccs
;
162 entry
->send_message
= send_message
;
164 this->lock
->write_lock(this->lock
);
165 this->connections
->insert_last(this->connections
, entry
);
166 this->lock
->unlock(this->lock
);
168 DBG1(DBG_TNC
, "assigned TNCCS Connection ID %u", entry
->id
);
172 METHOD(tnccs_manager_t
, remove_connection
, void,
173 private_tnccs_manager_t
*this, TNC_ConnectionID id
)
175 enumerator_t
*enumerator
;
176 tnccs_connection_entry_t
*entry
;
178 this->lock
->write_lock(this->lock
);
179 enumerator
= this->connections
->create_enumerator(this->connections
);
180 while (enumerator
->enumerate(enumerator
, &entry
))
184 this->connections
->remove_at(this->connections
, enumerator
);
186 DBG1(DBG_TNC
, "removed TNCCS Connection ID %u", id
);
189 enumerator
->destroy(enumerator
);
190 this->lock
->unlock(this->lock
);
193 METHOD(tnccs_manager_t
, send_message
, TNC_Result
,
194 private_tnccs_manager_t
*this, TNC_ConnectionID id
,
195 TNC_BufferReference message
,
196 TNC_UInt32 message_len
,
197 TNC_MessageType message_type
)
199 enumerator_t
*enumerator
;
200 tnccs_connection_entry_t
*entry
;
201 tnccs_send_message_t send_message
;
202 tnccs_t
*tnccs
= NULL
;
204 this->lock
->write_lock(this->lock
);
205 enumerator
= this->connections
->create_enumerator(this->connections
);
206 while (enumerator
->enumerate(enumerator
, &entry
))
210 tnccs
= entry
->tnccs
;
211 send_message
= entry
->send_message
;
215 enumerator
->destroy(enumerator
);
216 this->lock
->unlock(this->lock
);
220 send_message(tnccs
, message
, message_len
, message_type
);
221 return TNC_RESULT_SUCCESS
;
223 return TNC_RESULT_FATAL
;
226 METHOD(tnccs_manager_t
, destroy
, void,
227 private_tnccs_manager_t
*this)
229 this->protocols
->destroy_function(this->protocols
, free
);
230 this->connections
->destroy_function(this->connections
, free
);
231 this->lock
->destroy(this->lock
);
238 tnccs_manager_t
*tnccs_manager_create()
240 private_tnccs_manager_t
*this;
244 .add_method
= _add_method
,
245 .remove_method
= _remove_method
,
246 .create_instance
= _create_instance
,
247 .create_connection
= _create_connection
,
248 .remove_connection
= _remove_connection
,
249 .send_message
= _send_message
,
252 .protocols
= linked_list_create(),
253 .connections
= linked_list_create(),
254 .lock
= rwlock_create(RWLOCK_TYPE_DEFAULT
),
257 return &this->public;