3e8788718006ee00fe397dcb2132bea8413baeae
[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, get_ref, pa_tnc_attr_t*,
178 private_ietf_attr_numeric_version_t *this)
179 {
180 ref_get(&this->ref);
181 return &this->public.pa_tnc_attribute;
182 }
183
184 METHOD(pa_tnc_attr_t, destroy, void,
185 private_ietf_attr_numeric_version_t *this)
186 {
187 if (ref_put(&this->ref))
188 {
189 free(this->value.ptr);
190 free(this);
191 }
192 }
193
194 METHOD(ietf_attr_numeric_version_t, get_version, void,
195 private_ietf_attr_numeric_version_t *this, u_int32_t *major, u_int32_t *minor)
196 {
197 if (major)
198 {
199 *major = this->major_version;
200 }
201 if (minor)
202 {
203 *minor = this->minor_version;
204 }
205 }
206
207 METHOD(ietf_attr_numeric_version_t, get_build, u_int32_t,
208 private_ietf_attr_numeric_version_t *this)
209 {
210 return this->build;
211 }
212
213 METHOD(ietf_attr_numeric_version_t, get_service_pack, void,
214 private_ietf_attr_numeric_version_t *this, u_int16_t *major, u_int16_t *minor)
215 {
216 if (major)
217 {
218 *major = this->service_pack_major;
219 }
220 if (minor)
221 {
222 *minor = this->service_pack_minor;
223 }
224 }
225
226 /**
227 * Described in header.
228 */
229 pa_tnc_attr_t *ietf_attr_numeric_version_create(u_int32_t major, u_int32_t minor,
230 u_int32_t build,
231 u_int16_t service_pack_major,
232 u_int16_t service_pack_minor)
233 {
234 private_ietf_attr_numeric_version_t *this;
235
236 INIT(this,
237 .public = {
238 .pa_tnc_attribute = {
239 .get_type = _get_type,
240 .get_value = _get_value,
241 .get_noskip_flag = _get_noskip_flag,
242 .set_noskip_flag = _set_noskip_flag,
243 .build = _build,
244 .process = _process,
245 .get_ref = _get_ref,
246 .destroy = _destroy,
247 },
248 .get_version = _get_version,
249 .get_build = _get_build,
250 .get_service_pack = _get_service_pack,
251 },
252 .type = { PEN_IETF, IETF_ATTR_NUMERIC_VERSION },
253 .major_version = major,
254 .minor_version = minor,
255 .build = build,
256 .service_pack_major = service_pack_major,
257 .service_pack_minor = service_pack_minor,
258 .ref = 1,
259 );
260
261 return &this->public.pa_tnc_attribute;
262 }
263
264 /**
265 * Described in header.
266 */
267 pa_tnc_attr_t *ietf_attr_numeric_version_create_from_data(size_t length,
268 chunk_t data)
269 {
270 private_ietf_attr_numeric_version_t *this;
271
272 INIT(this,
273 .public = {
274 .pa_tnc_attribute = {
275 .get_type = _get_type,
276 .get_value = _get_value,
277 .get_noskip_flag = _get_noskip_flag,
278 .set_noskip_flag = _set_noskip_flag,
279 .build = _build,
280 .process = _process,
281 .get_ref = _get_ref,
282 .destroy = _destroy,
283 },
284 .get_version = _get_version,
285 .get_build = _get_build,
286 .get_service_pack = _get_service_pack,
287 },
288 .type = { PEN_IETF, IETF_ATTR_NUMERIC_VERSION },
289 .length = length,
290 .value = chunk_clone(data),
291 .ref = 1,
292 );
293
294 return &this->public.pa_tnc_attribute;
295 }