[libvirt] [PATCH v3 1/3] migration: add migration_host support for Ipv6 address without brackets

Chen Fan chen.fan.fnst at cn.fujitsu.com
Tue Oct 7 04:07:30 UTC 2014


if specifying migration_host to an Ipv6 address without brackets,
it was resolved to an incorrect address, such as:
    tcp:2001:0DB8::1428:4444,
but the correct address should be:
    tcp:[2001:0DB8::1428]:4444
so we should add brackets when parsing it.

Signed-off-by: Chen Fan <chen.fan.fnst at cn.fujitsu.com>
---
 src/libvirt_private.syms  |  2 +-
 src/qemu/qemu_migration.c | 49 +++++++++++++++++++++++------------------------
 src/util/virsocketaddr.c  | 19 +++++++++---------
 src/util/virsocketaddr.h  |  2 +-
 tests/sockettest.c        |  2 +-
 5 files changed, 36 insertions(+), 38 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 7cbc35b..8ab1394 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1911,11 +1911,11 @@ virSocketAddrGetIpPrefix;
 virSocketAddrGetPort;
 virSocketAddrGetRange;
 virSocketAddrIsNetmask;
-virSocketAddrIsNumeric;
 virSocketAddrIsPrivate;
 virSocketAddrIsWildcard;
 virSocketAddrMask;
 virSocketAddrMaskByPrefix;
+virSocketAddrNumericFamily;
 virSocketAddrParse;
 virSocketAddrParseIPv4;
 virSocketAddrParseIPv6;
diff --git a/src/qemu/qemu_migration.c b/src/qemu/qemu_migration.c
index 284cd5a..e135249 100644
--- a/src/qemu/qemu_migration.c
+++ b/src/qemu/qemu_migration.c
@@ -2605,7 +2605,6 @@ qemuMigrationPrepareAny(virQEMUDriverPtr driver,
         if (VIR_STRDUP(migrateFrom, "stdio") < 0)
             goto cleanup;
     } else {
-        virSocketAddr listenAddressSocket;
         bool encloseAddress = false;
         bool hostIPv6Capable = false;
         bool qemuIPv6Capable = false;
@@ -2627,28 +2626,21 @@ qemuMigrationPrepareAny(virQEMUDriverPtr driver,
         virObjectUnref(qemuCaps);
 
         if (listenAddress) {
-            if (virSocketAddrIsNumeric(listenAddress)) {
-                /* listenAddress is numeric IPv4 or IPv6 */
-                if (virSocketAddrParse(&listenAddressSocket, listenAddress, AF_UNSPEC) < 0)
+            if (virSocketAddrNumericFamily(listenAddress) == AF_INET6) {
+                if (!qemuIPv6Capable) {
+                    virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
+                                   _("qemu isn't capable of IPv6"));
                     goto cleanup;
-
-                /* address parsed successfully */
-                if (VIR_SOCKET_ADDR_IS_FAMILY(&listenAddressSocket, AF_INET6)) {
-                    if (!qemuIPv6Capable) {
-                        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
-                                       _("qemu isn't capable of IPv6"));
-                        goto cleanup;
-                    }
-                    if (!hostIPv6Capable) {
-                        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
-                                       _("host isn't capable of IPv6"));
-                        goto cleanup;
-                    }
-                    /* IPv6 address must be escaped in brackets on the cmd line */
-                    encloseAddress = true;
                 }
+                if (!hostIPv6Capable) {
+                    virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
+                                   _("host isn't capable of IPv6"));
+                    goto cleanup;
+                }
+                /* IPv6 address must be escaped in brackets on the cmd line */
+                encloseAddress = true;
             } else {
-                /* listenAddress is a hostname */
+                /* listenAddress is a hostname or IPv4 */
             }
         } else if (qemuIPv6Capable && hostIPv6Capable) {
             /* Listen on :: instead of 0.0.0.0 if QEMU understands it
@@ -2950,15 +2942,17 @@ qemuMigrationPrepareDirect(virQEMUDriverPtr driver,
      * to be a correct hostname which refers to the target machine).
      */
     if (uri_in == NULL) {
+        bool encloseAddress = false;
+        const char *incFormat;
+
         if (virPortAllocatorAcquire(driver->migrationPorts, &port) < 0)
             goto cleanup;
 
         if (migrateHost != NULL) {
-            if (virSocketAddrIsNumeric(migrateHost) &&
-                virSocketAddrParse(NULL, migrateHost, AF_UNSPEC) < 0)
-                goto cleanup;
+            if (virSocketAddrNumericFamily(migrateHost) == AF_INET6)
+                encloseAddress = true;
 
-           if (VIR_STRDUP(hostname, migrateHost) < 0)
+            if (VIR_STRDUP(hostname, migrateHost) < 0)
                 goto cleanup;
         } else {
             if ((hostname = virGetHostname()) == NULL)
@@ -2977,7 +2971,12 @@ qemuMigrationPrepareDirect(virQEMUDriverPtr driver,
          * compatibility with old targets. We at least make the
          * new targets accept both syntaxes though.
          */
-        if (virAsprintf(uri_out, "tcp:%s:%d", hostname, port) < 0)
+        if (encloseAddress)
+            incFormat = "%s:[%s]:%d";
+        else
+            incFormat = "%s:%s:%d";
+
+        if (virAsprintf(uri_out, incFormat, "tcp", hostname, port) < 0)
             goto cleanup;
     } else {
         bool well_formed_uri;
diff --git a/src/util/virsocketaddr.c b/src/util/virsocketaddr.c
index 7cc4bde..7fe7a15 100644
--- a/src/util/virsocketaddr.c
+++ b/src/util/virsocketaddr.c
@@ -856,26 +856,25 @@ virSocketAddrGetIpPrefix(const virSocketAddr *address,
 }
 
 /**
- * virSocketAddrIsNumeric:
+ * virSocketAddrNumericFamily:
  * @address: address to check
  *
- * Check if passed address is an IP address in numeric format. For
- * instance, for 0.0.0.0 true is returned, for 'examplehost"
- * false is returned.
+ * Check if passed address is an IP address in numeric format. and
+ * return the address family, otherwise return 0.
  *
- * Returns: true if @address is an IP address,
- *          false otherwise
+ * Returns: AF_INET or AF_INET6 if @address is an numeric IP address,
+ *          -1 otherwise.
  */
-bool
-virSocketAddrIsNumeric(const char *address)
+int
+virSocketAddrNumericFamily(const char *address)
 {
     struct addrinfo *res;
     unsigned short family;
 
     if (virSocketAddrParseInternal(&res, address, AF_UNSPEC, false) < 0)
-        return false;
+        return -1;
 
     family = res->ai_addr->sa_family;
     freeaddrinfo(res);
-    return family == AF_INET || family == AF_INET6;
+    return family;
 }
diff --git a/src/util/virsocketaddr.h b/src/util/virsocketaddr.h
index 27defa0..35c9c1a 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);
+int virSocketAddrNumericFamily(const char *address);
 #endif /* __VIR_SOCKETADDR_H__ */
diff --git a/tests/sockettest.c b/tests/sockettest.c
index 68b0536..abf7350 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 (virSocketAddrNumericFamily(data->addr) != -1)
         return data->pass ? 0 : -1;
     return data->pass ? -1 : 0;
 }
-- 
1.9.3




More information about the libvir-list mailing list