- changed creation of iterator
[strongswan.git] / Source / charon / encoding / payloads / transform_attribute.c
1 /**
2 * @file transform_attribute.c
3 *
4 * @brief Implementation of transform_attribute_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 /* offsetof macro */
24 #include <stddef.h>
25
26 #include "transform_attribute.h"
27
28 #include <encoding/payloads/encodings.h>
29 #include <types.h>
30 #include <utils/allocator.h>
31
32 typedef struct private_transform_attribute_t private_transform_attribute_t;
33
34 /**
35 * Private data of an transform_attribute_t object.
36 *
37 */
38 struct private_transform_attribute_t {
39 /**
40 * Public transform_attribute_t interface.
41 */
42 transform_attribute_t public;
43
44 /**
45 * Attribute Format Flag.
46 *
47 * - TRUE means value is stored in attribute_length_or_value
48 * - FALSE means value is stored in attribute_value
49 */
50 bool attribute_format;
51
52 /**
53 * Type of the attribute.
54 */
55 u_int16_t attribute_type;
56
57 /**
58 * Attribute Length if attribute_format is 0, attribute Value otherwise.
59 */
60 u_int16_t attribute_length_or_value;
61
62 /**
63 * Attribute value as chunk if attribute_format is 0 (FALSE).
64 */
65 chunk_t attribute_value;
66 };
67
68 /**
69 * String mappings for transform_attribute_type_t.
70 */
71 mapping_t transform_attribute_type_m[] = {
72 {ATTRIBUTE_UNDEFINED, "ATTRIBUTE_UNDEFINED"},
73 {KEY_LENGTH, "KEY_LENGTH"},
74 {MAPPING_END, NULL}
75 };
76
77 /**
78 * Encoding rules to parse or generate a Transform attribute.
79 *
80 * The defined offsets are the positions in a object of type
81 * private_transform_attribute_t.
82 *
83 */
84 encoding_rule_t transform_attribute_encodings[] = {
85 /* Flag defining the format of this payload */
86 { ATTRIBUTE_FORMAT, offsetof(private_transform_attribute_t, attribute_format) },
87 /* type of the attribute as 15 bit unsigned integer */
88 { ATTRIBUTE_TYPE, offsetof(private_transform_attribute_t, attribute_type) },
89 /* Length or value, depending on the attribute format flag */
90 { ATTRIBUTE_LENGTH_OR_VALUE, offsetof(private_transform_attribute_t, attribute_length_or_value) },
91 /* Value of attribute if attribute format flag is zero */
92 { ATTRIBUTE_VALUE, offsetof(private_transform_attribute_t, attribute_value) }
93 };
94
95 /*
96 1 2 3
97 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
98 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
99 !A! Attribute Type ! AF=0 Attribute Length !
100 !F! ! AF=1 Attribute Value !
101 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
102 ! AF=0 Attribute Value !
103 ! AF=1 Not Transmitted !
104 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105 */
106
107 /**
108 * Implementation of payload_t.verify.
109 */
110 static status_t verify(private_transform_attribute_t *this)
111 {
112 if (this->attribute_type != KEY_LENGTH)
113 {
114 return FAILED;
115 }
116
117 return SUCCESS;
118 }
119
120 /**
121 * Implementation of payload_t.get_encoding_rules.
122 */
123 static void get_encoding_rules(private_transform_attribute_t *this, encoding_rule_t **rules, size_t *rule_count)
124 {
125 *rules = transform_attribute_encodings;
126 *rule_count = sizeof(transform_attribute_encodings) / sizeof(encoding_rule_t);
127 }
128
129 /**
130 * Implementation of payload_t.get_type.
131 */
132 static payload_type_t get_type(private_transform_attribute_t *this)
133 {
134 return TRANSFORM_ATTRIBUTE;
135 }
136
137 /**
138 * Implementation of payload_t.get_next_type.
139 */
140 static payload_type_t get_next_type(private_transform_attribute_t *this)
141 {
142 return (NO_PAYLOAD);
143 }
144
145 /**
146 * Implementation of payload_t.set_next_type.
147 */
148 static void set_next_type(private_transform_attribute_t *this,payload_type_t type)
149 {
150 }
151
152 /**
153 * Implementation of transform_attribute_t.get_length.
154 */
155 static size_t get_length(private_transform_attribute_t *this)
156 {
157 if (this->attribute_format == TRUE)
158 {
159 /*Attribute size is only 4 byte */
160 return 4;
161 }
162 return (this->attribute_length_or_value + 4);
163 }
164
165 /**
166 * Implementation of transform_attribute_t.set_value_chunk.
167 */
168 static void set_value_chunk(private_transform_attribute_t *this, chunk_t value)
169 {
170 if (this->attribute_value.ptr != NULL)
171 {
172 /* free existing value */
173 allocator_free(this->attribute_value.ptr);
174 this->attribute_value.ptr = NULL;
175 this->attribute_value.len = 0;
176
177 }
178
179 if (value.len > 2)
180 {
181 this->attribute_value.ptr = allocator_clone_bytes(value.ptr,value.len);
182 this->attribute_value.len = value.len;
183 this->attribute_length_or_value = value.len;
184 /* attribute has not a fixed length */
185 this->attribute_format = FALSE;
186 }
187 else
188 {
189 memcpy(&(this->attribute_length_or_value),value.ptr,value.len);
190 }
191 }
192
193 /**
194 * Implementation of transform_attribute_t.set_value.
195 */
196 static void set_value(private_transform_attribute_t *this, u_int16_t value)
197 {
198 if (this->attribute_value.ptr != NULL)
199 {
200 /* free existing value */
201 allocator_free(this->attribute_value.ptr);
202 this->attribute_value.ptr = NULL;
203 this->attribute_value.len = 0;
204
205 }
206 this->attribute_length_or_value = value;
207 }
208
209 /**
210 * Implementation of transform_attribute_t.get_value_chunk.
211 */
212 static chunk_t get_value_chunk (private_transform_attribute_t *this)
213 {
214 chunk_t value;
215
216 if (this->attribute_format == FALSE)
217 {
218 value.ptr = this->attribute_value.ptr;
219 value.len = this->attribute_value.len;
220 }
221 else
222 {
223 value.ptr = (void *) &(this->attribute_length_or_value);
224 value.len = 2;
225 }
226
227 return value;
228 }
229
230 /**
231 * Implementation of transform_attribute_t.get_value.
232 */
233 static u_int16_t get_value (private_transform_attribute_t *this)
234 {
235 return this->attribute_length_or_value;
236 }
237
238
239 /**
240 * Implementation of transform_attribute_t.set_attribute_type.
241 */
242 static void set_attribute_type (private_transform_attribute_t *this, u_int16_t type)
243 {
244 this->attribute_type = type & 0x7FFF;
245 }
246
247 /**
248 * Implementation of transform_attribute_t.get_attribute_type.
249 */
250 static u_int16_t get_attribute_type (private_transform_attribute_t *this)
251 {
252 return this->attribute_type;
253 }
254
255 /**
256 * Implementation of transform_attribute_t.clone.
257 */
258 static transform_attribute_t * clone(private_transform_attribute_t *this)
259 {
260 private_transform_attribute_t *new_clone;
261
262 new_clone = (private_transform_attribute_t *) transform_attribute_create();
263
264 new_clone->attribute_format = this->attribute_format;
265 new_clone->attribute_type = this->attribute_type;
266 new_clone->attribute_length_or_value = this->attribute_length_or_value;
267
268 if (!new_clone->attribute_format)
269 {
270 new_clone->attribute_value.ptr = allocator_clone_bytes(this->attribute_value.ptr,this->attribute_value.len);
271 new_clone->attribute_value.len = this->attribute_value.len;
272 }
273
274 return (transform_attribute_t *) new_clone;
275 }
276
277 /**
278 * Implementation of transform_attribute_t.destroy and payload_t.destroy.
279 */
280 static void destroy(private_transform_attribute_t *this)
281 {
282 if (this->attribute_value.ptr != NULL)
283 {
284 allocator_free(this->attribute_value.ptr);
285 }
286 allocator_free(this);
287 }
288
289 /*
290 * Described in header.
291 */
292 transform_attribute_t *transform_attribute_create()
293 {
294 private_transform_attribute_t *this = allocator_alloc_thing(private_transform_attribute_t);
295
296 /* payload interface */
297 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
298 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
299 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
300 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
301 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
302 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
303 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
304
305 /* public functions */
306 this->public.set_value_chunk = (void (*) (transform_attribute_t *,chunk_t)) set_value_chunk;
307 this->public.set_value = (void (*) (transform_attribute_t *,u_int16_t)) set_value;
308 this->public.get_value_chunk = (chunk_t (*) (transform_attribute_t *)) get_value_chunk;
309 this->public.get_value = (u_int16_t (*) (transform_attribute_t *)) get_value;
310 this->public.set_attribute_type = (void (*) (transform_attribute_t *,u_int16_t type)) set_attribute_type;
311 this->public.get_attribute_type = (u_int16_t (*) (transform_attribute_t *)) get_attribute_type;
312 this->public.clone = (transform_attribute_t * (*) (transform_attribute_t *)) clone;
313 this->public.destroy = (void (*) (transform_attribute_t *)) destroy;
314
315 /* set default values of the fields */
316 this->attribute_format = TRUE;
317 this->attribute_type = 0;
318 this->attribute_length_or_value = 0;
319 this->attribute_value.ptr = NULL;
320 this->attribute_value.len = 0;
321
322 return (&(this->public));
323 }
324