04f164a67a971b925b15033117cb669ea44efeac
[strongswan.git] / Source / charon / generator.c
1 /**
2 * @file generator.c
3 *
4 * @brief Generic generator class used to generate IKEv2-Header and Payload
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 <stdlib.h>
24 #include <string.h>
25 #include <freeswan.h>
26 #include <pluto/constants.h>
27 #include <pluto/defs.h>
28
29 #include "allocator.h"
30 #include "types.h"
31 #include "generator.h"
32
33
34 /**
35 * buffer_t: used for geneartor operations
36 */
37 typedef struct generator_infos_s generator_infos_t;
38
39 struct generator_infos_s {
40
41 /**
42 * Buffer used to generate to
43 */
44 u_int8_t *buffer;
45
46 /**
47 * current write position in buffer (one byte alligned)
48 */
49 u_int8_t *out_position;
50
51 /**
52 * position of last byte in buffer
53 */
54 u_int8_t *roof_position;
55
56 /**
57 * Current bit writing to
58 */
59 size_t current_bit;
60
61 /**
62 * Associated data struct to read informations from
63 */
64 void * data_struct;
65 /**
66 * @brief Destroys a generator_infos_t object
67 *
68 * @param generator_infos_t generator_infos_t object
69 * @return SUCCESSFUL if succeeded, FAILED otherwise
70 */
71 status_t (*destroy) (generator_infos_t *this);
72
73 /**
74 * Checks if enough space is available in buffer and if not,
75 * the buffer size is increased until at least the asked amount of space
76 * is available
77 *
78 * @param bits number of bits to make at leas available in buffer
79 * @param generator_infos_t generator_infos_t object
80 * @return SUCCESSFUL if succeeded, OUT_OF_RES otherwise
81 */
82 status_t (*make_space_available) (generator_infos_t *this,size_t bits);
83
84 status_t (*write_chunk) (generator_infos_t *this,chunk_t *data);
85 };
86
87 /**
88 * implements generator_infos_t's increase_buffer function
89 */
90 static status_t generator_info_make_space_available (generator_infos_t *this, size_t bits)
91 {
92 size_t free_bits = ((this->roof_position - this->out_position) * 8) - this->current_bit;
93
94 while (free_bits < bits)
95 {
96 size_t old_buffer_size = ((this->roof_position) - ( this->buffer));
97 size_t new_buffer_size = old_buffer_size + GENERATOR_DATA_BUFFER_INCREASE_VALUE;
98 size_t out_position_offset = ((this->out_position) - (this->buffer));
99 u_int8_t *new_buffer;
100
101 new_buffer = allocator_realloc(this->buffer,new_buffer_size);
102 if (new_buffer == NULL)
103 {
104 return OUT_OF_RES;
105 }
106
107 this->buffer = new_buffer;
108
109 this->out_position = (this->buffer + out_position_offset);
110 this->roof_position = (this->buffer + new_buffer_size);
111 }
112
113 return SUCCESS;
114 }
115
116 static status_t generator_infos_write_chunk (generator_infos_t *this,chunk_t *data)
117 {
118 size_t data_length = this->out_position - this->buffer;
119 if (this->current_bit > 0)
120 data_length++;
121 data->ptr = allocator_alloc(data_length);
122 if (data->ptr == NULL)
123 {
124 data->len = 0;
125 return OUT_OF_RES;
126 }
127 memcpy(data->ptr,this->buffer,data_length);
128 data->len = data_length;
129 return SUCCESS;
130 }
131
132
133 static status_t generator_infos_destroy (generator_infos_t *this)
134 {
135 if (this == NULL)
136 {
137 return FAILED;
138 }
139 allocator_free(this->buffer);
140 allocator_free(this);
141 return SUCCESS;
142 }
143
144 /**
145 * Creates a generator_infos_t-object holding necessary informations
146 * for generating (buffer, data_struct, etc)
147 *
148 * @param data_struct where to read the data out
149 */
150 generator_infos_t * generator_infos_create(void *data_struct)
151 {
152 generator_infos_t *this = allocator_alloc_thing(generator_infos_t);
153
154 if (this == NULL)
155 {
156 return NULL;
157 }
158
159 /* object methods */
160 this->destroy = generator_infos_destroy;
161 this->make_space_available = generator_info_make_space_available;
162 this->write_chunk = generator_infos_write_chunk;
163
164 /* allocate memory for buffer */
165 this->buffer = allocator_alloc(GENERATOR_DATA_BUFFER_SIZE);
166 if (this->buffer == NULL)
167 {
168 allocator_free(this);
169 return NULL;
170 }
171
172 /* set private data */
173 this->out_position = this->buffer;
174 this->roof_position = this->buffer + GENERATOR_DATA_BUFFER_SIZE;
175 this->data_struct = data_struct;
176 this->current_bit = 0;
177 return (this);
178 }
179
180
181
182 /**
183 * Private data of a generator_t object
184 */
185 typedef struct private_generator_s private_generator_t;
186
187 struct private_generator_s {
188 /**
189 * Public part of a generator object
190 */
191 generator_t public;
192
193 /* private functions and fields */
194
195 /**
196 * Generates a chunk_t with specific encoding rules
197 *
198 * items are bytewhise written
199 *
200 * @param this private_generator_t-object
201 * @param data_struct data_struct to read data from
202 * @param encoding_rules pointer to first encoding_rule of encoding rules array
203 * @param encoding_rules_count number of encoding rules in encoding rules array
204 * @param data pointer to chunk where to write the data in
205 *
206 * @return SUCCESS if succeeded,
207 * OUT_OF_RES if out of ressources
208 */
209 status_t (*generate) (private_generator_t *this,void * data_struct,encoding_rule_t *encoding_rules, size_t encoding_rules_count, chunk_t *data);
210
211 /**
212 * TODO
213 */
214 status_t (*generate_u_int_type) (private_generator_t *this,encoding_type_t int_type,u_int32_t offset, generator_infos_t *generator_infos);
215
216 /**
217 * Pointer to the payload informations needed to automatic
218 * generate a specific payload type
219 */
220 payload_info_t **payload_infos;
221 };
222
223 /**
224 * implements private_generator_t's double_buffer function
225 */
226 static status_t generate_u_int_type (private_generator_t *this,encoding_type_t int_type,u_int32_t offset,generator_infos_t *generator_infos)
227 {
228 size_t number_of_bits = 0;
229
230 status_t status;
231
232
233 switch (int_type)
234 {
235 case U_INT_4:
236 number_of_bits = 4;
237 break;
238 case U_INT_8:
239 number_of_bits = 8;
240 break;
241 case U_INT_16:
242 number_of_bits = 16;
243 break;
244 case U_INT_32:
245 number_of_bits = 32;
246 break;
247 case U_INT_64:
248 number_of_bits = 64;
249 break;
250 default:
251 return FAILED;
252 }
253
254 status = generator_infos->make_space_available(generator_infos,number_of_bits);
255
256 if (status != SUCCESS)
257 {
258 return status;
259 }
260
261 /* process 4 byte integer special */
262 if (number_of_bits == 4)
263 {
264 if (generator_infos->current_bit == 0)
265 {
266 *(generator_infos->out_position) = *((u_int8_t *)(generator_infos->data_struct + offset)) << 4;
267 generator_infos->current_bit = 4;
268 }
269 else if (generator_infos->current_bit == 4)
270 {
271 generator_infos->out_position++;
272 generator_infos->current_bit = 0;
273
274 }
275 else
276 {
277 /* 4 Bit integers must have a 4 bit alignment */
278 return FAILED;
279 }
280 }
281 return SUCCESS;
282 }
283
284 /**
285 * implements private_generator_t's generate function
286 */
287 static status_t generate (private_generator_t *this,void * data_struct,encoding_rule_t *encoding_rules, size_t encoding_rules_count, chunk_t *data)
288 {
289 int i;
290 status_t status;
291
292
293 generator_infos_t *infos = generator_infos_create(data_struct);
294
295 if (infos == NULL)
296 {
297 return OUT_OF_RES;
298 }
299
300 for (i = 0; i < encoding_rules_count;i++)
301 {
302 status = SUCCESS;
303 switch (encoding_rules[i].type)
304 {
305 case U_INT_4:
306 case U_INT_8:
307 case U_INT_16:
308 case U_INT_32:
309 case U_INT_64:
310 status = this->generate_u_int_type(this,encoding_rules[i].type,encoding_rules[i].offset,infos);
311 break;
312 case RESERVED_BIT:
313 case RESERVED_BYTE:
314 case FLAG:
315 case LENGTH:
316 case SPI_SIZE:
317 default:
318 break;
319 }
320 if (status != SUCCESS)
321 {
322 infos->destroy(infos);
323 return status;
324 }
325 }
326
327
328 status = infos->write_chunk(infos,data);
329 infos->destroy(infos);
330 return status;
331 }
332
333 static status_t generate_payload (private_generator_t *this,payload_type_t payload_type,void * data_struct, chunk_t *data)
334 {
335 int i;
336
337 /* check every payload info for specific type */
338 for (i = 0; this->payload_infos[i] != NULL; i++)
339 {
340 if (this->payload_infos[i]->payload_type == payload_type)
341 {
342 /* found payload informations, generating is done in private function generate() */
343 return (this->generate(this, data_struct,this->payload_infos[i]->ecoding_rules,this->payload_infos[i]->encoding_rules_count,data));
344 }
345 }
346 return NOT_SUPPORTED;
347 }
348
349 /**
350 * Implementation of generator_t's destroy function
351 */
352 static status_t destroy(private_generator_t *this)
353 {
354 if (this == NULL)
355 {
356 return FAILED;
357 }
358
359 allocator_free(this);
360 return SUCCESS;
361 }
362
363 /*
364 * Described in header
365 */
366 generator_t * generator_create(payload_info_t ** payload_infos)
367 {
368 private_generator_t *this;
369
370 if (payload_infos == NULL)
371 {
372 return NULL;
373 }
374
375 this = allocator_alloc_thing(private_generator_t);
376 if (this == NULL)
377 {
378 return NULL;
379 }
380
381 /* initiate public functions */
382 this->public.generate_payload = (status_t(*)(generator_t*, payload_type_t, void *, chunk_t *)) generate_payload;
383 this->public.destroy = (status_t(*)(generator_t*)) destroy;
384
385 /* initiate private functions */
386 this->generate = generate;
387 this->generate_u_int_type = generate_u_int_type;
388
389 /* initiate private variables */
390 this->payload_infos = payload_infos;
391
392 return &(this->public);
393 }