05a3bf65a14c0c0a83a9b2715d42b49600947340
[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
69 */
70 char *info;
71
72 /**
73 * OS Installed Package request sent - mandatory response expected
74 */
75 bool package_request;
76
77 /**
78 * Angel count
79 */
80 int angel_count;
81
82 };
83
84 typedef struct entry_t entry_t;
85
86 /**
87 * Define an internal reason string entry
88 */
89 struct entry_t {
90 char *lang;
91 char *string;
92 };
93
94 /**
95 * Table of multi-lingual reason string entries
96 */
97 static entry_t reasons[] = {
98 { "en", "" },
99 { "de", "" },
100 { "fr", "" },
101 { "pl", "" }
102 };
103
104 METHOD(imv_state_t, get_connection_id, TNC_ConnectionID,
105 private_imv_os_state_t *this)
106 {
107 return this->connection_id;
108 }
109
110 METHOD(imv_state_t, has_long, bool,
111 private_imv_os_state_t *this)
112 {
113 return this->has_long;
114 }
115
116 METHOD(imv_state_t, has_excl, bool,
117 private_imv_os_state_t *this)
118 {
119 return this->has_excl;
120 }
121
122 METHOD(imv_state_t, set_flags, void,
123 private_imv_os_state_t *this, bool has_long, bool has_excl)
124 {
125 this->has_long = has_long;
126 this->has_excl = has_excl;
127 }
128
129 METHOD(imv_state_t, set_max_msg_len, void,
130 private_imv_os_state_t *this, u_int32_t max_msg_len)
131 {
132 this->max_msg_len = max_msg_len;
133 }
134
135 METHOD(imv_state_t, get_max_msg_len, u_int32_t,
136 private_imv_os_state_t *this)
137 {
138 return this->max_msg_len;
139 }
140
141 METHOD(imv_state_t, change_state, void,
142 private_imv_os_state_t *this, TNC_ConnectionState new_state)
143 {
144 this->state = new_state;
145 }
146
147 METHOD(imv_state_t, get_recommendation, void,
148 private_imv_os_state_t *this, TNC_IMV_Action_Recommendation *rec,
149 TNC_IMV_Evaluation_Result *eval)
150 {
151 *rec = this->rec;
152 *eval = this->eval;
153 }
154
155 METHOD(imv_state_t, set_recommendation, void,
156 private_imv_os_state_t *this, TNC_IMV_Action_Recommendation rec,
157 TNC_IMV_Evaluation_Result eval)
158 {
159 this->rec = rec;
160 this->eval = eval;
161 }
162
163 METHOD(imv_state_t, get_reason_string, bool,
164 private_imv_os_state_t *this, chunk_t preferred_language,
165 chunk_t *reason_string, chunk_t *reason_language)
166 {
167 return FALSE;
168 }
169
170 METHOD(imv_state_t, destroy, void,
171 private_imv_os_state_t *this)
172 {
173 free(this->info);
174 free(this);
175 }
176
177 METHOD(imv_os_state_t, set_info, void,
178 private_imv_os_state_t *this, chunk_t name, chunk_t version)
179 {
180 int len = name.len + 1 + version.len + 1;
181
182 /* OS info is a concatenation of OS name and OS version */
183 free(this->info);
184 this->info = malloc(len);
185 snprintf(this->info, len, "%.*s %.*s", name.len, name.ptr,
186 version.len, version.ptr);
187 }
188
189 METHOD(imv_os_state_t, get_info, char*,
190 private_imv_os_state_t *this)
191 {
192 return this->info;
193 }
194
195 METHOD(imv_os_state_t, set_package_request, void,
196 private_imv_os_state_t *this, bool set)
197 {
198 this->package_request = set;
199 }
200
201 METHOD(imv_os_state_t, get_package_request, bool,
202 private_imv_os_state_t *this)
203 {
204 return this->package_request;
205 }
206
207 METHOD(imv_os_state_t, set_angel_count, void,
208 private_imv_os_state_t *this, bool start)
209 {
210 this->angel_count += start ? 1 : -1;
211 }
212
213 METHOD(imv_os_state_t, get_angel_count, int,
214 private_imv_os_state_t *this)
215 {
216 return this->angel_count;
217 }
218
219 /**
220 * Described in header.
221 */
222 imv_state_t *imv_os_state_create(TNC_ConnectionID connection_id)
223 {
224 private_imv_os_state_t *this;
225
226 INIT(this,
227 .public = {
228 .interface = {
229 .get_connection_id = _get_connection_id,
230 .has_long = _has_long,
231 .has_excl = _has_excl,
232 .set_flags = _set_flags,
233 .set_max_msg_len = _set_max_msg_len,
234 .get_max_msg_len = _get_max_msg_len,
235 .change_state = _change_state,
236 .get_recommendation = _get_recommendation,
237 .set_recommendation = _set_recommendation,
238 .get_reason_string = _get_reason_string,
239 .destroy = _destroy,
240 },
241 .set_info = _set_info,
242 .get_info = _get_info,
243 .set_package_request = _set_package_request,
244 .get_package_request = _get_package_request,
245 .set_angel_count = _set_angel_count,
246 .get_angel_count = _get_angel_count,
247 },
248 .state = TNC_CONNECTION_STATE_CREATE,
249 .rec = TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
250 .eval = TNC_IMV_EVALUATION_RESULT_DONT_KNOW,
251 .connection_id = connection_id,
252 );
253
254 return &this->public.interface;
255 }
256
257