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