- transform_substructure fully implemented
[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
77 /**
78 * Encoding rules to parse or generate a Transform substructure
79 *
80 * The defined offsets are the positions in a object of type
81 * private_transform_substructure_t.
82 *
83 */
84 encoding_rule_t transform_substructure_encodings[] = {
85 /* 1 Byte next payload type, stored in the field next_payload */
86 { U_INT_8, offsetof(private_transform_substructure_t, next_payload) },
87 /* Reserved Byte is skipped */
88 { RESERVED_BYTE, 0 },
89 /* Length of the whole transform substructure*/
90 { PAYLOAD_LENGTH, offsetof(private_transform_substructure_t, transform_length) },
91 /* transform type is a number of 8 bit */
92 { U_INT_8, offsetof(private_transform_substructure_t, transform_type) },
93 /* Reserved Byte is skipped */
94 { RESERVED_BYTE, 0 },
95 /* tranform ID is a number of 8 bit */
96 { U_INT_16, offsetof(private_transform_substructure_t, transform_id) },
97 /* Attributes are stored in a transform attribute,
98 offset points to a linked_list_t pointer */
99 { TRANSFORM_ATTRIBUTES, offsetof(private_transform_substructure_t, attributes) }
100 };
101
102 /**
103 * Implements payload_t's and transform_substructure_t's destroy function.
104 * See #payload_s.destroy or transform_substructure_s.destroy for description.
105 */
106 static status_t destroy(private_transform_substructure_t *this)
107 {
108 /* all proposals are getting destroyed */
109 while (this->attributes->get_count(this->attributes) > 0)
110 {
111 transform_attribute_t *current_attribute;
112 if (this->attributes->remove_last(this->attributes,(void **)&current_attribute) != SUCCESS)
113 {
114 break;
115 }
116 current_attribute->destroy(current_attribute);
117 }
118 this->attributes->destroy(this->attributes);
119
120 allocator_free(this);
121
122 return SUCCESS;
123 }
124
125 /**
126 * Implements payload_t's get_encoding_rules function.
127 * See #payload_s.get_encoding_rules for description.
128 */
129 static status_t get_encoding_rules(private_transform_substructure_t *this, encoding_rule_t **rules, size_t *rule_count)
130 {
131 *rules = transform_substructure_encodings;
132 *rule_count = sizeof(transform_substructure_encodings) / sizeof(encoding_rule_t);
133
134 return SUCCESS;
135 }
136
137 /**
138 * Implements payload_t's get_type function.
139 * See #payload_s.get_type for description.
140 */
141 static payload_type_t get_type(private_transform_substructure_t *this)
142 {
143 return TRANSFORM_SUBSTRUCTURE;
144 }
145
146 /**
147 * Implements payload_t's get_next_type function.
148 * See #payload_s.get_next_type for description.
149 */
150 static payload_type_t get_next_type(private_transform_substructure_t *this)
151 {
152 return (this->next_payload);
153 }
154
155 /**
156 * Implements payload_t's get_length function.
157 * See #payload_s.get_length for description.
158 */
159 static size_t get_length(private_transform_substructure_t *this)
160 {
161 linked_list_iterator_t *iterator;
162 status_t status;
163 size_t length = TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH;
164 status = this->attributes->create_iterator(this->attributes,&iterator,TRUE);
165 if (status != SUCCESS)
166 return length;
167 while (iterator->has_next(iterator))
168 {
169 payload_t * current_attribute;
170 iterator->current(iterator,(void **) &current_attribute);
171 length += current_attribute->get_length(current_attribute);
172 }
173
174 this->transform_length = length;
175
176 return this->transform_length;
177 }
178
179 /**
180 * Implements transform_substructure_t's create_transform_attribute_iterator function.
181 * See #transform_substructure_s.create_transform_attribute_iterator for description.
182 */
183 static status_t create_transform_attribute_iterator (private_transform_substructure_t *this,linked_list_iterator_t **iterator,bool forward)
184 {
185 return (this->attributes->create_iterator(this->attributes,iterator,forward));
186 }
187
188 /**
189 * Implements transform_substructure_t's add_transform_attribute function.
190 * See #transform_substructure_s.add_transform_attribute for description.
191 */
192 static status_t add_transform_attribute (private_transform_substructure_t *this,transform_attribute_t *attribute)
193 {
194 return (this->attributes->insert_last(this->attributes,(void *) attribute));
195 this->transform_length += ((payload_t *)this->attributes)->get_length(((payload_t *)this->attributes));
196 return SUCCESS;
197 }
198
199 /**
200 * Implements transform_substructure_t's set_is_last_transform function.
201 * See #transform_substructure_s.set_is_last_transform for description.
202 */
203 static status_t set_is_last_transform (private_transform_substructure_t *this, bool is_last)
204 {
205 this->next_payload = (is_last) ? 0 : TRANSFORM_TYPE_VALUE;
206 return SUCCESS;
207 }
208
209 /**
210 * Implements transform_substructure_t's get_is_last_transform function.
211 * See #transform_substructure_s.get_is_last_transform for description.
212 */
213 static bool get_is_last_transform (private_transform_substructure_t *this)
214 {
215 return ((this->next_payload == TRANSFORM_TYPE_VALUE) ? FALSE : TRUE);
216 }
217
218 /**
219 * Implements transform_substructure_t's set_transform_type function.
220 * See #transform_substructure_s.set_transform_type for description.
221 */
222 static status_t set_transform_type (private_transform_substructure_t *this,u_int8_t type)
223 {
224 this->transform_type = type;
225 return SUCCESS;
226 }
227
228 /**
229 * Implements transform_substructure_t's get_transform_type function.
230 * See #transform_substructure_s.get_transform_type for description.
231 */
232 static u_int8_t get_transform_type (private_transform_substructure_t *this)
233 {
234 return this->transform_type;
235 }
236
237 /**
238 * Implements transform_substructure_t's set_transform_id function.
239 * See #transform_substructure_s.set_transform_id for description.
240 */
241 static status_t set_transform_id (private_transform_substructure_t *this,u_int16_t id)
242 {
243 this->transform_id = id;
244 return SUCCESS;
245 }
246
247 /**
248 * Implements transform_substructure_t's get_transform_id function.
249 * See #transform_substructure_s.get_transform_id for description.
250 */
251 static u_int16_t get_transform_id (private_transform_substructure_t *this)
252 {
253 return this->transform_id;
254 }
255
256 /*
257 * Described in header
258 */
259 transform_substructure_t *transform_substructure_create()
260 {
261 private_transform_substructure_t *this = allocator_alloc_thing(private_transform_substructure_t);
262 if (this == NULL)
263 {
264 return NULL;
265 }
266
267 this->public.payload_interface.get_encoding_rules = (status_t (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
268 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
269 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
270 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
271 this->public.payload_interface.destroy = (status_t (*) (payload_t *))destroy;
272 this->public.create_transform_attribute_iterator = (status_t (*) (transform_substructure_t *,linked_list_iterator_t **,bool)) create_transform_attribute_iterator;
273 this->public.add_transform_attribute = (status_t (*) (transform_substructure_t *,transform_attribute_t *)) add_transform_attribute;
274 this->public.set_is_last_transform = (status_t (*) (transform_substructure_t *,bool)) set_is_last_transform;
275 this->public.get_is_last_transform = (bool (*) (transform_substructure_t *)) get_is_last_transform;
276 this->public.set_transform_type = (status_t (*) (transform_substructure_t *,u_int8_t)) set_transform_type;
277 this->public.get_transform_type = (u_int8_t (*) (transform_substructure_t *)) get_transform_type;
278 this->public.set_transform_id = (status_t (*) (transform_substructure_t *,u_int16_t)) set_transform_id;
279 this->public.get_transform_id = (u_int16_t (*) (transform_substructure_t *)) get_transform_id;
280 this->public.destroy = (status_t (*) (transform_substructure_t *)) destroy;
281
282 /* set default values of the fields */
283 this->next_payload = NO_PAYLOAD;
284 this->transform_length = TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH;
285 this->transform_id = 0;
286 this->transform_type = 0;
287
288 this->attributes = linked_list_create();
289
290 if (this->attributes == NULL)
291 {
292 allocator_free(this);
293 return NULL;
294 }
295 return (&(this->public));
296 }