- creation of ts payloads using ts
[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 return ((this->is_initiator) ? TRAFFIC_SELECTOR_INITIATOR : TRAFFIC_SELECTOR_RESPONDER);
179 }
180
181 /**
182 * Implementation of payload_t.get_next_type.
183 */
184 static payload_type_t get_next_type(private_ts_payload_t *this)
185 {
186 return (this->next_payload);
187 }
188
189 /**
190 * Implementation of payload_t.set_next_type.
191 */
192 static void set_next_type(private_ts_payload_t *this,payload_type_t type)
193 {
194 this->next_payload = type;
195 }
196
197 /**
198 * Implementation of payload_t.get_length.
199 */
200 static size_t get_length(private_ts_payload_t *this)
201 {
202 this->compute_length(this);
203 return this->payload_length;
204 }
205
206 /**
207 * Implementation of ts_payload_t.get_initiator.
208 */
209 static bool get_initiator (private_ts_payload_t *this)
210 {
211 return (this->is_initiator);
212 }
213
214 /**
215 * Implementation of ts_payload_t.set_initiator.
216 */
217 static void set_initiator (private_ts_payload_t *this,bool is_initiator)
218 {
219 this->is_initiator = is_initiator;
220 }
221
222 /**
223 * Implementation of ts_payload_t.add_traffic_selector_substructure.
224 */
225 static void add_traffic_selector_substructure (private_ts_payload_t *this,traffic_selector_substructure_t *traffic_selector)
226 {
227 this->traffic_selectors->insert_last(this->traffic_selectors,traffic_selector);
228 this->number_of_traffic_selectors = this->traffic_selectors->get_count(this->traffic_selectors);
229 }
230
231 /**
232 * Implementation of ts_payload_t.create_traffic_selector_substructure_iterator.
233 */
234 static iterator_t * create_traffic_selector_substructure_iterator (private_ts_payload_t *this, bool forward)
235 {
236 return this->traffic_selectors->create_iterator(this->traffic_selectors,forward);
237 }
238
239 static size_t get_traffic_selectors(private_ts_payload_t *this, traffic_selector_t **traffic_selectors[])
240 {
241 traffic_selector_t **ts;
242 iterator_t *iterator;
243 int i = 0;
244
245 //ts = allocator_alloc(sizeof(traffic_selector_t*) * this->number_of_traffic_selectors);
246 iterator = this->traffic_selectors->create_iterator(this->traffic_selectors, TRUE);
247 int x = this->traffic_selectors->get_count(this->traffic_selectors);
248 while (iterator->has_next)
249 {
250 traffic_selector_substructure_t *ts_substructure;
251 iterator->current(iterator, (void**)&ts_substructure);
252 //ts[i] = ts_substructure->get_traffic_selector(ts_substructure);
253 i++;
254 }
255
256 /* return values */
257 //*traffic_selectors = ts;
258 return this->number_of_traffic_selectors;
259 }
260
261 /**
262 * Implementation of private_ts_payload_t.compute_length.
263 */
264 static void compute_length (private_ts_payload_t *this)
265 {
266 iterator_t *iterator;
267 size_t ts_count = 0;
268 size_t length = TS_PAYLOAD_HEADER_LENGTH;
269 iterator = this->traffic_selectors->create_iterator(this->traffic_selectors,TRUE);
270 while (iterator->has_next(iterator))
271 {
272 payload_t * current_traffic_selector;
273 iterator->current(iterator,(void **) &current_traffic_selector);
274 length += current_traffic_selector->get_length(current_traffic_selector);
275 ts_count++;
276 }
277 iterator->destroy(iterator);
278
279 this->number_of_traffic_selectors= ts_count;
280 this->payload_length = length;
281
282 }
283
284
285 /**
286 * Implementation of payload_t.destroy and ts_payload_t.destroy.
287 */
288 static void destroy(private_ts_payload_t *this)
289 {
290 while (this->traffic_selectors->get_count(this->traffic_selectors) > 0)
291 {
292 payload_t *current_traffic_selector;
293
294 this->traffic_selectors->remove_last(this->traffic_selectors,(void **) &current_traffic_selector);
295
296 current_traffic_selector->destroy(current_traffic_selector);
297 }
298
299 this->traffic_selectors->destroy(this->traffic_selectors);
300
301 allocator_free(this);
302 }
303
304 /*
305 * Described in header
306 */
307 ts_payload_t *ts_payload_create(bool is_initiator)
308 {
309 private_ts_payload_t *this = allocator_alloc_thing(private_ts_payload_t);
310
311 /* interface functions */
312 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
313 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
314 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
315 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
316 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
317 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_payload_type;
318 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
319
320 /* public functions */
321 this->public.destroy = (void (*) (ts_payload_t *)) destroy;
322 this->public.get_initiator = (bool (*) (ts_payload_t *)) get_initiator;
323 this->public.set_initiator = (void (*) (ts_payload_t *,bool)) set_initiator;
324 this->public.add_traffic_selector_substructure = (void (*) (ts_payload_t *,traffic_selector_substructure_t *)) add_traffic_selector_substructure;
325 this->public.create_traffic_selector_substructure_iterator = (iterator_t* (*) (ts_payload_t *,bool)) create_traffic_selector_substructure_iterator;
326 this->public.get_traffic_selectors = (size_t (*) (ts_payload_t *, traffic_selector_t**[])) get_traffic_selectors;
327
328 /* private functions */
329 this->compute_length = compute_length;
330
331 /* private variables */
332 this->critical = FALSE;
333 this->next_payload = NO_PAYLOAD;
334 this->payload_length =TS_PAYLOAD_HEADER_LENGTH;
335 this->is_initiator = is_initiator;
336 this->number_of_traffic_selectors=0;
337 this->traffic_selectors = linked_list_create();
338
339 return &(this->public);
340 }
341
342 /*
343 * Described in header
344 */
345 ts_payload_t *ts_payload_create_from_traffic_selectors(bool is_initiator, traffic_selector_t *traffic_selectors[], size_t count)
346 {
347 int i;
348 private_ts_payload_t *this;
349
350 this = (private_ts_payload_t*)ts_payload_create(is_initiator);
351
352 for (i = 0; i < count; i++)
353 {
354 traffic_selector_substructure_t *ts_substructure;
355 ts_substructure = traffic_selector_substructure_create_from_traffic_selector(traffic_selectors[i]);
356 this->public.add_traffic_selector_substructure(&(this->public), ts_substructure);
357 }
358
359 return &(this->public);
360 }
361