2 * Copyright (C) 2011 Andreas Steffen, HSR Hochschule fuer Technik Rapperswil
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 #include "imc_agent.h"
18 #include <tncif_names.h>
21 #include <utils/linked_list.h>
22 #include <threading/rwlock.h>
24 typedef struct private_imc_agent_t private_imc_agent_t
;
27 * Private data of an imc_agent_t object.
29 struct private_imc_agent_t
{
32 * Public members of imc_agent_t
42 * message vendor ID of IMC
44 TNC_VendorID vendor_id
;
47 * message subtype of IMC
49 TNC_MessageSubtype subtype
;
52 * ID of IMC as assigned by TNCC
57 * list of TNCC connection entries
59 linked_list_t
*connections
;
62 * rwlock to lock TNCC connection entries
64 rwlock_t
*connection_lock
;
67 * Inform a TNCC about the set of message types the IMC is able to receive
69 * @param imc_id IMC ID assigned by TNCC
70 * @param supported_types list of supported message types
71 * @param type_count number of list elements
72 * @return TNC result code
74 TNC_Result (*report_message_types
)(TNC_IMCID imc_id
,
75 TNC_MessageTypeList supported_types
,
76 TNC_UInt32 type_count
);
79 * Inform a TNCC about the set of message types the IMC is able to receive
81 * @param imc_id IMC ID assigned by TNCC
82 * @param supported_vids list of supported message vendor IDs
83 * @param supported_subtypes list of supported message subtypes
84 * @param type_count number of list elements
85 * @return TNC result code
87 TNC_Result (*report_message_types_long
)(TNC_IMCID imc_id
,
88 TNC_VendorIDList supported_vids
,
89 TNC_MessageSubtypeList supported_subtypes
,
90 TNC_UInt32 type_count
);
93 * Call when an IMC-IMC message is to be sent
95 * @param imc_id IMC ID assigned by TNCC
96 * @param connection_id network connection ID assigned by TNCC
97 * @param msg message to send
98 * @param msg_len message length in bytes
99 * @param msg_type message type
100 * @return TNC result code
102 TNC_Result (*send_message
)(TNC_IMCID imc_id
,
103 TNC_ConnectionID connection_id
,
104 TNC_BufferReference msg
,
106 TNC_MessageType msg_type
);
109 METHOD(imc_agent_t
, bind_functions
, TNC_Result
,
110 private_imc_agent_t
*this, TNC_TNCC_BindFunctionPointer bind_function
)
114 DBG1(DBG_IMC
, "TNC client failed to provide bind function");
115 return TNC_RESULT_INVALID_PARAMETER
;
117 if (bind_function(this->id
, "TNC_TNCC_ReportMessageTypes",
118 (void**)&this->report_message_types
) != TNC_RESULT_SUCCESS
)
120 this->report_message_types
= NULL
;
122 if (bind_function(this->id
, "TNC_TNCC_ReportMessageTypesLong",
123 (void**)&this->report_message_types_long
) != TNC_RESULT_SUCCESS
)
125 this->report_message_types_long
= NULL
;
127 if (bind_function(this->id
, "TNC_TNCC_RequestHandshakeRetry",
128 (void**)&this->public.request_handshake_retry
) != TNC_RESULT_SUCCESS
)
130 this->public.request_handshake_retry
= NULL
;
132 if (bind_function(this->id
, "TNC_TNCC_SendMessage",
133 (void**)&this->send_message
) != TNC_RESULT_SUCCESS
)
135 this->send_message
= NULL
;
137 DBG2(DBG_IMC
, "IMC %u \"%s\" provided with bind function",
138 this->id
, this->name
);
140 if (this->report_message_types_long
)
142 this->report_message_types_long(this->id
, &this->vendor_id
,
145 else if (this->report_message_types
&&
146 this->vendor_id
<= TNC_VENDORID_ANY
&&
147 this->subtype
<= TNC_SUBTYPE_ANY
)
149 TNC_MessageType type
;
151 type
= (this->vendor_id
<< 8) | this->subtype
;
152 this->report_message_types(this->id
, &type
, 1);
154 return TNC_RESULT_SUCCESS
;
158 * finds a connection state based on its Connection ID
160 static imc_state_t
* find_connection(private_imc_agent_t
*this,
163 enumerator_t
*enumerator
;
164 imc_state_t
*state
, *found
= NULL
;
166 this->connection_lock
->read_lock(this->connection_lock
);
167 enumerator
= this->connections
->create_enumerator(this->connections
);
168 while (enumerator
->enumerate(enumerator
, &state
))
170 if (id
== state
->get_connection_id(state
))
176 enumerator
->destroy(enumerator
);
177 this->connection_lock
->unlock(this->connection_lock
);
183 * delete a connection state with a given Connection ID
185 static bool delete_connection(private_imc_agent_t
*this, TNC_ConnectionID id
)
187 enumerator_t
*enumerator
;
191 this->connection_lock
->write_lock(this->connection_lock
);
192 enumerator
= this->connections
->create_enumerator(this->connections
);
193 while (enumerator
->enumerate(enumerator
, &state
))
195 if (id
== state
->get_connection_id(state
))
198 state
->destroy(state
);
199 this->connections
->remove_at(this->connections
, enumerator
);
203 enumerator
->destroy(enumerator
);
204 this->connection_lock
->unlock(this->connection_lock
);
209 METHOD(imc_agent_t
, create_state
, TNC_Result
,
210 private_imc_agent_t
*this, imc_state_t
*state
)
212 TNC_ConnectionID connection_id
;
214 connection_id
= state
->get_connection_id(state
);
215 if (find_connection(this, connection_id
))
217 DBG1(DBG_IMC
, "IMC %u \"%s\" already created a state for Connection ID %u",
218 this->id
, this->name
, connection_id
);
219 state
->destroy(state
);
220 return TNC_RESULT_OTHER
;
222 this->connection_lock
->write_lock(this->connection_lock
);
223 this->connections
->insert_last(this->connections
, state
);
224 this->connection_lock
->unlock(this->connection_lock
);
225 DBG2(DBG_IMC
, "IMC %u \"%s\" created a state for Connection ID %u",
226 this->id
, this->name
, connection_id
);
227 return TNC_RESULT_SUCCESS
;
230 METHOD(imc_agent_t
, delete_state
, TNC_Result
,
231 private_imc_agent_t
*this, TNC_ConnectionID connection_id
)
233 if (!delete_connection(this, connection_id
))
235 DBG1(DBG_IMC
, "IMC %u \"%s\" has no state for Connection ID %u",
236 this->id
, this->name
, connection_id
);
237 return TNC_RESULT_FATAL
;
239 DBG2(DBG_IMC
, "IMC %u \"%s\" deleted the state of Connection ID %u",
240 this->id
, this->name
, connection_id
);
241 return TNC_RESULT_SUCCESS
;
244 METHOD(imc_agent_t
, change_state
, TNC_Result
,
245 private_imc_agent_t
*this, TNC_ConnectionID connection_id
,
246 TNC_ConnectionState new_state
,
247 imc_state_t
**state_p
)
253 case TNC_CONNECTION_STATE_HANDSHAKE
:
254 case TNC_CONNECTION_STATE_ACCESS_ALLOWED
:
255 case TNC_CONNECTION_STATE_ACCESS_ISOLATED
:
256 case TNC_CONNECTION_STATE_ACCESS_NONE
:
257 state
= find_connection(this, connection_id
);
261 DBG1(DBG_IMC
, "IMC %u \"%s\" has no state for Connection ID %u",
262 this->id
, this->name
, connection_id
);
263 return TNC_RESULT_FATAL
;
265 state
->change_state(state
, new_state
);
266 DBG2(DBG_IMC
, "IMC %u \"%s\" changed state of Connection ID %u to '%N'",
267 this->id
, this->name
, connection_id
,
268 TNC_Connection_State_names
, new_state
);
274 case TNC_CONNECTION_STATE_CREATE
:
275 DBG1(DBG_IMC
, "state '%N' should be handled by create_state()",
276 TNC_Connection_State_names
, new_state
);
277 return TNC_RESULT_FATAL
;
278 case TNC_CONNECTION_STATE_DELETE
:
279 DBG1(DBG_IMC
, "state '%N' should be handled by delete_state()",
280 TNC_Connection_State_names
, new_state
);
281 return TNC_RESULT_FATAL
;
283 DBG1(DBG_IMC
, "IMC %u \"%s\" was notified of unknown state %u "
284 "for Connection ID %u",
285 this->id
, this->name
, new_state
, connection_id
);
286 return TNC_RESULT_INVALID_PARAMETER
;
288 return TNC_RESULT_SUCCESS
;
291 METHOD(imc_agent_t
, get_state
, bool,
292 private_imc_agent_t
*this, TNC_ConnectionID connection_id
,
295 *state
= find_connection(this, connection_id
);
298 DBG1(DBG_IMC
, "IMC %u \"%s\" has no state for Connection ID %u",
299 this->id
, this->name
, connection_id
);
305 METHOD(imc_agent_t
, send_message
, TNC_Result
,
306 private_imc_agent_t
*this, TNC_ConnectionID connection_id
, chunk_t msg
)
308 TNC_MessageType type
;
310 if (!this->send_message
)
312 return TNC_RESULT_FATAL
;
314 type
= (this->vendor_id
<< 8) | this->subtype
;
315 return this->send_message(this->id
, connection_id
, msg
.ptr
, msg
.len
, type
);
318 METHOD(imc_agent_t
, receive_message
, TNC_Result
,
319 private_imc_agent_t
*this, TNC_ConnectionID connection_id
, chunk_t msg
,
320 TNC_MessageType msg_type
, pa_tnc_msg_t
**pa_tnc_msg
)
322 pa_tnc_msg_t
*pa_msg
, *error_msg
;
323 pa_tnc_attr_t
*error_attr
;
324 enumerator_t
*enumerator
;
327 DBG2(DBG_IMV
, "IMC %u \"%s\" received message type 0x%08x for Connection ID %u",
328 this->id
, this->name
, msg_type
, connection_id
);
331 pa_msg
= pa_tnc_msg_create_from_data(msg
);
333 switch (pa_msg
->process(pa_msg
))
336 *pa_tnc_msg
= pa_msg
;
339 if (!this->send_message
)
341 /* TNCC doen't have a SendMessage() function */
342 return TNC_RESULT_FATAL
;
345 /* build error message */
346 error_msg
= pa_tnc_msg_create();
347 enumerator
= pa_msg
->create_error_enumerator(pa_msg
);
348 while (enumerator
->enumerate(enumerator
, &error_attr
))
350 error_msg
->add_attribute(error_msg
,
351 error_attr
->get_ref(error_attr
));
353 enumerator
->destroy(enumerator
);
354 error_msg
->build(error_msg
);
356 /* send error message */
357 msg
= error_msg
->get_encoding(error_msg
);
358 result
= this->send_message(this->id
, connection_id
,
359 msg
.ptr
, msg
.len
, msg_type
);
362 error_msg
->destroy(error_msg
);
363 pa_msg
->destroy(pa_msg
);
367 pa_msg
->destroy(pa_msg
);
368 return TNC_RESULT_FATAL
;
370 return TNC_RESULT_SUCCESS
;
373 METHOD(imc_agent_t
, destroy
, void,
374 private_imc_agent_t
*this)
376 DBG1(DBG_IMC
, "IMC %u \"%s\" terminated", this->id
, this->name
);
377 this->connections
->destroy_function(this->connections
, free
);
378 this->connection_lock
->destroy(this->connection_lock
);
381 /* decrease the reference count or terminate */
386 * Described in header.
388 imc_agent_t
*imc_agent_create(const char *name
,
389 pen_t vendor_id
, u_int32_t subtype
,
390 TNC_IMCID id
, TNC_Version
*actual_version
)
392 private_imc_agent_t
*this;
394 /* initialize or increase the reference count */
402 .bind_functions
= _bind_functions
,
403 .create_state
= _create_state
,
404 .delete_state
= _delete_state
,
405 .change_state
= _change_state
,
406 .get_state
= _get_state
,
407 .send_message
= _send_message
,
408 .receive_message
= _receive_message
,
412 .vendor_id
= vendor_id
,
415 .connections
= linked_list_create(),
416 .connection_lock
= rwlock_create(RWLOCK_TYPE_DEFAULT
),
419 *actual_version
= TNC_IFIMC_VERSION_1
;
420 DBG1(DBG_IMC
, "IMC %u \"%s\" initialized", this->id
, this->name
);
422 return &this->public;