282538ba72c1bf60300b59f7e1c8f9f84260e239
[strongswan.git] / Source / charon / payloads / proposal_substructure.c
1 /**
2 * @file proposal_substructure.h
3 *
4 * @brief Declaration of the class proposal_substructure_t.
5 *
6 * An object of this type represents an IKEv2 PROPOSAL Substructure and contains transforms.
7 *
8 */
9
10 /*
11 * Copyright (C) 2005 Jan Hutter, Martin Willi
12 * Hochschule fuer Technik Rapperswil
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
23 */
24
25 /* offsetof macro */
26 #include <stddef.h>
27
28 #include "proposal_substructure.h"
29
30 #include "encodings.h"
31 #include "transform_substructure.h"
32 #include "../types.h"
33 #include "../utils/allocator.h"
34 #include "../utils/linked_list.h"
35
36 /**
37 * Private data of an proposal_substructure_t' Object
38 *
39 */
40 typedef struct private_proposal_substructure_s private_proposal_substructure_t;
41
42 struct private_proposal_substructure_s {
43 /**
44 * public proposal_substructure_t interface
45 */
46 proposal_substructure_t public;
47
48 /**
49 * next payload type
50 */
51 u_int8_t next_payload;
52
53
54 /**
55 * Length of this payload
56 */
57 u_int16_t proposal_length;
58
59
60 /**
61 * Proposal number
62 */
63 u_int8_t proposal_number;
64
65 /**
66 * Protocol ID
67 */
68 u_int8_t protocol_id;
69
70 /**
71 * SPI size of the following SPI
72 */
73 u_int8_t spi_size;
74
75 /**
76 * Number of transforms
77 */
78 u_int8_t transforms_count;
79
80 /**
81 * SPI is stored as chunk
82 */
83 chunk_t spi;
84
85 /**
86 * Transforms are stored in a linked_list_t
87 */
88 linked_list_t * transforms;
89 };
90
91 /**
92 * Encoding rules to parse or generate a Proposal substructure
93 *
94 * The defined offsets are the positions in a object of type
95 * private_proposal_substructure_t.
96 *
97 */
98 encoding_rule_t proposal_substructure_encodings[] = {
99 /* 1 Byte next payload type, stored in the field next_payload */
100 { U_INT_8, offsetof(private_proposal_substructure_t, next_payload) },
101 /* Reserved Byte is skipped */
102 { RESERVED_BYTE, 0 },
103 /* Length of the whole proposal substructure payload*/
104 { PAYLOAD_LENGTH, offsetof(private_proposal_substructure_t, proposal_length) },
105 /* proposal number is a number of 8 bit */
106 { U_INT_8, offsetof(private_proposal_substructure_t, proposal_number) },
107 /* protocol ID is a number of 8 bit */
108 { U_INT_8, offsetof(private_proposal_substructure_t, protocol_id) },
109 /* SPI Size has its own type */
110 { SPI_SIZE, offsetof(private_proposal_substructure_t, spi_size) },
111 /* Number of transforms is a number of 8 bit */
112 { U_INT_8, offsetof(private_proposal_substructure_t, transforms_count) },
113 /* SPI is a chunk of variable size*/
114 { SPI, offsetof(private_proposal_substructure_t, spi) },
115 /* Transforms are stored in a transform substructure,
116 offset points to a linked_list_t pointer */
117 { TRANSFORMS, offsetof(private_proposal_substructure_t, transforms) }
118 };
119
120 /**
121 * Implements payload_t's and proposal_substructure_t's destroy function.
122 * See #payload_s.destroy or proposal_substructure_s.destroy for description.
123 */
124 static status_t destroy(private_proposal_substructure_t *this)
125 {
126 /* all proposals are getting destroyed */
127 while (this->transforms->get_count(this->transforms) > 0)
128 {
129 transform_substructure_t *current_transform;
130 if (this->transforms->remove_last(this->transforms,(void **)&current_transform) != SUCCESS)
131 {
132 break;
133 }
134 current_transform->destroy(current_transform);
135 }
136 this->transforms->destroy(this->transforms);
137
138 if (this->spi.ptr != NULL)
139 {
140 allocator_free(this->spi.ptr);
141 }
142
143 allocator_free(this);
144
145 return SUCCESS;
146 }
147
148 /**
149 * Implements payload_t's get_encoding_rules function.
150 * See #payload_s.get_encoding_rules for description.
151 */
152 static status_t get_encoding_rules(private_proposal_substructure_t *this, encoding_rule_t **rules, size_t *rule_count)
153 {
154 *rules = proposal_substructure_encodings;
155 *rule_count = sizeof(proposal_substructure_encodings) / sizeof(encoding_rule_t);
156
157 return SUCCESS;
158 }
159
160 /**
161 * Implements payload_t's get_type function.
162 * See #payload_s.get_type for description.
163 */
164 static payload_type_t get_type(private_proposal_substructure_t *this)
165 {
166 return PROPOSAL_SUBSTRUCTURE;
167 }
168
169 /**
170 * Implements payload_t's get_next_type function.
171 * See #payload_s.get_next_type for description.
172 */
173 static payload_type_t get_next_type(private_proposal_substructure_t *this)
174 {
175 return (this->next_payload);
176 }
177
178 /**
179 * Implements payload_t's get_length function.
180 * See #payload_s.get_length for description.
181 */
182 static size_t get_length(private_proposal_substructure_t *this)
183 {
184 return this->proposal_length;
185 }
186
187 /**
188 * Implements proposal_substructure_t's create_transform_substructure_iterator function.
189 * See #proposal_substructure_s.create_transform_substructure_iterator for description.
190 */
191 static status_t create_transform_substructure_iterator (private_proposal_substructure_t *this,linked_list_iterator_t **iterator,bool forward)
192 {
193 return (this->transforms->create_iterator(this->transforms,iterator,forward));
194 }
195
196 /**
197 * Implements proposal_substructure_t's add_transform_substructure function.
198 * See #proposal_substructure_s.add_transform_substructure for description.
199 */
200 static status_t add_transform_substructure (private_proposal_substructure_t *this,transform_substructure_t *transform)
201 {
202 return (this->transforms->insert_last(this->transforms,(void *) transform));
203 }
204
205 /*
206 * Described in header
207 */
208 proposal_substructure_t *proposal_substructure_create()
209 {
210 private_proposal_substructure_t *this = allocator_alloc_thing(private_proposal_substructure_t);
211 if (this == NULL)
212 {
213 return NULL;
214 }
215
216 this->public.payload_interface.get_encoding_rules = (status_t (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
217 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
218 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
219 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
220 this->public.payload_interface.destroy = (status_t (*) (payload_t *))destroy;
221 this->public.create_transform_substructure_iterator = (status_t (*) (proposal_substructure_t *,linked_list_iterator_t **,bool)) create_transform_substructure_iterator;
222 this->public.add_transform_substructure = (status_t (*) (proposal_substructure_t *,transform_substructure_t *)) add_transform_substructure;
223 this->public.destroy = (status_t (*) (proposal_substructure_t *)) destroy;
224
225 /* set default values of the fields */
226 this->next_payload = NO_PAYLOAD;
227 this->proposal_length = 0;
228 this->proposal_number = 0;
229 this->protocol_id = 0;
230 this->transforms_count = 0;
231 this->spi_size = 0;
232 this->spi.ptr = NULL;
233 this->spi.len = 0;
234
235 this->transforms = linked_list_create();
236
237 if (this->transforms == NULL)
238 {
239 allocator_free(this);
240 return NULL;
241 }
242 return (&(this->public));
243 }