4f7bd7d4fb366f2452fbccda35a555138c615048
[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, "could not initialize IMC '%s'",
59 imc->get_name(imc));
60 return FALSE;
61 }
62 this->imcs->insert_last(this->imcs, imc);
63 this->next_imc_id++;
64
65 if (imc->provide_bind_function(imc->get_id(imc), TNC_TNCC_BindFunction)
66 != TNC_RESULT_SUCCESS)
67 {
68 DBG1(DBG_TNC, "could not provide bind function for IMC '%s'",
69 imc->get_name(imc));
70 this->imcs->remove_last(this->imcs, (void**)&imc);
71 return FALSE;
72 }
73
74 return TRUE;
75 }
76
77 METHOD(imc_manager_t, remove_, imc_t*,
78 private_tnc_imc_manager_t *this, TNC_IMCID id)
79 {
80 enumerator_t *enumerator;
81 imc_t *imc;
82
83 enumerator = this->imcs->create_enumerator(this->imcs);
84 while (enumerator->enumerate(enumerator, &imc))
85 {
86 if (id == imc->get_id(imc))
87 {
88 this->imcs->remove_at(this->imcs, enumerator);
89 return imc;
90 }
91 }
92 enumerator->destroy(enumerator);
93 return NULL;
94 }
95
96 METHOD(imc_manager_t, get_count, int,
97 private_tnc_imc_manager_t *this)
98 {
99 return this->imcs->get_count(this->imcs);
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, destroy, void,
179 private_tnc_imc_manager_t *this)
180 {
181 imc_t *imc;
182
183 while (this->imcs->remove_last(this->imcs, (void**)&imc) == SUCCESS)
184 {
185 if (imc->terminate &&
186 imc->terminate(imc->get_id(imc)) != TNC_RESULT_SUCCESS)
187 {
188 DBG1(DBG_TNC, "IMC '%s' not terminated successfully",
189 imc->get_name(imc));
190 }
191 imc->destroy(imc);
192 }
193 this->imcs->destroy(this->imcs);
194 free(this);
195 }
196
197 /**
198 * Described in header.
199 */
200 imc_manager_t* tnc_imc_manager_create(void)
201 {
202 private_tnc_imc_manager_t *this;
203
204 INIT(this,
205 .public = {
206 .add = _add,
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,
213 .destroy = _destroy,
214 },
215 .imcs = linked_list_create(),
216 .next_imc_id = 1,
217 );
218
219 return &this->public;
220 }