Add a lookup method to lookip plugin, using a callback to invoke

This commit is contained in:
Martin Willi 2012-10-03 17:13:37 +02:00
parent 2caa27d42e
commit 9c54b445e2
2 changed files with 53 additions and 0 deletions

View 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),

View File

@ -25,6 +25,19 @@
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.
*/
@ -35,6 +48,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.
*/