From 3012a34361e6ca61e897d9e74079752b00c28ede Mon Sep 17 00:00:00 2001
From: Xavier Claessens <xavier.claessens@collabora.co.uk>
Date: Mon, 18 Jun 2012 15:49:21 +0000
Subject: WockyConnector: Fix problem if host is an ipv6 or contains a port

Make WockyConnector::connect_to_host_async() uses
g_socket_client_connect_to_host_async()'s semantic. It means that the
port argument is used only if the host does not already contains
a port.
---
Index: telepathy-gabble-0.16.0/lib/ext/wocky/wocky/wocky-connector.c
===================================================================
--- telepathy-gabble-0.16.0.orig/lib/ext/wocky/wocky/wocky-connector.c	2012-02-29 21:04:41.000000000 +0100
+++ telepathy-gabble-0.16.0/lib/ext/wocky/wocky/wocky-connector.c	2012-07-09 17:25:42.092400105 +0200
@@ -807,22 +807,40 @@
 
 static void
 connect_to_host_async (WockyConnector *connector,
-    const gchar *host,
-    guint port)
+    const gchar *host_and_port,
+    guint default_port)
 {
   WockyConnectorPrivate *priv = connector->priv;
 
 #if HAVE_GIO_PROXY
-  /* Legacy SSL mode is just like doing HTTPS, so let's trigger HTTPS
-   * proxy setting if any */
-  gchar *uri = g_strdup_printf ("%s://%s:%i",
-      priv->legacy_ssl ? "https" : "xmpp-client", host, port);
-  g_socket_client_connect_to_uri_async (priv->client,
-      uri, port, NULL, tcp_host_connected, connector);
-  g_free (uri);
+  {
+    const gchar *uri_format = "%s://%s";
+    gchar *uri;
+
+    /* If host_and_port is an ipv6 address we must ensure it has [] around it */
+    if (host_and_port[0] != '[')
+      {
+        const gchar *p;
+
+        /* if host_and_port contains 2 ':' chars, it must be an ipv6 address */
+        p = g_strstr_len (host_and_port, -1, ":");
+        if (p != NULL)
+          p = g_strstr_len (p + 1, -1, ":");
+        if (p != NULL)
+          uri_format = "%s://[%s]";
+      }
+
+    /* Legacy SSL mode is just like doing HTTPS, so let's trigger HTTPS
+     * proxy setting if any */
+    uri = g_strdup_printf (uri_format,
+        priv->legacy_ssl ? "https" : "xmpp-client", host_and_port);
+    g_socket_client_connect_to_uri_async (priv->client,
+        uri, default_port, NULL, tcp_host_connected, connector);
+    g_free (uri);
+  }
 #else
   g_socket_client_connect_to_host_async (priv->client,
-      host, port, NULL, tcp_host_connected, connector);
+      host_and_port, default_port, NULL, tcp_host_connected, connector);
 #endif
 }
 
