Replaced more complex iterator usages.
[strongswan.git] / src / libcharon / sa / connect_manager.c
index fad0813..aa765a9 100644 (file)
@@ -476,19 +476,19 @@ static status_t get_initiated_by_ids(private_connect_manager_t *this,
 static void remove_initiated(private_connect_manager_t *this,
                                                         initiated_t *initiated)
 {
 static void remove_initiated(private_connect_manager_t *this,
                                                         initiated_t *initiated)
 {
-       iterator_t *iterator;
+       enumerator_t *enumerator;
        initiated_t *current;
 
        initiated_t *current;
 
-       iterator = this->initiated->create_iterator(this->initiated, TRUE);
-       while (iterator->iterate(iterator, (void**)&current))
+       enumerator = this->initiated->create_enumerator(this->initiated);
+       while (enumerator->enumerate(enumerator, (void**)&current))
        {
                if (current == initiated)
                {
        {
                if (current == initiated)
                {
-                       iterator->remove(iterator);
+                       this->initiated->remove_at(this->initiated, enumerator);
                        break;
                }
        }
                        break;
                }
        }
-       iterator->destroy(iterator);
+       enumerator->destroy(enumerator);
 }
 
 /**
 }
 
 /**
@@ -514,19 +514,19 @@ static status_t get_checklist_by_id(private_connect_manager_t *this,
 static void remove_checklist(private_connect_manager_t *this,
                                                         check_list_t *checklist)
 {
 static void remove_checklist(private_connect_manager_t *this,
                                                         check_list_t *checklist)
 {
-       iterator_t *iterator;
+       enumerator_t *enumerator;
        check_list_t *current;
 
        check_list_t *current;
 
-       iterator = this->checklists->create_iterator(this->checklists, TRUE);
-       while (iterator->iterate(iterator, (void**)&current))
+       enumerator = this->checklists->create_enumerator(this->checklists);
+       while (enumerator->enumerate(enumerator, (void**)&current))
        {
                if (current == checklist)
                {
        {
                if (current == checklist)
                {
-                       iterator->remove(iterator);
+                       this->checklists->remove_at(this->checklists, enumerator);
                        break;
                }
        }
                        break;
                }
        }
-       iterator->destroy(iterator);
+       enumerator->destroy(enumerator);
 }
 
 /**
 }
 
 /**
@@ -550,26 +550,15 @@ static status_t endpoints_contain(linked_list_t *endpoints, host_t *host,
  */
 static void insert_pair_by_priority(linked_list_t *pairs, endpoint_pair_t *pair)
 {
  */
 static void insert_pair_by_priority(linked_list_t *pairs, endpoint_pair_t *pair)
 {
-       iterator_t *iterator;
+       enumerator_t *enumerator = pairs->create_enumerator(pairs);
        endpoint_pair_t *current;
        endpoint_pair_t *current;
-       bool inserted = FALSE;
-
-       iterator = pairs->create_iterator(pairs, TRUE);
-       while (iterator->iterate(iterator, (void**)&current))
-       {
-               if (current->priority < pair->priority)
-               {
-                       iterator->insert_before(iterator, pair);
-                       inserted = TRUE;
-                       break;
-               }
-       }
-       iterator->destroy(iterator);
-
-       if (!inserted)
+       while (enumerator->enumerate(enumerator, (void**)&current) &&
+                  current->priority >= pair->priority)
        {
        {
-               pairs->insert_last(pairs, pair);
+               continue;
        }
        }
+       pairs->insert_before(pairs, enumerator, pair);
+       enumerator->destroy(enumerator);
 }
 
 /**
 }
 
 /**
@@ -631,14 +620,14 @@ static bool match_waiting_pair(endpoint_pair_t *current)
 static status_t get_triggered_pair(check_list_t *checklist,
                                                                   endpoint_pair_t **pair)
 {
 static status_t get_triggered_pair(check_list_t *checklist,
                                                                   endpoint_pair_t **pair)
 {
-       iterator_t *iterator;
+       enumerator_t *enumerator;
        endpoint_pair_t *current;
        status_t status = NOT_FOUND;
 
        endpoint_pair_t *current;
        status_t status = NOT_FOUND;
 
-       iterator = checklist->triggered->create_iterator(checklist->triggered, TRUE);
-       while (iterator->iterate(iterator, (void**)&current))
+       enumerator = checklist->triggered->create_enumerator(checklist->triggered);
+       while (enumerator->enumerate(enumerator, (void**)&current))
        {
        {
-               iterator->remove(iterator);
+               checklist->triggered->remove_at(checklist->triggered, enumerator);
 
                if (current->state == CHECK_WAITING)
                {
 
                if (current->state == CHECK_WAITING)
                {
@@ -650,7 +639,7 @@ static status_t get_triggered_pair(check_list_t *checklist,
                        break;
                }
        }
                        break;
                }
        }
-       iterator->destroy(iterator);
+       enumerator->destroy(enumerator);
 
        return status;
 }
 
        return status;
 }
@@ -660,17 +649,17 @@ static status_t get_triggered_pair(check_list_t *checklist,
  */
 static void print_checklist(check_list_t *checklist)
 {
  */
 static void print_checklist(check_list_t *checklist)
 {
-       iterator_t *iterator;
+       enumerator_t *enumerator;
        endpoint_pair_t *current;
 
        DBG1(DBG_IKE, "pairs on checklist %#B:", &checklist->connect_id);
        endpoint_pair_t *current;
 
        DBG1(DBG_IKE, "pairs on checklist %#B:", &checklist->connect_id);
-       iterator = checklist->pairs->create_iterator(checklist->pairs, TRUE);
-       while (iterator->iterate(iterator, (void**)&current))
+       enumerator = checklist->pairs->create_enumerator(checklist->pairs);
+       while (enumerator->enumerate(enumerator, (void**)&current))
        {
                DBG1(DBG_IKE, " * %#H - %#H (%d)", current->local, current->remote,
                         current->priority);
        }
        {
                DBG1(DBG_IKE, " * %#H - %#H (%d)", current->local, current->remote,
                         current->priority);
        }
-       iterator->destroy(iterator);
+       enumerator->destroy(enumerator);
 }
 
 /**
 }
 
 /**
@@ -679,17 +668,17 @@ static void print_checklist(check_list_t *checklist)
  */
 static void prune_pairs(linked_list_t *pairs)
 {
  */
 static void prune_pairs(linked_list_t *pairs)
 {
-       iterator_t *iterator, *search;
+       enumerator_t *enumerator, *search;
        endpoint_pair_t *current, *other;
        u_int32_t id = 0;
 
        endpoint_pair_t *current, *other;
        u_int32_t id = 0;
 
-       iterator = pairs->create_iterator(pairs, TRUE);
-       search = pairs->create_iterator(pairs, TRUE);
-       while (iterator->iterate(iterator, (void**)&current))
+       enumerator = pairs->create_enumerator(pairs);
+       search = pairs->create_enumerator(pairs);
+       while (enumerator->enumerate(enumerator, (void**)&current))
        {
                current->id = ++id;
 
        {
                current->id = ++id;
 
-               while (search->iterate(search, (void**)&other))
+               while (search->enumerate(search, (void**)&other))
                {
                        if (current == other)
                        {
                {
                        if (current == other)
                        {
@@ -705,14 +694,14 @@ static void prune_pairs(linked_list_t *pairs)
                                 * 'current', remove it */
                                DBG1(DBG_IKE, "pruning endpoint pair %#H - %#H with priority %d",
                                         other->local, other->remote, other->priority);
                                 * 'current', remove it */
                                DBG1(DBG_IKE, "pruning endpoint pair %#H - %#H with priority %d",
                                         other->local, other->remote, other->priority);
-                               search->remove(search);
+                               pairs->remove_at(pairs, search);
                                endpoint_pair_destroy(other);
                        }
                }
                                endpoint_pair_destroy(other);
                        }
                }
-               search->reset(search);
+               pairs->reset_enumerator(pairs, search);
        }
        search->destroy(search);
        }
        search->destroy(search);
-       iterator->destroy(iterator);
+       enumerator->destroy(enumerator);
 }
 
 /**
 }
 
 /**
@@ -721,16 +710,16 @@ static void prune_pairs(linked_list_t *pairs)
 static void build_pairs(check_list_t *checklist)
 {
        /* FIXME: limit endpoints and pairs */
 static void build_pairs(check_list_t *checklist)
 {
        /* FIXME: limit endpoints and pairs */
-       iterator_t *iterator_i, *iterator_r;
+       enumerator_t *enumerator_i, *enumerator_r;
        endpoint_notify_t *initiator, *responder;
 
        endpoint_notify_t *initiator, *responder;
 
-       iterator_i = checklist->initiator.endpoints->create_iterator(
-                                                                               checklist->initiator.endpoints, TRUE);
-       while (iterator_i->iterate(iterator_i, (void**)&initiator))
+       enumerator_i = checklist->initiator.endpoints->create_enumerator(
+                                                                               checklist->initiator.endpoints);
+       while (enumerator_i->enumerate(enumerator_i, (void**)&initiator))
        {
        {
-               iterator_r = checklist->responder.endpoints->create_iterator(
-                                                                               checklist->responder.endpoints, TRUE);
-               while (iterator_r->iterate(iterator_r, (void**)&responder))
+               enumerator_r = checklist->responder.endpoints->create_enumerator(
+                                                                               checklist->responder.endpoints);
+               while (enumerator_r->enumerate(enumerator_r, (void**)&responder))
                {
                        if (initiator->get_family(initiator) != responder->get_family(responder))
                        {
                {
                        if (initiator->get_family(initiator) != responder->get_family(responder))
                        {
@@ -740,9 +729,9 @@ static void build_pairs(check_list_t *checklist)
                        insert_pair_by_priority(checklist->pairs, endpoint_pair_create(
                                                        initiator, responder, checklist->is_initiator));
                }
                        insert_pair_by_priority(checklist->pairs, endpoint_pair_create(
                                                        initiator, responder, checklist->is_initiator));
                }
-               iterator_r->destroy(iterator_r);
+               enumerator_r->destroy(enumerator_r);
        }
        }
-       iterator_i->destroy(iterator_i);
+       enumerator_i->destroy(enumerator_i);
 
        print_checklist(checklist);
 
 
        print_checklist(checklist);
 
@@ -895,19 +884,19 @@ static job_requeue_t initiator_finish(callback_data_t *data)
 static void update_checklist_state(private_connect_manager_t *this,
                                                                   check_list_t *checklist)
 {
 static void update_checklist_state(private_connect_manager_t *this,
                                                                   check_list_t *checklist)
 {
-       iterator_t *iterator;
+       enumerator_t *enumerator;
        endpoint_pair_t *current;
        bool in_progress = FALSE, succeeded = FALSE;
 
        endpoint_pair_t *current;
        bool in_progress = FALSE, succeeded = FALSE;
 
-       iterator = checklist->pairs->create_iterator(checklist->pairs, TRUE);
-       while (iterator->iterate(iterator, (void**)&current))
+       enumerator = checklist->pairs->create_enumerator(checklist->pairs);
+       while (enumerator->enumerate(enumerator, (void**)&current))
        {
                switch(current->state)
                {
                        case CHECK_WAITING:
                                /* at least one is still waiting -> checklist remains
                                 * in waiting state */
        {
                switch(current->state)
                {
                        case CHECK_WAITING:
                                /* at least one is still waiting -> checklist remains
                                 * in waiting state */
-                               iterator->destroy(iterator);
+                               enumerator->destroy(enumerator);
                                return;
                        case CHECK_IN_PROGRESS:
                                in_progress = TRUE;
                                return;
                        case CHECK_IN_PROGRESS:
                                in_progress = TRUE;
@@ -919,7 +908,7 @@ static void update_checklist_state(private_connect_manager_t *this,
                                break;
                }
        }
                                break;
                }
        }
-       iterator->destroy(iterator);
+       enumerator->destroy(enumerator);
 
        if (checklist->is_initiator && succeeded && !checklist->is_finishing)
        {
 
        if (checklist->is_initiator && succeeded && !checklist->is_finishing)
        {
@@ -932,7 +921,7 @@ static void update_checklist_state(private_connect_manager_t *this,
 
                callback_data_t *data = callback_data_create(this, checklist->connect_id);
                job_t *job = (job_t*)callback_job_create((callback_job_cb_t)initiator_finish, data, (callback_job_cleanup_t)callback_data_destroy, NULL);
 
                callback_data_t *data = callback_data_create(this, checklist->connect_id);
                job_t *job = (job_t*)callback_job_create((callback_job_cb_t)initiator_finish, data, (callback_job_cleanup_t)callback_data_destroy, NULL);
-               charon->scheduler->schedule_job_ms(charon->scheduler, job, ME_WAIT_TO_FINISH);
+               lib->scheduler->schedule_job_ms(lib->scheduler, job, ME_WAIT_TO_FINISH);
                checklist->is_finishing = TRUE;
        }
 
                checklist->is_finishing = TRUE;
        }
 
@@ -1031,7 +1020,7 @@ static void queue_retransmission(private_connect_manager_t *this, check_list_t *
        DBG2(DBG_IKE, "scheduling retransmission %d of pair '%d' in %dms",
                 retransmission, pair->id, rto);
 
        DBG2(DBG_IKE, "scheduling retransmission %d of pair '%d' in %dms",
                 retransmission, pair->id, rto);
 
-       charon->scheduler->schedule_job_ms(charon->scheduler, (job_t*)job, rto);
+       lib->scheduler->schedule_job_ms(lib->scheduler, (job_t*)job, rto);
 }
 
 /**
 }
 
 /**
@@ -1170,7 +1159,7 @@ static void schedule_checks(private_connect_manager_t *this, check_list_t *check
 {
        callback_data_t *data = callback_data_create(this, checklist->connect_id);
        checklist->sender = (job_t*)callback_job_create((callback_job_cb_t)sender, data, (callback_job_cleanup_t)callback_data_destroy, NULL);
 {
        callback_data_t *data = callback_data_create(this, checklist->connect_id);
        checklist->sender = (job_t*)callback_job_create((callback_job_cb_t)sender, data, (callback_job_cleanup_t)callback_data_destroy, NULL);
-       charon->scheduler->schedule_job_ms(charon->scheduler, checklist->sender, time);
+       lib->scheduler->schedule_job_ms(lib->scheduler, checklist->sender, time);
 }
 
 /**
 }
 
 /**
@@ -1185,8 +1174,9 @@ static job_requeue_t initiate_mediated(initiate_data_t *data)
        if (get_best_valid_pair(checklist, &pair) == SUCCESS)
        {
                ike_sa_id_t *waiting_sa;
        if (get_best_valid_pair(checklist, &pair) == SUCCESS)
        {
                ike_sa_id_t *waiting_sa;
-               iterator_t *iterator = initiated->mediated->create_iterator(initiated->mediated, TRUE);
-               while (iterator->iterate(iterator, (void**)&waiting_sa))
+               enumerator_t *enumerator = initiated->mediated->create_enumerator(
+                                                                                                               initiated->mediated);
+               while (enumerator->enumerate(enumerator, (void**)&waiting_sa))
                {
                        ike_sa_t *sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager, waiting_sa);
                        if (sa->initiate_mediated(sa, pair->local, pair->remote, checklist->connect_id) != SUCCESS)
                {
                        ike_sa_t *sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager, waiting_sa);
                        if (sa->initiate_mediated(sa, pair->local, pair->remote, checklist->connect_id) != SUCCESS)
@@ -1194,9 +1184,12 @@ static job_requeue_t initiate_mediated(initiate_data_t *data)
                                DBG1(DBG_IKE, "establishing mediated connection failed");
                                charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, sa);
                        }
                                DBG1(DBG_IKE, "establishing mediated connection failed");
                                charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, sa);
                        }
-                       charon->ike_sa_manager->checkin(charon->ike_sa_manager, sa);
+                       else
+                       {
+                               charon->ike_sa_manager->checkin(charon->ike_sa_manager, sa);
+                       }
                }
                }
-               iterator->destroy(iterator);
+               enumerator->destroy(enumerator);
        }
        else
        {
        }
        else
        {
@@ -1222,7 +1215,7 @@ static void finish_checks(private_connect_manager_t *this, check_list_t *checkli
 
                        initiate_data_t *data = initiate_data_create(checklist, initiated);
                        job_t *job = (job_t*)callback_job_create((callback_job_cb_t)initiate_mediated, data, (callback_job_cleanup_t)initiate_data_destroy, NULL);
 
                        initiate_data_t *data = initiate_data_create(checklist, initiated);
                        job_t *job = (job_t*)callback_job_create((callback_job_cb_t)initiate_mediated, data, (callback_job_cleanup_t)initiate_data_destroy, NULL);
-                       charon->processor->queue_job(charon->processor, job);
+                       lib->processor->queue_job(lib->processor, job);
                        return;
                }
                else
                        return;
                }
                else
@@ -1357,7 +1350,7 @@ static void process_request(private_connect_manager_t *this, check_t *check,
  */
 static void process_check(private_connect_manager_t *this, message_t *message)
 {
  */
 static void process_check(private_connect_manager_t *this, message_t *message)
 {
-       if (message->parse_body(message, NULL, NULL) != SUCCESS)
+       if (message->parse_body(message, NULL) != SUCCESS)
        {
                DBG1(DBG_IKE, "%N %s with message ID %d processing failed",
                         exchange_type_names, message->get_exchange_type(message),
        {
                DBG1(DBG_IKE, "%N %s with message ID %d processing failed",
                         exchange_type_names, message->get_exchange_type(message),
@@ -1471,15 +1464,15 @@ static void check_and_initiate(private_connect_manager_t *this,
        }
 
        ike_sa_id_t *waiting_sa;
        }
 
        ike_sa_id_t *waiting_sa;
-       iterator_t *iterator = initiated->mediated->create_iterator(
-                                                                                                       initiated->mediated, TRUE);
-       while (iterator->iterate(iterator, (void**)&waiting_sa))
+       enumerator_t *enumerator = initiated->mediated->create_enumerator(
+                                                                                                               initiated->mediated);
+       while (enumerator->enumerate(enumerator, (void**)&waiting_sa))
        {
                job_t *job = (job_t*)reinitiate_mediation_job_create(mediation_sa,
                                                                                                                         waiting_sa);
        {
                job_t *job = (job_t*)reinitiate_mediation_job_create(mediation_sa,
                                                                                                                         waiting_sa);
-               charon->processor->queue_job(charon->processor, job);
+               lib->processor->queue_job(lib->processor, job);
        }
        }
-       iterator->destroy(iterator);
+       enumerator->destroy(enumerator);
 
        this->mutex->unlock(this->mutex);
 }
 
        this->mutex->unlock(this->mutex);
 }