2 * Copyright (C) 2010 Sansar Choinyanbuu
3 * Copyright (C) 2010 Andreas Steffen
5 * HSR Hochschule fuer Technik Rapperswil
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 #include "pb_pa_msg.h"
20 #include <tnc/tnccs/tnccs.h>
22 #include <bio/bio_writer.h>
23 #include <bio/bio_reader.h>
27 typedef struct private_pb_pa_msg_t private_pb_pa_msg_t
;
33 * 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
34 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 * | Flags | PA Message Vendor ID |
36 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 * | Posture Collector Identifier | Posture Validator Identifier |
40 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 * | PA Message Body (Variable Length) |
42 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 #define PA_FLAG_NONE 0x00
46 #define PA_FLAG_EXCL (1<<7)
47 #define PA_RESERVED_SUBTYPE 0xffffffff
51 * Private data of a pb_pa_msg_t object.
54 struct private_pb_pa_msg_t
{
56 * Public pb_pa_msg_t interface.
63 pb_tnc_msg_type_t type
;
71 * PA Message Vendor ID
81 * Posture Validator Identifier
83 u_int16_t collector_id
;
86 * Posture Validator Identifier
88 u_int16_t validator_id
;
101 METHOD(pb_tnc_msg_t
, get_type
, pb_tnc_msg_type_t
,
102 private_pb_pa_msg_t
*this)
107 METHOD(pb_tnc_msg_t
, get_encoding
, chunk_t
,
108 private_pb_pa_msg_t
*this)
110 return this->encoding
;
113 METHOD(pb_tnc_msg_t
, build
, void,
114 private_pb_pa_msg_t
*this)
117 bio_writer_t
*writer
;
119 /* build message header */
120 writer
= bio_writer_create(64);
121 writer
->write_uint8 (writer
, this->excl ? PA_FLAG_EXCL
: PA_FLAG_NONE
);
122 writer
->write_uint24(writer
, this->vendor_id
);
123 writer
->write_uint32(writer
, this->subtype
);
124 writer
->write_uint16(writer
, this->collector_id
);
125 writer
->write_uint16(writer
, this->validator_id
);
126 msg_header
= writer
->get_buf(writer
);
128 /* create encoding by concatenating message header and message body */
129 free(this->encoding
.ptr
);
130 this->encoding
= chunk_cat("cc", msg_header
, this->msg_body
);
131 writer
->destroy(writer
);
134 METHOD(pb_tnc_msg_t
, process
, status_t
,
135 private_pb_pa_msg_t
*this, u_int32_t
*offset
)
139 bio_reader_t
*reader
;
141 /* process message header */
142 reader
= bio_reader_create(this->encoding
);
143 reader
->read_uint8 (reader
, &flags
);
144 reader
->read_uint24(reader
, &this->vendor_id
);
145 reader
->read_uint32(reader
, &this->subtype
);
146 reader
->read_uint16(reader
, &this->collector_id
);
147 reader
->read_uint16(reader
, &this->validator_id
);
148 this->excl
= ((flags
& PA_FLAG_EXCL
) != PA_FLAG_NONE
);
150 /* process message body */
151 msg_body_len
= reader
->remaining(reader
);
154 reader
->read_data(reader
, msg_body_len
, &this->msg_body
);
155 this->msg_body
= chunk_clone(this->msg_body
);
157 reader
->destroy(reader
);
159 if (this->vendor_id
== PEN_RESERVED
)
161 DBG1(DBG_TNC
, "Vendor ID 0x%06x is reserved", PEN_RESERVED
);
166 if (this->subtype
== PA_RESERVED_SUBTYPE
)
168 DBG1(DBG_TNC
, "PA Subtype 0x%08x is reserved", PA_RESERVED_SUBTYPE
);
175 METHOD(pb_tnc_msg_t
, destroy
, void,
176 private_pb_pa_msg_t
*this)
178 free(this->encoding
.ptr
);
179 free(this->msg_body
.ptr
);
183 METHOD(pb_pa_msg_t
, get_vendor_id
, u_int32_t
,
184 private_pb_pa_msg_t
*this, u_int32_t
*subtype
)
186 *subtype
= this->subtype
;
187 return this->vendor_id
;
190 METHOD(pb_pa_msg_t
, get_collector_id
, u_int16_t
,
191 private_pb_pa_msg_t
*this)
193 return this->collector_id
;
196 METHOD(pb_pa_msg_t
, get_validator_id
, u_int16_t
,
197 private_pb_pa_msg_t
*this)
199 return this->validator_id
;
202 METHOD(pb_pa_msg_t
, get_body
, chunk_t
,
203 private_pb_pa_msg_t
*this)
205 return this->msg_body
;
208 METHOD(pb_pa_msg_t
, get_exclusive_flag
, bool,
209 private_pb_pa_msg_t
*this)
214 METHOD(pb_pa_msg_t
, set_exclusive_flag
, void,
215 private_pb_pa_msg_t
*this, bool excl
)
223 pb_tnc_msg_t
*pb_pa_msg_create_from_data(chunk_t data
)
225 private_pb_pa_msg_t
*this;
230 .get_type
= _get_type
,
231 .get_encoding
= _get_encoding
,
235 .get_vendor_id
= _get_vendor_id
,
236 .get_collector_id
= _get_collector_id
,
237 .get_validator_id
= _get_validator_id
,
238 .get_body
= _get_body
,
239 .get_exclusive_flag
= _get_exclusive_flag
,
240 .set_exclusive_flag
= _set_exclusive_flag
,
243 .encoding
= chunk_clone(data
),
246 return &this->public.pb_interface
;
252 pb_tnc_msg_t
*pb_pa_msg_create(u_int32_t vendor_id
, u_int32_t subtype
,
253 u_int16_t collector_id
, u_int16_t validator_id
,
256 private_pb_pa_msg_t
*this;
261 .get_type
= _get_type
,
262 .get_encoding
= _get_encoding
,
267 .get_vendor_id
= _get_vendor_id
,
268 .get_collector_id
= _get_collector_id
,
269 .get_validator_id
= _get_validator_id
,
270 .get_body
= _get_body
,
271 .get_exclusive_flag
= _get_exclusive_flag
,
272 .set_exclusive_flag
= _set_exclusive_flag
,
275 .vendor_id
= vendor_id
,
277 .collector_id
= collector_id
,
278 .validator_id
= validator_id
,
279 .msg_body
= chunk_clone(msg_body
),
282 return &this->public.pb_interface
;