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