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