implement IMC and IMV manager classes
[strongswan.git] / src / libcharon / plugins / tnc_imv / tnc_imv_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_imv_manager.h"
17
18 #include <tnc/imv/imv_manager.h>
19 #include <tnc/tncifimv.h>
20
21 #include <debug.h>
22 #include <library.h>
23 #include <utils/linked_list.h>
24
25 typedef struct private_tnc_imv_manager_t private_tnc_imv_manager_t;
26
27 struct private_tnc_imv_manager_t {
28
29 /**
30 * Public members of imv_manager_t.
31 */
32 imv_manager_t public;
33
34 /**
35 * Linked list of IMVs
36 */
37 linked_list_t *imvs;
38
39 /**
40 * Next IMV ID to be assigned
41 */
42 TNC_IMVID next_imv_id;
43 };
44
45 METHOD(imv_manager_t, add, bool,
46 private_tnc_imv_manager_t *this, imv_t *imv)
47 {
48 TNC_Version version;
49
50 /* Initialize the IMV module */
51 imv->set_id(imv, this->next_imv_id);
52 if (imv->initialize(imv->get_id(imv), TNC_IFIMV_VERSION_1,
53 TNC_IFIMV_VERSION_1, &version) != TNC_RESULT_SUCCESS)
54 {
55 DBG1(DBG_TNC, "could not initialize IMV '%s'",
56 imv->get_name(imv));
57 return FALSE;
58 }
59 if (imv->provide_bind_function(imv->get_id(imv), TNC_TNCS_BindFunction)
60 != TNC_RESULT_SUCCESS)
61 {
62 DBG1(DBG_TNC, "could not provide bind function for IMV '%s'",
63 imv->get_name(imv));
64 return FALSE;
65 }
66 this->imvs->insert_last(this->imvs, imv);
67 this->next_imv_id++;
68 return TRUE;
69 }
70
71 METHOD(imv_manager_t, notify_connection_change, void,
72 private_tnc_imv_manager_t *this, TNC_ConnectionID id,
73 TNC_ConnectionState state)
74 {
75 enumerator_t *enumerator;
76 imv_t *imv;
77
78 enumerator = this->imvs->create_enumerator(this->imvs);
79 while (enumerator->enumerate(enumerator, &imv))
80 {
81 if (imv->notify_connection_change)
82 {
83 imv->notify_connection_change(imv->get_id(imv), id, state);
84 }
85 }
86 enumerator->destroy(enumerator);
87 }
88
89 METHOD(imv_manager_t, destroy, void,
90 private_tnc_imv_manager_t *this)
91 {
92 imv_t *imv;
93
94 while (this->imvs->remove_last(this->imvs, (void**)&imv) == SUCCESS)
95 {
96 if (imv->terminate &&
97 imv->terminate(imv->get_id(imv)) != TNC_RESULT_SUCCESS)
98 {
99 DBG1(DBG_TNC, "IMV '%s' not terminated successfully",
100 imv->get_name(imv));
101 }
102 imv->destroy(imv);
103 }
104 this->imvs->destroy(this->imvs);
105 free(this);
106 }
107
108 /**
109 * Described in header.
110 */
111 imv_manager_t* tnc_imv_manager_create(void)
112 {
113 private_tnc_imv_manager_t *this;
114
115 INIT(this,
116 .public = {
117 .add = _add,
118 .notify_connection_change = _notify_connection_change,
119 .destroy = _destroy,
120 },
121 .imvs = linked_list_create(),
122 .next_imv_id = 1,
123 );
124
125
126 return &this->public;
127 }
128
129