4 * @brief Implementation of generator_t.
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 #include <arpa/inet.h>
30 #include "generator.h"
34 #include <utils/linked_list.h>
35 #include <utils/logger_manager.h>
36 #include <encoding/payloads/payload.h>
37 #include <encoding/payloads/proposal_substructure.h>
38 #include <encoding/payloads/transform_substructure.h>
39 #include <encoding/payloads/sa_payload.h>
40 #include <encoding/payloads/ke_payload.h>
41 #include <encoding/payloads/notify_payload.h>
42 #include <encoding/payloads/nonce_payload.h>
43 #include <encoding/payloads/id_payload.h>
44 #include <encoding/payloads/auth_payload.h>
45 #include <encoding/payloads/cert_payload.h>
46 #include <encoding/payloads/certreq_payload.h>
47 #include <encoding/payloads/ts_payload.h>
48 #include <encoding/payloads/delete_payload.h>
49 #include <encoding/payloads/vendor_id_payload.h>
50 #include <encoding/payloads/cp_payload.h>
51 #include <encoding/payloads/configuration_attribute.h>
52 #include <encoding/payloads/eap_payload.h>
55 typedef struct private_generator_t private_generator_t
;
58 * Private part of a generator_t object.
60 struct private_generator_t
{
62 * Public part of a generator_t object.
67 * Generates a U_INT-Field type and writes it to buffer.
69 * @param this private_generator_t object
70 * @param int_type type of U_INT field (U_INT_4, U_INT_8, etc.)
71 * ATTRIBUTE_TYPE is also generated in this function
72 * @param offset offset of value in data struct
73 * @param generator_contexts generator_contexts_t object where the context is written or read from
76 * - FAILED if allignment is wrong
78 void (*generate_u_int_type
) (private_generator_t
*this,encoding_type_t int_type
,u_int32_t offset
);
81 * Get size of current buffer in bytes.
83 * @param this private_generator_t object
84 * @return Size of buffer in bytes
86 size_t (*get_current_buffer_size
) (private_generator_t
*this);
89 * Get free space of current buffer in bytes.
91 * @param this private_generator_t object
92 * @return space in buffer in bytes
94 size_t (*get_current_buffer_space
) (private_generator_t
*this);
97 * Get length of data in buffer (in bytes).
99 * @param this private_generator_t object
100 * @return length of data in bytes
102 size_t (*get_current_data_length
) (private_generator_t
*this);
105 * Get current offset in buffer (in bytes).
107 * @param this private_generator_t object
108 * @return offset in bytes
110 u_int32_t (*get_current_buffer_offset
) (private_generator_t
*this);
113 * Generates a RESERVED BIT field or a RESERVED BYTE field and writes
116 * @param this private_generator_t object
117 * @param generator_contexts generator_contexts_t object where the context is written or read from
118 * @param bits number of bits to generate
120 void (*generate_reserved_field
) (private_generator_t
*this,int bits
);
123 * Generates a FLAG field.
125 * @param this private_generator_t object
126 * @param generator_contexts generator_contexts_t object where the context is written or read from
127 * @param offset offset of flag value in data struct
129 void (*generate_flag
) (private_generator_t
*this,u_int32_t offset
);
132 * Writes the current buffer content into a chunk_t.
134 * Memory of specific chunk_t gets allocated.
136 * @param this calling private_generator_t object
137 * @param data pointer of chunk_t to write to
139 void (*write_chunk
) (private_generator_t
*this,chunk_t
*data
);
142 * Generates a bytestream from a chunk_t.
144 * @param this private_generator_t object
145 * @param offset offset of chunk_t value in data struct
147 void (*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 void (*make_space_available
) (private_generator_t
*this,size_t bits
);
161 * Writes a specific amount of byte into the buffer.
163 * If buffer is to small to hold the specific amount of bytes it
166 * @param this calling private_generator_t object
167 * @param bytes pointer to bytes to write
168 * @param number_of_bytes number of bytes to write into buffer
170 void (*write_bytes_to_buffer
) (private_generator_t
*this,void * bytes
,size_t number_of_bytes
);
174 * Writes a specific amount of byte into the buffer at a specific offset.
176 * @warning buffer size is not check to hold the data if offset is to large.
178 * @param this calling private_generator_t object
179 * @param bytes pointer to bytes to write
180 * @param number_of_bytes number of bytes to write into buffer
181 * @param offset offset to write the data into
183 void (*write_bytes_to_buffer_at_offset
) (private_generator_t
*this,void * bytes
,size_t number_of_bytes
,u_int32_t offset
);
186 * Buffer used to generate the data into.
191 * Current write position in buffer (one byte aligned).
193 u_int8_t
*out_position
;
196 * Position of last byte in buffer.
198 u_int8_t
*roof_position
;
201 * Current bit writing to in current byte (between 0 and 7).
206 * Associated data struct to read informations from.
211 * Last payload length position offset in the buffer.
213 u_int32_t last_payload_length_position_offset
;
216 * Offset of the header length field in the buffer.
218 u_int32_t header_length_position_offset
;
223 u_int8_t last_spi_size
;
226 * Attribute format of the last generated transform attribute.
228 * Used to check if a variable value field is used or not for
229 * the transform attribute value.
231 bool attribute_format
;
234 * Depending on the value of attribute_format this field is used
235 * to hold the length of the transform attribute in bytes.
237 u_int16_t attribute_length
;
246 * Implementation of private_generator_t.get_current_buffer_size.
248 static size_t get_current_buffer_size (private_generator_t
*this)
250 return ((this->roof_position
) - (this->buffer
));
254 * Implementation of private_generator_t.get_current_buffer_space.
256 static size_t get_current_buffer_space (private_generator_t
*this)
258 /* we know, one byte more */
259 size_t space
= (this->roof_position
) - (this->out_position
);
264 * Implementation of private_generator_t.get_current_data_length.
266 static size_t get_current_data_length (private_generator_t
*this)
268 return (this->out_position
- this->buffer
);
272 * Implementation of private_generator_t.get_current_buffer_offset.
274 static u_int32_t
get_current_buffer_offset (private_generator_t
*this)
276 return (this->out_position
- this->buffer
);
280 * Implementation of private_generator_t.generate_u_int_type.
282 static void generate_u_int_type (private_generator_t
*this,encoding_type_t int_type
,u_int32_t offset
)
284 size_t number_of_bits
= 0;
286 /* find out number of bits of each U_INT type to check for enough space
298 case CONFIGURATION_ATTRIBUTE_LENGTH
:
315 this->logger
->log(this->logger
, ERROR
, "U_INT Type %s is not supported",
316 mapping_find(encoding_type_m
,int_type
));
320 /* U_INT Types of multiple then 8 bits must be aligned */
321 if (((number_of_bits
% 8) == 0) && (this->current_bit
!= 0))
323 this->logger
->log(this->logger
, ERROR
, "U_INT Type %s is not 8 Bit aligned",
324 mapping_find(encoding_type_m
,int_type
));
325 /* current bit has to be zero for values multiple of 8 bits */
329 /* make sure enough space is available in buffer */
330 this->make_space_available(this,number_of_bits
);
331 /* now handle each u int type differently */
336 if (this->current_bit
== 0)
338 /* highval of current byte in buffer has to be set to the new value*/
339 u_int8_t high_val
= *((u_int8_t
*)(this->data_struct
+ offset
)) << 4;
340 /* lowval in buffer is not changed */
341 u_int8_t low_val
= *(this->out_position
) & 0x0F;
342 /* highval is set, low_val is not changed */
343 *(this->out_position
) = high_val
| low_val
;
344 this->logger
->log(this->logger
, RAW
|LEVEL2
, " => %d", *(this->out_position
));
345 /* write position is not changed, just bit position is moved */
346 this->current_bit
= 4;
348 else if (this->current_bit
== 4)
350 /* highval in buffer is not changed */
351 u_int high_val
= *(this->out_position
) & 0xF0;
352 /* lowval of current byte in buffer has to be set to the new value*/
353 u_int low_val
= *((u_int8_t
*)(this->data_struct
+ offset
)) & 0x0F;
354 *(this->out_position
) = high_val
| low_val
;
355 this->logger
->log(this->logger
, RAW
|LEVEL2
, " => %d", *(this->out_position
));
356 this->out_position
++;
357 this->current_bit
= 0;
362 this->logger
->log(this->logger
, ERROR
, "U_INT_4 Type is not 4 Bit aligned");
363 /* 4 Bit integers must have a 4 bit alignment */
371 /* 8 bit values are written as they are */
372 *this->out_position
= *((u_int8_t
*)(this->data_struct
+ offset
));
373 this->logger
->log(this->logger
, RAW
|LEVEL2
, " => %d", *(this->out_position
));
374 this->out_position
++;
380 /* attribute type must not change first bit uf current byte ! */
381 if (this->current_bit
!= 1)
383 this->logger
->log(this->logger
, ERROR
, "ATTRIBUTE FORMAT flag is not set");
384 /* first bit has to be set! */
387 /* get value of attribute format flag */
388 u_int8_t attribute_format_flag
= *(this->out_position
) & 0x80;
389 /* get attribute type value as 16 bit integer*/
390 u_int16_t int16_val
= htons(*((u_int16_t
*)(this->data_struct
+ offset
)));
391 /* last bit must be unset */
392 int16_val
= int16_val
& 0xFF7F;
394 int16_val
= int16_val
| attribute_format_flag
;
395 this->logger
->log(this->logger
, RAW
|LEVEL2
, " => %d", int16_val
);
396 /* write bytes to buffer (set bit is overwritten)*/
397 this->write_bytes_to_buffer(this,&int16_val
,sizeof(u_int16_t
));
398 this->current_bit
= 0;
403 case CONFIGURATION_ATTRIBUTE_LENGTH
:
405 u_int16_t int16_val
= htons(*((u_int16_t
*)(this->data_struct
+ offset
)));
406 this->logger
->log_bytes(this->logger
, RAW
|LEVEL2
, " =>", (void*)&int16_val
, sizeof(int16_val
));
407 this->write_bytes_to_buffer(this,&int16_val
,sizeof(u_int16_t
));
412 u_int32_t int32_val
= htonl(*((u_int32_t
*)(this->data_struct
+ offset
)));
413 this->logger
->log_bytes(this->logger
, RAW
|LEVEL2
, " =>", (void*)&int32_val
, sizeof(int32_val
));
414 this->write_bytes_to_buffer(this,&int32_val
,sizeof(u_int32_t
));
419 /* 64 bit integers are written as two 32 bit integers */
420 u_int32_t int32_val_low
= htonl(*((u_int32_t
*)(this->data_struct
+ offset
)));
421 u_int32_t int32_val_high
= htonl(*((u_int32_t
*)(this->data_struct
+ offset
) + 1));
422 this->logger
->log_bytes(this->logger
, RAW
|LEVEL2
, " => (low)", (void*)&int32_val_low
, sizeof(int32_val_low
));
423 this->logger
->log_bytes(this->logger
, RAW
|LEVEL2
, " => (high)", (void*)&int32_val_high
, sizeof(int32_val_high
));
424 /* TODO add support for big endian machines */
425 this->write_bytes_to_buffer(this,&int32_val_high
,sizeof(u_int32_t
));
426 this->write_bytes_to_buffer(this,&int32_val_low
,sizeof(u_int32_t
));
432 /* 64 bit are written as they come :-) */
433 this->write_bytes_to_buffer(this,(this->data_struct
+ offset
),sizeof(u_int64_t
));
434 this->logger
->log_bytes(this->logger
, RAW
|LEVEL2
, " =>", (void*)(this->data_struct
+ offset
), sizeof(u_int64_t
));
439 this->logger
->log(this->logger
, ERROR
, "U_INT Type %s is not supported", mapping_find(encoding_type_m
,int_type
));
446 * Implementation of private_generator_t.generate_reserved_field.
448 static void generate_reserved_field(private_generator_t
*this,int bits
)
450 /* only one bit or 8 bit fields are supported */
451 if ((bits
!= 1) && (bits
!= 8))
453 this->logger
->log(this->logger
, ERROR
, "Reserved field of %d bits cannot be generated", bits
);
456 /* make sure enough space is available in buffer */
457 this->make_space_available(this,bits
);
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
, ERROR
,
484 "Reserved field cannot be written cause allignement of current bit is %d",
488 *(this->out_position
) = 0x00;
489 this->out_position
++;
494 * Implementation of private_generator_t.generate_flag.
496 static void 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 this->make_space_available(this,1);
510 if (this->current_bit
== 0)
512 /* memory must be zero */
513 *(this->out_position
) = 0x00;
516 *(this->out_position
) = *(this->out_position
) | flag
;
519 this->logger
->log(this->logger
, RAW
|LEVEL2
, " => %d", *(this->out_position
));
522 if (this->current_bit
>= 8)
524 this->current_bit
= this->current_bit
% 8;
525 this->out_position
++;
530 * Implementation of private_generator_t.generate_from_chunk.
532 static void generate_from_chunk (private_generator_t
*this,u_int32_t offset
)
534 if (this->current_bit
!= 0)
536 this->logger
->log(this->logger
, ERROR
, "can not generate a chunk at Bitpos %d", this->current_bit
);
540 /* position in buffer */
541 chunk_t
*attribute_value
= (chunk_t
*)(this->data_struct
+ offset
);
543 this->logger
->log_chunk(this->logger
, RAW
|LEVEL2
, " =>", *attribute_value
);
545 /* use write_bytes_to_buffer function to do the job */
546 this->write_bytes_to_buffer(this,attribute_value
->ptr
,attribute_value
->len
);
550 * Implementation of private_generator_t.make_space_available.
552 static void make_space_available (private_generator_t
*this, size_t bits
)
554 while (((this->get_current_buffer_space(this) * 8) - this->current_bit
) < bits
)
556 /* must increase 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
|LEVEL3
, "increased gen buffer from %d to %d byte",
562 old_buffer_size
, new_buffer_size
);
564 /* Reallocate space for new buffer */
565 this->buffer
= realloc(this->buffer
,new_buffer_size
);
567 this->out_position
= (this->buffer
+ out_position_offset
);
568 this->roof_position
= (this->buffer
+ new_buffer_size
);
573 * Implementation of private_generator_t.write_bytes_to_buffer.
575 static void write_bytes_to_buffer (private_generator_t
*this,void * bytes
, size_t number_of_bytes
)
578 u_int8_t
*read_position
= (u_int8_t
*) bytes
;
580 this->make_space_available(this,number_of_bytes
* 8);
582 for (i
= 0; i
< number_of_bytes
; i
++)
584 *(this->out_position
) = *(read_position
);
586 this->out_position
++;
591 * Implementation of private_generator_t.write_bytes_to_buffer_at_offset.
593 static void write_bytes_to_buffer_at_offset (private_generator_t
*this,void * bytes
,size_t number_of_bytes
,u_int32_t offset
)
596 u_int8_t
*read_position
= (u_int8_t
*) bytes
;
597 u_int8_t
*write_position
;
598 u_int32_t free_space_after_offset
= (this->get_current_buffer_size(this) - offset
);
600 /* check first if enough space for new data is available */
601 if (number_of_bytes
> free_space_after_offset
)
603 this->make_space_available(this,(number_of_bytes
- free_space_after_offset
) * 8);
606 write_position
= this->buffer
+ offset
;
607 for (i
= 0; i
< number_of_bytes
; i
++)
609 *(write_position
) = *(read_position
);
616 * Implementation of private_generator_t.write_to_chunk.
618 static void write_to_chunk (private_generator_t
*this,chunk_t
*data
)
620 size_t data_length
= this->get_current_data_length(this);
621 u_int32_t header_length_field
= data_length
;
623 /* write length into header length field */
624 if (this->header_length_position_offset
> 0)
626 u_int32_t int32_val
= htonl(header_length_field
);
627 this->write_bytes_to_buffer_at_offset(this,&int32_val
,sizeof(u_int32_t
),this->header_length_position_offset
);
630 if (this->current_bit
> 0)
632 data
->ptr
= malloc(data_length
);
633 memcpy(data
->ptr
,this->buffer
,data_length
);
634 data
->len
= data_length
;
636 this->logger
->log_chunk(this->logger
, RAW
|LEVEL3
, "generated data of this generator", *data
);
640 * Implementation of private_generator_t.generate_payload.
642 static void generate_payload (private_generator_t
*this,payload_t
*payload
)
645 this->data_struct
= payload
;
647 encoding_rule_t
*rules
;
648 payload_type_t payload_type
;
649 u_int8_t
*payload_start
;
651 /* get payload type */
652 payload_type
= payload
->get_type(payload
);
653 /* spi size has to get reseted */
654 this->last_spi_size
= 0;
656 payload_start
= this->out_position
;
658 this->logger
->log(this->logger
, CONTROL
|LEVEL1
, "generating payload of type %s",
659 mapping_find(payload_type_m
,payload_type
));
661 /* each payload has its own encoding rules */
662 payload
->get_encoding_rules(payload
,&rules
,&rule_count
);
664 for (i
= 0; i
< rule_count
;i
++)
666 this->logger
->log(this->logger
, CONTROL
|LEVEL2
, " generating rule %d %s",
667 i
, mapping_find(encoding_type_m
,rules
[i
].type
));
668 switch (rules
[i
].type
)
670 /* all u int values, IKE_SPI,TS_TYPE and ATTRIBUTE_TYPE are generated in generate_u_int_type */
679 case CONFIGURATION_ATTRIBUTE_LENGTH
:
681 this->generate_u_int_type(this,rules
[i
].type
,rules
[i
].offset
);
686 this->generate_reserved_field(this,1);
691 this->generate_reserved_field(this,8);
696 this->generate_flag(this,rules
[i
].offset
);
701 /* position of payload lenght field is temporary stored */
702 this->last_payload_length_position_offset
= this->get_current_buffer_offset(this);
703 /* payload length is generated like an U_INT_16 */
704 this->generate_u_int_type(this,U_INT_16
,rules
[i
].offset
);
709 /* position of header length field is temporary stored */
710 this->header_length_position_offset
= this->get_current_buffer_offset(this);
711 /* header length is generated like an U_INT_32 */
712 this->generate_u_int_type(this,U_INT_32
,rules
[i
].offset
);
716 /* spi size is handled as 8 bit unsigned integer */
717 this->generate_u_int_type(this,U_INT_8
,rules
[i
].offset
);
718 /* last spi size is temporary stored */
719 this->last_spi_size
= *((u_int8_t
*)(this->data_struct
+ rules
[i
].offset
));
723 /* the Address value is generated from chunk */
724 this->generate_from_chunk(this,rules
[i
].offset
);
729 /* the SPI value is generated from chunk */
730 this->generate_from_chunk(this,rules
[i
].offset
);
733 case KEY_EXCHANGE_DATA
:
734 case NOTIFICATION_DATA
:
741 case CONFIGURATION_ATTRIBUTE_VALUE
:
745 u_int32_t payload_length_position_offset
;
746 u_int16_t length_of_payload
;
747 u_int16_t header_length
= 0;
748 u_int16_t length_in_network_order
;
750 switch(rules
[i
].type
)
752 case KEY_EXCHANGE_DATA
:
753 header_length
= KE_PAYLOAD_HEADER_LENGTH
;
755 case NOTIFICATION_DATA
:
756 header_length
= NOTIFY_PAYLOAD_HEADER_LENGTH
+ this->last_spi_size
;
759 header_length
= NONCE_PAYLOAD_HEADER_LENGTH
;
762 header_length
= ID_PAYLOAD_HEADER_LENGTH
;
765 header_length
= AUTH_PAYLOAD_HEADER_LENGTH
;
768 header_length
= CERT_PAYLOAD_HEADER_LENGTH
;
771 header_length
= CERTREQ_PAYLOAD_HEADER_LENGTH
;
774 header_length
= DELETE_PAYLOAD_HEADER_LENGTH
;
777 header_length
= VENDOR_ID_PAYLOAD_HEADER_LENGTH
;
779 case CONFIGURATION_ATTRIBUTE_VALUE
:
780 header_length
= CONFIGURATION_ATTRIBUTE_HEADER_LENGTH
;
783 header_length
= EAP_PAYLOAD_HEADER_LENGTH
;
789 /* the data value is generated from chunk */
790 this->generate_from_chunk(this,rules
[i
].offset
);
792 payload_length_position_offset
= this->last_payload_length_position_offset
;
795 /* Length of payload is calculated */
796 length_of_payload
= header_length
+ ((chunk_t
*)(this->data_struct
+ rules
[i
].offset
))->len
;
798 length_in_network_order
= htons(length_of_payload
);
799 this->write_bytes_to_buffer_at_offset(this,&length_in_network_order
,sizeof(u_int16_t
),payload_length_position_offset
);
804 /* before iterative generate the transforms, store the current payload length position */
805 u_int32_t payload_length_position_offset
= this->last_payload_length_position_offset
;
806 /* Length of SA_PAYLOAD is calculated */
807 u_int16_t length_of_sa_payload
= SA_PAYLOAD_HEADER_LENGTH
;
809 /* proposals are stored in a linked list and so accessed */
810 linked_list_t
*proposals
= *((linked_list_t
**)(this->data_struct
+ rules
[i
].offset
));
812 iterator_t
*iterator
;
813 /* create forward iterator */
814 iterator
= proposals
->create_iterator(proposals
,TRUE
);
815 /* every proposal is processed (iterative call )*/
816 while (iterator
->has_next(iterator
))
818 payload_t
*current_proposal
;
819 u_int32_t before_generate_position_offset
;
820 u_int32_t after_generate_position_offset
;
822 iterator
->current(iterator
,(void **)¤t_proposal
);
824 before_generate_position_offset
= this->get_current_buffer_offset(this);
825 this->public.generate_payload(&(this->public),current_proposal
);
826 after_generate_position_offset
= this->get_current_buffer_offset(this);
828 /* increase size of transform */
829 length_of_sa_payload
+= (after_generate_position_offset
- before_generate_position_offset
);
831 iterator
->destroy(iterator
);
833 int16_val
= htons(length_of_sa_payload
);
834 this->write_bytes_to_buffer_at_offset(this,&int16_val
,sizeof(u_int16_t
),payload_length_position_offset
);
839 /* before iterative generate the transforms, store the current length position */
840 u_int32_t payload_length_position_offset
= this->last_payload_length_position_offset
;
841 u_int16_t length_of_proposal
= PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH
+ this->last_spi_size
;
843 linked_list_t
*transforms
= *((linked_list_t
**)(this->data_struct
+ rules
[i
].offset
));
844 iterator_t
*iterator
;
846 /* create forward iterator */
847 iterator
= transforms
->create_iterator(transforms
,TRUE
);
848 while (iterator
->has_next(iterator
))
850 payload_t
*current_transform
;
851 u_int32_t before_generate_position_offset
;
852 u_int32_t after_generate_position_offset
;
854 iterator
->current(iterator
,(void **)¤t_transform
);
856 before_generate_position_offset
= this->get_current_buffer_offset(this);
857 this->public.generate_payload(&(this->public),current_transform
);
858 after_generate_position_offset
= this->get_current_buffer_offset(this);
860 /* increase size of transform */
861 length_of_proposal
+= (after_generate_position_offset
- before_generate_position_offset
);
864 iterator
->destroy(iterator
);
866 int16_val
= htons(length_of_proposal
);
867 this->write_bytes_to_buffer_at_offset(this,&int16_val
,sizeof(u_int16_t
),payload_length_position_offset
);
871 case TRANSFORM_ATTRIBUTES
:
873 /* before iterative generate the transform attributes, store the current length position */
874 u_int32_t transform_length_position_offset
= this->last_payload_length_position_offset
;
876 u_int16_t length_of_transform
= TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH
;
878 linked_list_t
*transform_attributes
=*((linked_list_t
**)(this->data_struct
+ rules
[i
].offset
));
880 iterator_t
*iterator
;
881 /* create forward iterator */
882 iterator
= transform_attributes
->create_iterator(transform_attributes
,TRUE
);
883 while (iterator
->has_next(iterator
))
885 payload_t
*current_attribute
;
886 u_int32_t before_generate_position_offset
;
887 u_int32_t after_generate_position_offset
;
889 iterator
->current(iterator
,(void **)¤t_attribute
);
891 before_generate_position_offset
= this->get_current_buffer_offset(this);
892 this->public.generate_payload(&(this->public),current_attribute
);
893 after_generate_position_offset
= this->get_current_buffer_offset(this);
895 /* increase size of transform */
896 length_of_transform
+= (after_generate_position_offset
- before_generate_position_offset
);
899 iterator
->destroy(iterator
);
901 int16_val
= htons(length_of_transform
);
902 this->write_bytes_to_buffer_at_offset(this,&int16_val
,sizeof(u_int16_t
),transform_length_position_offset
);
906 case CONFIGURATION_ATTRIBUTES
:
908 /* before iterative generate the configuration attributes, store the current length position */
909 u_int32_t configurations_length_position_offset
= this->last_payload_length_position_offset
;
911 u_int16_t length_of_configurations
= CP_PAYLOAD_HEADER_LENGTH
;
913 linked_list_t
*configuration_attributes
=*((linked_list_t
**)(this->data_struct
+ rules
[i
].offset
));
915 iterator_t
*iterator
;
916 /* create forward iterator */
917 iterator
= configuration_attributes
->create_iterator(configuration_attributes
,TRUE
);
918 while (iterator
->has_next(iterator
))
920 payload_t
*current_attribute
;
921 u_int32_t before_generate_position_offset
;
922 u_int32_t after_generate_position_offset
;
924 iterator
->current(iterator
,(void **)¤t_attribute
);
926 before_generate_position_offset
= this->get_current_buffer_offset(this);
927 this->public.generate_payload(&(this->public),current_attribute
);
928 after_generate_position_offset
= this->get_current_buffer_offset(this);
930 /* increase size of transform */
931 length_of_configurations
+= (after_generate_position_offset
- before_generate_position_offset
);
934 iterator
->destroy(iterator
);
936 int16_val
= htons(length_of_configurations
);
937 this->write_bytes_to_buffer_at_offset(this,&int16_val
,sizeof(u_int16_t
),configurations_length_position_offset
);
941 case ATTRIBUTE_FORMAT
:
943 this->generate_flag(this,rules
[i
].offset
);
944 /* Attribute format is a flag which is stored in context*/
945 this->attribute_format
= *((bool *) (this->data_struct
+ rules
[i
].offset
));
949 case ATTRIBUTE_LENGTH_OR_VALUE
:
951 if (this->attribute_format
== FALSE
)
953 this->generate_u_int_type(this,U_INT_16
,rules
[i
].offset
);
954 /* this field hold the length of the attribute */
955 this->attribute_length
= *((u_int16_t
*)(this->data_struct
+ rules
[i
].offset
));
959 this->generate_u_int_type(this,U_INT_16
,rules
[i
].offset
);
963 case ATTRIBUTE_VALUE
:
965 if (this->attribute_format
== FALSE
)
967 this->logger
->log(this->logger
, CONTROL
|LEVEL3
, "attribute value has not fixed size");
968 /* the attribute value is generated */
969 this->generate_from_chunk(this,rules
[i
].offset
);
973 case TRAFFIC_SELECTORS
:
975 /* before iterative generate the traffic_selectors, store the current payload length position */
976 u_int32_t payload_length_position_offset
= this->last_payload_length_position_offset
;
977 /* Length of SA_PAYLOAD is calculated */
978 u_int16_t length_of_ts_payload
= TS_PAYLOAD_HEADER_LENGTH
;
980 /* traffic selectors are stored in a linked list and so accessed */
981 linked_list_t
*traffic_selectors
= *((linked_list_t
**)(this->data_struct
+ rules
[i
].offset
));
983 iterator_t
*iterator
;
984 /* create forward iterator */
985 iterator
= traffic_selectors
->create_iterator(traffic_selectors
,TRUE
);
986 /* every proposal is processed (iterative call )*/
987 while (iterator
->has_next(iterator
))
989 payload_t
*current_traffic_selector_substructure
;
990 u_int32_t before_generate_position_offset
;
991 u_int32_t after_generate_position_offset
;
993 iterator
->current(iterator
,(void **)¤t_traffic_selector_substructure
);
995 before_generate_position_offset
= this->get_current_buffer_offset(this);
996 this->public.generate_payload(&(this->public),current_traffic_selector_substructure
);
997 after_generate_position_offset
= this->get_current_buffer_offset(this);
999 /* increase size of transform */
1000 length_of_ts_payload
+= (after_generate_position_offset
- before_generate_position_offset
);
1002 iterator
->destroy(iterator
);
1004 int16_val
= htons(length_of_ts_payload
);
1005 this->write_bytes_to_buffer_at_offset(this,&int16_val
,sizeof(u_int16_t
),payload_length_position_offset
);
1009 case ENCRYPTED_DATA
:
1011 this->generate_from_chunk(this, rules
[i
].offset
);
1015 this->logger
->log(this->logger
, ERROR
, "field type %s is not supported",
1016 mapping_find(encoding_type_m
,rules
[i
].type
));
1020 this->logger
->log(this->logger
, CONTROL
|LEVEL2
, "generating %s payload finished.",
1021 mapping_find(payload_type_m
, payload_type
));
1022 this->logger
->log_bytes(this->logger
, RAW
|LEVEL3
, "generated data for this payload",
1023 payload_start
, this->out_position
-payload_start
);
1027 * Implementation of generator_t.destroy.
1029 static status_t
destroy(private_generator_t
*this)
1037 * Described in header
1039 generator_t
*generator_create()
1041 private_generator_t
*this;
1043 this = malloc_thing(private_generator_t
);
1045 /* initiate public functions */
1046 this->public.generate_payload
= (void(*)(generator_t
*, payload_t
*)) generate_payload
;
1047 this->public.destroy
= (void(*)(generator_t
*)) destroy
;
1048 this->public.write_to_chunk
= (void (*) (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
= malloc(GENERATOR_DATA_BUFFER_SIZE
);
1068 /* initiate private variables */
1069 this->out_position
= this->buffer
;
1070 this->roof_position
= this->buffer
+ GENERATOR_DATA_BUFFER_SIZE
;
1071 this->data_struct
= NULL
;
1072 this->current_bit
= 0;
1073 this->last_payload_length_position_offset
= 0;
1074 this->header_length_position_offset
= 0;
1075 this->logger
= logger_manager
->get_logger(logger_manager
, GENERATOR
);
1077 return &(this->public);