Made IP address enumeration more flexible
authorTobias Brunner <tobias@strongswan.org>
Mon, 17 Sep 2012 17:04:51 +0000 (19:04 +0200)
committerTobias Brunner <tobias@strongswan.org>
Fri, 21 Sep 2012 16:16:26 +0000 (18:16 +0200)
Also added an option to enumerate addresses on ignored interfaces.

src/libcharon/plugins/stroke/stroke_list.c
src/libcharon/plugins/tnc_ifmap/tnc_ifmap_listener.c
src/libcharon/sa/ikev2/tasks/ike_me.c
src/libcharon/sa/ikev2/tasks/ike_mobike.c
src/libcharon/sa/ikev2/tasks/ike_natd.c
src/libhydra/kernel/kernel_interface.c
src/libhydra/kernel/kernel_interface.h
src/libhydra/kernel/kernel_net.h
src/libhydra/plugins/kernel_netlink/kernel_netlink_net.c
src/libhydra/plugins/kernel_pfroute/kernel_pfroute_net.c

index 10254c8..c012ff2 100644 (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))
                {
index 6902bcb..eac285c 100644 (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))
index 779f525..135c06d 100644 (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);
index 285009f..ae3526f 100644 (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))
index dc8a000..0a93db9 100644 (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 */
index 7bdc0be..7bfda24 100644 (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))
index dce3ef2..c09f07b 100644 (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.
index 1f56bf8..10350d6 100644 (file)
 #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.
  *
  * The kernel network interface handles the communication with 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.
index 745f409..99d750d 100644 (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(
index ae13709..2d34c81 100644 (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(