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