Version bump to 5.9.0
[strongswan.git] / src / libimcv / plugins / imc_test / imc_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 "imc_test_state.h"
17
18 #include <tncif_names.h>
19
20 #include <utils/debug.h>
21 #include <collections/linked_list.h>
22
23 typedef struct private_imc_test_state_t private_imc_test_state_t;
24 typedef struct entry_t entry_t;
25
26 /**
27 * Private data of an imc_test_state_t object.
28 */
29 struct private_imc_test_state_t {
30
31 /**
32 * Public members of imc_test_state_t
33 */
34 imc_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 * Assessment/Evaluation Results for all IMC IDs
48 */
49 linked_list_t *results;
50
51 /**
52 * Does the TNCCS connection support long message types?
53 */
54 bool has_long;
55
56 /**
57 * Does the TNCCS connection support exclusive delivery?
58 */
59 bool has_excl;
60
61 /**
62 * Maximum PA-TNC message size for this TNCCS connection
63 */
64 uint32_t max_msg_len;
65
66 /**
67 * PA-TNC attribute segmentation contracts associated with TNCCS connection
68 */
69 seg_contract_manager_t *contracts;
70
71 /**
72 * Command to transmit to IMV
73 */
74 char *command;
75
76 /**
77 * Size of the dummy attribute value to transmit to IMV
78 */
79 int dummy_size;
80
81 /**
82 * Is it the first handshake?
83 */
84 bool first_handshake;
85
86 /**
87 * Do a handshake retry
88 */
89 bool handshake_retry;
90
91 };
92
93 /**
94 * Stores the Assessment/Evaluation Result for a given IMC ID
95 */
96 struct entry_t {
97 TNC_IMCID id;
98 TNC_IMV_Evaluation_Result result;
99 };
100
101 METHOD(imc_state_t, get_connection_id, TNC_ConnectionID,
102 private_imc_test_state_t *this)
103 {
104 return this->connection_id;
105 }
106
107 METHOD(imc_state_t, has_long, bool,
108 private_imc_test_state_t *this)
109 {
110 return this->has_long;
111 }
112
113 METHOD(imc_state_t, has_excl, bool,
114 private_imc_test_state_t *this)
115 {
116 return this->has_excl;
117 }
118
119 METHOD(imc_state_t, set_flags, void,
120 private_imc_test_state_t *this, bool has_long, bool has_excl)
121 {
122 this->has_long = has_long;
123 this->has_excl = has_excl;
124 }
125
126 METHOD(imc_state_t, set_max_msg_len, void,
127 private_imc_test_state_t *this, uint32_t max_msg_len)
128 {
129 this->max_msg_len = max_msg_len;
130 }
131
132 METHOD(imc_state_t, get_max_msg_len, uint32_t,
133 private_imc_test_state_t *this)
134 {
135 return this->max_msg_len;
136 }
137
138 METHOD(imc_state_t, get_contracts, seg_contract_manager_t*,
139 private_imc_test_state_t *this)
140 {
141 return this->contracts;
142 }
143
144 METHOD(imc_state_t, change_state, TNC_ConnectionState,
145 private_imc_test_state_t *this, TNC_ConnectionState new_state)
146 {
147 TNC_ConnectionState old_state;
148
149 old_state = this->state;
150 this->state = new_state;
151 return old_state;
152 }
153
154 METHOD(imc_state_t, set_result, void,
155 private_imc_test_state_t *this, TNC_IMCID id,
156 TNC_IMV_Evaluation_Result result)
157 {
158 enumerator_t *enumerator;
159 entry_t *entry;
160 bool found = FALSE;
161
162 enumerator = this->results->create_enumerator(this->results);
163 while (enumerator->enumerate(enumerator, &entry))
164 {
165 if (entry->id == id)
166 {
167 entry->result = result;
168 found = TRUE;
169 break;
170 }
171 }
172 enumerator->destroy(enumerator);
173
174 if (!found)
175 {
176 entry = malloc_thing(entry_t);
177 entry->id = id;
178 entry->result = result;
179 this->results->insert_last(this->results, entry);
180 }
181 }
182
183 METHOD(imc_state_t, get_result, bool,
184 private_imc_test_state_t *this, TNC_IMCID id,
185 TNC_IMV_Evaluation_Result *result)
186 {
187 enumerator_t *enumerator;
188 entry_t *entry;
189 TNC_IMV_Evaluation_Result eval = TNC_IMV_EVALUATION_RESULT_DONT_KNOW;
190
191 enumerator = this->results->create_enumerator(this->results);
192 while (enumerator->enumerate(enumerator, &entry))
193 {
194 if (entry->id == id)
195 {
196 eval = entry->result;
197 break;
198 }
199 }
200 enumerator->destroy(enumerator);
201
202 if (result)
203 {
204 *result = eval;
205 }
206 return eval != TNC_IMV_EVALUATION_RESULT_DONT_KNOW;
207 }
208
209 METHOD(imc_state_t, reset, void,
210 private_imc_test_state_t *this)
211 {
212 /* nothing to reset */
213 }
214
215 METHOD(imc_state_t, destroy, void,
216 private_imc_test_state_t *this)
217 {
218 this->results->destroy_function(this->results, free);
219 this->contracts->destroy(this->contracts);
220 free(this->command);
221 free(this);
222 }
223
224 METHOD(imc_test_state_t, get_command, char*,
225 private_imc_test_state_t *this)
226 {
227 return this->command;
228 }
229
230 METHOD(imc_test_state_t, set_command, void,
231 private_imc_test_state_t *this, char* command)
232 {
233 char *old_command;
234
235 old_command = this->command;
236 this->command = strdup(command);
237 free(old_command);
238 }
239
240 METHOD(imc_test_state_t, get_dummy_size, int,
241 private_imc_test_state_t *this)
242 {
243 return this->dummy_size;
244 }
245
246
247 METHOD(imc_test_state_t, is_first_handshake, bool,
248 private_imc_test_state_t *this)
249 {
250 bool first;
251
252 /* test and reset first_handshake flag */
253 first= this->first_handshake;
254 this->first_handshake = FALSE;
255 return first;
256 }
257
258 METHOD(imc_test_state_t, do_handshake_retry, bool,
259 private_imc_test_state_t *this)
260 {
261 bool retry;
262
263 /* test and reset handshake_retry flag */
264 retry = this->handshake_retry;
265 this->handshake_retry = FALSE;
266 return retry;
267 }
268
269 /**
270 * Described in header.
271 */
272 imc_state_t *imc_test_state_create(TNC_ConnectionID connection_id,
273 char *command, int dummy_size, bool retry)
274 {
275 private_imc_test_state_t *this;
276
277 INIT(this,
278 .public = {
279 .interface = {
280 .get_connection_id = _get_connection_id,
281 .has_long = _has_long,
282 .has_excl = _has_excl,
283 .set_flags = _set_flags,
284 .set_max_msg_len = _set_max_msg_len,
285 .get_max_msg_len = _get_max_msg_len,
286 .get_contracts = _get_contracts,
287 .change_state = _change_state,
288 .set_result = _set_result,
289 .get_result = _get_result,
290 .reset = _reset,
291 .destroy = _destroy,
292 },
293 .get_command = _get_command,
294 .set_command = _set_command,
295 .get_dummy_size = _get_dummy_size,
296 .is_first_handshake = _is_first_handshake,
297 .do_handshake_retry = _do_handshake_retry,
298 },
299 .state = TNC_CONNECTION_STATE_CREATE,
300 .results = linked_list_create(),
301 .connection_id = connection_id,
302 .contracts = seg_contract_manager_create(),
303 .command = strdup(command),
304 .dummy_size = dummy_size,
305 .first_handshake = TRUE,
306 .handshake_retry = retry,
307 );
308
309 return &this->public.interface;
310 }
311