diff options
26 files changed, 311 insertions, 550 deletions
diff --git a/src/charon/bus/bus.c b/src/charon/bus/bus.c index 028fd37c9..e207dfa89 100644 --- a/src/charon/bus/bus.c +++ b/src/charon/bus/bus.c @@ -365,14 +365,7 @@ static void signal_(private_bus_t *this, signal_t signal, level_t level, */ static void destroy(private_bus_t *this) { - active_listener_t *listener; - while (this->active_listeners->remove_last(this->active_listeners, - (void**)&listener) == SUCCESS) - { - free(listener); - } - - this->active_listeners->destroy(this->active_listeners); + this->active_listeners->destroy_function(this->active_listeners, free); this->listeners->destroy(this->listeners); free(this); } diff --git a/src/charon/config/connections/connection.c b/src/charon/config/connections/connection.c index 5dfc00eb2..dcc433c9f 100644 --- a/src/charon/config/connections/connection.c +++ b/src/charon/config/connections/connection.c @@ -333,14 +333,7 @@ static void destroy(private_connection_t *this) { if (ref_put(&this->refcount)) { - proposal_t *proposal; - - while (this->proposals->remove_last(this->proposals, (void**)&proposal) == SUCCESS) - { - proposal->destroy(proposal); - } - this->proposals->destroy(this->proposals); - + this->proposals->destroy_offset(this->proposals, offsetof(proposal_t, destroy)); this->my_host->destroy(this->my_host); this->other_host->destroy(this->other_host); free(this->name); diff --git a/src/charon/config/connections/local_connection_store.c b/src/charon/config/connections/local_connection_store.c index 8748fb730..30e7e9c8a 100644 --- a/src/charon/config/connections/local_connection_store.c +++ b/src/charon/config/connections/local_connection_store.c @@ -215,14 +215,8 @@ static iterator_t* create_iterator(private_local_connection_store_t *this) */ static void destroy (private_local_connection_store_t *this) { - connection_t *connection; - pthread_mutex_lock(&(this->mutex)); - while (this->connections->remove_last(this->connections, (void**)&connection) == SUCCESS) - { - connection->destroy(connection); - } - this->connections->destroy(this->connections); + this->connections->destroy_offset(this->connections, offsetof(connection_t, destroy)); pthread_mutex_unlock(&(this->mutex)); free(this); } diff --git a/src/charon/config/credentials/local_credential_store.c b/src/charon/config/credentials/local_credential_store.c index be9ac6acb..309f4a2fe 100644 --- a/src/charon/config/credentials/local_credential_store.c +++ b/src/charon/config/credentials/local_credential_store.c @@ -63,16 +63,8 @@ struct shared_key_t { */ static void shared_key_destroy(shared_key_t *this) { - identification_t *id; - - /* destroy peer id list */ - while (this->peers->remove_last(this->peers, (void**)&id) == SUCCESS) - { - id->destroy(id); - } - this->peers->destroy(this->peers); + this->peers->destroy_offset(this->peers, offsetof(identification_t, destroy)); chunk_free(&this->secret); - free(this); } @@ -1077,48 +1069,11 @@ error: */ static void destroy(private_local_credential_store_t *this) { - x509_t *cert; - crl_t *crl; - rsa_private_key_t *key; - shared_key_t *shared_key; - - /* destroy cert list */ - while (this->certs->remove_last(this->certs, (void**)&cert) == SUCCESS) - { - cert->destroy(cert); - } - this->certs->destroy(this->certs); - - /* destroy ca cert list */ - while (this->ca_certs->remove_last(this->ca_certs, (void**)&cert) == SUCCESS) - { - cert->destroy(cert); - } - this->ca_certs->destroy(this->ca_certs); - - /* destroy crl list */ - pthread_mutex_lock(&(this->crls_mutex)); - while (this->crls->remove_last(this->crls, (void**)&crl) == SUCCESS) - { - crl->destroy(crl); - } - this->crls->destroy(this->crls); - pthread_mutex_unlock(&(this->crls_mutex)); - - /* destroy private key list */ - while (this->private_keys->remove_last(this->private_keys, (void**)&key) == SUCCESS) - { - key->destroy(key); - } - this->private_keys->destroy(this->private_keys); - - /* destroy shared keys list */ - while (this->shared_keys->remove_last(this->shared_keys, (void**)&shared_key) == SUCCESS) - { - shared_key_destroy(shared_key); - } - this->shared_keys->destroy(this->shared_keys); - + this->certs->destroy_offset(this->certs, offsetof(x509_t, destroy)); + this->ca_certs->destroy_offset(this->ca_certs, offsetof(x509_t, destroy)); + this->crls->destroy_offset(this->crls, offsetof(crl_t, destroy)); + this->private_keys->destroy_offset(this->private_keys, offsetof(rsa_private_key_t, destroy)); + this->shared_keys->destroy_function(this->shared_keys, (void*)shared_key_destroy); free(this); } @@ -1128,7 +1083,7 @@ static void destroy(private_local_credential_store_t *this) local_credential_store_t * local_credential_store_create(bool strict) { private_local_credential_store_t *this = malloc_thing(private_local_credential_store_t); - + this->public.credential_store.get_shared_key = (status_t (*) (credential_store_t*,identification_t*,identification_t*,chunk_t*))get_shared_key; this->public.credential_store.get_rsa_public_key = (rsa_public_key_t*(*)(credential_store_t*,identification_t*))get_rsa_public_key; this->public.credential_store.get_rsa_private_key = (rsa_private_key_t* (*) (credential_store_t*,rsa_public_key_t*))get_rsa_private_key; diff --git a/src/charon/config/policies/local_policy_store.c b/src/charon/config/policies/local_policy_store.c index 577b83a28..1fca08107 100644 --- a/src/charon/config/policies/local_policy_store.c +++ b/src/charon/config/policies/local_policy_store.c @@ -69,7 +69,6 @@ static bool contains_traffic_selectors(policy_t *policy, bool mine, { linked_list_t *selected; bool contains = FALSE; - traffic_selector_t *to; if (mine) { @@ -83,11 +82,7 @@ static bool contains_traffic_selectors(policy_t *policy, bool mine, { contains = TRUE; } - while (selected->remove_last(selected, (void**)&to) == SUCCESS) - { - to->destroy(to); - } - selected->destroy(selected); + selected->destroy_offset(selected, offsetof(traffic_selector_t, destroy)); return contains; } @@ -249,14 +244,8 @@ static iterator_t* create_iterator(private_local_policy_store_t *this) */ static void destroy(private_local_policy_store_t *this) { - policy_t *policy; - pthread_mutex_lock(&(this->mutex)); - while (this->policies->remove_last(this->policies, (void**)&policy) == SUCCESS) - { - policy->destroy(policy); - } - this->policies->destroy(this->policies); + this->policies->destroy_offset(this->policies, offsetof(policy_t, destroy)); pthread_mutex_unlock(&(this->mutex)); free(this); } diff --git a/src/charon/config/policies/policy.c b/src/charon/config/policies/policy.c index dcae0504c..c9ac7884b 100644 --- a/src/charon/config/policies/policy.c +++ b/src/charon/config/policies/policy.c @@ -432,29 +432,10 @@ static void destroy(private_policy_t *this) { if (ref_put(&this->refcount)) { - proposal_t *proposal; - traffic_selector_t *traffic_selector; - /* delete proposals */ - while(this->proposals->remove_last(this->proposals, (void**)&proposal) == SUCCESS) - { - proposal->destroy(proposal); - } - this->proposals->destroy(this->proposals); - - /* delete traffic selectors */ - while(this->my_ts->remove_last(this->my_ts, (void**)&traffic_selector) == SUCCESS) - { - traffic_selector->destroy(traffic_selector); - } - this->my_ts->destroy(this->my_ts); - - /* delete traffic selectors */ - while(this->other_ts->remove_last(this->other_ts, (void**)&traffic_selector) == SUCCESS) - { - traffic_selector->destroy(traffic_selector); - } - this->other_ts->destroy(this->other_ts); + this->proposals->destroy_offset(this->proposals, offsetof(proposal_t, destroy)); + this->my_ts->destroy_offset(this->my_ts, offsetof(traffic_selector_t, destroy)); + this->other_ts->destroy_offset(this->other_ts, offsetof(traffic_selector_t, destroy)); /* delete certification authorities */ if (this->my_ca) diff --git a/src/charon/config/proposal.c b/src/charon/config/proposal.c index fac0e31c2..6b45d48bf 100644 --- a/src/charon/config/proposal.c +++ b/src/charon/config/proposal.c @@ -392,21 +392,6 @@ static proposal_t *clone_(private_proposal_t *this) return &clone->public; } -/** - * Frees all list items and destroys the list - */ -static void free_algo_list(linked_list_t *list) -{ - algorithm_t *algo; - - while(list->get_count(list) > 0) - { - list->remove_last(list, (void**)&algo); - free(algo); - } - list->destroy(list); -} - static status_t add_string_algo(private_proposal_t *this, chunk_t alg) { if (strncmp(alg.ptr, "aes128", alg.len) == 0) @@ -489,11 +474,11 @@ static status_t add_string_algo(private_proposal_t *this, chunk_t alg) */ static void destroy(private_proposal_t *this) { - free_algo_list(this->encryption_algos); - free_algo_list(this->integrity_algos); - free_algo_list(this->prf_algos); - free_algo_list(this->dh_groups); - free_algo_list(this->esns); + this->encryption_algos->destroy_function(this->encryption_algos, free); + this->integrity_algos->destroy_function(this->integrity_algos, free); + this->prf_algos->destroy_function(this->prf_algos, free); + this->dh_groups->destroy_function(this->dh_groups, free); + this->esns->destroy_function(this->esns, free); free(this); } diff --git a/src/charon/encoding/payloads/cp_payload.c b/src/charon/encoding/payloads/cp_payload.c index f1297e444..580a0c64a 100644 --- a/src/charon/encoding/payloads/cp_payload.c +++ b/src/charon/encoding/payloads/cp_payload.c @@ -240,20 +240,11 @@ static config_type_t get_config_type (private_cp_payload_t *this) /** * Implementation of payload_t.destroy and cp_payload_t.destroy. */ -static status_t destroy(private_cp_payload_t *this) +static void destroy(private_cp_payload_t *this) { - /* all attributes are getting destroyed */ - while (this->attributes->get_count(this->attributes) > 0) - { - configuration_attribute_t *current_attribute; - this->attributes->remove_last(this->attributes,(void **)¤t_attribute); - current_attribute->destroy(current_attribute); - } - this->attributes->destroy(this->attributes); - + this->attributes->destroy_offset(this->attributes, + offsetof(configuration_attribute_t, destroy)); free(this); - - return SUCCESS; } /* diff --git a/src/charon/encoding/payloads/encryption_payload.c b/src/charon/encoding/payloads/encryption_payload.c index d32e6c902..c33bea781 100644 --- a/src/charon/encoding/payloads/encryption_payload.c +++ b/src/charon/encoding/payloads/encryption_payload.c @@ -600,14 +600,7 @@ static status_t verify_signature(private_encryption_payload_t *this, chunk_t dat */ static void destroy(private_encryption_payload_t *this) { - /* all proposals are getting destroyed */ - while (this->payloads->get_count(this->payloads) > 0) - { - payload_t *current_payload; - this->payloads->remove_last(this->payloads,(void **)¤t_payload); - current_payload->destroy(current_payload); - } - this->payloads->destroy(this->payloads); + this->payloads->destroy_offset(this->payloads, offsetof(payload_t, destroy)); free(this->encrypted.ptr); free(this->decrypted.ptr); free(this); diff --git a/src/charon/encoding/payloads/proposal_substructure.c b/src/charon/encoding/payloads/proposal_substructure.c index 58c1dd29b..5842f6e8b 100644 --- a/src/charon/encoding/payloads/proposal_substructure.c +++ b/src/charon/encoding/payloads/proposal_substructure.c @@ -472,28 +472,12 @@ static private_proposal_substructure_t* clone_(private_proposal_substructure_t * * Implements payload_t's and proposal_substructure_t's destroy function. * See #payload_s.destroy or proposal_substructure_s.destroy for description. */ -static status_t destroy(private_proposal_substructure_t *this) +static void destroy(private_proposal_substructure_t *this) { - /* all proposals are getting destroyed */ - while (this->transforms->get_count(this->transforms) > 0) - { - transform_substructure_t *current_transform; - if (this->transforms->remove_last(this->transforms,(void **)¤t_transform) != SUCCESS) - { - break; - } - current_transform->destroy(current_transform); - } - this->transforms->destroy(this->transforms); - - if (this->spi.ptr != NULL) - { - free(this->spi.ptr); - } - + this->transforms->destroy_offset(this->transforms, + offsetof(transform_substructure_t, destroy)); + chunk_free(&this->spi); free(this); - - return SUCCESS; } /* diff --git a/src/charon/encoding/payloads/sa_payload.c b/src/charon/encoding/payloads/sa_payload.c index e03770d1e..f0a13eb58 100644 --- a/src/charon/encoding/payloads/sa_payload.c +++ b/src/charon/encoding/payloads/sa_payload.c @@ -165,17 +165,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 **)¤t_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; } diff --git a/src/charon/encoding/payloads/transform_substructure.c b/src/charon/encoding/payloads/transform_substructure.c index 59e335a74..944336cc7 100644 --- a/src/charon/encoding/payloads/transform_substructure.c +++ b/src/charon/encoding/payloads/transform_substructure.c @@ -354,15 +354,8 @@ static status_t get_key_length(private_transform_substructure_t *this, u_int16_t */ static void destroy(private_transform_substructure_t *this) { - /* all proposals are getting destroyed */ - while (this->attributes->get_count(this->attributes) > 0) - { - transform_attribute_t *current_attribute; - this->attributes->remove_last(this->attributes,(void **)¤t_attribute); - current_attribute->destroy(current_attribute); - } - this->attributes->destroy(this->attributes); - + this->attributes->destroy_offset(this->attributes, + offsetof(transform_attribute_t, destroy)); free(this); } diff --git a/src/charon/encoding/payloads/ts_payload.c b/src/charon/encoding/payloads/ts_payload.c index 7cd08eeed..cce2fc282 100644 --- a/src/charon/encoding/payloads/ts_payload.c +++ b/src/charon/encoding/payloads/ts_payload.c @@ -280,18 +280,9 @@ static linked_list_t *get_traffic_selectors(private_ts_payload_t *this) */ static void destroy(private_ts_payload_t *this) { - while (this->traffic_selectors->get_count(this->traffic_selectors) > 0) - { - payload_t *current_traffic_selector; - - this->traffic_selectors->remove_last(this->traffic_selectors,(void **) ¤t_traffic_selector); - - current_traffic_selector->destroy(current_traffic_selector); - } - - this->traffic_selectors->destroy(this->traffic_selectors); - - free(this); + this->traffic_selectors->destroy_offset(this->traffic_selectors, + offsetof(payload_t, destroy)); + free(this); } /* diff --git a/src/charon/queues/event_queue.c b/src/charon/queues/event_queue.c index 10f139e7a..3a5b21039 100644 --- a/src/charon/queues/event_queue.c +++ b/src/charon/queues/event_queue.c @@ -48,6 +48,15 @@ struct event_t { job_t * job; }; +/** + * destroy an event and its job + */ +static void event_destroy(event_t *event) +{ + event->job->destroy(event->job); + free(event); +} + typedef struct private_event_queue_t private_event_queue_t; /** @@ -262,15 +271,7 @@ static void add_relative(event_queue_t *this, job_t *job, u_int32_t ms) */ static void event_queue_destroy(private_event_queue_t *this) { - event_t *event; - while (this->list->remove_last(this->list, (void**)&event) == SUCCESS) - { - event->job->destroy(event->job); - free(event); - } - this->list->destroy(this->list); - pthread_mutex_destroy(&(this->mutex)); - pthread_cond_destroy(&(this->condvar)); + this->list->destroy_function(this->list, (void*)event_destroy); free(this); } diff --git a/src/charon/queues/job_queue.c b/src/charon/queues/job_queue.c index d33cd5ae4..2310ca6ff 100644 --- a/src/charon/queues/job_queue.c +++ b/src/charon/queues/job_queue.c @@ -114,14 +114,7 @@ static void add(private_job_queue_t *this, job_t *job) */ static void job_queue_destroy (private_job_queue_t *this) { - job_t *job; - while (this->list->remove_last(this->list, (void**)&job) == SUCCESS) - { - job->destroy(job); - } - this->list->destroy(this->list); - pthread_mutex_destroy(&(this->mutex)); - pthread_cond_destroy(&(this->condvar)); + this->list->destroy_offset(this->list, offsetof(job_t, destroy)); free(this); } diff --git a/src/charon/queues/send_queue.c b/src/charon/queues/send_queue.c index aa3ac9228..b0c77515c 100644 --- a/src/charon/queues/send_queue.c +++ b/src/charon/queues/send_queue.c @@ -120,19 +120,11 @@ static void add(private_send_queue_t *this, packet_t *packet) */ static void destroy (private_send_queue_t *this) { - packet_t *packet; - while (this->list->remove_last(this->list, (void**)&packet) == SUCCESS) - { - packet->destroy(packet); - } - this->list->destroy(this->list); - pthread_mutex_destroy(&(this->mutex)); - pthread_cond_destroy(&(this->condvar)); + this->list->destroy_offset(this->list, offsetof(packet_t, destroy)); free(this); } /* - * * Documented in header */ send_queue_t *send_queue_create(void) diff --git a/src/charon/sa/ike_sa.c b/src/charon/sa/ike_sa.c index c7e0a51ad..105285be7 100644 --- a/src/charon/sa/ike_sa.c +++ b/src/charon/sa/ike_sa.c @@ -1048,19 +1048,6 @@ static status_t acquire(private_ike_sa_t *this, u_int32_t reqid) } /** - * destroy a list of traffic selectors - */ -static void ts_list_destroy(linked_list_t *list) -{ - traffic_selector_t *ts; - while (list->remove_last(list, (void**)&ts) == SUCCESS) - { - ts->destroy(ts); - } - list->destroy(list); -} - -/** * compare two lists of traffic selectors for equality */ static bool ts_list_equals(linked_list_t *l1, linked_list_t *l2) @@ -1114,18 +1101,17 @@ static status_t route(private_ike_sa_t *this, connection_t *connection, policy_t other_ts_conf = policy->get_other_traffic_selectors(policy, this->other_host); if (ts_list_equals(my_ts, my_ts_conf) && - ts_list_equals(other_ts, other_ts_conf)) + ts_list_equals(other_ts, other_ts_conf)) { - ts_list_destroy(my_ts_conf); - ts_list_destroy(other_ts_conf); iterator->destroy(iterator); + my_ts_conf->destroy_offset(my_ts_conf, offsetof(traffic_selector_t, destroy)); + other_ts_conf->destroy_offset(other_ts_conf, offsetof(traffic_selector_t, destroy)); SIG(SIG_CHILD_FAILED, "CHILD_SA with such a policy " "already routed"); - return FAILED; } - ts_list_destroy(my_ts_conf); - ts_list_destroy(other_ts_conf); + my_ts_conf->destroy_offset(my_ts_conf, offsetof(traffic_selector_t, destroy)); + other_ts_conf->destroy_offset(other_ts_conf, offsetof(traffic_selector_t, destroy)); } } iterator->destroy(iterator); @@ -1184,12 +1170,11 @@ static status_t route(private_ike_sa_t *this, connection_t *connection, policy_t my_ts = policy->get_my_traffic_selectors(policy, this->my_host); other_ts = policy->get_other_traffic_selectors(policy, this->other_host); status = child_sa->add_policies(child_sa, my_ts, other_ts); - ts_list_destroy(my_ts); - ts_list_destroy(other_ts); + my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy)); + other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy)); this->child_sas->insert_last(this->child_sas, child_sa); SIG(SIG_CHILD_ROUTE, "CHILD_SA routed: %R...%R", my_ts, other_ts); - return status; } @@ -1220,12 +1205,12 @@ static status_t unroute(private_ike_sa_t *this, policy_t *policy) iterator->remove(iterator); SIG(SIG_CHILD_UNROUTE, "CHILD_SA unrouted"); child_sa->destroy(child_sa); - ts_list_destroy(my_ts_conf); - ts_list_destroy(other_ts_conf); + my_ts_conf->destroy_offset(my_ts_conf, offsetof(traffic_selector_t, destroy)); + other_ts_conf->destroy_offset(other_ts_conf, offsetof(traffic_selector_t, destroy)); break; } - ts_list_destroy(my_ts_conf); - ts_list_destroy(other_ts_conf); + my_ts_conf->destroy_offset(my_ts_conf, offsetof(traffic_selector_t, destroy)); + other_ts_conf->destroy_offset(other_ts_conf, offsetof(traffic_selector_t, destroy)); } } iterator->destroy(iterator); @@ -1907,20 +1892,8 @@ static void __attribute__ ((constructor))print_register() */ static void destroy(private_ike_sa_t *this) { - child_sa_t *child_sa; - transaction_t *transaction; - - while (this->child_sas->remove_last(this->child_sas, (void**)&child_sa) == SUCCESS) - { - child_sa->destroy(child_sa); - } - this->child_sas->destroy(this->child_sas); - - while (this->transaction_queue->remove_last(this->transaction_queue, (void**)&transaction) == SUCCESS) - { - transaction->destroy(transaction); - } - this->transaction_queue->destroy(this->transaction_queue); + this->child_sas->destroy_offset(this->child_sas, offsetof(child_sa_t, destroy)); + this->transaction_queue->destroy_offset(this->transaction_queue, offsetof(transaction_t, destroy)); DESTROY_IF(this->transaction_in); DESTROY_IF(this->transaction_in_next); diff --git a/src/charon/sa/ike_sa_manager.c b/src/charon/sa/ike_sa_manager.c index c9bdac460..0945b6b72 100644 --- a/src/charon/sa/ike_sa_manager.c +++ b/src/charon/sa/ike_sa_manager.c @@ -851,11 +851,7 @@ static void destroy(private_ike_sa_manager_t *this) DBG2(SIG_DBG_MGR, "destroy all entries"); /* Step 4: destroy all entries */ - while (list->remove_last(list, (void**)&entry) == SUCCESS) - { - entry_destroy(entry); - } - list->destroy(list); + list->destroy_function(list, (void*)entry_destroy); pthread_mutex_unlock(&(this->mutex)); this->randomizer->destroy(this->randomizer); diff --git a/src/charon/sa/transactions/create_child_sa.c b/src/charon/sa/transactions/create_child_sa.c index 8e9648ca7..69d0d92ae 100644 --- a/src/charon/sa/transactions/create_child_sa.c +++ b/src/charon/sa/transactions/create_child_sa.c @@ -180,36 +180,6 @@ static void cancel(private_create_child_sa_t *this) } /** - * destroy a list of traffic selectors - */ -static void destroy_ts_list(linked_list_t *list) -{ - if (list) - { - traffic_selector_t *ts; - while (list->remove_last(list, (void**)&ts) == SUCCESS) - { - ts->destroy(ts); - } - list->destroy(list); - } -} - -/** - * destroy a list of proposals - */ -static void destroy_proposal_list(linked_list_t *list) -{ - proposal_t *proposal; - - while (list->remove_last(list, (void**)&proposal) == SUCCESS) - { - proposal->destroy(proposal); - } - list->destroy(list); -} - -/** * Implementation of transaction_t.get_request. */ static status_t get_request(private_create_child_sa_t *this, message_t **result) @@ -305,7 +275,7 @@ static status_t get_request(private_create_child_sa_t *this, message_t **result) return FAILED; } sa_payload = sa_payload_create_from_proposal_list(proposals); - destroy_proposal_list(proposals); + proposals->destroy_offset(proposals, offsetof(proposal_t, destroy)); request->add_payload(request, (payload_t*)sa_payload); } @@ -328,7 +298,7 @@ static status_t get_request(private_create_child_sa_t *this, message_t **result) ts_list = this->policy->get_my_traffic_selectors(this->policy, me); ts_payload = ts_payload_create_from_traffic_selectors(TRUE, ts_list); - destroy_ts_list(ts_list); + ts_list->destroy_offset(ts_list, offsetof(traffic_selector_t, destroy)); request->add_payload(request, (payload_t*)ts_payload); } @@ -338,7 +308,7 @@ static status_t get_request(private_create_child_sa_t *this, message_t **result) ts_list = this->policy->get_other_traffic_selectors(this->policy, other); ts_payload = ts_payload_create_from_traffic_selectors(FALSE, ts_list); - destroy_ts_list(ts_list); + ts_list->destroy_offset(ts_list, offsetof(traffic_selector_t, destroy)); request->add_payload(request, (payload_t*)ts_payload); } @@ -644,8 +614,8 @@ static status_t get_response(private_create_child_sa_t *this, message_t *request this->tsr = this->policy->select_my_traffic_selectors(this->policy, my_ts, me); this->tsi = this->policy->select_other_traffic_selectors(this->policy, other_ts, other); } - destroy_ts_list(my_ts); - destroy_ts_list(other_ts); + my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy)); + other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy)); if (this->policy == NULL) { @@ -667,7 +637,7 @@ static status_t get_response(private_create_child_sa_t *this, message_t *request proposal_list = sa_request->get_proposals(sa_request); DBG2(SIG_DBG_IKE, "selecting proposals:"); this->proposal = this->policy->select_proposal(this->policy, proposal_list); - destroy_proposal_list(proposal_list); + proposal_list->destroy_offset(proposal_list, offsetof(proposal_t, destroy)); /* do we have a proposal? */ if (this->proposal == NULL) @@ -822,13 +792,13 @@ static status_t conclude(private_create_child_sa_t *this, message_t *response, { /* process traffic selectors for us */ linked_list_t *ts_received = tsi_payload->get_traffic_selectors(tsi_payload); this->tsi = this->policy->select_my_traffic_selectors(this->policy, ts_received, me); - destroy_ts_list(ts_received); + ts_received->destroy_offset(ts_received, offsetof(traffic_selector_t, destroy)); } { /* process traffic selectors for other */ linked_list_t *ts_received = tsr_payload->get_traffic_selectors(tsr_payload); this->tsr = this->policy->select_other_traffic_selectors(this->policy, ts_received, other); - destroy_ts_list(ts_received); + ts_received->destroy_offset(ts_received, offsetof(traffic_selector_t, destroy)); } { /* process sa payload */ @@ -837,7 +807,7 @@ static status_t conclude(private_create_child_sa_t *this, message_t *response, proposal_list = sa_payload->get_proposals(sa_payload); /* we have to re-check here if other's selection is valid */ this->proposal = this->policy->select_proposal(this->policy, proposal_list); - destroy_proposal_list(proposal_list); + proposal_list->destroy_offset(proposal_list, offsetof(proposal_t, destroy)); /* everything fine to create CHILD? */ if (this->proposal == NULL || @@ -917,8 +887,8 @@ static void destroy(private_create_child_sa_t *this) DESTROY_IF(this->proposal); DESTROY_IF(this->child_sa); DESTROY_IF(this->policy); - destroy_ts_list(this->tsi); - destroy_ts_list(this->tsr); + this->tsi->destroy_offset(this->tsi, offsetof(traffic_selector_t, destroy)); + this->tsr->destroy_offset(this->tsr, offsetof(traffic_selector_t, destroy)); chunk_free(&this->nonce_i); chunk_free(&this->nonce_r); chunk_free(&this->nonce_s); diff --git a/src/charon/sa/transactions/ike_auth.c b/src/charon/sa/transactions/ike_auth.c index bacc6d0b2..44cd8079d 100644 --- a/src/charon/sa/transactions/ike_auth.c +++ b/src/charon/sa/transactions/ike_auth.c @@ -183,37 +183,6 @@ static void set_init_messages(private_ike_auth_t *this, chunk_t init_request, ch } /** - * destroy a list of traffic selectors - */ -static void destroy_ts_list(linked_list_t *list) -{ - if (list) - { - traffic_selector_t *ts; - - while (list->remove_last(list, (void**)&ts) == SUCCESS) - { - ts->destroy(ts); - } - list->destroy(list); - } -} - -/** - * destroy a list of proposals - */ -static void destroy_proposal_list(linked_list_t *list) -{ - proposal_t *proposal; - - while (list->remove_last(list, (void**)&proposal) == SUCCESS) - { - proposal->destroy(proposal); - } - list->destroy(list); -} - -/** * Implementation of transaction_t.get_request. */ static status_t get_request(private_ike_auth_t *this, message_t **result) @@ -330,7 +299,7 @@ static status_t get_request(private_ike_auth_t *this, message_t **result) return DESTROY_ME; } sa_payload = sa_payload_create_from_proposal_list(proposal_list); - destroy_proposal_list(proposal_list); + proposal_list->destroy_offset(proposal_list, offsetof(proposal_t, destroy)); request->add_payload(request, (payload_t*)sa_payload); } @@ -340,7 +309,7 @@ static status_t get_request(private_ike_auth_t *this, message_t **result) ts_list = this->policy->get_my_traffic_selectors(this->policy, me); ts_payload = ts_payload_create_from_traffic_selectors(TRUE, ts_list); - destroy_ts_list(ts_list); + ts_list->destroy_offset(ts_list, offsetof(traffic_selector_t, destroy)); request->add_payload(request, (payload_t*)ts_payload); } @@ -351,7 +320,7 @@ static status_t get_request(private_ike_auth_t *this, message_t **result) ts_list = this->policy->get_other_traffic_selectors(this->policy, other); ts_payload = ts_payload_create_from_traffic_selectors(FALSE, ts_list); - destroy_ts_list(ts_list); + ts_list->destroy_offset(ts_list, offsetof(traffic_selector_t, destroy)); request->add_payload(request, (payload_t*)ts_payload); } @@ -663,8 +632,8 @@ static status_t get_response(private_ike_auth_t *this, message_t *request, this->tsr = this->policy->select_my_traffic_selectors(this->policy, my_ts, me); this->tsi = this->policy->select_other_traffic_selectors(this->policy, other_ts, other); } - destroy_ts_list(my_ts); - destroy_ts_list(other_ts); + my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy)); + other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy)); /* TODO: We should check somehow if we have a policy, but with other * traffic selectors. Then we would create a IKE_SA without a CHILD_SA. */ @@ -762,7 +731,7 @@ static status_t get_response(private_ike_auth_t *this, message_t *request, proposal_list = sa_request->get_proposals(sa_request); DBG2(SIG_DBG_IKE, "selecting proposals:"); this->proposal = this->policy->select_proposal(this->policy, proposal_list); - destroy_proposal_list(proposal_list); + proposal_list->destroy_offset(proposal_list, offsetof(proposal_t, destroy)); /* do we have a proposal? */ if (this->proposal == NULL) @@ -947,13 +916,13 @@ static status_t conclude(private_ike_auth_t *this, message_t *response, { /* process traffic selectors for us */ linked_list_t *ts_received = tsi_payload->get_traffic_selectors(tsi_payload); this->tsi = this->policy->select_my_traffic_selectors(this->policy, ts_received, me); - destroy_ts_list(ts_received); + ts_received->destroy_offset(ts_received, offsetof(traffic_selector_t, destroy)); } { /* process traffic selectors for other */ linked_list_t *ts_received = tsr_payload->get_traffic_selectors(tsr_payload); this->tsr = this->policy->select_other_traffic_selectors(this->policy, ts_received, other); - destroy_ts_list(ts_received); + ts_received->destroy_offset(ts_received, offsetof(traffic_selector_t, destroy)); } { /* process sa payload */ @@ -962,7 +931,7 @@ static status_t conclude(private_ike_auth_t *this, message_t *response, proposal_list = sa_payload->get_proposals(sa_payload); /* we have to re-check here if other's selection is valid */ this->proposal = this->policy->select_proposal(this->policy, proposal_list); - destroy_proposal_list(proposal_list); + proposal_list->destroy_offset(proposal_list, offsetof(proposal_t, destroy)); /* everything fine to create CHILD? */ if (this->proposal == NULL || @@ -995,8 +964,8 @@ static void destroy(private_ike_auth_t *this) DESTROY_IF(this->child_sa); DESTROY_IF(this->policy); DESTROY_IF(this->connection); - destroy_ts_list(this->tsi); - destroy_ts_list(this->tsr); + this->tsi->destroy_offset(this->tsi, offsetof(traffic_selector_t, destroy)); + this->tsr->destroy_offset(this->tsr, offsetof(traffic_selector_t, destroy)); chunk_free(&this->nonce_i); chunk_free(&this->nonce_r); chunk_free(&this->init_request); diff --git a/src/charon/sa/transactions/ike_sa_init.c b/src/charon/sa/transactions/ike_sa_init.c index 8232500f4..ab1af0c63 100644 --- a/src/charon/sa/transactions/ike_sa_init.c +++ b/src/charon/sa/transactions/ike_sa_init.c @@ -260,20 +260,6 @@ static notify_payload_t *build_natd_payload(private_ike_sa_init_t *this, } /** - * destroy a list of proposals - */ -static void destroy_proposal_list(linked_list_t *list) -{ - proposal_t *proposal; - - while (list->remove_last(list, (void**)&proposal) == SUCCESS) - { - proposal->destroy(proposal); - } - list->destroy(list); -} - -/** * Implementation of transaction_t.get_request. */ static status_t get_request(private_ike_sa_init_t *this, message_t **result) @@ -328,8 +314,8 @@ static status_t get_request(private_ike_sa_init_t *this, message_t **result) this->diffie_hellman = diffie_hellman_create(dh_group); if (this->diffie_hellman == NULL) { - DBG1(SIG_DBG_IKE, "DH group %N not supported, aborting", - diffie_hellman_group_names, dh_group); + SIG(SIG_IKE_FAILED, "DH group %N not supported, aborting", + diffie_hellman_group_names, dh_group); return DESTROY_ME; } } @@ -340,7 +326,7 @@ static status_t get_request(private_ike_sa_init_t *this, message_t **result) proposal_list = this->connection->get_proposals(this->connection); sa_payload = sa_payload_create_from_proposal_list(proposal_list); - destroy_proposal_list(proposal_list); + proposal_list->destroy_offset(proposal_list, offsetof(proposal_t, destroy)); request->add_payload(request, (payload_t*)sa_payload); } @@ -359,6 +345,7 @@ static status_t get_request(private_ike_sa_init_t *this, message_t **result) if (this->randomizer->allocate_pseudo_random_bytes(this->randomizer, NONCE_SIZE, &this->nonce_i) != SUCCESS) { + SIG(SIG_IKE_FAILED, "could not generate nonce, aborting"); return DESTROY_ME; } nonce_payload = nonce_payload_create(); @@ -407,12 +394,14 @@ static status_t process_notifys(private_ike_sa_init_t *this, notify_payload_t *n { case NO_PROPOSAL_CHOSEN: { - DBG1(SIG_DBG_IKE, "received a NO_PROPOSAL_CHOSEN notify, deleting IKE_SA"); + SIG(SIG_IKE_FAILED, + "received a NO_PROPOSAL_CHOSEN notify, deleting IKE_SA"); return DESTROY_ME; } case INVALID_MAJOR_VERSION: { - DBG1(SIG_DBG_IKE, "received a INVALID_MAJOR_VERSION notify, deleting IKE_SA"); + SIG(SIG_IKE_FAILED, + "received a INVALID_MAJOR_VERSION notify, deleting IKE_SA"); return DESTROY_ME; } case INVALID_KE_PAYLOAD: @@ -483,14 +472,14 @@ static status_t process_notifys(private_ike_sa_init_t *this, notify_payload_t *n { if (notify_type < 16383) { - DBG1(SIG_DBG_IKE, "received %N notify error, deleting IKE_SA", - notify_type_names, notify_type); + SIG(SIG_IKE_FAILED, "received %N notify error, deleting IKE_SA", + notify_type_names, notify_type); return DESTROY_ME; } else { - DBG1(SIG_DBG_IKE, "received %N notify, ignored", - notify_type_names, notify_type); + SIG(SIG_IKE_FAILED, "received %N notify, ignored", + notify_type_names, notify_type); return SUCCESS; } } @@ -540,7 +529,7 @@ static status_t get_response(private_ike_sa_init_t *this, /* check message type */ if (request->get_exchange_type(request) != IKE_SA_INIT) { - DBG1(SIG_DBG_IKE, "IKE_SA_INIT request of invalid type, deleting IKE_SA"); + SIG(SIG_IKE_FAILED, "IKE_SA_INIT request of invalid type, deleting IKE_SA"); return DESTROY_ME; } @@ -553,8 +542,8 @@ static status_t get_response(private_ike_sa_init_t *this, notify->set_notify_type(notify, NO_PROPOSAL_CHOSEN); response->add_payload(response, (payload_t*)notify); - DBG1(SIG_DBG_IKE, "no connection for hosts %H...%H found, deleting IKE_SA", - me, other); + SIG(SIG_IKE_FAILED, "no connection for hosts %H...%H found, " + "deleting IKE_SA", me, other); return DESTROY_ME; } @@ -620,7 +609,7 @@ static status_t get_response(private_ike_sa_init_t *this, notify_payload_t *notify = notify_payload_create(); notify->set_notify_type(notify, INVALID_SYNTAX); response->add_payload(response, (payload_t*)notify); - DBG1(SIG_DBG_IKE, "request message incomplete, deleting IKE_SA"); + SIG(SIG_IKE_FAILED, "request message incomplete, deleting IKE_SA"); return DESTROY_ME; } @@ -636,13 +625,14 @@ static status_t get_response(private_ike_sa_init_t *this, proposal_list = sa_request->get_proposals(sa_request); this->proposal = this->connection->select_proposal(this->connection, proposal_list); - destroy_proposal_list(proposal_list); + proposal_list->destroy_offset(proposal_list, offsetof(proposal_t, destroy)); if (this->proposal == NULL) { notify_payload_t *notify = notify_payload_create(); notify->set_notify_type(notify, NO_PROPOSAL_CHOSEN); response->add_payload(response, (payload_t*)notify); - DBG1(SIG_DBG_IKE, "request did not contain any acceptable proposals, deleting IKE_SA"); + SIG(SIG_IKE_FAILED, "request did not contain any acceptable " + "proposals, deleting IKE_SA"); return DESTROY_ME; } sa_response = sa_payload_create_from_proposal(this->proposal); @@ -719,7 +709,7 @@ static status_t get_response(private_ike_sa_init_t *this, notify_payload_t *notify = notify_payload_create(); notify->set_notify_type(notify, NO_PROPOSAL_CHOSEN); response->add_payload(response, (payload_t*)notify); - DBG1(SIG_DBG_IKE, "could not get random bytes for nonce, deleting IKE_SA"); + SIG(SIG_IKE_FAILED, "could not create nonce, deleting IKE_SA"); return DESTROY_ME; } nonce_response = nonce_payload_create(); @@ -741,7 +731,8 @@ static status_t get_response(private_ike_sa_init_t *this, notify = notify_payload_create(); notify->set_notify_type(notify, INVALID_SYNTAX); response->add_payload(response, (payload_t*)notify); - DBG1(SIG_DBG_IKE, "request contained wrong number of NAT-D payloads, deleting IKE_SA"); + SIG(SIG_IKE_FAILED, "request contained invalid number of NAT-D" + "payloads, deleting IKE_SA"); return DESTROY_ME; } if (this->natd_dst_seen && !this->natd_dst_matched) @@ -774,7 +765,7 @@ static status_t get_response(private_ike_sa_init_t *this, notify_payload_t *notify = notify_payload_create(); notify->set_notify_type(notify, NO_PROPOSAL_CHOSEN); response->add_payload(response, (payload_t*)notify); - DBG1(SIG_DBG_IKE, "transform objects could not be created from selected proposal, deleting IKE_SA"); + SIG(SIG_IKE_FAILED, "error creating transform from proposal, deleting IKE_SA"); return DESTROY_ME; } @@ -792,7 +783,7 @@ static status_t get_response(private_ike_sa_init_t *this, * as we don't use a crypter/signer in ike_sa_init... */ if (response->generate(response, NULL, NULL, &response_packet) != SUCCESS) { - DBG1(SIG_DBG_IKE, "error in response generation, deleting IKE_SA"); + SIG(SIG_IKE_FAILED, "error in response generation, deleting IKE_SA"); return DESTROY_ME; } response_packet->destroy(response_packet); @@ -845,7 +836,7 @@ static status_t conclude(private_ike_sa_init_t *this, message_t *response, /* check message type */ if (response->get_exchange_type(response) != IKE_SA_INIT) { - DBG1(SIG_DBG_IKE, "IKE_SA_INIT response of invalid type, deleting IKE_SA"); + SIG(SIG_IKE_FAILED, "IKE_SA_INIT response of invalid type, deleting IKE_SA"); return DESTROY_ME; } @@ -859,7 +850,7 @@ static status_t conclude(private_ike_sa_init_t *this, message_t *response, responder_spi = response->get_responder_spi(response); if (responder_spi == 0) { - DBG1(SIG_DBG_IKE, "response contained a SPI of zero, deleting IKE_SA"); + SIG(SIG_IKE_FAILED, "response contained a SPI of zero, deleting IKE_SA"); return DESTROY_ME; } @@ -871,7 +862,7 @@ static status_t conclude(private_ike_sa_init_t *this, message_t *response, /* Iterate over all payloads to collect them */ payloads = response->get_payload_iterator(response); while (payloads->has_next(payloads)) - { + { payload_t *payload; payloads->current(payloads, (void**)&payload); @@ -920,7 +911,7 @@ static status_t conclude(private_ike_sa_init_t *this, message_t *response, if (!(nonce_payload && sa_payload && ke_payload)) { - DBG1(SIG_DBG_IKE, "response message incomplete, deleting IKE_SA"); + SIG(SIG_IKE_FAILED, "response message incomplete, deleting IKE_SA"); return DESTROY_ME; } @@ -930,29 +921,24 @@ static status_t conclude(private_ike_sa_init_t *this, message_t *response, * - check if peer selected a proposal * - verify it's selection againts our set */ - proposal_t *proposal; linked_list_t *proposal_list; /* get the list of selected proposals, the peer has to select only one proposal */ proposal_list = sa_payload->get_proposals (sa_payload); if (proposal_list->get_count(proposal_list) != 1) { - DBG1(SIG_DBG_IKE, "response did not contain a single proposal, deleting IKE_SA"); - while (proposal_list->remove_last(proposal_list, (void**)&proposal) == SUCCESS) - { - proposal->destroy(proposal); - } - proposal_list->destroy(proposal_list); + SIG(SIG_IKE_FAILED, "response did not contain a single proposal, deleting IKE_SA"); + proposal_list->destroy_offset(proposal_list, offsetof(proposal_t, destroy)); return DESTROY_ME; } /* we have to re-check if the others selection is valid */ this->proposal = this->connection->select_proposal(this->connection, proposal_list); - destroy_proposal_list(proposal_list); + proposal_list->destroy_offset(proposal_list, offsetof(proposal_t, destroy)); if (this->proposal == NULL) { - DBG1(SIG_DBG_IKE, "peer selected a proposal we did not offer, deleting IKE_SA"); + SIG(SIG_IKE_FAILED, "peer selected a proposal we did not offer, deleting IKE_SA"); return DESTROY_ME; } } @@ -980,7 +966,7 @@ static status_t conclude(private_ike_sa_init_t *this, message_t *response, if ((!this->natd_dst_seen && this->natd_src_seen) || (this->natd_dst_seen && !this->natd_src_seen)) { - DBG1(SIG_DBG_IKE, "request contained wrong number of NAT-D payloads, deleting IKE_SA"); + SIG(SIG_IKE_FAILED, "request contained invalid number of NAT-D payloads, deleting IKE_SA"); return DESTROY_ME; } if (this->natd_src_seen && !this->natd_src_matched) @@ -1012,7 +998,7 @@ static status_t conclude(private_ike_sa_init_t *this, message_t *response, this->nonce_i, this->nonce_r, TRUE, NULL, NULL) != SUCCESS) { - DBG1(SIG_DBG_IKE, "transform objects could not be created from selected proposal, deleting IKE_SA"); + SIG(SIG_IKE_FAILED, "error creating transforms from proposal, deleting IKE_SA"); return DESTROY_ME; } diff --git a/src/charon/sa/transactions/rekey_ike_sa.c b/src/charon/sa/transactions/rekey_ike_sa.c index 4b9dcc176..6bc2f5990 100644 --- a/src/charon/sa/transactions/rekey_ike_sa.c +++ b/src/charon/sa/transactions/rekey_ike_sa.c @@ -149,20 +149,6 @@ static void cancel(private_rekey_ike_sa_t *this) } /** - * destroy a list of proposals - */ -static void destroy_proposal_list(linked_list_t *list) -{ - proposal_t *proposal; - - while (list->remove_last(list, (void**)&proposal) == SUCCESS) - { - proposal->destroy(proposal); - } - list->destroy(list); -} - -/** * Implementation of transaction_t.get_request. */ static status_t get_request(private_rekey_ike_sa_t *this, message_t **result) @@ -240,7 +226,7 @@ static status_t get_request(private_rekey_ike_sa_t *this, message_t **result) iterator->destroy(iterator); sa_payload = sa_payload_create_from_proposal_list(proposals); - destroy_proposal_list(proposals); + proposals->destroy_offset(proposals, offsetof(proposal_t, destroy)); request->add_payload(request, (payload_t*)sa_payload); } @@ -570,7 +556,7 @@ static status_t get_response(private_rekey_ike_sa_t *this, message_t *request, proposal_list = sa_request->get_proposals(sa_request); DBG2(SIG_DBG_IKE, "selecting proposals:"); this->proposal = this->connection->select_proposal(this->connection, proposal_list); - destroy_proposal_list(proposal_list); + proposal_list->destroy_offset(proposal_list, offsetof(proposal_t, destroy)); /* do we have a proposal? */ if (this->proposal == NULL) @@ -754,7 +740,7 @@ static status_t conclude(private_rekey_ike_sa_t *this, message_t *response, proposal_list = sa_payload->get_proposals(sa_payload); /* we have to re-check here if other's selection is valid */ this->proposal = this->connection->select_proposal(this->connection, proposal_list); - destroy_proposal_list(proposal_list); + proposal_list->destroy_offset(proposal_list, offsetof(proposal_t, destroy)); if (this->proposal == NULL) { diff --git a/src/libstrongswan/crypto/crl.c b/src/libstrongswan/crypto/crl.c index b2c24b80b..f1c21c74b 100755 --- a/src/libstrongswan/crypto/crl.c +++ b/src/libstrongswan/crypto/crl.c @@ -411,24 +411,10 @@ static void get_status(const private_crl_t *this, certinfo_t *certinfo) */ static void destroy(private_crl_t *this) { - revokedCert_t *revokedCert; - identification_t *id; - - while (this->revokedCertificates->remove_last(this->revokedCertificates, (void**)&revokedCert) == SUCCESS) - { - free(revokedCert); - } - this->revokedCertificates->destroy(this->revokedCertificates); - - while (this->crlDistributionPoints->remove_last(this->crlDistributionPoints, (void**)&id) == SUCCESS) - { - id->destroy(id); - } - this->crlDistributionPoints->destroy(this->crlDistributionPoints); - - if (this->issuer) - this->issuer->destroy(this->issuer); - + this->revokedCertificates->destroy_function(this->revokedCertificates, free); + this->crlDistributionPoints->destroy_offset(this->crlDistributionPoints, + offsetof(identification_t, destroy)); + DESTROY_IF(this->issuer); free(this->certificateList.ptr); free(this); } diff --git a/src/libstrongswan/crypto/x509.c b/src/libstrongswan/crypto/x509.c index 4c5e014c8..9f59a0ee4 100755 --- a/src/libstrongswan/crypto/x509.c +++ b/src/libstrongswan/crypto/x509.c @@ -1188,19 +1188,10 @@ static void __attribute__ ((constructor))print_register() */ static void destroy(private_x509_t *this) { - identification_t *id; - while (this->subjectAltNames->remove_last(this->subjectAltNames, (void**)&id) == SUCCESS) - { - id->destroy(id); - } - this->subjectAltNames->destroy(this->subjectAltNames); - - while (this->crlDistributionPoints->remove_last(this->crlDistributionPoints, (void**)&id) == SUCCESS) - { - id->destroy(id); - } - this->crlDistributionPoints->destroy(this->crlDistributionPoints); - + this->subjectAltNames->destroy_offset(this->subjectAltNames, + offsetof(identification_t, destroy)); + this->crlDistributionPoints->destroy_offset(this->crlDistributionPoints, + offsetof(identification_t, destroy)); DESTROY_IF(this->issuer); DESTROY_IF(this->subject); DESTROY_IF(this->public_key); diff --git a/src/libstrongswan/utils/linked_list.c b/src/libstrongswan/utils/linked_list.c index 83371018f..5510fe5d5 100644 --- a/src/libstrongswan/utils/linked_list.c +++ b/src/libstrongswan/utils/linked_list.c @@ -25,16 +25,12 @@ #include "linked_list.h" - - -typedef struct linked_list_element_t linked_list_element_t; +typedef struct element_t element_t; /** - * @brief Element in a linked list. - * * This element holds a pointer to the value it represents. */ -struct linked_list_element_t { +struct element_t { /** * Value of a list item. @@ -46,22 +42,22 @@ struct linked_list_element_t { * * NULL if first element in list. */ - linked_list_element_t *previous; + element_t *previous; /** * Next list element. * * NULL if last element in list. */ - linked_list_element_t *next; + element_t *next; }; /** * Creates an empty linked list object. */ -linked_list_element_t *linked_list_element_create(void *value) +element_t *element_create(void *value) { - linked_list_element_t *this = malloc_thing(linked_list_element_t); + element_t *this = malloc_thing(element_t); this->previous = NULL; this->next = NULL; @@ -92,13 +88,13 @@ struct private_linked_list_t { * First element in list. * NULL if no elements in list. */ - linked_list_element_t *first; + element_t *first; /** * Last element in list. * NULL if no elements in list. */ - linked_list_element_t *last; + element_t *last; }; @@ -121,7 +117,7 @@ struct private_iterator_t { /** * Current element of the iterator. */ - linked_list_element_t *current; + element_t *current; /** * Direction of iterator. @@ -233,9 +229,9 @@ static void iterator_reset(private_iterator_t *this) /** * Implementation of iterator_t.remove. */ -static status_t remove(private_iterator_t *this) +static status_t remove_(private_iterator_t *this) { - linked_list_element_t *new_current; + element_t *new_current; if (this->current == NULL) { @@ -298,7 +294,7 @@ static void insert_before(private_iterator_t * iterator, void *item) iterator->list->public.insert_first(&(iterator->list->public), item); } - linked_list_element_t *element = linked_list_element_create(item); + element_t *element = element_create(item); if (iterator->current->previous == NULL) { iterator->current->previous = element; @@ -344,7 +340,7 @@ static void insert_after(private_iterator_t * iterator, void *item) return; } - linked_list_element_t *element = linked_list_element_create(item); + element_t *element = element_create(item); if (iterator->current->next == NULL) { iterator->current->next = element; @@ -386,9 +382,9 @@ static int get_count(private_linked_list_t *this) */ static void insert_first(private_linked_list_t *this, void *item) { - linked_list_element_t *element; + element_t *element; - element = linked_list_element_create(item); + element = element_create(item); if (this->count == 0) { /* first entry in list */ @@ -399,7 +395,7 @@ static void insert_first(private_linked_list_t *this, void *item) } else { - linked_list_element_t *old_first_element = this->first; + element_t *old_first_element = this->first; element->next = old_first_element; element->previous = NULL; old_first_element->previous = element; @@ -418,7 +414,7 @@ static status_t remove_first(private_linked_list_t *this, void **item) return NOT_FOUND; } - linked_list_element_t *element = this->first; + element_t *element = this->first; if (element->next != NULL) { element->next->previous = NULL; @@ -452,7 +448,7 @@ static status_t get_first(private_linked_list_t *this, void **item) */ static void insert_last(private_linked_list_t *this, void *item) { - linked_list_element_t *element = linked_list_element_create(item); + element_t *element = element_create(item); if (this->count == 0) { @@ -464,7 +460,7 @@ static void insert_last(private_linked_list_t *this, void *item) } else { - linked_list_element_t *old_last_element = this->last; + element_t *old_last_element = this->last; element->previous = old_last_element; element->next = NULL; old_last_element->next = element; @@ -483,7 +479,7 @@ static status_t remove_last(private_linked_list_t *this, void **item) return NOT_FOUND; } - linked_list_element_t *element = this->last; + element_t *element = this->last; if (element->previous != NULL) { @@ -506,7 +502,7 @@ static status_t remove_last(private_linked_list_t *this, void **item) */ static status_t insert_at_position (private_linked_list_t *this,size_t position, void *item) { - linked_list_element_t *current_element; + element_t *current_element; int i; if (this->count <= position) @@ -527,7 +523,7 @@ static status_t insert_at_position (private_linked_list_t *this,size_t position, return SUCCESS; } - linked_list_element_t *element = linked_list_element_create(item); + element_t *element = element_create(item); if (current_element->previous == NULL) { current_element->previous = element; @@ -615,9 +611,74 @@ static status_t get_last(private_linked_list_t *this, void **item) } /** + * Implementation of linked_list_t.invoke. + */ +static void invoke(private_linked_list_t *this, size_t offset) +{ + element_t *current = this->first; + + while (current) + { + void (**method)(void*) = current->value + offset; + (*method)(current->value); + current = current->next; + } +} + +/** + * Implementation of linked_list_t.destroy. + */ +static void destroy(private_linked_list_t *this) +{ + void *value; + /* Remove all list items before destroying list */ + while (this->public.remove_first(&(this->public), &value) == SUCCESS) + { + /* values are not destroyed so memory leaks are possible + * if list is not empty when deleting */ + } + free(this); +} + +/** + * Implementation of linked_list_t.destroy_offset. + */ +static void destroy_offset(private_linked_list_t *this, size_t offset) +{ + element_t *current = this->first, *next; + + while (current) + { + void (**method)(void*) = current->value + offset; + (*method)(current->value); + next = current->next; + free(current); + current = next; + } + free(this); +} + +/** + * Implementation of linked_list_t.destroy_function. + */ +static void destroy_function(private_linked_list_t *this, void (*fn)(void*)) +{ + element_t *current = this->first, *next; + + while (current) + { + fn(current->value); + next = current->next; + free(current); + current = next; + } + free(this); +} + +/** * Implementation of linked_list_t.create_iterator. */ -static iterator_t *create_iterator (private_linked_list_t *linked_list, bool forward) +static iterator_t *create_iterator(private_linked_list_t *linked_list, bool forward) { private_iterator_t *this = malloc_thing(private_iterator_t); @@ -628,7 +689,7 @@ static iterator_t *create_iterator (private_linked_list_t *linked_list, bool for this->public.insert_before = (void (*) (iterator_t *this, void *item)) insert_before; this->public.insert_after = (void (*) (iterator_t *this, void *item)) insert_after; this->public.replace = (status_t (*) (iterator_t *, void **, void *)) replace; - this->public.remove = (status_t (*) (iterator_t *this)) remove; + this->public.remove = (status_t (*) (iterator_t *this)) remove_; this->public.reset = (void (*) (iterator_t *this)) iterator_reset; this->public.destroy = (void (*) (iterator_t *this)) iterator_destroy; @@ -654,21 +715,6 @@ static iterator_t *create_iterator_locked(private_linked_list_t *linked_list, return &this->public; } -/** - * Implementation of linked_list_t.destroy. - */ -static void linked_list_destroy(private_linked_list_t *this) -{ - void *value; - /* Remove all list items before destroying list */ - while (this->public.remove_first(&(this->public), &value) != NOT_FOUND) - { - /* values are not destroyed so memory leaks are possible - * if list is not empty when deleting */ - } - free(this); -} - /* * Described in header. */ @@ -688,7 +734,10 @@ linked_list_t *linked_list_create() this->public.insert_at_position = (status_t (*) (linked_list_t *,size_t, void *))insert_at_position; this->public.remove_at_position = (status_t (*) (linked_list_t *,size_t, void **))remove_at_position; this->public.get_at_position = (status_t (*) (linked_list_t *,size_t, void **))get_at_position; - this->public.destroy = (void (*) (linked_list_t *))linked_list_destroy; + this->public.invoke = (void (*)(linked_list_t*,size_t))invoke; + this->public.destroy = (void (*) (linked_list_t *))destroy; + this->public.destroy_offset = (void (*) (linked_list_t *,size_t))destroy_offset; + this->public.destroy_function = (void (*)(linked_list_t*,void(*)(void*)))destroy_function; this->count = 0; this->first = NULL; diff --git a/src/libstrongswan/utils/linked_list.h b/src/libstrongswan/utils/linked_list.h index 9c824177e..63c7eb6e3 100644 --- a/src/libstrongswan/utils/linked_list.h +++ b/src/libstrongswan/utils/linked_list.h @@ -47,21 +47,21 @@ struct linked_list_t { /** * @brief Gets the count of items in the list. * - * @param linked_list calling object - * @return number of items in list + * @param this calling object + * @return number of items in list */ - int (*get_count) (linked_list_t *linked_list); + int (*get_count) (linked_list_t *this); /** * @brief Creates a iterator for the given list. * * @warning Created iterator_t object has to get destroyed by the caller. * - * @param linked_list calling object - * @param forward iterator direction (TRUE: front to end) - * @return new iterator_t object + * @param this calling object + * @param forward iterator direction (TRUE: front to end) + * @return new iterator_t object */ - iterator_t *(*create_iterator) (linked_list_t *linked_list, bool forward); + iterator_t *(*create_iterator) (linked_list_t *this, bool forward); /** * @brief Creates a iterator, locking a mutex. @@ -69,120 +69,155 @@ struct linked_list_t { * The supplied mutex is acquired immediately, and released * when the iterator gets destroyed. * - * @param linked_list calling object - * @param mutex mutex to use for exclusive access - * @return new iterator_t object + * @param this calling object + * @param mutex mutex to use for exclusive access + * @return new iterator_t object */ - iterator_t *(*create_iterator_locked) (linked_list_t *linked_list, + iterator_t *(*create_iterator_locked) (linked_list_t *this, pthread_mutex_t *mutex); /** * @brief Inserts a new item at the beginning of the list. * - * @param linked_list calling object - * @param[in] item item value to insert in list + * @param this calling object + * @param[in] item item value to insert in list */ - void (*insert_first) (linked_list_t *linked_list, void *item); + void (*insert_first) (linked_list_t *this, void *item); /** * @brief Removes the first item in the list and returns its value. * - * @param linked_list calling object - * @param[out] item returned value of first item, or NULL + * @param this calling object + * @param[out] item returned value of first item, or NULL * @return - * - SUCCESS - * - NOT_FOUND, if list is empty + * - SUCCESS + * - NOT_FOUND, if list is empty */ - status_t (*remove_first) (linked_list_t *linked_list, void **item); + status_t (*remove_first) (linked_list_t *this, void **item); /** * @brief Returns the value of the first list item without removing it. * - * @param linked_list calling object - * @param[out] item item returned value of first item + * @param this calling object + * @param[out] item returned value of first item * @return - * - SUCCESS - * - NOT_FOUND, if list is empty + * - SUCCESS + * - NOT_FOUND, if list is empty */ - status_t (*get_first) (linked_list_t *linked_list, void **item); + status_t (*get_first) (linked_list_t *this, void **item); /** * @brief Inserts a new item at the end of the list. * - * @param linked_list calling object - * @param[in] item item value to insert into list + * @param this calling object + * @param[in] item value to insert into list */ - void (*insert_last) (linked_list_t *linked_list, void *item); + void (*insert_last) (linked_list_t *this, void *item); /** * @brief Inserts a new item at a given position in the list. * - * @param linked_list calling object - * @param position position starting at 0 to insert new entry - * @param[in] item item value to insert into list + * @param this calling object + * @param position position starting at 0 to insert new entry + * @param[in] item value to insert into list * @return - * - SUCCESS - * - INVALID_ARG if position not existing + * - SUCCESS + * - INVALID_ARG if position not existing */ - status_t (*insert_at_position) (linked_list_t *linked_list,size_t position, void *item); + status_t (*insert_at_position) (linked_list_t *this,size_t position, void *item); /** * @brief Removes an item from a given position in the list. * - * @param linked_list calling object - * @param position position starting at 0 to remove entry from - * @param[out] item removed item will be stored at this location + * @param this calling object + * @param position position starting at 0 to remove entry from + * @param[out] item removed item will be stored at this location * @return * - SUCCESS * - INVALID_ARG if position not existing */ - status_t (*remove_at_position) (linked_list_t *linked_list,size_t position, void **item); + status_t (*remove_at_position) (linked_list_t *this, size_t position, void **item); /** * @brief Get an item from a given position in the list. * - * @param linked_list calling object - * @param position position starting at 0 to get entry from - * @param[out] item item will be stored at this location + * @param this calling object + * @param position position starting at 0 to get entry from + * @param[out] item item will be stored at this location * @return * - SUCCESS * - INVALID_ARG if position not existing */ - status_t (*get_at_position) (linked_list_t *linked_list,size_t position, void **item); + status_t (*get_at_position) (linked_list_t *this, size_t position, void **item); /** * @brief Removes the last item in the list and returns its value. * - * @param linked_list calling object - * @param[out] item returned value of last item, or NULL + * @param this calling object + * @param[out] item returned value of last item, or NULL * @return * - SUCCESS * - NOT_FOUND if list is empty */ - status_t (*remove_last) (linked_list_t *linked_list, void **item); + status_t (*remove_last) (linked_list_t *this, void **item); /** * @brief Returns the value of the last list item without removing it. * - * @param linked_list calling object - * @param[out] item returned value of last item + * @param this calling object + * @param[out] item returned value of last item * @return - * - SUCCESS - * - NOT_FOUND if list is empty + * - SUCCESS + * - NOT_FOUND if list is empty + */ + status_t (*get_last) (linked_list_t *this, void **item); + + /** + * @brief Invoke a method on all of the contained objects. + * + * If a linked list contains objects with function pointers, + * invoke() can call a method on each of the objects. The + * method is specified by an offset of the function pointer, + * which can be evalutated at compile time using the offsetof + * macro, e.g.: list->invoke(list, offsetof(object_t, method)); + * + * @param this calling object + * @param offset offset of the method to invoke on objects */ - status_t (*get_last) (linked_list_t *linked_list, void **item); + void (*invoke) (linked_list_t *this, size_t offset); /** * @brief Destroys a linked_list object. * - * @warning All items are removed before deleting the list. The - * associated values are NOT destroyed. - * Destroying an list which is not empty may cause - * memory leaks! + * @param this calling object + */ + void (*destroy) (linked_list_t *this); + + /** + * @brief Destroys a list and its objects using the destructor. + * + * If a linked list and the contained objects should be destroyed, use + * destroy_offset. The supplied offset specifies the destructor to + * call on each object. The offset may be calculated using the offsetof + * macro, e.g.: list->destroy_offset(list, offsetof(object_t, destroy)); + * + * @param this calling object + * @param offset offset of the objects destructor + */ + void (*destroy_offset) (linked_list_t *this, size_t offset); + + /** + * @brief Destroys a list and its contents using a a cleanup function. * - * @param linked_list calling object + * If a linked list and its contents should get destroyed using a specific + * cleanup function, use destroy_function. This is useful when the + * list contains malloc()-ed blocks which should get freed, + * e.g.: list->destroy_function(list, free); + * + * @param this calling object + * @param function function to call on each object */ - void (*destroy) (linked_list_t *linked_list); + void (*destroy_function) (linked_list_t *this, void (*)(void*)); }; /** |