- proposal substructure written, but not tested
authorJan Hutter <jhutter@hsr.ch>
Mon, 14 Nov 2005 16:03:44 +0000 (16:03 -0000)
committerJan Hutter <jhutter@hsr.ch>
Mon, 14 Nov 2005 16:03:44 +0000 (16:03 -0000)
Source/charon/payloads/proposal_substructure.c
Source/charon/payloads/proposal_substructure.h

index 282538b..e6472ed 100644 (file)
@@ -86,6 +86,15 @@ struct private_proposal_substructure_s {
         * Transforms are stored in a linked_list_t
         */
        linked_list_t * transforms;
         * Transforms are stored in a linked_list_t
         */
        linked_list_t * transforms;
+       
+       /**
+        * @brief Computes the length of this substructure.
+        *
+        * @param this  calling private_proposal_substructure_t object
+        * @return              
+        *                              SUCCESS in any case
+        */
+       status_t (*compute_length) (private_proposal_substructure_t *this);
 };
 
 /**
 };
 
 /**
@@ -200,8 +209,122 @@ static status_t create_transform_substructure_iterator (private_proposal_substru
 static status_t add_transform_substructure (private_proposal_substructure_t *this,transform_substructure_t *transform)
 {
        return (this->transforms->insert_last(this->transforms,(void *) transform));
 static status_t add_transform_substructure (private_proposal_substructure_t *this,transform_substructure_t *transform)
 {
        return (this->transforms->insert_last(this->transforms,(void *) transform));
+       this->compute_length(this);
+}
+
+/**
+ * Implements proposal_substructure_t's set_proposal_number function.
+ * See #proposal_substructure_s.set_proposal_number for description.
+ */
+static status_t set_proposal_number(private_proposal_substructure_t *this,u_int8_t proposal_number)
+{
+       this->proposal_number = proposal_number;
+       return SUCCESS;
+}
+
+/**
+ * Implements proposal_substructure_t's get_proposal_number function.
+ * See #proposal_substructure_s.get_proposal_number for description.
+ */
+static u_int8_t get_proposal_number (private_proposal_substructure_t *this)
+{
+       return (this->proposal_number);
+}
+
+/**
+ * Implements proposal_substructure_t's set_protocol_id function.
+ * See #proposal_substructure_s.set_protocol_id for description.
+ */
+static status_t set_protocol_id(private_proposal_substructure_t *this,u_int8_t protocol_id)
+{
+       this->protocol_id = protocol_id;
+       return SUCCESS;
+}
+
+/**
+ * Implements proposal_substructure_t's get_protocol_id function.
+ * See #proposal_substructure_s.get_protocol_id for description.
+ */
+static u_int8_t get_protocol_id (private_proposal_substructure_t *this)
+{
+       return (this->protocol_id);
 }
 
 }
 
+
+/**
+ * Implements proposal_substructure_t's set_spi function.
+ * See #proposal_substructure_s.set_spi for description.
+ */
+static status_t set_spi (private_proposal_substructure_t *this, chunk_t spi)
+{
+       /* first delete already set spi value */
+       if (this->spi.ptr != NULL)
+       {
+               allocator_free(this->spi.ptr);
+               this->spi.ptr = NULL;
+               this->spi.len = 0;
+               this->compute_length(this);
+       }
+       
+       this->spi.ptr = allocator_clone_bytes(spi.ptr,spi.len);
+       if (this->spi.ptr == NULL)
+       {
+               return OUT_OF_RES;
+       }
+       this->spi.len = spi.len;
+       this->spi_size = spi.len;
+       this->compute_length(this);
+
+       return SUCCESS;
+}
+
+/**
+ * Implements proposal_substructure_t's get_spi function.
+ * See #proposal_substructure_s.get_spi for description.
+ */
+static chunk_t get_spi (private_proposal_substructure_t *this)
+{
+       chunk_t spi;
+       spi.ptr = this->spi.ptr;
+       spi.len = this->spi.len;                
+       
+       return spi;
+}
+
+/**
+ * Implements private_proposal_substructure_t's compute_length function.
+ * See #private_proposal_substructure_s.compute_length for description.
+ */
+static status_t compute_length (private_proposal_substructure_t *this)
+{
+       linked_list_iterator_t *iterator;
+       status_t status;
+       size_t transforms_count = 0;
+       size_t length = PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH;
+       status = this->transforms->create_iterator(this->transforms,&iterator,TRUE);
+       if (status != SUCCESS)
+       {
+               return length;
+       }
+       while (iterator->has_next(iterator))
+       {
+               payload_t * current_transform;
+               iterator->current(iterator,(void **) &current_transform);
+               length += current_transform->get_length(current_transform);
+               transforms_count++;
+       }
+       
+       
+       length += this->spi.len;
+       
+       
+       this->transforms_count= transforms_count;
+       this->proposal_length = length; 
+
+       return SUCCESS;
+}
+
+
 /*
  * Described in header
  */
 /*
  * Described in header
  */
@@ -220,8 +343,17 @@ proposal_substructure_t *proposal_substructure_create()
        this->public.payload_interface.destroy = (status_t (*) (payload_t *))destroy;
        this->public.create_transform_substructure_iterator = (status_t (*) (proposal_substructure_t *,linked_list_iterator_t **,bool)) create_transform_substructure_iterator;
        this->public.add_transform_substructure = (status_t (*) (proposal_substructure_t *,transform_substructure_t *)) add_transform_substructure;
        this->public.payload_interface.destroy = (status_t (*) (payload_t *))destroy;
        this->public.create_transform_substructure_iterator = (status_t (*) (proposal_substructure_t *,linked_list_iterator_t **,bool)) create_transform_substructure_iterator;
        this->public.add_transform_substructure = (status_t (*) (proposal_substructure_t *,transform_substructure_t *)) add_transform_substructure;
+       this->public.set_proposal_number = (status_t (*) (proposal_substructure_t *,u_int8_t))set_proposal_number;
+       this->public.get_proposal_number = (u_int8_t (*) (proposal_substructure_t *)) get_proposal_number;
+       this->public.set_protocol_id = (status_t (*) (proposal_substructure_t *,u_int8_t))set_protocol_id;
+       this->public.get_protocol_id = (u_int8_t (*) (proposal_substructure_t *)) get_protocol_id;
+       this->public.set_spi = (status_t (*) (proposal_substructure_t *,chunk_t))set_spi;
+       this->public.get_spi = (chunk_t (*) (proposal_substructure_t *)) get_spi;
        this->public.destroy = (status_t (*) (proposal_substructure_t *)) destroy;
        
        this->public.destroy = (status_t (*) (proposal_substructure_t *)) destroy;
        
+       /* private functions */
+       this->compute_length = compute_length;
+       
        /* set default values of the fields */
        this->next_payload = NO_PAYLOAD;
        this->proposal_length = 0;
        /* set default values of the fields */
        this->next_payload = NO_PAYLOAD;
        this->proposal_length = 0;
index 9146bc8..35d9c9f 100644 (file)
 #include "transform_substructure.h"
 
 /**
 #include "transform_substructure.h"
 
 /**
+ * Length of the proposal substructure header
+ * (without spi)
+ */
+#define PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH 8
+
+/**
  * Object representing an IKEv2- PROPOSAL SUBSTRUCTURE
  * 
  * The PROPOSAL SUBSTRUCTURE format is described in RFC section 3.3.1.
  * Object representing an IKEv2- PROPOSAL SUBSTRUCTURE
  * 
  * The PROPOSAL SUBSTRUCTURE format is described in RFC section 3.3.1.
@@ -48,6 +54,8 @@ struct proposal_substructure_s {
         * @brief Creates an iterator of stored transform_substructure_t objects.
         * 
         * @warning The created iterator has to get destroyed by the caller!
         * @brief Creates an iterator of stored transform_substructure_t objects.
         * 
         * @warning The created iterator has to get destroyed by the caller!
+        *                      When deleting any transform over this iterator, call 
+        *                      get_size to make sure the length and number values are ok.
         *
         * @param this                  calling proposal_substructure_t object
         * @param iterator              the created iterator is stored at the pointed pointer
         *
         * @param this                  calling proposal_substructure_t object
         * @param iterator              the created iterator is stored at the pointed pointer
@@ -70,6 +78,64 @@ struct proposal_substructure_s {
         *                                      - FAILED otherwise
         */
        status_t (*add_transform_substructure) (proposal_substructure_t *this,transform_substructure_t *transform);
         *                                      - FAILED otherwise
         */
        status_t (*add_transform_substructure) (proposal_substructure_t *this,transform_substructure_t *transform);
+       
+       /**
+        * @brief Sets the proposal number of current proposal.
+        *
+        * @param this          calling proposal_substructure_t object
+        * @param id                    proposal number to set
+        * @return                      - SUCCESS
+        */
+       status_t (*set_proposal_number) (proposal_substructure_t *this,u_int8_t proposal_number);
+       
+       /**
+        * @brief get proposal number of current proposal.
+        * 
+        * @param this          calling proposal_substructure_t object
+        * @return                      proposal number of current proposal substructure.
+        */
+       u_int8_t (*get_proposal_number) (proposal_substructure_t *this);
+
+       /**
+        * @brief Sets the protocol id of current proposal.
+        *
+        * @param this          calling proposal_substructure_t object
+        * @param id                    protocol id to set
+        * @return                      - SUCCESS
+        */
+       status_t (*set_protocol_id) (proposal_substructure_t *this,u_int8_t protocol_id);
+       
+       /**
+        * @brief get protocol id of current proposal.
+        * 
+        * @param this          calling proposal_substructure_t object
+        * @return                      protocol id of current proposal substructure.
+        */
+       u_int8_t (*get_protocol_id) (proposal_substructure_t *this);
+
+
+       /**
+        * @brief Returns the currently set SPI of this proposal.
+        *      
+        * @warning Returned data are not copied
+        * 
+        * @param this  calling proposal_substructure_t object
+        * @return              chunk_t pointing to the value
+        */
+       chunk_t (*get_spi) (proposal_substructure_t *this);
+       
+       /**
+        * @brief Sets the SPI of the current proposal.
+        *      
+        * @warning SPI is getting copied
+        * 
+        * @param this  calling proposal_substructure_t object
+        * @param spi   chunk_t pointing to the value to set
+        * @return              
+        *                              - SUCCESS or
+        *                              - OUT_OF_RES
+        */
+       status_t (*set_spi) (proposal_substructure_t *this, chunk_t spi);
 
        /**
         * @brief Destroys an proposal_substructure_t object.
 
        /**
         * @brief Destroys an proposal_substructure_t object.