Added add_segment() method to IETF attributes
[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 u_int32_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, u_int32_t max_msg_len)
128 {
129 this->max_msg_len = max_msg_len;
130 }
131
132 METHOD(imc_state_t, get_max_msg_len, u_int32_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, void,
145 private_imc_test_state_t *this, TNC_ConnectionState new_state)
146 {
147 this->state = new_state;
148 }
149
150 METHOD(imc_state_t, set_result, void,
151 private_imc_test_state_t *this, TNC_IMCID id,
152 TNC_IMV_Evaluation_Result result)
153 {
154 enumerator_t *enumerator;
155 entry_t *entry;
156 bool found = FALSE;
157
158 enumerator = this->results->create_enumerator(this->results);
159 while (enumerator->enumerate(enumerator, &entry))
160 {
161 if (entry->id == id)
162 {
163 entry->result = result;
164 found = TRUE;
165 break;
166 }
167 }
168 enumerator->destroy(enumerator);
169
170 if (!found)
171 {
172 entry = malloc_thing(entry_t);
173 entry->id = id;
174 entry->result = result;
175 this->results->insert_last(this->results, entry);
176 }
177 }
178
179 METHOD(imc_state_t, get_result, bool,
180 private_imc_test_state_t *this, TNC_IMCID id,
181 TNC_IMV_Evaluation_Result *result)
182 {
183 enumerator_t *enumerator;
184 entry_t *entry;
185 TNC_IMV_Evaluation_Result eval = TNC_IMV_EVALUATION_RESULT_DONT_KNOW;
186
187 enumerator = this->results->create_enumerator(this->results);
188 while (enumerator->enumerate(enumerator, &entry))
189 {
190 if (entry->id == id)
191 {
192 eval = entry->result;
193 break;
194 }
195 }
196 enumerator->destroy(enumerator);
197
198 if (result)
199 {
200 *result = eval;
201 }
202 return eval != TNC_IMV_EVALUATION_RESULT_DONT_KNOW;
203 }
204
205 METHOD(imc_state_t, destroy, void,
206 private_imc_test_state_t *this)
207 {
208 this->results->destroy_function(this->results, free);
209 this->contracts->destroy(this->contracts);
210 free(this->command);
211 free(this);
212 }
213
214 METHOD(imc_test_state_t, get_command, char*,
215 private_imc_test_state_t *this)
216 {
217 return this->command;
218 }
219
220 METHOD(imc_test_state_t, set_command, void,
221 private_imc_test_state_t *this, char* command)
222 {
223 char *old_command;
224
225 old_command = this->command;
226 this->command = strdup(command);
227 free(old_command);
228 }
229
230 METHOD(imc_test_state_t, get_dummy_size, int,
231 private_imc_test_state_t *this)
232 {
233 return this->dummy_size;
234 }
235
236
237 METHOD(imc_test_state_t, is_first_handshake, bool,
238 private_imc_test_state_t *this)
239 {
240 bool first;
241
242 /* test and reset first_handshake flag */
243 first= this->first_handshake;
244 this->first_handshake = FALSE;
245 return first;
246 }
247
248 METHOD(imc_test_state_t, do_handshake_retry, bool,
249 private_imc_test_state_t *this)
250 {
251 bool retry;
252
253 /* test and reset handshake_retry flag */
254 retry = this->handshake_retry;
255 this->handshake_retry = FALSE;
256 return retry;
257 }
258
259 /**
260 * Described in header.
261 */
262 imc_state_t *imc_test_state_create(TNC_ConnectionID connection_id,
263 char *command, int dummy_size, bool retry)
264 {
265 private_imc_test_state_t *this;
266
267 INIT(this,
268 .public = {
269 .interface = {
270 .get_connection_id = _get_connection_id,
271 .has_long = _has_long,
272 .has_excl = _has_excl,
273 .set_flags = _set_flags,
274 .set_max_msg_len = _set_max_msg_len,
275 .get_max_msg_len = _get_max_msg_len,
276 .get_contracts = _get_contracts,
277 .change_state = _change_state,
278 .set_result = _set_result,
279 .get_result = _get_result,
280 .destroy = _destroy,
281 },
282 .get_command = _get_command,
283 .set_command = _set_command,
284 .get_dummy_size = _get_dummy_size,
285 .is_first_handshake = _is_first_handshake,
286 .do_handshake_retry = _do_handshake_retry,
287 },
288 .state = TNC_CONNECTION_STATE_CREATE,
289 .results = linked_list_create(),
290 .connection_id = connection_id,
291 .contracts = seg_contract_manager_create(),
292 .command = strdup(command),
293 .dummy_size = dummy_size,
294 .first_handshake = TRUE,
295 .handshake_retry = retry,
296 );
297
298 return &this->public.interface;
299 }
300