generation of uint implemented but not tested
authorJan Hutter <jhutter@hsr.ch>
Wed, 9 Nov 2005 13:34:36 +0000 (13:34 -0000)
committerJan Hutter <jhutter@hsr.ch>
Wed, 9 Nov 2005 13:34:36 +0000 (13:34 -0000)
Source/charon/allocator.h
Source/charon/generator.c
Source/charon/generator.h
Source/charon/tests.c
Source/charon/tests/generator_test.c
Source/charon/tests/generator_test.h

index 6e2ac8f..9c74ada 100644 (file)
        #define allocator_alloc(bytes) (allocate(bytes,__FILE__,__LINE__))
        #define allocator_realloc(old,bytes) (reallocate(old,bytes,__FILE__, __LINE__))
        #define allocator_free(pointer) (free_pointer(pointer))
-
+       #define allocator_free_chunk(chunk){    \
+               free_pointer(chunk.ptr);                        \
+               chunk.ptr = NULL;                               \
+               chunk.len = 0;                                  \
+       }
        /**
         * Report memory leaks to stderr
         */
        #define allocator_alloc(bytes) (malloc(bytes))
        #define allocator_realloc(old,bytes) (realloc(old,bytes))
        #define allocator_free(pointer) (free(pointer))
+       #define allocator_free_chunk(chunk){    \
+               free(chunk.ptr);                                        \
+               chunk.ptr = NULL;                               \
+               chunk.len = 0;                                  \
+       }
        #define report_memory_leaks(void) {}
 #endif
 
index c06cba6..04f164a 100644 (file)
@@ -21,6 +21,7 @@
  */
 
 #include <stdlib.h>
+#include <string.h>
 #include <freeswan.h>
 #include <pluto/constants.h>
 #include <pluto/defs.h>
 #include "types.h"
 #include "generator.h"
 
+
+/**
+ * buffer_t: used for geneartor operations
+ */
+typedef struct generator_infos_s generator_infos_t;
+
+struct generator_infos_s {
+
+       /**
+        * Buffer used to generate to 
+        */
+       u_int8_t *buffer;
+       
+       /**
+        * current write position in buffer (one byte alligned)
+        */
+       u_int8_t *out_position;
+       
+       /**
+        * position of last byte in buffer
+        */
+       u_int8_t *roof_position;
+       
+       /**
+        * Current bit writing to
+        */
+       size_t current_bit;
+       
+       /**
+        * Associated data struct to read informations from
+        */
+        void * data_struct;
+       /**
+        * @brief Destroys a generator_infos_t object
+        * 
+        * @param generator_infos_t generator_infos_t object
+        * @return SUCCESSFUL if succeeded, FAILED otherwise
+        */
+       status_t (*destroy) (generator_infos_t *this);
+       
+       /**
+        * Checks if enough space is available in buffer and if not,
+        * the buffer size is increased until at least the asked amount of space 
+        * is available
+        * 
+        * @param bits number of bits to make at leas available in buffer
+        * @param generator_infos_t generator_infos_t object
+        * @return SUCCESSFUL if succeeded, OUT_OF_RES otherwise
+        */
+       status_t (*make_space_available) (generator_infos_t *this,size_t bits);
+       
+       status_t (*write_chunk) (generator_infos_t *this,chunk_t *data);        
+};
+
+/**
+ * implements generator_infos_t's increase_buffer function
+ */
+static status_t generator_info_make_space_available (generator_infos_t *this, size_t bits)
+{
+       size_t free_bits = ((this->roof_position - this->out_position) * 8) - this->current_bit;
+       
+       while (free_bits < bits)
+       {
+               size_t old_buffer_size = ((this->roof_position) - (     this->buffer));
+               size_t new_buffer_size = old_buffer_size + GENERATOR_DATA_BUFFER_INCREASE_VALUE;
+               size_t out_position_offset = ((this->out_position) - (this->buffer));
+               u_int8_t *new_buffer;
+       
+               new_buffer = allocator_realloc(this->buffer,new_buffer_size);
+               if (new_buffer == NULL)
+               {
+                       return OUT_OF_RES;
+               }
+       
+               this->buffer = new_buffer;
+       
+               this->out_position = (this->buffer + out_position_offset);
+               this->roof_position = (this->buffer + new_buffer_size);
+       }
+       
+       return SUCCESS;
+}
+
+static status_t generator_infos_write_chunk (generator_infos_t *this,chunk_t *data)
+{
+       size_t data_length = this->out_position - this->buffer;
+       if (this->current_bit > 0)
+       data_length++;
+       data->ptr = allocator_alloc(data_length);
+       if (data->ptr == NULL)
+       {
+               data->len = 0;
+               return OUT_OF_RES;
+       }
+       memcpy(data->ptr,this->buffer,data_length);
+       data->len = data_length;
+       return SUCCESS;
+}
+
+
+static status_t generator_infos_destroy (generator_infos_t *this)
+{
+       if (this == NULL)
+       {
+               return FAILED;
+       }
+       allocator_free(this->buffer);
+       allocator_free(this);
+       return SUCCESS;
+}
+
+/**
+ * Creates a generator_infos_t-object holding necessary informations 
+ * for generating (buffer, data_struct, etc)
+ * 
+ * @param data_struct where to read the data out
+ */
+generator_infos_t * generator_infos_create(void *data_struct)
+{
+       generator_infos_t *this = allocator_alloc_thing(generator_infos_t);
+
+       if (this == NULL)
+       {
+               return NULL;
+       }
+
+       /* object methods */
+       this->destroy = generator_infos_destroy;
+       this->make_space_available = generator_info_make_space_available;
+       this->write_chunk = generator_infos_write_chunk;
+
+       /* allocate memory for buffer */
+       this->buffer = allocator_alloc(GENERATOR_DATA_BUFFER_SIZE);
+       if (this->buffer == NULL)
+       {
+               allocator_free(this);
+               return NULL;
+       }
+       
+       /* set private data */
+       this->out_position = this->buffer;
+       this->roof_position = this->buffer + GENERATOR_DATA_BUFFER_SIZE;
+       this->data_struct = data_struct;
+       this->current_bit = 0;
+       return (this);
+}
+
+
+
 /**
  * Private data of a generator_t object
  */
@@ -61,7 +211,7 @@ struct private_generator_s {
        /**
         * TODO
         */
-       status_t (*generate_u_int_type) (private_generator_t *this,encoding_type_t int_type,u_int8_t **buffer,u_int8_t **out_position,u_int8_t **roof_position,size_t *current_bit);
+       status_t (*generate_u_int_type) (private_generator_t *this,encoding_type_t int_type,u_int32_t offset, generator_infos_t *generator_infos);
 
        /**
         * Pointer to the payload informations needed to automatic
@@ -70,14 +220,15 @@ struct private_generator_s {
        payload_info_t **payload_infos;
 };
 
-
 /**
- * implements private_generator_t's generate_u_int_type function
+ * implements private_generator_t's double_buffer function
  */
-
-static status_t generate_u_int_type (private_generator_t *this,encoding_type_t int_type,u_int8_t **buffer,u_int8_t **out_position,u_int8_t **roof_position,size_t *current_bit)
+static status_t generate_u_int_type (private_generator_t *this,encoding_type_t int_type,u_int32_t offset,generator_infos_t *generator_infos)
 {
        size_t number_of_bits = 0;
+       
+       status_t status;
+       
 
        switch (int_type)
        {
@@ -99,6 +250,34 @@ static status_t generate_u_int_type (private_generator_t *this,encoding_type_t i
                        default:
                        return FAILED;
        }
+       
+       status = generator_infos->make_space_available(generator_infos,number_of_bits);
+       
+       if (status != SUCCESS)
+       {
+               return status;
+       }
+       
+       /* process 4 byte integer special */
+       if (number_of_bits == 4)
+       {
+               if (generator_infos->current_bit == 0)
+               {                       
+                       *(generator_infos->out_position) = *((u_int8_t *)(generator_infos->data_struct + offset)) << 4;
+                       generator_infos->current_bit = 4;
+               }
+               else if (generator_infos->current_bit == 4)
+               {
+                       generator_infos->out_position++;
+                       generator_infos->current_bit = 0;
+                       
+               }
+               else
+               {
+                       /* 4 Bit integers must have a 4 bit alignment */
+                       return FAILED;
+               }
+       }
        return SUCCESS;
 }
 
@@ -107,19 +286,20 @@ static status_t generate_u_int_type (private_generator_t *this,encoding_type_t i
  */
 static status_t generate (private_generator_t *this,void * data_struct,encoding_rule_t *encoding_rules, size_t encoding_rules_count, chunk_t *data)
 {
-       u_int8_t * buffer = allocator_alloc(GENERATOR_DATA_BUFFER_SIZE);
-       u_int8_t * out_position = buffer;
-       u_int8_t * roof_position = buffer + GENERATOR_DATA_BUFFER_SIZE;
-       size_t current_bit = 0;
        int i;
+       status_t status;
+       
+       
+       generator_infos_t *infos = generator_infos_create(data_struct);
 
-       if (buffer == NULL)
+       if (infos == NULL)
        {
                return OUT_OF_RES;
        }
+
        for (i = 0; i < encoding_rules_count;i++)
        {
-               status_t status = SUCCESS;
+               status = SUCCESS;
                switch (encoding_rules[i].type)
                {
                        case U_INT_4:
@@ -127,7 +307,7 @@ static status_t generate (private_generator_t *this,void * data_struct,encoding_
                        case U_INT_16:
                        case U_INT_32:
                        case U_INT_64:
-                               status = this->generate_u_int_type(this,encoding_rules[i].type,&buffer,&out_position,&roof_position,&current_bit);
+                               status = this->generate_u_int_type(this,encoding_rules[i].type,encoding_rules[i].offset,infos);
                                break;
                        case RESERVED_BIT:
                        case RESERVED_BYTE:
@@ -139,12 +319,15 @@ static status_t generate (private_generator_t *this,void * data_struct,encoding_
                }
                if (status != SUCCESS)
                {
-                       allocator_free(buffer);
+                       infos->destroy(infos);
                        return status;
                }
        }
 
-       return SUCCESS;
+
+       status = infos->write_chunk(infos,data);
+       infos->destroy(infos);
+       return status;
 }
 
 static status_t generate_payload (private_generator_t *this,payload_type_t payload_type,void * data_struct, chunk_t *data)
@@ -195,13 +378,15 @@ generator_t * generator_create(payload_info_t ** payload_infos)
                return NULL;
        }
 
+       /* initiate public functions */
        this->public.generate_payload = (status_t(*)(generator_t*, payload_type_t, void *, chunk_t *)) generate_payload;
        this->public.destroy = (status_t(*)(generator_t*)) destroy;
 
-       /* initiate private fields */
+       /* initiate private functions */
        this->generate = generate;
        this->generate_u_int_type = generate_u_int_type;
 
+       /* initiate private variables */
        this->payload_infos = payload_infos;
 
        return &(this->public);
index e765eff..38fe065 100644 (file)
 #define GENERATOR_DATA_BUFFER_SIZE 3000
 
 /**
+ * Number of Bytes to increase the buffer if it is to small
+ */
+#define GENERATOR_DATA_BUFFER_INCREASE_VALUE 1000
+
+/**
  * @brief A generator_t-object which generates payloads of specific type
  */
 typedef struct generator_s generator_t;
index 490703c..bd1769d 100644 (file)
@@ -121,7 +121,12 @@ test_t ike_sa_manager_test = {test_ike_sa_manager, "IKE_SA-Manager"};
 /**
  * Test for generator_t
  */
-test_t generator_test = {test_generator_with_unsupported_payload,"Generator: unsupported payload"};
+test_t generator_test1 = {test_generator_with_unsupported_payload,"Generator: unsupported payload"};
+
+/**
+ * Test 2 for generator_t
+ */
+test_t generator_test2 = {test_generator_with_header_payload,"Generator: header payload"};
 
 /**
  * Global job-queue
@@ -161,7 +166,8 @@ socket_t *global_socket;
        &receiver_test,
        &ike_sa_id_test,
        &ike_sa_test,
-       &generator_test,
+       &generator_test1,
+       &generator_test2,
        &ike_sa_manager_test,
        NULL
        };
@@ -174,8 +180,8 @@ socket_t *global_socket;
                
        tester_t *tester = tester_create(test_output, FALSE);
 
-       tester->perform_tests(tester,all_tests);
-/*     tester->perform_test(tester,&scheduler_test);   */
+       /*tester->perform_tests(tester,all_tests);*/
+       tester->perform_test(tester,&generator_test2);   
        
        tester->destroy(tester);
 
index 99c3948..f254992 100644 (file)
  * for more details.
  */
  
+#include <freeswan.h>
+#include <pluto/constants.h>
+#include <pluto/defs.h>
+
+#include "../allocator.h"
 #include "generator_test.h"
 #include "../tester.h"
 #include "../encodings.h"
 #include "../generator.h"
+#include "../encodings/ike_header.h"
 
 extern payload_info_t *payload_infos[];
 
@@ -39,3 +45,32 @@ void test_generator_with_unsupported_payload(tester_t *tester)
        
        tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
 }
+
+void test_generator_with_header_payload(tester_t *tester)
+{
+       generator_t *generator;
+       ike_header_t header_data;
+       chunk_t generated_data;
+       
+       header_data.initiator_spi = 1;
+       header_data.responder_spi = 2;
+       header_data.next_payload = 3;
+       header_data.maj_version = 4;
+       header_data.min_version = 5;
+       header_data.exchange_type = 6;
+       header_data.flags.initiator = TRUE;
+       header_data.flags.version = TRUE;
+       header_data.flags.response = TRUE;
+       header_data.message_id = 7;
+       header_data.length = 8;
+       
+       generator = generator_create(payload_infos);
+       tester->assert_true(tester,(generator != NULL), "generator create check");
+       tester->assert_true(tester,(generator->generate_payload(generator,HEADER,&header_data,&generated_data) == SUCCESS),"generate_payload call check");
+
+       DBG_dump("test:",generated_data.ptr,generated_data.len);
+       
+       allocator_free_chunk(generated_data);
+       
+       tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
+}
index c94e58d..38f0129 100644 (file)
  */
 void test_generator_with_unsupported_payload(tester_t *tester);
 
+/**
+ * @brief Test function used to test the generator with header payload
+ * 
+ *
+ * @param tester associated tester object
+ */
+void test_generator_with_header_payload(tester_t *tester);
+
 #endif /*GENERATOR_TEST_H_*/