diff options
Diffstat (limited to 'src/libcharon/sa/mediation_manager.c')
-rw-r--r-- | src/libcharon/sa/mediation_manager.c | 131 |
1 files changed, 61 insertions, 70 deletions
diff --git a/src/libcharon/sa/mediation_manager.c b/src/libcharon/sa/mediation_manager.c index 2fbab7c7c..60eeb5d4b 100644 --- a/src/libcharon/sa/mediation_manager.c +++ b/src/libcharon/sa/mediation_manager.c @@ -53,13 +53,12 @@ static void peer_destroy(peer_t *this) */ static peer_t *peer_create(identification_t *id, ike_sa_id_t* ike_sa_id) { - peer_t *this = malloc_thing(peer_t); - - /* clone everything */ - this->id = id->clone(id); - this->ike_sa_id = ike_sa_id ? ike_sa_id->clone(ike_sa_id) : NULL; - this->requested_by = linked_list_create(); - + peer_t *this; + INIT(this, + .id = id->clone(id), + .ike_sa_id = ike_sa_id ? ike_sa_id->clone(ike_sa_id) : NULL, + .requested_by = linked_list_create(), + ); return this; } @@ -90,19 +89,19 @@ struct private_mediation_manager_t { */ static void register_peer(peer_t *peer, identification_t *peer_id) { - iterator_t *iterator; + enumerator_t *enumerator; identification_t *current; - iterator = peer->requested_by->create_iterator(peer->requested_by, TRUE); - while (iterator->iterate(iterator, (void**)¤t)) + enumerator = peer->requested_by->create_enumerator(peer->requested_by); + while (enumerator->enumerate(enumerator, (void**)¤t)) { if (peer_id->equals(peer_id, current)) { - iterator->destroy(iterator); + enumerator->destroy(enumerator); return; } } - iterator->destroy(iterator); + enumerator->destroy(enumerator); peer->requested_by->insert_last(peer->requested_by, peer_id->clone(peer_id)); @@ -114,12 +113,12 @@ static void register_peer(peer_t *peer, identification_t *peer_id) static status_t get_peer_by_id(private_mediation_manager_t *this, identification_t *id, peer_t **peer) { - iterator_t *iterator; + enumerator_t *enumerator; peer_t *current; status_t status = NOT_FOUND; - iterator = this->peers->create_iterator(this->peers, TRUE); - while (iterator->iterate(iterator, (void**)¤t)) + enumerator = this->peers->create_enumerator(this->peers); + while (enumerator->enumerate(enumerator, (void**)¤t)) { if (id->equals(id, current->id)) { @@ -131,7 +130,7 @@ static status_t get_peer_by_id(private_mediation_manager_t *this, break; } } - iterator->destroy(iterator); + enumerator->destroy(enumerator); return status; } @@ -144,52 +143,50 @@ static status_t get_peer_by_id(private_mediation_manager_t *this, static void unregister_peer(private_mediation_manager_t *this, identification_t *peer_id) { - iterator_t *iterator, *iterator_r; + enumerator_t *enumerator, *enumerator_r; peer_t *peer; identification_t *registered; - iterator = this->peers->create_iterator(this->peers, TRUE); - while (iterator->iterate(iterator, (void**)&peer)) + enumerator = this->peers->create_enumerator(this->peers); + while (enumerator->enumerate(enumerator, (void**)&peer)) { - iterator_r = peer->requested_by->create_iterator(peer->requested_by, - TRUE); - while (iterator_r->iterate(iterator_r, (void**)®istered)) + enumerator_r = peer->requested_by->create_enumerator(peer->requested_by); + while (enumerator_r->enumerate(enumerator_r, (void**)®istered)) { if (peer_id->equals(peer_id, registered)) { - iterator_r->remove(iterator_r); + peer->requested_by->remove_at(peer->requested_by, enumerator_r); registered->destroy(registered); break; } } - iterator_r->destroy(iterator_r); + enumerator_r->destroy(enumerator_r); - if (!peer->ike_sa_id && !peer->requested_by->get_count(peer->requested_by)) + if (!peer->ike_sa_id && + !peer->requested_by->get_count(peer->requested_by)) { - iterator->remove(iterator); + this->peers->remove_at(this->peers, enumerator); peer_destroy(peer); break; } } - iterator->destroy(iterator); + enumerator->destroy(enumerator); } -/** - * Implementation of mediation_manager_t.remove - */ -static void remove_sa(private_mediation_manager_t *this, ike_sa_id_t *ike_sa_id) +METHOD(mediation_manager_t, remove_sa, void, + private_mediation_manager_t *this, ike_sa_id_t *ike_sa_id) { - iterator_t *iterator; + enumerator_t *enumerator; peer_t *peer; this->mutex->lock(this->mutex); - iterator = this->peers->create_iterator(this->peers, TRUE); - while (iterator->iterate(iterator, (void**)&peer)) + enumerator = this->peers->create_enumerator(this->peers); + while (enumerator->enumerate(enumerator, (void**)&peer)) { if (ike_sa_id->equals(ike_sa_id, peer->ike_sa_id)) { - iterator->remove(iterator); + this->peers->remove_at(this->peers, enumerator); unregister_peer(this, peer->id); @@ -197,24 +194,23 @@ static void remove_sa(private_mediation_manager_t *this, ike_sa_id_t *ike_sa_id) break; } } - iterator->destroy(iterator); + enumerator->destroy(enumerator); this->mutex->unlock(this->mutex); } -/** - * Implementation of mediation_manager_t.update_sa_id - */ -static void update_sa_id(private_mediation_manager_t *this, identification_t *peer_id, ike_sa_id_t *ike_sa_id) +METHOD(mediation_manager_t, update_sa_id, void, + private_mediation_manager_t *this, identification_t *peer_id, + ike_sa_id_t *ike_sa_id) { - iterator_t *iterator; + enumerator_t *enumerator; peer_t *peer; bool found = FALSE; this->mutex->lock(this->mutex); - iterator = this->peers->create_iterator(this->peers, TRUE); - while (iterator->iterate(iterator, (void**)&peer)) + enumerator = this->peers->create_enumerator(this->peers); + while (enumerator->enumerate(enumerator, (void**)&peer)) { if (peer_id->equals(peer_id, peer->id)) { @@ -223,7 +219,7 @@ static void update_sa_id(private_mediation_manager_t *this, identification_t *pe break; } } - iterator->destroy(iterator); + enumerator->destroy(enumerator); if (!found) { @@ -248,11 +244,8 @@ static void update_sa_id(private_mediation_manager_t *this, identification_t *pe this->mutex->unlock(this->mutex); } -/** - * Implementation of mediation_manager_t.check. - */ -static ike_sa_id_t *check(private_mediation_manager_t *this, - identification_t *peer_id) +METHOD(mediation_manager_t, check, ike_sa_id_t*, + private_mediation_manager_t *this, identification_t *peer_id) { peer_t *peer; ike_sa_id_t *ike_sa_id; @@ -272,11 +265,9 @@ static ike_sa_id_t *check(private_mediation_manager_t *this, return ike_sa_id; } -/** - * Implementation of mediation_manager_t.check_and_register. - */ -static ike_sa_id_t *check_and_register(private_mediation_manager_t *this, - identification_t *peer_id, identification_t *requester) +METHOD(mediation_manager_t, check_and_register, ike_sa_id_t*, + private_mediation_manager_t *this, identification_t *peer_id, + identification_t *requester) { peer_t *peer; ike_sa_id_t *ike_sa_id; @@ -307,10 +298,8 @@ static ike_sa_id_t *check_and_register(private_mediation_manager_t *this, return ike_sa_id; } -/** - * Implementation of mediation_manager_t.destroy. - */ -static void destroy(private_mediation_manager_t *this) +METHOD(mediation_manager_t, destroy, void, + private_mediation_manager_t *this) { this->mutex->lock(this->mutex); @@ -326,16 +315,18 @@ static void destroy(private_mediation_manager_t *this) */ mediation_manager_t *mediation_manager_create() { - private_mediation_manager_t *this = malloc_thing(private_mediation_manager_t); - - this->public.destroy = (void(*)(mediation_manager_t*))destroy; - this->public.remove = (void(*)(mediation_manager_t*,ike_sa_id_t*))remove_sa; - this->public.update_sa_id = (void(*)(mediation_manager_t*,identification_t*,ike_sa_id_t*))update_sa_id; - this->public.check = (ike_sa_id_t*(*)(mediation_manager_t*,identification_t*))check; - this->public.check_and_register = (ike_sa_id_t*(*)(mediation_manager_t*,identification_t*,identification_t*))check_and_register; - - this->peers = linked_list_create(); - this->mutex = mutex_create(MUTEX_TYPE_DEFAULT); - - return (mediation_manager_t*)this; + private_mediation_manager_t *this; + + INIT(this, + .public = { + .destroy = _destroy, + .remove = _remove_sa, + .update_sa_id = _update_sa_id, + .check = _check, + .check_and_register = _check_and_register, + }, + .peers = linked_list_create(), + .mutex = mutex_create(MUTEX_TYPE_DEFAULT), + ); + return &this->public; } |