trap-manager: add a method to find reqid for installed traps by config
authorMartin Willi <martin@revosec.ch>
Wed, 5 Jun 2013 12:20:44 +0000 (14:20 +0200)
committerMartin Willi <martin@revosec.ch>
Wed, 19 Jun 2013 14:30:40 +0000 (16:30 +0200)
src/libcharon/sa/trap_manager.c
src/libcharon/sa/trap_manager.h

index 5d947ec..ab638ff 100644 (file)
@@ -142,6 +142,8 @@ METHOD(trap_manager_t, install, u_int32_t,
                }
        }
        enumerator->destroy(enumerator);
+       this->lock->unlock(this->lock);
+
        if (found)
        {       /* config might have changed so update everything */
                DBG1(DBG_CFG, "updating already routed CHILD_SA '%s'",
@@ -180,10 +182,11 @@ METHOD(trap_manager_t, install, u_int32_t,
                        .child_sa = child_sa,
                        .peer_cfg = peer->get_ref(peer),
                );
+               this->lock->write_lock(this->lock);
                this->traps->insert_last(this->traps, entry);
+               this->lock->unlock(this->lock);
                reqid = child_sa->get_reqid(child_sa);
        }
-       this->lock->unlock(this->lock);
 
        if (status != SUCCESS)
        {
@@ -252,6 +255,31 @@ METHOD(trap_manager_t, create_enumerator, enumerator_t*,
                                                                        (void*)this->lock->unlock);
 }
 
+METHOD(trap_manager_t, find_reqid, u_int32_t,
+       private_trap_manager_t *this, child_cfg_t *child)
+{
+       enumerator_t *enumerator;
+       child_cfg_t *current;
+       entry_t *entry;
+       u_int32_t reqid = 0;
+
+       this->lock->read_lock(this->lock);
+       enumerator = this->traps->create_enumerator(this->traps);
+       while (enumerator->enumerate(enumerator, &entry))
+       {
+               current = entry->child_sa->get_config(entry->child_sa);
+               if (streq(current->get_name(current), child->get_name(child)))
+               {
+                       reqid = entry->child_sa->get_reqid(entry->child_sa);
+                       break;
+               }
+       }
+       enumerator->destroy(enumerator);
+       this->lock->unlock(this->lock);
+
+       return reqid;
+}
+
 METHOD(trap_manager_t, acquire, void,
        private_trap_manager_t *this, u_int32_t reqid,
        traffic_selector_t *src, traffic_selector_t *dst)
@@ -417,6 +445,7 @@ trap_manager_t *trap_manager_create(void)
                        .install = _install,
                        .uninstall = _uninstall,
                        .create_enumerator = _create_enumerator,
+                       .find_reqid = _find_reqid,
                        .acquire = _acquire,
                        .flush = _flush,
                        .destroy = _destroy,
@@ -435,4 +464,3 @@ trap_manager_t *trap_manager_create(void)
 
        return &this->public;
 }
-
index e3d3556..97de456 100644 (file)
@@ -58,6 +58,14 @@ struct trap_manager_t {
        enumerator_t* (*create_enumerator)(trap_manager_t *this);
 
        /**
+        * Find the reqid of a child config installed as a trap.
+        *
+        * @param child         CHILD_SA config to get the reqid for
+        * @return                      reqid of trap, 0 if not found
+        */
+       u_int32_t (*find_reqid)(trap_manager_t *this, child_cfg_t *child);
+
+       /**
         * Acquire an SA triggered by an installed trap.
         *
         * @param reqid         requid of the triggering CHILD_SA