- added ts for both initator and responder
authorMartin Willi <martin@strongswan.org>
Thu, 1 Dec 2005 18:14:22 +0000 (18:14 -0000)
committerMartin Willi <martin@strongswan.org>
Thu, 1 Dec 2005 18:14:22 +0000 (18:14 -0000)
- ts_payload can get now ts's

Source/charon/config/sa_config.c
Source/charon/config/sa_config.h
Source/charon/encoding/payloads/ts_payload.c
Source/charon/testcases/sa_config_test.c
Source/charon/testcases/testcases.c

index 51fc94a..3f253b5 100644 (file)
@@ -59,14 +59,29 @@ struct private_sa_config_t {
        linked_list_t *proposals;
        
        /**
-        * list for traffic selectors
+        * list for traffic selectors for initiator site
         */
-       linked_list_t *ts;
+       linked_list_t *ts_initiator;
+       
+       /**
+        * list for traffic selectors for responder site
+        */
+       linked_list_t *ts_responder;
 
        /**
         * compare two proposals for equality
         */
        bool (*proposal_equals) (private_sa_config_t *this, child_proposal_t *first, child_proposal_t *second);
+
+       /**
+        * get_traffic_selectors for both
+        */
+       size_t (*get_traffic_selectors) (private_sa_config_t *,linked_list_t*,traffic_selector_t**[]);
+
+       /**
+        * select_traffic_selectors for both
+        */
+       size_t (*select_traffic_selectors) (private_sa_config_t *,linked_list_t*,traffic_selector_t*[],size_t,traffic_selector_t**[]);
 };
 
 /**
@@ -93,18 +108,35 @@ static auth_method_t get_auth_method(private_sa_config_t *this)
        return this->auth_method;
 }
 
+
 /**
- * implements sa_config_t.get_traffic_selectors
+ * implements sa_config_t.get_traffic_selectors_initiator
  */
-static size_t get_traffic_selectors(private_sa_config_t *this, traffic_selector_t **traffic_selectors[])
+static size_t get_traffic_selectors_initiator(private_sa_config_t *this, traffic_selector_t **traffic_selectors[])
+{
+       return this->get_traffic_selectors(this, this->ts_initiator, traffic_selectors);
+}
+
+/**
+ * implements sa_config_t.get_traffic_selectors_responder
+ */
+static size_t get_traffic_selectors_responder(private_sa_config_t *this, traffic_selector_t **traffic_selectors[])
+{
+       return this->get_traffic_selectors(this, this->ts_responder, traffic_selectors);
+}
+
+/**
+ * implements private_sa_config_t.get_traffic_selectors
+ */
+static size_t get_traffic_selectors(private_sa_config_t *this, linked_list_t *ts_list, traffic_selector_t **traffic_selectors[])
 {
        iterator_t *iterator;
        traffic_selector_t *current_ts;
        int counter = 0;
-       *traffic_selectors = allocator_alloc(sizeof(traffic_selector_t*) * this->ts->get_count(this->ts));
+       *traffic_selectors = allocator_alloc(sizeof(traffic_selector_t*) * ts_list->get_count(ts_list));
        
        /* copy all ts from the list in an array */
-       iterator = this->ts->create_iterator(this->ts, TRUE);
+       iterator = ts_list->create_iterator(ts_list, TRUE);
        while (iterator->has_next(iterator))
        {
                iterator->current(iterator, (void**)&current_ts);
@@ -116,17 +148,32 @@ static size_t get_traffic_selectors(private_sa_config_t *this, traffic_selector_
 }
 
 /**
- * implements sa_config_t.select_traffic_selectors
+ * implements private_sa_config_t.select_traffic_selectors_initiator
  */
-static size_t select_traffic_selectors(private_sa_config_t *this, traffic_selector_t *supplied[], size_t count, traffic_selector_t **selected[])
+static size_t select_traffic_selectors_initiator(private_sa_config_t *this,traffic_selector_t *supplied[], size_t count, traffic_selector_t **selected[])
+{
+       return this->select_traffic_selectors(this, this->ts_initiator, supplied, count, selected);
+}
+
+/**
+ * implements private_sa_config_t.select_traffic_selectors_responder
+ */
+static size_t select_traffic_selectors_responder(private_sa_config_t *this,traffic_selector_t *supplied[], size_t count, traffic_selector_t **selected[])
+{
+       return this->select_traffic_selectors(this, this->ts_responder, supplied, count, selected);
+}
+/**
+ * implements private_sa_config_t.select_traffic_selectors
+ */
+static size_t select_traffic_selectors(private_sa_config_t *this, linked_list_t *ts_list, traffic_selector_t *supplied[], size_t count, traffic_selector_t **selected[])
 {
        iterator_t *iterator;
        traffic_selector_t *current_ts;
        int i, counter = 0;
-       *selected = allocator_alloc(sizeof(traffic_selector_t*) * this->ts->get_count(this->ts));
+       *selected = allocator_alloc(sizeof(traffic_selector_t*) * ts_list->get_count(ts_list));
        
        /* iterate over all stored proposals */
-       iterator = this->ts->create_iterator(this->ts, TRUE);
+       iterator = ts_list->create_iterator(ts_list, TRUE);
        while (iterator->has_next(iterator))
        {
                iterator->current(iterator, (void**)&current_ts);
@@ -243,13 +290,21 @@ static bool proposal_equals(private_sa_config_t *this, child_proposal_t *first,
 }
 
 /**
- * implements sa_config_t.add_traffic_selector
+ * implements sa_config_t.add_traffic_selector_initiator
  */
-static void add_traffic_selector(private_sa_config_t *this, traffic_selector_t *traffic_selector)
+static void add_traffic_selector_initiator(private_sa_config_t *this, traffic_selector_t *traffic_selector)
 {
        /* clone ts, and add*/
-       
-       this->ts->insert_last(this->ts, (void*)traffic_selector->clone(traffic_selector));
+       this->ts_initiator->insert_last(this->ts_initiator, (void*)traffic_selector->clone(traffic_selector));
+}
+
+/**
+ * implements sa_config_t.add_traffic_selector_responder
+ */
+static void add_traffic_selector_responder(private_sa_config_t *this, traffic_selector_t *traffic_selector)
+{
+       /* clone ts, and add*/
+       this->ts_responder->insert_last(this->ts_responder, (void*)traffic_selector->clone(traffic_selector));
 }
 
 /**
@@ -281,12 +336,20 @@ static status_t destroy(private_sa_config_t *this)
        this->proposals->destroy(this->proposals);
        
        /* delete traffic selectors */
-       while(this->ts->get_count(this->ts) > 0)
+       while(this->ts_initiator->get_count(this->ts_initiator) > 0)
+       {
+               this->ts_initiator->remove_last(this->ts_initiator, (void**)&traffic_selector);
+               traffic_selector->destroy(traffic_selector);
+       }
+       this->ts_initiator->destroy(this->ts_initiator);
+       
+       /* delete traffic selectors */
+       while(this->ts_responder->get_count(this->ts_responder) > 0)
        {
-               this->ts->remove_last(this->ts, (void**)&traffic_selector);
+               this->ts_responder->remove_last(this->ts_responder, (void**)&traffic_selector);
                traffic_selector->destroy(traffic_selector);
        }
-       this->ts->destroy(this->ts);
+       this->ts_responder->destroy(this->ts_responder);
        
        /* delete ids */
        this->my_id->destroy(this->my_id);
@@ -307,11 +370,14 @@ sa_config_t *sa_config_create(id_type_t my_id_type, char *my_id, id_type_t other
        this->public.get_my_id = (identification_t*(*)(sa_config_t*))get_my_id;
        this->public.get_other_id = (identification_t*(*)(sa_config_t*))get_other_id;
        this->public.get_auth_method = (auth_method_t(*)(sa_config_t*))get_auth_method;
-       this->public.get_traffic_selectors = (size_t(*)(sa_config_t*,traffic_selector_t**[]))get_traffic_selectors;
-       this->public.select_traffic_selectors = (size_t(*)(sa_config_t*,traffic_selector_t*[],size_t,traffic_selector_t**[]))select_traffic_selectors;
+       this->public.get_traffic_selectors_initiator = (size_t(*)(sa_config_t*,traffic_selector_t**[]))get_traffic_selectors_initiator;
+       this->public.select_traffic_selectors_initiator = (size_t(*)(sa_config_t*,traffic_selector_t*[],size_t,traffic_selector_t**[]))select_traffic_selectors_initiator;
+       this->public.get_traffic_selectors_responder = (size_t(*)(sa_config_t*,traffic_selector_t**[]))get_traffic_selectors_responder;
+       this->public.select_traffic_selectors_responder = (size_t(*)(sa_config_t*,traffic_selector_t*[],size_t,traffic_selector_t**[]))select_traffic_selectors_responder;
        this->public.get_proposals = (size_t(*)(sa_config_t*,u_int8_t[4],u_int8_t[4],child_proposal_t**))get_proposals;
        this->public.select_proposal = (child_proposal_t*(*)(sa_config_t*,u_int8_t[4],u_int8_t[4],child_proposal_t*,size_t))select_proposal;
-       this->public.add_traffic_selector = (void(*)(sa_config_t*,traffic_selector_t*))add_traffic_selector;
+       this->public.add_traffic_selector_initiator = (void(*)(sa_config_t*,traffic_selector_t*))add_traffic_selector_initiator;
+       this->public.add_traffic_selector_responder = (void(*)(sa_config_t*,traffic_selector_t*))add_traffic_selector_responder;
        this->public.add_proposal = (void(*)(sa_config_t*,child_proposal_t*))add_proposal;
        this->public.destroy = (void(*)(sa_config_t*))destroy;
 
@@ -333,8 +399,11 @@ sa_config_t *sa_config_create(id_type_t my_id_type, char *my_id, id_type_t other
        
        /* init private members*/
        this->proposal_equals = proposal_equals;
+       this->select_traffic_selectors = select_traffic_selectors;
+       this->get_traffic_selectors = get_traffic_selectors;
        this->proposals = linked_list_create();
-       this->ts = linked_list_create();
+       this->ts_initiator = linked_list_create();
+       this->ts_responder = linked_list_create();
 
        return (&this->public);
 }
index b3fb41a..bbeec7f 100644 (file)
@@ -113,7 +113,7 @@ struct sa_config_t {
        auth_method_t (*get_auth_method) (sa_config_t *this);
        
        /**
-        * @brief Get configured traffic selectors.
+        * @brief Get configured traffic selectors for initiator site.
         * 
         * Returns a pointer to an allocated array, in which
         * pointers to traffic selectors are stored.
@@ -125,10 +125,26 @@ struct sa_config_t {
         * @param[out]traffic_selectors pointer where traffic selectors will be allocated
         * @return                                              number of returned traffic selectors
         */
-       size_t (*get_traffic_selectors) (sa_config_t *this, traffic_selector_t **traffic_selectors[]);
+       size_t (*get_traffic_selectors_initiator) (sa_config_t *this, traffic_selector_t **traffic_selectors[]);
        
+               
        /**
-        * @brief Select traffic selectors from a supplied list.
+        * @brief Get configured traffic selectors for responder site.
+        * 
+        * Returns a pointer to an allocated array, in which
+        * pointers to traffic selectors are stored.
+        * 
+        * @warning Resulting pointer array must be freed!
+        * @warning Traffic selectors in array must be destroyed!
+        * 
+        * @param this                                  calling object
+        * @param[out]traffic_selectors pointer where traffic selectors will be allocated
+        * @return                                              number of returned traffic selectors
+        */
+       size_t (*get_traffic_selectors_responder) (sa_config_t *this, traffic_selector_t **traffic_selectors[]);
+       
+       /**
+        * @brief Select traffic selectors from a supplied list for initiator.
         * 
         * Returns a pointer to an allocated array, in which
         * pointers to traffic selectors are stored.
@@ -142,7 +158,24 @@ struct sa_config_t {
         * @param[out]traffic_selectors pointer where selected traffic selectors will be allocated
         * @return                                              number of selected traffic selectors
         */
-       size_t (*select_traffic_selectors) (sa_config_t *this, traffic_selector_t *supplied[], size_t count, traffic_selector_t **selected[]);
+       size_t (*select_traffic_selectors_initiator) (sa_config_t *this, traffic_selector_t *supplied[], size_t count, traffic_selector_t **selected[]);
+               
+       /**
+        * @brief Select traffic selectors from a supplied list for responder.
+        * 
+        * Returns a pointer to an allocated array, in which
+        * pointers to traffic selectors are stored.
+        * 
+        * @warning Resulting pointer array must be freed!
+        * @warning Traffic selectors in array must be destroyed!
+        * 
+        * @param this                                  calling object
+        * @param supplied                              pointer to an array of ts to select from.
+        * @param count                                 number of ts stored at supplied
+        * @param[out]traffic_selectors pointer where selected traffic selectors will be allocated
+        * @return                                              number of selected traffic selectors
+        */
+       size_t (*select_traffic_selectors_responder) (sa_config_t *this, traffic_selector_t *supplied[], size_t count, traffic_selector_t **selected[]);
        
        /**
         * @brief Get the list of proposals for this config.
@@ -168,7 +201,19 @@ struct sa_config_t {
        child_proposal_t* (*select_proposal) (sa_config_t *this, u_int8_t ah_spi[4], u_int8_t esp_spi[4], child_proposal_t *supplied, size_t count);
        
        /**
-        * @brief Add a traffic selector to the list. 
+        * @brief Add a traffic selector to the list for initiator. 
+        * 
+        * Added proposal will be cloned.
+        * 
+        * @warning Do not add while other threads are reading.
+        * 
+        * @param this                                  calling object
+        * @param traffic_selector              traffic_selector to add
+        */
+       void (*add_traffic_selector_initiator) (sa_config_t *this, traffic_selector_t *traffic_selector);
+       
+       /**
+        * @brief Add a traffic selector to the list for responder. 
         * 
         * Added proposal will be cloned.
         * 
@@ -177,7 +222,7 @@ struct sa_config_t {
         * @param this                                  calling object
         * @param traffic_selector              traffic_selector to add
         */
-       void (*add_traffic_selector) (sa_config_t *this, traffic_selector_t *traffic_selector);
+       void (*add_traffic_selector_responder) (sa_config_t *this, traffic_selector_t *traffic_selector);
        
        /**
         * @brief Add a proposal to the list. 
index 6153e95..0e6ed99 100644 (file)
@@ -236,25 +236,28 @@ static iterator_t * create_traffic_selector_substructure_iterator (private_ts_pa
        return this->traffic_selectors->create_iterator(this->traffic_selectors,forward);
 }
 
+/**
+ * Implementation of ts_payload_t.get_traffic_selectors.
+ */
 static size_t get_traffic_selectors(private_ts_payload_t *this, traffic_selector_t **traffic_selectors[])
 {
        traffic_selector_t **ts;
        iterator_t *iterator;
        int i = 0;
        
-       //ts = allocator_alloc(sizeof(traffic_selector_t*) * this->number_of_traffic_selectors);
+       ts = allocator_alloc(sizeof(traffic_selector_t*) * this->number_of_traffic_selectors);
        iterator = this->traffic_selectors->create_iterator(this->traffic_selectors, TRUE);
-       int x = this->traffic_selectors->get_count(this->traffic_selectors);
-       while (iterator->has_next)
+       while (iterator->has_next(iterator))
        {
                traffic_selector_substructure_t *ts_substructure;
                iterator->current(iterator, (void**)&ts_substructure);
-               //ts[i] = ts_substructure->get_traffic_selector(ts_substructure);
+               ts[i] = ts_substructure->get_traffic_selector(ts_substructure);
                i++;
        }
+       iterator->destroy(iterator);
        
        /* return values */
-       //*traffic_selectors = ts;
+       *traffic_selectors = ts;
        return this->number_of_traffic_selectors;
 }
 
index c00a385..10808c1 100644 (file)
@@ -27,6 +27,7 @@
 #include <config/traffic_selector.h>
 #include <utils/allocator.h>
 #include <utils/logger.h>
+#include <encoding/payloads/ts_payload.h>
 
 
 /**
@@ -39,6 +40,7 @@ void test_sa_config(tester_t *tester)
        child_proposal_t prop[3], *prop_result;
        size_t count;
        logger_t *logger;
+       ts_payload_t *ts_payload;
        
        u_int8_t spi[4] = {0x01,0x02,0x03,0x04};
        
@@ -132,21 +134,36 @@ void test_sa_config(tester_t *tester)
        /* icmp request, should be discarded */
        ts_request[3] = traffic_selector_create_from_string(1, TS_IPV4_ADDR_RANGE, "0.0.0.0", 0, "255.255.255.255", 65535);
        
-       sa_config->add_traffic_selector(sa_config, ts_policy[0]);
-       sa_config->add_traffic_selector(sa_config, ts_policy[1]);
-       sa_config->add_traffic_selector(sa_config, ts_policy[2]);
+       sa_config->add_traffic_selector_initiator(sa_config, ts_policy[0]);
+       sa_config->add_traffic_selector_initiator(sa_config, ts_policy[1]);
+       sa_config->add_traffic_selector_initiator(sa_config, ts_policy[2]);
        
-       count = sa_config->get_traffic_selectors(sa_config, &ts_result);
+       count = sa_config->get_traffic_selectors_initiator(sa_config, &ts_result);
        tester->assert_true(tester, (count == 3), "ts get count");
        ts_result[0]->destroy(ts_result[0]);
        ts_result[0]->destroy(ts_result[1]);
        ts_result[0]->destroy(ts_result[2]);
        allocator_free(ts_result);
        
-       count = sa_config->select_traffic_selectors(sa_config, &ts_request[0], 4, &ts_result);
+       count = sa_config->select_traffic_selectors_initiator(sa_config, &ts_request[0], 4, &ts_result);
        tester->assert_true(tester, (count == 3), "ts select count");
        
        
+       /* store and restore into ts payload, tricky tricky */
+       ts_payload = ts_payload_create_from_traffic_selectors(TRUE, ts_result, count);
+       
+       /* destroy */
+       ts_result[0]->destroy(ts_result[0]);
+       ts_result[0]->destroy(ts_result[1]); 
+       ts_result[0]->destroy(ts_result[2]);
+       allocator_free(ts_result);
+       
+       /* get them again out of the payload */
+       count = ts_payload->get_traffic_selectors(ts_payload, &ts_result);
+       ts_payload->destroy(ts_payload);
+       
+       
+       
        int i;
        for (i = 0; i<count; i++)
        {
@@ -178,10 +195,12 @@ void test_sa_config(tester_t *tester)
                allocator_free(ta_ref.ptr);
        }
 
+
+       /* destroy */
        ts_result[0]->destroy(ts_result[0]);
-       ts_result[0]->destroy(ts_result[1]);
+       ts_result[0]->destroy(ts_result[1]); 
        ts_result[0]->destroy(ts_result[2]);
-       allocator_free(ts_result);
+       allocator_free(ts_result);      
        
        ts_policy[0]->destroy(ts_policy[0]);
        ts_policy[1]->destroy(ts_policy[1]);
index 3419915..53e81f9 100644 (file)
@@ -137,7 +137,7 @@ daemon_t *daemon_create()
        charon->kill = daemon_kill;
        
        charon->logger_manager = logger_manager_create(0);
-       charon->socket = socket_create(4600);
+       charon->socket = socket_create(4601);
        charon->ike_sa_manager = ike_sa_manager_create();
        charon->job_queue = job_queue_create();
        charon->event_queue = event_queue_create();
@@ -214,8 +214,8 @@ int main()
        tester_t *tester = tester_create(test_output, FALSE);
        
 
-       tester->perform_tests(tester,all_tests);
-//     tester->perform_test(tester,&parser_test2); 
+//     tester->perform_tests(tester,all_tests);
+       tester->perform_test(tester,&sa_config_test); 
        
        
        tester->destroy(tester);