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