2 * Copyright (C) 2015 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 "generic_attr_bool.h"
19 #include <pa_tnc/pa_tnc_msg.h>
20 #include <bio/bio_writer.h>
21 #include <bio/bio_reader.h>
22 #include <utils/debug.h>
24 typedef struct private_generic_attr_bool_t private_generic_attr_bool_t
;
27 * Generic PA-TNC attribute containing boolean status value in 32 bit encoding
30 * 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
31 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 #define ATTR_BOOL_SIZE 4
39 * Private data of an generic_attr_bool_t object.
41 struct private_generic_attr_bool_t
{
44 * Public members of generic_attr_bool_t
46 generic_attr_bool_t
public;
49 * Vendor-specific attribute type
54 * Length of attribute value
59 * Attribute value or segment
69 * Boolean status value
79 METHOD(pa_tnc_attr_t
, get_type
, pen_type_t
,
80 private_generic_attr_bool_t
*this)
85 METHOD(pa_tnc_attr_t
, get_value
, chunk_t
,
86 private_generic_attr_bool_t
*this)
91 METHOD(pa_tnc_attr_t
, get_noskip_flag
, bool,
92 private_generic_attr_bool_t
*this)
94 return this->noskip_flag
;
97 METHOD(pa_tnc_attr_t
, set_noskip_flag
,void,
98 private_generic_attr_bool_t
*this, bool noskip
)
100 this->noskip_flag
= noskip
;
103 METHOD(pa_tnc_attr_t
, build
, void,
104 private_generic_attr_bool_t
*this)
106 bio_writer_t
*writer
;
112 writer
= bio_writer_create(ATTR_BOOL_SIZE
);
113 writer
->write_uint32(writer
, this->status
);
115 this->value
= writer
->extract_buf(writer
);
116 this->length
= this->value
.len
;
117 writer
->destroy(writer
);
120 METHOD(pa_tnc_attr_t
, process
, status_t
,
121 private_generic_attr_bool_t
*this, u_int32_t
*offset
)
123 enum_name_t
*pa_attr_names
;
124 bio_reader_t
*reader
;
127 pa_attr_names
= imcv_pa_tnc_attributes
->get_names(imcv_pa_tnc_attributes
,
128 this->type
.vendor_id
);
131 if (this->value
.len
< this->length
)
135 if (this->value
.len
!= ATTR_BOOL_SIZE
)
137 DBG1(DBG_TNC
, "incorrect attribute size for %N/%N",
138 pen_names
, this->type
.vendor_id
, pa_attr_names
, this->type
.type
);
141 reader
= bio_reader_create(this->value
);
142 reader
->read_uint32(reader
, &status
);
143 reader
->destroy(reader
);
147 DBG1(DBG_TNC
, "%N/%N attribute contains invalid non-boolean value %u",
148 pen_names
, this->type
.vendor_id
, pa_attr_names
, this->type
.type
,
152 this->status
= status
;
157 METHOD(pa_tnc_attr_t
, add_segment
, void,
158 private_generic_attr_bool_t
*this, chunk_t segment
)
160 this->value
= chunk_cat("mc", this->value
, segment
);
163 METHOD(pa_tnc_attr_t
, get_ref
, pa_tnc_attr_t
*,
164 private_generic_attr_bool_t
*this)
167 return &this->public.pa_tnc_attribute
;
170 METHOD(pa_tnc_attr_t
, destroy
, void,
171 private_generic_attr_bool_t
*this)
173 if (ref_put(&this->ref
))
175 free(this->value
.ptr
);
180 METHOD(generic_attr_bool_t
, get_status
, bool,
181 private_generic_attr_bool_t
*this)
187 * Described in header.
189 pa_tnc_attr_t
*generic_attr_bool_create(bool status
, pen_type_t type
)
191 private_generic_attr_bool_t
*this;
195 .pa_tnc_attribute
= {
196 .get_type
= _get_type
,
197 .get_value
= _get_value
,
198 .get_noskip_flag
= _get_noskip_flag
,
199 .set_noskip_flag
= _set_noskip_flag
,
202 .add_segment
= _add_segment
,
206 .get_status
= _get_status
,
213 return &this->public.pa_tnc_attribute
;
217 * Described in header.
219 pa_tnc_attr_t
*generic_attr_bool_create_from_data(size_t length
, chunk_t data
,
222 private_generic_attr_bool_t
*this;
226 .pa_tnc_attribute
= {
227 .get_type
= _get_type
,
228 .get_value
= _get_value
,
229 .get_noskip_flag
= _get_noskip_flag
,
230 .set_noskip_flag
= _set_noskip_flag
,
233 .add_segment
= _add_segment
,
237 .get_status
= _get_status
,
241 .value
= chunk_clone(data
),
245 return &this->public.pa_tnc_attribute
;