Added missing noskip_flag setter/getter to some pa_tnc_attr_t constructors
[strongswan.git] / src / libimcv / ietf / ietf_attr_op_status.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_op_status.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 #include <time.h>
24
25 typedef struct private_ietf_attr_op_status_t private_ietf_attr_op_status_t;
26
27 ENUM(op_status_names, OP_STATUS_UNKNOWN, OP_STATUS_OPERATIONAL,
28 "unknown",
29 "not installed",
30 "installed",
31 "operational"
32 );
33
34 ENUM(op_result_names, OP_RESULT_UNKNOWN, OP_RESULT_UNSUCCESSFUL,
35 "unknown",
36 "successful",
37 "errored",
38 "unsuccessful"
39 );
40
41 /**
42 * PA-TNC Operational Status type (see section 4.2.5 of RFC 5792)
43 *
44 * 1 2 3
45 * 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
46 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 * | Status | Result | Reserved |
48 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49 * | Last Use |
50 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 * | Last Use (continued) |
52 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53 * | Last Use (continued) |
54 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55 * | Last Use (continued) |
56 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57 * | Last Use (continued) |
58 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
59 */
60
61 #define OP_STATUS_SIZE 24
62
63 /**
64 * Private data of an ietf_attr_op_status_t object.
65 */
66 struct private_ietf_attr_op_status_t {
67
68 /**
69 * Public members of ietf_attr_op_status_t
70 */
71 ietf_attr_op_status_t public;
72
73 /**
74 * Vendor-specific attribute type
75 */
76 pen_type_t type;
77
78 /**
79 * Attribute value
80 */
81 chunk_t value;
82
83 /**
84 * Noskip flag
85 */
86 bool noskip_flag;
87
88 /**
89 * Status
90 */
91 u_int8_t status;
92
93 /**
94 * Result
95 */
96 u_int8_t result;
97
98 /**
99 * Last Use
100 */
101 time_t last_use;
102
103 /**
104 * Reference count
105 */
106 refcount_t ref;
107 };
108
109 METHOD(pa_tnc_attr_t, get_type, pen_type_t,
110 private_ietf_attr_op_status_t *this)
111 {
112 return this->type;
113 }
114
115 METHOD(pa_tnc_attr_t, get_value, chunk_t,
116 private_ietf_attr_op_status_t *this)
117 {
118 return this->value;
119 }
120
121 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
122 private_ietf_attr_op_status_t *this)
123 {
124 return this->noskip_flag;
125 }
126
127 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
128 private_ietf_attr_op_status_t *this, bool noskip)
129 {
130 this->noskip_flag = noskip;
131 }
132
133 METHOD(pa_tnc_attr_t, build, void,
134 private_ietf_attr_op_status_t *this)
135 {
136 bio_writer_t *writer;
137 char last_use[24];
138 struct tm t;
139
140 if (this->value.ptr)
141 {
142 return;
143 }
144
145 /* Conversion from time_t to RFC 3339 ASCII string */
146 gmtime_r(&this->last_use, &t);
147 snprintf(last_use, 21, "%04d-%02d-%02dT%02d:%02d:%02dZ", 1900 + t.tm_year,
148 t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
149
150 writer = bio_writer_create(OP_STATUS_SIZE);
151 writer->write_uint8 (writer, this->status);
152 writer->write_uint8 (writer, this->result);
153 writer->write_uint16(writer, 0x0000);
154 writer->write_data (writer, chunk_create(last_use, 20));
155
156 this->value = chunk_clone(writer->get_buf(writer));
157 writer->destroy(writer);
158 }
159
160 METHOD(pa_tnc_attr_t, process, status_t,
161 private_ietf_attr_op_status_t *this, u_int32_t *offset)
162 {
163 bio_reader_t *reader;
164 chunk_t last_use;
165 u_int16_t reserved;
166 struct tm t;
167
168 *offset = 0;
169
170 if (this->value.len != OP_STATUS_SIZE)
171 {
172 DBG1(DBG_TNC, "incorrect size for IETF operational status");
173 return FAILED;
174 }
175 reader = bio_reader_create(this->value);
176 reader->read_uint8 (reader, &this->status);
177 reader->read_uint8 (reader, &this->result);
178 reader->read_uint16(reader, &reserved);
179 reader->read_data (reader, 20, &last_use);
180 reader->destroy(reader);
181
182 if (this->status > OP_STATUS_ROOF)
183 {
184 DBG1(DBG_TNC, "invalid status value %c for IETF operational status",
185 this->status);
186 return FAILED;
187 }
188
189 *offset = 1;
190
191 if (this->result > OP_RESULT_ROOF)
192 {
193 DBG1(DBG_TNC, "invalid result value %c for IETF operational status",
194 this->result);
195 return FAILED;
196 }
197
198 *offset = 4;
199
200 /* Conversion from RFC 3339 ASCII string to time_t */
201 if (sscanf(last_use.ptr, "%4d-%2d-%2dT%2d:%2d:%2dZ", &t.tm_year, &t.tm_mon,
202 &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec) != 6)
203 {
204 DBG1(DBG_TNC, "invalid last_use time format in IETF operational status");
205 return FAILED;
206 }
207 t.tm_year -= 1900;
208 t.tm_mon -= 1;
209 t.tm_isdst = 0;
210 this->last_use = mktime(&t) - timezone;
211
212 return SUCCESS;
213 }
214
215 METHOD(pa_tnc_attr_t, get_ref, pa_tnc_attr_t*,
216 private_ietf_attr_op_status_t *this)
217 {
218 ref_get(&this->ref);
219 return &this->public.pa_tnc_attribute;
220 }
221
222 METHOD(pa_tnc_attr_t, destroy, void,
223 private_ietf_attr_op_status_t *this)
224 {
225 if (ref_put(&this->ref))
226 {
227 free(this->value.ptr);
228 free(this);
229 }
230 }
231
232 METHOD(ietf_attr_op_status_t, get_status, u_int8_t,
233 private_ietf_attr_op_status_t *this)
234 {
235 return this->status;
236 }
237
238 METHOD(ietf_attr_op_status_t, get_result, u_int8_t,
239 private_ietf_attr_op_status_t *this)
240 {
241 return this->result;
242 }
243
244 METHOD(ietf_attr_op_status_t, get_last_use, time_t,
245 private_ietf_attr_op_status_t *this)
246 {
247 return this->last_use;
248 }
249
250 /**
251 * Described in header.
252 */
253 pa_tnc_attr_t *ietf_attr_op_status_create(u_int8_t status, u_int8_t result,
254 time_t last_use)
255 {
256 private_ietf_attr_op_status_t *this;
257
258 INIT(this,
259 .public = {
260 .pa_tnc_attribute = {
261 .get_type = _get_type,
262 .get_value = _get_value,
263 .get_noskip_flag = _get_noskip_flag,
264 .set_noskip_flag = _set_noskip_flag,
265 .build = _build,
266 .process = _process,
267 .get_ref = _get_ref,
268 .destroy = _destroy,
269 },
270 .get_status = _get_status,
271 .get_result = _get_result,
272 .get_last_use = _get_last_use,
273 },
274 .type = { PEN_IETF, IETF_ATTR_OPERATIONAL_STATUS },
275 .status = status,
276 .result = result,
277 .last_use = last_use,
278 .ref = 1,
279 );
280
281 return &this->public.pa_tnc_attribute;
282 }
283
284 /**
285 * Described in header.
286 */
287 pa_tnc_attr_t *ietf_attr_op_status_create_from_data(chunk_t data)
288 {
289 private_ietf_attr_op_status_t *this;
290
291 INIT(this,
292 .public = {
293 .pa_tnc_attribute = {
294 .get_type = _get_type,
295 .get_value = _get_value,
296 .get_noskip_flag = _get_noskip_flag,
297 .set_noskip_flag = _set_noskip_flag,
298 .build = _build,
299 .process = _process,
300 .get_ref = _get_ref,
301 .destroy = _destroy,
302 },
303 .get_status = _get_status,
304 .get_result = _get_result,
305 .get_last_use = _get_last_use,
306 },
307 .type = { PEN_IETF, IETF_ATTR_OPERATIONAL_STATUS },
308 .value = chunk_clone(data),
309 .ref = 1,
310 );
311
312 return &this->public.pa_tnc_attribute;
313 }
314