kernel-interface: support enumeration of virtual-only IPs
authorMartin Willi <martin@revosec.ch>
Fri, 19 Apr 2013 12:52:29 +0000 (14:52 +0200)
committerMartin Willi <martin@revosec.ch>
Mon, 6 May 2013 14:10:13 +0000 (16:10 +0200)
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 0d3417f..1650362 100644 (file)
@@ -35,17 +35,17 @@ typedef enum kernel_address_type_t kernel_address_type_t;
  */
 enum kernel_address_type_t {
        /** normal addresses (on regular, up, non-ignored) interfaces */
-       ADDR_TYPE_REGULAR = 0,
+       ADDR_TYPE_REGULAR = (1 << 0),
        /** addresses on down interfaces */
-       ADDR_TYPE_DOWN =  (1 << 0),
+       ADDR_TYPE_DOWN =  (1 << 1),
        /** addresses on ignored interfaces */
-       ADDR_TYPE_IGNORED = (1 << 1),
+       ADDR_TYPE_IGNORED = (1 << 2),
        /** addresses on loopback interfaces */
-       ADDR_TYPE_LOOPBACK = (1 << 2),
+       ADDR_TYPE_LOOPBACK = (1 << 3),
        /** virtual IP addresses */
-       ADDR_TYPE_VIRTUAL = (1 << 3),
+       ADDR_TYPE_VIRTUAL = (1 << 4),
        /** to enumerate all available addresses */
-       ADDR_TYPE_ALL = (1 << 4) - 1,
+       ADDR_TYPE_ALL = (1 << 5) - 1,
 };
 
 /**
index 739a1f4..d4cb4e1 100644 (file)
@@ -1165,6 +1165,10 @@ static bool filter_addresses(address_enumerator_t *data,
        {       /* skip virtual interfaces added by us */
                return FALSE;
        }
+       if (!(data->which & ADDR_TYPE_REGULAR) && !(*in)->refcount)
+       {       /* address is regular, but not requested */
+               return FALSE;
+       }
        if ((*in)->scope >= RT_SCOPE_LINK)
        {       /* skip addresses with a unusable scope */
                return FALSE;
@@ -1209,9 +1213,12 @@ 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, kernel_address_type_t which)
 {
-       address_enumerator_t *data = malloc_thing(address_enumerator_t);
-       data->this = this;
-       data->which = which;
+       address_enumerator_t *data;
+
+       INIT(data,
+               .this = this,
+               .which = which,
+       );
 
        this->lock->read_lock(this->lock);
        return enumerator_create_nested(
index f8d3f2c..2aeb840 100644 (file)
@@ -704,6 +704,10 @@ static bool filter_addresses(address_enumerator_t *data,
        {   /* skip virtual interfaces added by us */
                return FALSE;
        }
+       if (!(data->which & ADDR_TYPE_REGULAR) && !(*in)->virtual)
+       {       /* address is regular, but not requested */
+               return FALSE;
+       }
        ip = (*in)->ip;
        if (ip->get_family(ip) == AF_INET6)
        {