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

[libvirt] [PATCH v2 1/4] virsocketaddr: return address family in virSocketAddrIsNumeric



nowadays, virSocketAddrIsNumeric only validated the income address
if numeric, but sometimes we need to know whether the address is
an IPv4 or an IPv6 address.

Signed-off-by: Chen Fan <chen fan fnst cn fujitsu com>
---
 src/qemu/qemu_migration.c |  4 ++--
 src/util/virsocketaddr.c  | 13 +++++++++----
 src/util/virsocketaddr.h  |  2 +-
 tests/sockettest.c        |  2 +-
 4 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/src/qemu/qemu_migration.c b/src/qemu/qemu_migration.c
index ce1a5cd..155f5b9 100644
--- a/src/qemu/qemu_migration.c
+++ b/src/qemu/qemu_migration.c
@@ -2565,7 +2565,7 @@ qemuMigrationPrepareAny(virQEMUDriverPtr driver,
         virObjectUnref(qemuCaps);
 
         if (listenAddress) {
-            if (virSocketAddrIsNumeric(listenAddress)) {
+            if (virSocketAddrIsNumeric(listenAddress, NULL)) {
                 /* listenAddress is numeric IPv4 or IPv6 */
                 if (virSocketAddrParse(&listenAddressSocket, listenAddress, AF_UNSPEC) < 0)
                     goto cleanup;
@@ -2850,7 +2850,7 @@ qemuMigrationPrepareDirect(virQEMUDriverPtr driver,
             goto cleanup;
 
         if (migrateHost != NULL) {
-            if (virSocketAddrIsNumeric(migrateHost) &&
+            if (virSocketAddrIsNumeric(migrateHost, NULL) &&
                 virSocketAddrParse(NULL, migrateHost, AF_UNSPEC) < 0)
                 goto cleanup;
 
diff --git a/src/util/virsocketaddr.c b/src/util/virsocketaddr.c
index 7cc4bde..64409a6 100644
--- a/src/util/virsocketaddr.c
+++ b/src/util/virsocketaddr.c
@@ -858,6 +858,7 @@ virSocketAddrGetIpPrefix(const virSocketAddr *address,
 /**
  * virSocketAddrIsNumeric:
  * @address: address to check
+ * @family: where to store the address family, optional.
  *
  * Check if passed address is an IP address in numeric format. For
  * instance, for 0.0.0.0 true is returned, for 'examplehost"
@@ -867,15 +868,19 @@ virSocketAddrGetIpPrefix(const virSocketAddr *address,
  *          false otherwise
  */
 bool
-virSocketAddrIsNumeric(const char *address)
+virSocketAddrIsNumeric(const char *address, int *family)
 {
     struct addrinfo *res;
-    unsigned short family;
+    unsigned short sa_family;
 
     if (virSocketAddrParseInternal(&res, address, AF_UNSPEC, false) < 0)
         return false;
 
-    family = res->ai_addr->sa_family;
+    sa_family = res->ai_addr->sa_family;
     freeaddrinfo(res);
-    return family == AF_INET || family == AF_INET6;
+
+    if (family != NULL) {
+        *family = sa_family;
+    }
+    return sa_family == AF_INET || sa_family == AF_INET6;
 }
diff --git a/src/util/virsocketaddr.h b/src/util/virsocketaddr.h
index 27defa0..7b11afb 100644
--- a/src/util/virsocketaddr.h
+++ b/src/util/virsocketaddr.h
@@ -125,5 +125,5 @@ bool virSocketAddrIsPrivate(const virSocketAddr *addr);
 
 bool virSocketAddrIsWildcard(const virSocketAddr *addr);
 
-bool virSocketAddrIsNumeric(const char *address);
+bool virSocketAddrIsNumeric(const char *address, int *family);
 #endif /* __VIR_SOCKETADDR_H__ */
diff --git a/tests/sockettest.c b/tests/sockettest.c
index 68b0536..dde0bb8 100644
--- a/tests/sockettest.c
+++ b/tests/sockettest.c
@@ -229,7 +229,7 @@ testIsNumericHelper(const void *opaque)
 {
     const struct testIsNumericData *data = opaque;
 
-    if (virSocketAddrIsNumeric(data->addr))
+    if (virSocketAddrIsNumeric(data->addr, NULL))
         return data->pass ? 0 : -1;
     return data->pass ? -1 : 0;
 }
-- 
1.9.3


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