Build all IMC/IMVs with -no-undefined
[strongswan.git] / src / libimcv / plugins / imc_test / imc_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 "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 * Command to transmit to IMV
68 */
69 char *command;
70
71 /**
72 * Size of the dummy attribute value to transmit to IMV
73 */
74 int dummy_size;
75
76 /**
77 * Is it the first handshake?
78 */
79 bool first_handshake;
80
81 /**
82 * Do a handshake retry
83 */
84 bool handshake_retry;
85
86 };
87
88 /**
89 * Stores the Assessment/Evaluation Result for a given IMC ID
90 */
91 struct entry_t {
92 TNC_IMCID id;
93 TNC_IMV_Evaluation_Result result;
94 };
95
96 METHOD(imc_state_t, get_connection_id, TNC_ConnectionID,
97 private_imc_test_state_t *this)
98 {
99 return this->connection_id;
100 }
101
102 METHOD(imc_state_t, has_long, bool,
103 private_imc_test_state_t *this)
104 {
105 return this->has_long;
106 }
107
108 METHOD(imc_state_t, has_excl, bool,
109 private_imc_test_state_t *this)
110 {
111 return this->has_excl;
112 }
113
114 METHOD(imc_state_t, set_flags, void,
115 private_imc_test_state_t *this, bool has_long, bool has_excl)
116 {
117 this->has_long = has_long;
118 this->has_excl = has_excl;
119 }
120
121 METHOD(imc_state_t, set_max_msg_len, void,
122 private_imc_test_state_t *this, u_int32_t max_msg_len)
123 {
124 this->max_msg_len = max_msg_len;
125 }
126
127 METHOD(imc_state_t, get_max_msg_len, u_int32_t,
128 private_imc_test_state_t *this)
129 {
130 return this->max_msg_len;
131 }
132
133 METHOD(imc_state_t, change_state, void,
134 private_imc_test_state_t *this, TNC_ConnectionState new_state)
135 {
136 this->state = new_state;
137 }
138
139 METHOD(imc_state_t, set_result, void,
140 private_imc_test_state_t *this, TNC_IMCID id,
141 TNC_IMV_Evaluation_Result result)
142 {
143 enumerator_t *enumerator;
144 entry_t *entry;
145 bool found = FALSE;
146
147 enumerator = this->results->create_enumerator(this->results);
148 while (enumerator->enumerate(enumerator, &entry))
149 {
150 if (entry->id == id)
151 {
152 entry->result = result;
153 found = TRUE;
154 break;
155 }
156 }
157 enumerator->destroy(enumerator);
158
159 if (!found)
160 {
161 entry = malloc_thing(entry_t);
162 entry->id = id;
163 entry->result = result;
164 this->results->insert_last(this->results, entry);
165 }
166 }
167
168 METHOD(imc_state_t, get_result, bool,
169 private_imc_test_state_t *this, TNC_IMCID id,
170 TNC_IMV_Evaluation_Result *result)
171 {
172 enumerator_t *enumerator;
173 entry_t *entry;
174 TNC_IMV_Evaluation_Result eval = TNC_IMV_EVALUATION_RESULT_DONT_KNOW;
175
176 enumerator = this->results->create_enumerator(this->results);
177 while (enumerator->enumerate(enumerator, &entry))
178 {
179 if (entry->id == id)
180 {
181 eval = entry->result;
182 break;
183 }
184 }
185 enumerator->destroy(enumerator);
186
187 if (result)
188 {
189 *result = eval;
190 }
191 return eval != TNC_IMV_EVALUATION_RESULT_DONT_KNOW;
192 }
193
194 METHOD(imc_state_t, destroy, void,
195 private_imc_test_state_t *this)
196 {
197 this->results->destroy_function(this->results, free);
198 free(this->command);
199 free(this);
200 }
201
202 METHOD(imc_test_state_t, get_command, char*,
203 private_imc_test_state_t *this)
204 {
205 return this->command;
206 }
207
208 METHOD(imc_test_state_t, set_command, void,
209 private_imc_test_state_t *this, char* command)
210 {
211 char *old_command;
212
213 old_command = this->command;
214 this->command = strdup(command);
215 free(old_command);
216 }
217
218 METHOD(imc_test_state_t, get_dummy_size, int,
219 private_imc_test_state_t *this)
220 {
221 return this->dummy_size;
222 }
223
224
225 METHOD(imc_test_state_t, is_first_handshake, bool,
226 private_imc_test_state_t *this)
227 {
228 bool first;
229
230 /* test and reset first_handshake flag */
231 first= this->first_handshake;
232 this->first_handshake = FALSE;
233 return first;
234 }
235
236 METHOD(imc_test_state_t, do_handshake_retry, bool,
237 private_imc_test_state_t *this)
238 {
239 bool retry;
240
241 /* test and reset handshake_retry flag */
242 retry = this->handshake_retry;
243 this->handshake_retry = FALSE;
244 return retry;
245 }
246
247 /**
248 * Described in header.
249 */
250 imc_state_t *imc_test_state_create(TNC_ConnectionID connection_id,
251 char *command, int dummy_size, bool retry)
252 {
253 private_imc_test_state_t *this;
254
255 INIT(this,
256 .public = {
257 .interface = {
258 .get_connection_id = _get_connection_id,
259 .has_long = _has_long,
260 .has_excl = _has_excl,
261 .set_flags = _set_flags,
262 .set_max_msg_len = _set_max_msg_len,
263 .get_max_msg_len = _get_max_msg_len,
264 .change_state = _change_state,
265 .set_result = _set_result,
266 .get_result = _get_result,
267 .destroy = _destroy,
268 },
269 .get_command = _get_command,
270 .set_command = _set_command,
271 .get_dummy_size = _get_dummy_size,
272 .is_first_handshake = _is_first_handshake,
273 .do_handshake_retry = _do_handshake_retry,
274 },
275 .state = TNC_CONNECTION_STATE_CREATE,
276 .results = linked_list_create(),
277 .connection_id = connection_id,
278 .command = strdup(command),
279 .dummy_size = dummy_size,
280 .first_handshake = TRUE,
281 .handshake_retry = retry,
282 );
283
284 return &this->public.interface;
285 }
286