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 encoding_rule_t ts_payload_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 /* some ts data bytes, length is defined in PAYLOAD_LENGTH */
106 { TRAFFIC_SELECTORS
,offsetof(private_ts_payload_t
, substrs
) }
111 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
112 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
113 ! Next Payload !C! RESERVED ! Payload Length !
114 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
115 ! Number of TSs ! RESERVED !
116 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
118 ~ <Traffic Selectors> ~
120 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
123 METHOD(payload_t
, verify
, status_t
,
124 private_ts_payload_t
*this)
126 enumerator_t
*enumerator
;
128 status_t status
= SUCCESS
;
130 if (this->ts_num
!= this->substrs
->get_count(this->substrs
))
134 enumerator
= this->substrs
->create_enumerator(this->substrs
);
135 while (enumerator
->enumerate(enumerator
, &substr
))
137 status
= substr
->verify(substr
);
138 if (status
!= SUCCESS
)
143 enumerator
->destroy(enumerator
);
148 METHOD(payload_t
, get_encoding_rules
, void,
149 private_ts_payload_t
*this, encoding_rule_t
**rules
, size_t *rule_count
)
151 *rules
= ts_payload_encodings
;
152 *rule_count
= countof(ts_payload_encodings
);
155 METHOD(payload_t
, get_type
, payload_type_t
,
156 private_ts_payload_t
*this)
158 if (this->is_initiator
)
160 return TRAFFIC_SELECTOR_INITIATOR
;
162 return TRAFFIC_SELECTOR_RESPONDER
;
165 METHOD(payload_t
, get_next_type
, payload_type_t
,
166 private_ts_payload_t
*this)
168 return this->next_payload
;
171 METHOD(payload_t
, set_next_type
, void,
172 private_ts_payload_t
*this,payload_type_t type
)
174 this->next_payload
= type
;
178 * recompute the length of the payload.
180 static void compute_length(private_ts_payload_t
*this)
182 enumerator_t
*enumerator
;
185 this->payload_length
= TS_PAYLOAD_HEADER_LENGTH
;
187 enumerator
= this->substrs
->create_enumerator(this->substrs
);
188 while (enumerator
->enumerate(enumerator
, &subst
))
190 this->payload_length
+= subst
->get_length(subst
);
193 enumerator
->destroy(enumerator
);
196 METHOD(payload_t
, get_length
, size_t,
197 private_ts_payload_t
*this)
199 return this->payload_length
;
202 METHOD(ts_payload_t
, get_initiator
, bool,
203 private_ts_payload_t
*this)
205 return this->is_initiator
;
208 METHOD(ts_payload_t
, set_initiator
, void,
209 private_ts_payload_t
*this,bool is_initiator
)
211 this->is_initiator
= is_initiator
;
214 METHOD(ts_payload_t
, get_traffic_selectors
, linked_list_t
*,
215 private_ts_payload_t
*this)
217 traffic_selector_t
*ts
;
218 enumerator_t
*enumerator
;
219 traffic_selector_substructure_t
*subst
;
222 list
= linked_list_create();
223 enumerator
= this->substrs
->create_enumerator(this->substrs
);
224 while (enumerator
->enumerate(enumerator
, &subst
))
226 ts
= subst
->get_traffic_selector(subst
);
227 list
->insert_last(list
, ts
);
229 enumerator
->destroy(enumerator
);
234 METHOD2(payload_t
, ts_payload_t
, destroy
, void,
235 private_ts_payload_t
*this)
237 this->substrs
->destroy_offset(this->substrs
, offsetof(payload_t
, destroy
));
242 * Described in header
244 ts_payload_t
*ts_payload_create(bool is_initiator
)
246 private_ts_payload_t
*this;
250 .payload_interface
= {
252 .get_encoding_rules
= _get_encoding_rules
,
253 .get_length
= _get_length
,
254 .get_next_type
= _get_next_type
,
255 .set_next_type
= _set_next_type
,
256 .get_type
= _get_type
,
259 .get_initiator
= _get_initiator
,
260 .set_initiator
= _set_initiator
,
261 .get_traffic_selectors
= _get_traffic_selectors
,
264 .next_payload
= NO_PAYLOAD
,
265 .payload_length
= TS_PAYLOAD_HEADER_LENGTH
,
266 .is_initiator
= is_initiator
,
267 .substrs
= linked_list_create(),
269 return &this->public;
273 * Described in header
275 ts_payload_t
*ts_payload_create_from_traffic_selectors(bool is_initiator
,
276 linked_list_t
*traffic_selectors
)
278 enumerator_t
*enumerator
;
279 traffic_selector_t
*ts
;
280 traffic_selector_substructure_t
*subst
;
281 private_ts_payload_t
*this;
283 this = (private_ts_payload_t
*)ts_payload_create(is_initiator
);
285 enumerator
= traffic_selectors
->create_enumerator(traffic_selectors
);
286 while (enumerator
->enumerate(enumerator
, &ts
))
288 subst
= traffic_selector_substructure_create_from_traffic_selector(ts
);
289 this->substrs
->insert_last(this->substrs
, subst
);
291 enumerator
->destroy(enumerator
);
292 compute_length(this);
294 return &this->public;