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"
32 #include "utils/allocator.h"
33 #include "utils/linked_list.h"
34 #include "utils/logger_manager.h"
35 #include "payloads/payload.h"
36 #include "payloads/proposal_substructure.h"
37 #include "payloads/transform_substructure.h"
38 #include "payloads/sa_payload.h"
39 #include "payloads/ke_payload.h"
41 extern logger_manager_t
*global_logger_manager
;
44 * Private part of a generator_t object
46 typedef struct private_generator_s private_generator_t
;
48 struct private_generator_s
{
50 * Public part of a generator_t object
54 /* private functions and fields */
58 * Generates a U_INT-Field type and writes it to buffer.
60 * @param this private_generator_t object
61 * @param int_type type of U_INT field (U_INT_4, U_INT_8, etc.)
62 * ATTRIBUTE_TYPE is also generated in this function
63 * @param offset offset of value in data struct
64 * @param generator_contexts generator_contexts_t object where the context is written or read from
65 * @return - SUCCESS if succeeded
66 * - OUT_OF_RES if out of ressources
68 status_t (*generate_u_int_type
) (private_generator_t
*this,encoding_type_t int_type
,u_int32_t offset
);
71 * Get size of current buffer in bytes.
73 * @param this private_generator_t object
74 * @return Size of buffer in bytes
76 size_t (*get_current_buffer_size
) (private_generator_t
*this);
79 * Get free space of current buffer in bytes.
81 * @param this private_generator_t object
82 * @return space in buffer in bytes
84 size_t (*get_current_buffer_space
) (private_generator_t
*this);
87 * Get length of data in buffer (in bytes).
89 * @param this private_generator_t object
90 * @return length of data in bytes
92 size_t (*get_current_data_length
) (private_generator_t
*this);
95 * Get current offset in buffer (in bytes).
97 * @param this private_generator_t object
98 * @return offset in bytes
100 u_int32_t (*get_current_buffer_offset
) (private_generator_t
*this);
103 * Generates a RESERVED BIT field or a RESERVED BYTE field and writes
106 * @param this private_generator_t object
107 * @param generator_contexts generator_contexts_t object where the context is written or read from
108 * @param bits number of bits to generate
109 * @return - SUCCESS if succeeded
110 * - OUT_OF_RES if out of ressources
111 * - FAILED if bit count not supported
113 status_t (*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
121 * @return - SUCCESS if succeeded
122 * - OUT_OF_RES if out of ressources
124 status_t (*generate_flag
) (private_generator_t
*this,u_int32_t offset
);
127 * Writes the current buffer content into a chunk_t
129 * Memory of specific chunk_t gets allocated.
131 * @param this calling private_generator_t object
132 * @param data pointer of chunk_t to write to
134 * - SUCCESSFUL if succeeded
135 * - OUT_OF_RES otherwise
137 status_t (*write_chunk
) (private_generator_t
*this,chunk_t
*data
);
140 * Generates a bytestream from a chunk_t
142 * @param this private_generator_t object
143 * @param offset offset of chunk_t value in data struct
144 * @return - SUCCESS if succeeded
145 * - OUT_OF_RES if out of ressources
147 status_t (*generate_from_chunk
) (private_generator_t
*this,u_int32_t offset
);
150 * Makes sure enough space is available in buffer to store amount of bits.
152 * If buffer is to small to hold the specific amount of bits it
153 * is increased using reallocation function of allocator.
155 * @param this calling private_generator_t object
156 * @param bits number of bits to make available in buffer
158 * - SUCCESSFUL if succeeded
159 * - OUT_OF_RES otherwise
161 status_t (*make_space_available
) (private_generator_t
*this,size_t bits
);
164 * Writes a specific amount of byte into the buffer.
166 * If buffer is to small to hold the specific amount of bytes it
169 * @param this calling private_generator_t object
170 * @param bytes pointer to bytes to write
171 * @param number_of_bytes number of bytes to write into buffer
173 * - SUCCESSFUL if succeeded
174 * - OUT_OF_RES otherwise
176 status_t (*write_bytes_to_buffer
) (private_generator_t
*this,void * bytes
,size_t number_of_bytes
);
180 * Writes a specific amount of byte into the buffer at a specific offset.
182 * @warning buffer size is not check to hold the data if offset is to large.
184 * @param this calling private_generator_t object
185 * @param bytes pointer to bytes to write
186 * @param number_of_bytes number of bytes to write into buffer
187 * @param offset offset to write the data into
189 * - SUCCESSFUL if succeeded
190 * - OUT_OF_RES otherwise
192 status_t (*write_bytes_to_buffer_at_offset
) (private_generator_t
*this,void * bytes
,size_t number_of_bytes
,u_int32_t offset
);
195 * Buffer used to generate the data into.
200 * Current write position in buffer (one byte aligned).
202 u_int8_t
*out_position
;
205 * Position of last byte in buffer.
207 u_int8_t
*roof_position
;
210 * Current bit writing to in current byte (between 0 and 7).
215 * Associated data struct to read informations from.
220 * Last payload length position offset in the buffer
222 u_int32_t last_payload_length_position_offset
;
225 * Offset of the header length field in the buffer
227 u_int32_t header_length_position_offset
;
232 u_int8_t last_spi_size
;
235 * Attribute format of the last generated transform attribute
237 * Used to check if a variable value field is used or not for
238 * the transform attribute value.
240 bool attribute_format
;
243 * Depending on the value of attribute_format this field is used
244 * to hold the length of the transform attribute in bytes
246 u_int16_t attribute_length
;
255 * Implements private_generator_t's get_current_buffer_size function.
256 * See #private_generator_s.get_current_buffer_size.
258 static size_t get_current_buffer_size (private_generator_t
*this)
260 return ((this->roof_position
) - (this->buffer
));
264 * Implements private_generator_t's get_current_buffer_space function.
265 * See #private_generator_s.get_current_buffer_space.
267 static size_t get_current_buffer_space (private_generator_t
*this)
269 /* we know, one byte more */
270 size_t space
= (this->roof_position
) - (this->out_position
);
271 if (this->current_bit
== 0)
279 * Implements private_generator_t's get_current_buffer_space function.
280 * See #private_generator_s.get_current_buffer_space.
282 static size_t get_current_data_length (private_generator_t
*this)
284 return (this->out_position
- this->buffer
);
288 * Implements private_generator_t's get_current_buffer_offset function.
289 * See #private_generator_s.get_current_buffer_offset.
291 static u_int32_t
get_current_buffer_offset (private_generator_t
*this)
293 return (this->out_position
- this->buffer
);
298 * Implements private_generator_t's generate_u_int_type function.
299 * See #private_generator_s.generate_u_int_type.
301 static status_t
generate_u_int_type (private_generator_t
*this,encoding_type_t int_type
,u_int32_t offset
)
303 size_t number_of_bits
= 0;
306 /* find out number of bits of each U_INT type to check for enough space
331 /* U_INT Types of multiple then 8 bits must be aligned */
332 if (((number_of_bits
% 8) == 0) && (this->current_bit
!= 0))
334 this->logger
->log(this->logger
,CONTROL_MORE
,"U_INT Type %s is not 8 Bit aligned", mapping_find(encoding_type_t_mappings
,int_type
));
335 /* current bit has to be zero for values multiple of 8 bits */
339 /* make sure enough space is available in buffer */
340 status
= this->make_space_available(this,number_of_bits
);
341 if (status
!= SUCCESS
)
345 /* now handle each u int type differently */
350 if (this->current_bit
== 0)
352 /* highval of current byte in buffer has to be set to the new value*/
353 u_int8_t high_val
= *((u_int8_t
*)(this->data_struct
+ offset
)) << 4;
354 /* lowval in buffer is not changed */
355 u_int8_t low_val
= *(this->out_position
) & 0x0F;
356 /* highval is set, low_val is not changed */
357 *(this->out_position
) = high_val
| low_val
;
358 /* write position is not changed, just bit position is moved */
359 this->current_bit
= 4;
361 else if (this->current_bit
== 4)
363 /* highval in buffer is not changed */
364 u_int high_val
= *(this->out_position
) & 0xF0;
365 /* lowval of current byte in buffer has to be set to the new value*/
366 u_int low_val
= *((u_int8_t
*)(this->data_struct
+ offset
)) & 0x0F;
367 *(this->out_position
) = high_val
| low_val
;
368 this->out_position
++;
369 this->current_bit
= 0;
374 this->logger
->log(this->logger
,CONTROL_MORE
,"U_INT_4 Type is not 4 Bit aligned");
375 /* 4 Bit integers must have a 4 bit alignment */
382 /* 8 bit values are written as they are */
383 *this->out_position
= *((u_int8_t
*)(this->data_struct
+ offset
));
384 this->out_position
++;
390 /* attribute type must not change first bit uf current byte ! */
391 if (this->current_bit
!= 1)
393 this->logger
->log(this->logger
,CONTROL_MORE
,"ATTRIBUTE FORMAT flag is not set");
394 /* first bit has to be set! */
397 /* get value of attribute format flag */
398 u_int8_t attribute_format_flag
= *(this->out_position
) & 0x80;
399 /* get attribute type value as 16 bit integer*/
400 u_int16_t int16_val
= htons(*((u_int16_t
*)(this->data_struct
+ offset
)));
401 /* last bit must be unset */
402 int16_val
= int16_val
& 0xFF7F;
404 int16_val
= int16_val
| attribute_format_flag
;
405 /* write bytes to buffer (set bit is overwritten)*/
406 this->write_bytes_to_buffer(this,&int16_val
,sizeof(u_int16_t
));
407 this->current_bit
= 0;
413 u_int16_t int16_val
= htons(*((u_int16_t
*)(this->data_struct
+ offset
)));
414 this->write_bytes_to_buffer(this,&int16_val
,sizeof(u_int16_t
));
419 u_int32_t int32_val
= htonl(*((u_int32_t
*)(this->data_struct
+ offset
)));
420 this->write_bytes_to_buffer(this,&int32_val
,sizeof(u_int32_t
));
425 /* 64 bit integers are written as two 32 bit integers */
426 u_int32_t int32_val_low
= htonl(*((u_int32_t
*)(this->data_struct
+ offset
)));
427 u_int32_t int32_val_high
= htonl(*((u_int32_t
*)(this->data_struct
+ offset
) + 1));
428 /* TODO add support for big endian machines */
429 this->write_bytes_to_buffer(this,&int32_val_high
,sizeof(u_int32_t
));
430 this->write_bytes_to_buffer(this,&int32_val_low
,sizeof(u_int32_t
));
435 this->logger
->log(this->logger
,CONTROL_MORE
,"U_INT Type %s is not supported", mapping_find(encoding_type_t_mappings
,int_type
));
442 * Implements private_generator_t's generate_reserved_field function.
443 * See #private_generator_s.generate_reserved_field.
445 static status_t
generate_reserved_field (private_generator_t
*this,int bits
)
449 /* only one bit or 8 bit fields are supported */
450 if ((bits
!= 1) && (bits
!= 8))
452 this->logger
->log(this->logger
,CONTROL_MORE
,"Reserved field of %d bits cannot be generated",bits
);
455 /* make sure enough space is available in buffer */
456 status
= this->make_space_available(this,bits
);
457 if (status
!= SUCCESS
)
464 /* one bit processing */
465 u_int8_t reserved_bit
= ~(1 << (7 - this->current_bit
));
466 *(this->out_position
) = *(this->out_position
) & reserved_bit
;
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
,CONTROL_MORE
,"Reserved field cannot be written cause allignement of current bit is %d",this->current_bit
);
482 *(this->out_position
) = 0x00;
483 this->out_position
++;
492 * Implements private_generator_t's generate_flag function.
493 * See #private_generator_s.generate_flag.
495 static status_t
generate_flag (private_generator_t
*this,u_int32_t offset
)
498 /* value of current flag */
500 /* position of flag in current byte */
503 /* if the value in the data_struct is TRUE, flag_value is set to 1, 0 otherwise */
504 flag_value
= (*((bool *) (this->data_struct
+ offset
))) ?
1 : 0;
505 /* get flag position */
506 flag
= (flag_value
<< (7 - this->current_bit
));
508 /* make sure one bit is available in buffer */
509 status
= this->make_space_available(this,1);
510 if (status
!= SUCCESS
)
515 *(this->out_position
) = *(this->out_position
) | flag
;
518 if (this->current_bit
>= 8)
520 this->current_bit
= this->current_bit
% 8;
521 this->out_position
++;
527 * Implements private_generator_t's generate_from_chunk function.
528 * See #private_generator_s.generate_from_chunk.
530 static status_t
generate_from_chunk (private_generator_t
*this,u_int32_t offset
)
532 if (this->current_bit
!= 0)
534 this->logger
->log(this->logger
,CONTROL_MORE
,"Chunks can only be taken if bits are alligned in buffer");
537 /* position in buffer */
538 chunk_t
*attribute_value
= (chunk_t
*)(this->data_struct
+ offset
);
540 /* use write_bytes_to_buffer function to do the job */
541 return this->write_bytes_to_buffer(this,attribute_value
->ptr
,attribute_value
->len
);
546 * Implements private_generator_t's generator_context_make_space_available function.
547 * See #private_generator_s.generator_context_make_space_available.
549 static status_t
make_space_available (private_generator_t
*this, size_t bits
)
551 while (((this->get_current_buffer_space(this) * 8) - this->current_bit
) < bits
)
553 /* must increase buffer */
554 u_int8_t
*new_buffer
;
555 size_t old_buffer_size
= this->get_current_buffer_size(this);
556 size_t new_buffer_size
= old_buffer_size
+ GENERATOR_DATA_BUFFER_INCREASE_VALUE
;
557 size_t out_position_offset
= ((this->out_position
) - (this->buffer
));
559 this->logger
->log(this->logger
,CONTROL_MORE
,"Gen-Buffer is increased from %d to %d byte",old_buffer_size
,new_buffer_size
);
561 /* Reallocate space for new buffer */
562 new_buffer
= allocator_realloc(this->buffer
,new_buffer_size
);
563 if (new_buffer
== NULL
)
568 this->buffer
= new_buffer
;
570 this->out_position
= (this->buffer
+ out_position_offset
);
571 this->roof_position
= (this->buffer
+ new_buffer_size
);
577 * Implements private_generator_t's write_bytes_to_buffer function.
578 * See #private_generator_s.write_bytes_to_buffer.
580 static status_t
write_bytes_to_buffer (private_generator_t
*this,void * bytes
,size_t number_of_bytes
)
584 u_int8_t
*read_position
= (u_int8_t
*) bytes
;
586 status
= this->make_space_available(this,number_of_bytes
* 8);
587 if (status
!= SUCCESS
)
592 for (i
= 0; i
< number_of_bytes
; i
++)
594 *(this->out_position
) = *(read_position
);
596 this->out_position
++;
602 * Implements private_generator_t's write_bytes_to_buffer_at_offset function.
603 * See #private_generator_s.write_bytes_to_buffer_at_offset.
605 static status_t
write_bytes_to_buffer_at_offset (private_generator_t
*this,void * bytes
,size_t number_of_bytes
,u_int32_t offset
)
609 u_int8_t
*read_position
= (u_int8_t
*) bytes
;
610 u_int8_t
*write_position
;
611 u_int32_t free_space_after_offset
= (this->get_current_buffer_size(this) - offset
);
613 /* check first if enough space for new data is available */
614 if (number_of_bytes
> free_space_after_offset
)
616 status
= this->make_space_available(this,(number_of_bytes
- free_space_after_offset
) * 8);
619 write_position
= this->buffer
+ offset
;
620 for (i
= 0; i
< number_of_bytes
; i
++)
622 *(write_position
) = *(read_position
);
630 * Implements generator_t's write_chunk function.
631 * See #generator_s.write_chunk.
633 static status_t
write_to_chunk (private_generator_t
*this,chunk_t
*data
)
635 size_t data_length
= this->get_current_data_length(this);
636 u_int32_t header_length_field
= data_length
;
638 /* write length into header length field */
639 if (this->header_length_position_offset
> 0)
641 u_int32_t int32_val
= htonl(header_length_field
);
642 this->write_bytes_to_buffer_at_offset(this,&int32_val
,sizeof(u_int32_t
),this->header_length_position_offset
);
645 if (this->current_bit
> 0)
647 data
->ptr
= allocator_alloc(data_length
);
648 if (data
->ptr
== NULL
)
651 this->logger
->log(this->logger
,CONTROL_MORE
,"OUT OF Ressources to wrote data to chunk!!!!!");
654 memcpy(data
->ptr
,this->buffer
,data_length
);
655 data
->len
= data_length
;
660 * Implements generator_t's generate_payload function.
661 * See #generator_s.generate_payload.
663 static status_t
generate_payload (private_generator_t
*this,payload_t
*payload
)
667 this->data_struct
= payload
;
669 encoding_rule_t
*rules
;
670 payload_type_t payload_type
;
672 /* get payload type */
673 payload_type
= payload
->get_type(payload
);
675 this->logger
->log(this->logger
,CONTROL
,"Start generating payload of type %s",mapping_find(payload_type_t_mappings
,payload_type
));
677 /* each payload has its own encoding rules */
678 payload
->get_encoding_rules(payload
,&rules
,&rule_count
);
680 for (i
= 0; i
< rule_count
;i
++)
683 switch (rules
[i
].type
)
685 /* all u int values and ATTRIBUTE_TYPE are generated in generate_u_int_type */
693 status
= this->generate_u_int_type(this,rules
[i
].type
,rules
[i
].offset
);
698 status
= this->generate_reserved_field(this,1);
703 status
= this->generate_reserved_field(this,8);
708 status
= this->generate_flag(this,rules
[i
].offset
);
713 /* position of payload lenght field is temporary stored */
714 this->last_payload_length_position_offset
= this->get_current_buffer_offset(this);
715 /* payload length is generated like an U_INT_16 */
716 status
= this->generate_u_int_type(this,U_INT_16
,rules
[i
].offset
);
721 /* position of header length field is temporary stored */
722 this->header_length_position_offset
= this->get_current_buffer_offset(this);
723 /* header length is generated like an U_INT_32 */
724 status
= this->generate_u_int_type(this,U_INT_32
,rules
[i
].offset
);
728 /* spi size is handled as 8 bit unsigned integer */
729 status
= this->generate_u_int_type(this,U_INT_8
,rules
[i
].offset
);
730 /* last spi size is temporary stored */
731 this->last_spi_size
= *((u_int8_t
*)(this->data_struct
+ rules
[i
].offset
));
735 /* the SPI value is generated from chunk */
736 status
= this->generate_from_chunk(this,rules
[i
].offset
);
739 case KEY_EXCHANGE_DATA
:
741 /* the Key Exchange Data value is generated from chunk */
742 status
= this->generate_from_chunk(this,rules
[i
].offset
);
743 if (status
!= SUCCESS
)
745 this->logger
->log(this->logger
,CONTROL_MORE
,"Could no write key exchange data from chunk");
749 /* before iterative generate the transforms, store the current payload length position */
750 u_int32_t payload_length_position_offset
= this->last_payload_length_position_offset
;
751 /* Length of KE_PAYLOAD is calculated */
752 u_int16_t length_of_ke_payload
= KE_PAYLOAD_HEADER_LENGTH
+ ((chunk_t
*)(this->data_struct
+ rules
[i
].offset
))->len
;
754 u_int16_t int16_val
= htons(length_of_ke_payload
);
755 status
= this->write_bytes_to_buffer_at_offset(this,&int16_val
,sizeof(u_int16_t
),payload_length_position_offset
);
756 if (status
!= SUCCESS
)
758 this->logger
->log(this->logger
,CONTROL_MORE
,"Could no write payload length into buffer");
765 /* before iterative generate the transforms, store the current payload length position */
766 u_int32_t payload_length_position_offset
= this->last_payload_length_position_offset
;
767 /* Length of SA_PAYLOAD is calculated */
768 u_int16_t length_of_sa_payload
= SA_PAYLOAD_HEADER_LENGTH
;
770 /* proposals are stored in a linked list and so accessed */
771 linked_list_t
*proposals
= *((linked_list_t
**)(this->data_struct
+ rules
[i
].offset
));
773 this->logger
->log(this->logger
,CONTROL_MORE
,"Generate Proposals");
775 linked_list_iterator_t
*iterator
;
776 /* create forward iterator */
777 status
= proposals
->create_iterator(proposals
,&iterator
,TRUE
);
778 if (status
!= SUCCESS
)
780 this->logger
->log(this->logger
,CONTROL_MORE
,"Could not iterator of proposals");
783 /* every proposal is processed (iterative call )*/
784 while (iterator
->has_next(iterator
))
786 payload_t
*current_proposal
;
787 u_int32_t before_generate_position_offset
;
788 u_int32_t after_generate_position_offset
;
790 status
= iterator
->current(iterator
,(void **)¤t_proposal
);
791 if (status
!= SUCCESS
)
793 iterator
->destroy(iterator
);
796 before_generate_position_offset
= this->get_current_buffer_offset(this);
797 status
= this->public.generate_payload(&(this->public),current_proposal
);
798 after_generate_position_offset
= this->get_current_buffer_offset(this);
799 if (status
!= SUCCESS
)
801 iterator
->destroy(iterator
);
805 /* increase size of transform */
806 length_of_sa_payload
+= (after_generate_position_offset
- before_generate_position_offset
);
808 iterator
->destroy(iterator
);
809 this->logger
->log(this->logger
,CONTROL_MORE
,"Length of Payload is %d, offset is %d",length_of_sa_payload
,payload_length_position_offset
);
811 int16_val
= htons(length_of_sa_payload
);
812 status
= this->write_bytes_to_buffer_at_offset(this,&int16_val
,sizeof(u_int16_t
),payload_length_position_offset
);
813 if (status
!= SUCCESS
)
815 this->logger
->log(this->logger
,CONTROL_MORE
,"Could no write payload length into buffer");
823 /* before iterative generate the transforms, store the current length position */
824 u_int32_t payload_length_position_offset
= this->last_payload_length_position_offset
;
825 u_int16_t length_of_proposal
= PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH
+ this->last_spi_size
;
827 linked_list_t
*transforms
= *((linked_list_t
**)(this->data_struct
+ rules
[i
].offset
));
828 linked_list_iterator_t
*iterator
;
830 this->logger
->log(this->logger
,CONTROL_MORE
,"Generate Transforms");
832 /* create forward iterator */
833 status
= transforms
->create_iterator(transforms
,&iterator
,TRUE
);
834 if (status
!= SUCCESS
)
838 while (iterator
->has_next(iterator
))
840 payload_t
*current_transform
;
841 u_int32_t before_generate_position_offset
;
842 u_int32_t after_generate_position_offset
;
844 status
= iterator
->current(iterator
,(void **)¤t_transform
);
845 if (status
!= SUCCESS
)
847 iterator
->destroy(iterator
);
851 before_generate_position_offset
= this->get_current_buffer_offset(this);
852 status
= this->public.generate_payload(&(this->public),current_transform
);
853 after_generate_position_offset
= this->get_current_buffer_offset(this);
854 if (status
!= SUCCESS
)
856 iterator
->destroy(iterator
);
860 /* increase size of transform */
861 length_of_proposal
+= (after_generate_position_offset
- before_generate_position_offset
);
864 iterator
->destroy(iterator
);
866 this->logger
->log(this->logger
,CONTROL_MORE
,"Length of Transform is %d, offset is %d",length_of_proposal
,payload_length_position_offset
);
868 int16_val
= htons(length_of_proposal
);
869 this->write_bytes_to_buffer_at_offset(this,&int16_val
,sizeof(u_int16_t
),payload_length_position_offset
);
873 case TRANSFORM_ATTRIBUTES
:
875 this->logger
->log(this->logger
,CONTROL_MORE
,"Generate Transform attributes");
876 /* before iterative generate the transform attributes, store the current length position */
877 u_int32_t transform_length_position_offset
= this->last_payload_length_position_offset
;
879 u_int16_t length_of_transform
= TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH
;
881 linked_list_t
*transform_attributes
=*((linked_list_t
**)(this->data_struct
+ rules
[i
].offset
));
883 linked_list_iterator_t
*iterator
;
884 /* create forward iterator */
885 status
= transform_attributes
->create_iterator(transform_attributes
,&iterator
,TRUE
);
886 if (status
!= SUCCESS
)
890 while (iterator
->has_next(iterator
))
892 payload_t
*current_attribute
;
893 u_int32_t before_generate_position_offset
;
894 u_int32_t after_generate_position_offset
;
896 status
= iterator
->current(iterator
,(void **)¤t_attribute
);
897 if (status
!= SUCCESS
)
899 iterator
->destroy(iterator
);
903 before_generate_position_offset
= this->get_current_buffer_offset(this);
904 this->public.generate_payload(&(this->public),current_attribute
);
905 after_generate_position_offset
= this->get_current_buffer_offset(this);
907 /* increase size of transform */
908 length_of_transform
+= (after_generate_position_offset
- before_generate_position_offset
);
911 iterator
->destroy(iterator
);
913 this->logger
->log(this->logger
,CONTROL_MORE
,"Length of Transform is %d, offset is %d",length_of_transform
,transform_length_position_offset
);
915 int16_val
= htons(length_of_transform
);
916 this->write_bytes_to_buffer_at_offset(this,&int16_val
,sizeof(u_int16_t
),transform_length_position_offset
);
920 case ATTRIBUTE_FORMAT
:
922 status
= this->generate_flag(this,rules
[i
].offset
);
923 /* Attribute format is a flag which is stored in context*/
924 this->attribute_format
= *((bool *) (this->data_struct
+ rules
[i
].offset
));
928 case ATTRIBUTE_LENGTH_OR_VALUE
:
930 this->logger
->log(this->logger
,CONTROL_MORE
,"Generate Attribute Length or Value field");
931 if (this->attribute_format
== FALSE
)
933 status
= this->generate_u_int_type(this,U_INT_16
,rules
[i
].offset
);
934 /* this field hold the length of the attribute */
935 this->attribute_length
= *((u_int16_t
*)(this->data_struct
+ rules
[i
].offset
));
939 status
= this->write_bytes_to_buffer(this,(this->data_struct
+ rules
[i
].offset
),2);
943 case ATTRIBUTE_VALUE
:
945 if (this->attribute_format
== FALSE
)
947 this->logger
->log(this->logger
,CONTROL_MORE
,"Attribute value has not fixed size");
948 /* the attribute value is generated */
949 status
= this->generate_from_chunk(this,rules
[i
].offset
);
954 this->logger
->log(this->logger
,CONTROL_MORE
,"Field Type %s is not supported",mapping_find(encoding_type_t_mappings
,rules
[i
].type
));
955 return NOT_SUPPORTED
;
963 * Implements generator_t's destroy function.
964 * See #generator_s.destroy.
966 static status_t
destroy(private_generator_t
*this)
968 allocator_free(this->buffer
);
969 global_logger_manager
->destroy_logger(global_logger_manager
,this->logger
);
970 allocator_free(this);
975 * Described in header
977 generator_t
* generator_create()
979 private_generator_t
*this;
981 this = allocator_alloc_thing(private_generator_t
);
987 /* initiate public functions */
988 this->public.generate_payload
= (status_t(*)(generator_t
*, payload_t
*)) generate_payload
;
989 this->public.destroy
= (status_t(*)(generator_t
*)) destroy
;
990 this->public.write_to_chunk
= (status_t (*) (generator_t
*,chunk_t
*)) write_to_chunk
;
993 /* initiate private functions */
994 this->get_current_buffer_size
= get_current_buffer_size
;
995 this->get_current_buffer_space
= get_current_buffer_space
;
996 this->get_current_data_length
= get_current_data_length
;
997 this->get_current_buffer_offset
= get_current_buffer_offset
;
998 this->generate_u_int_type
= generate_u_int_type
;
999 this->generate_reserved_field
= generate_reserved_field
;
1000 this->generate_flag
= generate_flag
;
1001 this->generate_from_chunk
= generate_from_chunk
;
1002 this->make_space_available
= make_space_available
;
1003 this->write_bytes_to_buffer
= write_bytes_to_buffer
;
1004 this->write_bytes_to_buffer_at_offset
= write_bytes_to_buffer_at_offset
;
1007 /* allocate memory for buffer */
1008 this->buffer
= allocator_alloc(GENERATOR_DATA_BUFFER_SIZE
);
1009 if (this->buffer
== NULL
)
1011 allocator_free(this);
1015 /* initiate private variables */
1016 this->out_position
= this->buffer
;
1017 this->roof_position
= this->buffer
+ GENERATOR_DATA_BUFFER_SIZE
;
1018 this->data_struct
= NULL
;
1019 this->current_bit
= 0;
1020 this->last_payload_length_position_offset
= 0;
1021 this->header_length_position_offset
= 0;
1022 this->logger
= global_logger_manager
->create_logger(global_logger_manager
,GENERATOR
,NULL
);
1024 if (this->logger
== NULL
)
1026 allocator_free(this->buffer
);
1027 allocator_free(this);
1030 return &(this->public);