2 * Copyright (C) 2006 Mike McCauley
3 * Copyright (C) 2010 Andreas Steffen, 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 "tnc_imc_manager.h"
18 #include <tnc/imc/imc_manager.h>
19 #include <tnc/tncifimc.h>
23 #include <utils/linked_list.h>
25 typedef struct private_tnc_imc_manager_t private_tnc_imc_manager_t
;
28 * Private data of an imc_manager_t object.
30 struct private_tnc_imc_manager_t
{
33 * Public members of imc_manager_t.
43 * Next IMC ID to be assigned
45 TNC_IMCID next_imc_id
;
48 METHOD(imc_manager_t
, add
, bool,
49 private_tnc_imc_manager_t
*this, imc_t
*imc
)
53 /* Initialize the module */
54 imc
->set_id(imc
, this->next_imc_id
);
55 if (imc
->initialize(imc
->get_id(imc
), TNC_IFIMC_VERSION_1
,
56 TNC_IFIMC_VERSION_1
, &version
) != TNC_RESULT_SUCCESS
)
58 DBG1(DBG_TNC
, "could not initialize IMC '%s'",
62 this->imcs
->insert_last(this->imcs
, imc
);
65 if (imc
->provide_bind_function(imc
->get_id(imc
), TNC_TNCC_BindFunction
)
66 != TNC_RESULT_SUCCESS
)
68 DBG1(DBG_TNC
, "could not provide bind function for IMC '%s'",
70 this->imcs
->remove_last(this->imcs
, (void**)&imc
);
77 METHOD(imc_manager_t
, remove_
, imc_t
*,
78 private_tnc_imc_manager_t
*this, TNC_IMCID id
)
80 enumerator_t
*enumerator
;
83 enumerator
= this->imcs
->create_enumerator(this->imcs
);
84 while (enumerator
->enumerate(enumerator
, &imc
))
86 if (id
== imc
->get_id(imc
))
88 this->imcs
->remove_at(this->imcs
, enumerator
);
92 enumerator
->destroy(enumerator
);
96 METHOD(imc_manager_t
, get_count
, int,
97 private_tnc_imc_manager_t
*this)
99 return this->imcs
->get_count(this->imcs
);
102 METHOD(imc_manager_t
, notify_connection_change
, void,
103 private_tnc_imc_manager_t
*this, TNC_ConnectionID id
,
104 TNC_ConnectionState state
)
106 enumerator_t
*enumerator
;
109 enumerator
= this->imcs
->create_enumerator(this->imcs
);
110 while (enumerator
->enumerate(enumerator
, &imc
))
112 if (imc
->notify_connection_change
)
114 imc
->notify_connection_change(imc
->get_id(imc
), id
, state
);
117 enumerator
->destroy(enumerator
);
120 METHOD(imc_manager_t
, begin_handshake
, void,
121 private_tnc_imc_manager_t
*this, TNC_ConnectionID id
)
123 enumerator_t
*enumerator
;
126 enumerator
= this->imcs
->create_enumerator(this->imcs
);
127 while (enumerator
->enumerate(enumerator
, &imc
))
129 imc
->begin_handshake(imc
->get_id(imc
), id
);
131 enumerator
->destroy(enumerator
);
134 METHOD(imc_manager_t
, set_message_types
, TNC_Result
,
135 private_tnc_imc_manager_t
*this, TNC_IMCID id
,
136 TNC_MessageTypeList supported_types
,
137 TNC_UInt32 type_count
)
139 enumerator_t
*enumerator
;
141 TNC_Result result
= TNC_RESULT_FATAL
;
143 enumerator
= this->imcs
->create_enumerator(this->imcs
);
144 while (enumerator
->enumerate(enumerator
, &imc
))
146 if (id
== imc
->get_id(imc
))
148 imc
->set_message_types(imc
, supported_types
, type_count
);
149 result
= TNC_RESULT_SUCCESS
;
153 enumerator
->destroy(enumerator
);
157 METHOD(imc_manager_t
, receive_message
, void,
158 private_tnc_imc_manager_t
*this, TNC_ConnectionID connection_id
,
159 TNC_BufferReference message
,
160 TNC_UInt32 message_len
,
161 TNC_MessageType message_type
)
163 enumerator_t
*enumerator
;
166 enumerator
= this->imcs
->create_enumerator(this->imcs
);
167 while (enumerator
->enumerate(enumerator
, &imc
))
169 if (imc
->receive_message
&& imc
->type_supported(imc
, message_type
))
171 imc
->receive_message(imc
->get_id(imc
), connection_id
,
172 message
, message_len
, message_type
);
175 enumerator
->destroy(enumerator
);
178 METHOD(imc_manager_t
, destroy
, void,
179 private_tnc_imc_manager_t
*this)
183 while (this->imcs
->remove_last(this->imcs
, (void**)&imc
) == SUCCESS
)
185 if (imc
->terminate
&&
186 imc
->terminate(imc
->get_id(imc
)) != TNC_RESULT_SUCCESS
)
188 DBG1(DBG_TNC
, "IMC '%s' not terminated successfully",
193 this->imcs
->destroy(this->imcs
);
198 * Described in header.
200 imc_manager_t
* tnc_imc_manager_create(void)
202 private_tnc_imc_manager_t
*this;
207 .remove
= _remove_
, /* avoid name conflict with stdio.h */
208 .get_count
= _get_count
,
209 .notify_connection_change
= _notify_connection_change
,
210 .begin_handshake
= _begin_handshake
,
211 .set_message_types
= _set_message_types
,
212 .receive_message
= _receive_message
,
215 .imcs
= linked_list_create(),
219 return &this->public;