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