- parser succesfully parses sa payload
[strongswan.git] / Source / charon / payloads / transform_substructure.c
1 /**
2 * @file transform_substructure.h
3 *
4 * @brief Declaration of the class transform_substructure_t.
5 *
6 * An object of this type represents an IKEv2 TRANSFORM Substructure and contains Attributes.
7 *
8 */
9
10 /*
11 * Copyright (C) 2005 Jan Hutter, Martin Willi
12 * Hochschule fuer Technik Rapperswil
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
23 */
24
25 /* offsetof macro */
26 #include <stddef.h>
27
28 #include "transform_substructure.h"
29
30 #include "transform_attribute.h"
31 #include "encodings.h"
32 #include "../types.h"
33 #include "../utils/allocator.h"
34 #include "../utils/linked_list.h"
35
36 /**
37 * Private data of an transform_substructure_t' Object
38 *
39 */
40 typedef struct private_transform_substructure_s private_transform_substructure_t;
41
42 struct private_transform_substructure_s {
43 /**
44 * public transform_substructure_t interface
45 */
46 transform_substructure_t public;
47
48 /**
49 * next payload type
50 */
51 u_int8_t next_payload;
52
53
54 /**
55 * Length of this payload
56 */
57 u_int16_t transform_length;
58
59
60 /**
61 * Type of the transform
62 */
63 u_int8_t transform_type;
64
65 /**
66 * Transform ID
67 */
68 u_int16_t transform_id;
69
70 /**
71 * Transforms Attributes are stored in a linked_list_t
72 */
73 linked_list_t *attributes;
74
75 /**
76 * @brief Computes the length of this substructure.
77 *
78 * @param this calling private_transform_substructure_t object
79 * @return
80 * SUCCESS in any case
81 */
82 status_t (*compute_length) (private_transform_substructure_t *this);
83 };
84
85
86 /**
87 * Encoding rules to parse or generate a Transform substructure
88 *
89 * The defined offsets are the positions in a object of type
90 * private_transform_substructure_t.
91 *
92 */
93 encoding_rule_t transform_substructure_encodings[] = {
94 /* 1 Byte next payload type, stored in the field next_payload */
95 { U_INT_8, offsetof(private_transform_substructure_t, next_payload) },
96 /* Reserved Byte is skipped */
97 { RESERVED_BYTE, 0 },
98 /* Length of the whole transform substructure*/
99 { PAYLOAD_LENGTH, offsetof(private_transform_substructure_t, transform_length) },
100 /* transform type is a number of 8 bit */
101 { U_INT_8, offsetof(private_transform_substructure_t, transform_type) },
102 /* Reserved Byte is skipped */
103 { RESERVED_BYTE, 0 },
104 /* tranform ID is a number of 8 bit */
105 { U_INT_16, offsetof(private_transform_substructure_t, transform_id) },
106 /* Attributes are stored in a transform attribute,
107 offset points to a linked_list_t pointer */
108 { TRANSFORM_ATTRIBUTES, offsetof(private_transform_substructure_t, attributes) }
109 };
110
111 /**
112 * Implements payload_t's and transform_substructure_t's destroy function.
113 * See #payload_s.destroy or transform_substructure_s.destroy for description.
114 */
115 static status_t destroy(private_transform_substructure_t *this)
116 {
117 /* all proposals are getting destroyed */
118 while (this->attributes->get_count(this->attributes) > 0)
119 {
120 transform_attribute_t *current_attribute;
121 if (this->attributes->remove_last(this->attributes,(void **)&current_attribute) != SUCCESS)
122 {
123 break;
124 }
125 current_attribute->destroy(current_attribute);
126 }
127 this->attributes->destroy(this->attributes);
128
129 allocator_free(this);
130
131 return SUCCESS;
132 }
133
134 /**
135 * Implements payload_t's get_encoding_rules function.
136 * See #payload_s.get_encoding_rules for description.
137 */
138 static status_t get_encoding_rules(private_transform_substructure_t *this, encoding_rule_t **rules, size_t *rule_count)
139 {
140 *rules = transform_substructure_encodings;
141 *rule_count = sizeof(transform_substructure_encodings) / sizeof(encoding_rule_t);
142
143 return SUCCESS;
144 }
145
146 /**
147 * Implements payload_t's get_type function.
148 * See #payload_s.get_type for description.
149 */
150 static payload_type_t get_type(private_transform_substructure_t *this)
151 {
152 return TRANSFORM_SUBSTRUCTURE;
153 }
154
155 /**
156 * Implements payload_t's get_next_type function.
157 * See #payload_s.get_next_type for description.
158 */
159 static payload_type_t get_next_type(private_transform_substructure_t *this)
160 {
161 return (this->next_payload);
162 }
163
164 /**
165 * Implements payload_t's get_length function.
166 * See #payload_s.get_length for description.
167 */
168 static size_t get_length(private_transform_substructure_t *this)
169 {
170 this->compute_length(this);
171
172 return this->transform_length;
173 }
174
175 /**
176 * Implements transform_substructure_t's create_transform_attribute_iterator function.
177 * See #transform_substructure_s.create_transform_attribute_iterator for description.
178 */
179 static status_t create_transform_attribute_iterator (private_transform_substructure_t *this,linked_list_iterator_t **iterator,bool forward)
180 {
181 return (this->attributes->create_iterator(this->attributes,iterator,forward));
182 }
183
184 /**
185 * Implements transform_substructure_t's add_transform_attribute function.
186 * See #transform_substructure_s.add_transform_attribute for description.
187 */
188 static status_t add_transform_attribute (private_transform_substructure_t *this,transform_attribute_t *attribute)
189 {
190 status_t status;
191 status = this->attributes->insert_last(this->attributes,(void *) attribute);
192 this->compute_length(this);
193 return status;
194 }
195
196 /**
197 * Implements transform_substructure_t's set_is_last_transform function.
198 * See #transform_substructure_s.set_is_last_transform for description.
199 */
200 static status_t set_is_last_transform (private_transform_substructure_t *this, bool is_last)
201 {
202 this->next_payload = (is_last) ? 0: TRANSFORM_TYPE_VALUE;
203 return SUCCESS;
204 }
205
206 /**
207 * Implements transform_substructure_t's get_is_last_transform function.
208 * See #transform_substructure_s.get_is_last_transform for description.
209 */
210 static bool get_is_last_transform (private_transform_substructure_t *this)
211 {
212 return ((this->next_payload == TRANSFORM_TYPE_VALUE) ? FALSE : TRUE);
213 }
214
215 /**
216 * Implements transform_substructure_t's set_transform_type function.
217 * See #transform_substructure_s.set_transform_type for description.
218 */
219 static status_t set_transform_type (private_transform_substructure_t *this,u_int8_t type)
220 {
221 this->transform_type = type;
222 return SUCCESS;
223 }
224
225 /**
226 * Implements transform_substructure_t's get_transform_type function.
227 * See #transform_substructure_s.get_transform_type for description.
228 */
229 static u_int8_t get_transform_type (private_transform_substructure_t *this)
230 {
231 return this->transform_type;
232 }
233
234 /**
235 * Implements transform_substructure_t's set_transform_id function.
236 * See #transform_substructure_s.set_transform_id for description.
237 */
238 static status_t set_transform_id (private_transform_substructure_t *this,u_int16_t id)
239 {
240 this->transform_id = id;
241 return SUCCESS;
242 }
243
244 /**
245 * Implements transform_substructure_t's get_transform_id function.
246 * See #transform_substructure_s.get_transform_id for description.
247 */
248 static u_int16_t get_transform_id (private_transform_substructure_t *this)
249 {
250 return this->transform_id;
251 }
252
253 /**
254 * Implements private_transform_substructure_t's compute_length function.
255 * See #private_transform_substructure_s.compute_length for description.
256 */
257 static status_t compute_length (private_transform_substructure_t *this)
258 {
259 linked_list_iterator_t *iterator;
260 status_t status;
261 size_t length = TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH;
262 status = this->attributes->create_iterator(this->attributes,&iterator,TRUE);
263 if (status != SUCCESS)
264 {
265 return length;
266 }
267 while (iterator->has_next(iterator))
268 {
269 payload_t * current_attribute;
270 iterator->current(iterator,(void **) &current_attribute);
271 length += current_attribute->get_length(current_attribute);
272 }
273 iterator->destroy(iterator);
274
275 return SUCCESS;
276 }
277
278 /*
279 * Described in header
280 */
281 transform_substructure_t *transform_substructure_create()
282 {
283 private_transform_substructure_t *this = allocator_alloc_thing(private_transform_substructure_t);
284 if (this == NULL)
285 {
286 return NULL;
287 }
288
289 this->public.payload_interface.get_encoding_rules = (status_t (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
290 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
291 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
292 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
293 this->public.payload_interface.destroy = (status_t (*) (payload_t *))destroy;
294 this->public.create_transform_attribute_iterator = (status_t (*) (transform_substructure_t *,linked_list_iterator_t **,bool)) create_transform_attribute_iterator;
295 this->public.add_transform_attribute = (status_t (*) (transform_substructure_t *,transform_attribute_t *)) add_transform_attribute;
296 this->public.set_is_last_transform = (status_t (*) (transform_substructure_t *,bool)) set_is_last_transform;
297 this->public.get_is_last_transform = (bool (*) (transform_substructure_t *)) get_is_last_transform;
298 this->public.set_transform_type = (status_t (*) (transform_substructure_t *,u_int8_t)) set_transform_type;
299 this->public.get_transform_type = (u_int8_t (*) (transform_substructure_t *)) get_transform_type;
300 this->public.set_transform_id = (status_t (*) (transform_substructure_t *,u_int16_t)) set_transform_id;
301 this->public.get_transform_id = (u_int16_t (*) (transform_substructure_t *)) get_transform_id;
302 this->public.destroy = (status_t (*) (transform_substructure_t *)) destroy;
303
304 /* private functions */
305 this->compute_length = compute_length;
306
307 /* set default values of the fields */
308 this->next_payload = NO_PAYLOAD;
309 this->transform_length = TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH;
310 this->transform_id = 0;
311 this->transform_type = 0;
312
313 this->attributes = linked_list_create();
314
315 if (this->attributes == NULL)
316 {
317 allocator_free(this);
318 return NULL;
319 }
320 return (&(this->public));
321 }