- u int values can be generated: Not fully tested !
[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_bytes_to_buffer) (generator_infos_t *this,void * bytes,size_t number_of_bytes);
85
86 status_t (*write_chunk) (generator_infos_t *this,chunk_t *data);
87 };
88
89 /**
90 * implements generator_infos_t's increase_buffer function
91 */
92 static status_t generator_info_make_space_available (generator_infos_t *this, size_t bits)
93 {
94 size_t free_bits = ((this->roof_position - this->out_position) * 8) - this->current_bit;
95
96 while (free_bits < bits)
97 {
98 size_t old_buffer_size = ((this->roof_position) - ( this->buffer));
99 size_t new_buffer_size = old_buffer_size + GENERATOR_DATA_BUFFER_INCREASE_VALUE;
100 size_t out_position_offset = ((this->out_position) - (this->buffer));
101 u_int8_t *new_buffer;
102
103 new_buffer = allocator_realloc(this->buffer,new_buffer_size);
104 if (new_buffer == NULL)
105 {
106 return OUT_OF_RES;
107 }
108
109 this->buffer = new_buffer;
110
111 this->out_position = (this->buffer + out_position_offset);
112 this->roof_position = (this->buffer + new_buffer_size);
113 }
114
115 return SUCCESS;
116 }
117
118 static status_t generator_info_write_bytes_to_buffer (generator_infos_t *this,void * bytes,size_t number_of_bytes)
119 {
120 u_int8_t *read_position = (u_int8_t *) bytes;
121 int i;
122 status_t status;
123
124 status = this->make_space_available(this,number_of_bytes * 8);
125
126 if (status != SUCCESS)
127 {
128 return status;
129 }
130
131 for (i = 0; i < number_of_bytes; i++)
132 {
133 *(this->out_position) = *(read_position);
134 read_position++;
135 this->out_position++;
136 }
137 return status;
138 }
139
140 static status_t generator_infos_write_chunk (generator_infos_t *this,chunk_t *data)
141 {
142 size_t data_length = this->out_position - this->buffer;
143 if (this->current_bit > 0)
144 data_length++;
145 data->ptr = allocator_alloc(data_length);
146 if (data->ptr == NULL)
147 {
148 data->len = 0;
149 return OUT_OF_RES;
150 }
151 memcpy(data->ptr,this->buffer,data_length);
152 data->len = data_length;
153 return SUCCESS;
154 }
155
156
157 static status_t generator_infos_destroy (generator_infos_t *this)
158 {
159 if (this == NULL)
160 {
161 return FAILED;
162 }
163 allocator_free(this->buffer);
164 allocator_free(this);
165 return SUCCESS;
166 }
167
168 /**
169 * Creates a generator_infos_t-object holding necessary informations
170 * for generating (buffer, data_struct, etc)
171 *
172 * @param data_struct where to read the data out
173 */
174 generator_infos_t * generator_infos_create(void *data_struct)
175 {
176 generator_infos_t *this = allocator_alloc_thing(generator_infos_t);
177
178 if (this == NULL)
179 {
180 return NULL;
181 }
182
183 /* object methods */
184 this->destroy = generator_infos_destroy;
185 this->make_space_available = generator_info_make_space_available;
186 this->write_chunk = generator_infos_write_chunk;
187 this->write_bytes_to_buffer = generator_info_write_bytes_to_buffer;
188
189 /* allocate memory for buffer */
190 this->buffer = allocator_alloc(GENERATOR_DATA_BUFFER_SIZE);
191 if (this->buffer == NULL)
192 {
193 allocator_free(this);
194 return NULL;
195 }
196
197 /* set private data */
198 this->out_position = this->buffer;
199 this->roof_position = this->buffer + GENERATOR_DATA_BUFFER_SIZE;
200 this->data_struct = data_struct;
201 this->current_bit = 0;
202 return (this);
203 }
204
205
206
207 /**
208 * Private data of a generator_t object
209 */
210 typedef struct private_generator_s private_generator_t;
211
212 struct private_generator_s {
213 /**
214 * Public part of a generator object
215 */
216 generator_t public;
217
218 /* private functions and fields */
219
220 /**
221 * Generates a chunk_t with specific encoding rules
222 *
223 * items are bytewhise written
224 *
225 * @param this private_generator_t-object
226 * @param data_struct data_struct to read data from
227 * @param encoding_rules pointer to first encoding_rule of encoding rules array
228 * @param encoding_rules_count number of encoding rules in encoding rules array
229 * @param data pointer to chunk where to write the data in
230 *
231 * @return SUCCESS if succeeded,
232 * OUT_OF_RES if out of ressources
233 */
234 status_t (*generate) (private_generator_t *this,void * data_struct,encoding_rule_t *encoding_rules, size_t encoding_rules_count, chunk_t *data);
235
236 /**
237 * TODO
238 */
239 status_t (*generate_u_int_type) (private_generator_t *this,encoding_type_t int_type,u_int32_t offset, generator_infos_t *generator_infos);
240
241 /**
242 * Pointer to the payload informations needed to automatic
243 * generate a specific payload type
244 */
245 payload_info_t **payload_infos;
246 };
247
248 /**
249 * implements private_generator_t's double_buffer function
250 */
251 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)
252 {
253 size_t number_of_bits = 0;
254 status_t status;
255
256
257 switch (int_type)
258 {
259 case U_INT_4:
260 number_of_bits = 4;
261 break;
262 case U_INT_8:
263 number_of_bits = 8;
264 break;
265 case U_INT_16:
266 number_of_bits = 16;
267 break;
268 case U_INT_32:
269 number_of_bits = 32;
270 break;
271 case U_INT_64:
272 number_of_bits = 64;
273 break;
274 default:
275 return FAILED;
276 }
277 if (((number_of_bits % 8) == 0) && (generator_infos->current_bit != 0))
278 {
279 /* current bit has to be zero for values greater then 4 bits */
280 return FAILED;
281 }
282
283 status = generator_infos->make_space_available(generator_infos,number_of_bits);
284
285 if (status != SUCCESS)
286 {
287 return status;
288 }
289
290 switch (int_type)
291 {
292 case U_INT_4:
293 {
294 if (generator_infos->current_bit == 0)
295 {
296 u_int8_t high_val = *((u_int8_t *)(generator_infos->data_struct + offset)) << 4;
297 u_int8_t low_val = *(generator_infos->out_position) & 0x0F;
298
299 *(generator_infos->out_position) = high_val | low_val;
300 /* write position is not changed, just bit position is moved */
301 generator_infos->current_bit = 4;
302 }
303 else if (generator_infos->current_bit == 4)
304 {
305 u_int high_val = *(generator_infos->out_position) & 0xF0;
306 u_int low_val = *((u_int8_t *)(generator_infos->data_struct + offset)) & 0x0F;
307 *(generator_infos->out_position) = high_val | low_val;
308 generator_infos->out_position++;
309 generator_infos->current_bit = 0;
310
311 }
312 else
313 {
314 /* 4 Bit integers must have a 4 bit alignment */
315 return FAILED;
316 };
317 break;
318 }
319
320 case U_INT_8:
321 {
322 *generator_infos->out_position = *((u_int8_t *)(generator_infos->data_struct + offset));
323 generator_infos->out_position++;
324 break;
325
326 }
327 case U_INT_16:
328 {
329 u_int16_t int16_val = htons(*((u_int16_t*)(generator_infos->data_struct + offset)));
330 generator_infos->write_bytes_to_buffer(generator_infos,&int16_val,sizeof(u_int16_t));
331
332 break;
333 }
334 case U_INT_32:
335 {
336 u_int32_t int32_val = htonl(*((u_int32_t*)(generator_infos->data_struct + offset)));
337 generator_infos->write_bytes_to_buffer(generator_infos,&int32_val,sizeof(u_int32_t));
338 break;
339 }
340 case U_INT_64:
341 {
342 u_int32_t int32_val_low = htonl(*((u_int32_t*)(generator_infos->data_struct + offset)));
343 u_int32_t int32_val_high = htonl(*((u_int32_t*)(generator_infos->data_struct + offset) + 1));
344 generator_infos->write_bytes_to_buffer(generator_infos,&int32_val_high,sizeof(u_int32_t));
345 generator_infos->write_bytes_to_buffer(generator_infos,&int32_val_low,sizeof(u_int32_t));
346 break;
347 }
348
349 default:
350 return FAILED;
351
352 }
353
354
355
356
357 return SUCCESS;
358 }
359
360 /**
361 * implements private_generator_t's generate function
362 */
363 static status_t generate (private_generator_t *this,void * data_struct,encoding_rule_t *encoding_rules, size_t encoding_rules_count, chunk_t *data)
364 {
365 int i;
366 status_t status;
367
368
369 generator_infos_t *infos = generator_infos_create(data_struct);
370
371 if (infos == NULL)
372 {
373 return OUT_OF_RES;
374 }
375
376
377 for (i = 0; i < encoding_rules_count;i++)
378 {
379 status = SUCCESS;
380 switch (encoding_rules[i].type)
381 {
382 case U_INT_4:
383 case U_INT_8:
384 case U_INT_16:
385 case U_INT_32:
386 case U_INT_64:
387 status = this->generate_u_int_type(this,encoding_rules[i].type,encoding_rules[i].offset,infos);
388 break;
389 case RESERVED_BIT:
390 case RESERVED_BYTE:
391 case FLAG:
392 case LENGTH:
393 case SPI_SIZE:
394 default:
395 break;
396 }
397 if (status != SUCCESS)
398 {
399 infos->destroy(infos);
400 return status;
401 }
402 }
403
404
405 status = infos->write_chunk(infos,data);
406 infos->destroy(infos);
407 return status;
408 }
409
410 static status_t generate_payload (private_generator_t *this,payload_type_t payload_type,void * data_struct, chunk_t *data)
411 {
412 int i;
413
414 /* check every payload info for specific type */
415 for (i = 0; this->payload_infos[i] != NULL; i++)
416 {
417 if (this->payload_infos[i]->payload_type == payload_type)
418 {
419 /* found payload informations, generating is done in private function generate() */
420 return (this->generate(this, data_struct,this->payload_infos[i]->ecoding_rules,this->payload_infos[i]->encoding_rules_count,data));
421 }
422 }
423 return NOT_SUPPORTED;
424 }
425
426 /**
427 * Implementation of generator_t's destroy function
428 */
429 static status_t destroy(private_generator_t *this)
430 {
431 if (this == NULL)
432 {
433 return FAILED;
434 }
435
436 allocator_free(this);
437 return SUCCESS;
438 }
439
440 /*
441 * Described in header
442 */
443 generator_t * generator_create(payload_info_t ** payload_infos)
444 {
445 private_generator_t *this;
446
447 if (payload_infos == NULL)
448 {
449 return NULL;
450 }
451
452 this = allocator_alloc_thing(private_generator_t);
453 if (this == NULL)
454 {
455 return NULL;
456 }
457
458 /* initiate public functions */
459 this->public.generate_payload = (status_t(*)(generator_t*, payload_type_t, void *, chunk_t *)) generate_payload;
460 this->public.destroy = (status_t(*)(generator_t*)) destroy;
461
462 /* initiate private functions */
463 this->generate = generate;
464 this->generate_u_int_type = generate_u_int_type;
465
466 /* initiate private variables */
467 this->payload_infos = payload_infos;
468
469 return &(this->public);
470 }