diff options
author | Martin Willi <martin@revosec.ch> | 2010-03-19 13:29:28 +0100 |
---|---|---|
committer | Martin Willi <martin@revosec.ch> | 2010-03-25 14:39:32 +0100 |
commit | 660e16f5b2154412f9bfbc54136010307feede26 (patch) | |
tree | b929a75de8c29be69cc6276605b3e2482258575a /src/libcharon/plugins/farp | |
parent | 0d7b48a388629c1d80874c38e598541c0b305bf7 (diff) | |
download | strongswan-660e16f5b2154412f9bfbc54136010307feede26.tar.bz2 strongswan-660e16f5b2154412f9bfbc54136010307feede26.tar.xz |
Added a listener to the farp plugin that keeps track of active virtual IPs
Diffstat (limited to 'src/libcharon/plugins/farp')
-rw-r--r-- | src/libcharon/plugins/farp/Makefile.am | 3 | ||||
-rw-r--r-- | src/libcharon/plugins/farp/farp_listener.c | 118 | ||||
-rw-r--r-- | src/libcharon/plugins/farp/farp_listener.h | 58 | ||||
-rw-r--r-- | src/libcharon/plugins/farp/farp_plugin.c | 12 |
4 files changed, 190 insertions, 1 deletions
diff --git a/src/libcharon/plugins/farp/Makefile.am b/src/libcharon/plugins/farp/Makefile.am index 2e2ab34af..632218918 100644 --- a/src/libcharon/plugins/farp/Makefile.am +++ b/src/libcharon/plugins/farp/Makefile.am @@ -9,6 +9,7 @@ else plugin_LTLIBRARIES = libstrongswan-farp.la endif -libstrongswan_farp_la_SOURCES = farp_plugin.h farp_plugin.c +libstrongswan_farp_la_SOURCES = farp_plugin.h farp_plugin.c \ + farp_listener.h farp_listener.c libstrongswan_farp_la_LDFLAGS = -module -avoid-version diff --git a/src/libcharon/plugins/farp/farp_listener.c b/src/libcharon/plugins/farp/farp_listener.c new file mode 100644 index 000000000..698815f07 --- /dev/null +++ b/src/libcharon/plugins/farp/farp_listener.c @@ -0,0 +1,118 @@ +/* + * Copyright (C) 2010 Martin Willi + * Copyright (C) 2010 revosec AG + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#include "farp_listener.h" + +#include <utils/hashtable.h> + +typedef struct private_farp_listener_t private_farp_listener_t; + +/** + * Private data of an farp_listener_t object. + */ +struct private_farp_listener_t { + + /** + * Public farp_listener_t interface. + */ + farp_listener_t public; + + /** + * Hashtable with active virtual IPs + */ + hashtable_t *ips; +}; + +/** + * Hashtable hash function + */ +static u_int hash(host_t *key) +{ + return chunk_hash(key->get_address(key)); +} + +/** + * Hashtable equals function + */ +static bool equals(host_t *a, host_t *b) +{ + return a->ip_equals(a, b); +} + +METHOD(listener_t, ike_updown, bool, + private_farp_listener_t *this, ike_sa_t *ike_sa, bool up) +{ + host_t *ip; + + ip = ike_sa->get_virtual_ip(ike_sa, FALSE); + if (ip) + { + if (up) + { + ip = ip->clone(ip); + ip = this->ips->put(this->ips, ip, ip); + } + else + { + ip = this->ips->remove(this->ips, ip); + } + DESTROY_IF(ip); + } + return TRUE; +} + +METHOD(farp_listener_t, is_active, bool, + private_farp_listener_t *this, host_t *ip) +{ + return this->ips->get(this->ips, ip) != NULL; +} + +METHOD(farp_listener_t, destroy, void, + private_farp_listener_t *this) +{ + enumerator_t *enumerator; + host_t *key, *value; + + enumerator = this->ips->create_enumerator(this->ips); + while (enumerator->enumerate(enumerator, &key, &value)) + { + value->destroy(value); + } + enumerator->destroy(enumerator); + this->ips->destroy(this->ips); + + free(this); +} + +/** + * See header + */ +farp_listener_t *farp_listener_create() +{ + private_farp_listener_t *this; + + INIT(this, + .public = { + .listener.ike_updown = _ike_updown, + .is_active = _is_active, + .destroy = _destroy, + }, + .ips = hashtable_create((hashtable_hash_t)hash, + (hashtable_equals_t)equals, 8), + ); + + return &this->public; +} + diff --git a/src/libcharon/plugins/farp/farp_listener.h b/src/libcharon/plugins/farp/farp_listener.h new file mode 100644 index 000000000..bd96d7a1c --- /dev/null +++ b/src/libcharon/plugins/farp/farp_listener.h @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2010 Martin Willi + * Copyright (C) 2010 revosec AG + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +/** + * @defgroup farp_listener farp_listener + * @{ @ingroup farp + */ + +#ifndef FARP_LISTENER_H_ +#define FARP_LISTENER_H_ + +#include <utils/host.h> +#include <bus/listeners/listener.h> + +typedef struct farp_listener_t farp_listener_t; + +/** + * Listener to register the set of IPs we spoof ARP responses for. + */ +struct farp_listener_t { + + /** + * Implements listener_t interface. + */ + listener_t listener; + + /** + * Check if a given IP is currently used as virtual IP by a peer. + * + * @param ip IP to check + * @return TRUE if IP is an active virtual IP + */ + bool (*is_active)(farp_listener_t *this, host_t *ip); + + /** + * Destroy a farp_listener_t. + */ + void (*destroy)(farp_listener_t *this); +}; + +/** + * Create a farp_listener instance. + */ +farp_listener_t *farp_listener_create(); + +#endif /** FARP_LISTENER_H_ @}*/ diff --git a/src/libcharon/plugins/farp/farp_plugin.c b/src/libcharon/plugins/farp/farp_plugin.c index 7735d1241..82f1fb2aa 100644 --- a/src/libcharon/plugins/farp/farp_plugin.c +++ b/src/libcharon/plugins/farp/farp_plugin.c @@ -15,6 +15,8 @@ #include "farp_plugin.h" +#include "farp_listener.h" + #include <daemon.h> typedef struct private_farp_plugin_t private_farp_plugin_t; @@ -28,11 +30,18 @@ struct private_farp_plugin_t { * implements plugin interface */ farp_plugin_t public; + + /** + * Listener registering active virtual IPs + */ + farp_listener_t *listener; }; METHOD(plugin_t, destroy, void, private_farp_plugin_t *this) { + charon->bus->remove_listener(charon->bus, &this->listener->listener); + this->listener->destroy(this->listener); free(this); } @@ -45,8 +54,11 @@ plugin_t *farp_plugin_create() INIT(this, .public.plugin.destroy = _destroy, + .listener = farp_listener_create(), ); + charon->bus->add_listener(charon->bus, &this->listener->listener); + return &this->public.plugin; } |