updated Doxyfile
[strongswan.git] / src / charon / encoding / payloads / transform_attribute.c
1 /*
2 * Copyright (C) 2005-2006 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * $Id$
17 */
18
19 #include <string.h>
20 #include <stddef.h>
21
22 #include "transform_attribute.h"
23
24 #include <encoding/payloads/encodings.h>
25 #include <library.h>
26
27 typedef struct private_transform_attribute_t private_transform_attribute_t;
28
29 /**
30 * Private data of an transform_attribute_t object.
31 *
32 */
33 struct private_transform_attribute_t {
34 /**
35 * Public transform_attribute_t interface.
36 */
37 transform_attribute_t public;
38
39 /**
40 * Attribute Format Flag.
41 *
42 * - TRUE means value is stored in attribute_length_or_value
43 * - FALSE means value is stored in attribute_value
44 */
45 bool attribute_format;
46
47 /**
48 * Type of the attribute.
49 */
50 u_int16_t attribute_type;
51
52 /**
53 * Attribute Length if attribute_format is 0, attribute Value otherwise.
54 */
55 u_int16_t attribute_length_or_value;
56
57 /**
58 * Attribute value as chunk if attribute_format is 0 (FALSE).
59 */
60 chunk_t attribute_value;
61 };
62
63
64 ENUM_BEGIN(transform_attribute_type_name, ATTRIBUTE_UNDEFINED, ATTRIBUTE_UNDEFINED,
65 "ATTRIBUTE_UNDEFINED");
66 ENUM_NEXT(transform_attribute_type_name, KEY_LENGTH, KEY_LENGTH, ATTRIBUTE_UNDEFINED,
67 "KEY_LENGTH");
68 ENUM_END(transform_attribute_type_name, KEY_LENGTH);
69
70 /**
71 * Encoding rules to parse or generate a Transform attribute.
72 *
73 * The defined offsets are the positions in a object of type
74 * private_transform_attribute_t.
75 *
76 */
77 encoding_rule_t transform_attribute_encodings[] = {
78 /* Flag defining the format of this payload */
79 { ATTRIBUTE_FORMAT, offsetof(private_transform_attribute_t, attribute_format) },
80 /* type of the attribute as 15 bit unsigned integer */
81 { ATTRIBUTE_TYPE, offsetof(private_transform_attribute_t, attribute_type) },
82 /* Length or value, depending on the attribute format flag */
83 { ATTRIBUTE_LENGTH_OR_VALUE, offsetof(private_transform_attribute_t, attribute_length_or_value) },
84 /* Value of attribute if attribute format flag is zero */
85 { ATTRIBUTE_VALUE, offsetof(private_transform_attribute_t, attribute_value) }
86 };
87
88 /*
89 1 2 3
90 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
91 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
92 !A! Attribute Type ! AF=0 Attribute Length !
93 !F! ! AF=1 Attribute Value !
94 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
95 ! AF=0 Attribute Value !
96 ! AF=1 Not Transmitted !
97 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
98 */
99
100 /**
101 * Implementation of payload_t.verify.
102 */
103 static status_t verify(private_transform_attribute_t *this)
104 {
105 if (this->attribute_type != KEY_LENGTH)
106 {
107 return FAILED;
108 }
109
110 return SUCCESS;
111 }
112
113 /**
114 * Implementation of payload_t.get_encoding_rules.
115 */
116 static void get_encoding_rules(private_transform_attribute_t *this, encoding_rule_t **rules, size_t *rule_count)
117 {
118 *rules = transform_attribute_encodings;
119 *rule_count = sizeof(transform_attribute_encodings) / sizeof(encoding_rule_t);
120 }
121
122 /**
123 * Implementation of payload_t.get_type.
124 */
125 static payload_type_t get_type(private_transform_attribute_t *this)
126 {
127 return TRANSFORM_ATTRIBUTE;
128 }
129
130 /**
131 * Implementation of payload_t.get_next_type.
132 */
133 static payload_type_t get_next_type(private_transform_attribute_t *this)
134 {
135 return (NO_PAYLOAD);
136 }
137
138 /**
139 * Implementation of payload_t.set_next_type.
140 */
141 static void set_next_type(private_transform_attribute_t *this,payload_type_t type)
142 {
143 }
144
145 /**
146 * Implementation of transform_attribute_t.get_length.
147 */
148 static size_t get_length(private_transform_attribute_t *this)
149 {
150 if (this->attribute_format == TRUE)
151 {
152 /*Attribute size is only 4 byte */
153 return 4;
154 }
155 return (this->attribute_length_or_value + 4);
156 }
157
158 /**
159 * Implementation of transform_attribute_t.set_value_chunk.
160 */
161 static void set_value_chunk(private_transform_attribute_t *this, chunk_t value)
162 {
163 if (this->attribute_value.ptr != NULL)
164 {
165 /* free existing value */
166 free(this->attribute_value.ptr);
167 this->attribute_value.ptr = NULL;
168 this->attribute_value.len = 0;
169
170 }
171
172 if (value.len > 2)
173 {
174 this->attribute_value.ptr = clalloc(value.ptr,value.len);
175 this->attribute_value.len = value.len;
176 this->attribute_length_or_value = value.len;
177 /* attribute has not a fixed length */
178 this->attribute_format = FALSE;
179 }
180 else
181 {
182 memcpy(&(this->attribute_length_or_value),value.ptr,value.len);
183 }
184 }
185
186 /**
187 * Implementation of transform_attribute_t.set_value.
188 */
189 static void set_value(private_transform_attribute_t *this, u_int16_t value)
190 {
191 if (this->attribute_value.ptr != NULL)
192 {
193 /* free existing value */
194 free(this->attribute_value.ptr);
195 this->attribute_value.ptr = NULL;
196 this->attribute_value.len = 0;
197
198 }
199 this->attribute_length_or_value = value;
200 }
201
202 /**
203 * Implementation of transform_attribute_t.get_value_chunk.
204 */
205 static chunk_t get_value_chunk (private_transform_attribute_t *this)
206 {
207 chunk_t value;
208
209 if (this->attribute_format == FALSE)
210 {
211 value.ptr = this->attribute_value.ptr;
212 value.len = this->attribute_value.len;
213 }
214 else
215 {
216 value.ptr = (void *) &(this->attribute_length_or_value);
217 value.len = 2;
218 }
219
220 return value;
221 }
222
223 /**
224 * Implementation of transform_attribute_t.get_value.
225 */
226 static u_int16_t get_value (private_transform_attribute_t *this)
227 {
228 return this->attribute_length_or_value;
229 }
230
231
232 /**
233 * Implementation of transform_attribute_t.set_attribute_type.
234 */
235 static void set_attribute_type (private_transform_attribute_t *this, u_int16_t type)
236 {
237 this->attribute_type = type & 0x7FFF;
238 }
239
240 /**
241 * Implementation of transform_attribute_t.get_attribute_type.
242 */
243 static u_int16_t get_attribute_type (private_transform_attribute_t *this)
244 {
245 return this->attribute_type;
246 }
247
248 /**
249 * Implementation of transform_attribute_t.clone.
250 */
251 static transform_attribute_t * clone(private_transform_attribute_t *this)
252 {
253 private_transform_attribute_t *new_clone;
254
255 new_clone = (private_transform_attribute_t *) transform_attribute_create();
256
257 new_clone->attribute_format = this->attribute_format;
258 new_clone->attribute_type = this->attribute_type;
259 new_clone->attribute_length_or_value = this->attribute_length_or_value;
260
261 if (!new_clone->attribute_format)
262 {
263 new_clone->attribute_value.ptr = clalloc(this->attribute_value.ptr,this->attribute_value.len);
264 new_clone->attribute_value.len = this->attribute_value.len;
265 }
266
267 return (transform_attribute_t *) new_clone;
268 }
269
270 /**
271 * Implementation of transform_attribute_t.destroy and payload_t.destroy.
272 */
273 static void destroy(private_transform_attribute_t *this)
274 {
275 if (this->attribute_value.ptr != NULL)
276 {
277 free(this->attribute_value.ptr);
278 }
279 free(this);
280 }
281
282 /*
283 * Described in header.
284 */
285 transform_attribute_t *transform_attribute_create()
286 {
287 private_transform_attribute_t *this = malloc_thing(private_transform_attribute_t);
288
289 /* payload interface */
290 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
291 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
292 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
293 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
294 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
295 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
296 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
297
298 /* public functions */
299 this->public.set_value_chunk = (void (*) (transform_attribute_t *,chunk_t)) set_value_chunk;
300 this->public.set_value = (void (*) (transform_attribute_t *,u_int16_t)) set_value;
301 this->public.get_value_chunk = (chunk_t (*) (transform_attribute_t *)) get_value_chunk;
302 this->public.get_value = (u_int16_t (*) (transform_attribute_t *)) get_value;
303 this->public.set_attribute_type = (void (*) (transform_attribute_t *,u_int16_t type)) set_attribute_type;
304 this->public.get_attribute_type = (u_int16_t (*) (transform_attribute_t *)) get_attribute_type;
305 this->public.clone = (transform_attribute_t * (*) (transform_attribute_t *)) clone;
306 this->public.destroy = (void (*) (transform_attribute_t *)) destroy;
307
308 /* set default values of the fields */
309 this->attribute_format = TRUE;
310 this->attribute_type = 0;
311 this->attribute_length_or_value = 0;
312 this->attribute_value.ptr = NULL;
313 this->attribute_value.len = 0;
314
315 return (&(this->public));
316 }
317
318 /*
319 * Described in header.
320 */
321 transform_attribute_t *transform_attribute_create_key_length(u_int16_t key_length)
322 {
323 transform_attribute_t *attribute = transform_attribute_create();
324 attribute->set_attribute_type(attribute,KEY_LENGTH);
325 attribute->set_value(attribute,key_length);
326 return attribute;
327 }