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