implemented sending of reason strings
[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 <debug.h>
18
19 typedef struct private_imv_test_state_t private_imv_test_state_t;
20
21 /**
22 * Private data of an imv_test_state_t object.
23 */
24 struct private_imv_test_state_t {
25
26 /**
27 * Public members of imv_test_state_t
28 */
29 imv_test_state_t public;
30
31 /**
32 * TNCCS connection ID
33 */
34 TNC_ConnectionID connection_id;
35
36 /**
37 * TNCCS connection state
38 */
39 TNC_ConnectionState state;
40
41 /**
42 * IMV action recommendation
43 */
44 TNC_IMV_Action_Recommendation rec;
45
46 /**
47 * IMV evaluation result
48 */
49 TNC_IMV_Evaluation_Result eval;
50
51 /**
52 * IMC-IMV round-trip count
53 */
54 int rounds;
55
56 };
57
58 typedef struct entry_t entry_t;
59
60 /**
61 * Define an internal reason string entry
62 */
63 struct entry_t {
64 char *lang;
65 char *string;
66 };
67
68 /**
69 * Table of multi-lingual reason string entries
70 */
71 static entry_t reasons[] = {
72 { "en", "IMC Test was not configured with \"command = allow\"" },
73 { "de", "IMC Test wurde nicht mit \"command = allow\" konfiguriert" },
74 { "fr", "IMC Test n'etait pas configuré avec \"command = allow\"" },
75 { "pl", "IMC Test nie zostało skonfigurowany z \"command = allow\"" }
76 };
77
78 METHOD(imv_state_t, get_connection_id, TNC_ConnectionID,
79 private_imv_test_state_t *this)
80 {
81 return this->connection_id;
82 }
83
84 METHOD(imv_state_t, change_state, void,
85 private_imv_test_state_t *this, TNC_ConnectionState new_state)
86 {
87 this->state = new_state;
88 }
89
90 METHOD(imv_state_t, get_recommendation, void,
91 private_imv_test_state_t *this, TNC_IMV_Action_Recommendation *rec,
92 TNC_IMV_Evaluation_Result *eval)
93 {
94 *rec = this->rec;
95 *eval = this->eval;
96 }
97
98 METHOD(imv_state_t, set_recommendation, void,
99 private_imv_test_state_t *this, TNC_IMV_Action_Recommendation rec,
100 TNC_IMV_Evaluation_Result eval)
101 {
102 this->rec = rec;
103 this->eval = eval;
104 }
105
106 METHOD(imv_state_t, get_reason_string, bool,
107 private_imv_test_state_t *this, chunk_t preferred_language,
108 chunk_t *reason_string, chunk_t *reason_language)
109 {
110 int i;
111
112 for (i = 0 ; i < countof(reasons); i++)
113 {
114 chunk_t lang;
115
116 lang = chunk_create(reasons[i].lang, strlen(reasons[i].lang));
117 if (chunk_equals(lang, preferred_language))
118 {
119 *reason_language = lang;
120 *reason_string = chunk_create(reasons[i].string,
121 strlen(reasons[i].string));
122 return TRUE;
123 }
124 }
125
126 /* no preferred language match found - use the default language */
127 *reason_string = chunk_create(reasons[0].string,
128 strlen(reasons[0].string));
129 *reason_language = chunk_create(reasons[0].lang,
130 strlen(reasons[0].lang));
131 return TRUE;
132 }
133
134 METHOD(imv_state_t, destroy, void,
135 private_imv_test_state_t *this)
136 {
137 free(this);
138 }
139
140 METHOD(imv_test_state_t, set_rounds, void,
141 private_imv_test_state_t *this, int rounds)
142 {
143 this->rounds = rounds;
144 }
145
146 METHOD(imv_test_state_t, another_round, bool,
147 private_imv_test_state_t *this)
148 {
149 return (this->rounds-- > 0);
150 }
151
152 /**
153 * Described in header.
154 */
155 imv_state_t *imv_test_state_create(TNC_ConnectionID connection_id)
156 {
157 private_imv_test_state_t *this;
158
159 INIT(this,
160 .public = {
161 .interface = {
162 .get_connection_id = _get_connection_id,
163 .change_state = _change_state,
164 .get_recommendation = _get_recommendation,
165 .set_recommendation = _set_recommendation,
166 .get_reason_string = _get_reason_string,
167 .destroy = _destroy,
168 },
169 .set_rounds = _set_rounds,
170 .another_round = _another_round,
171 },
172 .state = TNC_CONNECTION_STATE_CREATE,
173 .rec = TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
174 .eval = TNC_IMV_EVALUATION_RESULT_DONT_KNOW,
175 .connection_id = connection_id,
176 );
177
178 return &this->public.interface;
179 }
180
181