4 * @brief Generic generator class used to generate IKEv2-header and payloads.
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
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>.
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
25 #include <arpa/inet.h>
29 #include "generator.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>
45 typedef struct private_generator_t private_generator_t
;
48 * Private part of a generator_t object.
50 struct private_generator_t
{
52 * Public part of a generator_t object.
56 /* private functions and fields */
60 * Generates a U_INT-Field type and writes it to buffer.
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
69 * - FAILED if allignment is wrong
71 void (*generate_u_int_type
) (private_generator_t
*this,encoding_type_t int_type
,u_int32_t offset
);
74 * Get size of current buffer in bytes.
76 * @param this private_generator_t object
77 * @return Size of buffer in bytes
79 size_t (*get_current_buffer_size
) (private_generator_t
*this);
82 * Get free space of current buffer in bytes.
84 * @param this private_generator_t object
85 * @return space in buffer in bytes
87 size_t (*get_current_buffer_space
) (private_generator_t
*this);
90 * Get length of data in buffer (in bytes).
92 * @param this private_generator_t object
93 * @return length of data in bytes
95 size_t (*get_current_data_length
) (private_generator_t
*this);
98 * Get current offset in buffer (in bytes).
100 * @param this private_generator_t object
101 * @return offset in bytes
103 u_int32_t (*get_current_buffer_offset
) (private_generator_t
*this);
106 * Generates a RESERVED BIT field or a RESERVED BYTE field and writes
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
113 void (*generate_reserved_field
) (private_generator_t
*this,int bits
);
116 * Generates a FLAG field
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
122 void (*generate_flag
) (private_generator_t
*this,u_int32_t offset
);
125 * Writes the current buffer content into a chunk_t
127 * Memory of specific chunk_t gets allocated.
129 * @param this calling private_generator_t object
130 * @param data pointer of chunk_t to write to
132 void (*write_chunk
) (private_generator_t
*this,chunk_t
*data
);
135 * Generates a bytestream from a chunk_t
137 * @param this private_generator_t object
138 * @param offset offset of chunk_t value in data struct
140 void (*generate_from_chunk
) (private_generator_t
*this,u_int32_t offset
);
143 * Makes sure enough space is available in buffer to store amount of bits.
145 * If buffer is to small to hold the specific amount of bits it
146 * is increased using reallocation function of allocator.
148 * @param this calling private_generator_t object
149 * @param bits number of bits to make available in buffer
151 void (*make_space_available
) (private_generator_t
*this,size_t bits
);
154 * Writes a specific amount of byte into the buffer.
156 * If buffer is to small to hold the specific amount of bytes it
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
163 void (*write_bytes_to_buffer
) (private_generator_t
*this,void * bytes
,size_t number_of_bytes
);
167 * Writes a specific amount of byte into the buffer at a specific offset.
169 * @warning buffer size is not check to hold the data if offset is to large.
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
176 void (*write_bytes_to_buffer_at_offset
) (private_generator_t
*this,void * bytes
,size_t number_of_bytes
,u_int32_t offset
);
179 * Buffer used to generate the data into.
184 * Current write position in buffer (one byte aligned).
186 u_int8_t
*out_position
;
189 * Position of last byte in buffer.
191 u_int8_t
*roof_position
;
194 * Current bit writing to in current byte (between 0 and 7).
199 * Associated data struct to read informations from.
204 * Last payload length position offset in the buffer
206 u_int32_t last_payload_length_position_offset
;
209 * Offset of the header length field in the buffer
211 u_int32_t header_length_position_offset
;
216 u_int8_t last_spi_size
;
219 * Attribute format of the last generated transform attribute
221 * Used to check if a variable value field is used or not for
222 * the transform attribute value.
224 bool attribute_format
;
227 * Depending on the value of attribute_format this field is used
228 * to hold the length of the transform attribute in bytes
230 u_int16_t attribute_length
;
239 * Implements private_generator_t's get_current_buffer_size function.
240 * See #private_generator_s.get_current_buffer_size.
242 static size_t get_current_buffer_size (private_generator_t
*this)
244 return ((this->roof_position
) - (this->buffer
));
248 * Implements private_generator_t's get_current_buffer_space function.
249 * See #private_generator_s.get_current_buffer_space.
251 static size_t get_current_buffer_space (private_generator_t
*this)
253 /* we know, one byte more */
254 size_t space
= (this->roof_position
) - (this->out_position
);
259 * Implements private_generator_t's get_current_buffer_space function.
260 * See #private_generator_s.get_current_buffer_space.
262 static size_t get_current_data_length (private_generator_t
*this)
264 return (this->out_position
- this->buffer
);
268 * Implements private_generator_t's get_current_buffer_offset function.
269 * See #private_generator_s.get_current_buffer_offset.
271 static u_int32_t
get_current_buffer_offset (private_generator_t
*this)
273 return (this->out_position
- this->buffer
);
278 * Implements private_generator_t's generate_u_int_type function.
279 * See #private_generator_s.generate_u_int_type.
281 static void generate_u_int_type (private_generator_t
*this,encoding_type_t int_type
,u_int32_t offset
)
283 size_t number_of_bits
= 0;
285 /* find out number of bits of each U_INT type to check for enough space
312 this->logger
->log(this->logger
, ERROR
, "U_INT Type %s is not supported",
313 mapping_find(encoding_type_m
,int_type
));
317 /* U_INT Types of multiple then 8 bits must be aligned */
318 if (((number_of_bits
% 8) == 0) && (this->current_bit
!= 0))
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 */
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 */
333 if (this->current_bit
== 0)
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;
345 else if (this->current_bit
== 4)
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;
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 */
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
++;
376 /* attribute type must not change first bit uf current byte ! */
377 if (this->current_bit
!= 1)
379 this->logger
->log(this->logger
, ERROR
, "ATTRIBUTE FORMAT flag is not set");
380 /* first bit has to be set! */
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;
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;
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
));
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
));
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
));
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
));
434 this->logger
->log(this->logger
, ERROR
, "U_INT Type %s is not supported", mapping_find(encoding_type_m
,int_type
));
441 * Implements private_generator_t's generate_reserved_field function.
442 * See #private_generator_s.generate_reserved_field.
444 static void generate_reserved_field(private_generator_t
*this,int bits
)
446 /* only one bit or 8 bit fields are supported */
447 if ((bits
!= 1) && (bits
!= 8))
449 this->logger
->log(this->logger
, ERROR
, "Reserved field of %d bits cannot be generated", bits
);
452 /* make sure enough space is available in buffer */
453 this->make_space_available(this,bits
);
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)
462 /* memory must be zero */
463 *(this->out_position
) = 0x00;
468 if (this->current_bit
>= 8)
470 this->current_bit
= this->current_bit
% 8;
471 this->out_position
++;
476 /* one byte processing*/
477 if (this->current_bit
> 0)
479 this->logger
->log(this->logger
, ERROR
,
480 "Reserved field cannot be written cause allignement of current bit is %d",
484 *(this->out_position
) = 0x00;
485 this->out_position
++;
490 * Implements private_generator_t's generate_flag function.
491 * See #private_generator_s.generate_flag.
493 static void generate_flag (private_generator_t
*this,u_int32_t offset
)
495 /* value of current flag */
497 /* position of flag in current byte */
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
));
505 /* make sure one bit is available in buffer */
506 this->make_space_available(this,1);
507 if (this->current_bit
== 0)
509 /* memory must be zero */
510 *(this->out_position
) = 0x00;
513 *(this->out_position
) = *(this->out_position
) | flag
;
516 this->logger
->log(this->logger
, RAW
|MOST
, " => 0x0%x", *(this->out_position
));
519 if (this->current_bit
>= 8)
521 this->current_bit
= this->current_bit
% 8;
522 this->out_position
++;
527 * Implements private_generator_t's generate_from_chunk function.
528 * See #private_generator_s.generate_from_chunk.
530 static void generate_from_chunk (private_generator_t
*this,u_int32_t offset
)
532 if (this->current_bit
!= 0)
534 this->logger
->log(this->logger
, ERROR
, "can not generate a chunk at Bitpos %d", this->current_bit
);
538 /* position in buffer */
539 chunk_t
*attribute_value
= (chunk_t
*)(this->data_struct
+ offset
);
541 this->logger
->log_chunk(this->logger
, RAW
|MOST
, " =>", attribute_value
);
543 /* use write_bytes_to_buffer function to do the job */
544 this->write_bytes_to_buffer(this,attribute_value
->ptr
,attribute_value
->len
);
548 * Implements private_generator_t's generator_context_make_space_available function.
549 * See #private_generator_s.generator_context_make_space_available.
551 static void make_space_available (private_generator_t
*this, size_t bits
)
553 while (((this->get_current_buffer_space(this) * 8) - this->current_bit
) < bits
)
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
));
561 this->logger
->log(this->logger
, CONTROL
|MOST
, "increased gen buffer from %d to %d byte",
562 old_buffer_size
, new_buffer_size
);
564 /* Reallocate space for new buffer */
565 new_buffer
= allocator_realloc(this->buffer
,new_buffer_size
);
566 if (new_buffer
== NULL
)
568 this->logger
->log(this->logger
, ERROR
, "reallocation of gen buffer failed!!!");
571 this->buffer
= new_buffer
;
573 this->out_position
= (this->buffer
+ out_position_offset
);
574 this->roof_position
= (this->buffer
+ new_buffer_size
);
579 * Implements private_generator_t's write_bytes_to_buffer function.
580 * See #private_generator_s.write_bytes_to_buffer.
582 static void write_bytes_to_buffer (private_generator_t
*this,void * bytes
, size_t number_of_bytes
)
585 u_int8_t
*read_position
= (u_int8_t
*) bytes
;
587 this->make_space_available(this,number_of_bytes
* 8);
589 for (i
= 0; i
< number_of_bytes
; i
++)
591 *(this->out_position
) = *(read_position
);
593 this->out_position
++;
598 * Implements private_generator_t's write_bytes_to_buffer_at_offset function.
599 * See #private_generator_s.write_bytes_to_buffer_at_offset.
601 static void write_bytes_to_buffer_at_offset (private_generator_t
*this,void * bytes
,size_t number_of_bytes
,u_int32_t offset
)
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
);
608 /* check first if enough space for new data is available */
609 if (number_of_bytes
> free_space_after_offset
)
611 this->make_space_available(this,(number_of_bytes
- free_space_after_offset
) * 8);
614 write_position
= this->buffer
+ offset
;
615 for (i
= 0; i
< number_of_bytes
; i
++)
617 *(write_position
) = *(read_position
);
625 * Implements generator_t's write_chunk function.
626 * See #generator_s.write_chunk.
628 static void write_to_chunk (private_generator_t
*this,chunk_t
*data
)
630 size_t data_length
= this->get_current_data_length(this);
631 u_int32_t header_length_field
= data_length
;
633 /* write length into header length field */
634 if (this->header_length_position_offset
> 0)
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
);
640 if (this->current_bit
> 0)
642 data
->ptr
= allocator_alloc(data_length
);
643 memcpy(data
->ptr
,this->buffer
,data_length
);
644 data
->len
= data_length
;
646 this->logger
->log_chunk(this->logger
, RAW
, "generated data of this parser", data
);
650 * Implements generator_t's generate_payload function.
651 * See #generator_s.generate_payload.
653 static void generate_payload (private_generator_t
*this,payload_t
*payload
)
656 this->data_struct
= payload
;
658 encoding_rule_t
*rules
;
659 payload_type_t payload_type
;
660 u_int8_t
*payload_start
;
662 /* get payload type */
663 payload_type
= payload
->get_type(payload
);
664 /* spi size has to get reseted */
665 this->last_spi_size
= 0;
667 payload_start
= this->out_position
;
669 this->logger
->log(this->logger
, CONTROL
, "generating payload of type %s",
670 mapping_find(payload_type_m
,payload_type
));
672 /* each payload has its own encoding rules */
673 payload
->get_encoding_rules(payload
,&rules
,&rule_count
);
675 for (i
= 0; i
< rule_count
;i
++)
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
)
681 /* all u int values, IKE_SPI and ATTRIBUTE_TYPE are generated in generate_u_int_type */
690 this->generate_u_int_type(this,rules
[i
].type
,rules
[i
].offset
);
695 this->generate_reserved_field(this,1);
700 this->generate_reserved_field(this,8);
705 this->generate_flag(this,rules
[i
].offset
);
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
);
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
);
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
));
732 /* the SPI value is generated from chunk */
733 this->generate_from_chunk(this,rules
[i
].offset
);
736 case KEY_EXCHANGE_DATA
:
738 /* the Key Exchange Data value is generated from chunk */
739 this->generate_from_chunk(this,rules
[i
].offset
);
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
;
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
);
749 case NOTIFICATION_DATA
:
751 /* the Notification Data value is generated from chunk */
752 this->generate_from_chunk(this,rules
[i
].offset
);
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
);
760 this->write_bytes_to_buffer_at_offset(this,&int16_val
,sizeof(u_int16_t
),payload_length_position_offset
);
765 /* the Nonce Data value is generated from chunk */
766 this->generate_from_chunk(this, rules
[i
].offset
);
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
);
773 this->write_bytes_to_buffer_at_offset(this,&int16_val
,sizeof(u_int16_t
),payload_length_position_offset
);
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
;
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
));
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
))
792 payload_t
*current_proposal
;
793 u_int32_t before_generate_position_offset
;
794 u_int32_t after_generate_position_offset
;
796 iterator
->current(iterator
,(void **)¤t_proposal
);
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);
802 /* increase size of transform */
803 length_of_sa_payload
+= (after_generate_position_offset
- before_generate_position_offset
);
805 iterator
->destroy(iterator
);
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
);
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
;
818 linked_list_t
*transforms
= *((linked_list_t
**)(this->data_struct
+ rules
[i
].offset
));
819 iterator_t
*iterator
;
821 /* create forward iterator */
822 transforms
->create_iterator(transforms
,&iterator
,TRUE
);
823 while (iterator
->has_next(iterator
))
825 payload_t
*current_transform
;
826 u_int32_t before_generate_position_offset
;
827 u_int32_t after_generate_position_offset
;
829 iterator
->current(iterator
,(void **)¤t_transform
);
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);
835 /* increase size of transform */
836 length_of_proposal
+= (after_generate_position_offset
- before_generate_position_offset
);
839 iterator
->destroy(iterator
);
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
);
846 case TRANSFORM_ATTRIBUTES
:
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
;
851 u_int16_t length_of_transform
= TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH
;
853 linked_list_t
*transform_attributes
=*((linked_list_t
**)(this->data_struct
+ rules
[i
].offset
));
855 iterator_t
*iterator
;
856 /* create forward iterator */
857 transform_attributes
->create_iterator(transform_attributes
,&iterator
,TRUE
);
858 while (iterator
->has_next(iterator
))
860 payload_t
*current_attribute
;
861 u_int32_t before_generate_position_offset
;
862 u_int32_t after_generate_position_offset
;
864 iterator
->current(iterator
,(void **)¤t_attribute
);
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);
870 /* increase size of transform */
871 length_of_transform
+= (after_generate_position_offset
- before_generate_position_offset
);
874 iterator
->destroy(iterator
);
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
);
881 case ATTRIBUTE_FORMAT
:
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
));
889 case ATTRIBUTE_LENGTH_OR_VALUE
:
891 if (this->attribute_format
== FALSE
)
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
));
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);
904 case ATTRIBUTE_VALUE
:
906 if (this->attribute_format
== FALSE
)
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
);
916 this->generate_from_chunk(this, rules
[i
].offset
);
920 this->logger
->log(this->logger
, ERROR
, "field type %s is not supported",
921 mapping_find(encoding_type_m
,rules
[i
].type
));
925 this->logger
->log_bytes(this->logger
, RAW
|MORE
, "generated data for this payload",
926 payload_start
, this->out_position
-payload_start
);
931 * Implements generator_t's destroy function.
932 * See #generator_s.destroy.
934 static status_t
destroy(private_generator_t
*this)
936 allocator_free(this->buffer
);
937 global_logger_manager
->destroy_logger(global_logger_manager
,this->logger
);
938 allocator_free(this);
943 * Described in header
945 generator_t
* generator_create()
947 private_generator_t
*this;
949 this = allocator_alloc_thing(private_generator_t
);
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
;
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
;
975 /* allocate memory for buffer */
976 this->buffer
= allocator_alloc(GENERATOR_DATA_BUFFER_SIZE
);
977 if (this->buffer
== NULL
)
979 allocator_free(this);
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
);
992 if (this->logger
== NULL
)
994 allocator_free(this->buffer
);
995 allocator_free(this);
998 return &(this->public);