cbecf14c7f76e4e45114700e9b35c1bddd011351
[strongswan.git] / src / libtnccs / plugins / tnc_imc / tnc_imc.c
1 /*
2 * Copyright (C) 2006 Mike McCauley
3 * Copyright (C) 2010-2011 Andreas Steffen,
4 * HSR Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "tnc_imc.h"
18
19 #include <dlfcn.h>
20
21 #include <tncif_pa_subtypes.h>
22
23 #include <utils/debug.h>
24 #include <library.h>
25 #include <collections/linked_list.h>
26 #include <threading/mutex.h>
27
28 typedef struct private_tnc_imc_t private_tnc_imc_t;
29
30 /**
31 * Private data of an imc_t object.
32 */
33 struct private_tnc_imc_t {
34
35 /**
36 * Public members of imc_t.
37 */
38 imc_t public;
39
40 /**
41 * Name of loaded IMC
42 */
43 char *name;
44
45 /**
46 * Handle of loaded IMC
47 */
48 void *handle;
49
50 /**
51 * ID of loaded IMC
52 */
53 TNC_IMCID id;
54
55 /**
56 * list of additional IMC IDs
57 */
58 linked_list_t *additional_ids;
59
60 /**
61 * List of message types supported by IMC - Vendor ID part
62 */
63 TNC_VendorIDList supported_vids;
64
65 /**
66 * List of message types supported by IMC - Subtype part
67 */
68 TNC_MessageSubtypeList supported_subtypes;
69
70 /**
71 * Number of supported message types
72 */
73 TNC_UInt32 type_count;
74
75 /**
76 * mutex to lock the imc_t object
77 */
78 mutex_t *mutex;
79 };
80
81 METHOD(imc_t, set_id, void,
82 private_tnc_imc_t *this, TNC_IMCID id)
83 {
84 this->id = id;
85 }
86
87 METHOD(imc_t, get_id, TNC_IMCID,
88 private_tnc_imc_t *this)
89 {
90 return this->id;
91 }
92
93 METHOD(imc_t, add_id, void,
94 private_tnc_imc_t *this, TNC_IMCID id)
95 {
96 void *pointer;
97
98 /* store the scalar value in the pointer */
99 pointer = (void*)id;
100 this->additional_ids->insert_last(this->additional_ids, pointer);
101 }
102
103 METHOD(imc_t, has_id, bool,
104 private_tnc_imc_t *this, TNC_IMCID id)
105 {
106 enumerator_t *enumerator;
107 TNC_IMCID additional_id;
108 void *pointer;
109 bool found = FALSE;
110
111 /* check primary IMC ID */
112 if (id == this->id)
113 {
114 return TRUE;
115 }
116
117 /* return if there are no additional IMC IDs */
118 if (this->additional_ids->get_count(this->additional_ids) == 0)
119 {
120 return FALSE;
121 }
122
123 /* check additional IMC IDs */
124 enumerator = this->additional_ids->create_enumerator(this->additional_ids);
125 while (enumerator->enumerate(enumerator, &pointer))
126 {
127 /* interpret pointer as scalar value */
128 additional_id = (TNC_UInt32)pointer;
129
130 if (id == additional_id)
131 {
132 found = TRUE;
133 break;
134 }
135 }
136 enumerator->destroy(enumerator);
137
138 return found;
139 }
140
141 METHOD(imc_t, get_name, char*,
142 private_tnc_imc_t *this)
143 {
144 return this->name;
145 }
146
147 METHOD(imc_t, set_message_types, void,
148 private_tnc_imc_t *this, TNC_MessageTypeList supported_types,
149 TNC_UInt32 type_count)
150 {
151 char buf[BUF_LEN];
152 char *pos = buf;
153 int len = sizeof(buf);
154 int i, written;
155 size_t size;
156 TNC_VendorID vid;
157 TNC_MessageSubtype subtype;
158 enum_name_t *pa_subtype_names;
159
160 /* lock the imc_t instance */
161 this->mutex->lock(this->mutex);
162
163 /* Free existing VendorID and MessageSubtype lists */
164 free(this->supported_vids);
165 this->supported_vids = NULL;
166 free(this->supported_subtypes);
167 this->supported_subtypes = NULL;
168
169 /* Store the new MessageType list */
170 this->type_count = type_count;
171 if (type_count && supported_types)
172 {
173 size = type_count * sizeof(TNC_VendorID);
174 this->supported_vids = malloc(size);
175 size = type_count * sizeof(TNC_MessageSubtype);
176 this->supported_subtypes = malloc(size);
177
178 for (i = 0; i < type_count; i++)
179 {
180 vid = (supported_types[i] >> 8) & TNC_VENDORID_ANY;
181 subtype = supported_types[i] & TNC_SUBTYPE_ANY;
182
183 pa_subtype_names = get_pa_subtype_names(vid);
184 if (pa_subtype_names)
185 {
186 written = snprintf(pos, len," '%N/%N' 0x%06x/0x%02x",
187 pen_names, vid, pa_subtype_names, subtype,
188 vid, subtype);
189 }
190 else
191 {
192 written = snprintf(pos, len," '%N' 0x%06x/0x%02x",
193 pen_names, vid, vid, subtype);
194 }
195 if (written >= len)
196 {
197 break;
198 }
199 pos += written;
200 len -= written;
201
202 this->supported_vids[i] = vid;
203 this->supported_subtypes[i] = subtype;
204 }
205 }
206 *pos = '\0';
207 DBG2(DBG_TNC, "IMC %u supports %u message type%s:%s",
208 this->id, type_count, (type_count == 1) ? "":"s", buf);
209
210 /* unlock the imc_t instance */
211 this->mutex->unlock(this->mutex);
212 }
213
214 METHOD(imc_t, set_message_types_long, void,
215 private_tnc_imc_t *this, TNC_VendorIDList supported_vids,
216 TNC_MessageSubtypeList supported_subtypes, TNC_UInt32 type_count)
217 {
218 char buf[BUF_LEN];
219 char *pos = buf;
220 int len = sizeof(buf);
221 int i, written;
222 size_t size;
223 TNC_VendorID vid;
224 TNC_MessageSubtype subtype;
225 enum_name_t *pa_subtype_names;
226
227 /* lock the imc_t instance */
228 this->mutex->lock(this->mutex);
229
230 /* Free existing VendorID and MessageSubtype lists */
231 free(this->supported_vids);
232 this->supported_vids = NULL;
233 free(this->supported_subtypes);
234 this->supported_subtypes = NULL;
235
236 /* Store the new MessageType list */
237 this->type_count = type_count;
238 if (type_count && supported_vids && supported_subtypes)
239 {
240 size = type_count * sizeof(TNC_VendorID);
241 this->supported_vids = malloc(size);
242 memcpy(this->supported_vids, supported_vids, size);
243 size = type_count * sizeof(TNC_MessageSubtype);
244 this->supported_subtypes = malloc(size);
245 memcpy(this->supported_subtypes, supported_subtypes, size);
246
247 for (i = 0; i < type_count; i++)
248 {
249 vid = supported_vids[i];
250 subtype = supported_subtypes[i];
251
252 pa_subtype_names = get_pa_subtype_names(vid);
253 if (pa_subtype_names)
254 {
255 written = snprintf(pos, len," '%N/%N' 0x%06x/0x%08x",
256 pen_names, vid, pa_subtype_names, subtype,
257 vid, subtype);
258 }
259 else
260 {
261 written = snprintf(pos, len," '%N' 0x%06x/0x%08x",
262 pen_names, vid, vid, subtype);
263 }
264 if (written >= len)
265 {
266 break;
267 }
268 pos += written;
269 len -= written;
270 }
271 }
272 *pos = '\0';
273 DBG2(DBG_TNC, "IMC %u supports %u message type%s:%s",
274 this->id, type_count, (type_count == 1) ? "":"s", buf);
275
276 /* unlock the imc_t instance */
277 this->mutex->unlock(this->mutex);
278 }
279
280 METHOD(imc_t, type_supported, bool,
281 private_tnc_imc_t *this, TNC_VendorID msg_vid, TNC_MessageSubtype msg_subtype)
282 {
283 TNC_VendorID vid;
284 TNC_MessageSubtype subtype;
285 int i;
286
287 for (i = 0; i < this->type_count; i++)
288 {
289 vid = this->supported_vids[i];
290 subtype = this->supported_subtypes[i];
291
292 if ((vid == TNC_VENDORID_ANY && subtype == TNC_SUBTYPE_ANY) ||
293 (vid == msg_vid && (subtype == TNC_SUBTYPE_ANY ||
294 subtype == msg_subtype)))
295 {
296 return TRUE;
297 }
298 }
299 return FALSE;
300 }
301
302 METHOD(imc_t, destroy, void,
303 private_tnc_imc_t *this)
304 {
305 if (this->handle && lib->settings->get_bool(lib->settings,
306 "libtnccs.plugins.tnc-imc.dlclose", TRUE))
307 {
308 dlclose(this->handle);
309 }
310 this->mutex->destroy(this->mutex);
311 this->additional_ids->destroy(this->additional_ids);
312 free(this->supported_vids);
313 free(this->supported_subtypes);
314 free(this->name);
315 free(this);
316 }
317
318 /**
319 * Generic constructor
320 */
321 static private_tnc_imc_t* tnc_imc_create_empty(char *name)
322 {
323 private_tnc_imc_t *this;
324
325 INIT(this,
326 .public = {
327 .set_id = _set_id,
328 .get_id = _get_id,
329 .add_id = _add_id,
330 .has_id = _has_id,
331 .get_name = _get_name,
332 .set_message_types = _set_message_types,
333 .set_message_types_long = _set_message_types_long,
334 .type_supported = _type_supported,
335 .destroy = _destroy,
336 },
337 .name = strdup(name),
338 .additional_ids = linked_list_create(),
339 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
340 );
341
342 return this;
343 }
344
345 /**
346 * See header
347 */
348 imc_t* tnc_imc_create(char *name, char *path)
349 {
350 private_tnc_imc_t *this;
351
352 this = tnc_imc_create_empty(name);
353
354 this->handle = dlopen(path, RTLD_LAZY);
355 if (!this->handle)
356 {
357 DBG1(DBG_TNC, "IMC \"%s\" failed to load: %s", name, dlerror());
358 destroy(this);
359 return NULL;
360 }
361
362 this->public.initialize = dlsym(this->handle, "TNC_IMC_Initialize");
363 if (!this->public.initialize)
364 {
365 DBG1(DBG_TNC, "could not resolve TNC_IMC_Initialize in %s: %s\n",
366 path, dlerror());
367 destroy(this);
368 return NULL;
369 }
370 this->public.notify_connection_change =
371 dlsym(this->handle, "TNC_IMC_NotifyConnectionChange");
372 this->public.begin_handshake = dlsym(this->handle, "TNC_IMC_BeginHandshake");
373 if (!this->public.begin_handshake)
374 {
375 DBG1(DBG_TNC, "could not resolve TNC_IMC_BeginHandshake in %s: %s\n",
376 path, dlerror());
377 destroy(this);
378 return NULL;
379 }
380 this->public.receive_message =
381 dlsym(this->handle, "TNC_IMC_ReceiveMessage");
382 this->public.receive_message_long =
383 dlsym(this->handle, "TNC_IMC_ReceiveMessageLong");
384 this->public.batch_ending =
385 dlsym(this->handle, "TNC_IMC_BatchEnding");
386 this->public.terminate =
387 dlsym(this->handle, "TNC_IMC_Terminate");
388 this->public.provide_bind_function =
389 dlsym(this->handle, "TNC_IMC_ProvideBindFunction");
390 if (!this->public.provide_bind_function)
391 {
392 DBG1(DBG_TNC, "could not resolve TNC_IMC_ProvideBindFunction in %s: %s\n",
393 path, dlerror());
394 destroy(this);
395 return NULL;
396 }
397
398 return &this->public;
399 }
400
401 /**
402 * See header
403 */
404 imc_t* tnc_imc_create_from_functions(char *name,
405 TNC_IMC_InitializePointer initialize,
406 TNC_IMC_NotifyConnectionChangePointer notify_connection_change,
407 TNC_IMC_BeginHandshakePointer begin_handshake,
408 TNC_IMC_ReceiveMessagePointer receive_message,
409 TNC_IMC_ReceiveMessageLongPointer receive_message_long,
410 TNC_IMC_BatchEndingPointer batch_ending,
411 TNC_IMC_TerminatePointer terminate,
412 TNC_IMC_ProvideBindFunctionPointer provide_bind_function)
413 {
414 private_tnc_imc_t *this;
415
416 this = tnc_imc_create_empty(name);
417
418 this->public.initialize = initialize;
419 this->public.notify_connection_change = notify_connection_change;
420 this->public.begin_handshake = begin_handshake;
421 this->public.receive_message = receive_message;
422 this->public.receive_message_long = receive_message_long;
423 this->public.batch_ending = batch_ending;
424 this->public.terminate = terminate;
425 this->public.provide_bind_function = provide_bind_function;
426
427 return &this->public;
428 }