updated Doxyfile
[strongswan.git] / src / charon / encoding / payloads / configuration_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 <stddef.h>
20
21 #include "configuration_attribute.h"
22
23 #include <encoding/payloads/encodings.h>
24 #include <library.h>
25 #include <daemon.h>
26
27
28 typedef struct private_configuration_attribute_t private_configuration_attribute_t;
29
30 /**
31 * Private data of an configuration_attribute_t object.
32 *
33 */
34 struct private_configuration_attribute_t {
35 /**
36 * Public configuration_attribute_t interface.
37 */
38 configuration_attribute_t public;
39
40 /**
41 * Type of the attribute.
42 */
43 u_int16_t attribute_type;
44
45 /**
46 * Length of the attribute.
47 */
48 u_int16_t attribute_length;
49
50 /**
51 * Attribute value as chunk.
52 */
53 chunk_t attribute_value;
54 };
55
56 ENUM_BEGIN(configuration_attribute_type_names, INTERNAL_IP4_ADDRESS, INTERNAL_IP6_ADDRESS,
57 "INTERNAL_IP4_ADDRESS",
58 "INTERNAL_IP4_NETMASK",
59 "INTERNAL_IP4_DNS",
60 "INTERNAL_IP4_NBNS",
61 "INTERNAL_ADDRESS_EXPIRY",
62 "INTERNAL_IP4_DHCP",
63 "APPLICATION_VERSION",
64 "INTERNAL_IP6_ADDRESS");
65 ENUM_NEXT(configuration_attribute_type_names, INTERNAL_IP6_DNS, INTERNAL_IP6_SUBNET, INTERNAL_IP6_ADDRESS,
66 "INTERNAL_IP6_DNS",
67 "INTERNAL_IP6_NBNS",
68 "INTERNAL_IP6_DHCP",
69 "INTERNAL_IP4_SUBNET",
70 "SUPPORTED_ATTRIBUTES",
71 "INTERNAL_IP6_SUBNET");
72 ENUM_NEXT(configuration_attribute_type_names, INTERNAL_IP4_SERVER, INTERNAL_IP6_SERVER, INTERNAL_IP6_SUBNET,
73 "INTERNAL_IP4_SERVER",
74 "INTERNAL_IP6_SERVER");
75 ENUM_END(configuration_attribute_type_names, INTERNAL_IP6_SERVER);
76
77 /**
78 * Encoding rules to parse or generate a configuration attribute.
79 *
80 * The defined offsets are the positions in a object of type
81 * private_configuration_attribute_t.
82 *
83 */
84 encoding_rule_t configuration_attribute_encodings[] = {
85
86 { RESERVED_BIT, 0 },
87 /* type of the attribute as 15 bit unsigned integer */
88 { ATTRIBUTE_TYPE, offsetof(private_configuration_attribute_t, attribute_type) },
89 /* Length of attribute value */
90 { CONFIGURATION_ATTRIBUTE_LENGTH, offsetof(private_configuration_attribute_t, attribute_length)},
91 /* Value of attribute if attribute format flag is zero */
92 { CONFIGURATION_ATTRIBUTE_VALUE, offsetof(private_configuration_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 !R| Attribute Type ! Length |
100 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
101 | |
102 ~ Value ~
103 | |
104 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105 */
106
107 /**
108 * Implementation of payload_t.verify.
109 */
110 static status_t verify(private_configuration_attribute_t *this)
111 {
112 bool failed = FALSE;
113
114 if (this->attribute_length != this->attribute_value.len)
115 {
116 DBG1(DBG_ENC, "invalid attribute length");
117 return FAILED;
118 }
119
120 switch (this->attribute_type)
121 {
122 case INTERNAL_IP4_ADDRESS:
123 case INTERNAL_IP4_NETMASK:
124 case INTERNAL_IP4_DNS:
125 case INTERNAL_IP4_NBNS:
126 case INTERNAL_ADDRESS_EXPIRY:
127 case INTERNAL_IP4_DHCP:
128 if (this->attribute_length != 0 && this->attribute_length != 4)
129 {
130 failed = TRUE;
131 }
132 break;
133 case INTERNAL_IP4_SUBNET:
134 if (this->attribute_length != 0 && this->attribute_length != 8)
135 {
136 failed = TRUE;
137 }
138 break;
139 case INTERNAL_IP6_ADDRESS:
140 case INTERNAL_IP6_SUBNET:
141 if (this->attribute_length != 0 && this->attribute_length != 17)
142 {
143 failed = TRUE;
144 }
145 break;
146 case INTERNAL_IP6_DNS:
147 case INTERNAL_IP6_NBNS:
148 case INTERNAL_IP6_DHCP:
149 if (this->attribute_length != 0 && this->attribute_length != 16)
150 {
151 failed = TRUE;
152 }
153 break;
154 case SUPPORTED_ATTRIBUTES:
155 if (this->attribute_length % 2)
156 {
157 failed = TRUE;
158 }
159 break;
160 case APPLICATION_VERSION:
161 /* any length acceptable */
162 break;
163 default:
164 DBG1(DBG_ENC, "unknown attribute type %N",
165 configuration_attribute_type_names, this->attribute_type);
166 break;
167 }
168
169 if (failed)
170 {
171 DBG1(DBG_ENC, "invalid attribute length %d for %N",
172 this->attribute_length, configuration_attribute_type_names,
173 this->attribute_type);
174 return FAILED;
175 }
176 return SUCCESS;
177 }
178
179 /**
180 * Implementation of payload_t.get_encoding_rules.
181 */
182 static void get_encoding_rules(private_configuration_attribute_t *this, encoding_rule_t **rules, size_t *rule_count)
183 {
184 *rules = configuration_attribute_encodings;
185 *rule_count = sizeof(configuration_attribute_encodings) / sizeof(encoding_rule_t);
186 }
187
188 /**
189 * Implementation of payload_t.get_type.
190 */
191 static payload_type_t get_type(private_configuration_attribute_t *this)
192 {
193 return CONFIGURATION_ATTRIBUTE;
194 }
195
196 /**
197 * Implementation of payload_t.get_next_type.
198 */
199 static payload_type_t get_next_type(private_configuration_attribute_t *this)
200 {
201 return (NO_PAYLOAD);
202 }
203
204 /**
205 * Implementation of payload_t.set_next_type.
206 */
207 static void set_next_type(private_configuration_attribute_t *this,payload_type_t type)
208 {
209 }
210
211 /**
212 * Implementation of configuration_attribute_t.get_length.
213 */
214 static size_t get_length(private_configuration_attribute_t *this)
215 {
216 return (this->attribute_value.len + CONFIGURATION_ATTRIBUTE_HEADER_LENGTH);
217 }
218
219 /**
220 * Implementation of configuration_attribute_t.set_value.
221 */
222 static void set_value(private_configuration_attribute_t *this, chunk_t value)
223 {
224 if (this->attribute_value.ptr != NULL)
225 {
226 /* free existing value */
227 chunk_free(&(this->attribute_value));
228 }
229
230 this->attribute_value.ptr = clalloc(value.ptr,value.len);
231 this->attribute_value.len = value.len;
232
233 this->attribute_length = this->attribute_value.len;
234 }
235
236 /**
237 * Implementation of configuration_attribute_t.get_value.
238 */
239 static chunk_t get_value (private_configuration_attribute_t *this)
240 {
241 return this->attribute_value;
242 }
243
244 /**
245 * Implementation of configuration_attribute_t.set_type.
246 */
247 static void set_attribute_type (private_configuration_attribute_t *this, u_int16_t type)
248 {
249 this->attribute_type = type & 0x7FFF;
250 }
251
252 /**
253 * Implementation of configuration_attribute_t.get_type.
254 */
255 static u_int16_t get_attribute_type (private_configuration_attribute_t *this)
256 {
257 return this->attribute_type;
258 }
259
260 /**
261 * Implementation of configuration_attribute_t.get_length.
262 */
263 static u_int16_t get_attribute_length (private_configuration_attribute_t *this)
264 {
265 return this->attribute_length;
266 }
267
268
269 /**
270 * Implementation of configuration_attribute_t.destroy and payload_t.destroy.
271 */
272 static void destroy(private_configuration_attribute_t *this)
273 {
274 if (this->attribute_value.ptr != NULL)
275 {
276 free(this->attribute_value.ptr);
277 }
278 free(this);
279 }
280
281 /*
282 * Described in header.
283 */
284 configuration_attribute_t *configuration_attribute_create()
285 {
286 private_configuration_attribute_t *this = malloc_thing(private_configuration_attribute_t);
287
288 /* payload interface */
289 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
290 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
291 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
292 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
293 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
294 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
295 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
296
297 /* public functions */
298 this->public.set_value = (void (*) (configuration_attribute_t *,chunk_t)) set_value;
299 this->public.get_value = (chunk_t (*) (configuration_attribute_t *)) get_value;
300 this->public.set_type = (void (*) (configuration_attribute_t *,u_int16_t type)) set_attribute_type;
301 this->public.get_type = (u_int16_t (*) (configuration_attribute_t *)) get_attribute_type;
302 this->public.get_length = (u_int16_t (*) (configuration_attribute_t *)) get_attribute_length;
303 this->public.destroy = (void (*) (configuration_attribute_t *)) destroy;
304
305 /* set default values of the fields */
306 this->attribute_type = 0;
307 this->attribute_value = chunk_empty;
308 this->attribute_length = 0;
309
310 return (&(this->public));
311 }