tnc_imc/tnc_imv cosmetics
[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 /**
28 * Private data of an imv_manager_t object.
29 */
30 struct private_tnc_imv_manager_t {
31
32 /**
33 * Public members of imv_manager_t.
34 */
35 imv_manager_t public;
36
37 /**
38 * Linked list of IMVs
39 */
40 linked_list_t *imvs;
41
42 /**
43 * Next IMV ID to be assigned
44 */
45 TNC_IMVID next_imv_id;
46 };
47
48 METHOD(imv_manager_t, add, bool,
49 private_tnc_imv_manager_t *this, imv_t *imv)
50 {
51 TNC_Version version;
52
53 /* Initialize the IMV module */
54 imv->set_id(imv, this->next_imv_id);
55 if (imv->initialize(imv->get_id(imv), TNC_IFIMV_VERSION_1,
56 TNC_IFIMV_VERSION_1, &version) != TNC_RESULT_SUCCESS)
57 {
58 DBG1(DBG_TNC, "could not initialize IMV '%s'",
59 imv->get_name(imv));
60 return FALSE;
61 }
62 if (imv->provide_bind_function(imv->get_id(imv), TNC_TNCS_BindFunction)
63 != TNC_RESULT_SUCCESS)
64 {
65 DBG1(DBG_TNC, "could not provide bind function for IMV '%s'",
66 imv->get_name(imv));
67 return FALSE;
68 }
69 this->imvs->insert_last(this->imvs, imv);
70 this->next_imv_id++;
71 return TRUE;
72 }
73
74 METHOD(imv_manager_t, notify_connection_change, void,
75 private_tnc_imv_manager_t *this, TNC_ConnectionID id,
76 TNC_ConnectionState state)
77 {
78 enumerator_t *enumerator;
79 imv_t *imv;
80
81 enumerator = this->imvs->create_enumerator(this->imvs);
82 while (enumerator->enumerate(enumerator, &imv))
83 {
84 if (imv->notify_connection_change)
85 {
86 imv->notify_connection_change(imv->get_id(imv), id, state);
87 }
88 }
89 enumerator->destroy(enumerator);
90 }
91
92 METHOD(imv_manager_t, set_message_types, TNC_Result,
93 private_tnc_imv_manager_t *this, TNC_IMVID id,
94 TNC_MessageTypeList supported_types,
95 TNC_UInt32 type_count)
96 {
97 enumerator_t *enumerator;
98 imv_t *imv;
99 TNC_Result result = TNC_RESULT_FATAL;
100
101 enumerator = this->imvs->create_enumerator(this->imvs);
102 while (enumerator->enumerate(enumerator, &imv))
103 {
104 if (id == imv->get_id(imv))
105 {
106 imv->set_message_types(imv, supported_types, type_count);
107 result = TNC_RESULT_SUCCESS;
108 break;
109 }
110 }
111 enumerator->destroy(enumerator);
112 return result;
113 }
114
115 METHOD(imv_manager_t, receive_message, void,
116 private_tnc_imv_manager_t *this, TNC_ConnectionID connection_id,
117 TNC_BufferReference message,
118 TNC_UInt32 message_len,
119 TNC_MessageType message_type)
120 {
121 enumerator_t *enumerator;
122 imv_t *imv;
123
124 enumerator = this->imvs->create_enumerator(this->imvs);
125 while (enumerator->enumerate(enumerator, &imv))
126 {
127 if (imv->receive_message && imv->type_supported(imv, message_type))
128 {
129 imv->receive_message(imv->get_id(imv), connection_id,
130 message, message_len, message_type);
131 }
132 }
133 enumerator->destroy(enumerator);
134 }
135
136 METHOD(imv_manager_t, destroy, void,
137 private_tnc_imv_manager_t *this)
138 {
139 imv_t *imv;
140
141 while (this->imvs->remove_last(this->imvs, (void**)&imv) == SUCCESS)
142 {
143 if (imv->terminate &&
144 imv->terminate(imv->get_id(imv)) != TNC_RESULT_SUCCESS)
145 {
146 DBG1(DBG_TNC, "IMV '%s' not terminated successfully",
147 imv->get_name(imv));
148 }
149 imv->destroy(imv);
150 }
151 this->imvs->destroy(this->imvs);
152 free(this);
153 }
154
155 /**
156 * Described in header.
157 */
158 imv_manager_t* tnc_imv_manager_create(void)
159 {
160 private_tnc_imv_manager_t *this;
161
162 INIT(this,
163 .public = {
164 .add = _add,
165 .notify_connection_change = _notify_connection_change,
166 .set_message_types = _set_message_types,
167 .receive_message = _receive_message,
168 .destroy = _destroy,
169 },
170 .imvs = linked_list_create(),
171 .next_imv_id = 1,
172 );
173
174
175 return &this->public;
176 }
177
178