]> git.ipfire.org Git - thirdparty/strongswan.git/blob - Source/charon/encoding/payloads/cp_payload.c
- renamed get_block_size of hasher
[thirdparty/strongswan.git] / Source / charon / encoding / payloads / cp_payload.c
1 /**
2 * @file cp_payload.c
3 *
4 * @brief Implementation of cp_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 "cp_payload.h"
26
27 #include <encoding/payloads/encodings.h>
28 #include <utils/linked_list.h>
29
30
31 /**
32 * String mappings for config_type_t.
33 */
34 mapping_t config_type_m[] = {
35 {CFG_REQUEST, "CFG_REQUEST"},
36 {CFG_REPLY, "CFG_REPLY"},
37 {CFG_SET, "CFG_SET"},
38 {CFG_ACK, "CFG_ACK"},
39 {MAPPING_END, NULL}
40 };
41
42
43 typedef struct private_cp_payload_t private_cp_payload_t;
44
45 /**
46 * Private data of an cp_payload_t object.
47 *
48 */
49 struct private_cp_payload_t {
50 /**
51 * Public cp_payload_t interface.
52 */
53 cp_payload_t public;
54
55 /**
56 * Next payload type.
57 */
58 u_int8_t next_payload;
59
60 /**
61 * Critical flag.
62 */
63 bool critical;
64
65 /**
66 * Length of this payload.
67 */
68 u_int16_t payload_length;
69
70 /**
71 * Configuration Attributes in this payload are stored in a linked_list_t.
72 */
73 linked_list_t * attributes;
74
75 /**
76 * Config Type.
77 */
78 u_int8_t config_type;
79
80 /**
81 * @brief Computes the length of this payload.
82 *
83 * @param this calling private_cp_payload_t object
84 */
85 void (*compute_length) (private_cp_payload_t *this);
86 };
87
88 /**
89 * Encoding rules to parse or generate a IKEv2-CP Payload
90 *
91 * The defined offsets are the positions in a object of type
92 * private_cp_payload_t.
93 *
94 */
95 encoding_rule_t cp_payload_encodings[] = {
96 /* 1 Byte next payload type, stored in the field next_payload */
97 { U_INT_8, offsetof(private_cp_payload_t, next_payload) },
98 /* the critical bit */
99 { FLAG, offsetof(private_cp_payload_t, critical) },
100 /* 7 Bit reserved bits, nowhere stored */
101 { RESERVED_BIT, 0 },
102 { RESERVED_BIT, 0 },
103 { RESERVED_BIT, 0 },
104 { RESERVED_BIT, 0 },
105 { RESERVED_BIT, 0 },
106 { RESERVED_BIT, 0 },
107 { RESERVED_BIT, 0 },
108 /* Length of the whole CP payload*/
109 { PAYLOAD_LENGTH, offsetof(private_cp_payload_t, payload_length) },
110 /* Proposals are stored in a proposal substructure,
111 offset points to a linked_list_t pointer */
112 { U_INT_8, offsetof(private_cp_payload_t, config_type) },
113 { RESERVED_BYTE,0 },
114 { RESERVED_BYTE,0 },
115 { RESERVED_BYTE,0 },
116 { CONFIGURATION_ATTRIBUTES, offsetof(private_cp_payload_t, attributes) }
117 };
118
119 /*
120 1 2 3
121 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
122 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
123 ! Next Payload !C! RESERVED ! Payload Length !
124 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
125 ! CFG Type ! RESERVED !
126 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
127 ! !
128 ~ Configuration Attributes ~
129 ! !
130 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
131 */
132
133 /**
134 * Implementation of payload_t.verify.
135 */
136 static status_t verify(private_cp_payload_t *this)
137 {
138 status_t status = SUCCESS;
139 iterator_t *iterator;
140
141 iterator = this->attributes->create_iterator(this->attributes,TRUE);
142
143 while(iterator->has_next(iterator))
144 {
145 configuration_attribute_t *attribute;
146 iterator->current(iterator,(void **)&attribute);
147 status = attribute->payload_interface.verify(&(attribute->payload_interface));
148 if (status != SUCCESS)
149 {
150 break;
151 }
152 }
153
154 iterator->destroy(iterator);
155 return status;
156 }
157
158 /**
159 * Implementation of payload_t.get_encoding_rules.
160 */
161 static void get_encoding_rules(private_cp_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
162 {
163 *rules = cp_payload_encodings;
164 *rule_count = sizeof(cp_payload_encodings) / sizeof(encoding_rule_t);
165 }
166
167 /**
168 * Implementation of payload_t.get_type.
169 */
170 static payload_type_t get_type(private_cp_payload_t *this)
171 {
172 return CONFIGURATION;
173 }
174
175 /**
176 * Implementation of payload_t.get_next_type.
177 */
178 static payload_type_t get_next_type(private_cp_payload_t *this)
179 {
180 return (this->next_payload);
181 }
182
183 /**
184 * Implementation of payload_t.set_next_type.
185 */
186 static void set_next_type(private_cp_payload_t *this,payload_type_t type)
187 {
188 this->next_payload = type;
189 }
190
191 /**
192 * Implementation of payload_t.get_length.
193 */
194 static size_t get_length(private_cp_payload_t *this)
195 {
196 this->compute_length(this);
197 return this->payload_length;
198 }
199
200 /**
201 * Implementation of cp_payload_t.create_configuration_attribute_iterator.
202 */
203 static iterator_t *create_configuration_attribute_iterator (private_cp_payload_t *this,bool forward)
204 {
205 return this->attributes->create_iterator(this->attributes,forward);
206 }
207
208 /**
209 * Implementation of cp_payload_t.add_proposal_substructure.
210 */
211 static void add_configuration_attribute (private_cp_payload_t *this,configuration_attribute_t *attribute)
212 {
213 this->attributes->insert_last(this->attributes,(void *) attribute);
214 this->compute_length(this);
215 }
216
217 /**
218 * Implementation of cp_payload_t.set_config_type.
219 */
220 static void set_config_type (private_cp_payload_t *this,config_type_t config_type)
221 {
222 this->config_type = config_type;
223 }
224
225 /**
226 * Implementation of cp_payload_t.get_config_type.
227 */
228 static config_type_t get_config_type (private_cp_payload_t *this)
229 {
230 return this->config_type;
231 }
232
233 /**
234 * Implementation of private_cp_payload_t.compute_length.
235 */
236 static void compute_length (private_cp_payload_t *this)
237 {
238 iterator_t *iterator;
239 size_t length = CP_PAYLOAD_HEADER_LENGTH;
240 iterator = this->attributes->create_iterator(this->attributes,TRUE);
241 while (iterator->has_next(iterator))
242 {
243 payload_t *current_attribute;
244 iterator->current(iterator,(void **) &current_attribute);
245 length += current_attribute->get_length(current_attribute);
246 }
247 iterator->destroy(iterator);
248
249 this->payload_length = length;
250 }
251
252 /**
253 * Implementation of payload_t.destroy and cp_payload_t.destroy.
254 */
255 static status_t destroy(private_cp_payload_t *this)
256 {
257 /* all attributes are getting destroyed */
258 while (this->attributes->get_count(this->attributes) > 0)
259 {
260 configuration_attribute_t *current_attribute;
261 this->attributes->remove_last(this->attributes,(void **)&current_attribute);
262 current_attribute->destroy(current_attribute);
263 }
264 this->attributes->destroy(this->attributes);
265
266 free(this);
267
268 return SUCCESS;
269 }
270
271 /*
272 * Described in header.
273 */
274 cp_payload_t *cp_payload_create()
275 {
276 private_cp_payload_t *this = malloc_thing(private_cp_payload_t);
277
278 /* public interface */
279 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
280 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
281 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
282 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
283 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
284 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
285 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
286
287 /* public functions */
288 this->public.create_configuration_attribute_iterator = (iterator_t* (*) (cp_payload_t *,bool)) create_configuration_attribute_iterator;
289 this->public.add_configuration_attribute = (void (*) (cp_payload_t *,configuration_attribute_t *)) add_configuration_attribute;
290 this->public.set_config_type = (void (*) (cp_payload_t *, config_type_t)) set_config_type;
291 this->public.get_config_type = (config_type_t (*) (cp_payload_t *)) get_config_type;
292 this->public.destroy = (void (*) (cp_payload_t *)) destroy;
293
294
295 /* private functions */
296 this->compute_length = compute_length;
297
298 /* set default values of the fields */
299 this->critical = FALSE;
300 this->next_payload = NO_PAYLOAD;
301 this->payload_length = CP_PAYLOAD_HEADER_LENGTH;
302
303 this->attributes = linked_list_create();
304 return (&(this->public));
305 }