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-17 19:04:51 +02:00
parent 308ec0b7df
commit 4106aea8e4
10 changed files with 50 additions and 53 deletions

View File

@ -517,7 +517,7 @@ METHOD(stroke_list_t, status, void,
enumerator->destroy(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");
while (enumerator->enumerate(enumerator, (void**)&host))
{

View File

@ -49,7 +49,7 @@ static bool publish_device_ip_addresses(private_tnc_ifmap_listener_t *this)
bool success = TRUE;
enumerator = hydra->kernel_interface->create_address_enumerator(
hydra->kernel_interface, FALSE, FALSE, FALSE);
hydra->kernel_interface, ADDR_TYPE_REGULAR);
while (enumerator->enumerate(enumerator, &host))
{
if (!this->ifmap->publish_device_ip(this->ifmap, host))

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);
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))
{
host = addr->clone(addr);

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);
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))
{
if (me->ip_equals(me, host))

View File

@ -341,7 +341,7 @@ METHOD(task_t, build_i, status_t,
else
{ /* 3. */
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))
{
/* apply port 500 to host, but work on a copy */

View File

@ -302,15 +302,13 @@ METHOD(kernel_interface_t, get_interface, bool,
}
METHOD(kernel_interface_t, create_address_enumerator, enumerator_t*,
private_kernel_interface_t *this, bool include_down_ifaces,
bool include_virtual_ips, bool include_loopback)
private_kernel_interface_t *this, kernel_address_type_t which)
{
if (!this->net)
{
return enumerator_create_empty();
}
return this->net->create_address_enumerator(this->net, include_down_ifaces,
include_virtual_ips, include_loopback);
return this->net->create_address_enumerator(this->net, which);
}
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);
addrs = create_address_enumerator(this, TRUE, TRUE, TRUE);
addrs = create_address_enumerator(this, ADDR_TYPE_ALL);
while (addrs->enumerate(addrs, (void**)&host))
{
if (ts->includes(ts, host))

View File

@ -321,14 +321,11 @@ struct kernel_interface_t {
* enumerator gets destroyed.
* The hosts are read-only, do not modify of free.
*
* @param include_down_ifaces TRUE to enumerate addresses from down interfaces
* @param include_virtual_ips TRUE to enumerate virtual IP addresses
* @param include_loopback TRUE to enumerate addresses on loopback interfaces
* @return enumerator over host_t's
* @param which a combination of address types to enumerate
* @return enumerator over host_t's
*/
enumerator_t *(*create_address_enumerator) (kernel_interface_t *this,
bool include_down_ifaces, bool include_virtual_ips,
bool include_loopback);
kernel_address_type_t which);
/**
* Add a virtual IP to an interface.

View File

@ -23,11 +23,30 @@
#define KERNEL_NET_H_
typedef struct kernel_net_t kernel_net_t;
typedef enum kernel_address_type_t kernel_address_type_t;
#include <utils/enumerator.h>
#include <utils/host.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.
*
@ -81,14 +100,11 @@ struct kernel_net_t {
* enumerator gets destroyed.
* The hosts are read-only, do not modify of free.
*
* @param include_down_ifaces TRUE to enumerate addresses from down interfaces
* @param include_virtual_ips TRUE to enumerate virtual IP addresses
* @param include_loopback TRUE to enumerate addresses on loopback interfaces
* @return enumerator over host_t's
* @param which a combination of address types to enumerate
* @return enumerator over host_t's
*/
enumerator_t *(*create_address_enumerator) (kernel_net_t *this,
bool include_down_ifaces, bool include_virtual_ips,
bool include_loopback);
kernel_address_type_t which);
/**
* Add a virtual IP to an interface.

View File

@ -1080,12 +1080,8 @@ static job_requeue_t receive_events(private_kernel_netlink_net_t *this)
/** enumerator over addresses */
typedef struct {
private_kernel_netlink_net_t* this;
/** whether to enumerate down interfaces */
bool include_down_ifaces;
/** whether to enumerate virtual ip addresses */
bool include_virtual_ips;
/** whether to enumerate loopback interfaces */
bool include_loopback;
/** which addresses to enumerate */
kernel_address_type_t which;
} address_enumerator_t;
/**
@ -1103,7 +1099,7 @@ static void address_enumerator_destroy(address_enumerator_t *data)
static bool filter_addresses(address_enumerator_t *data,
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 */
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,
iface_entry_t** out)
{
if (!(*in)->usable)
if (!(data->which & ADDR_TYPE_IGNORED) && !(*in)->usable)
{ /* skip interfaces excluded by config */
return FALSE;
}
if (!data->include_loopback && ((*in)->flags & IFF_LOOPBACK))
if (!(data->which & ADDR_TYPE_LOOPBACK) && ((*in)->flags & IFF_LOOPBACK))
{ /* ignore loopback devices */
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 */
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*,
private_kernel_netlink_net_t *this,
bool include_down_ifaces, bool include_virtual_ips, bool include_loopback)
private_kernel_netlink_net_t *this, kernel_address_type_t which)
{
address_enumerator_t *data = malloc_thing(address_enumerator_t);
data->this = this;
data->include_down_ifaces = include_down_ifaces;
data->include_virtual_ips = include_virtual_ips;
data->include_loopback = include_loopback;
data->which = which;
this->mutex->lock(this->mutex);
return enumerator_create_nested(

View File

@ -509,12 +509,8 @@ static job_requeue_t receive_events(private_kernel_pfroute_net_t *this)
/** enumerator over addresses */
typedef struct {
private_kernel_pfroute_net_t* this;
/** whether to enumerate down interfaces */
bool include_down_ifaces;
/** whether to enumerate virtual ip addresses */
bool include_virtual_ips;
/** whether to enumerate loopback interfaces */
bool include_loopback;
/** which addresses to enumerate */
address_type_t which;
} address_enumerator_t;
/**
@ -533,7 +529,7 @@ static bool filter_addresses(address_enumerator_t *data,
addr_entry_t** in, host_t** out)
{
host_t *ip;
if (!data->include_virtual_ips && (*in)->virtual)
if (!(data->which & ADDR_TYPE_VIRTUAL) && (*in)->virtual)
{ /* skip virtual interfaces added by us */
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,
iface_entry_t** out)
{
if (!(*in)->usable)
if (!(data->which & ADDR_TYPE_IGNORED) && !(*in)->usable)
{ /* skip interfaces excluded by config */
return FALSE;
}
if (!data->include_loopback && ((*in)->flags & IFF_LOOPBACK))
if (!(data->which & ADDR_TYPE_LOOPBACK) && ((*in)->flags & IFF_LOOPBACK))
{ /* ignore loopback devices */
return FALSE;
}
if (!data->include_down_ifaces && !((*in)->flags & IFF_UP))
{ /* skip interfaces not up */
if (!(data->which & ADDR_TYPE_DOWN) && !((*in)->flags & IFF_UP))
{ /* skip interfaces not up */
return FALSE;
}
*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*,
private_kernel_pfroute_net_t *this,
bool include_down_ifaces, bool include_virtual_ips, bool include_loopback)
private_kernel_pfroute_net_t *this, address_type_t which)
{
address_enumerator_t *data = malloc_thing(address_enumerator_t);
data->this = this;
data->include_down_ifaces = include_down_ifaces;
data->include_virtual_ips = include_virtual_ips;
data->include_loopback = include_loopback;
data->which = which;
this->mutex->lock(this->mutex);
return enumerator_create_nested(