Add g_socket_connection_connect(), etc
[platform/upstream/glib.git] / gio / gsocketclient.c
index 7064fc8..88f2b36 100644 (file)
@@ -34,7 +34,6 @@
 #include <gio/gsocketconnection.h>
 #include <gio/gproxyaddressenumerator.h>
 #include <gio/gproxyaddress.h>
-#include <gio/gproxyconnection.h>
 #include <gio/gsimpleasyncresult.h>
 #include <gio/gcancellable.h>
 #include <gio/gioerror.h>
 #include <gio/gproxy.h>
 #include <gio/gsocketaddress.h>
 #include <gio/gtcpconnection.h>
+#include <gio/gtcpwrapperconnection.h>
+#include <gio/gtlscertificate.h>
+#include <gio/gtlsclientconnection.h>
+#include <gio/ginetaddress.h>
 #include "glibintl.h"
 
 
@@ -56,7 +59,7 @@
  * #GSocketClient is a high-level utility class for connecting to a
  * network host using a connection oriented socket type.
  *
- * You create a #GSocketClient object, set any options you want, then
+ * You create a #GSocketClient object, set any options you want, and then
  * call a sync or async connect operation, which returns a #GSocketConnection
  * subclass on success.
  *
@@ -79,6 +82,8 @@ enum
   PROP_LOCAL_ADDRESS,
   PROP_TIMEOUT,
   PROP_ENABLE_PROXY,
+  PROP_TLS,
+  PROP_TLS_VALIDATION_FLAGS
 };
 
 struct _GSocketClientPrivate
@@ -90,6 +95,8 @@ struct _GSocketClientPrivate
   guint timeout;
   gboolean enable_proxy;
   GHashTable *app_proxies;
+  gboolean tls;
+  GTlsCertificateFlags tls_validation_flags;
 };
 
 static GSocket *
@@ -132,7 +139,7 @@ create_socket (GSocketClient  *client,
   return socket;
 }
 
-gboolean
+static gboolean
 can_use_proxy (GSocketClient *client)
 {
   GSocketClientPrivate *priv = client->priv;
@@ -142,6 +149,40 @@ can_use_proxy (GSocketClient *client)
 }
 
 static void
+clarify_connect_error (GError             *error,
+                      GSocketConnectable *connectable,
+                      GSocketAddress     *address)
+{
+  const char *name;
+  char *tmp_name = NULL;
+
+  if (G_IS_PROXY_ADDRESS (address))
+    {
+      name = tmp_name = g_inet_address_to_string (g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address)));
+
+      g_prefix_error (&error, _("Could not connect to proxy server %s: "), name);
+    }
+  else
+    {
+      if (G_IS_NETWORK_ADDRESS (connectable))
+       name = g_network_address_get_hostname (G_NETWORK_ADDRESS (connectable));
+      else if (G_IS_NETWORK_SERVICE (connectable))
+       name = g_network_service_get_domain (G_NETWORK_SERVICE (connectable));
+      else if (G_IS_INET_SOCKET_ADDRESS (connectable))
+       name = tmp_name = g_inet_address_to_string (g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (connectable)));
+      else
+       name = NULL;
+
+      if (name)
+       g_prefix_error (&error, _("Could not connect to %s: "), name);
+      else
+       g_prefix_error (&error, _("Could not connect: "));
+    }
+
+  g_free (tmp_name);
+}
+
+static void
 g_socket_client_init (GSocketClient *client)
 {
   client->priv = G_TYPE_INSTANCE_GET_PRIVATE (client,
@@ -218,6 +259,14 @@ g_socket_client_get_property (GObject    *object,
        g_value_set_boolean (value, client->priv->enable_proxy);
        break;
 
+      case PROP_TLS:
+       g_value_set_boolean (value, g_socket_client_get_tls (client));
+       break;
+
+      case PROP_TLS_VALIDATION_FLAGS:
+       g_value_set_flags (value, g_socket_client_get_tls_validation_flags (client));
+       break;
+
       default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -257,6 +306,14 @@ g_socket_client_set_property (GObject      *object,
       g_socket_client_set_enable_proxy (client, g_value_get_boolean (value));
       break;
 
+    case PROP_TLS:
+      g_socket_client_set_tls (client, g_value_get_boolean (value));
+      break;
+
+    case PROP_TLS_VALIDATION_FLAGS:
+      g_socket_client_set_tls_validation_flags (client, g_value_get_flags (value));
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -401,7 +458,7 @@ g_socket_client_set_protocol (GSocketClient   *client,
  *
  * See g_socket_client_set_local_address() for details.
  *
- * Returns: (transfer none): a #GSocketAddres or %NULL. don't free
+ * Returns: (transfer none): a #GSocketAddress or %NULL. Do not free.
  *
  * Since: 2.22
  */
@@ -420,7 +477,7 @@ g_socket_client_get_local_address (GSocketClient *client)
  * The sockets created by this object will bound to the
  * specified address (if not %NULL) before connecting.
  *
- * This is useful if you want to ensure the the local
+ * This is useful if you want to ensure that the local
  * side of the connection is on a specific port, or on
  * a specific interface.
  *
@@ -525,6 +582,91 @@ g_socket_client_set_enable_proxy (GSocketClient *client,
   g_object_notify (G_OBJECT (client), "enable-proxy");
 }
 
+/**
+ * g_socket_client_get_tls:
+ * @client: a #GSocketClient.
+ *
+ * Gets whether @client creates TLS connections. See
+ * g_socket_client_set_tls() for details.
+ *
+ * Returns: whether @client uses TLS
+ *
+ * Since: 2.28
+ */
+gboolean
+g_socket_client_get_tls (GSocketClient *client)
+{
+  return client->priv->tls;
+}
+
+/**
+ * g_socket_client_set_tls:
+ * @client: a #GSocketClient.
+ * @tls: whether to use TLS
+ *
+ * Sets whether @client creates TLS (aka SSL) connections. If @tls is
+ * %TRUE, @client will wrap its connections in a #GTlsClientConnection
+ * and perform a TLS handshake when connecting.
+ *
+ * Note that since #GSocketClient must return a #GSocketConnection,
+ * but #GTlsClientConnection is not a #GSocketConnection, this
+ * actually wraps the resulting #GTlsClientConnection in a
+ * #GTcpWrapperConnection when returning it. You can use
+ * g_tcp_wrapper_connection_get_base_io_stream() on the return value
+ * to extract the #GTlsClientConnection.
+ *
+ * Since: 2.28
+ */
+void
+g_socket_client_set_tls (GSocketClient *client,
+                        gboolean       tls)
+{
+  tls = !!tls;
+  if (tls == client->priv->tls)
+    return;
+
+  client->priv->tls = tls;
+  g_object_notify (G_OBJECT (client), "tls");
+}
+
+/**
+ * g_socket_client_get_tls_validation_flags:
+ * @client: a #GSocketClient.
+ *
+ * Gets the TLS validation flags used creating TLS connections via
+ * @client.
+ *
+ * Returns: the TLS validation flags
+ *
+ * Since: 2.28
+ */
+GTlsCertificateFlags
+g_socket_client_get_tls_validation_flags (GSocketClient *client)
+{
+  return client->priv->tls_validation_flags;
+}
+
+/**
+ * g_socket_client_set_tls_validation_flags:
+ * @client: a #GSocketClient.
+ * @flags: the validation flags
+ *
+ * Sets the TLS validation flags used when creating TLS connections
+ * via @client. The default value is %G_TLS_CERTIFICATE_VALIDATE_ALL.
+ *
+ * Since: 2.28
+ */
+void
+g_socket_client_set_tls_validation_flags (GSocketClient        *client,
+                                         GTlsCertificateFlags  flags)
+{
+  if (client->priv->tls_validation_flags != flags)
+    {
+      client->priv->tls_validation_flags = flags;
+      g_object_notify (G_OBJECT (client), "tls-validation-flags");
+    }
+}
+
 static void
 g_socket_client_class_init (GSocketClientClass *class)
 {
@@ -593,16 +735,33 @@ g_socket_client_class_init (GSocketClientClass *class)
                                                          G_PARAM_READWRITE |
                                                          G_PARAM_STATIC_STRINGS));
 
+  g_object_class_install_property (gobject_class, PROP_TLS,
+                                  g_param_spec_boolean ("tls",
+                                                        P_("TLS"),
+                                                        P_("Whether to create TLS connections"),
+                                                        FALSE,
+                                                        G_PARAM_CONSTRUCT |
+                                                        G_PARAM_READWRITE |
+                                                        G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
+                                  g_param_spec_flags ("tls-validation-flags",
+                                                      P_("TLS validation flags"),
+                                                      P_("TLS validation flags to use"),
+                                                      G_TYPE_TLS_CERTIFICATE_FLAGS,
+                                                      G_TLS_CERTIFICATE_VALIDATE_ALL,
+                                                      G_PARAM_CONSTRUCT |
+                                                      G_PARAM_READWRITE |
+                                                      G_PARAM_STATIC_STRINGS));
 }
 
 /**
  * g_socket_client_connect:
  * @client: a #GSocketClient.
  * @connectable: a #GSocketConnectable specifying the remote address.
- * @cancellable: optional #GCancellable object, %NULL to ignore.
+ * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
  * @error: #GError for error reporting, or %NULL to ignore.
  *
- * Tries to resolve the @connectable and make a network connection to it..
+ * Tries to resolve the @connectable and make a network connection to it.
  *
  * Upon a successful connection, a new #GSocketConnection is constructed
  * and returned.  The caller owns this new object and must drop their
@@ -612,7 +771,7 @@ g_socket_client_class_init (GSocketClientClass *class)
  * the underlying socket that is used. For instance, for a TCP/IP connection
  * it will be a #GTcpConnection.
  *
- * The socket created will be the same family as the the address that the
+ * The socket created will be the same family as the address that the
  * @connectable resolves to, unless family is set with g_socket_client_set_family()
  * or indirectly via g_socket_client_set_local_address(). The socket type
  * defaults to %G_SOCKET_TYPE_STREAM but can be set with
@@ -631,7 +790,7 @@ g_socket_client_connect (GSocketClient       *client,
                         GCancellable        *cancellable,
                         GError             **error)
 {
-  GSocketConnection *connection = NULL;
+  GIOStream *connection = NULL;
   GSocketAddressEnumerator *enumerator = NULL;
   GError *last_error, *tmp_error;
 
@@ -645,6 +804,7 @@ g_socket_client_connect (GSocketClient       *client,
   while (connection == NULL)
     {
       GSocketAddress *address = NULL;
+      gboolean application_proxy = FALSE;
       GSocket *socket;
 
       if (g_cancellable_is_cancelled (cancellable))
@@ -679,12 +839,19 @@ g_socket_client_connect (GSocketClient       *client,
       g_clear_error (&last_error);
 
       socket = create_socket (client, address, &last_error);
-      if (socket != NULL)
+      if (socket == NULL)
        {
-         if (g_socket_connect (socket, address, cancellable, &last_error))
-           connection = g_socket_connection_factory_create_connection (socket);
+         g_object_unref (address);
+         continue;
+       }
 
-         g_object_unref (socket);
+      connection = (GIOStream *)g_socket_connection_factory_create_connection (socket);
+      if (!g_socket_connection_connect (G_SOCKET_CONNECTION (connection),
+                                       address, cancellable, &last_error))
+       {
+         clarify_connect_error (last_error, connectable, address);
+         g_object_unref (connection);
+         connection = NULL;
        }
 
       if (connection &&
@@ -715,31 +882,15 @@ g_socket_client_connect (GSocketClient       *client,
             }
           else if (proxy)
            {
-              GIOStream *io_stream;
-              GTcpConnection *old_connection = G_TCP_CONNECTION (connection);
-
-             io_stream = g_proxy_connect (proxy,
-                                          G_IO_STREAM (old_connection),
-                                          proxy_addr,
-                                          cancellable,
-                                          &last_error);
-
-              if (io_stream)
-                {
-                  if (G_IS_SOCKET_CONNECTION (io_stream))
-                    connection = G_SOCKET_CONNECTION (g_object_ref (io_stream));
-                  else
-                    connection = _g_proxy_connection_new (old_connection,
-                                                          io_stream);
-
-                  g_object_unref (io_stream);
-                }
-              else
-                {
-                  connection = NULL;
-                }
-
-              g_object_unref (old_connection);
+              GIOStream *proxy_connection;
+
+             proxy_connection = g_proxy_connect (proxy,
+                                                 connection,
+                                                 proxy_addr,
+                                                 cancellable,
+                                                 &last_error);
+             g_object_unref (connection);
+             connection = proxy_connection;
              g_object_unref (proxy);
            }
          else if (!g_hash_table_lookup_extended (client->priv->app_proxies,
@@ -751,13 +902,48 @@ g_socket_client_connect (GSocketClient       *client,
              g_object_unref (connection);
              connection = NULL;
            }
+         else
+           {
+             application_proxy = TRUE;
+           }
+       }
+
+      if (!application_proxy && connection && client->priv->tls)
+       {
+         GIOStream *tlsconn;
+
+         tlsconn = g_tls_client_connection_new (connection, connectable, &last_error);
+         g_object_unref (connection);
+         connection = tlsconn;
+
+         if (tlsconn)
+           {
+             g_tls_client_connection_set_validation_flags (G_TLS_CLIENT_CONNECTION (tlsconn),
+                                                            client->priv->tls_validation_flags);
+             if (!g_tls_connection_handshake (G_TLS_CONNECTION (tlsconn),
+                                              cancellable, &last_error))
+               {
+                 g_object_unref (tlsconn);
+                 connection = NULL;
+               }
+           }
+       }
+
+      if (connection && !G_IS_SOCKET_CONNECTION (connection))
+       {
+         GSocketConnection *wrapper_connection;
+
+         wrapper_connection = g_tcp_wrapper_connection_new (connection, socket);
+         g_object_unref (connection);
+         connection = (GIOStream *)wrapper_connection;
        }
 
+      g_object_unref (socket);
       g_object_unref (address);
     }
   g_object_unref (enumerator);
 
-  return connection;
+  return G_SOCKET_CONNECTION (connection);
 }
 
 /**
@@ -765,14 +951,14 @@ g_socket_client_connect (GSocketClient       *client,
  * @client: a #GSocketClient
  * @host_and_port: the name and optionally port of the host to connect to
  * @default_port: the default port to connect to
- * @cancellable: a #GCancellable, or %NULL
+ * @cancellable: (allow-none): a #GCancellable, or %NULL
  * @error: a pointer to a #GError, or %NULL
  *
  * This is a helper function for g_socket_client_connect().
  *
  * Attempts to create a TCP connection to the named host.
  *
- * @host_and_port may be in any of a number of recognised formats; an IPv6
+ * @host_and_port may be in any of a number of recognized formats; an IPv6
  * address, an IPv4 address, or a domain name (in which case a DNS
  * lookup is performed).  Quoting with [] is supported for all address
  * types.  A port override may be specified in the usual way with a
@@ -783,7 +969,7 @@ g_socket_client_connect (GSocketClient       *client,
  * used as the port number to connect to.
  *
  * In general, @host_and_port is expected to be provided by the user (allowing
- * them to give the hostname, and a port overide if necessary) and
+ * them to give the hostname, and a port override if necessary) and
  * @default_port is expected to be provided by the application.
  *
  * In the case that an IP address is given, a single connection
@@ -829,9 +1015,8 @@ g_socket_client_connect_to_host (GSocketClient  *client,
  * @client: a #GSocketConnection
  * @domain: a domain name
  * @service: the name of the service to connect to
- * @cancellable: a #GCancellable, or %NULL
+ * @cancellable: (allow-none): a #GCancellable, or %NULL
  * @error: a pointer to a #GError, or %NULL
- * @returns: (transfer full): a #GSocketConnection if successful, or %NULL on error
  *
  * Attempts to create a TCP connection to a service.
  *
@@ -847,6 +1032,8 @@ g_socket_client_connect_to_host (GSocketClient  *client,
  * In the event of any failure (DNS error, service not found, no hosts
  * connectable) %NULL is returned and @error (if non-%NULL) is set
  * accordingly.
+ *
+ * Returns: (transfer full): a #GSocketConnection if successful, or %NULL on error
  */
 GSocketConnection *
 g_socket_client_connect_to_service (GSocketClient  *client,
@@ -871,7 +1058,7 @@ g_socket_client_connect_to_service (GSocketClient  *client,
  * @client: a #GSocketClient
  * @uri: A network URI
  * @default_port: the default port to connect to
- * @cancellable: a #GCancellable, or %NULL
+ * @cancellable: (allow-none): a #GCancellable, or %NULL
  * @error: a pointer to a #GError, or %NULL
  *
  * This is a helper function for g_socket_client_connect().
@@ -880,7 +1067,9 @@ g_socket_client_connect_to_service (GSocketClient  *client,
  *
  * @uri may be any valid URI containing an "authority" (hostname/port)
  * component. If a port is not specified in the URI, @default_port
- * will be used.
+ * will be used. TLS will be negotiated if #GSocketClient:tls is %TRUE.
+ * (#GSocketClient does not know to automatically assume TLS for
+ * certain URI schemes.)
  *
  * Using this rather than g_socket_client_connect() or
  * g_socket_client_connect_to_host() allows #GSocketClient to
@@ -925,10 +1114,12 @@ typedef struct
   GCancellable *cancellable;
   GSocketClient *client;
 
+  GSocketConnectable *connectable;
   GSocketAddressEnumerator *enumerator;
   GProxyAddress *proxy_addr;
+  GSocketAddress *current_addr;
   GSocket *current_socket;
-  GSocketConnection *connection;
+  GIOStream *connection;
 
   GError *last_error;
 } GSocketClientAsyncConnectData;
@@ -938,13 +1129,22 @@ g_socket_client_async_connect_complete (GSocketClientAsyncConnectData *data)
 {
   if (data->last_error)
     {
-      g_simple_async_result_set_from_error (data->result, data->last_error);
-      g_error_free (data->last_error);
+      g_simple_async_result_take_error (data->result, data->last_error);
     }
   else
     {
       g_assert (data->connection);
 
+      if (!G_IS_SOCKET_CONNECTION (data->connection))
+       {
+         GSocketConnection *wrapper_connection;
+
+         wrapper_connection = g_tcp_wrapper_connection_new (data->connection,
+                                                            data->current_socket);
+         g_object_unref (data->connection);
+         data->connection = (GIOStream *)wrapper_connection;
+       }
+
       g_simple_async_result_set_op_res_gpointer (data->result,
                                                 data->connection,
                                                 g_object_unref);
@@ -952,9 +1152,12 @@ g_socket_client_async_connect_complete (GSocketClientAsyncConnectData *data)
 
   g_simple_async_result_complete (data->result);
   g_object_unref (data->result);
+  g_object_unref (data->connectable);
   g_object_unref (data->enumerator);
   if (data->cancellable)
     g_object_unref (data->cancellable);
+  if (data->current_addr)
+    g_object_unref (data->current_addr);
   if (data->current_socket)
     g_object_unref (data->current_socket);
   if (data->proxy_addr)
@@ -979,6 +1182,12 @@ set_last_error (GSocketClientAsyncConnectData *data,
 static void
 enumerator_next_async (GSocketClientAsyncConnectData *data)
 {
+  /* We need to cleanup the state */
+  g_clear_object (&data->current_socket);
+  g_clear_object (&data->current_addr);
+  g_clear_object (&data->proxy_addr);
+  g_clear_object (&data->connection);
+
   g_socket_address_enumerator_next_async (data->enumerator,
                                          data->cancellable,
                                          g_socket_client_enumerator_callback,
@@ -986,46 +1195,113 @@ enumerator_next_async (GSocketClientAsyncConnectData *data)
 }
 
 static void
-g_socket_client_proxy_connect_callback (GObject      *object,
+g_socket_client_tls_handshake_callback (GObject      *object,
                                        GAsyncResult *result,
                                        gpointer      user_data)
 {
   GSocketClientAsyncConnectData *data = user_data;
-  GIOStream *io_stream;
-  GTcpConnection *old_connection = G_TCP_CONNECTION (data->connection);
 
-  io_stream = g_proxy_connect_finish (G_PROXY (object),
-                                     result,
-                                     &data->last_error);
+  if (g_tls_connection_handshake_finish (G_TLS_CONNECTION (object),
+                                        result,
+                                        &data->last_error))
+    {
+      g_object_unref (data->connection);
+      data->connection = G_IO_STREAM (object);
+
+      g_socket_client_async_connect_complete (data);
+    }
+  else
+    {
+      g_object_unref (object);
+      enumerator_next_async (data);
+    }
+}
+
+static void
+g_socket_client_tls_handshake (GSocketClientAsyncConnectData *data)
+{
+  GIOStream *tlsconn;
 
-  if (io_stream)
+  if (!data->client->priv->tls)
     {
-      if (G_IS_SOCKET_CONNECTION (io_stream))
-        data->connection = G_SOCKET_CONNECTION (g_object_ref (io_stream));
-      else
-        data->connection = _g_proxy_connection_new (old_connection,
-                                                    io_stream);
-      g_object_unref (io_stream);
+      g_socket_client_async_connect_complete (data);
+      return;
+    }
+
+  tlsconn = g_tls_client_connection_new (data->connection,
+                                        data->connectable,
+                                        &data->last_error);
+  if (tlsconn)
+    {
+      g_tls_client_connection_set_validation_flags (G_TLS_CLIENT_CONNECTION (tlsconn),
+                                                    data->client->priv->tls_validation_flags);
+      g_tls_connection_handshake_async (G_TLS_CONNECTION (tlsconn),
+                                       G_PRIORITY_DEFAULT,
+                                       data->cancellable,
+                                       g_socket_client_tls_handshake_callback,
+                                       data);
     }
   else
     {
-      data->connection = NULL;
+      enumerator_next_async (data);
     }
+}
 
-  g_object_unref (old_connection);
+static void
+g_socket_client_proxy_connect_callback (GObject      *object,
+                                       GAsyncResult *result,
+                                       gpointer      user_data)
+{
+  GSocketClientAsyncConnectData *data = user_data;
+
+  g_object_unref (data->connection);
+  data->connection = g_proxy_connect_finish (G_PROXY (object),
+                                            result,
+                                            &data->last_error);
+  if (!data->connection)
+    {
+      enumerator_next_async (data);
+      return;
+    }
 
-  g_socket_client_async_connect_complete (data);
+  g_socket_client_tls_handshake (data);
 }
 
 static void
-g_socket_client_proxy_connect (GSocketClientAsyncConnectData *data)
+g_socket_client_connected_callback (GObject      *source,
+                                   GAsyncResult *result,
+                                   gpointer      user_data)
 {
+  GSocketClientAsyncConnectData *data = user_data;
+  GError *error = NULL;
   GProxy *proxy;
-  const gchar *protocol = g_proxy_address_get_protocol (data->proxy_addr);
+  const gchar *protocol;
+
+  if (!g_socket_connection_connect_finish (G_SOCKET_CONNECTION (source),
+                                          result, &error))
+    {
+      clarify_connect_error (error, data->connectable,
+                            data->current_addr);
+      set_last_error (data, error);
+
+      /* try next one */
+      enumerator_next_async (data);
+      return;
+    }
+
+  /* wrong, but backward compatible */
+  g_socket_set_blocking (data->current_socket, TRUE);
+
+  if (!data->proxy_addr)
+    {
+      g_socket_client_tls_handshake (data);
+      return;
+    }
 
+  protocol  = g_proxy_address_get_protocol (data->proxy_addr);
   proxy = g_proxy_get_default_for_protocol (protocol);
 
-  /* The connection should not be anything else then TCP Connection,
+  /* The connection should not be anything other than TCP,
    * but let's put a safety guard in case
    */
   if (!G_IS_TCP_CONNECTION (data->connection))
@@ -1037,15 +1313,12 @@ g_socket_client_proxy_connect (GSocketClientAsyncConnectData *data)
           G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
           _("Trying to proxy over non-TCP connection is not supported."));
 
-      g_object_unref (data->connection);
-      data->connection = NULL;
-
       enumerator_next_async (data);
     }
   else if (proxy)
     {
       g_proxy_connect_async (proxy,
-                             G_IO_STREAM (data->connection),
+                             data->connection,
                              data->proxy_addr,
                              data->cancellable,
                              g_socket_client_proxy_connect_callback,
@@ -1061,66 +1334,14 @@ g_socket_client_proxy_connect (GSocketClientAsyncConnectData *data)
           _("Proxy protocol '%s' is not supported."),
           protocol);
 
-      g_object_unref (data->connection);
-      data->connection = NULL;
-
       enumerator_next_async (data);
     }
-}
-
-static void
-g_socket_client_socket_connected (GSocketClientAsyncConnectData *data)
-{
-  g_socket_set_blocking (data->current_socket, TRUE);
-
-  data->connection =
-    g_socket_connection_factory_create_connection (data->current_socket);
-  g_object_unref (data->current_socket);
-  data->current_socket = NULL;
-
-  if (data->proxy_addr)
-    g_socket_client_proxy_connect (data);
   else
-    g_socket_client_async_connect_complete (data);
-}
-
-static gboolean
-g_socket_client_socket_callback (GSocket *socket,
-                                GIOCondition condition,
-                                GSocketClientAsyncConnectData *data)
-{
-  GError *error = NULL;
-
-  if (g_cancellable_is_cancelled (data->cancellable))
     {
-      /* Cancelled, return done with last error being cancelled */
-      g_clear_error (&data->last_error);
-      g_object_unref (data->current_socket);
-      data->current_socket = NULL;
-      g_cancellable_set_error_if_cancelled (data->cancellable,
-                                           &data->last_error);
-
+      /* Simply complete the connection, we don't want to do TLS handshake
+       * as the application proxy handling may need proxy handshake first */
       g_socket_client_async_connect_complete (data);
-      return FALSE;
-    }
-  else
-    {
-      /* socket is ready for writing means connect done, did it succeed? */
-      if (!g_socket_check_connect_result (data->current_socket, &error))
-       {
-         set_last_error (data, error);
-         g_object_unref (data->current_socket);
-         data->current_socket = NULL;
-
-         /* try next one */
-         enumerator_next_async (data);
-
-         return FALSE;
-       }
     }
-
-  g_socket_client_socket_connected (data);
-  return FALSE;
 }
 
 static void
@@ -1163,53 +1384,29 @@ g_socket_client_enumerator_callback (GObject      *object,
   g_clear_error (&data->last_error);
 
   socket = create_socket (data->client, address, &data->last_error);
-  if (socket != NULL)
+  if (socket == NULL)
     {
-      g_socket_set_blocking (socket, FALSE);
-      if (g_socket_connect (socket, address, data->cancellable, &tmp_error))
-       {
-         data->current_socket = socket;
-         g_socket_client_socket_connected (data);
-
-         g_object_unref (address);
-         return;
-       }
-      else if (g_error_matches (tmp_error, G_IO_ERROR, G_IO_ERROR_PENDING))
-       {
-         GSource *source;
-
-         data->current_socket = socket;
-         g_error_free (tmp_error);
-
-         source = g_socket_create_source (socket, G_IO_OUT,
-                                          data->cancellable);
-         g_source_set_callback (source,
-                                (GSourceFunc) g_socket_client_socket_callback,
-                                data, NULL);
-         g_source_attach (source, g_main_context_get_thread_default ());
-         g_source_unref (source);
-
-         g_object_unref (address);
-         return;
-       }
-      else
-       {
-         data->last_error = tmp_error;
-         g_object_unref (socket);
-       }
+      g_object_unref (address);
+      enumerator_next_async (data);
+      return;
     }
 
-  g_object_unref (address);
-  enumerator_next_async (data);
+  data->current_socket = socket;
+  data->current_addr = address;
+  data->connection = (GIOStream *) g_socket_connection_factory_create_connection (socket);
+
+  g_socket_connection_connect_async (G_SOCKET_CONNECTION (data->connection),
+                                    address, data->cancellable,
+                                    g_socket_client_connected_callback, data);
 }
 
 /**
  * g_socket_client_connect_async:
- * @client: a #GTcpClient
+ * @client: a #GSocketClient
  * @connectable: a #GSocketConnectable specifying the remote address.
- * @cancellable: a #GCancellable, or %NULL
- * @callback: a #GAsyncReadyCallback
- * @user_data: user data for the callback
+ * @cancellable: (allow-none): a #GCancellable, or %NULL
+ * @callback: (scope async): a #GAsyncReadyCallback
+ * @user_data: (closure): user data for the callback
  *
  * This is the asynchronous version of g_socket_client_connect().
  *
@@ -1238,6 +1435,10 @@ g_socket_client_connect_async (GSocketClient       *client,
   data->client = client;
   if (cancellable)
     data->cancellable = g_object_ref (cancellable);
+  else
+    data->cancellable = NULL;
+  data->last_error = NULL;
+  data->connectable = g_object_ref (connectable);
 
   if (can_use_proxy (client))
       data->enumerator = g_socket_connectable_proxy_enumerate (connectable);
@@ -1249,12 +1450,12 @@ g_socket_client_connect_async (GSocketClient       *client,
 
 /**
  * g_socket_client_connect_to_host_async:
- * @client: a #GTcpClient
+ * @client: a #GSocketClient
  * @host_and_port: the name and optionally the port of the host to connect to
  * @default_port: the default port to connect to
- * @cancellable: a #GCancellable, or %NULL
- * @callback: a #GAsyncReadyCallback
- * @user_data: user data for the callback
+ * @cancellable: (allow-none): a #GCancellable, or %NULL
+ * @callback: (scope async): a #GAsyncReadyCallback
+ * @user_data: (closure): user data for the callback
  *
  * This is the asynchronous version of g_socket_client_connect_to_host().
  *
@@ -1280,9 +1481,8 @@ g_socket_client_connect_to_host_async (GSocketClient        *client,
                                         &error);
   if (connectable == NULL)
     {
-      g_simple_async_report_gerror_in_idle (G_OBJECT (client),
+      g_simple_async_report_take_gerror_in_idle (G_OBJECT (client),
                                            callback, user_data, error);
-      g_error_free (error);
     }
   else
     {
@@ -1298,9 +1498,9 @@ g_socket_client_connect_to_host_async (GSocketClient        *client,
  * @client: a #GSocketClient
  * @domain: a domain name
  * @service: the name of the service to connect to
- * @cancellable: a #GCancellable, or %NULL
- * @callback: a #GAsyncReadyCallback
- * @user_data: user data for the callback
+ * @cancellable: (allow-none): a #GCancellable, or %NULL
+ * @callback: (scope async): a #GAsyncReadyCallback
+ * @user_data: (closure): user data for the callback
  *
  * This is the asynchronous version of
  * g_socket_client_connect_to_service().
@@ -1329,9 +1529,9 @@ g_socket_client_connect_to_service_async (GSocketClient       *client,
  * @client: a #GSocketClient
  * @uri: a network uri
  * @default_port: the default port to connect to
- * @cancellable: a #GCancellable, or %NULL
- * @callback: a #GAsyncReadyCallback
- * @user_data: user data for the callback
+ * @cancellable: (allow-none): a #GCancellable, or %NULL
+ * @callback: (scope async): a #GAsyncReadyCallback
+ * @user_data: (closure): user data for the callback
  *
  * This is the asynchronous version of g_socket_client_connect_to_uri().
  *
@@ -1356,9 +1556,8 @@ g_socket_client_connect_to_uri_async (GSocketClient        *client,
   connectable = g_network_address_parse_uri (uri, default_port, &error);
   if (connectable == NULL)
     {
-      g_simple_async_report_gerror_in_idle (G_OBJECT (client),
+      g_simple_async_report_take_gerror_in_idle (G_OBJECT (client),
                                            callback, user_data, error);
-      g_error_free (error);
     }
   else
     {
@@ -1374,7 +1573,7 @@ g_socket_client_connect_to_uri_async (GSocketClient        *client,
  * g_socket_client_connect_finish:
  * @client: a #GSocketClient.
  * @result: a #GAsyncResult.
- * @error: a #GError location to store the error occuring, or %NULL to
+ * @error: a #GError location to store the error occurring, or %NULL to
  * ignore.
  *
  * Finishes an async connect operation. See g_socket_client_connect_async()
@@ -1400,7 +1599,7 @@ g_socket_client_connect_finish (GSocketClient  *client,
  * g_socket_client_connect_to_host_finish:
  * @client: a #GSocketClient.
  * @result: a #GAsyncResult.
- * @error: a #GError location to store the error occuring, or %NULL to
+ * @error: a #GError location to store the error occurring, or %NULL to
  * ignore.
  *
  * Finishes an async connect operation. See g_socket_client_connect_to_host_async()
@@ -1421,7 +1620,7 @@ g_socket_client_connect_to_host_finish (GSocketClient  *client,
  * g_socket_client_connect_to_service_finish:
  * @client: a #GSocketClient.
  * @result: a #GAsyncResult.
- * @error: a #GError location to store the error occuring, or %NULL to
+ * @error: a #GError location to store the error occurring, or %NULL to
  * ignore.
  *
  * Finishes an async connect operation. See g_socket_client_connect_to_service_async()
@@ -1442,7 +1641,7 @@ g_socket_client_connect_to_service_finish (GSocketClient  *client,
  * g_socket_client_connect_to_uri_finish:
  * @client: a #GSocketClient.
  * @result: a #GAsyncResult.
- * @error: a #GError location to store the error occuring, or %NULL to
+ * @error: a #GError location to store the error occurring, or %NULL to
  * ignore.
  *
  * Finishes an async connect operation. See g_socket_client_connect_to_uri_async()
@@ -1467,7 +1666,7 @@ g_socket_client_connect_to_uri_finish (GSocketClient  *client,
  * Enable proxy protocols to be handled by the application. When the
  * indicated proxy protocol is returned by the #GProxyResolver,
  * #GSocketClient will consider this protocol as supported but will
- * not try find a #GProxy instance to handle handshaking. The
+ * not try to find a #GProxy instance to handle handshaking. The
  * application must check for this case by calling
  * g_socket_connection_get_remote_address() on the returned
  * #GSocketConnection, and seeing if it's a #GProxyAddress of the
@@ -1479,6 +1678,10 @@ g_socket_client_connect_to_uri_finish (GSocketClient  *client,
  * proxy protocols that are reused between protocols. A good example
  * is HTTP. It can be used to proxy HTTP, FTP and Gopher and can also
  * be use as generic socket proxy through the HTTP CONNECT method.
+ *
+ * When the proxy is detected as being an application proxy, TLS handshake
+ * will be skipped. This is required to let the application do the proxy
+ * specific handshake.
  */
 void
 g_socket_client_add_application_proxy (GSocketClient *client,