implemented receive_message() function
[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, set_message_types, TNC_Result,
90 private_tnc_imv_manager_t *this, TNC_IMVID id,
91 TNC_MessageTypeList supported_types,
92 TNC_UInt32 type_count)
93 {
94 enumerator_t *enumerator;
95 imv_t *imv;
96 TNC_Result result = TNC_RESULT_FATAL;
97
98 enumerator = this->imvs->create_enumerator(this->imvs);
99 while (enumerator->enumerate(enumerator, &imv))
100 {
101 if (id == imv->get_id(imv))
102 {
103 imv->set_message_types(imv, supported_types, type_count);
104 result = TNC_RESULT_SUCCESS;
105 break;
106 }
107 }
108 enumerator->destroy(enumerator);
109 return result;
110 }
111
112 METHOD(imv_manager_t, receive_message, void,
113 private_tnc_imv_manager_t *this, TNC_ConnectionID connection_id,
114 TNC_BufferReference message,
115 TNC_UInt32 message_len,
116 TNC_MessageType message_type)
117 {
118 enumerator_t *enumerator;
119 imv_t *imv;
120
121 enumerator = this->imvs->create_enumerator(this->imvs);
122 while (enumerator->enumerate(enumerator, &imv))
123 {
124 if (imv->receive_message && imv->type_supported(imv, message_type))
125 {
126 imv->receive_message(imv->get_id(imv), connection_id,
127 message, message_len, message_type);
128 }
129 }
130 enumerator->destroy(enumerator);
131 }
132
133 METHOD(imv_manager_t, destroy, void,
134 private_tnc_imv_manager_t *this)
135 {
136 imv_t *imv;
137
138 while (this->imvs->remove_last(this->imvs, (void**)&imv) == SUCCESS)
139 {
140 if (imv->terminate &&
141 imv->terminate(imv->get_id(imv)) != TNC_RESULT_SUCCESS)
142 {
143 DBG1(DBG_TNC, "IMV '%s' not terminated successfully",
144 imv->get_name(imv));
145 }
146 imv->destroy(imv);
147 }
148 this->imvs->destroy(this->imvs);
149 free(this);
150 }
151
152 /**
153 * Described in header.
154 */
155 imv_manager_t* tnc_imv_manager_create(void)
156 {
157 private_tnc_imv_manager_t *this;
158
159 INIT(this,
160 .public = {
161 .add = _add,
162 .notify_connection_change = _notify_connection_change,
163 .set_message_types = _set_message_types,
164 .receive_message = _receive_message,
165 .destroy = _destroy,
166 },
167 .imvs = linked_list_create(),
168 .next_imv_id = 1,
169 );
170
171
172 return &this->public;
173 }
174
175