2 * Copyright (C) 2005-2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
4 * Copyright (C) 2005 Jan Hutter
5 * 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
20 #include "ts_payload.h"
22 #include <encoding/payloads/encodings.h>
23 #include <utils/linked_list.h>
25 typedef struct private_ts_payload_t private_ts_payload_t
;
28 * Private data of an ts_payload_t object.
30 struct private_ts_payload_t
{
33 * Public ts_payload_t interface.
38 * TRUE if this TS payload is of type TSi, FALSE for TSr.
45 u_int8_t next_payload
;
60 bool reserved_byte
[3];
63 * Length of this payload.
65 u_int16_t payload_length
;
68 * Number of traffic selectors
73 * Contains the traffic selectors of type traffic_selector_substructure_t.
75 linked_list_t
*substrs
;
79 * Encoding rules to parse or generate a TS payload
81 * The defined offsets are the positions in a object of type
82 * private_ts_payload_t.
84 static encoding_rule_t encodings
[] = {
85 /* 1 Byte next payload type, stored in the field next_payload */
86 { U_INT_8
, offsetof(private_ts_payload_t
, next_payload
) },
87 /* the critical bit */
88 { FLAG
, offsetof(private_ts_payload_t
, critical
) },
89 /* 7 Bit reserved bits */
90 { RESERVED_BIT
, offsetof(private_ts_payload_t
, reserved_bit
[0]) },
91 { RESERVED_BIT
, offsetof(private_ts_payload_t
, reserved_bit
[1]) },
92 { RESERVED_BIT
, offsetof(private_ts_payload_t
, reserved_bit
[2]) },
93 { RESERVED_BIT
, offsetof(private_ts_payload_t
, reserved_bit
[3]) },
94 { RESERVED_BIT
, offsetof(private_ts_payload_t
, reserved_bit
[4]) },
95 { RESERVED_BIT
, offsetof(private_ts_payload_t
, reserved_bit
[5]) },
96 { RESERVED_BIT
, offsetof(private_ts_payload_t
, reserved_bit
[6]) },
97 /* Length of the whole payload*/
98 { PAYLOAD_LENGTH
, offsetof(private_ts_payload_t
, payload_length
) },
100 { U_INT_8
, offsetof(private_ts_payload_t
, ts_num
) },
101 /* 3 reserved bytes */
102 { RESERVED_BYTE
, offsetof(private_ts_payload_t
, reserved_byte
[0])},
103 { RESERVED_BYTE
, offsetof(private_ts_payload_t
, reserved_byte
[1])},
104 { RESERVED_BYTE
, offsetof(private_ts_payload_t
, reserved_byte
[2])},
105 /* wrapped list of traffic selectors substructures */
106 { PAYLOAD_LIST
+ TRAFFIC_SELECTOR_SUBSTRUCTURE
,
107 offsetof(private_ts_payload_t
, substrs
) },
112 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
113 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
114 ! Next Payload !C! RESERVED ! Payload Length !
115 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
116 ! Number of TSs ! RESERVED !
117 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
119 ~ <Traffic Selectors> ~
121 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
124 METHOD(payload_t
, verify
, status_t
,
125 private_ts_payload_t
*this)
127 enumerator_t
*enumerator
;
129 status_t status
= SUCCESS
;
131 if (this->ts_num
!= this->substrs
->get_count(this->substrs
))
135 enumerator
= this->substrs
->create_enumerator(this->substrs
);
136 while (enumerator
->enumerate(enumerator
, &substr
))
138 status
= substr
->verify(substr
);
139 if (status
!= SUCCESS
)
144 enumerator
->destroy(enumerator
);
149 METHOD(payload_t
, get_encoding_rules
, int,
150 private_ts_payload_t
*this, encoding_rule_t
**rules
)
153 return countof(encodings
);
156 METHOD(payload_t
, get_header_length
, int,
157 private_ts_payload_t
*this)
162 METHOD(payload_t
, get_type
, payload_type_t
,
163 private_ts_payload_t
*this)
165 if (this->is_initiator
)
167 return TRAFFIC_SELECTOR_INITIATOR
;
169 return TRAFFIC_SELECTOR_RESPONDER
;
172 METHOD(payload_t
, get_next_type
, payload_type_t
,
173 private_ts_payload_t
*this)
175 return this->next_payload
;
178 METHOD(payload_t
, set_next_type
, void,
179 private_ts_payload_t
*this,payload_type_t type
)
181 this->next_payload
= type
;
185 * recompute the length of the payload.
187 static void compute_length(private_ts_payload_t
*this)
189 enumerator_t
*enumerator
;
192 this->payload_length
= get_header_length(this);
194 enumerator
= this->substrs
->create_enumerator(this->substrs
);
195 while (enumerator
->enumerate(enumerator
, &subst
))
197 this->payload_length
+= subst
->get_length(subst
);
200 enumerator
->destroy(enumerator
);
203 METHOD(payload_t
, get_length
, size_t,
204 private_ts_payload_t
*this)
206 return this->payload_length
;
209 METHOD(ts_payload_t
, get_initiator
, bool,
210 private_ts_payload_t
*this)
212 return this->is_initiator
;
215 METHOD(ts_payload_t
, set_initiator
, void,
216 private_ts_payload_t
*this,bool is_initiator
)
218 this->is_initiator
= is_initiator
;
221 METHOD(ts_payload_t
, get_traffic_selectors
, linked_list_t
*,
222 private_ts_payload_t
*this)
224 traffic_selector_t
*ts
;
225 enumerator_t
*enumerator
;
226 traffic_selector_substructure_t
*subst
;
229 list
= linked_list_create();
230 enumerator
= this->substrs
->create_enumerator(this->substrs
);
231 while (enumerator
->enumerate(enumerator
, &subst
))
233 ts
= subst
->get_traffic_selector(subst
);
234 list
->insert_last(list
, ts
);
236 enumerator
->destroy(enumerator
);
241 METHOD2(payload_t
, ts_payload_t
, destroy
, void,
242 private_ts_payload_t
*this)
244 this->substrs
->destroy_offset(this->substrs
, offsetof(payload_t
, destroy
));
249 * Described in header
251 ts_payload_t
*ts_payload_create(bool is_initiator
)
253 private_ts_payload_t
*this;
257 .payload_interface
= {
259 .get_encoding_rules
= _get_encoding_rules
,
260 .get_header_length
= _get_header_length
,
261 .get_length
= _get_length
,
262 .get_next_type
= _get_next_type
,
263 .set_next_type
= _set_next_type
,
264 .get_type
= _get_type
,
267 .get_initiator
= _get_initiator
,
268 .set_initiator
= _set_initiator
,
269 .get_traffic_selectors
= _get_traffic_selectors
,
272 .next_payload
= NO_PAYLOAD
,
273 .payload_length
= get_header_length(this),
274 .is_initiator
= is_initiator
,
275 .substrs
= linked_list_create(),
277 return &this->public;
281 * Described in header
283 ts_payload_t
*ts_payload_create_from_traffic_selectors(bool is_initiator
,
284 linked_list_t
*traffic_selectors
)
286 enumerator_t
*enumerator
;
287 traffic_selector_t
*ts
;
288 traffic_selector_substructure_t
*subst
;
289 private_ts_payload_t
*this;
291 this = (private_ts_payload_t
*)ts_payload_create(is_initiator
);
293 enumerator
= traffic_selectors
->create_enumerator(traffic_selectors
);
294 while (enumerator
->enumerate(enumerator
, &ts
))
296 subst
= traffic_selector_substructure_create_from_traffic_selector(ts
);
297 this->substrs
->insert_last(this->substrs
, subst
);
299 enumerator
->destroy(enumerator
);
300 compute_length(this);
302 return &this->public;