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