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"
19 #include <utils/linked_list.h>
20 #include <threading/rwlock.h>
22 typedef struct private_imc_agent_t private_imc_agent_t
;
25 * Private data of an imc_agent_t object.
27 struct private_imc_agent_t
{
30 * Public members of imc_agent_t
45 * ID of IMC as assigned by TNCC
50 * list of TNCC connection entries
52 linked_list_t
*connections
;
55 * rwlock to lock TNCS connection entries
57 rwlock_t
*connection_lock
;
60 * Inform a TNCS about the set of message types the IMC is able to receive
62 * @param imc_id IMC ID assigned by TNCC
63 * @param supported_types list of supported message types
64 * @param type_count number of list elements
65 * @return TNC result code
67 TNC_Result (*report_message_types
)(TNC_IMCID imc_id
,
68 TNC_MessageTypeList supported_types
,
69 TNC_UInt32 type_count
);
72 * Call when an IMC-IMC message is to be sent
74 * @param imc_id IMC ID assigned by TNCC
75 * @param connection_id network connection ID assigned by TNCC
76 * @param msg message to send
77 * @param msg_len message length in bytes
78 * @param msg_type message type
79 * @return TNC result code
81 TNC_Result (*send_message
)(TNC_IMCID imc_id
,
82 TNC_ConnectionID connection_id
,
83 TNC_BufferReference msg
,
85 TNC_MessageType msg_type
);
88 METHOD(imc_agent_t
, bind_functions
, TNC_Result
,
89 private_imc_agent_t
*this, TNC_TNCC_BindFunctionPointer bind_function
)
93 DBG1(DBG_IMC
, "TNC client failed to provide bind function");
94 return TNC_RESULT_INVALID_PARAMETER
;
96 if (bind_function(this->id
, "TNC_TNCC_ReportMessageTypes",
97 (void**)&this->report_message_types
) != TNC_RESULT_SUCCESS
)
99 this->report_message_types
= NULL
;
101 if (bind_function(this->id
, "TNC_TNCC_RequestHandshakeRetry",
102 (void**)&this->public.request_handshake_retry
) != TNC_RESULT_SUCCESS
)
104 this->public.request_handshake_retry
= NULL
;
106 if (bind_function(this->id
, "TNC_TNCC_SendMessage",
107 (void**)&this->send_message
) != TNC_RESULT_SUCCESS
)
109 this->send_message
= NULL
;
111 DBG2(DBG_IMC
, "IMC %u \"%s\" provided with bind function",
112 this->id
, this->name
);
114 if (this->report_message_types
)
116 this->report_message_types(this->id
, &this->type
, 1);
118 return TNC_RESULT_SUCCESS
;
122 * finds a connection state based on its Connection ID
124 static imc_state_t
* find_connection(private_imc_agent_t
*this,
127 enumerator_t
*enumerator
;
128 imc_state_t
*state
, *found
= NULL
;
130 this->connection_lock
->read_lock(this->connection_lock
);
131 enumerator
= this->connections
->create_enumerator(this->connections
);
132 while (enumerator
->enumerate(enumerator
, &state
))
134 if (id
== state
->get_connection_id(state
))
140 enumerator
->destroy(enumerator
);
141 this->connection_lock
->unlock(this->connection_lock
);
147 * delete a connection state with a given Connection ID
149 static bool delete_connection(private_imc_agent_t
*this, TNC_ConnectionID id
)
151 enumerator_t
*enumerator
;
155 this->connection_lock
->write_lock(this->connection_lock
);
156 enumerator
= this->connections
->create_enumerator(this->connections
);
157 while (enumerator
->enumerate(enumerator
, &state
))
159 if (id
== state
->get_connection_id(state
))
162 state
->destroy(state
);
163 this->connections
->remove_at(this->connections
, enumerator
);
167 enumerator
->destroy(enumerator
);
168 this->connection_lock
->unlock(this->connection_lock
);
173 METHOD(imc_agent_t
, create_state
, TNC_Result
,
174 private_imc_agent_t
*this, imc_state_t
*state
)
176 TNC_ConnectionID connection_id
;
178 connection_id
= state
->get_connection_id(state
);
179 if (find_connection(this, connection_id
))
181 DBG1(DBG_IMC
, "IMC %u \"%s\" already created a state for Connection ID %u",
182 this->id
, this->name
, connection_id
);
183 state
->destroy(state
);
184 return TNC_RESULT_OTHER
;
186 this->connection_lock
->write_lock(this->connection_lock
);
187 this->connections
->insert_last(this->connections
, state
);
188 this->connection_lock
->unlock(this->connection_lock
);
189 DBG2(DBG_IMC
, "IMC %u \"%s\" created a state for Connection ID %u",
190 this->id
, this->name
, connection_id
);
191 return TNC_RESULT_SUCCESS
;
194 METHOD(imc_agent_t
, delete_state
, TNC_Result
,
195 private_imc_agent_t
*this, TNC_ConnectionID connection_id
)
197 if (!delete_connection(this, connection_id
))
199 DBG1(DBG_IMC
, "IMC %u \"%s\" has no state for Connection ID %u",
200 this->id
, this->name
, connection_id
);
201 return TNC_RESULT_FATAL
;
203 DBG2(DBG_IMC
, "IMC %u \"%s\" deleted the state of Connection ID %u",
204 this->id
, this->name
, connection_id
);
205 return TNC_RESULT_SUCCESS
;
208 METHOD(imc_agent_t
, change_state
, TNC_Result
,
209 private_imc_agent_t
*this, TNC_ConnectionID connection_id
,
210 TNC_ConnectionState new_state
)
216 case TNC_CONNECTION_STATE_HANDSHAKE
:
217 case TNC_CONNECTION_STATE_ACCESS_ALLOWED
:
218 case TNC_CONNECTION_STATE_ACCESS_ISOLATED
:
219 case TNC_CONNECTION_STATE_ACCESS_NONE
:
220 state
= find_connection(this, connection_id
);
223 DBG1(DBG_IMC
, "IMC %u \"%s\" has no state for Connection ID %u",
224 this->id
, this->name
, connection_id
);
225 return TNC_RESULT_FATAL
;
227 state
->change_state(state
, new_state
);
228 DBG2(DBG_IMC
, "IMC %u \"%s\" changed state of Connection ID %u to '%N'",
229 this->id
, this->name
, connection_id
,
230 TNC_Connection_State_names
, new_state
);
232 case TNC_CONNECTION_STATE_CREATE
:
233 DBG1(DBG_IMC
, "state '%N' should be handled by create_state()",
234 TNC_Connection_State_names
, new_state
);
235 return TNC_RESULT_FATAL
;
236 case TNC_CONNECTION_STATE_DELETE
:
237 DBG1(DBG_IMC
, "state '%N' should be handled by delete_state()",
238 TNC_Connection_State_names
, new_state
);
239 return TNC_RESULT_FATAL
;
241 DBG1(DBG_IMC
, "IMC %u \"%s\" was notified of unknown state %u "
242 "for Connection ID %u",
243 this->id
, this->name
, new_state
, connection_id
);
244 return TNC_RESULT_INVALID_PARAMETER
;
246 return TNC_RESULT_SUCCESS
;
249 METHOD(imc_agent_t
, get_state
, bool,
250 private_imc_agent_t
*this, TNC_ConnectionID connection_id
,
253 *state
= find_connection(this, connection_id
);
256 DBG1(DBG_IMC
, "IMC %u \"%s\" has no state for Connection ID %u",
257 this->id
, this->name
, connection_id
);
263 METHOD(imc_agent_t
, send_message
, TNC_Result
,
264 private_imc_agent_t
*this, TNC_ConnectionID connection_id
, chunk_t msg
)
266 if (!this->send_message
)
268 return TNC_RESULT_FATAL
;
270 return this->send_message(this->id
, connection_id
, msg
.ptr
, msg
.len
,
274 METHOD(imc_agent_t
, receive_message
, TNC_Result
,
275 private_imc_agent_t
*this, TNC_ConnectionID connection_id
, chunk_t msg
,
276 TNC_MessageType msg_type
, pa_tnc_msg_t
**pa_tnc_msg
)
278 pa_tnc_msg_t
*pa_msg
, *error_msg
;
279 pa_tnc_attr_t
*error_attr
;
280 enumerator_t
*enumerator
;
283 DBG2(DBG_IMV
, "IMC %u \"%s\" received message type 0x%08x for Connection ID %u",
284 this->id
, this->name
, msg_type
, connection_id
);
287 pa_msg
= pa_tnc_msg_create_from_data(msg
);
289 switch (pa_msg
->process(pa_msg
))
292 *pa_tnc_msg
= pa_msg
;
295 if (!this->send_message
)
297 /* TNCC doen't have a SendMessage() function */
298 return TNC_RESULT_FATAL
;
301 /* build error message */
302 error_msg
= pa_tnc_msg_create();
303 enumerator
= pa_msg
->create_error_enumerator(pa_msg
);
304 while (enumerator
->enumerate(enumerator
, &error_attr
))
306 error_msg
->add_attribute(error_msg
,
307 error_attr
->get_ref(error_attr
));
309 enumerator
->destroy(enumerator
);
310 error_msg
->build(error_msg
);
312 /* send error message */
313 msg
= error_msg
->get_encoding(error_msg
);
314 result
= this->send_message(this->id
, connection_id
,
315 msg
.ptr
, msg
.len
, msg_type
);
318 error_msg
->destroy(error_msg
);
319 pa_msg
->destroy(pa_msg
);
323 pa_msg
->destroy(pa_msg
);
324 return TNC_RESULT_FATAL
;
326 return TNC_RESULT_SUCCESS
;
329 METHOD(imc_agent_t
, destroy
, void,
330 private_imc_agent_t
*this)
332 DBG1(DBG_IMC
, "IMC %u \"%s\" terminated", this->id
, this->name
);
333 this->connections
->destroy_function(this->connections
, free
);
334 this->connection_lock
->destroy(this->connection_lock
);
337 /* decrease the reference count or terminate */
342 * Described in header.
344 imc_agent_t
*imc_agent_create(const char *name
,
345 pen_t vendor_id
, u_int32_t subtype
,
346 TNC_IMCID id
, TNC_Version
*actual_version
)
348 private_imc_agent_t
*this;
350 /* initialize or increase the reference count */
358 .bind_functions
= _bind_functions
,
359 .create_state
= _create_state
,
360 .delete_state
= _delete_state
,
361 .change_state
= _change_state
,
362 .get_state
= _get_state
,
363 .send_message
= _send_message
,
364 .receive_message
= _receive_message
,
368 .type
= (vendor_id
<< 8) | (subtype
&& 0xff),
370 .connections
= linked_list_create(),
371 .connection_lock
= rwlock_create(RWLOCK_TYPE_DEFAULT
),
374 *actual_version
= TNC_IFIMC_VERSION_1
;
375 DBG1(DBG_IMC
, "IMC %u \"%s\" initialized", this->id
, this->name
);
377 return &this->public;