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