4 * @brief Implementation of generator_t.
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 * Implementation of private_generator_t.get_current_buffer_size.
241 static size_t get_current_buffer_size (private_generator_t
*this)
243 return ((this->roof_position
) - (this->buffer
));
247 * Implementation of private_generator_t.get_current_buffer_space.
249 static size_t get_current_buffer_space (private_generator_t
*this)
251 /* we know, one byte more */
252 size_t space
= (this->roof_position
) - (this->out_position
);
257 * Implementation of private_generator_t.get_current_data_length.
259 static size_t get_current_data_length (private_generator_t
*this)
261 return (this->out_position
- this->buffer
);
265 * Implementation of private_generator_t.get_current_buffer_offset.
267 static u_int32_t
get_current_buffer_offset (private_generator_t
*this)
269 return (this->out_position
- this->buffer
);
273 * Implementation of private_generator_t.generate_u_int_type.
275 static void generate_u_int_type (private_generator_t
*this,encoding_type_t int_type
,u_int32_t offset
)
277 size_t number_of_bits
= 0;
279 /* find out number of bits of each U_INT type to check for enough space
306 this->logger
->log(this->logger
, ERROR
, "U_INT Type %s is not supported",
307 mapping_find(encoding_type_m
,int_type
));
311 /* U_INT Types of multiple then 8 bits must be aligned */
312 if (((number_of_bits
% 8) == 0) && (this->current_bit
!= 0))
314 this->logger
->log(this->logger
, ERROR
, "U_INT Type %s is not 8 Bit aligned",
315 mapping_find(encoding_type_m
,int_type
));
316 /* current bit has to be zero for values multiple of 8 bits */
320 /* make sure enough space is available in buffer */
321 this->make_space_available(this,number_of_bits
);
322 /* now handle each u int type differently */
327 if (this->current_bit
== 0)
329 /* highval of current byte in buffer has to be set to the new value*/
330 u_int8_t high_val
= *((u_int8_t
*)(this->data_struct
+ offset
)) << 4;
331 /* lowval in buffer is not changed */
332 u_int8_t low_val
= *(this->out_position
) & 0x0F;
333 /* highval is set, low_val is not changed */
334 *(this->out_position
) = high_val
| low_val
;
335 this->logger
->log(this->logger
, RAW
|MOST
, " => 0x%x", *(this->out_position
));
336 /* write position is not changed, just bit position is moved */
337 this->current_bit
= 4;
339 else if (this->current_bit
== 4)
341 /* highval in buffer is not changed */
342 u_int high_val
= *(this->out_position
) & 0xF0;
343 /* lowval of current byte in buffer has to be set to the new value*/
344 u_int low_val
= *((u_int8_t
*)(this->data_struct
+ offset
)) & 0x0F;
345 *(this->out_position
) = high_val
| low_val
;
346 this->logger
->log(this->logger
, RAW
|MOST
, " => 0x%x", *(this->out_position
));
347 this->out_position
++;
348 this->current_bit
= 0;
353 this->logger
->log(this->logger
, ERROR
, "U_INT_4 Type is not 4 Bit aligned");
354 /* 4 Bit integers must have a 4 bit alignment */
361 /* 8 bit values are written as they are */
362 *this->out_position
= *((u_int8_t
*)(this->data_struct
+ offset
));
363 this->logger
->log(this->logger
, RAW
|MOST
, " => 0x%x", *(this->out_position
));
364 this->out_position
++;
370 /* attribute type must not change first bit uf current byte ! */
371 if (this->current_bit
!= 1)
373 this->logger
->log(this->logger
, ERROR
, "ATTRIBUTE FORMAT flag is not set");
374 /* first bit has to be set! */
377 /* get value of attribute format flag */
378 u_int8_t attribute_format_flag
= *(this->out_position
) & 0x80;
379 /* get attribute type value as 16 bit integer*/
380 u_int16_t int16_val
= htons(*((u_int16_t
*)(this->data_struct
+ offset
)));
381 /* last bit must be unset */
382 int16_val
= int16_val
& 0xFF7F;
384 int16_val
= int16_val
| attribute_format_flag
;
385 this->logger
->log(this->logger
, RAW
|MOST
, " => 0x%x", int16_val
);
386 /* write bytes to buffer (set bit is overwritten)*/
387 this->write_bytes_to_buffer(this,&int16_val
,sizeof(u_int16_t
));
388 this->current_bit
= 0;
394 u_int16_t int16_val
= htons(*((u_int16_t
*)(this->data_struct
+ offset
)));
395 this->logger
->log_bytes(this->logger
, RAW
|MOST
, " =>", (void*)&int16_val
, sizeof(int16_val
));
396 this->write_bytes_to_buffer(this,&int16_val
,sizeof(u_int16_t
));
401 u_int32_t int32_val
= htonl(*((u_int32_t
*)(this->data_struct
+ offset
)));
402 this->logger
->log_bytes(this->logger
, RAW
|MOST
, " =>", (void*)&int32_val
, sizeof(int32_val
));
403 this->write_bytes_to_buffer(this,&int32_val
,sizeof(u_int32_t
));
408 /* 64 bit integers are written as two 32 bit integers */
409 u_int32_t int32_val_low
= htonl(*((u_int32_t
*)(this->data_struct
+ offset
)));
410 u_int32_t int32_val_high
= htonl(*((u_int32_t
*)(this->data_struct
+ offset
) + 1));
411 this->logger
->log_bytes(this->logger
, RAW
|MOST
, " => (low)", (void*)&int32_val_low
, sizeof(int32_val_low
));
412 this->logger
->log_bytes(this->logger
, RAW
|MOST
, " => (high)", (void*)&int32_val_high
, sizeof(int32_val_high
));
413 /* TODO add support for big endian machines */
414 this->write_bytes_to_buffer(this,&int32_val_high
,sizeof(u_int32_t
));
415 this->write_bytes_to_buffer(this,&int32_val_low
,sizeof(u_int32_t
));
421 /* 64 bit are written as they come :-) */
422 this->write_bytes_to_buffer(this,(this->data_struct
+ offset
),sizeof(u_int64_t
));
423 this->logger
->log_bytes(this->logger
, RAW
|MOST
, " =>", (void*)(this->data_struct
+ offset
), sizeof(u_int64_t
));
428 this->logger
->log(this->logger
, ERROR
, "U_INT Type %s is not supported", mapping_find(encoding_type_m
,int_type
));
435 * Implementation of private_generator_t.generate_reserved_field.
437 static void generate_reserved_field(private_generator_t
*this,int bits
)
439 /* only one bit or 8 bit fields are supported */
440 if ((bits
!= 1) && (bits
!= 8))
442 this->logger
->log(this->logger
, ERROR
, "Reserved field of %d bits cannot be generated", bits
);
445 /* make sure enough space is available in buffer */
446 this->make_space_available(this,bits
);
450 /* one bit processing */
451 u_int8_t reserved_bit
= ~(1 << (7 - this->current_bit
));
452 *(this->out_position
) = *(this->out_position
) & reserved_bit
;
453 if (this->current_bit
== 0)
455 /* memory must be zero */
456 *(this->out_position
) = 0x00;
461 if (this->current_bit
>= 8)
463 this->current_bit
= this->current_bit
% 8;
464 this->out_position
++;
469 /* one byte processing*/
470 if (this->current_bit
> 0)
472 this->logger
->log(this->logger
, ERROR
,
473 "Reserved field cannot be written cause allignement of current bit is %d",
477 *(this->out_position
) = 0x00;
478 this->out_position
++;
483 * Implementation of private_generator_t.generate_flag.
485 static void generate_flag (private_generator_t
*this,u_int32_t offset
)
487 /* value of current flag */
489 /* position of flag in current byte */
492 /* if the value in the data_struct is TRUE, flag_value is set to 1, 0 otherwise */
493 flag_value
= (*((bool *) (this->data_struct
+ offset
))) ?
1 : 0;
494 /* get flag position */
495 flag
= (flag_value
<< (7 - this->current_bit
));
497 /* make sure one bit is available in buffer */
498 this->make_space_available(this,1);
499 if (this->current_bit
== 0)
501 /* memory must be zero */
502 *(this->out_position
) = 0x00;
505 *(this->out_position
) = *(this->out_position
) | flag
;
508 this->logger
->log(this->logger
, RAW
|MOST
, " => 0x0%x", *(this->out_position
));
511 if (this->current_bit
>= 8)
513 this->current_bit
= this->current_bit
% 8;
514 this->out_position
++;
519 * Implementation of private_generator_t.generate_from_chunk.
521 static void generate_from_chunk (private_generator_t
*this,u_int32_t offset
)
523 if (this->current_bit
!= 0)
525 this->logger
->log(this->logger
, ERROR
, "can not generate a chunk at Bitpos %d", this->current_bit
);
529 /* position in buffer */
530 chunk_t
*attribute_value
= (chunk_t
*)(this->data_struct
+ offset
);
532 this->logger
->log_chunk(this->logger
, RAW
|MOST
, " =>", attribute_value
);
534 /* use write_bytes_to_buffer function to do the job */
535 this->write_bytes_to_buffer(this,attribute_value
->ptr
,attribute_value
->len
);
539 * Implementation of private_generator_t.make_space_available.
541 static void make_space_available (private_generator_t
*this, size_t bits
)
543 while (((this->get_current_buffer_space(this) * 8) - this->current_bit
) < bits
)
545 /* must increase buffer */
546 u_int8_t
*new_buffer
;
547 size_t old_buffer_size
= this->get_current_buffer_size(this);
548 size_t new_buffer_size
= old_buffer_size
+ GENERATOR_DATA_BUFFER_INCREASE_VALUE
;
549 size_t out_position_offset
= ((this->out_position
) - (this->buffer
));
551 this->logger
->log(this->logger
, CONTROL
|MOST
, "increased gen buffer from %d to %d byte",
552 old_buffer_size
, new_buffer_size
);
554 /* Reallocate space for new buffer */
555 new_buffer
= allocator_realloc(this->buffer
,new_buffer_size
);
556 if (new_buffer
== NULL
)
558 this->logger
->log(this->logger
, ERROR
, "reallocation of gen buffer failed!!!");
561 this->buffer
= new_buffer
;
563 this->out_position
= (this->buffer
+ out_position_offset
);
564 this->roof_position
= (this->buffer
+ new_buffer_size
);
569 * Implementation of private_generator_t.write_bytes_to_buffer.
571 static void write_bytes_to_buffer (private_generator_t
*this,void * bytes
, size_t number_of_bytes
)
574 u_int8_t
*read_position
= (u_int8_t
*) bytes
;
576 this->make_space_available(this,number_of_bytes
* 8);
578 for (i
= 0; i
< number_of_bytes
; i
++)
580 *(this->out_position
) = *(read_position
);
582 this->out_position
++;
587 * Implementation of private_generator_t.write_bytes_to_buffer_at_offset.
589 static void write_bytes_to_buffer_at_offset (private_generator_t
*this,void * bytes
,size_t number_of_bytes
,u_int32_t offset
)
592 u_int8_t
*read_position
= (u_int8_t
*) bytes
;
593 u_int8_t
*write_position
;
594 u_int32_t free_space_after_offset
= (this->get_current_buffer_size(this) - offset
);
596 /* check first if enough space for new data is available */
597 if (number_of_bytes
> free_space_after_offset
)
599 this->make_space_available(this,(number_of_bytes
- free_space_after_offset
) * 8);
602 write_position
= this->buffer
+ offset
;
603 for (i
= 0; i
< number_of_bytes
; i
++)
605 *(write_position
) = *(read_position
);
613 * Implementation of private_generator_t.write_to_chunk.
615 static void write_to_chunk (private_generator_t
*this,chunk_t
*data
)
617 size_t data_length
= this->get_current_data_length(this);
618 u_int32_t header_length_field
= data_length
;
620 /* write length into header length field */
621 if (this->header_length_position_offset
> 0)
623 u_int32_t int32_val
= htonl(header_length_field
);
624 this->write_bytes_to_buffer_at_offset(this,&int32_val
,sizeof(u_int32_t
),this->header_length_position_offset
);
627 if (this->current_bit
> 0)
629 data
->ptr
= allocator_alloc(data_length
);
630 memcpy(data
->ptr
,this->buffer
,data_length
);
631 data
->len
= data_length
;
633 this->logger
->log_chunk(this->logger
, RAW
, "generated data of this parser", data
);
637 * Implementation of private_generator_t.generate_payload.
639 static void generate_payload (private_generator_t
*this,payload_t
*payload
)
642 this->data_struct
= payload
;
644 encoding_rule_t
*rules
;
645 payload_type_t payload_type
;
646 u_int8_t
*payload_start
;
648 /* get payload type */
649 payload_type
= payload
->get_type(payload
);
650 /* spi size has to get reseted */
651 this->last_spi_size
= 0;
653 payload_start
= this->out_position
;
655 this->logger
->log(this->logger
, CONTROL
, "generating payload of type %s",
656 mapping_find(payload_type_m
,payload_type
));
658 /* each payload has its own encoding rules */
659 payload
->get_encoding_rules(payload
,&rules
,&rule_count
);
661 for (i
= 0; i
< rule_count
;i
++)
663 this->logger
->log(this->logger
, CONTROL
|MORE
, " generating rule %d %s",
664 i
, mapping_find(encoding_type_m
,rules
[i
].type
));
665 switch (rules
[i
].type
)
667 /* all u int values, IKE_SPI and ATTRIBUTE_TYPE are generated in generate_u_int_type */
676 this->generate_u_int_type(this,rules
[i
].type
,rules
[i
].offset
);
681 this->generate_reserved_field(this,1);
686 this->generate_reserved_field(this,8);
691 this->generate_flag(this,rules
[i
].offset
);
696 /* position of payload lenght field is temporary stored */
697 this->last_payload_length_position_offset
= this->get_current_buffer_offset(this);
698 /* payload length is generated like an U_INT_16 */
699 this->generate_u_int_type(this,U_INT_16
,rules
[i
].offset
);
704 /* position of header length field is temporary stored */
705 this->header_length_position_offset
= this->get_current_buffer_offset(this);
706 /* header length is generated like an U_INT_32 */
707 this->generate_u_int_type(this,U_INT_32
,rules
[i
].offset
);
711 /* spi size is handled as 8 bit unsigned integer */
712 this->generate_u_int_type(this,U_INT_8
,rules
[i
].offset
);
713 /* last spi size is temporary stored */
714 this->last_spi_size
= *((u_int8_t
*)(this->data_struct
+ rules
[i
].offset
));
718 /* the SPI value is generated from chunk */
719 this->generate_from_chunk(this,rules
[i
].offset
);
722 case KEY_EXCHANGE_DATA
:
724 /* the Key Exchange Data value is generated from chunk */
725 this->generate_from_chunk(this,rules
[i
].offset
);
727 u_int32_t payload_length_position_offset
= this->last_payload_length_position_offset
;
728 /* Length of KE_PAYLOAD is calculated */
729 u_int16_t length_of_ke_payload
= KE_PAYLOAD_HEADER_LENGTH
+ ((chunk_t
*)(this->data_struct
+ rules
[i
].offset
))->len
;
731 u_int16_t int16_val
= htons(length_of_ke_payload
);
732 this->write_bytes_to_buffer_at_offset(this,&int16_val
,sizeof(u_int16_t
),payload_length_position_offset
);
735 case NOTIFICATION_DATA
:
737 /* the Notification Data value is generated from chunk */
738 this->generate_from_chunk(this,rules
[i
].offset
);
740 u_int32_t payload_length_position_offset
= this->last_payload_length_position_offset
;
741 /* Length of Notification PAYLOAD is calculated */
742 u_int16_t length_of_notify_payload
= NOTIFY_PAYLOAD_HEADER_LENGTH
+ ((chunk_t
*)(this->data_struct
+ rules
[i
].offset
))->len
;
743 length_of_notify_payload
+= this->last_spi_size
;
744 u_int16_t int16_val
= htons(length_of_notify_payload
);
746 this->write_bytes_to_buffer_at_offset(this,&int16_val
,sizeof(u_int16_t
),payload_length_position_offset
);
751 /* the Nonce 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 nonce PAYLOAD is calculated */
756 u_int16_t length_of_nonce_payload
= NONCE_PAYLOAD_HEADER_LENGTH
+ ((chunk_t
*)(this->data_struct
+ rules
[i
].offset
))->len
;
757 u_int16_t int16_val
= htons(length_of_nonce_payload
);
759 this->write_bytes_to_buffer_at_offset(this,&int16_val
,sizeof(u_int16_t
),payload_length_position_offset
);
764 /* before iterative generate the transforms, store the current payload length position */
765 u_int32_t payload_length_position_offset
= this->last_payload_length_position_offset
;
766 /* Length of SA_PAYLOAD is calculated */
767 u_int16_t length_of_sa_payload
= SA_PAYLOAD_HEADER_LENGTH
;
769 /* proposals are stored in a linked list and so accessed */
770 linked_list_t
*proposals
= *((linked_list_t
**)(this->data_struct
+ rules
[i
].offset
));
772 iterator_t
*iterator
;
773 /* create forward iterator */
774 proposals
->create_iterator(proposals
,&iterator
,TRUE
);
775 /* every proposal is processed (iterative call )*/
776 while (iterator
->has_next(iterator
))
778 payload_t
*current_proposal
;
779 u_int32_t before_generate_position_offset
;
780 u_int32_t after_generate_position_offset
;
782 iterator
->current(iterator
,(void **)¤t_proposal
);
784 before_generate_position_offset
= this->get_current_buffer_offset(this);
785 this->public.generate_payload(&(this->public),current_proposal
);
786 after_generate_position_offset
= this->get_current_buffer_offset(this);
788 /* increase size of transform */
789 length_of_sa_payload
+= (after_generate_position_offset
- before_generate_position_offset
);
791 iterator
->destroy(iterator
);
793 int16_val
= htons(length_of_sa_payload
);
794 this->write_bytes_to_buffer_at_offset(this,&int16_val
,sizeof(u_int16_t
),payload_length_position_offset
);
800 /* before iterative generate the transforms, store the current length position */
801 u_int32_t payload_length_position_offset
= this->last_payload_length_position_offset
;
802 u_int16_t length_of_proposal
= PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH
+ this->last_spi_size
;
804 linked_list_t
*transforms
= *((linked_list_t
**)(this->data_struct
+ rules
[i
].offset
));
805 iterator_t
*iterator
;
807 /* create forward iterator */
808 transforms
->create_iterator(transforms
,&iterator
,TRUE
);
809 while (iterator
->has_next(iterator
))
811 payload_t
*current_transform
;
812 u_int32_t before_generate_position_offset
;
813 u_int32_t after_generate_position_offset
;
815 iterator
->current(iterator
,(void **)¤t_transform
);
817 before_generate_position_offset
= this->get_current_buffer_offset(this);
818 this->public.generate_payload(&(this->public),current_transform
);
819 after_generate_position_offset
= this->get_current_buffer_offset(this);
821 /* increase size of transform */
822 length_of_proposal
+= (after_generate_position_offset
- before_generate_position_offset
);
825 iterator
->destroy(iterator
);
827 int16_val
= htons(length_of_proposal
);
828 this->write_bytes_to_buffer_at_offset(this,&int16_val
,sizeof(u_int16_t
),payload_length_position_offset
);
832 case TRANSFORM_ATTRIBUTES
:
834 /* before iterative generate the transform attributes, store the current length position */
835 u_int32_t transform_length_position_offset
= this->last_payload_length_position_offset
;
837 u_int16_t length_of_transform
= TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH
;
839 linked_list_t
*transform_attributes
=*((linked_list_t
**)(this->data_struct
+ rules
[i
].offset
));
841 iterator_t
*iterator
;
842 /* create forward iterator */
843 transform_attributes
->create_iterator(transform_attributes
,&iterator
,TRUE
);
844 while (iterator
->has_next(iterator
))
846 payload_t
*current_attribute
;
847 u_int32_t before_generate_position_offset
;
848 u_int32_t after_generate_position_offset
;
850 iterator
->current(iterator
,(void **)¤t_attribute
);
852 before_generate_position_offset
= this->get_current_buffer_offset(this);
853 this->public.generate_payload(&(this->public),current_attribute
);
854 after_generate_position_offset
= this->get_current_buffer_offset(this);
856 /* increase size of transform */
857 length_of_transform
+= (after_generate_position_offset
- before_generate_position_offset
);
860 iterator
->destroy(iterator
);
862 int16_val
= htons(length_of_transform
);
863 this->write_bytes_to_buffer_at_offset(this,&int16_val
,sizeof(u_int16_t
),transform_length_position_offset
);
867 case ATTRIBUTE_FORMAT
:
869 this->generate_flag(this,rules
[i
].offset
);
870 /* Attribute format is a flag which is stored in context*/
871 this->attribute_format
= *((bool *) (this->data_struct
+ rules
[i
].offset
));
875 case ATTRIBUTE_LENGTH_OR_VALUE
:
877 if (this->attribute_format
== FALSE
)
879 this->generate_u_int_type(this,U_INT_16
,rules
[i
].offset
);
880 /* this field hold the length of the attribute */
881 this->attribute_length
= *((u_int16_t
*)(this->data_struct
+ rules
[i
].offset
));
885 this->generate_u_int_type(this,U_INT_16
,rules
[i
].offset
);
886 // status = this->write_bytes_to_buffer(this,(this->data_struct + rules[i].offset),2);
890 case ATTRIBUTE_VALUE
:
892 if (this->attribute_format
== FALSE
)
894 this->logger
->log(this->logger
, CONTROL
|MOST
, "attribute value has not fixed size");
895 /* the attribute value is generated */
896 this->generate_from_chunk(this,rules
[i
].offset
);
902 this->generate_from_chunk(this, rules
[i
].offset
);
906 this->logger
->log(this->logger
, ERROR
, "field type %s is not supported",
907 mapping_find(encoding_type_m
,rules
[i
].type
));
911 this->logger
->log_bytes(this->logger
, RAW
|MORE
, "generated data for this payload",
912 payload_start
, this->out_position
-payload_start
);
917 * Implementation of generator_t.destroy.
919 static status_t
destroy(private_generator_t
*this)
921 allocator_free(this->buffer
);
922 global_logger_manager
->destroy_logger(global_logger_manager
,this->logger
);
923 allocator_free(this);
928 * Described in header
930 generator_t
* generator_create()
932 private_generator_t
*this;
934 this = allocator_alloc_thing(private_generator_t
);
936 /* initiate public functions */
937 this->public.generate_payload
= (void(*)(generator_t
*, payload_t
*)) generate_payload
;
938 this->public.destroy
= (void(*)(generator_t
*)) destroy
;
939 this->public.write_to_chunk
= (void (*) (generator_t
*,chunk_t
*)) write_to_chunk
;
942 /* initiate private functions */
943 this->get_current_buffer_size
= get_current_buffer_size
;
944 this->get_current_buffer_space
= get_current_buffer_space
;
945 this->get_current_data_length
= get_current_data_length
;
946 this->get_current_buffer_offset
= get_current_buffer_offset
;
947 this->generate_u_int_type
= generate_u_int_type
;
948 this->generate_reserved_field
= generate_reserved_field
;
949 this->generate_flag
= generate_flag
;
950 this->generate_from_chunk
= generate_from_chunk
;
951 this->make_space_available
= make_space_available
;
952 this->write_bytes_to_buffer
= write_bytes_to_buffer
;
953 this->write_bytes_to_buffer_at_offset
= write_bytes_to_buffer_at_offset
;
956 /* allocate memory for buffer */
957 this->buffer
= allocator_alloc(GENERATOR_DATA_BUFFER_SIZE
);
959 /* initiate private variables */
960 this->out_position
= this->buffer
;
961 this->roof_position
= this->buffer
+ GENERATOR_DATA_BUFFER_SIZE
;
962 this->data_struct
= NULL
;
963 this->current_bit
= 0;
964 this->last_payload_length_position_offset
= 0;
965 this->header_length_position_offset
= 0;
966 this->logger
= global_logger_manager
->create_logger(global_logger_manager
,GENERATOR
,NULL
);
968 return &(this->public);