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
15 #include "imc_agent.h"
18 #include <utils/linked_list.h>
19 #include <threading/rwlock.h>
21 typedef struct private_imc_agent_t private_imc_agent_t
;
24 * Private data of an imc_agent_t object.
26 struct private_imc_agent_t
{
29 * Public members of imc_agent_t
44 * ID of IMC as assigned by TNCC
49 * list of TNCC connection entries
51 linked_list_t
*connections
;
54 * rwlock to lock TNCS connection entries
56 rwlock_t
*connection_lock
;
59 * Inform a TNCS about the set of message types the IMC is able to receive
61 * @param imc_id IMC ID assigned by TNCC
62 * @param supported_types list of supported message types
63 * @param type_count number of list elements
64 * @return TNC result code
66 TNC_Result (*report_message_types
)(TNC_IMCID imc_id
,
67 TNC_MessageTypeList supported_types
,
68 TNC_UInt32 type_count
);
71 * Call when an IMC-IMC message is to be sent
73 * @param imc_id IMC ID assigned by TNCC
74 * @param connection_id network connection ID assigned by TNCC
75 * @param msg message to send
76 * @param msg_len message length in bytes
77 * @param msg_type message type
78 * @return TNC result code
80 TNC_Result (*send_message
)(TNC_IMCID imc_id
,
81 TNC_ConnectionID connection_id
,
82 TNC_BufferReference msg
,
84 TNC_MessageType msg_type
);
87 METHOD(imc_agent_t
, bind_functions
, TNC_Result
,
88 private_imc_agent_t
*this, TNC_TNCC_BindFunctionPointer bind_function
)
92 DBG1(DBG_IMC
, "TNC client failed to provide bind function");
93 return TNC_RESULT_INVALID_PARAMETER
;
95 if (bind_function(this->id
, "TNC_TNCC_ReportMessageTypes",
96 (void**)&this->report_message_types
) != TNC_RESULT_SUCCESS
)
98 this->report_message_types
= NULL
;
100 if (bind_function(this->id
, "TNC_TNCC_RequestHandshakeRetry",
101 (void**)&this->public.request_handshake_retry
) != TNC_RESULT_SUCCESS
)
103 this->public.request_handshake_retry
= NULL
;
105 if (bind_function(this->id
, "TNC_TNCC_SendMessage",
106 (void**)&this->send_message
) != TNC_RESULT_SUCCESS
)
108 this->send_message
= NULL
;
110 DBG2(DBG_IMC
, "IMC %u \"%s\" provided with bind function",
111 this->id
, this->name
);
113 if (this->report_message_types
)
115 this->report_message_types(this->id
, &this->type
, 1);
117 return TNC_RESULT_SUCCESS
;
121 * finds a connection state based on its Connection ID
123 static imc_state_t
* find_connection(private_imc_agent_t
*this,
126 enumerator_t
*enumerator
;
127 imc_state_t
*state
, *found
= NULL
;
129 this->connection_lock
->read_lock(this->connection_lock
);
130 enumerator
= this->connections
->create_enumerator(this->connections
);
131 while (enumerator
->enumerate(enumerator
, &state
))
133 if (id
== state
->get_connection_id(state
))
139 enumerator
->destroy(enumerator
);
140 this->connection_lock
->unlock(this->connection_lock
);
146 * delete a connection state with a given Connection ID
148 static bool delete_connection(private_imc_agent_t
*this, TNC_ConnectionID id
)
150 enumerator_t
*enumerator
;
154 this->connection_lock
->write_lock(this->connection_lock
);
155 enumerator
= this->connections
->create_enumerator(this->connections
);
156 while (enumerator
->enumerate(enumerator
, &state
))
158 if (id
== state
->get_connection_id(state
))
161 state
->destroy(state
);
162 this->connections
->remove_at(this->connections
, enumerator
);
166 enumerator
->destroy(enumerator
);
167 this->connection_lock
->unlock(this->connection_lock
);
172 METHOD(imc_agent_t
, create_state
, TNC_Result
,
173 private_imc_agent_t
*this, imc_state_t
*state
)
175 TNC_ConnectionID connection_id
;
177 connection_id
= state
->get_connection_id(state
);
178 if (find_connection(this, connection_id
))
180 DBG1(DBG_IMC
, "IMC %u \"%s\" already created a state for Connection ID %u",
181 this->id
, this->name
, connection_id
);
182 state
->destroy(state
);
183 return TNC_RESULT_OTHER
;
185 this->connection_lock
->write_lock(this->connection_lock
);
186 this->connections
->insert_last(this->connections
, state
);
187 this->connection_lock
->unlock(this->connection_lock
);
188 DBG2(DBG_IMC
, "IMC %u \"%s\" created a state for Connection ID %u",
189 this->id
, this->name
, connection_id
);
190 return TNC_RESULT_SUCCESS
;
193 METHOD(imc_agent_t
, delete_state
, TNC_Result
,
194 private_imc_agent_t
*this, TNC_ConnectionID connection_id
)
196 if (!delete_connection(this, connection_id
))
198 DBG1(DBG_IMC
, "IMC %u \"%s\" has no state for Connection ID %u",
199 this->id
, this->name
, connection_id
);
200 return TNC_RESULT_FATAL
;
202 DBG2(DBG_IMC
, "IMC %u \"%s\" deleted the state of Connection ID %u",
203 this->id
, this->name
, connection_id
);
204 return TNC_RESULT_SUCCESS
;
207 METHOD(imc_agent_t
, change_state
, TNC_Result
,
208 private_imc_agent_t
*this, TNC_ConnectionID connection_id
,
209 TNC_ConnectionState new_state
)
215 case TNC_CONNECTION_STATE_HANDSHAKE
:
216 case TNC_CONNECTION_STATE_ACCESS_ALLOWED
:
217 case TNC_CONNECTION_STATE_ACCESS_ISOLATED
:
218 case TNC_CONNECTION_STATE_ACCESS_NONE
:
219 state
= find_connection(this, connection_id
);
222 DBG1(DBG_IMC
, "IMC %u \"%s\" has no state for Connection ID %u",
223 this->id
, this->name
, connection_id
);
224 return TNC_RESULT_FATAL
;
226 state
->change_state(state
, new_state
);
227 DBG2(DBG_IMC
, "IMC %u \"%s\" changed state of Connection ID %u to '%N'",
228 this->id
, this->name
, connection_id
,
229 TNC_Connection_State_names
, new_state
);
231 case TNC_CONNECTION_STATE_CREATE
:
232 DBG1(DBG_IMC
, "state '%N' should be handled by create_state()",
233 TNC_Connection_State_names
, new_state
);
234 return TNC_RESULT_FATAL
;
235 case TNC_CONNECTION_STATE_DELETE
:
236 DBG1(DBG_IMC
, "state '%N' should be handled by delete_state()",
237 TNC_Connection_State_names
, new_state
);
238 return TNC_RESULT_FATAL
;
240 DBG1(DBG_IMC
, "IMC %u \"%s\" was notified of unknown state %u "
241 "for Connection ID %u",
242 this->id
, this->name
, new_state
, connection_id
);
243 return TNC_RESULT_INVALID_PARAMETER
;
245 return TNC_RESULT_SUCCESS
;
248 METHOD(imc_agent_t
, get_state
, bool,
249 private_imc_agent_t
*this, TNC_ConnectionID connection_id
,
252 *state
= find_connection(this, connection_id
);
255 DBG1(DBG_IMC
, "IMC %u \"%s\" has no state for Connection ID %u",
256 this->id
, this->name
, connection_id
);
262 METHOD(imc_agent_t
, send_message
, TNC_Result
,
263 private_imc_agent_t
*this, TNC_ConnectionID connection_id
, chunk_t msg
)
265 if (!this->send_message
)
267 return TNC_RESULT_FATAL
;
269 return this->send_message(this->id
, connection_id
, msg
.ptr
, msg
.len
,
273 METHOD(imc_agent_t
, destroy
, void,
274 private_imc_agent_t
*this)
276 DBG1(DBG_IMC
, "IMC %u \"%s\" terminated", this->id
, this->name
);
277 this->connections
->destroy_function(this->connections
, free
);
278 this->connection_lock
->destroy(this->connection_lock
);
283 * Described in header.
285 imc_agent_t
*imc_agent_create(const char *name
,
286 pen_t vendor_id
, u_int32_t subtype
,
287 TNC_IMCID id
, TNC_Version
*actual_version
)
289 private_imc_agent_t
*this;
293 .bind_functions
= _bind_functions
,
294 .create_state
= _create_state
,
295 .delete_state
= _delete_state
,
296 .change_state
= _change_state
,
297 .get_state
= _get_state
,
298 .send_message
= _send_message
,
302 .type
= (vendor_id
<< 8) | (subtype
&& 0xff),
304 .connections
= linked_list_create(),
305 .connection_lock
= rwlock_create(RWLOCK_TYPE_DEFAULT
),
308 *actual_version
= TNC_IFIMC_VERSION_1
;
309 DBG1(DBG_IMC
, "IMC %u \"%s\" initialized", this->id
, this->name
);
311 return &this->public;