accumulate package counts over multiple attributes
[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 * Number of processed packages
89 */
90 int count;
91
92 /**
93 * Number of blacklisted or not updated packages
94 */
95 int count_bad;
96
97 /**
98 * Number of whitelisted packages
99 */
100 int count_ok;
101
102 /**
103 * OS Installed Package request sent - mandatory response expected
104 */
105 bool package_request;
106
107 /**
108 * Angel count
109 */
110 int angel_count;
111
112 };
113
114 typedef struct entry_t entry_t;
115
116 /**
117 * Define an internal reason string entry
118 */
119 struct entry_t {
120 char *lang;
121 char *string;
122 };
123
124 /**
125 * Table of multi-lingual reason string entries
126 */
127 static entry_t reasons[] = {
128 { "en", "" },
129 { "de", "" },
130 { "fr", "" },
131 { "pl", "" }
132 };
133
134 METHOD(imv_state_t, get_connection_id, TNC_ConnectionID,
135 private_imv_os_state_t *this)
136 {
137 return this->connection_id;
138 }
139
140 METHOD(imv_state_t, has_long, bool,
141 private_imv_os_state_t *this)
142 {
143 return this->has_long;
144 }
145
146 METHOD(imv_state_t, has_excl, bool,
147 private_imv_os_state_t *this)
148 {
149 return this->has_excl;
150 }
151
152 METHOD(imv_state_t, set_flags, void,
153 private_imv_os_state_t *this, bool has_long, bool has_excl)
154 {
155 this->has_long = has_long;
156 this->has_excl = has_excl;
157 }
158
159 METHOD(imv_state_t, set_max_msg_len, void,
160 private_imv_os_state_t *this, u_int32_t max_msg_len)
161 {
162 this->max_msg_len = max_msg_len;
163 }
164
165 METHOD(imv_state_t, get_max_msg_len, u_int32_t,
166 private_imv_os_state_t *this)
167 {
168 return this->max_msg_len;
169 }
170
171 METHOD(imv_state_t, change_state, void,
172 private_imv_os_state_t *this, TNC_ConnectionState new_state)
173 {
174 this->state = new_state;
175 }
176
177 METHOD(imv_state_t, get_recommendation, void,
178 private_imv_os_state_t *this, TNC_IMV_Action_Recommendation *rec,
179 TNC_IMV_Evaluation_Result *eval)
180 {
181 *rec = this->rec;
182 *eval = this->eval;
183 }
184
185 METHOD(imv_state_t, set_recommendation, void,
186 private_imv_os_state_t *this, TNC_IMV_Action_Recommendation rec,
187 TNC_IMV_Evaluation_Result eval)
188 {
189 this->rec = rec;
190 this->eval = eval;
191 }
192
193 METHOD(imv_state_t, get_reason_string, bool,
194 private_imv_os_state_t *this, chunk_t preferred_language,
195 chunk_t *reason_string, chunk_t *reason_language)
196 {
197 return FALSE;
198 }
199
200 METHOD(imv_state_t, destroy, void,
201 private_imv_os_state_t *this)
202 {
203 free(this->info);
204 free(this->name.ptr);
205 free(this->version.ptr);
206 free(this);
207 }
208
209 METHOD(imv_os_state_t, set_info, void,
210 private_imv_os_state_t *this, os_type_t type, chunk_t name, chunk_t version)
211 {
212 int len = name.len + 1 + version.len + 1;
213
214 /* OS info is a concatenation of OS name and OS version */
215 free(this->info);
216 this->info = malloc(len);
217 snprintf(this->info, len, "%.*s %.*s", name.len, name.ptr,
218 version.len, version.ptr);
219 this->type = type;
220 this->name = chunk_clone(name);
221 this->version = chunk_clone(version);
222 }
223
224 METHOD(imv_os_state_t, get_info, char*,
225 private_imv_os_state_t *this, os_type_t *type, chunk_t *name,
226 chunk_t *version)
227 {
228 if (type)
229 {
230 *type = this->type;
231 }
232 if (name)
233 {
234 *name = this->name;
235 }
236 if (version)
237 {
238 *version = this->version;
239 }
240 return this->info;
241 }
242
243 METHOD(imv_os_state_t, set_count, void,
244 private_imv_os_state_t *this, int count, int count_bad, int count_ok)
245 {
246 this->count += count;
247 this->count_bad += count_bad;
248 this->count_ok += count_ok;
249 }
250
251 METHOD(imv_os_state_t, get_count, void,
252 private_imv_os_state_t *this, int *count, int *count_bad, int *count_ok)
253 {
254 if (count)
255 {
256 *count = this->count;
257 }
258 if (count_bad)
259 {
260 *count_bad = this->count_bad;
261 }
262 if (count_ok)
263 {
264 *count_ok = this->count_ok;
265 }
266 }
267
268 METHOD(imv_os_state_t, get_type, os_type_t,
269 private_imv_os_state_t *this)
270 {
271 return this->type;
272 }
273
274 METHOD(imv_os_state_t, set_package_request, void,
275 private_imv_os_state_t *this, bool set)
276 {
277 this->package_request = set;
278 }
279
280 METHOD(imv_os_state_t, get_package_request, bool,
281 private_imv_os_state_t *this)
282 {
283 return this->package_request;
284 }
285
286 METHOD(imv_os_state_t, set_angel_count, void,
287 private_imv_os_state_t *this, bool start)
288 {
289 this->angel_count += start ? 1 : -1;
290 }
291
292 METHOD(imv_os_state_t, get_angel_count, int,
293 private_imv_os_state_t *this)
294 {
295 return this->angel_count;
296 }
297
298 /**
299 * Described in header.
300 */
301 imv_state_t *imv_os_state_create(TNC_ConnectionID connection_id)
302 {
303 private_imv_os_state_t *this;
304
305 INIT(this,
306 .public = {
307 .interface = {
308 .get_connection_id = _get_connection_id,
309 .has_long = _has_long,
310 .has_excl = _has_excl,
311 .set_flags = _set_flags,
312 .set_max_msg_len = _set_max_msg_len,
313 .get_max_msg_len = _get_max_msg_len,
314 .change_state = _change_state,
315 .get_recommendation = _get_recommendation,
316 .set_recommendation = _set_recommendation,
317 .get_reason_string = _get_reason_string,
318 .destroy = _destroy,
319 },
320 .set_info = _set_info,
321 .get_info = _get_info,
322 .set_count = _set_count,
323 .get_count = _get_count,
324 .set_package_request = _set_package_request,
325 .get_package_request = _get_package_request,
326 .set_angel_count = _set_angel_count,
327 .get_angel_count = _get_angel_count,
328 },
329 .state = TNC_CONNECTION_STATE_CREATE,
330 .rec = TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
331 .eval = TNC_IMV_EVALUATION_RESULT_DONT_KNOW,
332 .connection_id = connection_id,
333 );
334
335 return &this->public.interface;
336 }
337
338