- parser succesfully parses sa payload
[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 * Encoding rules to parse or generate a Transform attribute
73 *
74 * The defined offsets are the positions in a object of type
75 * private_transform_attribute_t.
76 *
77 */
78 encoding_rule_t transform_attribute_encodings[] = {
79 /* Flag defining the format of this payload */
80 { ATTRIBUTE_FORMAT, offsetof(private_transform_attribute_t, attribute_format) },
81 /* type of the attribute as 15 bit unsigned integer */
82 { ATTRIBUTE_TYPE, offsetof(private_transform_attribute_t, attribute_type) },
83 /* Length or value, depending on the attribute format flag */
84 { ATTRIBUTE_LENGTH_OR_VALUE, offsetof(private_transform_attribute_t, attribute_length_or_value) },
85 /* Value of attribute if attribute format flag is zero */
86 { ATTRIBUTE_VALUE, offsetof(private_transform_attribute_t, attribute_value) }
87 };
88
89 /**
90 * Implements payload_t's and transform_attribute_t's destroy function.
91 * See #payload_s.destroy or transform_attribute_s.destroy for description.
92 */
93 static status_t destroy(private_transform_attribute_t *this)
94 {
95 if (this->attribute_value.ptr != NULL)
96 {
97 allocator_free(this->attribute_value.ptr);
98 }
99 allocator_free(this);
100
101 return SUCCESS;
102 }
103
104 /**
105 * Implements payload_t's get_encoding_rules function.
106 * See #payload_s.get_encoding_rules for description.
107 */
108 static status_t get_encoding_rules(private_transform_attribute_t *this, encoding_rule_t **rules, size_t *rule_count)
109 {
110 *rules = transform_attribute_encodings;
111 *rule_count = sizeof(transform_attribute_encodings) / sizeof(encoding_rule_t);
112
113 return SUCCESS;
114 }
115
116 /**
117 * Implements payload_t's get_type function.
118 * See #payload_s.get_type for description.
119 */
120 static payload_type_t get_type(private_transform_attribute_t *this)
121 {
122 return TRANSFORM_ATTRIBUTE;
123 }
124
125 /**
126 * Implements payload_t's get_next_type function.
127 * See #payload_s.get_next_type for description.
128 */
129 static payload_type_t get_next_type(private_transform_attribute_t *this)
130 {
131 return (NO_PAYLOAD);
132 }
133
134 /**
135 * Implements payload_t's get_length function.
136 * See #payload_s.get_length for description.
137 */
138 static size_t get_length(private_transform_attribute_t *this)
139 {
140 if (this->attribute_format == TRUE)
141 {
142 /*Attribute size is only 4 byte */
143 return 4;
144 }
145 return (this->attribute_length_or_value + 4);
146 }
147 /**
148 * Implements transform_attribute_t's set_value function.
149 * See #transform_attribute_s.set_value for description.
150 */
151 static status_t set_value (private_transform_attribute_t *this, chunk_t value)
152 {
153 if (this->attribute_value.ptr != NULL)
154 {
155 /* free existing value */
156 allocator_free(this->attribute_value.ptr);
157 this->attribute_value.ptr = NULL;
158 this->attribute_value.len = 0;
159
160 }
161
162 if (value.len > 2)
163 {
164 this->attribute_value.ptr = allocator_clone_bytes(value.ptr,value.len);
165 if (this->attribute_value.ptr == NULL)
166 {
167 return OUT_OF_RES;
168 }
169 this->attribute_value.len = value.len;
170 this->attribute_length_or_value = value.len;
171 /* attribute has not a fixed length */
172 this->attribute_format = FALSE;
173 }
174 else
175 {
176 memcpy(&(this->attribute_length_or_value),value.ptr,value.len);
177 }
178 return SUCCESS;
179 }
180
181 /**
182 * Implements transform_attribute_t's get_value function.
183 * See #transform_attribute_s.get_value for description.
184 */
185 static chunk_t get_value (private_transform_attribute_t *this)
186 {
187 chunk_t value;
188
189 if (this->attribute_format == FALSE)
190 {
191 value.ptr = this->attribute_value.ptr;
192 value.len = this->attribute_value.len;
193 }
194 else
195 {
196 value.ptr = (void *) &(this->attribute_length_or_value);
197 value.len = 2;
198 }
199
200 return value;
201 }
202
203 /**
204 * Implements transform_attribute_t's set_attribute_type function.
205 * See #transform_attribute_s.set_attribute_type for description.
206 */
207 static status_t set_attribute_type (private_transform_attribute_t *this, u_int16_t type)
208 {
209 this->attribute_type = type & 0x7FFF;
210 return SUCCESS;
211 }
212
213 /**
214 * Implements transform_attribute_t's get_attribute_type function.
215 * See #transform_attribute_s.get_attribute_type for description.
216 */
217 static u_int16_t get_attribute_type (private_transform_attribute_t *this)
218 {
219 return this->attribute_type;
220 }
221
222 /*
223 * Described in header
224 */
225 transform_attribute_t *transform_attribute_create()
226 {
227 private_transform_attribute_t *this = allocator_alloc_thing(private_transform_attribute_t);
228 if (this == NULL)
229 {
230 return NULL;
231 }
232
233 this->public.payload_interface.get_encoding_rules = (status_t (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
234 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
235 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
236 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
237 this->public.payload_interface.destroy = (status_t (*) (payload_t *))destroy;
238 this->public.set_value = (status_t (*) (transform_attribute_t *,chunk_t value)) set_value;
239 this->public.get_value = (chunk_t (*) (transform_attribute_t *)) get_value;
240 this->public.set_attribute_type = (status_t (*) (transform_attribute_t *,u_int16_t type)) set_attribute_type;
241 this->public.get_attribute_type = (u_int16_t (*) (transform_attribute_t *)) get_attribute_type;
242 this->public.destroy = (status_t (*) (transform_attribute_t *)) destroy;
243
244 /* set default values of the fields */
245 this->attribute_format = TRUE;
246 this->attribute_type = 0;
247 this->attribute_length_or_value = 0;
248 this->attribute_value.ptr = NULL;
249 this->attribute_value.len = 0;
250
251 return (&(this->public));
252 }
253