- fixed bug in memory reallocation
[strongswan.git] / Source / charon / generator.c
1 /**
2 * @file generator.c
3 *
4 * @brief Generic generator class used to generate IKEv2-header and payloads.
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 <arpa/inet.h>
26
27 #include "allocator.h"
28 #include "types.h"
29 #include "generator.h"
30
31
32 /**
33 * Uused for geneartor operations
34 */
35 typedef struct generator_infos_s generator_infos_t;
36
37 struct generator_infos_s {
38
39 /**
40 * Buffer used to generate to
41 */
42 u_int8_t *buffer;
43
44 /**
45 * current write position in buffer (one byte alligned)
46 */
47 u_int8_t *out_position;
48
49 /**
50 * position of last byte in buffer
51 */
52 u_int8_t *roof_position;
53
54 /**
55 * Current bit writing to
56 */
57 size_t current_bit;
58
59 /**
60 * Associated data struct to read informations from
61 */
62 void * data_struct;
63 /**
64 * @brief Destroys a generator_infos_t object
65 *
66 * @param generator_infos_t generator_infos_t object
67 * @return SUCCESSFUL if succeeded, FAILED otherwise
68 */
69 status_t (*destroy) (generator_infos_t *this);
70
71 /**
72 * Checks if enough space is available in buffer and if not,
73 * the buffer size is increased until at least the asked amount of space
74 * is available
75 *
76 * @param bits number of bits to make at leas available in buffer
77 * @param generator_infos_t generator_infos_t object
78 * @return SUCCESSFUL if succeeded, OUT_OF_RES otherwise
79 */
80 status_t (*make_space_available) (generator_infos_t *this,size_t bits);
81
82 status_t (*write_bytes_to_buffer) (generator_infos_t *this,void * bytes,size_t number_of_bytes);
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 while ((((this->roof_position - this->out_position) * 8) - this->current_bit) < bits)
93 {
94 size_t old_buffer_size = ((this->roof_position) - ( this->buffer));
95 size_t new_buffer_size = old_buffer_size + GENERATOR_DATA_BUFFER_INCREASE_VALUE;
96 size_t out_position_offset = ((this->out_position) - (this->buffer));
97 u_int8_t *new_buffer;
98
99 new_buffer = allocator_realloc(this->buffer,new_buffer_size);
100 if (new_buffer == NULL)
101 {
102 return OUT_OF_RES;
103 }
104
105 this->buffer = new_buffer;
106
107 this->out_position = (this->buffer + out_position_offset);
108 this->roof_position = (this->buffer + new_buffer_size);
109
110 }
111
112 return SUCCESS;
113 }
114
115 static status_t generator_info_write_bytes_to_buffer (generator_infos_t *this,void * bytes,size_t number_of_bytes)
116 {
117 u_int8_t *read_position = (u_int8_t *) bytes;
118 int i;
119 status_t status;
120
121 status = this->make_space_available(this,number_of_bytes * 8);
122
123 if (status != SUCCESS)
124 {
125 return status;
126 }
127
128 for (i = 0; i < number_of_bytes; i++)
129 {
130 *(this->out_position) = *(read_position);
131 read_position++;
132 this->out_position++;
133 }
134 return status;
135 }
136
137 static status_t generator_infos_write_chunk (generator_infos_t *this,chunk_t *data)
138 {
139 size_t data_length = this->out_position - this->buffer;
140 if (this->current_bit > 0)
141 data_length++;
142 data->ptr = allocator_alloc(data_length);
143 if (data->ptr == NULL)
144 {
145 data->len = 0;
146 return OUT_OF_RES;
147 }
148 memcpy(data->ptr,this->buffer,data_length);
149 data->len = data_length;
150 return SUCCESS;
151 }
152
153
154 static status_t generator_infos_destroy (generator_infos_t *this)
155 {
156 if (this == NULL)
157 {
158 return FAILED;
159 }
160 allocator_free(this->buffer);
161 allocator_free(this);
162 return SUCCESS;
163 }
164
165 /**
166 * Creates a generator_infos_t-object holding necessary informations
167 * for generating (buffer, data_struct, etc)
168 *
169 * @param data_struct where to read the data out
170 */
171 generator_infos_t * generator_infos_create(void *data_struct)
172 {
173 generator_infos_t *this = allocator_alloc_thing(generator_infos_t);
174
175 if (this == NULL)
176 {
177 return NULL;
178 }
179
180 /* object methods */
181 this->destroy = generator_infos_destroy;
182 this->make_space_available = generator_info_make_space_available;
183 this->write_chunk = generator_infos_write_chunk;
184 this->write_bytes_to_buffer = generator_info_write_bytes_to_buffer;
185
186 /* allocate memory for buffer */
187 this->buffer = allocator_alloc(GENERATOR_DATA_BUFFER_SIZE);
188 if (this->buffer == NULL)
189 {
190 allocator_free(this);
191 return NULL;
192 }
193
194 /* set private data */
195 this->out_position = this->buffer;
196 this->roof_position = this->buffer + GENERATOR_DATA_BUFFER_SIZE;
197 this->data_struct = data_struct;
198 this->current_bit = 0;
199 return (this);
200 }
201
202
203
204 /**
205 * Private data of a generator_t object
206 */
207 typedef struct private_generator_s private_generator_t;
208
209 struct private_generator_s {
210 /**
211 * Public part of a generator object
212 */
213 generator_t public;
214
215 /* private functions and fields */
216
217 /**
218 * Generates a chunk_t with specific encoding rules
219 *
220 * items are bytewhise written
221 *
222 * @param this private_generator_t-object
223 * @param data_struct data_struct to read data from
224 * @param encoding_rules pointer to first encoding_rule of encoding rules array
225 * @param encoding_rules_count number of encoding rules in encoding rules array
226 * @param data pointer to chunk where to write the data in
227 *
228 * @return SUCCESS if succeeded,
229 * OUT_OF_RES if out of ressources
230 */
231 status_t (*generate) (private_generator_t *this,void * data_struct,encoding_rule_t *encoding_rules, size_t encoding_rules_count, chunk_t *data);
232
233 /**
234 * TODO
235 */
236 status_t (*generate_u_int_type) (private_generator_t *this,encoding_type_t int_type,u_int32_t offset, generator_infos_t *generator_infos);
237
238 /**
239 * Pointer to the payload informations needed to automatic
240 * generate a specific payload type
241 */
242 payload_info_t **payload_infos;
243 };
244
245 /**
246 * implements private_generator_t's double_buffer function
247 */
248 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)
249 {
250 size_t number_of_bits = 0;
251 status_t status;
252
253
254 switch (int_type)
255 {
256 case U_INT_4:
257 number_of_bits = 4;
258 break;
259 case U_INT_8:
260 number_of_bits = 8;
261 break;
262 case U_INT_16:
263 number_of_bits = 16;
264 break;
265 case U_INT_32:
266 number_of_bits = 32;
267 break;
268 case U_INT_64:
269 number_of_bits = 64;
270 break;
271 default:
272 return FAILED;
273 }
274 if (((number_of_bits % 8) == 0) && (generator_infos->current_bit != 0))
275 {
276 /* current bit has to be zero for values greater then 4 bits */
277 return FAILED;
278 }
279
280 status = generator_infos->make_space_available(generator_infos,number_of_bits);
281
282 if (status != SUCCESS)
283 {
284 return status;
285 }
286
287 switch (int_type)
288 {
289 case U_INT_4:
290 {
291 if (generator_infos->current_bit == 0)
292 {
293 u_int8_t high_val = *((u_int8_t *)(generator_infos->data_struct + offset)) << 4;
294 u_int8_t low_val = *(generator_infos->out_position) & 0x0F;
295
296 *(generator_infos->out_position) = high_val | low_val;
297 /* write position is not changed, just bit position is moved */
298 generator_infos->current_bit = 4;
299 }
300 else if (generator_infos->current_bit == 4)
301 {
302 u_int high_val = *(generator_infos->out_position) & 0xF0;
303 u_int low_val = *((u_int8_t *)(generator_infos->data_struct + offset)) & 0x0F;
304 *(generator_infos->out_position) = high_val | low_val;
305 generator_infos->out_position++;
306 generator_infos->current_bit = 0;
307
308 }
309 else
310 {
311 /* 4 Bit integers must have a 4 bit alignment */
312 return FAILED;
313 };
314 break;
315 }
316
317 case U_INT_8:
318 {
319 *generator_infos->out_position = *((u_int8_t *)(generator_infos->data_struct + offset));
320 generator_infos->out_position++;
321 break;
322
323 }
324 case U_INT_16:
325 {
326 u_int16_t int16_val = htons(*((u_int16_t*)(generator_infos->data_struct + offset)));
327 generator_infos->write_bytes_to_buffer(generator_infos,&int16_val,sizeof(u_int16_t));
328
329 break;
330 }
331 case U_INT_32:
332 {
333 u_int32_t int32_val = htonl(*((u_int32_t*)(generator_infos->data_struct + offset)));
334 generator_infos->write_bytes_to_buffer(generator_infos,&int32_val,sizeof(u_int32_t));
335 break;
336 }
337 case U_INT_64:
338 {
339 u_int32_t int32_val_low = htonl(*((u_int32_t*)(generator_infos->data_struct + offset)));
340 u_int32_t int32_val_high = htonl(*((u_int32_t*)(generator_infos->data_struct + offset) + 1));
341 generator_infos->write_bytes_to_buffer(generator_infos,&int32_val_high,sizeof(u_int32_t));
342 generator_infos->write_bytes_to_buffer(generator_infos,&int32_val_low,sizeof(u_int32_t));
343 break;
344 }
345
346 default:
347 return FAILED;
348
349 }
350
351 return SUCCESS;
352 }
353
354 /**
355 * implements private_generator_t's generate function
356 */
357 static status_t generate (private_generator_t *this,void * data_struct,encoding_rule_t *encoding_rules, size_t encoding_rules_count, chunk_t *data)
358 {
359 int i;
360 status_t status;
361
362
363 generator_infos_t *infos = generator_infos_create(data_struct);
364
365 if (infos == NULL)
366 {
367 return OUT_OF_RES;
368 }
369
370
371 for (i = 0; i < encoding_rules_count;i++)
372 {
373 status = SUCCESS;
374 switch (encoding_rules[i].type)
375 {
376 /* all u int values are generated in generate_u_int_type */
377 case U_INT_4:
378 case U_INT_8:
379 case U_INT_16:
380 case U_INT_32:
381 case U_INT_64:
382 status = this->generate_u_int_type(this,encoding_rules[i].type,encoding_rules[i].offset,infos);
383 break;
384 case RESERVED_BIT:
385 {
386 status = infos->make_space_available(infos,1);
387 u_int8_t reserved_bit = ~(1 << (7 - infos->current_bit));
388
389 *(infos->out_position) = *(infos->out_position) & reserved_bit;
390
391 infos->current_bit++;
392 if (infos->current_bit >= 8)
393 {
394 infos->current_bit = infos->current_bit % 8;
395 infos->out_position++;
396 }
397 break;
398 }
399 case RESERVED_BYTE:
400 {
401 status = infos->make_space_available(infos,8);
402 if ((status != SUCCESS) || (infos->current_bit > 0))
403 {
404 return FAILED;
405 }
406 *(infos->out_position) = 0x00;
407 infos->out_position++;
408 break;
409 }
410 case FLAG:
411 {
412 u_int8_t flag_value = (*((bool *) (infos->data_struct + encoding_rules[i].offset))) ? 1 : 0;
413 u_int8_t flag = (flag_value << (7 - infos->current_bit));
414
415 *(infos->out_position) = *(infos->out_position) | flag;
416
417 infos->current_bit++;
418 if (infos->current_bit >= 8)
419 {
420 infos->current_bit = infos->current_bit % 8;
421 status = infos->make_space_available(infos,8);
422 infos->out_position++;
423 }
424 break;
425 }
426 case LENGTH:
427 /* length is generated like an U_INT_32 */
428 status = this->generate_u_int_type(this,U_INT_32,encoding_rules[i].offset,infos);
429 case SPI_SIZE:
430 /* actually not implemented */
431 default:
432 break;
433 }
434 if (status != SUCCESS)
435 {
436 infos->destroy(infos);
437 return status;
438 }
439 }
440
441
442 status = infos->write_chunk(infos,data);
443 infos->destroy(infos);
444 return status;
445 }
446
447 static status_t generate_payload (private_generator_t *this,payload_type_t payload_type,void * data_struct, chunk_t *data)
448 {
449 int i;
450
451 /* check every payload info for specific type */
452 for (i = 0; this->payload_infos[i] != NULL; i++)
453 {
454 if (this->payload_infos[i]->payload_type == payload_type)
455 {
456 /* found payload informations, generating is done in private function generate() */
457 return (this->generate(this, data_struct,this->payload_infos[i]->ecoding_rules,this->payload_infos[i]->encoding_rules_count,data));
458 }
459 }
460 return NOT_SUPPORTED;
461 }
462
463 /**
464 * Implementation of generator_t's destroy function
465 */
466 static status_t destroy(private_generator_t *this)
467 {
468 if (this == NULL)
469 {
470 return FAILED;
471 }
472
473 allocator_free(this);
474 return SUCCESS;
475 }
476
477 /*
478 * Described in header
479 */
480 generator_t * generator_create(payload_info_t ** payload_infos)
481 {
482 private_generator_t *this;
483
484 if (payload_infos == NULL)
485 {
486 return NULL;
487 }
488
489 this = allocator_alloc_thing(private_generator_t);
490 if (this == NULL)
491 {
492 return NULL;
493 }
494
495 /* initiate public functions */
496 this->public.generate_payload = (status_t(*)(generator_t*, payload_type_t, void *, chunk_t *)) generate_payload;
497 this->public.destroy = (status_t(*)(generator_t*)) destroy;
498
499 /* initiate private functions */
500 this->generate = generate;
501 this->generate_u_int_type = generate_u_int_type;
502
503 /* initiate private variables */
504 this->payload_infos = payload_infos;
505
506 return &(this->public);
507 }