2ca5bc8cb7287a122c26b67b1499332ff3394bc4
[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;
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
146 while(iterator->has_next(iterator))
147 {
148 payload_t *current_traffic_selector;
149 iterator->current(iterator,(void **)&current_traffic_selector);
150
151 status = current_traffic_selector->verify(current_traffic_selector);
152 if (status != SUCCESS)
153 {
154 break;
155 }
156 }
157
158 iterator->destroy(iterator);
159
160
161 return status;
162 }
163
164 /**
165 * Implementation of ts_payload_t.get_encoding_rules.
166 */
167 static void get_encoding_rules(private_ts_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
168 {
169 *rules = ts_payload_encodings;
170 *rule_count = sizeof(ts_payload_encodings) / sizeof(encoding_rule_t);
171 }
172
173 /**
174 * Implementation of payload_t.get_type.
175 */
176 static payload_type_t get_payload_type(private_ts_payload_t *this)
177 {
178 if (this->is_initiator)
179 {
180 return TRAFFIC_SELECTOR_INITIATOR;
181 }
182 else
183 {
184 return TRAFFIC_SELECTOR_RESPONDER;
185 }
186 }
187
188 /**
189 * Implementation of payload_t.get_next_type.
190 */
191 static payload_type_t get_next_type(private_ts_payload_t *this)
192 {
193 return (this->next_payload);
194 }
195
196 /**
197 * Implementation of payload_t.set_next_type.
198 */
199 static void set_next_type(private_ts_payload_t *this,payload_type_t type)
200 {
201 this->next_payload = type;
202 }
203
204 /**
205 * Implementation of payload_t.get_length.
206 */
207 static size_t get_length(private_ts_payload_t *this)
208 {
209 this->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 size_t get_traffic_selectors(private_ts_payload_t *this, traffic_selector_t **traffic_selectors[])
250 {
251 traffic_selector_t **ts;
252 iterator_t *iterator;
253 int i = 0;
254
255 ts = allocator_alloc(sizeof(traffic_selector_t*) * this->number_of_traffic_selectors);
256 iterator = this->traffic_selectors->create_iterator(this->traffic_selectors, TRUE);
257 while (iterator->has_next(iterator))
258 {
259 traffic_selector_substructure_t *ts_substructure;
260 iterator->current(iterator, (void**)&ts_substructure);
261 ts[i] = ts_substructure->get_traffic_selector(ts_substructure);
262 i++;
263 }
264 iterator->destroy(iterator);
265
266 /* return values */
267 *traffic_selectors = ts;
268 return this->number_of_traffic_selectors;
269 }
270
271 /**
272 * Implementation of private_ts_payload_t.compute_length.
273 */
274 static void compute_length (private_ts_payload_t *this)
275 {
276 iterator_t *iterator;
277 size_t ts_count = 0;
278 size_t length = TS_PAYLOAD_HEADER_LENGTH;
279 iterator = this->traffic_selectors->create_iterator(this->traffic_selectors,TRUE);
280 while (iterator->has_next(iterator))
281 {
282 payload_t * current_traffic_selector;
283 iterator->current(iterator,(void **) &current_traffic_selector);
284 length += current_traffic_selector->get_length(current_traffic_selector);
285 ts_count++;
286 }
287 iterator->destroy(iterator);
288
289 this->number_of_traffic_selectors= ts_count;
290 this->payload_length = length;
291
292 }
293
294
295 /**
296 * Implementation of payload_t.destroy and ts_payload_t.destroy.
297 */
298 static void destroy(private_ts_payload_t *this)
299 {
300 while (this->traffic_selectors->get_count(this->traffic_selectors) > 0)
301 {
302 payload_t *current_traffic_selector;
303
304 this->traffic_selectors->remove_last(this->traffic_selectors,(void **) &current_traffic_selector);
305
306 current_traffic_selector->destroy(current_traffic_selector);
307 }
308
309 this->traffic_selectors->destroy(this->traffic_selectors);
310
311 allocator_free(this);
312 }
313
314 /*
315 * Described in header
316 */
317 ts_payload_t *ts_payload_create(bool is_initiator)
318 {
319 private_ts_payload_t *this = allocator_alloc_thing(private_ts_payload_t);
320
321 /* interface functions */
322 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
323 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
324 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
325 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
326 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
327 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_payload_type;
328 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
329
330 /* public functions */
331 this->public.destroy = (void (*) (ts_payload_t *)) destroy;
332 this->public.get_initiator = (bool (*) (ts_payload_t *)) get_initiator;
333 this->public.set_initiator = (void (*) (ts_payload_t *,bool)) set_initiator;
334 this->public.add_traffic_selector_substructure = (void (*) (ts_payload_t *,traffic_selector_substructure_t *)) add_traffic_selector_substructure;
335 this->public.create_traffic_selector_substructure_iterator = (iterator_t* (*) (ts_payload_t *,bool)) create_traffic_selector_substructure_iterator;
336 this->public.get_traffic_selectors = (size_t (*) (ts_payload_t *, traffic_selector_t**[])) get_traffic_selectors;
337
338 /* private functions */
339 this->compute_length = compute_length;
340
341 /* private variables */
342 this->critical = FALSE;
343 this->next_payload = NO_PAYLOAD;
344 this->payload_length =TS_PAYLOAD_HEADER_LENGTH;
345 this->is_initiator = is_initiator;
346 this->number_of_traffic_selectors=0;
347 this->traffic_selectors = linked_list_create();
348
349 return &(this->public);
350 }
351
352 /*
353 * Described in header
354 */
355 ts_payload_t *ts_payload_create_from_traffic_selectors(bool is_initiator, traffic_selector_t *traffic_selectors[], size_t count)
356 {
357 int i;
358 private_ts_payload_t *this;
359
360 this = (private_ts_payload_t*)ts_payload_create(is_initiator);
361
362 for (i = 0; i < count; i++)
363 {
364 traffic_selector_substructure_t *ts_substructure;
365 ts_substructure = traffic_selector_substructure_create_from_traffic_selector(traffic_selectors[i]);
366 this->public.add_traffic_selector_substructure(&(this->public), ts_substructure);
367 }
368
369 return &(this->public);
370 }
371