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