implemented IETF Numeric Version attribute
[strongswan.git] / src / libimcv / ietf / ietf_attr_numeric_version.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 "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 <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 * Attribute value
60 */
61 chunk_t value;
62
63 /**
64 * Noskip flag
65 */
66 bool noskip_flag;
67
68 /**
69 * Major Version Number
70 */
71 u_int32_t major_version;
72
73 /**
74 * Minor Version Number
75 */
76 u_int32_t minor_version;
77
78 /**
79 * IBuild Number
80 */
81 u_int32_t build;
82
83 /**
84 * Service Pack Major Number
85 */
86 u_int16_t service_pack_major;
87
88 /**
89 * Service Pack Minor Number
90 */
91 u_int16_t service_pack_minor;
92
93 /**
94 * Reference count
95 */
96 refcount_t ref;
97 };
98
99 METHOD(pa_tnc_attr_t, get_type, pen_type_t,
100 private_ietf_attr_numeric_version_t *this)
101 {
102 return this->type;
103 }
104
105 METHOD(pa_tnc_attr_t, get_value, chunk_t,
106 private_ietf_attr_numeric_version_t *this)
107 {
108 return this->value;
109 }
110
111 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
112 private_ietf_attr_numeric_version_t *this)
113 {
114 return this->noskip_flag;
115 }
116
117 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
118 private_ietf_attr_numeric_version_t *this, bool noskip)
119 {
120 this->noskip_flag = noskip;
121 }
122
123 METHOD(pa_tnc_attr_t, build, void,
124 private_ietf_attr_numeric_version_t *this)
125 {
126 bio_writer_t *writer;
127
128 if (this->value.ptr)
129 {
130 return;
131 }
132
133 writer = bio_writer_create(NUMERIC_VERSION_SIZE);
134 writer->write_uint32(writer, this->major_version);
135 writer->write_uint32(writer, this->minor_version);
136 writer->write_uint32(writer, this->build);
137 writer->write_uint32(writer, this->service_pack_major);
138 writer->write_uint32(writer, this->service_pack_minor);
139
140 this->value = chunk_clone(writer->get_buf(writer));
141 writer->destroy(writer);
142 }
143
144 METHOD(pa_tnc_attr_t, process, status_t,
145 private_ietf_attr_numeric_version_t *this, u_int32_t *offset)
146 {
147 bio_reader_t *reader;
148
149 if (this->value.len < NUMERIC_VERSION_SIZE)
150 {
151 DBG1(DBG_TNC, "insufficient data for IETF numeric version");
152 *offset = 0;
153 return FAILED;
154 }
155 reader = bio_reader_create(this->value);
156 reader->read_uint32(reader, &this->major_version);
157 reader->read_uint32(reader, &this->minor_version);
158 reader->read_uint32(reader, &this->build);
159 reader->read_uint16(reader, &this->service_pack_major);
160 reader->read_uint16(reader, &this->service_pack_minor);
161 reader->destroy(reader);
162
163 return SUCCESS;
164 }
165
166 METHOD(pa_tnc_attr_t, get_ref, pa_tnc_attr_t*,
167 private_ietf_attr_numeric_version_t *this)
168 {
169 ref_get(&this->ref);
170 return &this->public.pa_tnc_attribute;
171 }
172
173 METHOD(pa_tnc_attr_t, destroy, void,
174 private_ietf_attr_numeric_version_t *this)
175 {
176 if (ref_put(&this->ref))
177 {
178 free(this->value.ptr);
179 free(this);
180 }
181 }
182
183 METHOD(ietf_attr_numeric_version_t, get_version, void,
184 private_ietf_attr_numeric_version_t *this, u_int32_t *major, u_int32_t *minor)
185 {
186 if (major)
187 {
188 *major = this->major_version;
189 }
190 if (minor)
191 {
192 *minor = this->minor_version;
193 }
194 }
195
196 METHOD(ietf_attr_numeric_version_t, get_build, u_int32_t,
197 private_ietf_attr_numeric_version_t *this)
198 {
199 return this->build;
200 }
201
202 METHOD(ietf_attr_numeric_version_t, get_service_pack, void,
203 private_ietf_attr_numeric_version_t *this, u_int16_t *major, u_int16_t *minor)
204 {
205 if (major)
206 {
207 *major = this->service_pack_major;
208 }
209 if (minor)
210 {
211 *minor = this->service_pack_minor;
212 }
213 }
214
215 /**
216 * Described in header.
217 */
218 pa_tnc_attr_t *ietf_attr_numeric_version_create(u_int32_t major, u_int32_t minor,
219 u_int32_t build,
220 u_int16_t service_pack_major,
221 u_int16_t service_pack_minor)
222 {
223 private_ietf_attr_numeric_version_t *this;
224
225 INIT(this,
226 .public = {
227 .pa_tnc_attribute = {
228 .get_type = _get_type,
229 .get_value = _get_value,
230 .get_noskip_flag = _get_noskip_flag,
231 .set_noskip_flag = _set_noskip_flag,
232 .build = _build,
233 .process = _process,
234 .get_ref = _get_ref,
235 .destroy = _destroy,
236 },
237 .get_version = _get_version,
238 .get_build = _get_build,
239 .get_service_pack = _get_service_pack,
240 },
241 .type = { PEN_IETF, IETF_ATTR_NUMERIC_VERSION },
242 .major_version = major,
243 .minor_version = minor,
244 .build = build,
245 .service_pack_major = service_pack_major,
246 .service_pack_minor = service_pack_minor,
247 .ref = 1,
248 );
249
250 return &this->public.pa_tnc_attribute;
251 }
252
253 /**
254 * Described in header.
255 */
256 pa_tnc_attr_t *ietf_attr_numeric_version_create_from_data(chunk_t data)
257 {
258 private_ietf_attr_numeric_version_t *this;
259
260 INIT(this,
261 .public = {
262 .pa_tnc_attribute = {
263 .get_type = _get_type,
264 .get_value = _get_value,
265 .build = _build,
266 .process = _process,
267 .get_ref = _get_ref,
268 .destroy = _destroy,
269 },
270 .get_version = _get_version,
271 .get_build = _get_build,
272 .get_service_pack = _get_service_pack,
273 },
274 .type = { PEN_IETF, IETF_ATTR_NUMERIC_VERSION },
275 .value = chunk_clone(data),
276 .ref = 1,
277 );
278
279 return &this->public.pa_tnc_attribute;
280 }
281