Add a lookup method to lookip plugin, using a callback to invoke
authorMartin Willi <martin@revosec.ch>
Wed, 3 Oct 2012 15:13:37 +0000 (17:13 +0200)
committerMartin Willi <martin@revosec.ch>
Wed, 24 Oct 2012 09:43:34 +0000 (11:43 +0200)
src/libcharon/plugins/lookip/lookip_listener.c
src/libcharon/plugins/lookip/lookip_listener.h

index c1cdf5e..77ad78e 100644 (file)
@@ -172,6 +172,35 @@ METHOD(listener_t, ike_updown, bool,
        return TRUE;
 }
 
+METHOD(lookip_listener_t, lookup, void,
+       private_lookip_listener_t *this, host_t *vip,
+       lookip_callback_t cb, void *user)
+{
+       entry_t *entry;
+
+       this->lock->read_lock(this->lock);
+       if (vip)
+       {
+               entry = this->entries->get(this->entries, vip);
+               if (entry)
+               {
+                       cb(user, entry->vip, entry->other, entry->id, entry->name);
+               }
+       }
+       else
+       {
+               enumerator_t *enumerator;
+
+               enumerator = this->entries->create_enumerator(this->entries);
+               while (enumerator->enumerate(enumerator, &vip, &entry))
+               {
+                       cb(user, entry->vip, entry->other, entry->id, entry->name);
+               }
+               enumerator->destroy(enumerator);
+       }
+       this->lock->unlock(this->lock);
+}
+
 METHOD(lookip_listener_t, destroy, void,
        private_lookip_listener_t *this)
 {
@@ -193,6 +222,7 @@ lookip_listener_t *lookip_listener_create()
                                .message = _message_hook,
                                .ike_updown = _ike_updown,
                        },
+                       .lookup = _lookup,
                        .destroy = _destroy,
                },
                .lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
index daf979e..eeb955e 100644 (file)
 typedef struct lookip_listener_t lookip_listener_t;
 
 /**
+ * Callback function to query virtual IP entries
+ *
+ * @param user         user supplied pointer
+ * @param vip          virtual IP of remote peer
+ * @param other                peer external IP
+ * @param id           peer identity
+ * @param name         associated connection name
+ * @return                     TRUE to receive more results, FALSE to cancel
+ */
+typedef bool (*lookip_callback_t)(void *user, host_t *vip, host_t *other,
+                                                                 identification_t *id, char *name);
+
+/**
  * Listener collecting virtual IPs.
  */
 struct lookip_listener_t {
@@ -36,6 +49,16 @@ struct lookip_listener_t {
        listener_t listener;
 
        /**
+        * Perform a lookup for a given virtual IP, invoke callback for matches.
+        *
+        * @param vip           virtual IP to look up, NULL to get all entries
+        * @param cb            callback function to invoke
+        * @param user          user data to pass to callback function
+        */
+       void (*lookup)(lookip_listener_t *this, host_t *vip,
+                                  lookip_callback_t cb, void *user);
+
+       /**
         * Destroy a lookip_listener_t.
         */
        void (*destroy)(lookip_listener_t *this);