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 "payloads/payload.h"
37 #include "payloads/proposal_substructure.h"
38 #include "payloads/transform_substructure.h"
39 #include "payloads/sa_payload.h"
40 #include "payloads/ke_payload.h"
41 #include "payloads/notify_payload.h"
42 #include "payloads/nonce_payload.h"
45 * Private part of a generator_t object
47 typedef struct private_generator_s private_generator_t
;
49 struct private_generator_s
{
51 * Public part of a generator_t object
55 /* private functions and fields */
59 * Generates a U_INT-Field type and writes it to buffer.
61 * @param this private_generator_t object
62 * @param int_type type of U_INT field (U_INT_4, U_INT_8, etc.)
63 * ATTRIBUTE_TYPE is also generated in this function
64 * @param offset offset of value in data struct
65 * @param generator_contexts generator_contexts_t object where the context is written or read from
66 * @return - SUCCESS if succeeded
67 * - OUT_OF_RES if out of ressources
69 status_t (*generate_u_int_type
) (private_generator_t
*this,encoding_type_t int_type
,u_int32_t offset
);
72 * Get size of current buffer in bytes.
74 * @param this private_generator_t object
75 * @return Size of buffer in bytes
77 size_t (*get_current_buffer_size
) (private_generator_t
*this);
80 * Get free space of current buffer in bytes.
82 * @param this private_generator_t object
83 * @return space in buffer in bytes
85 size_t (*get_current_buffer_space
) (private_generator_t
*this);
88 * Get length of data in buffer (in bytes).
90 * @param this private_generator_t object
91 * @return length of data in bytes
93 size_t (*get_current_data_length
) (private_generator_t
*this);
96 * Get current offset in buffer (in bytes).
98 * @param this private_generator_t object
99 * @return offset in bytes
101 u_int32_t (*get_current_buffer_offset
) (private_generator_t
*this);
104 * Generates a RESERVED BIT field or a RESERVED BYTE field and writes
107 * @param this private_generator_t object
108 * @param generator_contexts generator_contexts_t object where the context is written or read from
109 * @param bits number of bits to generate
110 * @return - SUCCESS if succeeded
111 * - OUT_OF_RES if out of ressources
112 * - FAILED if bit count not supported
114 status_t (*generate_reserved_field
) (private_generator_t
*this,int bits
);
117 * Generates a FLAG field
119 * @param this private_generator_t object
120 * @param generator_contexts generator_contexts_t object where the context is written or read from
121 * @param offset offset of flag value in data struct
122 * @return - SUCCESS if succeeded
123 * - OUT_OF_RES if out of ressources
125 status_t (*generate_flag
) (private_generator_t
*this,u_int32_t offset
);
128 * Writes the current buffer content into a chunk_t
130 * Memory of specific chunk_t gets allocated.
132 * @param this calling private_generator_t object
133 * @param data pointer of chunk_t to write to
135 * - SUCCESSFUL if succeeded
136 * - OUT_OF_RES otherwise
138 status_t (*write_chunk
) (private_generator_t
*this,chunk_t
*data
);
141 * Generates a bytestream from a chunk_t
143 * @param this private_generator_t object
144 * @param offset offset of chunk_t value in data struct
145 * @return - SUCCESS if succeeded
146 * - OUT_OF_RES if out of ressources
148 status_t (*generate_from_chunk
) (private_generator_t
*this,u_int32_t offset
);
151 * Makes sure enough space is available in buffer to store amount of bits.
153 * If buffer is to small to hold the specific amount of bits it
154 * is increased using reallocation function of allocator.
156 * @param this calling private_generator_t object
157 * @param bits number of bits to make available in buffer
159 * - SUCCESSFUL if succeeded
160 * - OUT_OF_RES otherwise
162 status_t (*make_space_available
) (private_generator_t
*this,size_t bits
);
165 * Writes a specific amount of byte into the buffer.
167 * If buffer is to small to hold the specific amount of bytes it
170 * @param this calling private_generator_t object
171 * @param bytes pointer to bytes to write
172 * @param number_of_bytes number of bytes to write into buffer
174 * - SUCCESSFUL if succeeded
175 * - OUT_OF_RES otherwise
177 status_t (*write_bytes_to_buffer
) (private_generator_t
*this,void * bytes
,size_t number_of_bytes
);
181 * Writes a specific amount of byte into the buffer at a specific offset.
183 * @warning buffer size is not check to hold the data if offset is to large.
185 * @param this calling private_generator_t object
186 * @param bytes pointer to bytes to write
187 * @param number_of_bytes number of bytes to write into buffer
188 * @param offset offset to write the data into
190 * - SUCCESSFUL if succeeded
191 * - OUT_OF_RES otherwise
193 status_t (*write_bytes_to_buffer_at_offset
) (private_generator_t
*this,void * bytes
,size_t number_of_bytes
,u_int32_t offset
);
196 * Buffer used to generate the data into.
201 * Current write position in buffer (one byte aligned).
203 u_int8_t
*out_position
;
206 * Position of last byte in buffer.
208 u_int8_t
*roof_position
;
211 * Current bit writing to in current byte (between 0 and 7).
216 * Associated data struct to read informations from.
221 * Last payload length position offset in the buffer
223 u_int32_t last_payload_length_position_offset
;
226 * Offset of the header length field in the buffer
228 u_int32_t header_length_position_offset
;
233 u_int8_t last_spi_size
;
236 * Attribute format of the last generated transform attribute
238 * Used to check if a variable value field is used or not for
239 * the transform attribute value.
241 bool attribute_format
;
244 * Depending on the value of attribute_format this field is used
245 * to hold the length of the transform attribute in bytes
247 u_int16_t attribute_length
;
256 * Implements private_generator_t's get_current_buffer_size function.
257 * See #private_generator_s.get_current_buffer_size.
259 static size_t get_current_buffer_size (private_generator_t
*this)
261 return ((this->roof_position
) - (this->buffer
));
265 * Implements private_generator_t's get_current_buffer_space function.
266 * See #private_generator_s.get_current_buffer_space.
268 static size_t get_current_buffer_space (private_generator_t
*this)
270 /* we know, one byte more */
271 size_t space
= (this->roof_position
) - (this->out_position
);
276 * Implements private_generator_t's get_current_buffer_space function.
277 * See #private_generator_s.get_current_buffer_space.
279 static size_t get_current_data_length (private_generator_t
*this)
281 return (this->out_position
- this->buffer
);
285 * Implements private_generator_t's get_current_buffer_offset function.
286 * See #private_generator_s.get_current_buffer_offset.
288 static u_int32_t
get_current_buffer_offset (private_generator_t
*this)
290 return (this->out_position
- this->buffer
);
295 * Implements private_generator_t's generate_u_int_type function.
296 * See #private_generator_s.generate_u_int_type.
298 static status_t
generate_u_int_type (private_generator_t
*this,encoding_type_t int_type
,u_int32_t offset
)
300 size_t number_of_bits
= 0;
303 /* find out number of bits of each U_INT type to check for enough space
328 /* U_INT Types of multiple then 8 bits must be aligned */
329 if (((number_of_bits
% 8) == 0) && (this->current_bit
!= 0))
331 this->logger
->log(this->logger
,CONTROL
|MORE
,"U_INT Type %s is not 8 Bit aligned", mapping_find(encoding_type_m
,int_type
));
332 /* current bit has to be zero for values multiple of 8 bits */
336 /* make sure enough space is available in buffer */
337 status
= this->make_space_available(this,number_of_bits
);
338 if (status
!= SUCCESS
)
342 /* now handle each u int type differently */
347 if (this->current_bit
== 0)
349 /* highval of current byte in buffer has to be set to the new value*/
350 u_int8_t high_val
= *((u_int8_t
*)(this->data_struct
+ offset
)) << 4;
351 /* lowval in buffer is not changed */
352 u_int8_t low_val
= *(this->out_position
) & 0x0F;
353 /* highval is set, low_val is not changed */
354 *(this->out_position
) = high_val
| low_val
;
355 /* write position is not changed, just bit position is moved */
356 this->current_bit
= 4;
358 else if (this->current_bit
== 4)
360 /* highval in buffer is not changed */
361 u_int high_val
= *(this->out_position
) & 0xF0;
362 /* lowval of current byte in buffer has to be set to the new value*/
363 u_int low_val
= *((u_int8_t
*)(this->data_struct
+ offset
)) & 0x0F;
364 *(this->out_position
) = high_val
| low_val
;
365 this->out_position
++;
366 this->current_bit
= 0;
371 this->logger
->log(this->logger
,CONTROL
|MORE
,"U_INT_4 Type is not 4 Bit aligned");
372 /* 4 Bit integers must have a 4 bit alignment */
379 /* 8 bit values are written as they are */
380 *this->out_position
= *((u_int8_t
*)(this->data_struct
+ offset
));
381 this->out_position
++;
387 /* attribute type must not change first bit uf current byte ! */
388 if (this->current_bit
!= 1)
390 this->logger
->log(this->logger
,CONTROL
|MORE
,"ATTRIBUTE FORMAT flag is not set");
391 /* first bit has to be set! */
394 /* get value of attribute format flag */
395 u_int8_t attribute_format_flag
= *(this->out_position
) & 0x80;
396 /* get attribute type value as 16 bit integer*/
397 u_int16_t int16_val
= htons(*((u_int16_t
*)(this->data_struct
+ offset
)));
398 /* last bit must be unset */
399 int16_val
= int16_val
& 0xFF7F;
401 int16_val
= int16_val
| attribute_format_flag
;
402 /* write bytes to buffer (set bit is overwritten)*/
403 this->write_bytes_to_buffer(this,&int16_val
,sizeof(u_int16_t
));
404 this->current_bit
= 0;
410 u_int16_t int16_val
= htons(*((u_int16_t
*)(this->data_struct
+ offset
)));
411 this->write_bytes_to_buffer(this,&int16_val
,sizeof(u_int16_t
));
416 u_int32_t int32_val
= htonl(*((u_int32_t
*)(this->data_struct
+ offset
)));
417 this->write_bytes_to_buffer(this,&int32_val
,sizeof(u_int32_t
));
422 /* 64 bit integers are written as two 32 bit integers */
423 u_int32_t int32_val_low
= htonl(*((u_int32_t
*)(this->data_struct
+ offset
)));
424 u_int32_t int32_val_high
= htonl(*((u_int32_t
*)(this->data_struct
+ offset
) + 1));
425 /* TODO add support for big endian machines */
426 this->write_bytes_to_buffer(this,&int32_val_high
,sizeof(u_int32_t
));
427 this->write_bytes_to_buffer(this,&int32_val_low
,sizeof(u_int32_t
));
432 this->logger
->log(this->logger
,CONTROL
|MORE
,"U_INT Type %s is not supported", mapping_find(encoding_type_m
,int_type
));
439 * Implements private_generator_t's generate_reserved_field function.
440 * See #private_generator_s.generate_reserved_field.
442 static status_t
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
,CONTROL
|MORE
,"Reserved field of %d bits cannot be generated",bits
);
452 /* make sure enough space is available in buffer */
453 status
= this->make_space_available(this,bits
);
454 if (status
!= SUCCESS
)
461 /* one bit processing */
462 u_int8_t reserved_bit
= ~(1 << (7 - this->current_bit
));
463 *(this->out_position
) = *(this->out_position
) & reserved_bit
;
464 if (this->current_bit
== 0)
466 /* memory must be zero */
467 *(this->out_position
) = 0x00;
472 if (this->current_bit
>= 8)
474 this->current_bit
= this->current_bit
% 8;
475 this->out_position
++;
480 /* one byte processing*/
481 if (this->current_bit
> 0)
483 this->logger
->log(this->logger
,CONTROL
|MORE
,"Reserved field cannot be written cause allignement of current bit is %d",this->current_bit
);
486 *(this->out_position
) = 0x00;
487 this->out_position
++;
496 * Implements private_generator_t's generate_flag function.
497 * See #private_generator_s.generate_flag.
499 static status_t
generate_flag (private_generator_t
*this,u_int32_t offset
)
502 /* value of current flag */
504 /* position of flag in current byte */
507 /* if the value in the data_struct is TRUE, flag_value is set to 1, 0 otherwise */
508 flag_value
= (*((bool *) (this->data_struct
+ offset
))) ?
1 : 0;
509 /* get flag position */
510 flag
= (flag_value
<< (7 - this->current_bit
));
512 /* make sure one bit is available in buffer */
513 status
= this->make_space_available(this,1);
514 if (status
!= SUCCESS
)
518 if (this->current_bit
== 0)
520 /* memory must be zero */
521 *(this->out_position
) = 0x00;
524 *(this->out_position
) = *(this->out_position
) | flag
;
527 if (this->current_bit
>= 8)
529 this->current_bit
= this->current_bit
% 8;
530 this->out_position
++;
536 * Implements private_generator_t's generate_from_chunk function.
537 * See #private_generator_s.generate_from_chunk.
539 static status_t
generate_from_chunk (private_generator_t
*this,u_int32_t offset
)
541 if (this->current_bit
!= 0)
543 this->logger
->log(this->logger
,CONTROL
|MORE
,"Chunks can only be taken if bits are alligned in buffer");
546 /* position in buffer */
547 chunk_t
*attribute_value
= (chunk_t
*)(this->data_struct
+ offset
);
549 /* use write_bytes_to_buffer function to do the job */
550 return this->write_bytes_to_buffer(this,attribute_value
->ptr
,attribute_value
->len
);
555 * Implements private_generator_t's generator_context_make_space_available function.
556 * See #private_generator_s.generator_context_make_space_available.
558 static status_t
make_space_available (private_generator_t
*this, size_t bits
)
560 while (((this->get_current_buffer_space(this) * 8) - this->current_bit
) < bits
)
562 /* must increase buffer */
563 u_int8_t
*new_buffer
;
564 size_t old_buffer_size
= this->get_current_buffer_size(this);
565 size_t new_buffer_size
= old_buffer_size
+ GENERATOR_DATA_BUFFER_INCREASE_VALUE
;
566 size_t out_position_offset
= ((this->out_position
) - (this->buffer
));
568 this->logger
->log(this->logger
,CONTROL
|MORE
,"Gen-Buffer is increased from %d to %d byte",old_buffer_size
,new_buffer_size
);
570 /* Reallocate space for new buffer */
571 new_buffer
= allocator_realloc(this->buffer
,new_buffer_size
);
572 if (new_buffer
== NULL
)
574 this->logger
->log(this->logger
,CONTROL
|MORE
,"Fatal error: Reallocation of buffer failed!!!");
578 this->buffer
= new_buffer
;
580 this->out_position
= (this->buffer
+ out_position_offset
);
581 this->roof_position
= (this->buffer
+ new_buffer_size
);
587 * Implements private_generator_t's write_bytes_to_buffer function.
588 * See #private_generator_s.write_bytes_to_buffer.
590 static status_t
write_bytes_to_buffer (private_generator_t
*this,void * bytes
,size_t number_of_bytes
)
594 u_int8_t
*read_position
= (u_int8_t
*) bytes
;
596 status
= this->make_space_available(this,number_of_bytes
* 8);
597 if (status
!= SUCCESS
)
602 for (i
= 0; i
< number_of_bytes
; i
++)
604 *(this->out_position
) = *(read_position
);
606 this->out_position
++;
612 * Implements private_generator_t's write_bytes_to_buffer_at_offset function.
613 * See #private_generator_s.write_bytes_to_buffer_at_offset.
615 static status_t
write_bytes_to_buffer_at_offset (private_generator_t
*this,void * bytes
,size_t number_of_bytes
,u_int32_t offset
)
619 u_int8_t
*read_position
= (u_int8_t
*) bytes
;
620 u_int8_t
*write_position
;
621 u_int32_t free_space_after_offset
= (this->get_current_buffer_size(this) - offset
);
623 /* check first if enough space for new data is available */
624 if (number_of_bytes
> free_space_after_offset
)
626 status
= this->make_space_available(this,(number_of_bytes
- free_space_after_offset
) * 8);
629 write_position
= this->buffer
+ offset
;
630 for (i
= 0; i
< number_of_bytes
; i
++)
632 *(write_position
) = *(read_position
);
640 * Implements generator_t's write_chunk function.
641 * See #generator_s.write_chunk.
643 static status_t
write_to_chunk (private_generator_t
*this,chunk_t
*data
)
645 size_t data_length
= this->get_current_data_length(this);
646 u_int32_t header_length_field
= data_length
;
648 /* write length into header length field */
649 if (this->header_length_position_offset
> 0)
651 u_int32_t int32_val
= htonl(header_length_field
);
652 this->write_bytes_to_buffer_at_offset(this,&int32_val
,sizeof(u_int32_t
),this->header_length_position_offset
);
655 if (this->current_bit
> 0)
657 data
->ptr
= allocator_alloc(data_length
);
658 if (data
->ptr
== NULL
)
661 this->logger
->log(this->logger
,CONTROL
|MORE
,"OUT OF Ressources to wrote data to chunk!!!!!");
664 memcpy(data
->ptr
,this->buffer
,data_length
);
665 data
->len
= data_length
;
670 * Implements generator_t's generate_payload function.
671 * See #generator_s.generate_payload.
673 static status_t
generate_payload (private_generator_t
*this,payload_t
*payload
)
677 this->data_struct
= payload
;
679 encoding_rule_t
*rules
;
680 payload_type_t payload_type
;
682 /* get payload type */
683 payload_type
= payload
->get_type(payload
);
684 /* spi size has to get reseted */
685 this->last_spi_size
= 0;
687 this->logger
->log(this->logger
,CONTROL
,"Start generating payload of type %s",mapping_find(payload_type_m
,payload_type
));
689 /* each payload has its own encoding rules */
690 payload
->get_encoding_rules(payload
,&rules
,&rule_count
);
692 for (i
= 0; i
< rule_count
;i
++)
695 switch (rules
[i
].type
)
697 /* all u int values and ATTRIBUTE_TYPE are generated in generate_u_int_type */
705 status
= this->generate_u_int_type(this,rules
[i
].type
,rules
[i
].offset
);
710 status
= this->generate_reserved_field(this,1);
715 status
= this->generate_reserved_field(this,8);
720 status
= this->generate_flag(this,rules
[i
].offset
);
725 /* position of payload lenght field is temporary stored */
726 this->last_payload_length_position_offset
= this->get_current_buffer_offset(this);
727 /* payload length is generated like an U_INT_16 */
728 status
= this->generate_u_int_type(this,U_INT_16
,rules
[i
].offset
);
733 /* position of header length field is temporary stored */
734 this->header_length_position_offset
= this->get_current_buffer_offset(this);
735 /* header length is generated like an U_INT_32 */
736 status
= this->generate_u_int_type(this,U_INT_32
,rules
[i
].offset
);
740 /* spi size is handled as 8 bit unsigned integer */
741 status
= this->generate_u_int_type(this,U_INT_8
,rules
[i
].offset
);
742 /* last spi size is temporary stored */
743 this->last_spi_size
= *((u_int8_t
*)(this->data_struct
+ rules
[i
].offset
));
747 /* the SPI value is generated from chunk */
748 status
= this->generate_from_chunk(this,rules
[i
].offset
);
751 case KEY_EXCHANGE_DATA
:
753 /* the Key Exchange Data value is generated from chunk */
754 status
= this->generate_from_chunk(this,rules
[i
].offset
);
755 if (status
!= SUCCESS
)
757 this->logger
->log(this->logger
,CONTROL
|MORE
,"Could no write key exchange data from chunk");
761 u_int32_t payload_length_position_offset
= this->last_payload_length_position_offset
;
762 /* Length of KE_PAYLOAD is calculated */
763 u_int16_t length_of_ke_payload
= KE_PAYLOAD_HEADER_LENGTH
+ ((chunk_t
*)(this->data_struct
+ rules
[i
].offset
))->len
;
765 u_int16_t int16_val
= htons(length_of_ke_payload
);
766 status
= this->write_bytes_to_buffer_at_offset(this,&int16_val
,sizeof(u_int16_t
),payload_length_position_offset
);
767 if (status
!= SUCCESS
)
769 this->logger
->log(this->logger
,CONTROL
|MORE
,"Could no write payload length into buffer");
774 case NOTIFICATION_DATA
:
776 /* the Notification Data value is generated from chunk */
777 status
= this->generate_from_chunk(this,rules
[i
].offset
);
778 if (status
!= SUCCESS
)
780 this->logger
->log(this->logger
,CONTROL
|MORE
,"Could no write notification data from chunk");
784 u_int32_t payload_length_position_offset
= this->last_payload_length_position_offset
;
785 /* Length of Notification PAYLOAD is calculated */
786 u_int16_t length_of_notify_payload
= NOTIFY_PAYLOAD_HEADER_LENGTH
+ ((chunk_t
*)(this->data_struct
+ rules
[i
].offset
))->len
;
787 length_of_notify_payload
+= this->last_spi_size
;
788 u_int16_t int16_val
= htons(length_of_notify_payload
);
790 status
= this->write_bytes_to_buffer_at_offset(this,&int16_val
,sizeof(u_int16_t
),payload_length_position_offset
);
791 if (status
!= SUCCESS
)
793 this->logger
->log(this->logger
,CONTROL
|MORE
,"Could no write payload length into buffer");
800 /* the Nonce Data value is generated from chunk */
801 status
= this->generate_from_chunk(this,rules
[i
].offset
);
802 if (status
!= SUCCESS
)
804 this->logger
->log(this->logger
,CONTROL
|MORE
,"Could no write nonce data from chunk");
808 u_int32_t payload_length_position_offset
= this->last_payload_length_position_offset
;
809 /* Length of nonce PAYLOAD is calculated */
810 u_int16_t length_of_nonce_payload
= NONCE_PAYLOAD_HEADER_LENGTH
+ ((chunk_t
*)(this->data_struct
+ rules
[i
].offset
))->len
;
811 u_int16_t int16_val
= htons(length_of_nonce_payload
);
813 status
= this->write_bytes_to_buffer_at_offset(this,&int16_val
,sizeof(u_int16_t
),payload_length_position_offset
);
814 if (status
!= SUCCESS
)
816 this->logger
->log(this->logger
,CONTROL
|MORE
,"Could no write payload length into buffer");
823 /* before iterative generate the transforms, store the current payload length position */
824 u_int32_t payload_length_position_offset
= this->last_payload_length_position_offset
;
825 /* Length of SA_PAYLOAD is calculated */
826 u_int16_t length_of_sa_payload
= SA_PAYLOAD_HEADER_LENGTH
;
828 /* proposals are stored in a linked list and so accessed */
829 linked_list_t
*proposals
= *((linked_list_t
**)(this->data_struct
+ rules
[i
].offset
));
831 this->logger
->log(this->logger
,CONTROL
|MORE
,"Generate Proposals");
833 linked_list_iterator_t
*iterator
;
834 /* create forward iterator */
835 status
= proposals
->create_iterator(proposals
,&iterator
,TRUE
);
836 if (status
!= SUCCESS
)
838 this->logger
->log(this->logger
,CONTROL
|MORE
,"Could not iterator of proposals");
841 /* every proposal is processed (iterative call )*/
842 while (iterator
->has_next(iterator
))
844 payload_t
*current_proposal
;
845 u_int32_t before_generate_position_offset
;
846 u_int32_t after_generate_position_offset
;
848 status
= iterator
->current(iterator
,(void **)¤t_proposal
);
849 if (status
!= SUCCESS
)
851 iterator
->destroy(iterator
);
854 before_generate_position_offset
= this->get_current_buffer_offset(this);
855 status
= this->public.generate_payload(&(this->public),current_proposal
);
856 after_generate_position_offset
= this->get_current_buffer_offset(this);
857 if (status
!= SUCCESS
)
859 iterator
->destroy(iterator
);
863 /* increase size of transform */
864 length_of_sa_payload
+= (after_generate_position_offset
- before_generate_position_offset
);
866 iterator
->destroy(iterator
);
867 this->logger
->log(this->logger
,CONTROL
|MORE
,"Length of Payload is %d, offset is %d",length_of_sa_payload
,payload_length_position_offset
);
869 int16_val
= htons(length_of_sa_payload
);
870 status
= this->write_bytes_to_buffer_at_offset(this,&int16_val
,sizeof(u_int16_t
),payload_length_position_offset
);
871 if (status
!= SUCCESS
)
873 this->logger
->log(this->logger
,CONTROL
|MORE
,"Could no write payload length into buffer");
881 /* before iterative generate the transforms, store the current length position */
882 u_int32_t payload_length_position_offset
= this->last_payload_length_position_offset
;
883 u_int16_t length_of_proposal
= PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH
+ this->last_spi_size
;
885 linked_list_t
*transforms
= *((linked_list_t
**)(this->data_struct
+ rules
[i
].offset
));
886 linked_list_iterator_t
*iterator
;
888 this->logger
->log(this->logger
,CONTROL
|MORE
,"Generate Transforms");
890 /* create forward iterator */
891 status
= transforms
->create_iterator(transforms
,&iterator
,TRUE
);
892 if (status
!= SUCCESS
)
896 while (iterator
->has_next(iterator
))
898 payload_t
*current_transform
;
899 u_int32_t before_generate_position_offset
;
900 u_int32_t after_generate_position_offset
;
902 status
= iterator
->current(iterator
,(void **)¤t_transform
);
903 if (status
!= SUCCESS
)
905 iterator
->destroy(iterator
);
909 before_generate_position_offset
= this->get_current_buffer_offset(this);
910 status
= this->public.generate_payload(&(this->public),current_transform
);
911 after_generate_position_offset
= this->get_current_buffer_offset(this);
912 if (status
!= SUCCESS
)
914 iterator
->destroy(iterator
);
918 /* increase size of transform */
919 length_of_proposal
+= (after_generate_position_offset
- before_generate_position_offset
);
922 iterator
->destroy(iterator
);
924 this->logger
->log(this->logger
,CONTROL
|MORE
,"Length of Transform is %d, offset is %d",length_of_proposal
,payload_length_position_offset
);
926 int16_val
= htons(length_of_proposal
);
927 this->write_bytes_to_buffer_at_offset(this,&int16_val
,sizeof(u_int16_t
),payload_length_position_offset
);
931 case TRANSFORM_ATTRIBUTES
:
933 this->logger
->log(this->logger
,CONTROL
|MORE
,"Generate Transform attributes");
934 /* before iterative generate the transform attributes, store the current length position */
935 u_int32_t transform_length_position_offset
= this->last_payload_length_position_offset
;
937 u_int16_t length_of_transform
= TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH
;
939 linked_list_t
*transform_attributes
=*((linked_list_t
**)(this->data_struct
+ rules
[i
].offset
));
941 linked_list_iterator_t
*iterator
;
942 /* create forward iterator */
943 status
= transform_attributes
->create_iterator(transform_attributes
,&iterator
,TRUE
);
944 if (status
!= SUCCESS
)
948 while (iterator
->has_next(iterator
))
950 payload_t
*current_attribute
;
951 u_int32_t before_generate_position_offset
;
952 u_int32_t after_generate_position_offset
;
954 status
= iterator
->current(iterator
,(void **)¤t_attribute
);
955 if (status
!= SUCCESS
)
957 iterator
->destroy(iterator
);
961 before_generate_position_offset
= this->get_current_buffer_offset(this);
962 this->public.generate_payload(&(this->public),current_attribute
);
963 after_generate_position_offset
= this->get_current_buffer_offset(this);
965 /* increase size of transform */
966 length_of_transform
+= (after_generate_position_offset
- before_generate_position_offset
);
969 iterator
->destroy(iterator
);
971 this->logger
->log(this->logger
,CONTROL
|MORE
,"Length of Transform is %d, offset is %d",length_of_transform
,transform_length_position_offset
);
973 int16_val
= htons(length_of_transform
);
974 this->write_bytes_to_buffer_at_offset(this,&int16_val
,sizeof(u_int16_t
),transform_length_position_offset
);
978 case ATTRIBUTE_FORMAT
:
980 status
= this->generate_flag(this,rules
[i
].offset
);
981 /* Attribute format is a flag which is stored in context*/
982 this->attribute_format
= *((bool *) (this->data_struct
+ rules
[i
].offset
));
986 case ATTRIBUTE_LENGTH_OR_VALUE
:
988 this->logger
->log(this->logger
,CONTROL
|MORE
,"Generate Attribute Length or Value field");
989 if (this->attribute_format
== FALSE
)
991 status
= this->generate_u_int_type(this,U_INT_16
,rules
[i
].offset
);
992 /* this field hold the length of the attribute */
993 this->attribute_length
= *((u_int16_t
*)(this->data_struct
+ rules
[i
].offset
));
997 status
= this->write_bytes_to_buffer(this,(this->data_struct
+ rules
[i
].offset
),2);
1001 case ATTRIBUTE_VALUE
:
1003 if (this->attribute_format
== FALSE
)
1005 this->logger
->log(this->logger
,CONTROL
|MORE
,"Attribute value has not fixed size");
1006 /* the attribute value is generated */
1007 status
= this->generate_from_chunk(this,rules
[i
].offset
);
1012 this->logger
->log(this->logger
,CONTROL
|MORE
,"Field Type %s is not supported",mapping_find(encoding_type_m
,rules
[i
].type
));
1013 return NOT_SUPPORTED
;
1021 * Implements generator_t's destroy function.
1022 * See #generator_s.destroy.
1024 static status_t
destroy(private_generator_t
*this)
1026 allocator_free(this->buffer
);
1027 global_logger_manager
->destroy_logger(global_logger_manager
,this->logger
);
1028 allocator_free(this);
1033 * Described in header
1035 generator_t
* generator_create()
1037 private_generator_t
*this;
1039 this = allocator_alloc_thing(private_generator_t
);
1045 /* initiate public functions */
1046 this->public.generate_payload
= (status_t(*)(generator_t
*, payload_t
*)) generate_payload
;
1047 this->public.destroy
= (status_t(*)(generator_t
*)) destroy
;
1048 this->public.write_to_chunk
= (status_t (*) (generator_t
*,chunk_t
*)) write_to_chunk
;
1051 /* initiate private functions */
1052 this->get_current_buffer_size
= get_current_buffer_size
;
1053 this->get_current_buffer_space
= get_current_buffer_space
;
1054 this->get_current_data_length
= get_current_data_length
;
1055 this->get_current_buffer_offset
= get_current_buffer_offset
;
1056 this->generate_u_int_type
= generate_u_int_type
;
1057 this->generate_reserved_field
= generate_reserved_field
;
1058 this->generate_flag
= generate_flag
;
1059 this->generate_from_chunk
= generate_from_chunk
;
1060 this->make_space_available
= make_space_available
;
1061 this->write_bytes_to_buffer
= write_bytes_to_buffer
;
1062 this->write_bytes_to_buffer_at_offset
= write_bytes_to_buffer_at_offset
;
1065 /* allocate memory for buffer */
1066 this->buffer
= allocator_alloc(GENERATOR_DATA_BUFFER_SIZE
);
1067 if (this->buffer
== NULL
)
1069 allocator_free(this);
1073 /* initiate private variables */
1074 this->out_position
= this->buffer
;
1075 this->roof_position
= this->buffer
+ GENERATOR_DATA_BUFFER_SIZE
;
1076 this->data_struct
= NULL
;
1077 this->current_bit
= 0;
1078 this->last_payload_length_position_offset
= 0;
1079 this->header_length_position_offset
= 0;
1080 this->logger
= global_logger_manager
->create_logger(global_logger_manager
,GENERATOR
,NULL
);
1082 if (this->logger
== NULL
)
1084 allocator_free(this->buffer
);
1085 allocator_free(this);
1088 return &(this->public);