Made IP address enumeration more flexible

Also added an option to enumerate addresses on ignored interfaces.
This commit is contained in:
Tobias Brunner
2012-09-21 18:16:26 +02:00
parent 308ec0b7df
commit 4106aea8e4
10 changed files with 50 additions and 53 deletions
+1 -1
View File
@@ -517,7 +517,7 @@ METHOD(stroke_list_t, status, void,
enumerator->destroy(enumerator); enumerator->destroy(enumerator);
enumerator = hydra->kernel_interface->create_address_enumerator( enumerator = hydra->kernel_interface->create_address_enumerator(
hydra->kernel_interface, FALSE, FALSE, FALSE); hydra->kernel_interface, ADDR_TYPE_REGULAR);
fprintf(out, "Listening IP addresses:\n"); fprintf(out, "Listening IP addresses:\n");
while (enumerator->enumerate(enumerator, (void**)&host)) while (enumerator->enumerate(enumerator, (void**)&host))
{ {
@@ -49,7 +49,7 @@ static bool publish_device_ip_addresses(private_tnc_ifmap_listener_t *this)
bool success = TRUE; bool success = TRUE;
enumerator = hydra->kernel_interface->create_address_enumerator( enumerator = hydra->kernel_interface->create_address_enumerator(
hydra->kernel_interface, FALSE, FALSE, FALSE); hydra->kernel_interface, ADDR_TYPE_REGULAR);
while (enumerator->enumerate(enumerator, &host)) while (enumerator->enumerate(enumerator, &host))
{ {
if (!this->ifmap->publish_device_ip(this->ifmap, host)) if (!this->ifmap->publish_device_ip(this->ifmap, host))
+1 -1
View File
@@ -136,7 +136,7 @@ static void gather_and_add_endpoints(private_ike_me_t *this, message_t *message)
port = host->get_port(host); port = host->get_port(host);
enumerator = hydra->kernel_interface->create_address_enumerator( enumerator = hydra->kernel_interface->create_address_enumerator(
hydra->kernel_interface, FALSE, FALSE, FALSE); hydra->kernel_interface, ADDR_TYPE_REGULAR);
while (enumerator->enumerate(enumerator, (void**)&addr)) while (enumerator->enumerate(enumerator, (void**)&addr))
{ {
host = addr->clone(addr); host = addr->clone(addr);
+1 -1
View File
@@ -192,7 +192,7 @@ static void build_address_list(private_ike_mobike_t *this, message_t *message)
me = this->ike_sa->get_my_host(this->ike_sa); me = this->ike_sa->get_my_host(this->ike_sa);
enumerator = hydra->kernel_interface->create_address_enumerator( enumerator = hydra->kernel_interface->create_address_enumerator(
hydra->kernel_interface, FALSE, FALSE, FALSE); hydra->kernel_interface, ADDR_TYPE_REGULAR);
while (enumerator->enumerate(enumerator, (void**)&host)) while (enumerator->enumerate(enumerator, (void**)&host))
{ {
if (me->ip_equals(me, host)) if (me->ip_equals(me, host))
+1 -1
View File
@@ -341,7 +341,7 @@ METHOD(task_t, build_i, status_t,
else else
{ /* 3. */ { /* 3. */
enumerator = hydra->kernel_interface->create_address_enumerator( enumerator = hydra->kernel_interface->create_address_enumerator(
hydra->kernel_interface, FALSE, FALSE, FALSE); hydra->kernel_interface, ADDR_TYPE_REGULAR);
while (enumerator->enumerate(enumerator, (void**)&host)) while (enumerator->enumerate(enumerator, (void**)&host))
{ {
/* apply port 500 to host, but work on a copy */ /* apply port 500 to host, but work on a copy */
+3 -5
View File
@@ -302,15 +302,13 @@ METHOD(kernel_interface_t, get_interface, bool,
} }
METHOD(kernel_interface_t, create_address_enumerator, enumerator_t*, METHOD(kernel_interface_t, create_address_enumerator, enumerator_t*,
private_kernel_interface_t *this, bool include_down_ifaces, private_kernel_interface_t *this, kernel_address_type_t which)
bool include_virtual_ips, bool include_loopback)
{ {
if (!this->net) if (!this->net)
{ {
return enumerator_create_empty(); return enumerator_create_empty();
} }
return this->net->create_address_enumerator(this->net, include_down_ifaces, return this->net->create_address_enumerator(this->net, which);
include_virtual_ips, include_loopback);
} }
METHOD(kernel_interface_t, add_ip, status_t, METHOD(kernel_interface_t, add_ip, status_t,
@@ -423,7 +421,7 @@ METHOD(kernel_interface_t, get_address_by_ts, status_t,
} }
host->destroy(host); host->destroy(host);
addrs = create_address_enumerator(this, TRUE, TRUE, TRUE); addrs = create_address_enumerator(this, ADDR_TYPE_ALL);
while (addrs->enumerate(addrs, (void**)&host)) while (addrs->enumerate(addrs, (void**)&host))
{ {
if (ts->includes(ts, host)) if (ts->includes(ts, host))
+3 -6
View File
@@ -321,14 +321,11 @@ struct kernel_interface_t {
* enumerator gets destroyed. * enumerator gets destroyed.
* The hosts are read-only, do not modify of free. * The hosts are read-only, do not modify of free.
* *
* @param include_down_ifaces TRUE to enumerate addresses from down interfaces * @param which a combination of address types to enumerate
* @param include_virtual_ips TRUE to enumerate virtual IP addresses * @return enumerator over host_t's
* @param include_loopback TRUE to enumerate addresses on loopback interfaces
* @return enumerator over host_t's
*/ */
enumerator_t *(*create_address_enumerator) (kernel_interface_t *this, enumerator_t *(*create_address_enumerator) (kernel_interface_t *this,
bool include_down_ifaces, bool include_virtual_ips, kernel_address_type_t which);
bool include_loopback);
/** /**
* Add a virtual IP to an interface. * Add a virtual IP to an interface.
+22 -6
View File
@@ -23,11 +23,30 @@
#define KERNEL_NET_H_ #define KERNEL_NET_H_
typedef struct kernel_net_t kernel_net_t; typedef struct kernel_net_t kernel_net_t;
typedef enum kernel_address_type_t kernel_address_type_t;
#include <utils/enumerator.h> #include <utils/enumerator.h>
#include <utils/host.h> #include <utils/host.h>
#include <plugins/plugin.h> #include <plugins/plugin.h>
/**
* Type of addresses (e.g. when enumerating them)
*/
enum kernel_address_type_t {
/** normal addresses (on regular, up, non-ignored) interfaces */
ADDR_TYPE_REGULAR = 0,
/** addresses on down interfaces */
ADDR_TYPE_DOWN = (1 << 0),
/** addresses on ignored interfaces */
ADDR_TYPE_IGNORED = (1 << 1),
/** addresses on loopback interfaces */
ADDR_TYPE_LOOPBACK = (1 << 2),
/** virtual IP addresses */
ADDR_TYPE_VIRTUAL = (1 << 3),
/** to enumerate all available addresses */
ADDR_TYPE_ALL = (1 << 4) - 1,
};
/** /**
* Interface to the network subsystem of the kernel. * Interface to the network subsystem of the kernel.
* *
@@ -81,14 +100,11 @@ struct kernel_net_t {
* enumerator gets destroyed. * enumerator gets destroyed.
* The hosts are read-only, do not modify of free. * The hosts are read-only, do not modify of free.
* *
* @param include_down_ifaces TRUE to enumerate addresses from down interfaces * @param which a combination of address types to enumerate
* @param include_virtual_ips TRUE to enumerate virtual IP addresses * @return enumerator over host_t's
* @param include_loopback TRUE to enumerate addresses on loopback interfaces
* @return enumerator over host_t's
*/ */
enumerator_t *(*create_address_enumerator) (kernel_net_t *this, enumerator_t *(*create_address_enumerator) (kernel_net_t *this,
bool include_down_ifaces, bool include_virtual_ips, kernel_address_type_t which);
bool include_loopback);
/** /**
* Add a virtual IP to an interface. * Add a virtual IP to an interface.
@@ -1080,12 +1080,8 @@ static job_requeue_t receive_events(private_kernel_netlink_net_t *this)
/** enumerator over addresses */ /** enumerator over addresses */
typedef struct { typedef struct {
private_kernel_netlink_net_t* this; private_kernel_netlink_net_t* this;
/** whether to enumerate down interfaces */ /** which addresses to enumerate */
bool include_down_ifaces; kernel_address_type_t which;
/** whether to enumerate virtual ip addresses */
bool include_virtual_ips;
/** whether to enumerate loopback interfaces */
bool include_loopback;
} address_enumerator_t; } address_enumerator_t;
/** /**
@@ -1103,7 +1099,7 @@ static void address_enumerator_destroy(address_enumerator_t *data)
static bool filter_addresses(address_enumerator_t *data, static bool filter_addresses(address_enumerator_t *data,
addr_entry_t** in, host_t** out) addr_entry_t** in, host_t** out)
{ {
if (!data->include_virtual_ips && (*in)->virtual) if (!(data->which & ADDR_TYPE_VIRTUAL) && (*in)->virtual)
{ /* skip virtual interfaces added by us */ { /* skip virtual interfaces added by us */
return FALSE; return FALSE;
} }
@@ -1132,15 +1128,15 @@ static enumerator_t *create_iface_enumerator(iface_entry_t *iface,
static bool filter_interfaces(address_enumerator_t *data, iface_entry_t** in, static bool filter_interfaces(address_enumerator_t *data, iface_entry_t** in,
iface_entry_t** out) iface_entry_t** out)
{ {
if (!(*in)->usable) if (!(data->which & ADDR_TYPE_IGNORED) && !(*in)->usable)
{ /* skip interfaces excluded by config */ { /* skip interfaces excluded by config */
return FALSE; return FALSE;
} }
if (!data->include_loopback && ((*in)->flags & IFF_LOOPBACK)) if (!(data->which & ADDR_TYPE_LOOPBACK) && ((*in)->flags & IFF_LOOPBACK))
{ /* ignore loopback devices */ { /* ignore loopback devices */
return FALSE; return FALSE;
} }
if (!data->include_down_ifaces && !((*in)->flags & IFF_UP)) if (!(data->which & ADDR_TYPE_DOWN) && !((*in)->flags & IFF_UP))
{ /* skip interfaces not up */ { /* skip interfaces not up */
return FALSE; return FALSE;
} }
@@ -1149,14 +1145,11 @@ static bool filter_interfaces(address_enumerator_t *data, iface_entry_t** in,
} }
METHOD(kernel_net_t, create_address_enumerator, enumerator_t*, METHOD(kernel_net_t, create_address_enumerator, enumerator_t*,
private_kernel_netlink_net_t *this, private_kernel_netlink_net_t *this, kernel_address_type_t which)
bool include_down_ifaces, bool include_virtual_ips, bool include_loopback)
{ {
address_enumerator_t *data = malloc_thing(address_enumerator_t); address_enumerator_t *data = malloc_thing(address_enumerator_t);
data->this = this; data->this = this;
data->include_down_ifaces = include_down_ifaces; data->which = which;
data->include_virtual_ips = include_virtual_ips;
data->include_loopback = include_loopback;
this->mutex->lock(this->mutex); this->mutex->lock(this->mutex);
return enumerator_create_nested( return enumerator_create_nested(
@@ -509,12 +509,8 @@ static job_requeue_t receive_events(private_kernel_pfroute_net_t *this)
/** enumerator over addresses */ /** enumerator over addresses */
typedef struct { typedef struct {
private_kernel_pfroute_net_t* this; private_kernel_pfroute_net_t* this;
/** whether to enumerate down interfaces */ /** which addresses to enumerate */
bool include_down_ifaces; address_type_t which;
/** whether to enumerate virtual ip addresses */
bool include_virtual_ips;
/** whether to enumerate loopback interfaces */
bool include_loopback;
} address_enumerator_t; } address_enumerator_t;
/** /**
@@ -533,7 +529,7 @@ static bool filter_addresses(address_enumerator_t *data,
addr_entry_t** in, host_t** out) addr_entry_t** in, host_t** out)
{ {
host_t *ip; host_t *ip;
if (!data->include_virtual_ips && (*in)->virtual) if (!(data->which & ADDR_TYPE_VIRTUAL) && (*in)->virtual)
{ /* skip virtual interfaces added by us */ { /* skip virtual interfaces added by us */
return FALSE; return FALSE;
} }
@@ -566,16 +562,16 @@ static enumerator_t *create_iface_enumerator(iface_entry_t *iface,
static bool filter_interfaces(address_enumerator_t *data, iface_entry_t** in, static bool filter_interfaces(address_enumerator_t *data, iface_entry_t** in,
iface_entry_t** out) iface_entry_t** out)
{ {
if (!(*in)->usable) if (!(data->which & ADDR_TYPE_IGNORED) && !(*in)->usable)
{ /* skip interfaces excluded by config */ { /* skip interfaces excluded by config */
return FALSE; return FALSE;
} }
if (!data->include_loopback && ((*in)->flags & IFF_LOOPBACK)) if (!(data->which & ADDR_TYPE_LOOPBACK) && ((*in)->flags & IFF_LOOPBACK))
{ /* ignore loopback devices */ { /* ignore loopback devices */
return FALSE; return FALSE;
} }
if (!data->include_down_ifaces && !((*in)->flags & IFF_UP)) if (!(data->which & ADDR_TYPE_DOWN) && !((*in)->flags & IFF_UP))
{ /* skip interfaces not up */ { /* skip interfaces not up */
return FALSE; return FALSE;
} }
*out = *in; *out = *in;
@@ -583,14 +579,11 @@ static bool filter_interfaces(address_enumerator_t *data, iface_entry_t** in,
} }
METHOD(kernel_net_t, create_address_enumerator, enumerator_t*, METHOD(kernel_net_t, create_address_enumerator, enumerator_t*,
private_kernel_pfroute_net_t *this, private_kernel_pfroute_net_t *this, address_type_t which)
bool include_down_ifaces, bool include_virtual_ips, bool include_loopback)
{ {
address_enumerator_t *data = malloc_thing(address_enumerator_t); address_enumerator_t *data = malloc_thing(address_enumerator_t);
data->this = this; data->this = this;
data->include_down_ifaces = include_down_ifaces; data->which = which;
data->include_virtual_ips = include_virtual_ips;
data->include_loopback = include_loopback;
this->mutex->lock(this->mutex); this->mutex->lock(this->mutex);
return enumerator_create_nested( return enumerator_create_nested(