Whitespace cleanups in tnc code
[strongswan.git] / src / libcharon / plugins / tnc_imc / tnc_imc_manager.c
1 /*
2 * Copyright (C) 2006 Mike McCauley
3 * Copyright (C) 2010 Andreas Steffen, HSR Hochschule fuer Technik Rapperswil
4 *
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>.
9 *
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
13 * for more details.
14 */
15
16 #include "tnc_imc_manager.h"
17
18 #include <tnc/imc/imc_manager.h>
19 #include <tnc/tncifimc.h>
20
21 #include <debug.h>
22 #include <library.h>
23 #include <utils/linked_list.h>
24
25 typedef struct private_tnc_imc_manager_t private_tnc_imc_manager_t;
26
27 /**
28 * Private data of an imc_manager_t object.
29 */
30 struct private_tnc_imc_manager_t {
31
32 /**
33 * Public members of imc_manager_t.
34 */
35 imc_manager_t public;
36
37 /**
38 * Linked list of IMCs
39 */
40 linked_list_t *imcs;
41
42 /**
43 * Next IMC ID to be assigned
44 */
45 TNC_IMCID next_imc_id;
46 };
47
48 METHOD(imc_manager_t, add, bool,
49 private_tnc_imc_manager_t *this, imc_t *imc)
50 {
51 TNC_Version version;
52
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)
57 {
58 DBG1(DBG_TNC, "IMC \"%s\" failed to initialize", imc->get_name(imc));
59 return FALSE;
60 }
61 this->imcs->insert_last(this->imcs, imc);
62 this->next_imc_id++;
63
64 if (imc->provide_bind_function(imc->get_id(imc), TNC_TNCC_BindFunction)
65 != TNC_RESULT_SUCCESS)
66 {
67 DBG1(DBG_TNC, "IMC \"%s\" failed to obtain bind function",
68 imc->get_name(imc));
69 this->imcs->remove_last(this->imcs, (void**)&imc);
70 return FALSE;
71 }
72
73 return TRUE;
74 }
75
76 METHOD(imc_manager_t, remove_, imc_t*,
77 private_tnc_imc_manager_t *this, TNC_IMCID id)
78 {
79 enumerator_t *enumerator;
80 imc_t *imc;
81
82 enumerator = this->imcs->create_enumerator(this->imcs);
83 while (enumerator->enumerate(enumerator, &imc))
84 {
85 if (id == imc->get_id(imc))
86 {
87 this->imcs->remove_at(this->imcs, enumerator);
88 return imc;
89 }
90 }
91 enumerator->destroy(enumerator);
92 return NULL;
93 }
94
95 METHOD(imc_manager_t, get_preferred_language, char*,
96 private_tnc_imc_manager_t *this)
97 {
98 return lib->settings->get_str(lib->settings,
99 "charon.plugins.tnc-imc.preferred_language", "en");
100 }
101
102 METHOD(imc_manager_t, notify_connection_change, void,
103 private_tnc_imc_manager_t *this, TNC_ConnectionID id,
104 TNC_ConnectionState state)
105 {
106 enumerator_t *enumerator;
107 imc_t *imc;
108
109 enumerator = this->imcs->create_enumerator(this->imcs);
110 while (enumerator->enumerate(enumerator, &imc))
111 {
112 if (imc->notify_connection_change)
113 {
114 imc->notify_connection_change(imc->get_id(imc), id, state);
115 }
116 }
117 enumerator->destroy(enumerator);
118 }
119
120 METHOD(imc_manager_t, begin_handshake, void,
121 private_tnc_imc_manager_t *this, TNC_ConnectionID id)
122 {
123 enumerator_t *enumerator;
124 imc_t *imc;
125
126 enumerator = this->imcs->create_enumerator(this->imcs);
127 while (enumerator->enumerate(enumerator, &imc))
128 {
129 imc->begin_handshake(imc->get_id(imc), id);
130 }
131 enumerator->destroy(enumerator);
132 }
133
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)
138 {
139 enumerator_t *enumerator;
140 imc_t *imc;
141 TNC_Result result = TNC_RESULT_FATAL;
142
143 enumerator = this->imcs->create_enumerator(this->imcs);
144 while (enumerator->enumerate(enumerator, &imc))
145 {
146 if (id == imc->get_id(imc))
147 {
148 imc->set_message_types(imc, supported_types, type_count);
149 result = TNC_RESULT_SUCCESS;
150 break;
151 }
152 }
153 enumerator->destroy(enumerator);
154 return result;
155 }
156
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)
162 {
163 enumerator_t *enumerator;
164 imc_t *imc;
165
166 enumerator = this->imcs->create_enumerator(this->imcs);
167 while (enumerator->enumerate(enumerator, &imc))
168 {
169 if (imc->receive_message && imc->type_supported(imc, message_type))
170 {
171 imc->receive_message(imc->get_id(imc), connection_id,
172 message, message_len, message_type);
173 }
174 }
175 enumerator->destroy(enumerator);
176 }
177
178 METHOD(imc_manager_t, batch_ending, void,
179 private_tnc_imc_manager_t *this, TNC_ConnectionID id)
180 {
181 enumerator_t *enumerator;
182 imc_t *imc;
183
184 enumerator = this->imcs->create_enumerator(this->imcs);
185 while (enumerator->enumerate(enumerator, &imc))
186 {
187 if (imc->batch_ending)
188 {
189 imc->batch_ending(imc->get_id(imc), id);
190 }
191 }
192 enumerator->destroy(enumerator);
193 }
194
195 METHOD(imc_manager_t, destroy, void,
196 private_tnc_imc_manager_t *this)
197 {
198 imc_t *imc;
199
200 while (this->imcs->remove_last(this->imcs, (void**)&imc) == SUCCESS)
201 {
202 if (imc->terminate &&
203 imc->terminate(imc->get_id(imc)) != TNC_RESULT_SUCCESS)
204 {
205 DBG1(DBG_TNC, "IMC \"%s\" not terminated successfully",
206 imc->get_name(imc));
207 }
208 imc->destroy(imc);
209 }
210 this->imcs->destroy(this->imcs);
211 free(this);
212 }
213
214 /**
215 * Described in header.
216 */
217 imc_manager_t* tnc_imc_manager_create(void)
218 {
219 private_tnc_imc_manager_t *this;
220
221 INIT(this,
222 .public = {
223 .add = _add,
224 .remove = _remove_, /* avoid name conflict with stdio.h */
225 .get_preferred_language = _get_preferred_language,
226 .notify_connection_change = _notify_connection_change,
227 .begin_handshake = _begin_handshake,
228 .set_message_types = _set_message_types,
229 .receive_message = _receive_message,
230 .batch_ending = _batch_ending,
231 .destroy = _destroy,
232 },
233 .imcs = linked_list_create(),
234 .next_imc_id = 1,
235 );
236
237 return &this->public;
238 }