(no commit message)
[strongswan.git] / src / 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 <stddef.h>
24
25 #include "ts_payload.h"
26
27 #include <encoding/payloads/encodings.h>
28 #include <utils/linked_list.h>
29
30 typedef struct private_ts_payload_t private_ts_payload_t;
31
32 /**
33 * Private data of an ts_payload_t object.
34 *
35 */
36 struct private_ts_payload_t {
37 /**
38 * Public ts_payload_t interface.
39 */
40 ts_payload_t public;
41
42 /**
43 * TRUE if this TS payload is of type TSi, FALSE for TSr.
44 */
45 bool is_initiator;
46
47 /**
48 * Next payload type.
49 */
50 u_int8_t next_payload;
51
52 /**
53 * Critical flag.
54 */
55 bool critical;
56
57 /**
58 * Length of this payload.
59 */
60 u_int16_t payload_length;
61
62 /**
63 * Number of traffic selectors
64 */
65 u_int8_t number_of_traffic_selectors;
66
67 /**
68 * Contains the traffic selectors of type traffic_selector_substructure_t.
69 */
70 linked_list_t *traffic_selectors;
71
72 /**
73 * @brief Computes the length of this payload.
74 *
75 * @param this calling private_ts_payload_t object
76 */
77 void (*compute_length) (private_ts_payload_t *this);
78 };
79
80 /**
81 * Encoding rules to parse or generate a TS payload
82 *
83 * The defined offsets are the positions in a object of type
84 * private_ts_payload_t.
85 *
86 */
87 encoding_rule_t ts_payload_encodings[] = {
88 /* 1 Byte next payload type, stored in the field next_payload */
89 { U_INT_8, offsetof(private_ts_payload_t, next_payload) },
90 /* the critical bit */
91 { FLAG, offsetof(private_ts_payload_t, critical) },
92 /* 7 Bit reserved bits, nowhere stored */
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 { RESERVED_BIT, 0 },
100 /* Length of the whole payload*/
101 { PAYLOAD_LENGTH, offsetof(private_ts_payload_t, payload_length)},
102 /* 1 Byte TS type*/
103 { U_INT_8, offsetof(private_ts_payload_t, number_of_traffic_selectors) },
104 /* 3 reserved bytes */
105 { RESERVED_BYTE, 0 },
106 { RESERVED_BYTE, 0 },
107 { RESERVED_BYTE, 0 },
108 /* some ts data bytes, length is defined in PAYLOAD_LENGTH */
109 { TRAFFIC_SELECTORS, offsetof(private_ts_payload_t, traffic_selectors) }
110 };
111
112 /*
113 1 2 3
114 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
115 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
116 ! Next Payload !C! RESERVED ! Payload Length !
117 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
118 ! Number of TSs ! RESERVED !
119 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
120 ! !
121 ~ <Traffic Selectors> ~
122 ! !
123 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
124 */
125
126 /**
127 * Implementation of payload_t.verify.
128 */
129 static status_t verify(private_ts_payload_t *this)
130 {
131 iterator_t *iterator;
132 status_t status = SUCCESS;
133
134 if (this->number_of_traffic_selectors != (this->traffic_selectors->get_count(this->traffic_selectors)))
135 {
136 /* must be the same */
137 return FAILED;
138 }
139
140 iterator = this->traffic_selectors->create_iterator(this->traffic_selectors,TRUE);
141 while(iterator->has_next(iterator))
142 {
143 payload_t *current_traffic_selector;
144 iterator->current(iterator,(void **)&current_traffic_selector);
145
146 status = current_traffic_selector->verify(current_traffic_selector);
147 if (status != SUCCESS)
148 {
149 break;
150 }
151 }
152 iterator->destroy(iterator);
153
154 return status;
155 }
156
157 /**
158 * Implementation of ts_payload_t.get_encoding_rules.
159 */
160 static void get_encoding_rules(private_ts_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
161 {
162 *rules = ts_payload_encodings;
163 *rule_count = sizeof(ts_payload_encodings) / sizeof(encoding_rule_t);
164 }
165
166 /**
167 * Implementation of payload_t.get_type.
168 */
169 static payload_type_t get_payload_type(private_ts_payload_t *this)
170 {
171 if (this->is_initiator)
172 {
173 return TRAFFIC_SELECTOR_INITIATOR;
174 }
175 else
176 {
177 return TRAFFIC_SELECTOR_RESPONDER;
178 }
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 /**
240 * Implementation of ts_payload_t.get_traffic_selectors.
241 */
242 static linked_list_t *get_traffic_selectors(private_ts_payload_t *this)
243 {
244 traffic_selector_t *ts;
245 iterator_t *iterator;
246 linked_list_t *ts_list = linked_list_create();
247
248 iterator = this->traffic_selectors->create_iterator(this->traffic_selectors, TRUE);
249 while (iterator->has_next(iterator))
250 {
251 traffic_selector_substructure_t *ts_substructure;
252 iterator->current(iterator, (void**)&ts_substructure);
253 ts = ts_substructure->get_traffic_selector(ts_substructure);
254 ts_list->insert_last(ts_list, (void*)ts);
255 }
256 iterator->destroy(iterator);
257
258 return ts_list;
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 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 = malloc_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 = (linked_list_t *(*) (ts_payload_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, linked_list_t *traffic_selectors)
346 {
347 iterator_t *iterator;
348 traffic_selector_t *ts;
349 traffic_selector_substructure_t *ts_substructure;
350 private_ts_payload_t *this;
351
352 this = (private_ts_payload_t*)ts_payload_create(is_initiator);
353
354 iterator = traffic_selectors->create_iterator(traffic_selectors, TRUE);
355 while (iterator->has_next(iterator))
356 {
357 iterator->current(iterator, (void**)&ts);
358 ts_substructure = traffic_selector_substructure_create_from_traffic_selector(ts);
359 this->public.add_traffic_selector_substructure(&(this->public), ts_substructure);
360 }
361 iterator->destroy(iterator);
362
363 return &(this->public);
364 }
365