4 * @brief Interface of id_payload_t.
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 #include "id_payload.h"
25 #include <encoding/payloads/encodings.h>
26 #include <utils/allocator.h>
29 * String mappings for id_type_t.
31 mapping_t id_type_m
[] = {
32 {ID_IPV4_ADDR
, "ID_IPV4_ADDR"},
34 {ID_RFC822_ADDR
, "ID_RFC822_ADDR"},
35 {ID_IPV6_ADDR
, "ID_IPV6_ADDR"},
36 {ID_DER_ASN1_DN
, "ID_DER_ASN1_DN"},
37 {ID_DER_ASN1_GN
, "ID_DER_ASN1_GN"},
38 {ID_KEY_ID
, "ID_KEY_ID"},
43 typedef struct private_id_payload_t private_id_payload_t
;
46 * Private data of an id_payload_t object.
49 struct private_id_payload_t
{
51 * Public id_payload_t interface.
56 * TRUE if this ID payload is of type IDi, FALSE for IDr
63 u_int8_t next_payload
;
71 * Length of this payload.
73 u_int16_t payload_length
;
76 * Type of the ID Data.
81 * The contained id data value.
86 * @brief Computes the length of this payload.
88 * @param this calling private_id_payload_t object
90 void (*compute_length
) (private_id_payload_t
*this);
94 * Encoding rules to parse or generate a ID payload
96 * The defined offsets are the positions in a object of type
97 * private_id_payload_t.
100 encoding_rule_t id_payload_encodings
[] = {
101 /* 1 Byte next payload type, stored in the field next_payload */
102 { U_INT_8
, offsetof(private_id_payload_t
, next_payload
) },
103 /* the critical bit */
104 { FLAG
, offsetof(private_id_payload_t
, critical
) },
105 /* 7 Bit reserved bits, nowhere stored */
113 /* Length of the whole payload*/
114 { PAYLOAD_LENGTH
, offsetof(private_id_payload_t
, payload_length
) },
116 { U_INT_8
, offsetof(private_id_payload_t
, id_type
) },
117 /* 3 reserved bytes */
118 { RESERVED_BYTE
, 0 },
119 { RESERVED_BYTE
, 0 },
120 { RESERVED_BYTE
, 0 },
121 /* some id data bytes, length is defined in PAYLOAD_LENGTH */
122 { ID_DATA
, offsetof(private_id_payload_t
, id_data
) }
127 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
128 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
129 ! Next Payload !C! RESERVED ! Payload Length !
130 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
131 ! ID Type ! RESERVED |
132 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
134 ~ Identification Data ~
136 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
140 * Implementation of payload_t.verify.
142 static status_t
verify(private_id_payload_t
*this)
146 /* critical bit is set! */
153 * Implementation of id_payload_t.get_encoding_rules.
155 static void get_encoding_rules(private_id_payload_t
*this, encoding_rule_t
**rules
, size_t *rule_count
)
157 *rules
= id_payload_encodings
;
158 *rule_count
= sizeof(id_payload_encodings
) / sizeof(encoding_rule_t
);
162 * Implementation of payload_t.get_type.
164 static payload_type_t
get_payload_type(private_id_payload_t
*this)
166 return ((this->is_initiator
) ? ID_INITIATOR
: ID_RESPONDER
);
170 * Implementation of payload_t.get_next_type.
172 static payload_type_t
get_next_type(private_id_payload_t
*this)
174 return (this->next_payload
);
178 * Implementation of payload_t.set_next_type.
180 static void set_next_type(private_id_payload_t
*this,payload_type_t type
)
182 this->next_payload
= type
;
186 * Implementation of payload_t.get_length.
188 static size_t get_length(private_id_payload_t
*this)
190 this->compute_length(this);
191 return this->payload_length
;
195 * Implementation of id_payload_t.set_type.
197 static void set_id_type (private_id_payload_t
*this, id_type_t type
)
199 this->id_type
= type
;
203 * Implementation of id_payload_t.get_id_type.
205 static id_type_t
get_id_type (private_id_payload_t
*this)
207 return (this->id_type
);
211 * Implementation of id_payload_t.set_data.
213 static void set_data (private_id_payload_t
*this, chunk_t data
)
215 if (this->id_data
.ptr
!= NULL
)
217 allocator_free_chunk(&(this->id_data
));
219 this->id_data
.ptr
= allocator_clone_bytes(data
.ptr
,data
.len
);
220 this->id_data
.len
= data
.len
;
224 * Implementation of id_payload_t.get_data.
226 static chunk_t
get_data (private_id_payload_t
*this)
229 if (this->id_data
.ptr
== NULL
)
231 return (this->id_data
);
233 cloned_data
.ptr
= allocator_clone_bytes(this->id_data
.ptr
,this->id_data
.len
);
234 cloned_data
.len
= this->id_data
.len
;
239 * Implementation of id_payload_t.get_initiator.
241 static bool get_initiator (private_id_payload_t
*this)
243 return (this->is_initiator
);
247 * Implementation of id_payload_t.set_initiator.
249 static void set_initiator (private_id_payload_t
*this,bool is_initiator
)
251 this->is_initiator
= is_initiator
;
255 * Implementation of payload_t.destroy and id_payload_t.destroy.
257 static void destroy(private_id_payload_t
*this)
259 if (this->id_data
.ptr
!= NULL
)
261 allocator_free_chunk(&(this->id_data
));
264 allocator_free(this);
268 * Described in header
270 id_payload_t
*id_payload_create(bool is_initiator
)
272 private_id_payload_t
*this = allocator_alloc_thing(private_id_payload_t
);
274 /* interface functions */
275 this->public.payload_interface
.verify
= (status_t (*) (payload_t
*))verify
;
276 this->public.payload_interface
.get_encoding_rules
= (void (*) (payload_t
*, encoding_rule_t
**, size_t *) ) get_encoding_rules
;
277 this->public.payload_interface
.get_length
= (size_t (*) (payload_t
*)) get_length
;
278 this->public.payload_interface
.get_next_type
= (payload_type_t (*) (payload_t
*)) get_next_type
;
279 this->public.payload_interface
.set_next_type
= (void (*) (payload_t
*,payload_type_t
)) set_next_type
;
280 this->public.payload_interface
.get_type
= (payload_type_t (*) (payload_t
*)) get_payload_type
;
281 this->public.payload_interface
.destroy
= (void (*) (payload_t
*))destroy
;
283 /* public functions */
284 this->public.destroy
= (void (*) (id_payload_t
*)) destroy
;
285 this->public.set_id_type
= (void (*) (id_payload_t
*,id_type_t
)) set_id_type
;
286 this->public.get_id_type
= (id_type_t (*) (id_payload_t
*)) get_id_type
;
287 this->public.set_data
= (void (*) (id_payload_t
*,chunk_t
)) set_data
;
288 this->public.get_data
= (chunk_t (*) (id_payload_t
*)) get_data
;
289 this->public.get_initiator
= (bool (*) (id_payload_t
*)) get_initiator
;
290 this->public.set_initiator
= (void (*) (id_payload_t
*,bool)) set_initiator
;
292 /* private variables */
293 this->critical
= FALSE
;
294 this->next_payload
= NO_PAYLOAD
;
295 this->payload_length
=ID_PAYLOAD_HEADER_LENGTH
;
296 this->id_data
= CHUNK_INITIALIZER
;
297 this->is_initiator
= is_initiator
;
299 return (&(this->public));