generator rewritten (no memory allocation test anymore)
[strongswan.git] / Source / charon / encoding / 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 #include <stdio.h>
27
28
29 #include "generator.h"
30
31 #include <types.h>
32 #include <globals.h>
33 #include <utils/allocator.h>
34 #include <utils/linked_list.h>
35 #include <utils/logger_manager.h>
36 #include <encoding/payloads/payload.h>
37 #include <encoding/payloads/proposal_substructure.h>
38 #include <encoding/payloads/transform_substructure.h>
39 #include <encoding/payloads/sa_payload.h>
40 #include <encoding/payloads/ke_payload.h>
41 #include <encoding/payloads/notify_payload.h>
42 #include <encoding/payloads/nonce_payload.h>
43
44
45 typedef struct private_generator_t private_generator_t;
46
47 /**
48 * Private part of a generator_t object.
49 */
50 struct private_generator_t {
51 /**
52 * Public part of a generator_t object.
53 */
54 generator_t public;
55
56 /* private functions and fields */
57
58
59 /**
60 * Generates a U_INT-Field type and writes it to buffer.
61 *
62 * @param this private_generator_t object
63 * @param int_type type of U_INT field (U_INT_4, U_INT_8, etc.)
64 * ATTRIBUTE_TYPE is also generated in this function
65 * @param offset offset of value in data struct
66 * @param generator_contexts generator_contexts_t object where the context is written or read from
67 * @return
68 * - SUCCESS
69 * - FAILED if allignment is wrong
70 */
71 void (*generate_u_int_type) (private_generator_t *this,encoding_type_t int_type,u_int32_t offset);
72
73 /**
74 * Get size of current buffer in bytes.
75 *
76 * @param this private_generator_t object
77 * @return Size of buffer in bytes
78 */
79 size_t (*get_current_buffer_size) (private_generator_t *this);
80
81 /**
82 * Get free space of current buffer in bytes.
83 *
84 * @param this private_generator_t object
85 * @return space in buffer in bytes
86 */
87 size_t (*get_current_buffer_space) (private_generator_t *this);
88
89 /**
90 * Get length of data in buffer (in bytes).
91 *
92 * @param this private_generator_t object
93 * @return length of data in bytes
94 */
95 size_t (*get_current_data_length) (private_generator_t *this);
96
97 /**
98 * Get current offset in buffer (in bytes).
99 *
100 * @param this private_generator_t object
101 * @return offset in bytes
102 */
103 u_int32_t (*get_current_buffer_offset) (private_generator_t *this);
104
105 /**
106 * Generates a RESERVED BIT field or a RESERVED BYTE field and writes
107 * it to the buffer.
108 *
109 * @param this private_generator_t object
110 * @param generator_contexts generator_contexts_t object where the context is written or read from
111 * @param bits number of bits to generate
112 */
113 void (*generate_reserved_field) (private_generator_t *this,int bits);
114
115 /**
116 * Generates a FLAG field
117 *
118 * @param this private_generator_t object
119 * @param generator_contexts generator_contexts_t object where the context is written or read from
120 * @param offset offset of flag value in data struct
121 */
122 void (*generate_flag) (private_generator_t *this,u_int32_t offset);
123
124 /**
125 * Writes the current buffer content into a chunk_t
126 *
127 * Memory of specific chunk_t gets allocated.
128 *
129 * @param this calling private_generator_t object
130 * @param data pointer of chunk_t to write to
131 */
132 void (*write_chunk) (private_generator_t *this,chunk_t *data);
133
134 /**
135 * Generates a bytestream from a chunk_t
136 *
137 * @param this private_generator_t object
138 * @param offset offset of chunk_t value in data struct
139 */
140 void (*generate_from_chunk) (private_generator_t *this,u_int32_t offset);
141
142 /**
143 * Makes sure enough space is available in buffer to store amount of bits.
144 *
145 * If buffer is to small to hold the specific amount of bits it
146 * is increased using reallocation function of allocator.
147 *
148 * @param this calling private_generator_t object
149 * @param bits number of bits to make available in buffer
150 */
151 void (*make_space_available) (private_generator_t *this,size_t bits);
152
153 /**
154 * Writes a specific amount of byte into the buffer.
155 *
156 * If buffer is to small to hold the specific amount of bytes it
157 * is increased.
158 *
159 * @param this calling private_generator_t object
160 * @param bytes pointer to bytes to write
161 * @param number_of_bytes number of bytes to write into buffer
162 */
163 void (*write_bytes_to_buffer) (private_generator_t *this,void * bytes,size_t number_of_bytes);
164
165
166 /**
167 * Writes a specific amount of byte into the buffer at a specific offset.
168 *
169 * @warning buffer size is not check to hold the data if offset is to large.
170 *
171 * @param this calling private_generator_t object
172 * @param bytes pointer to bytes to write
173 * @param number_of_bytes number of bytes to write into buffer
174 * @param offset offset to write the data into
175 */
176 void (*write_bytes_to_buffer_at_offset) (private_generator_t *this,void * bytes,size_t number_of_bytes,u_int32_t offset);
177
178 /**
179 * Buffer used to generate the data into.
180 */
181 u_int8_t *buffer;
182
183 /**
184 * Current write position in buffer (one byte aligned).
185 */
186 u_int8_t *out_position;
187
188 /**
189 * Position of last byte in buffer.
190 */
191 u_int8_t *roof_position;
192
193 /**
194 * Current bit writing to in current byte (between 0 and 7).
195 */
196 size_t current_bit;
197
198 /**
199 * Associated data struct to read informations from.
200 */
201 void * data_struct;
202
203 /*
204 * Last payload length position offset in the buffer
205 */
206 u_int32_t last_payload_length_position_offset;
207
208 /**
209 * Offset of the header length field in the buffer
210 */
211 u_int32_t header_length_position_offset;
212
213 /**
214 * Last SPI size
215 */
216 u_int8_t last_spi_size;
217
218 /*
219 * Attribute format of the last generated transform attribute
220 *
221 * Used to check if a variable value field is used or not for
222 * the transform attribute value.
223 */
224 bool attribute_format;
225
226 /*
227 * Depending on the value of attribute_format this field is used
228 * to hold the length of the transform attribute in bytes
229 */
230 u_int16_t attribute_length;
231
232 /**
233 * Associated Logger
234 */
235 logger_t *logger;
236 };
237
238 /**
239 * Implements private_generator_t's get_current_buffer_size function.
240 * See #private_generator_s.get_current_buffer_size.
241 */
242 static size_t get_current_buffer_size (private_generator_t *this)
243 {
244 return ((this->roof_position) - (this->buffer));
245 }
246
247 /**
248 * Implements private_generator_t's get_current_buffer_space function.
249 * See #private_generator_s.get_current_buffer_space.
250 */
251 static size_t get_current_buffer_space (private_generator_t *this)
252 {
253 /* we know, one byte more */
254 size_t space = (this->roof_position) - (this->out_position);
255 return (space);
256 }
257
258 /**
259 * Implements private_generator_t's get_current_buffer_space function.
260 * See #private_generator_s.get_current_buffer_space.
261 */
262 static size_t get_current_data_length (private_generator_t *this)
263 {
264 return (this->out_position - this->buffer);
265 }
266
267 /**
268 * Implements private_generator_t's get_current_buffer_offset function.
269 * See #private_generator_s.get_current_buffer_offset.
270 */
271 static u_int32_t get_current_buffer_offset (private_generator_t *this)
272 {
273 return (this->out_position - this->buffer);
274 }
275
276
277 /**
278 * Implements private_generator_t's generate_u_int_type function.
279 * See #private_generator_s.generate_u_int_type.
280 */
281 static void generate_u_int_type (private_generator_t *this,encoding_type_t int_type,u_int32_t offset)
282 {
283 size_t number_of_bits = 0;
284
285 /* find out number of bits of each U_INT type to check for enough space
286 in buffer */
287 switch (int_type)
288 {
289 case U_INT_4:
290 number_of_bits = 4;
291 break;
292 case U_INT_8:
293 number_of_bits = 8;
294 break;
295 case U_INT_16:
296 number_of_bits = 16;
297 break;
298 case U_INT_32:
299 number_of_bits = 32;
300 break;
301 case U_INT_64:
302 number_of_bits = 64;
303 break;
304 case ATTRIBUTE_TYPE:
305 number_of_bits = 15;
306 break;
307 case IKE_SPI:
308 number_of_bits = 64;
309 break;
310
311 default:
312 this->logger->log(this->logger, ERROR, "U_INT Type %s is not supported",
313 mapping_find(encoding_type_m,int_type));
314
315 return;
316 }
317 /* U_INT Types of multiple then 8 bits must be aligned */
318 if (((number_of_bits % 8) == 0) && (this->current_bit != 0))
319 {
320 this->logger->log(this->logger, ERROR, "U_INT Type %s is not 8 Bit aligned",
321 mapping_find(encoding_type_m,int_type));
322 /* current bit has to be zero for values multiple of 8 bits */
323 return;
324 }
325
326 /* make sure enough space is available in buffer */
327 this->make_space_available(this,number_of_bits);
328 /* now handle each u int type differently */
329 switch (int_type)
330 {
331 case U_INT_4:
332 {
333 if (this->current_bit == 0)
334 {
335 /* highval of current byte in buffer has to be set to the new value*/
336 u_int8_t high_val = *((u_int8_t *)(this->data_struct + offset)) << 4;
337 /* lowval in buffer is not changed */
338 u_int8_t low_val = *(this->out_position) & 0x0F;
339 /* highval is set, low_val is not changed */
340 *(this->out_position) = high_val | low_val;
341 this->logger->log(this->logger, RAW|MOST, " => 0x%x", *(this->out_position));
342 /* write position is not changed, just bit position is moved */
343 this->current_bit = 4;
344 }
345 else if (this->current_bit == 4)
346 {
347 /* highval in buffer is not changed */
348 u_int high_val = *(this->out_position) & 0xF0;
349 /* lowval of current byte in buffer has to be set to the new value*/
350 u_int low_val = *((u_int8_t *)(this->data_struct + offset)) & 0x0F;
351 *(this->out_position) = high_val | low_val;
352 this->logger->log(this->logger, RAW|MOST, " => 0x%x", *(this->out_position));
353 this->out_position++;
354 this->current_bit = 0;
355
356 }
357 else
358 {
359 this->logger->log(this->logger, ERROR, "U_INT_4 Type is not 4 Bit aligned");
360 /* 4 Bit integers must have a 4 bit alignment */
361 return;
362 };
363 break;
364 }
365 case U_INT_8:
366 {
367 /* 8 bit values are written as they are */
368 *this->out_position = *((u_int8_t *)(this->data_struct + offset));
369 this->logger->log(this->logger, RAW|MOST, " => 0x%x", *(this->out_position));
370 this->out_position++;
371 break;
372
373 }
374 case ATTRIBUTE_TYPE:
375 {
376 /* attribute type must not change first bit uf current byte ! */
377 if (this->current_bit != 1)
378 {
379 this->logger->log(this->logger, ERROR, "ATTRIBUTE FORMAT flag is not set");
380 /* first bit has to be set! */
381 return;
382 }
383 /* get value of attribute format flag */
384 u_int8_t attribute_format_flag = *(this->out_position) & 0x80;
385 /* get attribute type value as 16 bit integer*/
386 u_int16_t int16_val = htons(*((u_int16_t*)(this->data_struct + offset)));
387 /* last bit must be unset */
388 int16_val = int16_val & 0xFF7F;
389
390 int16_val = int16_val | attribute_format_flag;
391 this->logger->log(this->logger, RAW|MOST, " => 0x%x", int16_val);
392 /* write bytes to buffer (set bit is overwritten)*/
393 this->write_bytes_to_buffer(this,&int16_val,sizeof(u_int16_t));
394 this->current_bit = 0;
395 break;
396
397 }
398 case U_INT_16:
399 {
400 u_int16_t int16_val = htons(*((u_int16_t*)(this->data_struct + offset)));
401 this->logger->log_bytes(this->logger, RAW|MOST, " =>", (void*)&int16_val, sizeof(int16_val));
402 this->write_bytes_to_buffer(this,&int16_val,sizeof(u_int16_t));
403 break;
404 }
405 case U_INT_32:
406 {
407 u_int32_t int32_val = htonl(*((u_int32_t*)(this->data_struct + offset)));
408 this->logger->log_bytes(this->logger, RAW|MOST, " =>", (void*)&int32_val, sizeof(int32_val));
409 this->write_bytes_to_buffer(this,&int32_val,sizeof(u_int32_t));
410 break;
411 }
412 case U_INT_64:
413 {
414 /* 64 bit integers are written as two 32 bit integers */
415 u_int32_t int32_val_low = htonl(*((u_int32_t*)(this->data_struct + offset)));
416 u_int32_t int32_val_high = htonl(*((u_int32_t*)(this->data_struct + offset) + 1));
417 this->logger->log_bytes(this->logger, RAW|MOST, " => (low)", (void*)&int32_val_low, sizeof(int32_val_low));
418 this->logger->log_bytes(this->logger, RAW|MOST, " => (high)", (void*)&int32_val_high, sizeof(int32_val_high));
419 /* TODO add support for big endian machines */
420 this->write_bytes_to_buffer(this,&int32_val_high,sizeof(u_int32_t));
421 this->write_bytes_to_buffer(this,&int32_val_low,sizeof(u_int32_t));
422 break;
423 }
424
425 case IKE_SPI:
426 {
427 /* 64 bit are written as they come :-) */
428 this->write_bytes_to_buffer(this,(this->data_struct + offset),sizeof(u_int64_t));
429 this->logger->log_bytes(this->logger, RAW|MOST, " =>", (void*)(this->data_struct + offset), sizeof(u_int64_t));
430 break;
431 }
432 default:
433 {
434 this->logger->log(this->logger, ERROR, "U_INT Type %s is not supported", mapping_find(encoding_type_m,int_type));
435 return;
436 }
437 }
438 }
439
440 /**
441 * Implements private_generator_t's generate_reserved_field function.
442 * See #private_generator_s.generate_reserved_field.
443 */
444 static void generate_reserved_field(private_generator_t *this,int bits)
445 {
446 /* only one bit or 8 bit fields are supported */
447 if ((bits != 1) && (bits != 8))
448 {
449 this->logger->log(this->logger, ERROR, "Reserved field of %d bits cannot be generated", bits);
450 return ;
451 }
452 /* make sure enough space is available in buffer */
453 this->make_space_available(this,bits);
454
455 if (bits == 1)
456 {
457 /* one bit processing */
458 u_int8_t reserved_bit = ~(1 << (7 - this->current_bit));
459 *(this->out_position) = *(this->out_position) & reserved_bit;
460 if (this->current_bit == 0)
461 {
462 /* memory must be zero */
463 *(this->out_position) = 0x00;
464 }
465
466
467 this->current_bit++;
468 if (this->current_bit >= 8)
469 {
470 this->current_bit = this->current_bit % 8;
471 this->out_position++;
472 }
473 }
474 else
475 {
476 /* one byte processing*/
477 if (this->current_bit > 0)
478 {
479 this->logger->log(this->logger, ERROR,
480 "Reserved field cannot be written cause allignement of current bit is %d",
481 this->current_bit);
482 return;
483 }
484 *(this->out_position) = 0x00;
485 this->out_position++;
486 }
487 }
488
489 /**
490 * Implements private_generator_t's generate_flag function.
491 * See #private_generator_s.generate_flag.
492 */
493 static void generate_flag (private_generator_t *this,u_int32_t offset)
494 {
495 /* value of current flag */
496 u_int8_t flag_value;
497 /* position of flag in current byte */
498 u_int8_t flag;
499
500 /* if the value in the data_struct is TRUE, flag_value is set to 1, 0 otherwise */
501 flag_value = (*((bool *) (this->data_struct + offset))) ? 1 : 0;
502 /* get flag position */
503 flag = (flag_value << (7 - this->current_bit));
504
505 /* make sure one bit is available in buffer */
506 this->make_space_available(this,1);
507 if (this->current_bit == 0)
508 {
509 /* memory must be zero */
510 *(this->out_position) = 0x00;
511 }
512
513 *(this->out_position) = *(this->out_position) | flag;
514
515
516 this->logger->log(this->logger, RAW|MOST, " => 0x0%x", *(this->out_position));
517
518 this->current_bit++;
519 if (this->current_bit >= 8)
520 {
521 this->current_bit = this->current_bit % 8;
522 this->out_position++;
523 }
524 }
525
526 /**
527 * Implements private_generator_t's generate_from_chunk function.
528 * See #private_generator_s.generate_from_chunk.
529 */
530 static void generate_from_chunk (private_generator_t *this,u_int32_t offset)
531 {
532 if (this->current_bit != 0)
533 {
534 this->logger->log(this->logger, ERROR, "can not generate a chunk at Bitpos %d", this->current_bit);
535 return ;
536 }
537
538 /* position in buffer */
539 chunk_t *attribute_value = (chunk_t *)(this->data_struct + offset);
540
541 this->logger->log_chunk(this->logger, RAW|MOST, " =>", attribute_value);
542
543 /* use write_bytes_to_buffer function to do the job */
544 this->write_bytes_to_buffer(this,attribute_value->ptr,attribute_value->len);
545 }
546
547 /**
548 * Implements private_generator_t's generator_context_make_space_available function.
549 * See #private_generator_s.generator_context_make_space_available.
550 */
551 static void make_space_available (private_generator_t *this, size_t bits)
552 {
553 while (((this->get_current_buffer_space(this) * 8) - this->current_bit) < bits)
554 {
555 /* must increase buffer */
556 u_int8_t *new_buffer;
557 size_t old_buffer_size = this->get_current_buffer_size(this);
558 size_t new_buffer_size = old_buffer_size + GENERATOR_DATA_BUFFER_INCREASE_VALUE;
559 size_t out_position_offset = ((this->out_position) - (this->buffer));
560
561 this->logger->log(this->logger, CONTROL|MOST, "increased gen buffer from %d to %d byte",
562 old_buffer_size, new_buffer_size);
563
564 /* Reallocate space for new buffer */
565 new_buffer = allocator_realloc(this->buffer,new_buffer_size);
566 if (new_buffer == NULL)
567 {
568 this->logger->log(this->logger, ERROR, "reallocation of gen buffer failed!!!");
569 }
570
571 this->buffer = new_buffer;
572
573 this->out_position = (this->buffer + out_position_offset);
574 this->roof_position = (this->buffer + new_buffer_size);
575 }
576 }
577
578 /**
579 * Implements private_generator_t's write_bytes_to_buffer function.
580 * See #private_generator_s.write_bytes_to_buffer.
581 */
582 static void write_bytes_to_buffer (private_generator_t *this,void * bytes, size_t number_of_bytes)
583 {
584 int i;
585 u_int8_t *read_position = (u_int8_t *) bytes;
586
587 this->make_space_available(this,number_of_bytes * 8);
588
589 for (i = 0; i < number_of_bytes; i++)
590 {
591 *(this->out_position) = *(read_position);
592 read_position++;
593 this->out_position++;
594 }
595 }
596
597 /**
598 * Implements private_generator_t's write_bytes_to_buffer_at_offset function.
599 * See #private_generator_s.write_bytes_to_buffer_at_offset.
600 */
601 static void write_bytes_to_buffer_at_offset (private_generator_t *this,void * bytes,size_t number_of_bytes,u_int32_t offset)
602 {
603 int i;
604 u_int8_t *read_position = (u_int8_t *) bytes;
605 u_int8_t *write_position;
606 u_int32_t free_space_after_offset = (this->get_current_buffer_size(this) - offset);
607
608 /* check first if enough space for new data is available */
609 if (number_of_bytes > free_space_after_offset)
610 {
611 this->make_space_available(this,(number_of_bytes - free_space_after_offset) * 8);
612 }
613
614 write_position = this->buffer + offset;
615 for (i = 0; i < number_of_bytes; i++)
616 {
617 *(write_position) = *(read_position);
618 read_position++;
619 write_position++;
620 }
621
622 }
623
624 /**
625 * Implements generator_t's write_chunk function.
626 * See #generator_s.write_chunk.
627 */
628 static void write_to_chunk (private_generator_t *this,chunk_t *data)
629 {
630 size_t data_length = this->get_current_data_length(this);
631 u_int32_t header_length_field = data_length;
632
633 /* write length into header length field */
634 if (this->header_length_position_offset > 0)
635 {
636 u_int32_t int32_val = htonl(header_length_field);
637 this->write_bytes_to_buffer_at_offset(this,&int32_val,sizeof(u_int32_t),this->header_length_position_offset);
638 }
639
640 if (this->current_bit > 0)
641 data_length++;
642 data->ptr = allocator_alloc(data_length);
643 memcpy(data->ptr,this->buffer,data_length);
644 data->len = data_length;
645
646 this->logger->log_chunk(this->logger, RAW, "generated data of this parser", data);
647 }
648
649 /**
650 * Implements generator_t's generate_payload function.
651 * See #generator_s.generate_payload.
652 */
653 static void generate_payload (private_generator_t *this,payload_t *payload)
654 {
655 int i;
656 this->data_struct = payload;
657 size_t rule_count;
658 encoding_rule_t *rules;
659 payload_type_t payload_type;
660 u_int8_t *payload_start;
661
662 /* get payload type */
663 payload_type = payload->get_type(payload);
664 /* spi size has to get reseted */
665 this->last_spi_size = 0;
666
667 payload_start = this->out_position;
668
669 this->logger->log(this->logger, CONTROL, "generating payload of type %s",
670 mapping_find(payload_type_m,payload_type));
671
672 /* each payload has its own encoding rules */
673 payload->get_encoding_rules(payload,&rules,&rule_count);
674
675 for (i = 0; i < rule_count;i++)
676 {
677 this->logger->log(this->logger, CONTROL|MORE, " generating rule %d %s",
678 i, mapping_find(encoding_type_m,rules[i].type));
679 switch (rules[i].type)
680 {
681 /* all u int values, IKE_SPI and ATTRIBUTE_TYPE are generated in generate_u_int_type */
682 case U_INT_4:
683 case U_INT_8:
684 case U_INT_16:
685 case U_INT_32:
686 case U_INT_64:
687 case IKE_SPI:
688 case ATTRIBUTE_TYPE:
689 {
690 this->generate_u_int_type(this,rules[i].type,rules[i].offset);
691 break;
692 }
693 case RESERVED_BIT:
694 {
695 this->generate_reserved_field(this,1);
696 break;
697 }
698 case RESERVED_BYTE:
699 {
700 this->generate_reserved_field(this,8);
701 break;
702 }
703 case FLAG:
704 {
705 this->generate_flag(this,rules[i].offset);
706 break;
707 }
708 case PAYLOAD_LENGTH:
709 {
710 /* position of payload lenght field is temporary stored */
711 this->last_payload_length_position_offset = this->get_current_buffer_offset(this);
712 /* payload length is generated like an U_INT_16 */
713 this->generate_u_int_type(this,U_INT_16,rules[i].offset);
714 break;
715 }
716 case HEADER_LENGTH:
717 {
718 /* position of header length field is temporary stored */
719 this->header_length_position_offset = this->get_current_buffer_offset(this);
720 /* header length is generated like an U_INT_32 */
721 this->generate_u_int_type(this,U_INT_32,rules[i].offset);
722 break;
723 }
724 case SPI_SIZE:
725 /* spi size is handled as 8 bit unsigned integer */
726 this->generate_u_int_type(this,U_INT_8,rules[i].offset);
727 /* last spi size is temporary stored */
728 this->last_spi_size = *((u_int8_t *)(this->data_struct + rules[i].offset));
729 break;
730 case SPI:
731 {
732 /* the SPI value is generated from chunk */
733 this->generate_from_chunk(this,rules[i].offset);
734 break;
735 }
736 case KEY_EXCHANGE_DATA:
737 {
738 /* the Key Exchange Data value is generated from chunk */
739 this->generate_from_chunk(this,rules[i].offset);
740
741 u_int32_t payload_length_position_offset = this->last_payload_length_position_offset;
742 /* Length of KE_PAYLOAD is calculated */
743 u_int16_t length_of_ke_payload = KE_PAYLOAD_HEADER_LENGTH + ((chunk_t *)(this->data_struct + rules[i].offset))->len;
744
745 u_int16_t int16_val = htons(length_of_ke_payload);
746 this->write_bytes_to_buffer_at_offset(this,&int16_val,sizeof(u_int16_t),payload_length_position_offset);
747 break;
748 }
749 case NOTIFICATION_DATA:
750 {
751 /* the Notification Data value is generated from chunk */
752 this->generate_from_chunk(this,rules[i].offset);
753
754 u_int32_t payload_length_position_offset = this->last_payload_length_position_offset;
755 /* Length of Notification PAYLOAD is calculated */
756 u_int16_t length_of_notify_payload = NOTIFY_PAYLOAD_HEADER_LENGTH + ((chunk_t *)(this->data_struct + rules[i].offset))->len;
757 length_of_notify_payload += this->last_spi_size;
758 u_int16_t int16_val = htons(length_of_notify_payload);
759
760 this->write_bytes_to_buffer_at_offset(this,&int16_val,sizeof(u_int16_t),payload_length_position_offset);
761 break;
762 }
763 case NONCE_DATA:
764 {
765 /* the Nonce Data value is generated from chunk */
766 this->generate_from_chunk(this, rules[i].offset);
767
768 u_int32_t payload_length_position_offset = this->last_payload_length_position_offset;
769 /* Length of nonce PAYLOAD is calculated */
770 u_int16_t length_of_nonce_payload = NONCE_PAYLOAD_HEADER_LENGTH + ((chunk_t *)(this->data_struct + rules[i].offset))->len;
771 u_int16_t int16_val = htons(length_of_nonce_payload);
772
773 this->write_bytes_to_buffer_at_offset(this,&int16_val,sizeof(u_int16_t),payload_length_position_offset);
774 break;
775 }
776 case PROPOSALS:
777 {
778 /* before iterative generate the transforms, store the current payload length position */
779 u_int32_t payload_length_position_offset = this->last_payload_length_position_offset;
780 /* Length of SA_PAYLOAD is calculated */
781 u_int16_t length_of_sa_payload = SA_PAYLOAD_HEADER_LENGTH;
782 u_int16_t int16_val;
783 /* proposals are stored in a linked list and so accessed */
784 linked_list_t *proposals = *((linked_list_t **)(this->data_struct + rules[i].offset));
785
786 iterator_t *iterator;
787 /* create forward iterator */
788 proposals->create_iterator(proposals,&iterator,TRUE);
789 /* every proposal is processed (iterative call )*/
790 while (iterator->has_next(iterator))
791 {
792 payload_t *current_proposal;
793 u_int32_t before_generate_position_offset;
794 u_int32_t after_generate_position_offset;
795
796 iterator->current(iterator,(void **)&current_proposal);
797
798 before_generate_position_offset = this->get_current_buffer_offset(this);
799 this->public.generate_payload(&(this->public),current_proposal);
800 after_generate_position_offset = this->get_current_buffer_offset(this);
801
802 /* increase size of transform */
803 length_of_sa_payload += (after_generate_position_offset - before_generate_position_offset);
804 }
805 iterator->destroy(iterator);
806
807 int16_val = htons(length_of_sa_payload);
808 this->write_bytes_to_buffer_at_offset(this,&int16_val,sizeof(u_int16_t),payload_length_position_offset);
809 break;
810 }
811
812 case TRANSFORMS:
813 {
814 /* before iterative generate the transforms, store the current length position */
815 u_int32_t payload_length_position_offset = this->last_payload_length_position_offset;
816 u_int16_t length_of_proposal = PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH + this->last_spi_size;
817 u_int16_t int16_val;
818 linked_list_t *transforms = *((linked_list_t **)(this->data_struct + rules[i].offset));
819 iterator_t *iterator;
820
821 /* create forward iterator */
822 transforms->create_iterator(transforms,&iterator,TRUE);
823 while (iterator->has_next(iterator))
824 {
825 payload_t *current_transform;
826 u_int32_t before_generate_position_offset;
827 u_int32_t after_generate_position_offset;
828
829 iterator->current(iterator,(void **)&current_transform);
830
831 before_generate_position_offset = this->get_current_buffer_offset(this);
832 this->public.generate_payload(&(this->public),current_transform);
833 after_generate_position_offset = this->get_current_buffer_offset(this);
834
835 /* increase size of transform */
836 length_of_proposal += (after_generate_position_offset - before_generate_position_offset);
837 }
838
839 iterator->destroy(iterator);
840
841 int16_val = htons(length_of_proposal);
842 this->write_bytes_to_buffer_at_offset(this,&int16_val,sizeof(u_int16_t),payload_length_position_offset);
843
844 break;
845 }
846 case TRANSFORM_ATTRIBUTES:
847 {
848 /* before iterative generate the transform attributes, store the current length position */
849 u_int32_t transform_length_position_offset = this->last_payload_length_position_offset;
850
851 u_int16_t length_of_transform = TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH;
852 u_int16_t int16_val;
853 linked_list_t *transform_attributes =*((linked_list_t **)(this->data_struct + rules[i].offset));
854
855 iterator_t *iterator;
856 /* create forward iterator */
857 transform_attributes->create_iterator(transform_attributes,&iterator,TRUE);
858 while (iterator->has_next(iterator))
859 {
860 payload_t *current_attribute;
861 u_int32_t before_generate_position_offset;
862 u_int32_t after_generate_position_offset;
863
864 iterator->current(iterator,(void **)&current_attribute);
865
866 before_generate_position_offset = this->get_current_buffer_offset(this);
867 this->public.generate_payload(&(this->public),current_attribute);
868 after_generate_position_offset = this->get_current_buffer_offset(this);
869
870 /* increase size of transform */
871 length_of_transform += (after_generate_position_offset - before_generate_position_offset);
872 }
873
874 iterator->destroy(iterator);
875
876 int16_val = htons(length_of_transform);
877 this->write_bytes_to_buffer_at_offset(this,&int16_val,sizeof(u_int16_t),transform_length_position_offset);
878
879 break;
880 }
881 case ATTRIBUTE_FORMAT:
882 {
883 this->generate_flag(this,rules[i].offset);
884 /* Attribute format is a flag which is stored in context*/
885 this->attribute_format = *((bool *) (this->data_struct + rules[i].offset));
886 break;
887 }
888
889 case ATTRIBUTE_LENGTH_OR_VALUE:
890 {
891 if (this->attribute_format == FALSE)
892 {
893 this->generate_u_int_type(this,U_INT_16,rules[i].offset);
894 /* this field hold the length of the attribute */
895 this->attribute_length = *((u_int16_t *)(this->data_struct + rules[i].offset));
896 }
897 else
898 {
899 this->generate_u_int_type(this,U_INT_16,rules[i].offset);
900 // status = this->write_bytes_to_buffer(this,(this->data_struct + rules[i].offset),2);
901 }
902 break;
903 }
904 case ATTRIBUTE_VALUE:
905 {
906 if (this->attribute_format == FALSE)
907 {
908 this->logger->log(this->logger, CONTROL|MOST, "attribute value has not fixed size");
909 /* the attribute value is generated */
910 this->generate_from_chunk(this,rules[i].offset);
911 }
912 break;
913 }
914 case ENCRYPTED_DATA:
915 {
916 this->generate_from_chunk(this, rules[i].offset);
917 break;
918 }
919 default:
920 this->logger->log(this->logger, ERROR, "field type %s is not supported",
921 mapping_find(encoding_type_m,rules[i].type));
922 return;
923 }
924 }
925 this->logger->log_bytes(this->logger, RAW|MORE, "generated data for this payload",
926 payload_start, this->out_position-payload_start);
927
928 }
929
930 /**
931 * Implements generator_t's destroy function.
932 * See #generator_s.destroy.
933 */
934 static status_t destroy(private_generator_t *this)
935 {
936 allocator_free(this->buffer);
937 global_logger_manager->destroy_logger(global_logger_manager,this->logger);
938 allocator_free(this);
939 return SUCCESS;
940 }
941
942 /*
943 * Described in header
944 */
945 generator_t * generator_create()
946 {
947 private_generator_t *this;
948
949 this = allocator_alloc_thing(private_generator_t);
950 if (this == NULL)
951 {
952 return NULL;
953 }
954
955 /* initiate public functions */
956 this->public.generate_payload = (void(*)(generator_t*, payload_t *)) generate_payload;
957 this->public.destroy = (void(*)(generator_t*)) destroy;
958 this->public.write_to_chunk = (void (*) (generator_t *,chunk_t *)) write_to_chunk;
959
960
961 /* initiate private functions */
962 this->get_current_buffer_size = get_current_buffer_size;
963 this->get_current_buffer_space = get_current_buffer_space;
964 this->get_current_data_length = get_current_data_length;
965 this->get_current_buffer_offset = get_current_buffer_offset;
966 this->generate_u_int_type = generate_u_int_type;
967 this->generate_reserved_field = generate_reserved_field;
968 this->generate_flag = generate_flag;
969 this->generate_from_chunk = generate_from_chunk;
970 this->make_space_available = make_space_available;
971 this->write_bytes_to_buffer = write_bytes_to_buffer;
972 this->write_bytes_to_buffer_at_offset = write_bytes_to_buffer_at_offset;
973
974
975 /* allocate memory for buffer */
976 this->buffer = allocator_alloc(GENERATOR_DATA_BUFFER_SIZE);
977 if (this->buffer == NULL)
978 {
979 allocator_free(this);
980 return NULL;
981 }
982
983 /* initiate private variables */
984 this->out_position = this->buffer;
985 this->roof_position = this->buffer + GENERATOR_DATA_BUFFER_SIZE;
986 this->data_struct = NULL;
987 this->current_bit = 0;
988 this->last_payload_length_position_offset = 0;
989 this->header_length_position_offset = 0;
990 this->logger = global_logger_manager->create_logger(global_logger_manager,GENERATOR,NULL);
991
992 if (this->logger == NULL)
993 {
994 allocator_free(this->buffer);
995 allocator_free(this);
996 return NULL;
997 }
998 return &(this->public);
999 }