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