Moved data structures to new collections subfolder
[strongswan.git] / src / libimcv / plugins / imv_test / imv_test_state.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 "imv_test_state.h"
17
18 #include <utils/lexparser.h>
19 #include <collections/linked_list.h>
20 #include <debug.h>
21
22 typedef struct private_imv_test_state_t private_imv_test_state_t;
23
24 /**
25 * Private data of an imv_test_state_t object.
26 */
27 struct private_imv_test_state_t {
28
29 /**
30 * Public members of imv_test_state_t
31 */
32 imv_test_state_t public;
33
34 /**
35 * TNCCS connection ID
36 */
37 TNC_ConnectionID connection_id;
38
39 /**
40 * TNCCS connection state
41 */
42 TNC_ConnectionState state;
43
44 /**
45 * Does the TNCCS connection support long message types?
46 */
47 bool has_long;
48
49 /**
50 * Does the TNCCS connection support exclusive delivery?
51 */
52 bool has_excl;
53
54 /**
55 * Maximum PA-TNC message size for this TNCCS connection
56 */
57 u_int32_t max_msg_len;
58
59 /**
60 * IMV action recommendation
61 */
62 TNC_IMV_Action_Recommendation rec;
63
64 /**
65 * IMV evaluation result
66 */
67 TNC_IMV_Evaluation_Result eval;
68
69 /**
70 * List of IMCs
71 */
72 linked_list_t *imcs;
73
74 };
75
76 typedef struct imc_entry_t imc_entry_t;
77
78 /**
79 * Define an internal IMC entry
80 */
81 struct imc_entry_t {
82 TNC_UInt32 imc_id;
83 int rounds;
84 };
85
86 typedef struct entry_t entry_t;
87
88 /**
89 * Define an internal reason string entry
90 */
91 struct entry_t {
92 char *lang;
93 char *string;
94 };
95
96 /**
97 * Table of multi-lingual reason string entries
98 */
99 static entry_t reasons[] = {
100 { "en", "IMC Test was not configured with \"command = allow\"" },
101 { "de", "IMC Test wurde nicht mit \"command = allow\" konfiguriert" },
102 { "fr", "IMC Test n'etait pas configuré avec \"command = allow\"" },
103 { "pl", "IMC Test nie zostało skonfigurowany z \"command = allow\"" }
104 };
105
106 METHOD(imv_state_t, get_connection_id, TNC_ConnectionID,
107 private_imv_test_state_t *this)
108 {
109 return this->connection_id;
110 }
111
112 METHOD(imv_state_t, has_long, bool,
113 private_imv_test_state_t *this)
114 {
115 return this->has_long;
116 }
117
118 METHOD(imv_state_t, has_excl, bool,
119 private_imv_test_state_t *this)
120 {
121 return this->has_excl;
122 }
123
124 METHOD(imv_state_t, set_flags, void,
125 private_imv_test_state_t *this, bool has_long, bool has_excl)
126 {
127 this->has_long = has_long;
128 this->has_excl = has_excl;
129 }
130
131 METHOD(imv_state_t, set_max_msg_len, void,
132 private_imv_test_state_t *this, u_int32_t max_msg_len)
133 {
134 this->max_msg_len = max_msg_len;
135 }
136
137 METHOD(imv_state_t, get_max_msg_len, u_int32_t,
138 private_imv_test_state_t *this)
139 {
140 return this->max_msg_len;
141 }
142
143 METHOD(imv_state_t, change_state, void,
144 private_imv_test_state_t *this, TNC_ConnectionState new_state)
145 {
146 this->state = new_state;
147 }
148
149 METHOD(imv_state_t, get_recommendation, void,
150 private_imv_test_state_t *this, TNC_IMV_Action_Recommendation *rec,
151 TNC_IMV_Evaluation_Result *eval)
152 {
153 *rec = this->rec;
154 *eval = this->eval;
155 }
156
157 METHOD(imv_state_t, set_recommendation, void,
158 private_imv_test_state_t *this, TNC_IMV_Action_Recommendation rec,
159 TNC_IMV_Evaluation_Result eval)
160 {
161 this->rec = rec;
162 this->eval = eval;
163 }
164
165 METHOD(imv_state_t, get_reason_string, bool,
166 private_imv_test_state_t *this, chunk_t preferred_language,
167 chunk_t *reason_string, chunk_t *reason_language)
168 {
169 chunk_t pref_lang, lang;
170 u_char *pos;
171 int i;
172
173 while (eat_whitespace(&preferred_language))
174 {
175 if (!extract_token(&pref_lang, ',', &preferred_language))
176 {
177 /* last entry in a comma-separated list or single entry */
178 pref_lang = preferred_language;
179 }
180
181 /* eat trailing whitespace */
182 pos = pref_lang.ptr + pref_lang.len - 1;
183 while (pref_lang.len && *pos-- == ' ')
184 {
185 pref_lang.len--;
186 }
187
188 for (i = 0 ; i < countof(reasons); i++)
189 {
190 lang = chunk_create(reasons[i].lang, strlen(reasons[i].lang));
191 if (chunk_equals(lang, pref_lang))
192 {
193 *reason_language = lang;
194 *reason_string = chunk_create(reasons[i].string,
195 strlen(reasons[i].string));
196 return TRUE;
197 }
198 }
199 }
200
201 /* no preferred language match found - use the default language */
202 *reason_string = chunk_create(reasons[0].string,
203 strlen(reasons[0].string));
204 *reason_language = chunk_create(reasons[0].lang,
205 strlen(reasons[0].lang));
206 return TRUE;
207 }
208
209 METHOD(imv_state_t, destroy, void,
210 private_imv_test_state_t *this)
211 {
212 this->imcs->destroy_function(this->imcs, free);
213 free(this);
214 }
215
216 METHOD(imv_test_state_t, add_imc, void,
217 private_imv_test_state_t *this, TNC_UInt32 imc_id, int rounds)
218 {
219 enumerator_t *enumerator;
220 imc_entry_t *imc_entry;
221 bool found = FALSE;
222
223 enumerator = this->imcs->create_enumerator(this->imcs);
224 while (enumerator->enumerate(enumerator, &imc_entry))
225 {
226 if (imc_entry->imc_id == imc_id)
227 {
228 found = TRUE;
229 break;
230 }
231 }
232 enumerator->destroy(enumerator);
233
234 if (!found)
235 {
236 imc_entry = malloc_thing(imc_entry_t);
237 imc_entry->imc_id = imc_id;
238 imc_entry->rounds = rounds;
239 this->imcs->insert_last(this->imcs, imc_entry);
240 }
241 }
242
243 METHOD(imv_test_state_t, set_rounds, void,
244 private_imv_test_state_t *this, int rounds)
245 {
246 enumerator_t *enumerator;
247 imc_entry_t *imc_entry;
248
249 enumerator = this->imcs->create_enumerator(this->imcs);
250 while (enumerator->enumerate(enumerator, &imc_entry))
251 {
252 imc_entry->rounds = rounds;
253 }
254 enumerator->destroy(enumerator);
255 }
256
257 METHOD(imv_test_state_t, another_round, bool,
258 private_imv_test_state_t *this, TNC_UInt32 imc_id)
259 {
260 enumerator_t *enumerator;
261 imc_entry_t *imc_entry;
262 bool not_finished = FALSE;
263
264 enumerator = this->imcs->create_enumerator(this->imcs);
265 while (enumerator->enumerate(enumerator, &imc_entry))
266 {
267 if (imc_entry->rounds > 0)
268 {
269 not_finished = TRUE;
270 }
271 if (imc_entry->imc_id == imc_id)
272 {
273 imc_entry->rounds--;
274 }
275 }
276 enumerator->destroy(enumerator);
277
278 return not_finished;
279 }
280
281 /**
282 * Described in header.
283 */
284 imv_state_t *imv_test_state_create(TNC_ConnectionID connection_id)
285 {
286 private_imv_test_state_t *this;
287
288 INIT(this,
289 .public = {
290 .interface = {
291 .get_connection_id = _get_connection_id,
292 .has_long = _has_long,
293 .has_excl = _has_excl,
294 .set_flags = _set_flags,
295 .set_max_msg_len = _set_max_msg_len,
296 .get_max_msg_len = _get_max_msg_len,
297 .change_state = _change_state,
298 .get_recommendation = _get_recommendation,
299 .set_recommendation = _set_recommendation,
300 .get_reason_string = _get_reason_string,
301 .destroy = _destroy,
302 },
303 .add_imc = _add_imc,
304 .set_rounds = _set_rounds,
305 .another_round = _another_round,
306 },
307 .state = TNC_CONNECTION_STATE_CREATE,
308 .rec = TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
309 .eval = TNC_IMV_EVALUATION_RESULT_DONT_KNOW,
310 .connection_id = connection_id,
311 .imcs = linked_list_create(),
312 );
313
314 return &this->public.interface;
315 }
316
317