cleaning up
[strongswan.git] / src / charon / encoding / generator.c
index 34e87fa..efa845b 100644 (file)
 
 #include "generator.h"
 
-#include <types.h>
+#include <library.h>
 #include <daemon.h>
 #include <utils/linked_list.h>
-#include <utils/logger_manager.h>
 #include <encoding/payloads/payload.h>
 #include <encoding/payloads/proposal_substructure.h>
 #include <encoding/payloads/transform_substructure.h>
@@ -222,24 +221,19 @@ struct private_generator_t {
         */
        u_int8_t last_spi_size;
        
-       /*
+       /**
         * Attribute format of the last generated transform attribute.
-        * 
+        *
         * Used to check if a variable value field is used or not for 
         * the transform attribute value.
         */
        bool attribute_format;
        
-       /*
+       /**
         * Depending on the value of attribute_format this field is used
         * to hold the length of the transform attribute in bytes.
         */
        u_int16_t attribute_length;
-       
-       /**
-        * Associated Logger.
-        */
-       logger_t *logger;
 };
 
 /**
@@ -312,16 +306,16 @@ static void generate_u_int_type (private_generator_t *this,encoding_type_t int_t
                                break;
 
                        default:
-                       this->logger->log(this->logger, ERROR, "U_INT Type %s is not supported", 
-                                                       mapping_find(encoding_type_m,int_type));
+                       DBG1(DBG_ENC, "U_INT Type %N is not supported",
+                                encoding_type_names, int_type);
 
                        return;
        }
        /* U_INT Types of multiple then 8 bits must be aligned */
        if (((number_of_bits % 8) == 0) && (this->current_bit != 0))
        {
-               this->logger->log(this->logger, ERROR, "U_INT Type %s is not 8 Bit aligned", 
-                                                       mapping_find(encoding_type_m,int_type));
+               DBG1(DBG_ENC, "U_INT Type %N is not 8 Bit aligned",
+                        encoding_type_names, int_type);
                /* current bit has to be zero for values multiple of 8 bits */
                return;
        }
@@ -341,7 +335,7 @@ static void generate_u_int_type (private_generator_t *this,encoding_type_t int_t
                                u_int8_t low_val = *(this->out_position) & 0x0F;
                                /* highval is set, low_val is not changed */
                                *(this->out_position) = high_val | low_val;
-                               this->logger->log(this->logger, RAW|LEVEL2, "   => %d", *(this->out_position));
+                               DBG3(DBG_ENC, "   => %d", *(this->out_position));
                                /* write position is not changed, just bit position is moved */
                                this->current_bit = 4;
                        }
@@ -352,14 +346,14 @@ static void generate_u_int_type (private_generator_t *this,encoding_type_t int_t
                                /* lowval of current byte in buffer has to be set to the new value*/
                                u_int low_val = *((u_int8_t *)(this->data_struct + offset)) & 0x0F;
                                *(this->out_position) = high_val | low_val;
-                               this->logger->log(this->logger, RAW|LEVEL2, "   => %d", *(this->out_position));
+                               DBG3(DBG_ENC, "   => %d", *(this->out_position));
                                this->out_position++;
                                this->current_bit = 0;
 
                        }
                        else
                        {
-                               this->logger->log(this->logger, ERROR, "U_INT_4 Type is not 4 Bit aligned");
+                               DBG1(DBG_ENC, "U_INT_4 Type is not 4 Bit aligned");
                                /* 4 Bit integers must have a 4 bit alignment */
                                return;
                        };
@@ -370,7 +364,7 @@ static void generate_u_int_type (private_generator_t *this,encoding_type_t int_t
                {
                        /* 8 bit values are written as they are */
                        *this->out_position = *((u_int8_t *)(this->data_struct + offset));
-                       this->logger->log(this->logger, RAW|LEVEL2, "   => %d", *(this->out_position));
+                       DBG3(DBG_ENC, "   => %d", *(this->out_position));
                        this->out_position++;
                        break;
 
@@ -380,19 +374,22 @@ static void generate_u_int_type (private_generator_t *this,encoding_type_t int_t
                        /* attribute type must not change first bit uf current byte ! */
                        if (this->current_bit != 1)
                        {
-                               this->logger->log(this->logger, ERROR, "ATTRIBUTE FORMAT flag is not set");
+                               DBG1(DBG_ENC, "ATTRIBUTE FORMAT flag is not set");
                                /* first bit has to be set! */
                                return;
                        }
                        /* get value of attribute format flag */
                        u_int8_t attribute_format_flag = *(this->out_position) & 0x80;
                        /* get attribute type value as 16 bit integer*/
-                       u_int16_t int16_val = htons(*((u_int16_t*)(this->data_struct + offset)));
-                       /* last bit must be unset */
-                       int16_val = int16_val & 0xFF7F;
-                       
-                       int16_val = int16_val | attribute_format_flag;
-                       this->logger->log(this->logger, RAW|LEVEL2, "   => %d", int16_val);
+                       u_int16_t int16_val = *((u_int16_t*)(this->data_struct + offset));
+                       /* unset most significant bit */
+                       int16_val &= 0x7FFF;
+                       if (attribute_format_flag)
+                       {
+                               int16_val |= 0x8000;
+                       }
+                       int16_val = htons(int16_val);
+                       DBG3(DBG_ENC, "   => %d", int16_val);
                        /* write bytes to buffer (set bit is overwritten)*/                             
                        this->write_bytes_to_buffer(this,&int16_val,sizeof(u_int16_t));
                        this->current_bit = 0;
@@ -403,14 +400,14 @@ static void generate_u_int_type (private_generator_t *this,encoding_type_t int_t
                case CONFIGURATION_ATTRIBUTE_LENGTH:
                {
                        u_int16_t int16_val = htons(*((u_int16_t*)(this->data_struct + offset)));
-                       this->logger->log_bytes(this->logger, RAW|LEVEL2, "   =>", (void*)&int16_val, sizeof(int16_val));
+                       DBG3(DBG_ENC, "   => %b", (void*)&int16_val, sizeof(int16_val));
                        this->write_bytes_to_buffer(this,&int16_val,sizeof(u_int16_t));
                        break;
                }
                case U_INT_32:
                {
                        u_int32_t int32_val = htonl(*((u_int32_t*)(this->data_struct + offset)));
-                       this->logger->log_bytes(this->logger, RAW|LEVEL2, "   =>", (void*)&int32_val, sizeof(int32_val));
+                       DBG3(DBG_ENC, "   => %b", (void*)&int32_val, sizeof(int32_val));
                        this->write_bytes_to_buffer(this,&int32_val,sizeof(u_int32_t));
                        break;
                }
@@ -419,8 +416,9 @@ static void generate_u_int_type (private_generator_t *this,encoding_type_t int_t
                        /* 64 bit integers are written as two 32 bit integers */
                        u_int32_t int32_val_low = htonl(*((u_int32_t*)(this->data_struct + offset)));
                        u_int32_t int32_val_high = htonl(*((u_int32_t*)(this->data_struct + offset) + 1));
-                       this->logger->log_bytes(this->logger, RAW|LEVEL2, "   => (low)", (void*)&int32_val_low, sizeof(int32_val_low));
-                       this->logger->log_bytes(this->logger, RAW|LEVEL2, "   => (high)", (void*)&int32_val_high, sizeof(int32_val_high));
+                       DBG3(DBG_ENC, "   => %b %b",
+                                (void*)&int32_val_low, sizeof(int32_val_low),
+                                (void*)&int32_val_high, sizeof(int32_val_high));
                        /* TODO add support for big endian machines */
                        this->write_bytes_to_buffer(this,&int32_val_high,sizeof(u_int32_t));
                        this->write_bytes_to_buffer(this,&int32_val_low,sizeof(u_int32_t));
@@ -431,12 +429,13 @@ static void generate_u_int_type (private_generator_t *this,encoding_type_t int_t
                {
                        /* 64 bit are written as they come :-) */
                        this->write_bytes_to_buffer(this,(this->data_struct + offset),sizeof(u_int64_t));
-                       this->logger->log_bytes(this->logger, RAW|LEVEL2, "   =>", (void*)(this->data_struct + offset), sizeof(u_int64_t));
+                       DBG3(DBG_ENC, "   => %b", (void*)(this->data_struct + offset), sizeof(u_int64_t));
                        break;
                }
                default:
                {
-                       this->logger->log(this->logger, ERROR, "U_INT Type %s is not supported", mapping_find(encoding_type_m,int_type));
+                       DBG1(DBG_ENC, "U_INT Type %N is not supported",
+                                encoding_type_names, int_type);
                        return;
                }
        }
@@ -450,7 +449,7 @@ static void generate_reserved_field(private_generator_t *this,int bits)
        /* only one bit or 8 bit fields are supported */
        if ((bits != 1) && (bits != 8))
        {
-               this->logger->log(this->logger, ERROR, "Reserved field of %d bits cannot be generated", bits);
+               DBG1(DBG_ENC, "reserved field of %d bits cannot be generated", bits);
                return ;
        }
        /* make sure enough space is available in buffer */
@@ -480,9 +479,8 @@ static void generate_reserved_field(private_generator_t *this,int bits)
                /* one byte processing*/
                if (this->current_bit > 0)
                {
-                       this->logger->log(this->logger, ERROR, 
-                                                               "Reserved field cannot be written cause allignement of current bit is %d",
-                                                               this->current_bit);
+                       DBG1(DBG_ENC, "reserved field cannot be written cause "
+                                "alignement of current bit is %d", this->current_bit);
                        return;
                }
                *(this->out_position) = 0x00;
@@ -516,7 +514,7 @@ static void generate_flag (private_generator_t *this,u_int32_t offset)
        *(this->out_position) = *(this->out_position) | flag;
        
        
-       this->logger->log(this->logger, RAW|LEVEL2, "   => %d", *(this->out_position));
+       DBG3(DBG_ENC, "   => %d", *(this->out_position));
 
        this->current_bit++;
        if (this->current_bit >= 8)
@@ -533,14 +531,14 @@ static void generate_from_chunk (private_generator_t *this,u_int32_t offset)
 {
        if (this->current_bit != 0)
        {
-               this->logger->log(this->logger, ERROR, "can not generate a chunk at Bitpos %d", this->current_bit);
+               DBG1(DBG_ENC, "can not generate a chunk at Bitpos %d", this->current_bit);
                return ;
        }
        
        /* position in buffer */
        chunk_t *attribute_value = (chunk_t *)(this->data_struct + offset);
        
-       this->logger->log_chunk(this->logger, RAW|LEVEL2, "   =>", *attribute_value);
+       DBG3(DBG_ENC, "   => %B", attribute_value);
        
        /* use write_bytes_to_buffer function to do the job */
        this->write_bytes_to_buffer(this,attribute_value->ptr,attribute_value->len);
@@ -558,8 +556,8 @@ static void make_space_available (private_generator_t *this, size_t bits)
                size_t new_buffer_size = old_buffer_size + GENERATOR_DATA_BUFFER_INCREASE_VALUE;
                size_t out_position_offset = ((this->out_position) - (this->buffer));
 
-               this->logger->log(this->logger, CONTROL|LEVEL3, "increased gen buffer from %d to %d byte", 
-                                                       old_buffer_size, new_buffer_size);
+               DBG2(DBG_ENC, "increased gen buffer from %d to %d byte", 
+                        old_buffer_size, new_buffer_size);
                
                /* Reallocate space for new buffer */
                this->buffer = realloc(this->buffer,new_buffer_size);
@@ -633,7 +631,7 @@ static void write_to_chunk (private_generator_t *this,chunk_t *data)
        memcpy(data->ptr,this->buffer,data_length);
        data->len = data_length;
        
-       this->logger->log_chunk(this->logger, RAW|LEVEL3, "generated data of this generator", *data);
+       DBG3(DBG_ENC, "generated data of this generator %B", data);
 }
 
 /**
@@ -655,16 +653,16 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
        
        payload_start = this->out_position;
        
-       this->logger->log(this->logger, CONTROL|LEVEL1, "generating payload of type %s",
-                                                                                       mapping_find(payload_type_m,payload_type));
+       DBG2(DBG_ENC, "generating payload of type %N",
+                payload_type_names, payload_type);
        
        /* each payload has its own encoding rules */
        payload->get_encoding_rules(payload,&rules,&rule_count);
 
        for (i = 0; i < rule_count;i++)
        {
-               this->logger->log(this->logger, CONTROL|LEVEL2, "  generating rule %d %s",
-                                                       i, mapping_find(encoding_type_m,rules[i].type));
+               DBG2(DBG_ENC, "  generating rule %d %N",
+                        i, encoding_type_names, rules[i].type);
                switch (rules[i].type)
                {
                        /* all u int values, IKE_SPI,TS_TYPE and ATTRIBUTE_TYPE are generated in generate_u_int_type */
@@ -740,7 +738,7 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
                        case SPIS:
                        case CONFIGURATION_ATTRIBUTE_VALUE:
                        case VID_DATA:
-                       case EAP_MESSAGE:
+                       case EAP_DATA:
                        {
                                u_int32_t payload_length_position_offset;
                                u_int16_t length_of_payload;
@@ -779,7 +777,7 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
                                        case CONFIGURATION_ATTRIBUTE_VALUE:
                                                header_length = CONFIGURATION_ATTRIBUTE_HEADER_LENGTH;
                                                break;
-                                       case EAP_MESSAGE:
+                                       case EAP_DATA:
                                                header_length = EAP_PAYLOAD_HEADER_LENGTH;
                                                break;
                                        default:
@@ -808,19 +806,17 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
                                u_int16_t int16_val;
                                /* proposals are stored in a linked list and so accessed */
                                linked_list_t *proposals = *((linked_list_t **)(this->data_struct + rules[i].offset));
-
                                iterator_t *iterator;
+                               payload_t *current_proposal;
+                               
                                /* create forward iterator */
                                iterator = proposals->create_iterator(proposals,TRUE);
                                /* every proposal is processed (iterative call )*/
-                               while (iterator->has_next(iterator))
+                               while (iterator->iterate(iterator, (void**)&current_proposal))
                                {
-                                       payload_t *current_proposal;
                                        u_int32_t before_generate_position_offset;
                                        u_int32_t after_generate_position_offset;
                                        
-                                       iterator->current(iterator,(void **)&current_proposal);
-
                                        before_generate_position_offset = this->get_current_buffer_offset(this);
                                        this->public.generate_payload(&(this->public),current_proposal);
                                        after_generate_position_offset = this->get_current_buffer_offset(this);
@@ -833,26 +829,24 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
                                int16_val = htons(length_of_sa_payload);
                                this->write_bytes_to_buffer_at_offset(this,&int16_val,sizeof(u_int16_t),payload_length_position_offset);
                                break;
-                       }       
+                       }
                        case TRANSFORMS:
-                       {       
+                       {
                                /* before iterative generate the transforms, store the current length position */
                                u_int32_t payload_length_position_offset = this->last_payload_length_position_offset;
                                u_int16_t length_of_proposal = PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH + this->last_spi_size;
                                u_int16_t int16_val;
                                linked_list_t *transforms = *((linked_list_t **)(this->data_struct + rules[i].offset));
                                iterator_t *iterator;
-                                                               
+                               payload_t *current_transform;
+                               
                                /* create forward iterator */
                                iterator = transforms->create_iterator(transforms,TRUE);
-                               while (iterator->has_next(iterator))
+                               while (iterator->iterate(iterator, (void**)&current_transform))
                                {
-                                       payload_t *current_transform;
                                        u_int32_t before_generate_position_offset;
                                        u_int32_t after_generate_position_offset;
                                        
-                                       iterator->current(iterator,(void **)&current_transform);
-                                       
                                        before_generate_position_offset = this->get_current_buffer_offset(this);
                                        this->public.generate_payload(&(this->public),current_transform);
                                        after_generate_position_offset = this->get_current_buffer_offset(this);
@@ -862,7 +856,7 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
                                }
                                
                                iterator->destroy(iterator);
-                                                               
+                               
                                int16_val = htons(length_of_proposal);
                                this->write_bytes_to_buffer_at_offset(this,&int16_val,sizeof(u_int16_t),payload_length_position_offset);
                                
@@ -872,22 +866,19 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
                        {
                                /* before iterative generate the transform attributes, store the current length position */
                                u_int32_t transform_length_position_offset = this->last_payload_length_position_offset;
-
                                u_int16_t length_of_transform = TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH;
                                u_int16_t int16_val;
                                linked_list_t *transform_attributes =*((linked_list_t **)(this->data_struct + rules[i].offset));
-
                                iterator_t *iterator;
+                               payload_t *current_attribute;
+                               
                                /* create forward iterator */
                                iterator = transform_attributes->create_iterator(transform_attributes,TRUE);
-                               while (iterator->has_next(iterator))
+                               while (iterator->iterate(iterator, (void**)&current_attribute))
                                {
-                                       payload_t *current_attribute;
                                        u_int32_t before_generate_position_offset;
                                        u_int32_t after_generate_position_offset;
                                        
-                                       iterator->current(iterator,(void **)&current_attribute);
-                                       
                                        before_generate_position_offset = this->get_current_buffer_offset(this);
                                        this->public.generate_payload(&(this->public),current_attribute);
                                        after_generate_position_offset = this->get_current_buffer_offset(this);
@@ -907,22 +898,19 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
                        {
                                /* before iterative generate the configuration attributes, store the current length position */
                                u_int32_t configurations_length_position_offset = this->last_payload_length_position_offset;
-
                                u_int16_t length_of_configurations = CP_PAYLOAD_HEADER_LENGTH;
                                u_int16_t int16_val;
                                linked_list_t *configuration_attributes =*((linked_list_t **)(this->data_struct + rules[i].offset));
-
                                iterator_t *iterator;
+                               payload_t *current_attribute;
+                               
                                /* create forward iterator */
                                iterator = configuration_attributes->create_iterator(configuration_attributes,TRUE);
-                               while (iterator->has_next(iterator))
+                               while (iterator->iterate(iterator, (void**)&current_attribute))
                                {
-                                       payload_t *current_attribute;
                                        u_int32_t before_generate_position_offset;
                                        u_int32_t after_generate_position_offset;
                                        
-                                       iterator->current(iterator,(void **)&current_attribute);
-                                       
                                        before_generate_position_offset = this->get_current_buffer_offset(this);
                                        this->public.generate_payload(&(this->public),current_attribute);
                                        after_generate_position_offset = this->get_current_buffer_offset(this);
@@ -964,7 +952,7 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
                        {
                                if (this->attribute_format == FALSE)
                                {
-                                       this->logger->log(this->logger, CONTROL|LEVEL3, "attribute value has not fixed size");
+                                       DBG2(DBG_ENC, "attribute value has not fixed size");
                                        /* the attribute value is generated */
                                        this->generate_from_chunk(this,rules[i].offset);
                                }
@@ -979,18 +967,16 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
                                u_int16_t int16_val;
                                /* traffic selectors are stored in a linked list and so accessed */
                                linked_list_t *traffic_selectors = *((linked_list_t **)(this->data_struct + rules[i].offset));
-
                                iterator_t *iterator;
+                               payload_t *current_traffic_selector_substructure;
+                               
                                /* create forward iterator */
                                iterator = traffic_selectors->create_iterator(traffic_selectors,TRUE);
                                /* every proposal is processed (iterative call )*/
-                               while (iterator->has_next(iterator))
+                               while (iterator->iterate(iterator, (void **)&current_traffic_selector_substructure))
                                {
-                                       payload_t *current_traffic_selector_substructure;
                                        u_int32_t before_generate_position_offset;
                                        u_int32_t after_generate_position_offset;
-                                       
-                                       iterator->current(iterator,(void **)&current_traffic_selector_substructure);
 
                                        before_generate_position_offset = this->get_current_buffer_offset(this);
                                        this->public.generate_payload(&(this->public),current_traffic_selector_substructure);
@@ -1012,15 +998,15 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
                                break;
                        }
                        default:
-                               this->logger->log(this->logger, ERROR, "field type %s is not supported",
-                                                                       mapping_find(encoding_type_m,rules[i].type));
+                               DBG1(DBG_ENC, "field type %N is not supported",
+                                        encoding_type_names, rules[i].type);
                                return;
                }
        }
-       this->logger->log(this->logger, CONTROL|LEVEL2, "generating %s payload finished.", 
-                                         mapping_find(payload_type_m, payload_type));
-       this->logger->log_bytes(this->logger, RAW|LEVEL3, "generated data for this payload",
-                                                       payload_start, this->out_position-payload_start);
+       DBG2(DBG_ENC, "generating %N payload finished",
+                payload_type_names, payload_type);
+       DBG3(DBG_ENC, "generated data for this payload %b",
+                payload_start, this->out_position-payload_start);
 }
 
 /**
@@ -1072,7 +1058,6 @@ generator_t *generator_create()
        this->current_bit = 0;
        this->last_payload_length_position_offset = 0;
        this->header_length_position_offset = 0;
-       this->logger = logger_manager->get_logger(logger_manager, GENERATOR);
 
        return &(this->public);
 }