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
47 * ID of IMC as assigned by TNCC
52 * list of TNCC connection entries
54 linked_list_t
*connections
;
57 * rwlock to lock TNCS connection entries
59 rwlock_t
*connection_lock
;
62 * Inform a TNCS about the set of message types the IMC is able to receive
64 * @param imc_id IMC ID assigned by TNCC
65 * @param supported_types list of supported message types
66 * @param type_count number of list elements
67 * @return TNC result code
69 TNC_Result (*report_message_types
)(TNC_IMCID imc_id
,
70 TNC_MessageTypeList supported_types
,
71 TNC_UInt32 type_count
);
74 * Call when an IMC-IMC message is to be sent
76 * @param imc_id IMC ID assigned by TNCC
77 * @param connection_id network connection ID assigned by TNCC
78 * @param msg message to send
79 * @param msg_len message length in bytes
80 * @param msg_type message type
81 * @return TNC result code
83 TNC_Result (*send_message
)(TNC_IMCID imc_id
,
84 TNC_ConnectionID connection_id
,
85 TNC_BufferReference msg
,
87 TNC_MessageType msg_type
);
90 METHOD(imc_agent_t
, bind_functions
, TNC_Result
,
91 private_imc_agent_t
*this, TNC_TNCC_BindFunctionPointer bind_function
)
95 DBG1(DBG_IMC
, "TNC client failed to provide bind function");
96 return TNC_RESULT_INVALID_PARAMETER
;
98 if (bind_function(this->id
, "TNC_TNCC_ReportMessageTypes",
99 (void**)&this->report_message_types
) != TNC_RESULT_SUCCESS
)
101 this->report_message_types
= NULL
;
103 if (bind_function(this->id
, "TNC_TNCC_RequestHandshakeRetry",
104 (void**)&this->public.request_handshake_retry
) != TNC_RESULT_SUCCESS
)
106 this->public.request_handshake_retry
= NULL
;
108 if (bind_function(this->id
, "TNC_TNCC_SendMessage",
109 (void**)&this->send_message
) != TNC_RESULT_SUCCESS
)
111 this->send_message
= NULL
;
113 DBG2(DBG_IMC
, "IMC %u \"%s\" provided with bind function",
114 this->id
, this->name
);
116 if (this->report_message_types
)
118 this->report_message_types(this->id
, &this->type
, 1);
120 return TNC_RESULT_SUCCESS
;
124 * finds a connection state based on its Connection ID
126 static imc_state_t
* find_connection(private_imc_agent_t
*this,
129 enumerator_t
*enumerator
;
130 imc_state_t
*state
, *found
= NULL
;
132 this->connection_lock
->read_lock(this->connection_lock
);
133 enumerator
= this->connections
->create_enumerator(this->connections
);
134 while (enumerator
->enumerate(enumerator
, &state
))
136 if (id
== state
->get_connection_id(state
))
142 enumerator
->destroy(enumerator
);
143 this->connection_lock
->unlock(this->connection_lock
);
149 * delete a connection state with a given Connection ID
151 static bool delete_connection(private_imc_agent_t
*this, TNC_ConnectionID id
)
153 enumerator_t
*enumerator
;
157 this->connection_lock
->write_lock(this->connection_lock
);
158 enumerator
= this->connections
->create_enumerator(this->connections
);
159 while (enumerator
->enumerate(enumerator
, &state
))
161 if (id
== state
->get_connection_id(state
))
164 state
->destroy(state
);
165 this->connections
->remove_at(this->connections
, enumerator
);
169 enumerator
->destroy(enumerator
);
170 this->connection_lock
->unlock(this->connection_lock
);
175 METHOD(imc_agent_t
, create_state
, TNC_Result
,
176 private_imc_agent_t
*this, imc_state_t
*state
)
178 TNC_ConnectionID connection_id
;
180 connection_id
= state
->get_connection_id(state
);
181 if (find_connection(this, connection_id
))
183 DBG1(DBG_IMC
, "IMC %u \"%s\" already created a state for Connection ID %u",
184 this->id
, this->name
, connection_id
);
185 state
->destroy(state
);
186 return TNC_RESULT_OTHER
;
188 this->connection_lock
->write_lock(this->connection_lock
);
189 this->connections
->insert_last(this->connections
, state
);
190 this->connection_lock
->unlock(this->connection_lock
);
191 DBG2(DBG_IMC
, "IMC %u \"%s\" created a state for Connection ID %u",
192 this->id
, this->name
, connection_id
);
193 return TNC_RESULT_SUCCESS
;
196 METHOD(imc_agent_t
, delete_state
, TNC_Result
,
197 private_imc_agent_t
*this, TNC_ConnectionID connection_id
)
199 if (!delete_connection(this, connection_id
))
201 DBG1(DBG_IMC
, "IMC %u \"%s\" has no state for Connection ID %u",
202 this->id
, this->name
, connection_id
);
203 return TNC_RESULT_FATAL
;
205 DBG2(DBG_IMC
, "IMC %u \"%s\" deleted the state of Connection ID %u",
206 this->id
, this->name
, connection_id
);
207 return TNC_RESULT_SUCCESS
;
210 METHOD(imc_agent_t
, change_state
, TNC_Result
,
211 private_imc_agent_t
*this, TNC_ConnectionID connection_id
,
212 TNC_ConnectionState new_state
)
218 case TNC_CONNECTION_STATE_HANDSHAKE
:
219 case TNC_CONNECTION_STATE_ACCESS_ALLOWED
:
220 case TNC_CONNECTION_STATE_ACCESS_ISOLATED
:
221 case TNC_CONNECTION_STATE_ACCESS_NONE
:
222 state
= find_connection(this, connection_id
);
225 DBG1(DBG_IMC
, "IMC %u \"%s\" has no state for Connection ID %u",
226 this->id
, this->name
, connection_id
);
227 return TNC_RESULT_FATAL
;
229 state
->change_state(state
, new_state
);
230 DBG2(DBG_IMC
, "IMC %u \"%s\" changed state of Connection ID %u to '%N'",
231 this->id
, this->name
, connection_id
,
232 TNC_Connection_State_names
, new_state
);
234 case TNC_CONNECTION_STATE_CREATE
:
235 DBG1(DBG_IMC
, "state '%N' should be handled by create_state()",
236 TNC_Connection_State_names
, new_state
);
237 return TNC_RESULT_FATAL
;
238 case TNC_CONNECTION_STATE_DELETE
:
239 DBG1(DBG_IMC
, "state '%N' should be handled by delete_state()",
240 TNC_Connection_State_names
, new_state
);
241 return TNC_RESULT_FATAL
;
243 DBG1(DBG_IMC
, "IMC %u \"%s\" was notified of unknown state %u "
244 "for Connection ID %u",
245 this->id
, this->name
, new_state
, connection_id
);
246 return TNC_RESULT_INVALID_PARAMETER
;
248 return TNC_RESULT_SUCCESS
;
251 METHOD(imc_agent_t
, get_state
, bool,
252 private_imc_agent_t
*this, TNC_ConnectionID connection_id
,
255 *state
= find_connection(this, connection_id
);
258 DBG1(DBG_IMC
, "IMC %u \"%s\" has no state for Connection ID %u",
259 this->id
, this->name
, connection_id
);
265 METHOD(imc_agent_t
, send_message
, TNC_Result
,
266 private_imc_agent_t
*this, TNC_ConnectionID connection_id
, chunk_t msg
)
268 if (!this->send_message
)
270 return TNC_RESULT_FATAL
;
272 return this->send_message(this->id
, connection_id
, msg
.ptr
, msg
.len
,
276 METHOD(imc_agent_t
, receive_message
, TNC_Result
,
277 private_imc_agent_t
*this, TNC_ConnectionID connection_id
, chunk_t msg
,
278 TNC_MessageType msg_type
, pa_tnc_msg_t
**pa_tnc_msg
)
280 pa_tnc_msg_t
*pa_msg
, *error_msg
;
281 pa_tnc_attr_t
*error_attr
;
282 enumerator_t
*enumerator
;
285 DBG2(DBG_IMV
, "IMC %u \"%s\" received message type 0x%08x for Connection ID %u",
286 this->id
, this->name
, msg_type
, connection_id
);
289 pa_msg
= pa_tnc_msg_create_from_data(msg
);
291 switch (pa_msg
->process(pa_msg
))
294 *pa_tnc_msg
= pa_msg
;
297 if (!this->send_message
)
299 /* TNCC doen't have a SendMessage() function */
300 return TNC_RESULT_FATAL
;
303 /* build error message */
304 error_msg
= pa_tnc_msg_create();
305 enumerator
= pa_msg
->create_error_enumerator(pa_msg
);
306 while (enumerator
->enumerate(enumerator
, &error_attr
))
308 error_msg
->add_attribute(error_msg
,
309 error_attr
->get_ref(error_attr
));
311 enumerator
->destroy(enumerator
);
312 error_msg
->build(error_msg
);
314 /* send error message */
315 msg
= error_msg
->get_encoding(error_msg
);
316 result
= this->send_message(this->id
, connection_id
,
317 msg
.ptr
, msg
.len
, msg_type
);
320 error_msg
->destroy(error_msg
);
321 pa_msg
->destroy(pa_msg
);
325 pa_msg
->destroy(pa_msg
);
326 return TNC_RESULT_FATAL
;
328 return TNC_RESULT_SUCCESS
;
331 METHOD(imc_agent_t
, destroy
, void,
332 private_imc_agent_t
*this)
334 DBG1(DBG_IMC
, "IMC %u \"%s\" terminated", this->id
, this->name
);
335 this->connections
->destroy_function(this->connections
, free
);
336 this->connection_lock
->destroy(this->connection_lock
);
339 /* decrease the reference count or terminate */
344 * Described in header.
346 imc_agent_t
*imc_agent_create(const char *name
,
347 pen_t vendor_id
, u_int32_t subtype
,
348 TNC_IMCID id
, TNC_Version
*actual_version
)
350 private_imc_agent_t
*this;
352 /* initialize or increase the reference count */
360 .bind_functions
= _bind_functions
,
361 .create_state
= _create_state
,
362 .delete_state
= _delete_state
,
363 .change_state
= _change_state
,
364 .get_state
= _get_state
,
365 .send_message
= _send_message
,
366 .receive_message
= _receive_message
,
370 .type
= (vendor_id
<< 8) | (subtype
&& 0xff),
372 .connections
= linked_list_create(),
373 .connection_lock
= rwlock_create(RWLOCK_TYPE_DEFAULT
),
376 *actual_version
= TNC_IFIMC_VERSION_1
;
377 DBG1(DBG_IMC
, "IMC %u \"%s\" initialized", this->id
, this->name
);
379 return &this->public;