store the long and excl flags in the connection state
[strongswan.git] / src / libimcv / plugins / imv_test / imv_test_state.c
1 /*
2 * Copyright (C) 2011 Andreas Steffen, HSR Hochschule fuer Technik Rapperswil
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 */
14
15 #include "imv_test_state.h"
16
17 #include <utils/lexparser.h>
18 #include <debug.h>
19
20 typedef struct private_imv_test_state_t private_imv_test_state_t;
21
22 /**
23 * Private data of an imv_test_state_t object.
24 */
25 struct private_imv_test_state_t {
26
27 /**
28 * Public members of imv_test_state_t
29 */
30 imv_test_state_t public;
31
32 /**
33 * TNCCS connection ID
34 */
35 TNC_ConnectionID connection_id;
36
37 /**
38 * TNCCS connection state
39 */
40 TNC_ConnectionState state;
41
42 /**
43 * Does the TNCCS connection support long message types?
44 */
45 bool has_long;
46
47 /**
48 * Does the TNCCS connection support exclusive delivery?
49 */
50 bool has_excl;
51
52 /**
53 * IMV action recommendation
54 */
55 TNC_IMV_Action_Recommendation rec;
56
57 /**
58 * IMV evaluation result
59 */
60 TNC_IMV_Evaluation_Result eval;
61
62 /**
63 * IMC-IMV round-trip count
64 */
65 int rounds;
66
67 };
68
69 typedef struct entry_t entry_t;
70
71 /**
72 * Define an internal reason string entry
73 */
74 struct entry_t {
75 char *lang;
76 char *string;
77 };
78
79 /**
80 * Table of multi-lingual reason string entries
81 */
82 static entry_t reasons[] = {
83 { "en", "IMC Test was not configured with \"command = allow\"" },
84 { "de", "IMC Test wurde nicht mit \"command = allow\" konfiguriert" },
85 { "fr", "IMC Test n'etait pas configuré avec \"command = allow\"" },
86 { "pl", "IMC Test nie zostało skonfigurowany z \"command = allow\"" }
87 };
88
89 METHOD(imv_state_t, get_connection_id, TNC_ConnectionID,
90 private_imv_test_state_t *this)
91 {
92 return this->connection_id;
93 }
94
95 METHOD(imv_state_t, has_long, bool,
96 private_imv_test_state_t *this)
97 {
98 return this->has_long;
99 }
100
101 METHOD(imv_state_t, has_excl, bool,
102 private_imv_test_state_t *this)
103 {
104 return this->has_excl;
105 }
106
107 METHOD(imv_state_t, set_flags, void,
108 private_imv_test_state_t *this, bool has_long, bool has_excl)
109 {
110 this->has_long = has_long;
111 this->has_excl = has_excl;
112 }
113
114 METHOD(imv_state_t, change_state, void,
115 private_imv_test_state_t *this, TNC_ConnectionState new_state)
116 {
117 this->state = new_state;
118 }
119
120 METHOD(imv_state_t, get_recommendation, void,
121 private_imv_test_state_t *this, TNC_IMV_Action_Recommendation *rec,
122 TNC_IMV_Evaluation_Result *eval)
123 {
124 *rec = this->rec;
125 *eval = this->eval;
126 }
127
128 METHOD(imv_state_t, set_recommendation, void,
129 private_imv_test_state_t *this, TNC_IMV_Action_Recommendation rec,
130 TNC_IMV_Evaluation_Result eval)
131 {
132 this->rec = rec;
133 this->eval = eval;
134 }
135
136 METHOD(imv_state_t, get_reason_string, bool,
137 private_imv_test_state_t *this, chunk_t preferred_language,
138 chunk_t *reason_string, chunk_t *reason_language)
139 {
140 chunk_t pref_lang, lang;
141 u_char *pos;
142 int i;
143
144 while (eat_whitespace(&preferred_language))
145 {
146 if (!extract_token(&pref_lang, ',', &preferred_language))
147 {
148 /* last entry in a comma-separated list or single entry */
149 pref_lang = preferred_language;
150 }
151
152 /* eat trailing whitespace */
153 pos = pref_lang.ptr + pref_lang.len - 1;
154 while (pref_lang.len && *pos-- == ' ')
155 {
156 pref_lang.len--;
157 }
158
159 for (i = 0 ; i < countof(reasons); i++)
160 {
161 lang = chunk_create(reasons[i].lang, strlen(reasons[i].lang));
162 if (chunk_equals(lang, pref_lang))
163 {
164 *reason_language = lang;
165 *reason_string = chunk_create(reasons[i].string,
166 strlen(reasons[i].string));
167 return TRUE;
168 }
169 }
170 }
171
172 /* no preferred language match found - use the default language */
173 *reason_string = chunk_create(reasons[0].string,
174 strlen(reasons[0].string));
175 *reason_language = chunk_create(reasons[0].lang,
176 strlen(reasons[0].lang));
177 return TRUE;
178 }
179
180 METHOD(imv_state_t, destroy, void,
181 private_imv_test_state_t *this)
182 {
183 free(this);
184 }
185
186 METHOD(imv_test_state_t, set_rounds, void,
187 private_imv_test_state_t *this, int rounds)
188 {
189 this->rounds = rounds;
190 }
191
192 METHOD(imv_test_state_t, another_round, bool,
193 private_imv_test_state_t *this)
194 {
195 return (this->rounds-- > 0);
196 }
197
198 /**
199 * Described in header.
200 */
201 imv_state_t *imv_test_state_create(TNC_ConnectionID connection_id)
202 {
203 private_imv_test_state_t *this;
204
205 INIT(this,
206 .public = {
207 .interface = {
208 .get_connection_id = _get_connection_id,
209 .has_long = _has_long,
210 .has_excl = _has_excl,
211 .set_flags = _set_flags,
212 .change_state = _change_state,
213 .get_recommendation = _get_recommendation,
214 .set_recommendation = _set_recommendation,
215 .get_reason_string = _get_reason_string,
216 .destroy = _destroy,
217 },
218 .set_rounds = _set_rounds,
219 .another_round = _another_round,
220 },
221 .state = TNC_CONNECTION_STATE_CREATE,
222 .rec = TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
223 .eval = TNC_IMV_EVALUATION_RESULT_DONT_KNOW,
224 .connection_id = connection_id,
225 );
226
227 return &this->public.interface;
228 }
229
230