Added add_segment() method to IETF attributes
[strongswan.git] / src / libimcv / ietf / ietf_attr_numeric_version.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 #include "ietf_attr_numeric_version.h"
17
18 #include <pa_tnc/pa_tnc_msg.h>
19 #include <bio/bio_writer.h>
20 #include <bio/bio_reader.h>
21 #include <utils/debug.h>
22
23 typedef struct private_ietf_attr_numeric_version_t private_ietf_attr_numeric_version_t;
24
25 /**
26 * PA-TNC Numeric Version type (see section 4.2.3 of RFC 5792)
27 *
28 * 1 2 3
29 * 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
30 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
31 * | Major Version Number |
32 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33 * | Minor Version Number |
34 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 * | Build Number |
36 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 * | Service Pack Major | Service Pack Minor |
38 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 */
40
41 #define NUMERIC_VERSION_SIZE 16
42
43 /**
44 * Private data of an ietf_attr_numeric_version_t object.
45 */
46 struct private_ietf_attr_numeric_version_t {
47
48 /**
49 * Public members of ietf_attr_numeric_version_t
50 */
51 ietf_attr_numeric_version_t public;
52
53 /**
54 * Vendor-specific attribute type
55 */
56 pen_type_t type;
57
58 /**
59 * Length of attribute value
60 */
61 size_t length;
62
63 /**
64 * Attribute value or segment
65 */
66 chunk_t value;
67
68 /**
69 * Noskip flag
70 */
71 bool noskip_flag;
72
73 /**
74 * Major Version Number
75 */
76 u_int32_t major_version;
77
78 /**
79 * Minor Version Number
80 */
81 u_int32_t minor_version;
82
83 /**
84 * IBuild Number
85 */
86 u_int32_t build;
87
88 /**
89 * Service Pack Major Number
90 */
91 u_int16_t service_pack_major;
92
93 /**
94 * Service Pack Minor Number
95 */
96 u_int16_t service_pack_minor;
97
98 /**
99 * Reference count
100 */
101 refcount_t ref;
102 };
103
104 METHOD(pa_tnc_attr_t, get_type, pen_type_t,
105 private_ietf_attr_numeric_version_t *this)
106 {
107 return this->type;
108 }
109
110 METHOD(pa_tnc_attr_t, get_value, chunk_t,
111 private_ietf_attr_numeric_version_t *this)
112 {
113 return this->value;
114 }
115
116 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
117 private_ietf_attr_numeric_version_t *this)
118 {
119 return this->noskip_flag;
120 }
121
122 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
123 private_ietf_attr_numeric_version_t *this, bool noskip)
124 {
125 this->noskip_flag = noskip;
126 }
127
128 METHOD(pa_tnc_attr_t, build, void,
129 private_ietf_attr_numeric_version_t *this)
130 {
131 bio_writer_t *writer;
132
133 if (this->value.ptr)
134 {
135 return;
136 }
137
138 writer = bio_writer_create(NUMERIC_VERSION_SIZE);
139 writer->write_uint32(writer, this->major_version);
140 writer->write_uint32(writer, this->minor_version);
141 writer->write_uint32(writer, this->build);
142 writer->write_uint16(writer, this->service_pack_major);
143 writer->write_uint16(writer, this->service_pack_minor);
144
145 this->value = writer->extract_buf(writer);
146 this->length = this->value.len;
147 writer->destroy(writer);
148 }
149
150 METHOD(pa_tnc_attr_t, process, status_t,
151 private_ietf_attr_numeric_version_t *this, u_int32_t *offset)
152 {
153 bio_reader_t *reader;
154
155 *offset = 0;
156
157 if (this->value.len < this->length)
158 {
159 return NEED_MORE;
160 }
161 if (this->value.len < NUMERIC_VERSION_SIZE)
162 {
163 DBG1(DBG_TNC, "insufficient data for IETF numeric version");
164 return FAILED;
165 }
166 reader = bio_reader_create(this->value);
167 reader->read_uint32(reader, &this->major_version);
168 reader->read_uint32(reader, &this->minor_version);
169 reader->read_uint32(reader, &this->build);
170 reader->read_uint16(reader, &this->service_pack_major);
171 reader->read_uint16(reader, &this->service_pack_minor);
172 reader->destroy(reader);
173
174 return SUCCESS;
175 }
176
177 METHOD(pa_tnc_attr_t, add_segment, void,
178 private_ietf_attr_numeric_version_t *this, chunk_t segment)
179 {
180 this->value = chunk_cat("mc", this->value, segment);
181 }
182
183 METHOD(pa_tnc_attr_t, get_ref, pa_tnc_attr_t*,
184 private_ietf_attr_numeric_version_t *this)
185 {
186 ref_get(&this->ref);
187 return &this->public.pa_tnc_attribute;
188 }
189
190 METHOD(pa_tnc_attr_t, destroy, void,
191 private_ietf_attr_numeric_version_t *this)
192 {
193 if (ref_put(&this->ref))
194 {
195 free(this->value.ptr);
196 free(this);
197 }
198 }
199
200 METHOD(ietf_attr_numeric_version_t, get_version, void,
201 private_ietf_attr_numeric_version_t *this, u_int32_t *major, u_int32_t *minor)
202 {
203 if (major)
204 {
205 *major = this->major_version;
206 }
207 if (minor)
208 {
209 *minor = this->minor_version;
210 }
211 }
212
213 METHOD(ietf_attr_numeric_version_t, get_build, u_int32_t,
214 private_ietf_attr_numeric_version_t *this)
215 {
216 return this->build;
217 }
218
219 METHOD(ietf_attr_numeric_version_t, get_service_pack, void,
220 private_ietf_attr_numeric_version_t *this, u_int16_t *major, u_int16_t *minor)
221 {
222 if (major)
223 {
224 *major = this->service_pack_major;
225 }
226 if (minor)
227 {
228 *minor = this->service_pack_minor;
229 }
230 }
231
232 /**
233 * Described in header.
234 */
235 pa_tnc_attr_t *ietf_attr_numeric_version_create(u_int32_t major, u_int32_t minor,
236 u_int32_t build,
237 u_int16_t service_pack_major,
238 u_int16_t service_pack_minor)
239 {
240 private_ietf_attr_numeric_version_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 .add_segment = _add_segment,
252 .get_ref = _get_ref,
253 .destroy = _destroy,
254 },
255 .get_version = _get_version,
256 .get_build = _get_build,
257 .get_service_pack = _get_service_pack,
258 },
259 .type = { PEN_IETF, IETF_ATTR_NUMERIC_VERSION },
260 .major_version = major,
261 .minor_version = minor,
262 .build = build,
263 .service_pack_major = service_pack_major,
264 .service_pack_minor = service_pack_minor,
265 .ref = 1,
266 );
267
268 return &this->public.pa_tnc_attribute;
269 }
270
271 /**
272 * Described in header.
273 */
274 pa_tnc_attr_t *ietf_attr_numeric_version_create_from_data(size_t length,
275 chunk_t data)
276 {
277 private_ietf_attr_numeric_version_t *this;
278
279 INIT(this,
280 .public = {
281 .pa_tnc_attribute = {
282 .get_type = _get_type,
283 .get_value = _get_value,
284 .get_noskip_flag = _get_noskip_flag,
285 .set_noskip_flag = _set_noskip_flag,
286 .build = _build,
287 .process = _process,
288 .add_segment = _add_segment,
289 .get_ref = _get_ref,
290 .destroy = _destroy,
291 },
292 .get_version = _get_version,
293 .get_build = _get_build,
294 .get_service_pack = _get_service_pack,
295 },
296 .type = { PEN_IETF, IETF_ATTR_NUMERIC_VERSION },
297 .length = length,
298 .value = chunk_clone(data),
299 .ref = 1,
300 );
301
302 return &this->public.pa_tnc_attribute;
303 }