49d2e56533ebc69d5561ce9b46d2c5b38812667f
[strongswan.git] / src / libimcv / plugins / imv_os / imv_os_state.c
1 /*
2 * Copyright (C) 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 "imv_os_state.h"
17
18 #include <utils/debug.h>
19
20 typedef struct private_imv_os_state_t private_imv_os_state_t;
21
22 /**
23 * Private data of an imv_os_state_t object.
24 */
25 struct private_imv_os_state_t {
26
27 /**
28 * Public members of imv_os_state_t
29 */
30 imv_os_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 * Maximum PA-TNC message size for this TNCCS connection
54 */
55 u_int32_t max_msg_len;
56
57 /**
58 * IMV action recommendation
59 */
60 TNC_IMV_Action_Recommendation rec;
61
62 /**
63 * IMV evaluation result
64 */
65 TNC_IMV_Evaluation_Result eval;
66
67 /**
68 * OS Product Information (concatenation of OS Name and Version)
69 */
70 char *info;
71
72 /**
73 * OS Type
74 */
75 os_type_t type;
76
77 /**
78 * OS Name
79 */
80 chunk_t name;
81
82 /**
83 * OS Version
84 */
85 chunk_t version;
86
87 /**
88 * OS Installed Package request sent - mandatory response expected
89 */
90 bool package_request;
91
92 /**
93 * Angel count
94 */
95 int angel_count;
96
97 };
98
99 typedef struct entry_t entry_t;
100
101 /**
102 * Define an internal reason string entry
103 */
104 struct entry_t {
105 char *lang;
106 char *string;
107 };
108
109 /**
110 * Table of multi-lingual reason string entries
111 */
112 static entry_t reasons[] = {
113 { "en", "" },
114 { "de", "" },
115 { "fr", "" },
116 { "pl", "" }
117 };
118
119 METHOD(imv_state_t, get_connection_id, TNC_ConnectionID,
120 private_imv_os_state_t *this)
121 {
122 return this->connection_id;
123 }
124
125 METHOD(imv_state_t, has_long, bool,
126 private_imv_os_state_t *this)
127 {
128 return this->has_long;
129 }
130
131 METHOD(imv_state_t, has_excl, bool,
132 private_imv_os_state_t *this)
133 {
134 return this->has_excl;
135 }
136
137 METHOD(imv_state_t, set_flags, void,
138 private_imv_os_state_t *this, bool has_long, bool has_excl)
139 {
140 this->has_long = has_long;
141 this->has_excl = has_excl;
142 }
143
144 METHOD(imv_state_t, set_max_msg_len, void,
145 private_imv_os_state_t *this, u_int32_t max_msg_len)
146 {
147 this->max_msg_len = max_msg_len;
148 }
149
150 METHOD(imv_state_t, get_max_msg_len, u_int32_t,
151 private_imv_os_state_t *this)
152 {
153 return this->max_msg_len;
154 }
155
156 METHOD(imv_state_t, change_state, void,
157 private_imv_os_state_t *this, TNC_ConnectionState new_state)
158 {
159 this->state = new_state;
160 }
161
162 METHOD(imv_state_t, get_recommendation, void,
163 private_imv_os_state_t *this, TNC_IMV_Action_Recommendation *rec,
164 TNC_IMV_Evaluation_Result *eval)
165 {
166 *rec = this->rec;
167 *eval = this->eval;
168 }
169
170 METHOD(imv_state_t, set_recommendation, void,
171 private_imv_os_state_t *this, TNC_IMV_Action_Recommendation rec,
172 TNC_IMV_Evaluation_Result eval)
173 {
174 this->rec = rec;
175 this->eval = eval;
176 }
177
178 METHOD(imv_state_t, get_reason_string, bool,
179 private_imv_os_state_t *this, chunk_t preferred_language,
180 chunk_t *reason_string, chunk_t *reason_language)
181 {
182 return FALSE;
183 }
184
185 METHOD(imv_state_t, destroy, void,
186 private_imv_os_state_t *this)
187 {
188 free(this->info);
189 free(this->name.ptr);
190 free(this->version.ptr);
191 free(this);
192 }
193
194 METHOD(imv_os_state_t, set_info, void,
195 private_imv_os_state_t *this, os_type_t type, chunk_t name, chunk_t version)
196 {
197 int len = name.len + 1 + version.len + 1;
198
199 /* OS info is a concatenation of OS name and OS version */
200 free(this->info);
201 this->info = malloc(len);
202 snprintf(this->info, len, "%.*s %.*s", name.len, name.ptr,
203 version.len, version.ptr);
204 this->type = type;
205 this->name = chunk_clone(name);
206 this->version = chunk_clone(version);
207 }
208
209 METHOD(imv_os_state_t, get_info, char*,
210 private_imv_os_state_t *this, os_type_t *type, chunk_t *name,
211 chunk_t *version)
212 {
213 if (type)
214 {
215 *type = this->type;
216 }
217 if (name)
218 {
219 *name = this->name;
220 }
221 if (version)
222 {
223 *version = this->version;
224 }
225 return this->info;
226 }
227
228 METHOD(imv_os_state_t, get_type, os_type_t,
229 private_imv_os_state_t *this)
230 {
231 return this->type;
232 }
233
234 METHOD(imv_os_state_t, set_package_request, void,
235 private_imv_os_state_t *this, bool set)
236 {
237 this->package_request = set;
238 }
239
240 METHOD(imv_os_state_t, get_package_request, bool,
241 private_imv_os_state_t *this)
242 {
243 return this->package_request;
244 }
245
246 METHOD(imv_os_state_t, set_angel_count, void,
247 private_imv_os_state_t *this, bool start)
248 {
249 this->angel_count += start ? 1 : -1;
250 }
251
252 METHOD(imv_os_state_t, get_angel_count, int,
253 private_imv_os_state_t *this)
254 {
255 return this->angel_count;
256 }
257
258 /**
259 * Described in header.
260 */
261 imv_state_t *imv_os_state_create(TNC_ConnectionID connection_id)
262 {
263 private_imv_os_state_t *this;
264
265 INIT(this,
266 .public = {
267 .interface = {
268 .get_connection_id = _get_connection_id,
269 .has_long = _has_long,
270 .has_excl = _has_excl,
271 .set_flags = _set_flags,
272 .set_max_msg_len = _set_max_msg_len,
273 .get_max_msg_len = _get_max_msg_len,
274 .change_state = _change_state,
275 .get_recommendation = _get_recommendation,
276 .set_recommendation = _set_recommendation,
277 .get_reason_string = _get_reason_string,
278 .destroy = _destroy,
279 },
280 .set_info = _set_info,
281 .get_info = _get_info,
282 .set_package_request = _set_package_request,
283 .get_package_request = _get_package_request,
284 .set_angel_count = _set_angel_count,
285 .get_angel_count = _get_angel_count,
286 },
287 .state = TNC_CONNECTION_STATE_CREATE,
288 .rec = TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
289 .eval = TNC_IMV_EVALUATION_RESULT_DONT_KNOW,
290 .connection_id = connection_id,
291 );
292
293 return &this->public.interface;
294 }
295
296