Simplify signature of get_encoding_rules(), make all rules static
[strongswan.git] / src / libcharon / encoding / payloads / ts_payload.c
1 /*
2 * Copyright (C) 2005-2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
6 *
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>.
11 *
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
15 * for more details.
16 */
17
18 #include <stddef.h>
19
20 #include "ts_payload.h"
21
22 #include <encoding/payloads/encodings.h>
23 #include <utils/linked_list.h>
24
25 typedef struct private_ts_payload_t private_ts_payload_t;
26
27 /**
28 * Private data of an ts_payload_t object.
29 */
30 struct private_ts_payload_t {
31
32 /**
33 * Public ts_payload_t interface.
34 */
35 ts_payload_t public;
36
37 /**
38 * TRUE if this TS payload is of type TSi, FALSE for TSr.
39 */
40 bool is_initiator;
41
42 /**
43 * Next payload type.
44 */
45 u_int8_t next_payload;
46
47 /**
48 * Critical flag.
49 */
50 bool critical;
51
52 /**
53 * reserved bits
54 */
55 bool reserved_bit[7];
56
57 /**
58 * reserved bytes
59 */
60 bool reserved_byte[3];
61
62 /**
63 * Length of this payload.
64 */
65 u_int16_t payload_length;
66
67 /**
68 * Number of traffic selectors
69 */
70 u_int8_t ts_num;
71
72 /**
73 * Contains the traffic selectors of type traffic_selector_substructure_t.
74 */
75 linked_list_t *substrs;
76 };
77
78 /**
79 * Encoding rules to parse or generate a TS payload
80 *
81 * The defined offsets are the positions in a object of type
82 * private_ts_payload_t.
83 */
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) },
99 /* 1 Byte TS type*/
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) }
107 };
108
109 /*
110 1 2 3
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 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
117 ! !
118 ~ <Traffic Selectors> ~
119 ! !
120 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
121 */
122
123 METHOD(payload_t, verify, status_t,
124 private_ts_payload_t *this)
125 {
126 enumerator_t *enumerator;
127 payload_t *substr;
128 status_t status = SUCCESS;
129
130 if (this->ts_num != this->substrs->get_count(this->substrs))
131 {
132 return FAILED;
133 }
134 enumerator = this->substrs->create_enumerator(this->substrs);
135 while (enumerator->enumerate(enumerator, &substr))
136 {
137 status = substr->verify(substr);
138 if (status != SUCCESS)
139 {
140 break;
141 }
142 }
143 enumerator->destroy(enumerator);
144
145 return status;
146 }
147
148 METHOD(payload_t, get_encoding_rules, int,
149 private_ts_payload_t *this, encoding_rule_t **rules)
150 {
151 *rules = encodings;
152 return countof(encodings);
153 }
154
155 METHOD(payload_t, get_type, payload_type_t,
156 private_ts_payload_t *this)
157 {
158 if (this->is_initiator)
159 {
160 return TRAFFIC_SELECTOR_INITIATOR;
161 }
162 return TRAFFIC_SELECTOR_RESPONDER;
163 }
164
165 METHOD(payload_t, get_next_type, payload_type_t,
166 private_ts_payload_t *this)
167 {
168 return this->next_payload;
169 }
170
171 METHOD(payload_t, set_next_type, void,
172 private_ts_payload_t *this,payload_type_t type)
173 {
174 this->next_payload = type;
175 }
176
177 /**
178 * recompute the length of the payload.
179 */
180 static void compute_length(private_ts_payload_t *this)
181 {
182 enumerator_t *enumerator;
183 payload_t *subst;
184
185 this->payload_length = TS_PAYLOAD_HEADER_LENGTH;
186 this->ts_num = 0;
187 enumerator = this->substrs->create_enumerator(this->substrs);
188 while (enumerator->enumerate(enumerator, &subst))
189 {
190 this->payload_length += subst->get_length(subst);
191 this->ts_num++;
192 }
193 enumerator->destroy(enumerator);
194 }
195
196 METHOD(payload_t, get_length, size_t,
197 private_ts_payload_t *this)
198 {
199 return this->payload_length;
200 }
201
202 METHOD(ts_payload_t, get_initiator, bool,
203 private_ts_payload_t *this)
204 {
205 return this->is_initiator;
206 }
207
208 METHOD(ts_payload_t, set_initiator, void,
209 private_ts_payload_t *this,bool is_initiator)
210 {
211 this->is_initiator = is_initiator;
212 }
213
214 METHOD(ts_payload_t, get_traffic_selectors, linked_list_t*,
215 private_ts_payload_t *this)
216 {
217 traffic_selector_t *ts;
218 enumerator_t *enumerator;
219 traffic_selector_substructure_t *subst;
220 linked_list_t *list;
221
222 list = linked_list_create();
223 enumerator = this->substrs->create_enumerator(this->substrs);
224 while (enumerator->enumerate(enumerator, &subst))
225 {
226 ts = subst->get_traffic_selector(subst);
227 list->insert_last(list, ts);
228 }
229 enumerator->destroy(enumerator);
230
231 return list;
232 }
233
234 METHOD2(payload_t, ts_payload_t, destroy, void,
235 private_ts_payload_t *this)
236 {
237 this->substrs->destroy_offset(this->substrs, offsetof(payload_t, destroy));
238 free(this);
239 }
240
241 /*
242 * Described in header
243 */
244 ts_payload_t *ts_payload_create(bool is_initiator)
245 {
246 private_ts_payload_t *this;
247
248 INIT(this,
249 .public = {
250 .payload_interface = {
251 .verify = _verify,
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,
257 .destroy = _destroy,
258 },
259 .get_initiator = _get_initiator,
260 .set_initiator = _set_initiator,
261 .get_traffic_selectors = _get_traffic_selectors,
262 .destroy = _destroy,
263 },
264 .next_payload = NO_PAYLOAD,
265 .payload_length = TS_PAYLOAD_HEADER_LENGTH,
266 .is_initiator = is_initiator,
267 .substrs = linked_list_create(),
268 );
269 return &this->public;
270 }
271
272 /*
273 * Described in header
274 */
275 ts_payload_t *ts_payload_create_from_traffic_selectors(bool is_initiator,
276 linked_list_t *traffic_selectors)
277 {
278 enumerator_t *enumerator;
279 traffic_selector_t *ts;
280 traffic_selector_substructure_t *subst;
281 private_ts_payload_t *this;
282
283 this = (private_ts_payload_t*)ts_payload_create(is_initiator);
284
285 enumerator = traffic_selectors->create_enumerator(traffic_selectors);
286 while (enumerator->enumerate(enumerator, &ts))
287 {
288 subst = traffic_selector_substructure_create_from_traffic_selector(ts);
289 this->substrs->insert_last(this->substrs, subst);
290 }
291 enumerator->destroy(enumerator);
292 compute_length(this);
293
294 return &this->public;
295 }