Accept NULL identities passed to peer config enumeration
[strongswan.git] / src / libcharon / config / backend_manager.c
index 90ef585..c84da25 100644 (file)
@@ -96,6 +96,11 @@ static ike_cfg_match_t get_ike_match(ike_cfg_t *cand, host_t *me, host_t *other)
                {
                        match += MATCH_ANY;
                }
                {
                        match += MATCH_ANY;
                }
+               else
+               {
+                       me_cand->destroy(me_cand);
+                       return MATCH_NONE;
+               }
                me_cand->destroy(me_cand);
        }
        else
                me_cand->destroy(me_cand);
        }
        else
@@ -119,6 +124,11 @@ static ike_cfg_match_t get_ike_match(ike_cfg_t *cand, host_t *me, host_t *other)
                {
                        match += MATCH_ANY;
                }
                {
                        match += MATCH_ANY;
                }
+               else
+               {
+                       other_cand->destroy(other_cand);
+                       return MATCH_NONE;
+               }
                other_cand->destroy(other_cand);
        }
        else
                other_cand->destroy(other_cand);
        }
        else
@@ -128,11 +138,8 @@ static ike_cfg_match_t get_ike_match(ike_cfg_t *cand, host_t *me, host_t *other)
        return match;
 }
 
        return match;
 }
 
-/**
- * implements backend_manager_t.get_ike_cfg.
- */
-static ike_cfg_t *get_ike_cfg(private_backend_manager_t *this,
-                                                         host_t *me, host_t *other)
+METHOD(backend_manager_t, get_ike_cfg, ike_cfg_t*,
+       private_backend_manager_t *this, host_t *me, host_t *other)
 {
        ike_cfg_t *current, *found = NULL;
        enumerator_t *enumerator;
 {
        ike_cfg_t *current, *found = NULL;
        enumerator_t *enumerator;
@@ -153,7 +160,7 @@ static ike_cfg_t *get_ike_cfg(private_backend_manager_t *this,
        while (enumerator->enumerate(enumerator, (void**)&current))
        {
                match = get_ike_match(current, me, other);
        while (enumerator->enumerate(enumerator, (void**)&current))
        {
                match = get_ike_match(current, me, other);
-
+               DBG3(DBG_CFG, "ike config match: %d (%H %H)", match, me, other);
                if (match)
                {
                        DBG2(DBG_CFG, "  candidate: %s...%s, prio %d",
                if (match)
                {
                        DBG2(DBG_CFG, "  candidate: %s...%s, prio %d",
@@ -188,9 +195,12 @@ static id_match_t get_peer_match(identification_t *id,
        auth_cfg_t *auth;
        identification_t *candidate;
        id_match_t match = ID_MATCH_NONE;
        auth_cfg_t *auth;
        identification_t *candidate;
        id_match_t match = ID_MATCH_NONE;
+       chunk_t data;
 
        if (!id)
        {
 
        if (!id)
        {
+               DBG3(DBG_CFG, "peer config match %s: %d (%N)",
+                        local ? "local" : "remote", ID_MATCH_ANY, id_type_names, ID_ANY);
                return ID_MATCH_ANY;
        }
 
                return ID_MATCH_ANY;
        }
 
@@ -214,6 +224,10 @@ static id_match_t get_peer_match(identification_t *id,
                }
        }
        enumerator->destroy(enumerator);
                }
        }
        enumerator->destroy(enumerator);
+
+       data = id->get_encoding(id);
+       DBG3(DBG_CFG, "peer config match %s: %d (%N -> %#B)",
+                match, id_type_names, id->get_type(id), &data);
        return match;
 }
 
        return match;
 }
 
@@ -308,12 +322,9 @@ static void insert_sorted(match_entry_t *entry, linked_list_t *list,
        }
 }
 
        }
 }
 
-/**
- * Implements backend_manager_t.create_peer_cfg_enumerator.
- */
-static enumerator_t *create_peer_cfg_enumerator(private_backend_manager_t *this,
-                                                       host_t *me, host_t *other, identification_t *my_id,
-                                                       identification_t *other_id)
+METHOD(backend_manager_t, create_peer_cfg_enumerator, enumerator_t*,
+       private_backend_manager_t *this, host_t *me, host_t *other,
+       identification_t *my_id, identification_t *other_id)
 {
        enumerator_t *enumerator;
        peer_data_t *data;
 {
        enumerator_t *enumerator;
        peer_data_t *data;
@@ -351,6 +362,7 @@ static enumerator_t *create_peer_cfg_enumerator(private_backend_manager_t *this,
                match_peer_me = get_peer_match(my_id, cfg, TRUE);
                match_peer_other = get_peer_match(other_id, cfg, FALSE);
                match_ike = get_ike_match(cfg->get_ike_cfg(cfg), me, other);
                match_peer_me = get_peer_match(my_id, cfg, TRUE);
                match_peer_other = get_peer_match(other_id, cfg, FALSE);
                match_ike = get_ike_match(cfg->get_ike_cfg(cfg), me, other);
+               DBG3(DBG_CFG, "ike config match: %d (%H %H)", match_ike, me, other);
 
                if (match_peer_me && match_peer_other && match_ike)
                {
 
                if (match_peer_me && match_peer_other && match_ike)
                {
@@ -372,10 +384,8 @@ static enumerator_t *create_peer_cfg_enumerator(private_backend_manager_t *this,
                                                                        (void*)peer_enum_filter_destroy);
 }
 
                                                                        (void*)peer_enum_filter_destroy);
 }
 
-/**
- * implements backend_manager_t.get_peer_cfg_by_name.
- */
-static peer_cfg_t *get_peer_cfg_by_name(private_backend_manager_t *this, char *name)
+METHOD(backend_manager_t, get_peer_cfg_by_name, peer_cfg_t*,
+       private_backend_manager_t *this, char *name)
 {
        backend_t *backend;
        peer_cfg_t *config = NULL;
 {
        backend_t *backend;
        peer_cfg_t *config = NULL;
@@ -392,30 +402,24 @@ static peer_cfg_t *get_peer_cfg_by_name(private_backend_manager_t *this, char *n
        return config;
 }
 
        return config;
 }
 
-/**
- * Implementation of backend_manager_t.remove_backend.
- */
-static void remove_backend(private_backend_manager_t *this, backend_t *backend)
+METHOD(backend_manager_t, remove_backend, void,
+       private_backend_manager_t *this, backend_t *backend)
 {
        this->lock->write_lock(this->lock);
        this->backends->remove(this->backends, backend, NULL);
        this->lock->unlock(this->lock);
 }
 
 {
        this->lock->write_lock(this->lock);
        this->backends->remove(this->backends, backend, NULL);
        this->lock->unlock(this->lock);
 }
 
-/**
- * Implementation of backend_manager_t.add_backend.
- */
-static void add_backend(private_backend_manager_t *this, backend_t *backend)
+METHOD(backend_manager_t, add_backend, void,
+       private_backend_manager_t *this, backend_t *backend)
 {
        this->lock->write_lock(this->lock);
        this->backends->insert_last(this->backends, backend);
        this->lock->unlock(this->lock);
 }
 
 {
        this->lock->write_lock(this->lock);
        this->backends->insert_last(this->backends, backend);
        this->lock->unlock(this->lock);
 }
 
-/**
- * Implementation of backend_manager_t.destroy.
- */
-static void destroy(private_backend_manager_t *this)
+METHOD(backend_manager_t, destroy, void,
+       private_backend_manager_t *this)
 {
        this->backends->destroy(this->backends);
        this->lock->destroy(this->lock);
 {
        this->backends->destroy(this->backends);
        this->lock->destroy(this->lock);
@@ -424,20 +428,24 @@ static void destroy(private_backend_manager_t *this)
 
 /*
  * Described in header-file
 
 /*
  * Described in header-file
+
  */
 backend_manager_t *backend_manager_create()
 {
  */
 backend_manager_t *backend_manager_create()
 {
-       private_backend_manager_t *this = malloc_thing(private_backend_manager_t);
-
-       this->public.get_ike_cfg = (ike_cfg_t* (*)(backend_manager_t*, host_t*, host_t*))get_ike_cfg;
-       this->public.get_peer_cfg_by_name = (peer_cfg_t* (*)(backend_manager_t*,char*))get_peer_cfg_by_name;
-       this->public.create_peer_cfg_enumerator = (enumerator_t* (*)(backend_manager_t*,host_t*,host_t*,identification_t*,identification_t*))create_peer_cfg_enumerator;
-       this->public.add_backend = (void(*)(backend_manager_t*, backend_t *backend))add_backend;
-       this->public.remove_backend = (void(*)(backend_manager_t*, backend_t *backend))remove_backend;
-       this->public.destroy = (void (*)(backend_manager_t*))destroy;
+       private_backend_manager_t *this;
 
 
-       this->backends = linked_list_create();
-       this->lock = rwlock_create(RWLOCK_TYPE_DEFAULT);
+       INIT(this,
+               .public = {
+                       .get_ike_cfg = _get_ike_cfg,
+                       .get_peer_cfg_by_name = _get_peer_cfg_by_name,
+                       .create_peer_cfg_enumerator = _create_peer_cfg_enumerator,
+                       .add_backend = _add_backend,
+                       .remove_backend = _remove_backend,
+                       .destroy = _destroy,
+               },
+               .backends = linked_list_create(),
+               .lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
+       );
 
        return &this->public;
 }
 
        return &this->public;
 }