updated Doxyfile
[strongswan.git] / src / charon / encoding / payloads / sa_payload.c
index 345264b..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;
 }
 
@@ -232,20 +230,22 @@ static iterator_t *create_proposal_substructure_iterator (private_sa_payload_t *
 /**
  * Implementation of sa_payload_t.add_proposal_substructure.
  */
-static void add_proposal_substructure (private_sa_payload_t *this,proposal_substructure_t *proposal)
+static void add_proposal_substructure(private_sa_payload_t *this,proposal_substructure_t *proposal)
 {
        status_t status;
-       if (this->proposals->get_count(this->proposals) > 0)
+       u_int proposal_count = this->proposals->get_count(this->proposals);
+       
+       if (proposal_count > 0)
        {
                proposal_substructure_t *last_proposal;
                status = this->proposals->get_last(this->proposals,(void **) &last_proposal);
                /* last transform is now not anymore last one */
-               last_proposal->set_is_last_proposal(last_proposal,FALSE);
+               last_proposal->set_is_last_proposal(last_proposal, FALSE);
        }
-       proposal->set_is_last_proposal(proposal,TRUE);
-       
+       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);
 }
 
 /**
@@ -254,19 +254,9 @@ static void add_proposal_substructure (private_sa_payload_t *this,proposal_subst
 static void add_proposal(private_sa_payload_t *this, proposal_t *proposal)
 {
        proposal_substructure_t *substructure;
-       protocol_id_t proto[2];
-       u_int i;
        
-       /* build the substructures for every protocol */
-       proposal->get_protocols(proposal, proto);
-       for (i = 0; i<2; i++)
-       {
-               if (proto[i] != PROTO_NONE)
-               {
-                       substructure = proposal_substructure_create_from_proposal(proposal, proto[i]);
-                       add_proposal_substructure(this, substructure);
-               }
-       }
+       substructure = proposal_substructure_create_from_proposal(proposal);
+       add_proposal_substructure(this, substructure);
 }
 
 /**
@@ -274,54 +264,48 @@ static void add_proposal(private_sa_payload_t *this, proposal_t *proposal)
  */
 static linked_list_t *get_proposals(private_sa_payload_t *this)
 {
-       int proposal_struct_number = 0;
+       int struct_number = 0;
+       int ignore_struct_number = 0;
        iterator_t *iterator;
-       proposal_t *proposal;
+       proposal_substructure_t *proposal_struct;
        linked_list_t *proposal_list;
        
        /* this list will hold our proposals */
        proposal_list = linked_list_create();
        
-       /* iterate over structures, one OR MORE structures will result in a proposal */
-       iterator = this->proposals->create_iterator(this->proposals,TRUE);
-       while (iterator->has_next(iterator))
+       /* we do not support proposals split up to two proposal substructures, as
+        * AH+ESP bundles are not supported in RFC4301 anymore.
+        * To handle such structures safely, we just skip proposals with multiple
+        * protocols.
+        */
+       iterator = this->proposals->create_iterator(this->proposals, TRUE);
+       while (iterator->iterate(iterator, (void **)&proposal_struct))
        {
-               proposal_substructure_t *proposal_struct;
-               iterator->current(iterator,(void **)&(proposal_struct));
+               proposal_t *proposal;
                
-               if (proposal_struct->get_proposal_number(proposal_struct) > proposal_struct_number)
+               /* 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->destroy(proposal);
+                               ignore_struct_number = struct_number;
+                       }
+                       continue;
+               }
+               struct_number++;
+               proposal = proposal_struct->get_proposal(proposal_struct);
+               if (proposal)
                {
-                       /* here starts a new proposal, create a new one and add it to the list */
-                       proposal_struct_number = proposal_struct->get_proposal_number(proposal_struct);
-                       proposal = proposal_create(proposal_struct_number);
                        proposal_list->insert_last(proposal_list, proposal);
                }
-               /* proposal_substructure_t does the dirty work and builds up the proposal */
-               proposal_struct->add_to_proposal(proposal_struct, proposal);
        }
        iterator->destroy(iterator);
        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.
  */
@@ -345,16 +329,12 @@ 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));
+       return &this->public;
 }
 
 /*
@@ -368,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);