updated Doxyfile
[strongswan.git] / src / charon / encoding / payloads / ts_payload.c
1 /*
2 * Copyright (C) 2005-2006 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * $Id$
17 */
18
19 #include <stddef.h>
20
21 #include "ts_payload.h"
22
23 #include <encoding/payloads/encodings.h>
24 #include <utils/linked_list.h>
25
26 typedef struct private_ts_payload_t private_ts_payload_t;
27
28 /**
29 * Private data of an ts_payload_t object.
30 *
31 */
32 struct private_ts_payload_t {
33 /**
34 * Public ts_payload_t interface.
35 */
36 ts_payload_t public;
37
38 /**
39 * TRUE if this TS payload is of type TSi, FALSE for TSr.
40 */
41 bool is_initiator;
42
43 /**
44 * Next payload type.
45 */
46 u_int8_t next_payload;
47
48 /**
49 * Critical flag.
50 */
51 bool critical;
52
53 /**
54 * Length of this payload.
55 */
56 u_int16_t payload_length;
57
58 /**
59 * Number of traffic selectors
60 */
61 u_int8_t number_of_traffic_selectors;
62
63 /**
64 * Contains the traffic selectors of type traffic_selector_substructure_t.
65 */
66 linked_list_t *traffic_selectors;
67 };
68
69 /**
70 * Encoding rules to parse or generate a TS payload
71 *
72 * The defined offsets are the positions in a object of type
73 * private_ts_payload_t.
74 *
75 */
76 encoding_rule_t ts_payload_encodings[] = {
77 /* 1 Byte next payload type, stored in the field next_payload */
78 { U_INT_8, offsetof(private_ts_payload_t, next_payload) },
79 /* the critical bit */
80 { FLAG, offsetof(private_ts_payload_t, critical) },
81 /* 7 Bit reserved bits, nowhere stored */
82 { RESERVED_BIT, 0 },
83 { RESERVED_BIT, 0 },
84 { RESERVED_BIT, 0 },
85 { RESERVED_BIT, 0 },
86 { RESERVED_BIT, 0 },
87 { RESERVED_BIT, 0 },
88 { RESERVED_BIT, 0 },
89 /* Length of the whole payload*/
90 { PAYLOAD_LENGTH, offsetof(private_ts_payload_t, payload_length)},
91 /* 1 Byte TS type*/
92 { U_INT_8, offsetof(private_ts_payload_t, number_of_traffic_selectors) },
93 /* 3 reserved bytes */
94 { RESERVED_BYTE, 0 },
95 { RESERVED_BYTE, 0 },
96 { RESERVED_BYTE, 0 },
97 /* some ts data bytes, length is defined in PAYLOAD_LENGTH */
98 { TRAFFIC_SELECTORS, offsetof(private_ts_payload_t, traffic_selectors) }
99 };
100
101 /*
102 1 2 3
103 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
104 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105 ! Next Payload !C! RESERVED ! Payload Length !
106 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
107 ! Number of TSs ! RESERVED !
108 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
109 ! !
110 ~ <Traffic Selectors> ~
111 ! !
112 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
113 */
114
115 /**
116 * Implementation of payload_t.verify.
117 */
118 static status_t verify(private_ts_payload_t *this)
119 {
120 iterator_t *iterator;
121 payload_t *current_traffic_selector;
122 status_t status = SUCCESS;
123
124 if (this->number_of_traffic_selectors != (this->traffic_selectors->get_count(this->traffic_selectors)))
125 {
126 /* must be the same */
127 return FAILED;
128 }
129
130 iterator = this->traffic_selectors->create_iterator(this->traffic_selectors,TRUE);
131 while(iterator->iterate(iterator, (void**)&current_traffic_selector))
132 {
133 status = current_traffic_selector->verify(current_traffic_selector);
134 if (status != SUCCESS)
135 {
136 break;
137 }
138 }
139 iterator->destroy(iterator);
140
141 return status;
142 }
143
144 /**
145 * Implementation of ts_payload_t.get_encoding_rules.
146 */
147 static void get_encoding_rules(private_ts_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
148 {
149 *rules = ts_payload_encodings;
150 *rule_count = sizeof(ts_payload_encodings) / sizeof(encoding_rule_t);
151 }
152
153 /**
154 * Implementation of payload_t.get_type.
155 */
156 static payload_type_t get_payload_type(private_ts_payload_t *this)
157 {
158 if (this->is_initiator)
159 {
160 return TRAFFIC_SELECTOR_INITIATOR;
161 }
162 else
163 {
164 return TRAFFIC_SELECTOR_RESPONDER;
165 }
166 }
167
168 /**
169 * Implementation of payload_t.get_next_type.
170 */
171 static payload_type_t get_next_type(private_ts_payload_t *this)
172 {
173 return (this->next_payload);
174 }
175
176 /**
177 * Implementation of payload_t.set_next_type.
178 */
179 static void set_next_type(private_ts_payload_t *this,payload_type_t type)
180 {
181 this->next_payload = type;
182 }
183
184 /**
185 * recompute the length of the payload.
186 */
187 static void compute_length (private_ts_payload_t *this)
188 {
189 iterator_t *iterator;
190 size_t ts_count = 0;
191 size_t length = TS_PAYLOAD_HEADER_LENGTH;
192 payload_t *current_traffic_selector;
193
194 iterator = this->traffic_selectors->create_iterator(this->traffic_selectors,TRUE);
195 while (iterator->iterate(iterator, (void**)&current_traffic_selector))
196 {
197 length += current_traffic_selector->get_length(current_traffic_selector);
198 ts_count++;
199 }
200 iterator->destroy(iterator);
201
202 this->number_of_traffic_selectors= ts_count;
203 this->payload_length = length;
204 }
205
206 /**
207 * Implementation of payload_t.get_length.
208 */
209 static size_t get_length(private_ts_payload_t *this)
210 {
211 compute_length(this);
212 return this->payload_length;
213 }
214
215 /**
216 * Implementation of ts_payload_t.get_initiator.
217 */
218 static bool get_initiator (private_ts_payload_t *this)
219 {
220 return (this->is_initiator);
221 }
222
223 /**
224 * Implementation of ts_payload_t.set_initiator.
225 */
226 static void set_initiator (private_ts_payload_t *this,bool is_initiator)
227 {
228 this->is_initiator = is_initiator;
229 }
230
231 /**
232 * Implementation of ts_payload_t.add_traffic_selector_substructure.
233 */
234 static void add_traffic_selector_substructure (private_ts_payload_t *this,traffic_selector_substructure_t *traffic_selector)
235 {
236 this->traffic_selectors->insert_last(this->traffic_selectors,traffic_selector);
237 this->number_of_traffic_selectors = this->traffic_selectors->get_count(this->traffic_selectors);
238 }
239
240 /**
241 * Implementation of ts_payload_t.create_traffic_selector_substructure_iterator.
242 */
243 static iterator_t * create_traffic_selector_substructure_iterator (private_ts_payload_t *this, bool forward)
244 {
245 return this->traffic_selectors->create_iterator(this->traffic_selectors,forward);
246 }
247
248 /**
249 * Implementation of ts_payload_t.get_traffic_selectors.
250 */
251 static linked_list_t *get_traffic_selectors(private_ts_payload_t *this)
252 {
253 traffic_selector_t *ts;
254 iterator_t *iterator;
255 traffic_selector_substructure_t *ts_substructure;
256 linked_list_t *ts_list = linked_list_create();
257
258 iterator = this->traffic_selectors->create_iterator(this->traffic_selectors, TRUE);
259 while (iterator->iterate(iterator, (void**)&ts_substructure))
260 {
261 ts = ts_substructure->get_traffic_selector(ts_substructure);
262 ts_list->insert_last(ts_list, (void*)ts);
263 }
264 iterator->destroy(iterator);
265
266 return ts_list;
267 }
268
269 /**
270 * Implementation of payload_t.destroy and ts_payload_t.destroy.
271 */
272 static void destroy(private_ts_payload_t *this)
273 {
274 this->traffic_selectors->destroy_offset(this->traffic_selectors,
275 offsetof(payload_t, destroy));
276 free(this);
277 }
278
279 /*
280 * Described in header
281 */
282 ts_payload_t *ts_payload_create(bool is_initiator)
283 {
284 private_ts_payload_t *this = malloc_thing(private_ts_payload_t);
285
286 /* interface functions */
287 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
288 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
289 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
290 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
291 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
292 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_payload_type;
293 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
294
295 /* public functions */
296 this->public.destroy = (void (*) (ts_payload_t *)) destroy;
297 this->public.get_initiator = (bool (*) (ts_payload_t *)) get_initiator;
298 this->public.set_initiator = (void (*) (ts_payload_t *,bool)) set_initiator;
299 this->public.add_traffic_selector_substructure = (void (*) (ts_payload_t *,traffic_selector_substructure_t *)) add_traffic_selector_substructure;
300 this->public.create_traffic_selector_substructure_iterator = (iterator_t* (*) (ts_payload_t *,bool)) create_traffic_selector_substructure_iterator;
301 this->public.get_traffic_selectors = (linked_list_t *(*) (ts_payload_t *)) get_traffic_selectors;
302
303 /* private variables */
304 this->critical = FALSE;
305 this->next_payload = NO_PAYLOAD;
306 this->payload_length =TS_PAYLOAD_HEADER_LENGTH;
307 this->is_initiator = is_initiator;
308 this->number_of_traffic_selectors = 0;
309 this->traffic_selectors = linked_list_create();
310
311 return &(this->public);
312 }
313
314 /*
315 * Described in header
316 */
317 ts_payload_t *ts_payload_create_from_traffic_selectors(bool is_initiator, linked_list_t *traffic_selectors)
318 {
319 iterator_t *iterator;
320 traffic_selector_t *ts;
321 traffic_selector_substructure_t *ts_substructure;
322 private_ts_payload_t *this;
323
324 this = (private_ts_payload_t*)ts_payload_create(is_initiator);
325
326 iterator = traffic_selectors->create_iterator(traffic_selectors, TRUE);
327 while (iterator->iterate(iterator, (void**)&ts))
328 {
329 ts_substructure = traffic_selector_substructure_create_from_traffic_selector(ts);
330 this->public.add_traffic_selector_substructure(&(this->public), ts_substructure);
331 }
332 iterator->destroy(iterator);
333
334 return &(this->public);
335 }
336