2 * Copyright (C) 2012 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
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>.
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
16 #include "ietf_attr_op_status.h"
18 #include <pa_tnc/pa_tnc_msg.h>
19 #include <bio/bio_writer.h>
20 #include <bio/bio_reader.h>
25 typedef struct private_ietf_attr_op_status_t private_ietf_attr_op_status_t
;
27 ENUM(op_status_names
, OP_STATUS_UNKNOWN
, OP_STATUS_OPERATIONAL
,
34 ENUM(op_result_names
, OP_RESULT_UNKNOWN
, OP_RESULT_UNSUCCESSFUL
,
42 * PA-TNC Operational Status type (see section 4.2.5 of RFC 5792)
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 * | Last Use (continued) |
52 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53 * | Last Use (continued) |
54 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55 * | Last Use (continued) |
56 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57 * | Last Use (continued) |
58 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61 #define OP_STATUS_SIZE 24
64 * Private data of an ietf_attr_op_status_t object.
66 struct private_ietf_attr_op_status_t
{
69 * Public members of ietf_attr_op_status_t
71 ietf_attr_op_status_t
public;
74 * Vendor-specific attribute type
109 METHOD(pa_tnc_attr_t
, get_type
, pen_type_t
,
110 private_ietf_attr_op_status_t
*this)
115 METHOD(pa_tnc_attr_t
, get_value
, chunk_t
,
116 private_ietf_attr_op_status_t
*this)
121 METHOD(pa_tnc_attr_t
, get_noskip_flag
, bool,
122 private_ietf_attr_op_status_t
*this)
124 return this->noskip_flag
;
127 METHOD(pa_tnc_attr_t
, set_noskip_flag
,void,
128 private_ietf_attr_op_status_t
*this, bool noskip
)
130 this->noskip_flag
= noskip
;
133 METHOD(pa_tnc_attr_t
, build
, void,
134 private_ietf_attr_op_status_t
*this)
136 bio_writer_t
*writer
;
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
);
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));
156 this->value
= chunk_clone(writer
->get_buf(writer
));
157 writer
->destroy(writer
);
160 METHOD(pa_tnc_attr_t
, process
, status_t
,
161 private_ietf_attr_op_status_t
*this, u_int32_t
*offset
)
163 bio_reader_t
*reader
;
171 if (this->value
.len
!= OP_STATUS_SIZE
)
173 DBG1(DBG_TNC
, "incorrect size for IETF operational status");
176 reader
= bio_reader_create(this->value
);
177 reader
->read_uint8 (reader
, &this->status
);
178 reader
->read_uint8 (reader
, &this->result
);
179 reader
->read_uint16(reader
, &reserved
);
180 reader
->read_data (reader
, 20, &last_use
);
181 reader
->destroy(reader
);
183 if (this->status
> OP_STATUS_ROOF
)
185 DBG1(DBG_TNC
, "invalid status value %c for IETF operational status",
192 if (this->result
> OP_RESULT_ROOF
)
194 DBG1(DBG_TNC
, "invalid result value %c for IETF operational status",
201 /* Conversion from RFC 3339 ASCII string to time_t */
202 if (sscanf(last_use
.ptr
, "%4d-%2d-%2dT%2d:%2d:%2dZ", &t
.tm_year
, &t
.tm_mon
,
203 &t
.tm_mday
, &t
.tm_hour
, &t
.tm_min
, &t
.tm_sec
) != 6)
205 DBG1(DBG_TNC
, "invalid last_use time format in IETF operational status");
211 this->last_use
= mktime(&t
) - timezone
;
216 METHOD(pa_tnc_attr_t
, get_ref
, pa_tnc_attr_t
*,
217 private_ietf_attr_op_status_t
*this)
220 return &this->public.pa_tnc_attribute
;
223 METHOD(pa_tnc_attr_t
, destroy
, void,
224 private_ietf_attr_op_status_t
*this)
226 if (ref_put(&this->ref
))
228 free(this->value
.ptr
);
233 METHOD(ietf_attr_op_status_t
, get_status
, u_int8_t
,
234 private_ietf_attr_op_status_t
*this)
239 METHOD(ietf_attr_op_status_t
, get_result
, u_int8_t
,
240 private_ietf_attr_op_status_t
*this)
245 METHOD(ietf_attr_op_status_t
, get_last_use
, time_t,
246 private_ietf_attr_op_status_t
*this)
248 return this->last_use
;
252 * Described in header.
254 pa_tnc_attr_t
*ietf_attr_op_status_create(u_int8_t status
, u_int8_t result
,
257 private_ietf_attr_op_status_t
*this;
261 .pa_tnc_attribute
= {
262 .get_type
= _get_type
,
263 .get_value
= _get_value
,
264 .get_noskip_flag
= _get_noskip_flag
,
265 .set_noskip_flag
= _set_noskip_flag
,
271 .get_status
= _get_status
,
272 .get_result
= _get_result
,
273 .get_last_use
= _get_last_use
,
275 .type
= { PEN_IETF
, IETF_ATTR_OPERATIONAL_STATUS
},
278 .last_use
= last_use
,
282 return &this->public.pa_tnc_attribute
;
286 * Described in header.
288 pa_tnc_attr_t
*ietf_attr_op_status_create_from_data(chunk_t data
)
290 private_ietf_attr_op_status_t
*this;
294 .pa_tnc_attribute
= {
295 .get_type
= _get_type
,
296 .get_value
= _get_value
,
302 .get_status
= _get_status
,
303 .get_result
= _get_result
,
304 .get_last_use
= _get_last_use
,
306 .type
= { PEN_IETF
, IETF_ATTR_OPERATIONAL_STATUS
},
307 .value
= chunk_clone(data
),
311 return &this->public.pa_tnc_attribute
;