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