From: "Daniel P. Berrange" <berrange(a)redhat.com>
Move the virNetDevGetIPv4Address function to virnetdev.c
* util/interface.c, util/interface.h: Remove virNetDevGetIPv4Address
* util/virnetdev.c, util/virnetdev.h: Add virNetDevGetIPv4Address
---
src/util/interface.c | 63 --------------------------------------------------
src/util/interface.h | 3 --
src/util/virnetdev.c | 53 ++++++++++++++++++++++++++++++++++++++++++
src/util/virnetdev.h | 2 +
4 files changed, 55 insertions(+), 66 deletions(-)
diff --git a/src/util/interface.c b/src/util/interface.c
index 4e1ee25..e86d183 100644
--- a/src/util/interface.c
+++ b/src/util/interface.c
@@ -141,69 +141,6 @@ ifaceCheck(bool reportError ATTRIBUTE_UNUSED,
#endif /* __linux__ */
-/**
- * virNetDevGetIPv4Address:
- * @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.
- */
-#ifdef __linux__
-int virNetDevGetIPv4Address(const char *ifname,
- virSocketAddrPtr addr)
-{
- struct ifreq ifr;
- int fd;
- int rc = 0;
-
- memset (addr, 0, sizeof(*addr));
- addr->data.stor.ss_family = AF_UNSPEC;
-
- fd = socket(AF_INET, SOCK_STREAM, 0);
- if (fd < 0) {
- virReportSystemError(errno, "%s",
- _("Unable to open control socket"));
- return -1;
- }
-
- memset(&ifr, 0, sizeof(struct ifreq));
- if (virStrcpyStatic(ifr.ifr_name, ifname) == NULL) {
- virReportSystemError(ERANGE,
- _("invalid interface name %s"),
- ifname);
- goto cleanup;
- }
-
- if (ioctl(fd, SIOCGIFADDR, (char *)&ifr) < 0) {
- virReportSystemError(errno,
- _("Unable to get IPv4 address for interface %s"),
ifname);
- goto cleanup;
- }
-
- addr->data.stor.ss_family = AF_INET;
- addr->len = sizeof(addr->data.inet4);
- memcpy(&addr->data.inet4, &ifr.ifr_addr, addr->len);
-
-cleanup:
- VIR_FORCE_CLOSE(fd);
- return rc;
-}
-
-#else
-
-int virNetDevGetIPv4Address(const char *ifname ATTRIBUTE_UNUSED,
- virSocketAddrPtr addr ATTRIBUTE_UNUSED)
-{
- virReportSystemError(ENOSYS, "%s",
- _("Unable to get IPv4 address on this platform"));
- return -1;
-}
-
-#endif /* __linux__ */
-
#if defined(__linux__) && defined(IFLA_PORT_MAX)
diff --git a/src/util/interface.h b/src/util/interface.h
index 0a11ecd..afe417d 100644
--- a/src/util/interface.h
+++ b/src/util/interface.h
@@ -33,9 +33,6 @@ struct nlattr;
int ifaceCheck(bool reportError, const char *ifname,
const unsigned char *macaddr, int ifindex);
-int virNetDevGetIPv4Address(const char *ifname, virSocketAddrPtr addr)
- ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
-
int ifaceMacvtapLinkDump(bool nltarget_kernel, const char *ifname, int ifindex,
struct nlattr **tb, unsigned char **recvbuf,
uint32_t (*getPidFunc)(void));
diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c
index c2a0f57..8a26d4a 100644
--- a/src/util/virnetdev.c
+++ b/src/util/virnetdev.c
@@ -803,3 +803,56 @@ cleanup:
virCommandFree(cmd);
return ret;
}
+
+
+/**
+ * virNetDevGetIPv4Address:
+ * @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.
+ */
+#ifdef __linux__
+int virNetDevGetIPv4Address(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);
+ goto cleanup;
+ }
+
+ addr->data.stor.ss_family = AF_INET;
+ addr->len = sizeof(addr->data.inet4);
+ memcpy(&addr->data.inet4, &ifr.ifr_addr, addr->len);
+ ret = 0;
+
+cleanup:
+ VIR_FORCE_CLOSE(fd);
+ return ret;
+}
+
+#else
+
+int virNetDevGetIPv4Address(const char *ifname ATTRIBUTE_UNUSED,
+ virSocketAddrPtr addr ATTRIBUTE_UNUSED)
+{
+ virReportSystemError(ENOSYS, "%s",
+ _("Unable to get IPv4 address on this platform"));
+ return -1;
+}
+
+#endif /* __linux__ */
diff --git a/src/util/virnetdev.h b/src/util/virnetdev.h
index 8c1d7c6..08bbcbf 100644
--- a/src/util/virnetdev.h
+++ b/src/util/virnetdev.h
@@ -43,6 +43,8 @@ int virNetDevClearIPv4Address(const char *ifname,
virSocketAddr *addr,
unsigned int prefix)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
+int virNetDevGetIPv4Address(const char *ifname, virSocketAddrPtr addr)
+ ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
int virNetDevSetMAC(const char *ifname,
--
1.7.6.4