[Spice-devel] [PATCH v2 1/3] proxy: support ipv6 addresses in brackets

Wolfgang Bumiller w.bumiller at proxmox.com
Mon Oct 5 07:16:41 PDT 2015


Signed-off-by: Wolfgang Bumiller <w.bumiller at proxmox.com>
---
 src/spice-uri.c | 36 +++++++++++++++++++++++++++++-------
 1 file changed, 29 insertions(+), 7 deletions(-)

diff --git a/src/spice-uri.c b/src/spice-uri.c
index 82aefdb..c0d2d9c 100644
--- a/src/spice-uri.c
+++ b/src/spice-uri.c
@@ -114,11 +114,24 @@ gboolean spice_uri_parse(SpiceURI *self, const gchar *_uri, GError **error)
         uri = next;
     }
 
-    /* max 2 parts, host:port */
-    gchar **uriv = g_strsplit(uri, ":", 2);
+    /* ipv6 support */
+    gchar **uriv = NULL;
     const gchar *uri_port = NULL;
+    if (uri[0] == '[') {
+        gchar *end = strchr(uri+1, ']');
+        if (end) {
+            *end = 0;
+            uriv = g_malloc(sizeof(gchar*)*3);
+            uriv[0] = g_strdup(uri+1);
+            uriv[1] = (end[1] == ':') ? g_strdup(end+2) : NULL;
+            uriv[2] = NULL;
+        }
+    } else {
+        /* max 2 parts, host:port */
+        uriv = g_strsplit(uri, ":", 2);
+    }
 
-    if (uriv[0] == NULL || strlen(uriv[0]) == 0) {
+    if (!uriv || uriv[0] == NULL || strlen(uriv[0]) == 0) {
         g_set_error(error, SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,
                     "Invalid hostname in uri address");
         goto end;
@@ -387,14 +400,23 @@ gchar* spice_uri_to_string(SpiceURI* self)
     if (self->scheme == NULL || self->hostname == NULL)
         return NULL;
 
+    gchar *brackets[2] = { "", "" };
+    if (strchr(self->hostname, ':')) {
+        brackets[0] = "[";
+        brackets[1] = "]";
+    }
+
     if (self->user || self->password)
-        return g_strdup_printf("%s://%s:%s@%s:%u",
+        return g_strdup_printf("%s://%s%s%s:%s@%s:%u",
                                self->scheme,
                                self->user, self->password,
-                               self->hostname, self->port);
+                               brackets[0], self->hostname, brackets[1],
+                               self->port);
     else
-        return g_strdup_printf("%s://%s:%u",
-                               self->scheme, self->hostname, self->port);
+        return g_strdup_printf("%s://%s%s%s:%u",
+                               self->scheme,
+                               brackets[0], self->hostname, brackets[1],
+                               self->port);
 }
 
 /**
-- 
2.1.4




More information about the Spice-devel mailing list