2 * Copyright (C) 2005-2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 #include "configuration_attribute.h"
22 #include <encoding/payloads/encodings.h>
26 typedef struct private_configuration_attribute_t private_configuration_attribute_t
;
29 * Private data of an configuration_attribute_t object.
31 struct private_configuration_attribute_t
{
34 * Public configuration_attribute_t interface.
36 configuration_attribute_t
public;
44 * Type of the attribute.
49 * Length of the attribute.
54 * Attribute value as chunk.
60 * Encoding rules to parse or generate a configuration attribute.
62 * The defined offsets are the positions in a object of type
63 * private_configuration_attribute_t.
65 encoding_rule_t configuration_attribute_encodings
[] = {
67 { RESERVED_BIT
, offsetof(private_configuration_attribute_t
, reserved
)},
68 /* type of the attribute as 15 bit unsigned integer */
69 { ATTRIBUTE_TYPE
, offsetof(private_configuration_attribute_t
, type
) },
70 /* Length of attribute value */
71 { CONFIGURATION_ATTRIBUTE_LENGTH
, offsetof(private_configuration_attribute_t
, length
) },
72 /* Value of attribute if attribute format flag is zero */
73 { CONFIGURATION_ATTRIBUTE_VALUE
, offsetof(private_configuration_attribute_t
, value
) }
78 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
79 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
80 !R| Attribute Type ! Length |
81 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
85 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
88 METHOD(payload_t
, verify
, status_t
,
89 private_configuration_attribute_t
*this)
93 if (this->length
!= this->value
.len
)
95 DBG1(DBG_ENC
, "invalid attribute length");
101 case INTERNAL_IP4_ADDRESS
:
102 case INTERNAL_IP4_NETMASK
:
103 case INTERNAL_IP4_DNS
:
104 case INTERNAL_IP4_NBNS
:
105 case INTERNAL_ADDRESS_EXPIRY
:
106 case INTERNAL_IP4_DHCP
:
107 if (this->length
!= 0 && this->length
!= 4)
112 case INTERNAL_IP4_SUBNET
:
113 if (this->length
!= 0 && this->length
!= 8)
118 case INTERNAL_IP6_ADDRESS
:
119 case INTERNAL_IP6_SUBNET
:
120 if (this->length
!= 0 && this->length
!= 17)
125 case INTERNAL_IP6_DNS
:
126 case INTERNAL_IP6_NBNS
:
127 case INTERNAL_IP6_DHCP
:
128 if (this->length
!= 0 && this->length
!= 16)
133 case SUPPORTED_ATTRIBUTES
:
134 if (this->length
% 2)
139 case APPLICATION_VERSION
:
140 /* any length acceptable */
143 DBG1(DBG_ENC
, "unknown attribute type %N",
144 configuration_attribute_type_names
, this->type
);
150 DBG1(DBG_ENC
, "invalid attribute length %d for %N",
151 this->length
, configuration_attribute_type_names
, this->type
);
157 METHOD(payload_t
, get_encoding_rules
, void,
158 private_configuration_attribute_t
*this, encoding_rule_t
**rules
,
161 *rules
= configuration_attribute_encodings
;
162 *rule_count
= countof(configuration_attribute_encodings
);
165 METHOD(payload_t
, get_type
, payload_type_t
,
166 private_configuration_attribute_t
*this)
168 return CONFIGURATION_ATTRIBUTE
;
171 METHOD(payload_t
, get_next_type
, payload_type_t
,
172 private_configuration_attribute_t
*this)
177 METHOD(payload_t
, set_next_type
, void,
178 private_configuration_attribute_t
*this, payload_type_t type
)
182 METHOD(payload_t
, get_length
, size_t,
183 private_configuration_attribute_t
*this)
185 return this->value
.len
+ CONFIGURATION_ATTRIBUTE_HEADER_LENGTH
;
188 METHOD(configuration_attribute_t
, get_cattr_type
, configuration_attribute_type_t
,
189 private_configuration_attribute_t
*this)
194 METHOD(configuration_attribute_t
, get_value
, chunk_t
,
195 private_configuration_attribute_t
*this)
200 METHOD2(payload_t
, configuration_attribute_t
, destroy
, void,
201 private_configuration_attribute_t
*this)
203 free(this->value
.ptr
);
208 * Described in header.
210 configuration_attribute_t
*configuration_attribute_create()
212 private_configuration_attribute_t
*this;
216 .payload_interface
= {
218 .get_encoding_rules
= _get_encoding_rules
,
219 .get_length
= _get_length
,
220 .get_next_type
= _get_next_type
,
221 .set_next_type
= _set_next_type
,
222 .get_type
= _get_type
,
225 .get_value
= _get_value
,
226 .get_type
= _get_cattr_type
,
230 return &this->public;
234 * Described in header.
236 configuration_attribute_t
*configuration_attribute_create_value(
237 configuration_attribute_type_t type
, chunk_t value
)
239 private_configuration_attribute_t
*this;
241 this = (private_configuration_attribute_t
*)configuration_attribute_create();
242 this->type
= ((u_int16_t
)type
) & 0x7FFF;
243 this->value
= chunk_clone(value
);
244 this->length
= value
.len
;
246 return &this->public;