updated Doxyfile
[strongswan.git] / src / charon / encoding / payloads / id_payload.c
1 /*
2 * Copyright (C) 2007 Tobias Brunner
3 * Copyright (C) 2005-2006 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 *
6 * Hochschule fuer Technik Rapperswil
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * $Id$
19 */
20
21 #include <stddef.h>
22
23 #include "id_payload.h"
24
25 #include <daemon.h>
26 #include <encoding/payloads/encodings.h>
27
28 typedef struct private_id_payload_t private_id_payload_t;
29
30 /**
31 * Private data of an id_payload_t object.
32 *
33 */
34 struct private_id_payload_t {
35 /**
36 * Public id_payload_t interface.
37 */
38 id_payload_t public;
39
40 /**
41 * one of ID_INITIATOR, ID_RESPONDER
42 */
43 payload_type_t payload_type;
44
45 /**
46 * Next payload type.
47 */
48 u_int8_t next_payload;
49
50 /**
51 * Critical flag.
52 */
53 bool critical;
54
55 /**
56 * Length of this payload.
57 */
58 u_int16_t payload_length;
59
60 /**
61 * Type of the ID Data.
62 */
63 u_int8_t id_type;
64
65 /**
66 * The contained id data value.
67 */
68 chunk_t id_data;
69 };
70
71 /**
72 * Encoding rules to parse or generate a ID payload
73 *
74 * The defined offsets are the positions in a object of type
75 * private_id_payload_t.
76 *
77 */
78 encoding_rule_t id_payload_encodings[] = {
79 /* 1 Byte next payload type, stored in the field next_payload */
80 { U_INT_8, offsetof(private_id_payload_t, next_payload) },
81 /* the critical bit */
82 { FLAG, offsetof(private_id_payload_t, critical) },
83 /* 7 Bit reserved bits, nowhere stored */
84 { RESERVED_BIT, 0 },
85 { RESERVED_BIT, 0 },
86 { RESERVED_BIT, 0 },
87 { RESERVED_BIT, 0 },
88 { RESERVED_BIT, 0 },
89 { RESERVED_BIT, 0 },
90 { RESERVED_BIT, 0 },
91 /* Length of the whole payload*/
92 { PAYLOAD_LENGTH, offsetof(private_id_payload_t, payload_length) },
93 /* 1 Byte ID type*/
94 { U_INT_8, offsetof(private_id_payload_t, id_type) },
95 /* 3 reserved bytes */
96 { RESERVED_BYTE, 0 },
97 { RESERVED_BYTE, 0 },
98 { RESERVED_BYTE, 0 },
99 /* some id data bytes, length is defined in PAYLOAD_LENGTH */
100 { ID_DATA, offsetof(private_id_payload_t, id_data) }
101 };
102
103 /*
104 1 2 3
105 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
106 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
107 ! Next Payload !C! RESERVED ! Payload Length !
108 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
109 ! ID Type ! RESERVED |
110 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
111 ! !
112 ~ Identification Data ~
113 ! !
114 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
115 */
116
117 /**
118 * Implementation of payload_t.verify.
119 */
120 static status_t verify(private_id_payload_t *this)
121 {
122 if ((this->id_type == 0) ||
123 (this->id_type == 4) ||
124 ((this->id_type >= 6) && (this->id_type <= 8)) ||
125 ((this->id_type >= 12) && (this->id_type <= 200)))
126 {
127 /* reserved IDs */
128 DBG1(DBG_ENC, "received ID with reserved type %d", this->id_type);
129 return FAILED;
130 }
131
132 return SUCCESS;
133 }
134
135 /**
136 * Implementation of id_payload_t.get_encoding_rules.
137 */
138 static void get_encoding_rules(private_id_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
139 {
140 *rules = id_payload_encodings;
141 *rule_count = sizeof(id_payload_encodings) / sizeof(encoding_rule_t);
142 }
143
144 /**
145 * Implementation of payload_t.get_type.
146 */
147 static payload_type_t get_payload_type(private_id_payload_t *this)
148 {
149 return this->payload_type;
150 }
151
152 /**
153 * Implementation of payload_t.get_next_type.
154 */
155 static payload_type_t get_next_type(private_id_payload_t *this)
156 {
157 return this->next_payload;
158 }
159
160 /**
161 * Implementation of payload_t.set_next_type.
162 */
163 static void set_next_type(private_id_payload_t *this,payload_type_t type)
164 {
165 this->next_payload = type;
166 }
167
168 /**
169 * Implementation of payload_t.get_length.
170 */
171 static size_t get_length(private_id_payload_t *this)
172 {
173 return this->payload_length;
174 }
175
176 /**
177 * Implementation of id_payload_t.set_type.
178 */
179 static void set_id_type (private_id_payload_t *this, id_type_t type)
180 {
181 this->id_type = type;
182 }
183
184 /**
185 * Implementation of id_payload_t.get_id_type.
186 */
187 static id_type_t get_id_type (private_id_payload_t *this)
188 {
189 return (this->id_type);
190 }
191
192 /**
193 * Implementation of id_payload_t.set_data.
194 */
195 static void set_data (private_id_payload_t *this, chunk_t data)
196 {
197 if (this->id_data.ptr != NULL)
198 {
199 chunk_free(&(this->id_data));
200 }
201 this->id_data.ptr = clalloc(data.ptr,data.len);
202 this->id_data.len = data.len;
203 this->payload_length = ID_PAYLOAD_HEADER_LENGTH + this->id_data.len;
204 }
205
206
207 /**
208 * Implementation of id_payload_t.get_data_clone.
209 */
210 static chunk_t get_data (private_id_payload_t *this)
211 {
212 return (this->id_data);
213 }
214
215 /**
216 * Implementation of id_payload_t.get_data_clone.
217 */
218 static chunk_t get_data_clone (private_id_payload_t *this)
219 {
220 chunk_t cloned_data;
221 if (this->id_data.ptr == NULL)
222 {
223 return (this->id_data);
224 }
225 cloned_data.ptr = clalloc(this->id_data.ptr,this->id_data.len);
226 cloned_data.len = this->id_data.len;
227 return cloned_data;
228 }
229
230 /**
231 * Implementation of id_payload_t.get_identification.
232 */
233 static identification_t *get_identification (private_id_payload_t *this)
234 {
235 return identification_create_from_encoding(this->id_type,this->id_data);
236 }
237
238 /**
239 * Implementation of payload_t.destroy and id_payload_t.destroy.
240 */
241 static void destroy(private_id_payload_t *this)
242 {
243 if (this->id_data.ptr != NULL)
244 {
245 chunk_free(&(this->id_data));
246 }
247 free(this);
248 }
249
250 /*
251 * Described in header.
252 */
253 id_payload_t *id_payload_create(payload_type_t payload_type)
254 {
255 private_id_payload_t *this = malloc_thing(private_id_payload_t);
256
257 /* interface functions */
258 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
259 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
260 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
261 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
262 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
263 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_payload_type;
264 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
265
266 /* public functions */
267 this->public.destroy = (void (*) (id_payload_t *)) destroy;
268 this->public.set_id_type = (void (*) (id_payload_t *,id_type_t)) set_id_type;
269 this->public.get_id_type = (id_type_t (*) (id_payload_t *)) get_id_type;
270 this->public.set_data = (void (*) (id_payload_t *,chunk_t)) set_data;
271 this->public.get_data = (chunk_t (*) (id_payload_t *)) get_data;
272 this->public.get_data_clone = (chunk_t (*) (id_payload_t *)) get_data_clone;
273
274 this->public.get_identification = (identification_t * (*) (id_payload_t *this)) get_identification;
275
276 /* private variables */
277 this->critical = FALSE;
278 this->next_payload = NO_PAYLOAD;
279 this->payload_length =ID_PAYLOAD_HEADER_LENGTH;
280 this->id_data = chunk_empty;
281 this->payload_type = payload_type;
282
283 return (&(this->public));
284 }
285
286 /*
287 * Described in header.
288 */
289 id_payload_t *id_payload_create_from_identification(payload_type_t payload_type, identification_t *identification)
290 {
291 id_payload_t *this= id_payload_create(payload_type);
292 this->set_data(this,identification->get_encoding(identification));
293 this->set_id_type(this,identification->get_type(identification));
294 return this;
295 }