When creating a URL to pass to curl, if the server name is an IPv6
address, enclose it in '[...]', for example forming a URL like:

  https://[1234:56:0:789a:bcde:72ff:fe0a:7baa]:443/sdk

Fixes: https://issues.redhat.com/browse/RHEL-138300
Updates: commit 845210011a9ffd9d17e30c51cbc81ba67c5d3166
Reported-by: Ming Xie <[email protected]>
Signed-off-by: Richard W.M. Jones <[email protected]>
Reviewed-by: Michal Privoznik <[email protected]>
---
 src/esx/esx_driver.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

diff --git a/src/esx/esx_driver.c b/src/esx/esx_driver.c
index 8d7c58c88f..208077eeda 100644
--- a/src/esx/esx_driver.c
+++ b/src/esx/esx_driver.c
@@ -582,6 +582,19 @@ esxCapsInit(esxPrivate *priv)
     return NULL;
 }
 
+static bool
+esxServerIsIPv6(const char *server)
+{
+    size_t i;
+    const size_t n = strlen(server);
+
+    for (i = 0; i < n; ++i) {
+        if (!g_ascii_isxdigit(server[i]) && server[i] != ':')
+            return false;
+    }
+    return true;
+}
+
 static char *
 esxCreateURL(const char *transport,
              const char *server,
@@ -589,10 +602,13 @@ esxCreateURL(const char *transport,
              const char *path)
 {
     char *url;
+    const bool is_ipv6 = esxServerIsIPv6(server);
 
-    url = g_strdup_printf("%s://%s:%d/%s",
+    url = g_strdup_printf("%s://%s%s%s:%d/%s",
                           transport,
+                          is_ipv6 ? "[" : "",
                           server,
+                          is_ipv6 ? "]" : "",
                           port,
                           path);
     return url;
@@ -605,13 +621,17 @@ esxCreateURLBuffer(virBuffer *buffer,
                    int port,
                    const char *path)
 {
+    const bool is_ipv6 = esxServerIsIPv6(server);
+
     /* Same as above, but add it to a buffer because the calling code
      * will append query strings etc.
      */
     virBufferAsprintf(buffer,
-                      "%s://%s:%d/%s",
+                      "%s://%s%s%s:%d/%s",
                       transport,
+                      is_ipv6 ? "[" : "",
                       server,
+                      is_ipv6 ? "]" : "",
                       port,
                       path);
 }
-- 
2.52.0

Reply via email to