implemented ITA Get Settings and ITA Settings attributes
[strongswan.git] / src / libimcv / ita / ita_attr_get_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_get_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_get_settings_t private_ita_attr_get_settings_t;
26
27 /**
28 * ITA Get Settings
29 *
30 * 1 2 3
31 * 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
32 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33 * | Settings Count |
34 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 * | Name Length | Name (Variable Length) ~
36 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 * ~ Name (Variable Length) ~
38 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 * | Name Length | Name (Variable Length) ~
40 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 * ~ Name (Variable Length) ~
42 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 * ...........................
44 */
45
46 #define ITA_GET_SETTINGS_MIN_SIZE 4
47
48 /**
49 * Private data of an ita_attr_get_settings_t object.
50 */
51 struct private_ita_attr_get_settings_t {
52
53 /**
54 * Public members of ita_attr_get_settings_t
55 */
56 ita_attr_get_settings_t public;
57
58 /**
59 * Vendor-specific attribute type
60 */
61 pen_type_t type;
62
63 /**
64 * Attribute value
65 */
66 chunk_t value;
67
68 /**
69 * Noskip flag
70 */
71 bool noskip_flag;
72
73 /**
74 * List of requested settings
75 */
76 linked_list_t *list;
77
78 /**
79 * Reference count
80 */
81 refcount_t ref;
82 };
83
84 METHOD(pa_tnc_attr_t, get_type, pen_type_t,
85 private_ita_attr_get_settings_t *this)
86 {
87 return this->type;
88 }
89
90 METHOD(pa_tnc_attr_t, get_value, chunk_t,
91 private_ita_attr_get_settings_t *this)
92 {
93 return this->value;
94 }
95
96 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
97 private_ita_attr_get_settings_t *this)
98 {
99 return this->noskip_flag;
100 }
101
102 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
103 private_ita_attr_get_settings_t *this, bool noskip)
104 {
105 this->noskip_flag = noskip;
106 }
107
108 METHOD(pa_tnc_attr_t, build, void,
109 private_ita_attr_get_settings_t *this)
110 {
111 bio_writer_t *writer;
112 enumerator_t *enumerator;
113 char *name;
114
115 if (this->value.ptr)
116 {
117 return;
118 }
119 writer = bio_writer_create(ITA_GET_SETTINGS_MIN_SIZE);
120 writer->write_uint32(writer, this->list->get_count(this->list));
121
122 enumerator = this->list->create_enumerator(this->list);
123 while (enumerator->enumerate(enumerator, &name))
124 {
125 writer->write_data16(writer, chunk_create(name, strlen(name)));
126 }
127 enumerator->destroy(enumerator);
128
129 this->value = chunk_clone(writer->get_buf(writer));
130 writer->destroy(writer);
131 }
132
133 METHOD(pa_tnc_attr_t, process, status_t,
134 private_ita_attr_get_settings_t *this, u_int32_t *offset)
135 {
136 bio_reader_t *reader;
137 u_int32_t count;
138 char *name_str;
139 chunk_t name;
140 status_t status = FAILED;
141
142 if (this->value.len < ITA_GET_SETTINGS_MIN_SIZE)
143 {
144 DBG1(DBG_TNC, "insufficient data for ITA Get Settings attribute");
145 *offset = 0;
146 return FAILED;
147 }
148
149 reader = bio_reader_create(this->value);
150 reader->read_uint32(reader, &count);
151
152 *offset = ITA_GET_SETTINGS_MIN_SIZE;
153
154 while (count--)
155 {
156 if (!reader->read_data16(reader, &name))
157 {
158 DBG1(DBG_TNC, "insufficient data for setting name");
159 goto end;
160 }
161 *offset += 2 + name.len;
162
163 name_str = malloc(name.len + 1);
164 memcpy(name_str, name.ptr, name.len);
165 name_str[name.len] = '\0';
166 this->list->insert_last(this->list, name_str);
167 }
168 status = SUCCESS;
169
170 end:
171 reader->destroy(reader);
172 return status;
173 }
174
175 METHOD(pa_tnc_attr_t, get_ref, pa_tnc_attr_t*,
176 private_ita_attr_get_settings_t *this)
177 {
178 ref_get(&this->ref);
179 return &this->public.pa_tnc_attribute;
180 }
181
182 METHOD(pa_tnc_attr_t, destroy, void,
183 private_ita_attr_get_settings_t *this)
184 {
185 if (ref_put(&this->ref))
186 {
187 this->list->destroy_function(this->list, free);
188 free(this->value.ptr);
189 free(this);
190 }
191 }
192
193 METHOD(ita_attr_get_settings_t, add, void,
194 private_ita_attr_get_settings_t *this, char *name)
195 {
196 this->list->insert_last(this->list, strdup(name));
197 }
198
199 METHOD(ita_attr_get_settings_t, create_enumerator, enumerator_t*,
200 private_ita_attr_get_settings_t *this)
201 {
202 return this->list->create_enumerator(this->list);
203 }
204
205 /**
206 * Described in header.
207 */
208 pa_tnc_attr_t *ita_attr_get_settings_create(void)
209 {
210 private_ita_attr_get_settings_t *this;
211
212 INIT(this,
213 .public = {
214 .pa_tnc_attribute = {
215 .get_type = _get_type,
216 .get_value = _get_value,
217 .get_noskip_flag = _get_noskip_flag,
218 .set_noskip_flag = _set_noskip_flag,
219 .build = _build,
220 .process = _process,
221 .get_ref = _get_ref,
222 .destroy = _destroy,
223 },
224 .add = _add,
225 .create_enumerator = _create_enumerator,
226 },
227 .type = { PEN_ITA, ITA_ATTR_GET_SETTINGS },
228 .list = linked_list_create(),
229 .ref = 1,
230 );
231
232 return &this->public.pa_tnc_attribute;
233 }
234
235 /**
236 * Described in header.
237 */
238 pa_tnc_attr_t *ita_attr_get_settings_create_from_data(chunk_t data)
239 {
240 private_ita_attr_get_settings_t *this;
241
242 INIT(this,
243 .public = {
244 .pa_tnc_attribute = {
245 .get_type = _get_type,
246 .get_value = _get_value,
247 .get_noskip_flag = _get_noskip_flag,
248 .set_noskip_flag = _set_noskip_flag,
249 .build = _build,
250 .process = _process,
251 .get_ref = _get_ref,
252 .destroy = _destroy,
253 },
254 .add = _add,
255 .create_enumerator = _create_enumerator,
256 },
257 .type = { PEN_ITA, ITA_ATTR_GET_SETTINGS },
258 .value = chunk_clone(data),
259 .list = linked_list_create(),
260 .ref = 1,
261 );
262
263 return &this->public.pa_tnc_attribute;
264 }
265
266