[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCHv4 1/3] util: Replace virNetDevGetIPv4Address with virNetDevGetIPAddress



From: John Ferlan <jferlan redhat com>

Rename it to virNetDevGetIPv4AddressIoctl and make
virNetDevGetIPAddress a wrapper around it, allowing
other ways of getting the address to be implemented,
and still falling back to the old method.

Signed-off-by: John Ferlan <jferlan redhat com>
Signed-off-by: Ján Tomko <jtomko redhat com>
---
 src/libvirt_private.syms    |  2 +-
 src/network/bridge_driver.c |  2 +-
 src/util/virnetdev.c        | 48 +++++++++++++++++++++++++++++++++------------
 src/util/virnetdev.h        |  2 +-
 4 files changed, 39 insertions(+), 15 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index a092714..b225707 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1708,7 +1708,7 @@ virNetDevDelMulti;
 virNetDevExists;
 virNetDevGetFeatures;
 virNetDevGetIndex;
-virNetDevGetIPv4Address;
+virNetDevGetIPAddress;
 virNetDevGetLinkInfo;
 virNetDevGetMAC;
 virNetDevGetMTU;
diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c
index f865adc..6db368b 100644
--- a/src/network/bridge_driver.c
+++ b/src/network/bridge_driver.c
@@ -4559,7 +4559,7 @@ networkGetNetworkAddress(const char *netname, char **netaddr)
     }
 
     if (dev_name) {
-        if (virNetDevGetIPv4Address(dev_name, &addr) < 0)
+        if (virNetDevGetIPAddress(dev_name, &addr) < 0)
             goto cleanup;
         addrptr = &addr;
     }
diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c
index 54d866e..59593f9 100644
--- a/src/util/virnetdev.c
+++ b/src/util/virnetdev.c
@@ -1386,7 +1386,7 @@ int virNetDevClearIPAddress(const char *ifname,
 #endif /* defined(__linux__) && defined(HAVE_LIBNL) */
 
 /**
- * virNetDevGetIPv4Address:
+ * virNetDevGetIPv4AddressIoctl:
  * @ifname: name of the interface whose IP address we want
  * @addr: filled with the IPv4 address
  *
@@ -1396,22 +1396,21 @@ int virNetDevClearIPAddress(const char *ifname,
  * Returns 0 on success, -errno on failure.
  */
 #if defined(SIOCGIFADDR) && defined(HAVE_STRUCT_IFREQ)
-int virNetDevGetIPv4Address(const char *ifname,
-                            virSocketAddrPtr addr)
+static int
+virNetDevGetIPv4AddressIoctl(const char *ifname,
+                             virSocketAddrPtr addr)
 {
     int fd = -1;
     int ret = -1;
     struct ifreq ifr;
 
-    memset(addr, 0, sizeof(*addr));
-    addr->data.stor.ss_family = AF_UNSPEC;
-
     if ((fd = virNetDevSetupControl(ifname, &ifr)) < 0)
         return -1;
 
     if (ioctl(fd, SIOCGIFADDR, (char *)&ifr) < 0) {
         virReportSystemError(errno,
-                             _("Unable to get IPv4 address for interface %s"), ifname);
+                             _("Unable to get IPv4 address for interface %s via ioctl"),
+                             ifname);
         goto cleanup;
     }
 
@@ -1427,16 +1426,41 @@ int virNetDevGetIPv4Address(const char *ifname,
 
 #else /* ! SIOCGIFADDR */
 
-int virNetDevGetIPv4Address(const char *ifname ATTRIBUTE_UNUSED,
-                            virSocketAddrPtr addr ATTRIBUTE_UNUSED)
+static int
+virNetDevGetIPv4AddressIoctl(const char *ifname ATTRIBUTE_UNUSED,
+                             virSocketAddrPtr addr ATTRIBUTE_UNUSED)
 {
-    virReportSystemError(ENOSYS, "%s",
-                         _("Unable to get IPv4 address on this platform"));
-    return -1;
+    return -2;
 }
 
 #endif /* ! SIOCGIFADDR */
 
+/**
+ * virNetDevGetIPAddress:
+ * @ifname: name of the interface whose IP address we want
+ * @addr: filled with the IPv4 address
+ *
+ * This function gets the IPv4 address for the interface @ifname
+ * and stores it in @addr
+ *
+ * Returns 0 on success, -errno on failure.
+ */
+int
+virNetDevGetIPAddress(const char *ifname,
+                      virSocketAddrPtr addr)
+{
+    int ret;
+
+    memset(addr, 0, sizeof(*addr));
+    addr->data.stor.ss_family = AF_UNSPEC;
+
+    if ((ret = virNetDevGetIPv4AddressIoctl(ifname, addr)) != -2)
+        return ret;
+
+    virReportSystemError(ENOSYS, "%s",
+                         _("Unable to get IP address on this platform"));
+    return -1;
+}
 
 /**
  * virNetDevValidateConfig:
diff --git a/src/util/virnetdev.h b/src/util/virnetdev.h
index 856127b..fb0fd46 100644
--- a/src/util/virnetdev.h
+++ b/src/util/virnetdev.h
@@ -103,7 +103,7 @@ int virNetDevClearIPAddress(const char *ifname,
                             virSocketAddr *addr,
                             unsigned int prefix)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
-int virNetDevGetIPv4Address(const char *ifname, virSocketAddrPtr addr)
+int virNetDevGetIPAddress(const char *ifname, virSocketAddrPtr addr)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
 
 
-- 
2.0.5


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]