implemented ITA Get Settings and ITA Settings attributes
[strongswan.git] / src / libimcv / ita / ita_attr_settings.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 "ita_attr.h"
17 #include "ita_attr_settings.h"
18
19 #include <bio/bio_reader.h>
20 #include <bio/bio_writer.h>
21 #include <collections/linked_list.h>
22 #include <pen/pen.h>
23 #include <utils/debug.h>
24
25 typedef struct private_ita_attr_settings_t private_ita_attr_settings_t;
26 typedef struct entry_t entry_t;
27
28 /**
29 * Contains a settins name/value pair
30 */
31 struct entry_t {
32 char *name;
33 chunk_t value;
34 };
35
36 /**
37 * Free an entry_t object
38 */
39 static void free_entry(entry_t *this)
40 {
41 free(this->name);
42 free(this->value.ptr);
43 free(this);
44 }
45
46 /**
47 * ITA Settings
48 *
49 * 1 2 3
50 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
51 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52 * | Settings Count |
53 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54 * | Name Length | Name (Variable Length) ~
55 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
56 * ~ Name (Variable Length) ~
57 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58 * | Value Length | Value (Variable Length) ~
59 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60 * ~ Value (Variable Length) ~
61 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
62 * | Name Length | Name (Variable Length) ~
63 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
64 * ~ Name (Variable Length) ~
65 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
66 * | Value Length | Value (Variable Length) ~
67 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
68 * ~ Value (Variable Length) ~
69 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
70 * ...........................
71 */
72
73 #define ITA_SETTINGS_MIN_SIZE 4
74
75 /**
76 * Private data of an ita_attr_settings_t object.
77 */
78 struct private_ita_attr_settings_t {
79
80 /**
81 * Public members of ita_attr_settings_t
82 */
83 ita_attr_settings_t public;
84
85 /**
86 * Vendor-specific attribute type
87 */
88 pen_type_t type;
89
90 /**
91 * Attribute value
92 */
93 chunk_t value;
94
95 /**
96 * Noskip flag
97 */
98 bool noskip_flag;
99
100 /**
101 * List of settings
102 */
103 linked_list_t *list;
104
105 /**
106 * Reference count
107 */
108 refcount_t ref;
109 };
110
111 METHOD(pa_tnc_attr_t, get_type, pen_type_t,
112 private_ita_attr_settings_t *this)
113 {
114 return this->type;
115 }
116
117 METHOD(pa_tnc_attr_t, get_value, chunk_t,
118 private_ita_attr_settings_t *this)
119 {
120 return this->value;
121 }
122
123 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
124 private_ita_attr_settings_t *this)
125 {
126 return this->noskip_flag;
127 }
128
129 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
130 private_ita_attr_settings_t *this, bool noskip)
131 {
132 this->noskip_flag = noskip;
133 }
134
135 METHOD(pa_tnc_attr_t, build, void,
136 private_ita_attr_settings_t *this)
137 {
138 bio_writer_t *writer;
139 enumerator_t *enumerator;
140 entry_t *entry;
141
142 if (this->value.ptr)
143 {
144 return;
145 }
146 writer = bio_writer_create(ITA_SETTINGS_MIN_SIZE);
147 writer->write_uint32(writer, this->list->get_count(this->list));
148
149 enumerator = this->list->create_enumerator(this->list);
150 while (enumerator->enumerate(enumerator, &entry))
151 {
152 writer->write_data16(writer, chunk_create(entry->name,
153 strlen(entry->name)));
154 writer->write_data16(writer, entry->value);
155 }
156 enumerator->destroy(enumerator);
157
158 this->value = chunk_clone(writer->get_buf(writer));
159 writer->destroy(writer);
160 }
161
162 METHOD(pa_tnc_attr_t, process, status_t,
163 private_ita_attr_settings_t *this, u_int32_t *offset)
164 {
165 bio_reader_t *reader;
166 u_int32_t count;
167 chunk_t name, value;
168 entry_t *entry;
169 status_t status = FAILED;
170
171 if (this->value.len < ITA_SETTINGS_MIN_SIZE)
172 {
173 DBG1(DBG_TNC, "insufficient data for ITA Settings attribute");
174 *offset = 0;
175 return FAILED;
176 }
177
178 reader = bio_reader_create(this->value);
179 reader->read_uint32(reader, &count);
180
181 *offset = ITA_SETTINGS_MIN_SIZE;
182
183 while (count--)
184 {
185 if (!reader->read_data16(reader, &name))
186 {
187 DBG1(DBG_TNC, "insufficient data for setting name");
188 goto end;
189 }
190 *offset += 2 + name.len;
191
192 if (!reader->read_data16(reader, &value))
193 {
194 DBG1(DBG_TNC, "insufficient data for setting value");
195 goto end;
196 }
197 *offset += 2 + value.len;
198
199 entry = malloc_thing(entry_t);
200 entry->name = malloc(name.len + 1);
201 memcpy(entry->name, name.ptr, name.len);
202 entry->name[name.len] = '\0';
203 entry->value = chunk_clone(value);
204 this->list->insert_last(this->list, entry);
205 }
206 status = SUCCESS;
207
208 end:
209 reader->destroy(reader);
210 return status;
211 }
212
213 METHOD(pa_tnc_attr_t, get_ref, pa_tnc_attr_t*,
214 private_ita_attr_settings_t *this)
215 {
216 ref_get(&this->ref);
217 return &this->public.pa_tnc_attribute;
218 }
219
220 METHOD(pa_tnc_attr_t, destroy, void,
221 private_ita_attr_settings_t *this)
222 {
223 if (ref_put(&this->ref))
224 {
225 this->list->destroy_function(this->list, (void*)free_entry);
226 free(this->value.ptr);
227 free(this);
228 }
229 }
230
231 METHOD(ita_attr_settings_t, add, void,
232 private_ita_attr_settings_t *this, char *name, chunk_t value)
233 {
234 entry_t *entry;
235
236 entry = malloc_thing(entry_t);
237 entry->name = strdup(name);
238 entry->value = chunk_clone(value);
239 this->list->insert_last(this->list, entry);
240 }
241
242 /**
243 * Enumerate name/value pairs
244 */
245 static bool entry_filter(void *null, entry_t **entry, char **name,
246 void *i2, chunk_t *value)
247 {
248 *name = (*entry)->name;
249 *value = (*entry)->value;
250 return TRUE;
251 }
252
253 METHOD(ita_attr_settings_t, create_enumerator, enumerator_t*,
254 private_ita_attr_settings_t *this)
255 {
256 return enumerator_create_filter(this->list->create_enumerator(this->list),
257 (void*)entry_filter, NULL, NULL);
258 }
259
260 /**
261 * Described in header.
262 */
263 pa_tnc_attr_t *ita_attr_settings_create(void)
264 {
265 private_ita_attr_settings_t *this;
266
267 INIT(this,
268 .public = {
269 .pa_tnc_attribute = {
270 .get_type = _get_type,
271 .get_value = _get_value,
272 .get_noskip_flag = _get_noskip_flag,
273 .set_noskip_flag = _set_noskip_flag,
274 .build = _build,
275 .process = _process,
276 .get_ref = _get_ref,
277 .destroy = _destroy,
278 },
279 .add = _add,
280 .create_enumerator = _create_enumerator,
281 },
282 .type = { PEN_ITA, ITA_ATTR_SETTINGS },
283 .list = linked_list_create(),
284 .ref = 1,
285 );
286
287 return &this->public.pa_tnc_attribute;
288 }
289
290 /**
291 * Described in header.
292 */
293 pa_tnc_attr_t *ita_attr_settings_create_from_data(chunk_t data)
294 {
295 private_ita_attr_settings_t *this;
296
297 INIT(this,
298 .public = {
299 .pa_tnc_attribute = {
300 .get_type = _get_type,
301 .get_value = _get_value,
302 .get_noskip_flag = _get_noskip_flag,
303 .set_noskip_flag = _set_noskip_flag,
304 .build = _build,
305 .process = _process,
306 .get_ref = _get_ref,
307 .destroy = _destroy,
308 },
309 .add = _add,
310 .create_enumerator = _create_enumerator,
311 },
312 .type = { PEN_ITA, ITA_ATTR_SETTINGS },
313 .value = chunk_clone(data),
314 .list = linked_list_create(),
315 .ref = 1,
316 );
317
318 return &this->public.pa_tnc_attribute;
319 }
320
321