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