updated Doxyfile
[strongswan.git] / src / charon / encoding / payloads / sa_payload.c
index a54e406..950e16d 100644 (file)
@@ -1,12 +1,6 @@
-/**
- * @file sa_payload.c
- * 
- * @brief Implementation of sa_payload_t.
- * 
- */
-
 /*
- * Copyright (C) 2005 Jan Hutter, Martin Willi
+ * Copyright (C) 2005-2006 Martin Willi
+ * Copyright (C) 2005 Jan Hutter
  * Hochschule fuer Technik Rapperswil
  *
  * This program is free software; you can redistribute it and/or modify it
@@ -18,6 +12,8 @@
  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  * for more details.
+ *
+ * $Id$
  */
 
 #include <stddef.h>
@@ -26,6 +22,7 @@
 
 #include <encoding/payloads/encodings.h>
 #include <utils/linked_list.h>
+#include <daemon.h>
 
 
 typedef struct private_sa_payload_t private_sa_payload_t;
@@ -59,13 +56,6 @@ struct private_sa_payload_t {
         * Proposals in this payload are stored in a linked_list_t.
         */
        linked_list_t * proposals;
-       
-       /**
-        * @brief Computes the length of this payload.
-        *
-        * @param this  calling private_sa_payload_t object
-        */
-       void (*compute_length) (private_sa_payload_t *this);
 };
 
 /**
@@ -112,37 +102,32 @@ encoding_rule_t sa_payload_encodings[] = {
  */
 static status_t verify(private_sa_payload_t *this)
 {
-       int proposal_number = 1;
+       int expected_number = 1, current_number;
        status_t status = SUCCESS;
        iterator_t *iterator;
+       proposal_substructure_t *current_proposal;
        bool first = TRUE;
 
-       /* check proposal numbering */          
+       /* check proposal numbering */
        iterator = this->proposals->create_iterator(this->proposals,TRUE);
        
-       while(iterator->has_next(iterator))
+       while(iterator->iterate(iterator, (void**)&current_proposal))
        {
-               proposal_substructure_t *current_proposal;
-               iterator->current(iterator,(void **)&current_proposal);
-               if (current_proposal->get_proposal_number(current_proposal) > proposal_number)
-               {
-                       if (first) 
-                       {
-                               /* first number must be 1 */
-                               status = FAILED;
-                               break;
-                       }
-                       
-                       if (current_proposal->get_proposal_number(current_proposal) != (proposal_number + 1))
+               current_number = current_proposal->get_proposal_number(current_proposal);
+               if (current_number < expected_number)
+               {                       
+                       if (current_number != (expected_number + 1))
                        {
-                               /* must be only one more then previous proposal */
+                               DBG1(DBG_ENC, "proposal number is %d, expected %d or %d",
+                                        current_number, expected_number, expected_number + 1);
                                status = FAILED;
                                break;
                        }
                }
-               else if (current_proposal->get_proposal_number(current_proposal) < proposal_number)
+               else if (current_number < expected_number)
                {
                        /* must not be smaller then proceeding one */
+                       DBG1(DBG_ENC, "proposal number smaller than that of previous proposal");
                        status = FAILED;
                        break;
                }
@@ -150,9 +135,11 @@ static status_t verify(private_sa_payload_t *this)
                status = current_proposal->payload_interface.verify(&(current_proposal->payload_interface));
                if (status != SUCCESS)
                {
+                       DBG1(DBG_ENC, "PROPOSAL_SUBSTRUCTURE verification failed");
                        break;
                }
                first = FALSE;
+               expected_number = current_number;
        }
        
        iterator->destroy(iterator);
@@ -165,17 +152,9 @@ static status_t verify(private_sa_payload_t *this)
  */
 static status_t destroy(private_sa_payload_t *this)
 {
-       /* all proposals are getting destroyed */ 
-       while (this->proposals->get_count(this->proposals) > 0)
-       {
-               proposal_substructure_t *current_proposal;
-               this->proposals->remove_last(this->proposals,(void **)&current_proposal);
-               current_proposal->destroy(current_proposal);
-       }
-       this->proposals->destroy(this->proposals);
-       
+       this->proposals->destroy_offset(this->proposals,
+                                                                       offsetof(proposal_substructure_t, destroy));
        free(this);
-       
        return SUCCESS;
 }
 
@@ -213,11 +192,30 @@ static void set_next_type(private_sa_payload_t *this,payload_type_t type)
 }
 
 /**
+ * recompute length of the payload.
+ */
+static void compute_length (private_sa_payload_t *this)
+{
+       iterator_t *iterator;
+       payload_t *current_proposal;
+       size_t length = SA_PAYLOAD_HEADER_LENGTH;
+       
+       iterator = this->proposals->create_iterator(this->proposals,TRUE);
+       while (iterator->iterate(iterator, (void **)&current_proposal))
+       {
+               length += current_proposal->get_length(current_proposal);
+       }
+       iterator->destroy(iterator);
+       
+       this->payload_length = length;
+}
+
+/**
  * Implementation of payload_t.get_length.
  */
 static size_t get_length(private_sa_payload_t *this)
 {
-       this->compute_length(this);
+       compute_length(this);
        return this->payload_length;
 }
 
@@ -247,7 +245,7 @@ static void add_proposal_substructure(private_sa_payload_t *this,proposal_substr
        proposal->set_is_last_proposal(proposal, TRUE);
        proposal->set_proposal_number(proposal, proposal_count + 1);
        this->proposals->insert_last(this->proposals,(void *) proposal);
-       this->compute_length(this);
+       compute_length(this);
 }
 
 /**
@@ -269,6 +267,7 @@ static linked_list_t *get_proposals(private_sa_payload_t *this)
        int struct_number = 0;
        int ignore_struct_number = 0;
        iterator_t *iterator;
+       proposal_substructure_t *proposal_struct;
        linked_list_t *proposal_list;
        
        /* this list will hold our proposals */
@@ -280,19 +279,17 @@ static linked_list_t *get_proposals(private_sa_payload_t *this)
         * protocols.
         */
        iterator = this->proposals->create_iterator(this->proposals, TRUE);
-       while (iterator->has_next(iterator))
+       while (iterator->iterate(iterator, (void **)&proposal_struct))
        {
                proposal_t *proposal;
-               proposal_substructure_t *proposal_struct;
                
-               iterator->current(iterator, (void **)&proposal_struct);
                /* check if a proposal has a single protocol */
                if (proposal_struct->get_proposal_number(proposal_struct) == struct_number)
                {
                        if (ignore_struct_number < struct_number)
                        {
                                /* remova an already added, if first of series */
-                               proposal_list->remove_last(proposal_list, (void**)proposal);
+                               proposal_list->remove_last(proposal_list, (void**)&proposal);
                                proposal->destroy(proposal);
                                ignore_struct_number = struct_number;
                        }
@@ -309,25 +306,6 @@ static linked_list_t *get_proposals(private_sa_payload_t *this)
        return proposal_list;
 }
 
-/**
- * Implementation of private_sa_payload_t.compute_length.
- */
-static void compute_length (private_sa_payload_t *this)
-{
-       iterator_t *iterator;
-       size_t length = SA_PAYLOAD_HEADER_LENGTH;
-       iterator = this->proposals->create_iterator(this->proposals,TRUE);
-       while (iterator->has_next(iterator))
-       {
-               payload_t *current_proposal;
-               iterator->current(iterator,(void **) &current_proposal);
-               length += current_proposal->get_length(current_proposal);
-       }
-       iterator->destroy(iterator);
-       
-       this->payload_length = length;
-}
-
 /*
  * Described in header.
  */
@@ -351,14 +329,10 @@ sa_payload_t *sa_payload_create()
        this->public.get_proposals = (linked_list_t* (*) (sa_payload_t *)) get_proposals;
        this->public.destroy = (void (*) (sa_payload_t *)) destroy;
        
-       /* private functions */
-       this->compute_length = compute_length;
-       
        /* set default values of the fields */
        this->critical = FALSE;
        this->next_payload = NO_PAYLOAD;
        this->payload_length = SA_PAYLOAD_HEADER_LENGTH;
-
        this->proposals = linked_list_create();
        return &this->public;
 }
@@ -374,9 +348,8 @@ sa_payload_t *sa_payload_create_from_proposal_list(linked_list_t *proposals)
        
        /* add every payload from the list */
        iterator = proposals->create_iterator(proposals, TRUE);
-       while (iterator->has_next(iterator))
+       while (iterator->iterate(iterator, (void**)&proposal))
        {
-               iterator->current(iterator, (void**)&proposal);
                add_proposal((private_sa_payload_t*)sa_payload, proposal);
        }
        iterator->destroy(iterator);