allow registration of multiple message type
[strongswan.git] / src / libimcv / imc / imc_agent.c
1 /*
2 * Copyright (C) 2011-2012 Andreas Steffen
3 * 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 "imcv.h"
17 #include "imc_agent.h"
18
19 #include <tncif_names.h>
20
21 #include <debug.h>
22 #include <threading/rwlock.h>
23
24 typedef struct private_imc_agent_t private_imc_agent_t;
25
26 /**
27 * Private data of an imc_agent_t object.
28 */
29 struct private_imc_agent_t {
30
31 /**
32 * Public members of imc_agent_t
33 */
34 imc_agent_t public;
35
36 /**
37 * name of IMC
38 */
39 const char *name;
40
41 /**
42 * message types registered by IMC
43 */
44 pen_type_t *supported_types;
45
46 /**
47 * number of message types registered by IMC
48 */
49 u_int32_t type_count;
50
51 /**
52 * ID of IMC as assigned by TNCC
53 */
54 TNC_IMCID id;
55
56 /**
57 * List of additional IMC IDs assigned by TNCC
58 */
59 linked_list_t *additional_ids;
60
61 /**
62 * list of TNCC connection entries
63 */
64 linked_list_t *connections;
65
66 /**
67 * rwlock to lock TNCC connection entries
68 */
69 rwlock_t *connection_lock;
70
71 /**
72 * Inform a TNCC about the set of message types the IMC is able to receive
73 *
74 * @param imc_id IMC ID assigned by TNCC
75 * @param supported_types list of supported message types
76 * @param type_count number of list elements
77 * @return TNC result code
78 */
79 TNC_Result (*report_message_types)(TNC_IMCID imc_id,
80 TNC_MessageTypeList supported_types,
81 TNC_UInt32 type_count);
82
83 /**
84 * Inform a TNCC about the set of message types the IMC is able to receive
85 *
86 * @param imc_id IMC ID assigned by TNCC
87 * @param supported_vids list of supported message vendor IDs
88 * @param supported_subtypes list of supported message subtypes
89 * @param type_count number of list elements
90 * @return TNC result code
91 */
92 TNC_Result (*report_message_types_long)(TNC_IMCID imc_id,
93 TNC_VendorIDList supported_vids,
94 TNC_MessageSubtypeList supported_subtypes,
95 TNC_UInt32 type_count);
96
97 /**
98 * Call when an IMC-IMC message is to be sent
99 *
100 * @param imc_id IMC ID assigned by TNCC
101 * @param connection_id network connection ID assigned by TNCC
102 * @param msg message to send
103 * @param msg_len message length in bytes
104 * @param msg_type message type
105 * @return TNC result code
106 */
107 TNC_Result (*send_message)(TNC_IMCID imc_id,
108 TNC_ConnectionID connection_id,
109 TNC_BufferReference msg,
110 TNC_UInt32 msg_len,
111 TNC_MessageType msg_type);
112
113
114 /**
115 * Call when an IMC-IMC message is to be sent with long message types
116 *
117 * @param imc_id IMC ID assigned by TNCC
118 * @param connection_id network connection ID assigned by TNCC
119 * @param msg_flags message flags
120 * @param msg message to send
121 * @param msg_len message length in bytes
122 * @param msg_vid message vendor ID
123 * @param msg_subtype message subtype
124 * @param dst_imc_id destination IMV ID
125 * @return TNC result code
126 */
127 TNC_Result (*send_message_long)(TNC_IMCID imc_id,
128 TNC_ConnectionID connection_id,
129 TNC_UInt32 msg_flags,
130 TNC_BufferReference msg,
131 TNC_UInt32 msg_len,
132 TNC_VendorID msg_vid,
133 TNC_MessageSubtype msg_subtype,
134 TNC_UInt32 dst_imv_id);
135
136 /**
137 * Get the value of an attribute associated with a connection
138 * or with the TNCC as a whole.
139 *
140 * @param imc_id IMC ID assigned by TNCC
141 * @param connection_id network connection ID assigned by TNCC
142 * @param attribute_id attribute ID
143 * @param buffer_len length of buffer in bytes
144 * @param buffer buffer
145 * @param out_value_len size in bytes of attribute stored in buffer
146 * @return TNC result code
147 */
148 TNC_Result (*get_attribute)(TNC_IMCID imc_id,
149 TNC_ConnectionID connection_id,
150 TNC_AttributeID attribute_id,
151 TNC_UInt32 buffer_len,
152 TNC_BufferReference buffer,
153 TNC_UInt32 *out_value_len);
154
155 /**
156 * Set the value of an attribute associated with a connection
157 * or with the TNCC as a whole.
158 *
159 * @param imc_id IMV ID assigned by TNCC
160 * @param connection_id network connection ID assigned by TNCC
161 * @param attribute_id attribute ID
162 * @param buffer_len length of buffer in bytes
163 * @param buffer buffer
164 * @return TNC result code
165 */
166 TNC_Result (*set_attribute)(TNC_IMCID imc_id,
167 TNC_ConnectionID connection_id,
168 TNC_AttributeID attribute_id,
169 TNC_UInt32 buffer_len,
170 TNC_BufferReference buffer);
171
172 /**
173 * Reserve an additional IMC ID
174 *
175 * @param imc_id primary IMC ID assigned by TNCC
176 * @param out_imc_id additional IMC ID assigned by TNCC
177 * @return TNC result code
178 */
179 TNC_Result (*reserve_additional_id)(TNC_IMCID imc_id,
180 TNC_UInt32 *out_imc_id);
181
182 };
183
184 METHOD(imc_agent_t, bind_functions, TNC_Result,
185 private_imc_agent_t *this, TNC_TNCC_BindFunctionPointer bind_function)
186 {
187 if (!bind_function)
188 {
189 DBG1(DBG_IMC, "TNC client failed to provide bind function");
190 return TNC_RESULT_INVALID_PARAMETER;
191 }
192 if (bind_function(this->id, "TNC_TNCC_ReportMessageTypes",
193 (void**)&this->report_message_types) != TNC_RESULT_SUCCESS)
194 {
195 this->report_message_types = NULL;
196 }
197 if (bind_function(this->id, "TNC_TNCC_ReportMessageTypesLong",
198 (void**)&this->report_message_types_long) != TNC_RESULT_SUCCESS)
199 {
200 this->report_message_types_long = NULL;
201 }
202 if (bind_function(this->id, "TNC_TNCC_RequestHandshakeRetry",
203 (void**)&this->public.request_handshake_retry) != TNC_RESULT_SUCCESS)
204 {
205 this->public.request_handshake_retry = NULL;
206 }
207 if (bind_function(this->id, "TNC_TNCC_SendMessage",
208 (void**)&this->send_message) != TNC_RESULT_SUCCESS)
209 {
210 this->send_message = NULL;
211 }
212 if (bind_function(this->id, "TNC_TNCC_SendMessageLong",
213 (void**)&this->send_message_long) != TNC_RESULT_SUCCESS)
214 {
215 this->send_message_long = NULL;
216 }
217 if (bind_function(this->id, "TNC_TNCC_GetAttribute",
218 (void**)&this->get_attribute) != TNC_RESULT_SUCCESS)
219 {
220 this->get_attribute = NULL;
221 }
222 if (bind_function(this->id, "TNC_TNCC_SetAttribute",
223 (void**)&this->set_attribute) != TNC_RESULT_SUCCESS)
224 {
225 this->set_attribute = NULL;
226 }
227 if (bind_function(this->id, "TNC_TNCC_ReserveAdditionalIMCID",
228 (void**)&this->reserve_additional_id) != TNC_RESULT_SUCCESS)
229 {
230 this->reserve_additional_id = NULL;
231 }
232 DBG2(DBG_IMC, "IMC %u \"%s\" provided with bind function",
233 this->id, this->name);
234
235 if (this->report_message_types_long)
236 {
237 TNC_VendorIDList vendor_id_list;
238 TNC_MessageSubtypeList subtype_list;
239 int i;
240
241 vendor_id_list = malloc(this->type_count * sizeof(TNC_UInt32));
242 subtype_list = malloc(this->type_count * sizeof(TNC_UInt32));
243
244 for (i = 0; i < this->type_count; i++)
245 {
246 vendor_id_list[i] = this->supported_types[i].vendor_id;
247 subtype_list[i] = this->supported_types[i].type;
248 }
249 this->report_message_types_long(this->id, vendor_id_list, subtype_list,
250 this->type_count);
251 free(vendor_id_list);
252 free(subtype_list);
253 }
254 else if (this->report_message_types)
255 {
256 TNC_MessageTypeList type_list;
257 int i;
258
259 type_list = malloc(this->type_count * sizeof(TNC_UInt32));
260
261 for (i = 0; i < this->type_count; i++)
262 {
263 type_list[i] = (this->supported_types[i].vendor_id << 8) |
264 (this->supported_types[i].type & 0xff);
265 }
266 this->report_message_types(this->id, type_list, this->type_count);
267 free(type_list);
268 }
269 return TNC_RESULT_SUCCESS;
270 }
271
272 /**
273 * finds a connection state based on its Connection ID
274 */
275 static imc_state_t* find_connection(private_imc_agent_t *this,
276 TNC_ConnectionID id)
277 {
278 enumerator_t *enumerator;
279 imc_state_t *state, *found = NULL;
280
281 this->connection_lock->read_lock(this->connection_lock);
282 enumerator = this->connections->create_enumerator(this->connections);
283 while (enumerator->enumerate(enumerator, &state))
284 {
285 if (id == state->get_connection_id(state))
286 {
287 found = state;
288 break;
289 }
290 }
291 enumerator->destroy(enumerator);
292 this->connection_lock->unlock(this->connection_lock);
293
294 return found;
295 }
296
297 /**
298 * delete a connection state with a given Connection ID
299 */
300 static bool delete_connection(private_imc_agent_t *this, TNC_ConnectionID id)
301 {
302 enumerator_t *enumerator;
303 imc_state_t *state;
304 bool found = FALSE;
305
306 this->connection_lock->write_lock(this->connection_lock);
307 enumerator = this->connections->create_enumerator(this->connections);
308 while (enumerator->enumerate(enumerator, &state))
309 {
310 if (id == state->get_connection_id(state))
311 {
312 found = TRUE;
313 state->destroy(state);
314 this->connections->remove_at(this->connections, enumerator);
315 break;
316 }
317 }
318 enumerator->destroy(enumerator);
319 this->connection_lock->unlock(this->connection_lock);
320
321 return found;
322 }
323
324 /**
325 * Read a boolean attribute
326 */
327 static bool get_bool_attribute(private_imc_agent_t *this, TNC_ConnectionID id,
328 TNC_AttributeID attribute_id)
329 {
330 TNC_UInt32 len;
331 char buf[4];
332
333 return this->get_attribute &&
334 this->get_attribute(this->id, id, attribute_id, 4, buf, &len) ==
335 TNC_RESULT_SUCCESS && len == 1 && *buf == 0x01;
336 }
337
338 /**
339 * Read a string attribute
340 */
341 static char* get_str_attribute(private_imc_agent_t *this, TNC_ConnectionID id,
342 TNC_AttributeID attribute_id)
343 {
344 TNC_UInt32 len;
345 char buf[BUF_LEN];
346
347 if (this->get_attribute &&
348 this->get_attribute(this->id, id, attribute_id, BUF_LEN, buf, &len) ==
349 TNC_RESULT_SUCCESS && len <= BUF_LEN)
350 {
351 return strdup(buf);
352 }
353 return NULL;
354 }
355
356 /**
357 * Read an UInt32 attribute
358 */
359 static u_int32_t get_uint_attribute(private_imc_agent_t *this, TNC_ConnectionID id,
360 TNC_AttributeID attribute_id)
361 {
362 TNC_UInt32 len;
363 char buf[4];
364
365 if (this->get_attribute &&
366 this->get_attribute(this->id, id, attribute_id, 4, buf, &len) ==
367 TNC_RESULT_SUCCESS && len == 4)
368 {
369 return untoh32(buf);
370 }
371 return 0;
372 }
373
374 METHOD(imc_agent_t, create_state, TNC_Result,
375 private_imc_agent_t *this, imc_state_t *state)
376 {
377 TNC_ConnectionID conn_id;
378 char *tnccs_p = NULL, *tnccs_v = NULL, *t_p = NULL, *t_v = NULL;
379 bool has_long = FALSE, has_excl = FALSE, has_soh = FALSE;
380 u_int32_t max_msg_len;
381
382 conn_id = state->get_connection_id(state);
383 if (find_connection(this, conn_id))
384 {
385 DBG1(DBG_IMC, "IMC %u \"%s\" already created a state for Connection ID %u",
386 this->id, this->name, conn_id);
387 state->destroy(state);
388 return TNC_RESULT_OTHER;
389 }
390
391 /* Get and display attributes from TNCC via IF-IMC */
392 has_long = get_bool_attribute(this, conn_id, TNC_ATTRIBUTEID_HAS_LONG_TYPES);
393 has_excl = get_bool_attribute(this, conn_id, TNC_ATTRIBUTEID_HAS_EXCLUSIVE);
394 has_soh = get_bool_attribute(this, conn_id, TNC_ATTRIBUTEID_HAS_SOH);
395 tnccs_p = get_str_attribute(this, conn_id, TNC_ATTRIBUTEID_IFTNCCS_PROTOCOL);
396 tnccs_v = get_str_attribute(this, conn_id, TNC_ATTRIBUTEID_IFTNCCS_VERSION);
397 t_p = get_str_attribute(this, conn_id, TNC_ATTRIBUTEID_IFT_PROTOCOL);
398 t_v = get_str_attribute(this, conn_id, TNC_ATTRIBUTEID_IFT_VERSION);
399 max_msg_len = get_uint_attribute(this, conn_id, TNC_ATTRIBUTEID_MAX_MESSAGE_SIZE);
400
401 state->set_flags(state, has_long, has_excl);
402 state->set_max_msg_len(state, max_msg_len);
403
404 DBG2(DBG_IMC, "IMC %u \"%s\" created a state for %s %s Connection ID %u: "
405 "%slong %sexcl %ssoh", this->id, this->name,
406 tnccs_p ? tnccs_p:"?", tnccs_v ? tnccs_v:"?", conn_id,
407 has_long ? "+":"-", has_excl ? "+":"-", has_soh ? "+":"-");
408 DBG2(DBG_IMC, " over %s %s with maximum PA-TNC message size of %u bytes",
409 t_p ? t_p:"?", t_v ? t_v :"?", max_msg_len);
410
411 free(tnccs_p);
412 free(tnccs_v);
413 free(t_p);
414 free(t_v);
415
416 this->connection_lock->write_lock(this->connection_lock);
417 this->connections->insert_last(this->connections, state);
418 this->connection_lock->unlock(this->connection_lock);
419 return TNC_RESULT_SUCCESS;
420 }
421
422 METHOD(imc_agent_t, delete_state, TNC_Result,
423 private_imc_agent_t *this, TNC_ConnectionID connection_id)
424 {
425 if (!delete_connection(this, connection_id))
426 {
427 DBG1(DBG_IMC, "IMC %u \"%s\" has no state for Connection ID %u",
428 this->id, this->name, connection_id);
429 return TNC_RESULT_FATAL;
430 }
431 DBG2(DBG_IMC, "IMC %u \"%s\" deleted the state of Connection ID %u",
432 this->id, this->name, connection_id);
433 return TNC_RESULT_SUCCESS;
434 }
435
436 METHOD(imc_agent_t, change_state, TNC_Result,
437 private_imc_agent_t *this, TNC_ConnectionID connection_id,
438 TNC_ConnectionState new_state,
439 imc_state_t **state_p)
440 {
441 imc_state_t *state;
442
443 switch (new_state)
444 {
445 case TNC_CONNECTION_STATE_HANDSHAKE:
446 case TNC_CONNECTION_STATE_ACCESS_ALLOWED:
447 case TNC_CONNECTION_STATE_ACCESS_ISOLATED:
448 case TNC_CONNECTION_STATE_ACCESS_NONE:
449 state = find_connection(this, connection_id);
450
451 if (!state)
452 {
453 DBG1(DBG_IMC, "IMC %u \"%s\" has no state for Connection ID %u",
454 this->id, this->name, connection_id);
455 return TNC_RESULT_FATAL;
456 }
457 state->change_state(state, new_state);
458 DBG2(DBG_IMC, "IMC %u \"%s\" changed state of Connection ID %u to '%N'",
459 this->id, this->name, connection_id,
460 TNC_Connection_State_names, new_state);
461 if (state_p)
462 {
463 *state_p = state;
464 }
465 break;
466 case TNC_CONNECTION_STATE_CREATE:
467 DBG1(DBG_IMC, "state '%N' should be handled by create_state()",
468 TNC_Connection_State_names, new_state);
469 return TNC_RESULT_FATAL;
470 case TNC_CONNECTION_STATE_DELETE:
471 DBG1(DBG_IMC, "state '%N' should be handled by delete_state()",
472 TNC_Connection_State_names, new_state);
473 return TNC_RESULT_FATAL;
474 default:
475 DBG1(DBG_IMC, "IMC %u \"%s\" was notified of unknown state %u "
476 "for Connection ID %u",
477 this->id, this->name, new_state, connection_id);
478 return TNC_RESULT_INVALID_PARAMETER;
479 }
480 return TNC_RESULT_SUCCESS;
481 }
482
483 METHOD(imc_agent_t, get_state, bool,
484 private_imc_agent_t *this, TNC_ConnectionID connection_id,
485 imc_state_t **state)
486 {
487 *state = find_connection(this, connection_id);
488 if (!*state)
489 {
490 DBG1(DBG_IMC, "IMC %u \"%s\" has no state for Connection ID %u",
491 this->id, this->name, connection_id);
492 return FALSE;
493 }
494 return TRUE;
495 }
496
497 METHOD(imc_agent_t, send_message, TNC_Result,
498 private_imc_agent_t *this, TNC_ConnectionID connection_id, bool excl,
499 TNC_UInt32 src_imc_id, TNC_UInt32 dst_imv_id, TNC_VendorID msg_vid,
500 TNC_MessageSubtype msg_subtype, linked_list_t *attr_list)
501 {
502 TNC_MessageType type;
503 TNC_UInt32 msg_flags;
504 TNC_Result result = TNC_RESULT_FATAL;
505 imc_state_t *state;
506 pa_tnc_attr_t *attr;
507 pa_tnc_msg_t *pa_tnc_msg;
508 chunk_t msg;
509 enumerator_t *enumerator;
510 bool attr_added;
511
512 state = find_connection(this, connection_id);
513 if (!state)
514 {
515 DBG1(DBG_IMV, "IMC %u \"%s\" has no state for Connection ID %u",
516 this->id, this->name, connection_id);
517 return TNC_RESULT_FATAL;
518 }
519
520 while (attr_list->get_count(attr_list))
521 {
522 pa_tnc_msg = pa_tnc_msg_create(state->get_max_msg_len(state));
523 attr_added = FALSE;
524
525 enumerator = attr_list->create_enumerator(attr_list);
526 while (enumerator->enumerate(enumerator, &attr))
527 {
528 if (pa_tnc_msg->add_attribute(pa_tnc_msg, attr))
529 {
530 attr_added = TRUE;
531 }
532 else
533 {
534 if (attr_added)
535 {
536 break;
537 }
538 else
539 {
540 DBG1(DBG_IMC, "PA-TNC attribute too large to send, deleted");
541 attr->destroy(attr);
542 }
543 }
544 attr_list->remove_at(attr_list, enumerator);
545 }
546 enumerator->destroy(enumerator);
547
548 /* build and send the PA-TNC message via the IF-IMC interface */
549 if (!pa_tnc_msg->build(pa_tnc_msg))
550 {
551 pa_tnc_msg->destroy(pa_tnc_msg);
552 return TNC_RESULT_FATAL;
553 }
554 msg = pa_tnc_msg->get_encoding(pa_tnc_msg);
555
556 if (state->has_long(state) && this->send_message_long)
557 {
558 if (!src_imc_id)
559 {
560 src_imc_id = this->id;
561 }
562 msg_flags = excl ? TNC_MESSAGE_FLAGS_EXCLUSIVE : 0;
563
564 result = this->send_message_long(src_imc_id, connection_id,
565 msg_flags, msg.ptr, msg.len, msg_vid,
566 msg_subtype, dst_imv_id);
567 }
568 else if (this->send_message)
569 {
570 type = msg_vid << 8 | msg_subtype;
571
572 result = this->send_message(this->id, connection_id, msg.ptr,
573 msg.len, type);
574 }
575
576 pa_tnc_msg->destroy(pa_tnc_msg);
577
578 if (result != TNC_RESULT_SUCCESS)
579 {
580 break;
581 }
582 }
583 return result;
584 }
585
586 METHOD(imc_agent_t, receive_message, TNC_Result,
587 private_imc_agent_t *this, imc_state_t *state, chunk_t msg,
588 TNC_VendorID msg_vid, TNC_MessageSubtype msg_subtype,
589 TNC_UInt32 src_imv_id, TNC_UInt32 dst_imc_id, pa_tnc_msg_t **pa_tnc_msg)
590 {
591 pa_tnc_msg_t *pa_msg;
592 pa_tnc_attr_t *error_attr;
593 linked_list_t *error_attr_list;
594 enumerator_t *enumerator;
595 TNC_UInt32 src_imc_id, dst_imv_id;
596 TNC_ConnectionID connection_id;
597 TNC_Result result;
598
599 connection_id = state->get_connection_id(state);
600
601 if (state->has_long(state))
602 {
603 if (dst_imc_id != TNC_IMCID_ANY)
604 {
605 DBG2(DBG_IMC, "IMC %u \"%s\" received message for Connection ID %u "
606 "from IMV %u to IMC %u", this->id, this->name,
607 connection_id, src_imv_id, dst_imc_id);
608 }
609 else
610 {
611 DBG2(DBG_IMC, "IMC %u \"%s\" received message for Connection ID %u "
612 "from IMV %u", this->id, this->name, connection_id,
613 src_imv_id);
614 }
615 }
616 else
617 {
618 DBG2(DBG_IMC, "IMC %u \"%s\" received message for Connection ID %u",
619 this->id, this->name, connection_id);
620 }
621
622 *pa_tnc_msg = NULL;
623 pa_msg = pa_tnc_msg_create_from_data(msg);
624
625 switch (pa_msg->process(pa_msg))
626 {
627 case SUCCESS:
628 *pa_tnc_msg = pa_msg;
629 break;
630 case VERIFY_ERROR:
631 /* extract and copy by refence all error attributes */
632 error_attr_list = linked_list_create();
633
634 enumerator = pa_msg->create_error_enumerator(pa_msg);
635 while (enumerator->enumerate(enumerator, &error_attr))
636 {
637 error_attr_list->insert_last(error_attr_list,
638 error_attr->get_ref(error_attr));
639 }
640 enumerator->destroy(enumerator);
641
642 src_imc_id = (dst_imc_id == TNC_IMCID_ANY) ? this->id : dst_imc_id;
643 dst_imv_id = state->has_excl(state) ? src_imv_id : TNC_IMVID_ANY;
644
645 result = send_message(this, connection_id, state->has_excl(state),
646 src_imc_id, dst_imv_id, msg_vid, msg_subtype,
647 error_attr_list);
648
649 error_attr_list->destroy(error_attr_list);
650 pa_msg->destroy(pa_msg);
651 return result;
652 case FAILED:
653 default:
654 pa_msg->destroy(pa_msg);
655 return TNC_RESULT_FATAL;
656 }
657 return TNC_RESULT_SUCCESS;
658 }
659
660 METHOD(imc_agent_t, reserve_additional_ids, TNC_Result,
661 private_imc_agent_t *this, int count)
662 {
663 TNC_Result result;
664 TNC_UInt32 id;
665 void *pointer;
666
667 if (!this->reserve_additional_id)
668 {
669 DBG1(DBG_IMC, "IMC %u \"%s\" did not detect the capability to reserve "
670 "additional IMC IDs from the TNCC", this->id, this->name);
671 return TNC_RESULT_ILLEGAL_OPERATION;
672 }
673 while (count > 0)
674 {
675 result = this->reserve_additional_id(this->id, &id);
676 if (result != TNC_RESULT_SUCCESS)
677 {
678 DBG1(DBG_IMC, "IMC %u \"%s\" failed to reserve %d additional IMC IDs",
679 this->id, this->name, count);
680 return result;
681 }
682 count--;
683
684 /* store the scalar value in the pointer */
685 pointer = (void*)id;
686 this->additional_ids->insert_last(this->additional_ids, pointer);
687 DBG2(DBG_IMC, "IMC %u \"%s\" reserved additional ID %u",
688 this->id, this->name, id);
689 }
690 return TNC_RESULT_SUCCESS;
691 }
692
693 METHOD(imc_agent_t, count_additional_ids, int,
694 private_imc_agent_t *this)
695 {
696 return this->additional_ids->get_count(this->additional_ids);
697 }
698
699 METHOD(imc_agent_t, create_id_enumerator, enumerator_t*,
700 private_imc_agent_t *this)
701 {
702 return this->additional_ids->create_enumerator(this->additional_ids);
703 }
704
705 METHOD(imc_agent_t, destroy, void,
706 private_imc_agent_t *this)
707 {
708 DBG1(DBG_IMC, "IMC %u \"%s\" terminated", this->id, this->name);
709 this->additional_ids->destroy(this->additional_ids);
710 this->connections->destroy_function(this->connections, free);
711 this->connection_lock->destroy(this->connection_lock);
712 free(this);
713
714 /* decrease the reference count or terminate */
715 libimcv_deinit();
716 }
717
718 /**
719 * Described in header.
720 */
721 imc_agent_t *imc_agent_create(const char *name,
722 pen_type_t *supported_types, u_int32_t type_count,
723 TNC_IMCID id, TNC_Version *actual_version)
724 {
725 private_imc_agent_t *this;
726
727 /* initialize or increase the reference count */
728 if (!libimcv_init())
729 {
730 return NULL;
731 }
732
733 INIT(this,
734 .public = {
735 .bind_functions = _bind_functions,
736 .create_state = _create_state,
737 .delete_state = _delete_state,
738 .change_state = _change_state,
739 .get_state = _get_state,
740 .send_message = _send_message,
741 .receive_message = _receive_message,
742 .reserve_additional_ids = _reserve_additional_ids,
743 .count_additional_ids = _count_additional_ids,
744 .create_id_enumerator = _create_id_enumerator,
745 .destroy = _destroy,
746 },
747 .name = name,
748 .supported_types = supported_types,
749 .type_count = type_count,
750 .id = id,
751 .additional_ids = linked_list_create(),
752 .connections = linked_list_create(),
753 .connection_lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
754 );
755
756 *actual_version = TNC_IFIMC_VERSION_1;
757 DBG1(DBG_IMC, "IMC %u \"%s\" initialized", this->id, this->name);
758
759 return &this->public;
760 }
761