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