Use g_simple_async_result_{new_,}take_error
[platform/upstream/glib.git] / gio / gsocketclient.c
index 944b73c..b2cbcee 100644 (file)
 #include "gsocketclient.h"
 
 #include <stdlib.h>
+#include <string.h>
 
 #include <gio/gioenumtypes.h>
 #include <gio/gsocketaddressenumerator.h>
 #include <gio/gsocketconnectable.h>
 #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/gsocket.h>
 #include <gio/gnetworkaddress.h>
+#include <gio/gnetworkservice.h>
+#include <gio/gproxy.h>
 #include <gio/gsocketaddress.h>
+#include <gio/gtcpconnection.h>
 #include "glibintl.h"
 
-#include "gioalias.h"
 
 /**
  * SECTION:gsocketclient
@@ -59,7 +65,7 @@
  * it will be a #GTcpConnection.
  *
  * Since: 2.22
- **/
+ */
 
 
 G_DEFINE_TYPE (GSocketClient, g_socket_client, G_TYPE_OBJECT);
@@ -70,7 +76,9 @@ enum
   PROP_FAMILY,
   PROP_TYPE,
   PROP_PROTOCOL,
-  PROP_LOCAL_ADDRESS
+  PROP_LOCAL_ADDRESS,
+  PROP_TIMEOUT,
+  PROP_ENABLE_PROXY,
 };
 
 struct _GSocketClientPrivate
@@ -79,6 +87,9 @@ struct _GSocketClientPrivate
   GSocketType type;
   GSocketProtocol protocol;
   GSocketAddress *local_address;
+  guint timeout;
+  gboolean enable_proxy;
+  GHashTable *app_proxies;
 };
 
 static GSocket *
@@ -115,9 +126,21 @@ create_socket (GSocketClient  *client,
        }
     }
 
+  if (client->priv->timeout)
+    g_socket_set_timeout (socket, client->priv->timeout);
+
   return socket;
 }
 
+gboolean
+can_use_proxy (GSocketClient *client)
+{
+  GSocketClientPrivate *priv = client->priv;
+
+  return priv->enable_proxy
+          && priv->type == G_SOCKET_TYPE_STREAM;
+}
+
 static void
 g_socket_client_init (GSocketClient *client)
 {
@@ -125,6 +148,10 @@ g_socket_client_init (GSocketClient *client)
                                              G_TYPE_SOCKET_CLIENT,
                                              GSocketClientPrivate);
   client->priv->type = G_SOCKET_TYPE_STREAM;
+  client->priv->app_proxies = g_hash_table_new_full (g_str_hash,
+                                                    g_str_equal,
+                                                    g_free,
+                                                    NULL);
 }
 
 /**
@@ -136,7 +163,7 @@ g_socket_client_init (GSocketClient *client)
  *     Free the returned object with g_object_unref().
  *
  * Since: 2.22
- **/
+ */
 GSocketClient *
 g_socket_client_new (void)
 {
@@ -153,6 +180,8 @@ g_socket_client_finalize (GObject *object)
 
   if (G_OBJECT_CLASS (g_socket_client_parent_class)->finalize)
     (*G_OBJECT_CLASS (g_socket_client_parent_class)->finalize) (object);
+
+  g_hash_table_unref (client->priv->app_proxies);
 }
 
 static void
@@ -181,6 +210,14 @@ g_socket_client_get_property (GObject    *object,
        g_value_set_object (value, client->priv->local_address);
        break;
 
+      case PROP_TIMEOUT:
+       g_value_set_uint (value, client->priv->timeout);
+       break;
+
+      case PROP_ENABLE_PROXY:
+       g_value_set_boolean (value, client->priv->enable_proxy);
+       break;
+
       default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -212,6 +249,14 @@ g_socket_client_set_property (GObject      *object,
       g_socket_client_set_local_address (client, g_value_get_object (value));
       break;
 
+    case PROP_TIMEOUT:
+      g_socket_client_set_timeout (client, g_value_get_uint (value));
+      break;
+
+    case PROP_ENABLE_PROXY:
+      g_socket_client_set_enable_proxy (client, g_value_get_boolean (value));
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -228,7 +273,7 @@ g_socket_client_set_property (GObject      *object,
  * Returns: a #GSocketFamily
  *
  * Since: 2.22
- **/
+ */
 GSocketFamily
 g_socket_client_get_family (GSocketClient *client)
 {
@@ -250,10 +295,10 @@ g_socket_client_get_family (GSocketClient *client)
  * be an ipv6 mapped to ipv4 address.
  *
  * Since: 2.22
- **/
+ */
 void
 g_socket_client_set_family (GSocketClient *client,
-                           GSocketFamily family)
+                           GSocketFamily  family)
 {
   if (client->priv->family == family)
     return;
@@ -273,7 +318,7 @@ g_socket_client_set_family (GSocketClient *client,
  * Returns: a #GSocketFamily
  *
  * Since: 2.22
- **/
+ */
 GSocketType
 g_socket_client_get_socket_type (GSocketClient *client)
 {
@@ -293,10 +338,10 @@ g_socket_client_get_socket_type (GSocketClient *client)
  * as GSocketClient is used for connection oriented services.
  *
  * Since: 2.22
- **/
+ */
 void
 g_socket_client_set_socket_type (GSocketClient *client,
-                                GSocketType type)
+                                GSocketType    type)
 {
   if (client->priv->type == type)
     return;
@@ -307,7 +352,7 @@ g_socket_client_set_socket_type (GSocketClient *client,
 
 /**
  * g_socket_client_get_protocol:
- * @client: a #GSocketClient.
+ * @client: a #GSocketClient
  *
  * Gets the protocol name type of the socket client.
  *
@@ -316,7 +361,7 @@ g_socket_client_set_socket_type (GSocketClient *client,
  * Returns: a #GSocketProtocol
  *
  * Since: 2.22
- **/
+ */
 GSocketProtocol
 g_socket_client_get_protocol (GSocketClient *client)
 {
@@ -336,10 +381,10 @@ g_socket_client_get_protocol (GSocketClient *client)
  * protocol for the socket family and type.
  *
  * Since: 2.22
- **/
+ */
 void
-g_socket_client_set_protocol (GSocketClient *client,
-                             GSocketProtocol protocol)
+g_socket_client_set_protocol (GSocketClient   *client,
+                             GSocketProtocol  protocol)
 {
   if (client->priv->protocol == protocol)
     return;
@@ -356,10 +401,10 @@ g_socket_client_set_protocol (GSocketClient *client,
  *
  * See g_socket_client_set_local_address() for details.
  *
- * Returns: a #GSocketAddres or %NULL. don't free
+ * Returns: (transfer none): a #GSocketAddres or %NULL. don't free
  *
  * Since: 2.22
- **/
+ */
 GSocketAddress *
 g_socket_client_get_local_address (GSocketClient *client)
 {
@@ -380,13 +425,13 @@ g_socket_client_get_local_address (GSocketClient *client)
  * a specific interface.
  *
  * Since: 2.22
- **/
+ */
 void
-g_socket_client_set_local_address (GSocketClient        *client,
-                                  GSocketAddress       *address)
+g_socket_client_set_local_address (GSocketClient  *client,
+                                  GSocketAddress *address)
 {
   if (address)
-  g_object_ref (address);
+    g_object_ref (address);
 
   if (client->priv->local_address)
     {
@@ -396,6 +441,90 @@ g_socket_client_set_local_address (GSocketClient        *client,
   g_object_notify (G_OBJECT (client), "local-address");
 }
 
+/**
+ * g_socket_client_get_timeout:
+ * @client: a #GSocketClient
+ *
+ * Gets the I/O timeout time for sockets created by @client.
+ *
+ * See g_socket_client_set_timeout() for details.
+ *
+ * Returns: the timeout in seconds
+ *
+ * Since: 2.26
+ */
+guint
+g_socket_client_get_timeout (GSocketClient *client)
+{
+  return client->priv->timeout;
+}
+
+
+/**
+ * g_socket_client_set_timeout:
+ * @client: a #GSocketClient.
+ * @timeout: the timeout
+ *
+ * Sets the I/O timeout for sockets created by @client. @timeout is a
+ * time in seconds, or 0 for no timeout (the default).
+ *
+ * The timeout value affects the initial connection attempt as well,
+ * so setting this may cause calls to g_socket_client_connect(), etc,
+ * to fail with %G_IO_ERROR_TIMED_OUT.
+ *
+ * Since: 2.26
+ */
+void
+g_socket_client_set_timeout (GSocketClient *client,
+                            guint          timeout)
+{
+  if (client->priv->timeout == timeout)
+    return;
+
+  client->priv->timeout = timeout;
+  g_object_notify (G_OBJECT (client), "timeout");
+}
+
+/**
+ * g_socket_client_get_enable_proxy:
+ * @client: a #GSocketClient.
+ *
+ * Gets the proxy enable state; see g_socket_client_set_enable_proxy()
+ *
+ * Returns: whether proxying is enabled
+ *
+ * Since: 2.26
+ */
+gboolean
+g_socket_client_get_enable_proxy (GSocketClient *client)
+{
+  return client->priv->enable_proxy;
+}
+
+/**
+ * g_socket_client_set_enable_proxy:
+ * @client: a #GSocketClient.
+ * @enable: whether to enable proxies
+ *
+ * Sets whether or not @client attempts to make connections via a
+ * proxy server. When enabled (the default), #GSocketClient will use a
+ * #GProxyResolver to determine if a proxy protocol such as SOCKS is
+ * needed, and automatically do the necessary proxy negotiation.
+ *
+ * Since: 2.26
+ */
+void
+g_socket_client_set_enable_proxy (GSocketClient *client,
+                                 gboolean       enable)
+{
+  enable = !!enable;
+  if (client->priv->enable_proxy == enable)
+    return;
+
+  client->priv->enable_proxy = enable;
+  g_object_notify (G_OBJECT (client), "enable-proxy");
+}
+
 static void
 g_socket_client_class_init (GSocketClientClass *class)
 {
@@ -413,7 +542,9 @@ g_socket_client_class_init (GSocketClientClass *class)
                                                      P_("The sockets address family to use for socket construction"),
                                                      G_TYPE_SOCKET_FAMILY,
                                                      G_SOCKET_FAMILY_INVALID,
-                                                     G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+                                                     G_PARAM_CONSTRUCT |
+                                                      G_PARAM_READWRITE |
+                                                      G_PARAM_STATIC_STRINGS));
 
   g_object_class_install_property (gobject_class, PROP_TYPE,
                                   g_param_spec_enum ("type",
@@ -421,7 +552,9 @@ g_socket_client_class_init (GSocketClientClass *class)
                                                      P_("The sockets type to use for socket construction"),
                                                      G_TYPE_SOCKET_TYPE,
                                                      G_SOCKET_TYPE_STREAM,
-                                                     G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+                                                     G_PARAM_CONSTRUCT |
+                                                      G_PARAM_READWRITE |
+                                                      G_PARAM_STATIC_STRINGS));
 
   g_object_class_install_property (gobject_class, PROP_PROTOCOL,
                                   g_param_spec_enum ("protocol",
@@ -429,14 +562,37 @@ g_socket_client_class_init (GSocketClientClass *class)
                                                      P_("The protocol to use for socket construction, or 0 for default"),
                                                      G_TYPE_SOCKET_PROTOCOL,
                                                      G_SOCKET_PROTOCOL_DEFAULT,
-                                                     G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+                                                     G_PARAM_CONSTRUCT |
+                                                      G_PARAM_READWRITE |
+                                                      G_PARAM_STATIC_STRINGS));
 
   g_object_class_install_property (gobject_class, PROP_LOCAL_ADDRESS,
                                   g_param_spec_object ("local-address",
                                                        P_("Local address"),
                                                        P_("The local address constructed sockets will be bound to"),
                                                        G_TYPE_SOCKET_ADDRESS,
-                                                       G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+                                                       G_PARAM_CONSTRUCT |
+                                                        G_PARAM_READWRITE |
+                                                        G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_property (gobject_class, PROP_TIMEOUT,
+                                  g_param_spec_uint ("timeout",
+                                                     P_("Socket timeout"),
+                                                     P_("The I/O timeout for sockets, or 0 for none"),
+                                                     0, G_MAXUINT, 0,
+                                                     G_PARAM_CONSTRUCT |
+                                                      G_PARAM_READWRITE |
+                                                      G_PARAM_STATIC_STRINGS));
+
+   g_object_class_install_property (gobject_class, PROP_ENABLE_PROXY,
+                                   g_param_spec_boolean ("enable-proxy",
+                                                         P_("Enable proxy"),
+                                                         P_("Enable proxy support"),
+                                                         TRUE,
+                                                         G_PARAM_CONSTRUCT |
+                                                         G_PARAM_READWRITE |
+                                                         G_PARAM_STATIC_STRINGS));
+
 }
 
 /**
@@ -465,10 +621,10 @@ g_socket_client_class_init (GSocketClientClass *class)
  * If a local address is specified with g_socket_client_set_local_address() the
  * socket will be bound to this address before connecting.
  *
- * Returns: a #GSocketConnection on success, %NULL on error.
+ * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
  *
  * Since: 2.22
- **/
+ */
 GSocketConnection *
 g_socket_client_connect (GSocketClient       *client,
                         GSocketConnectable  *connectable,
@@ -476,14 +632,19 @@ g_socket_client_connect (GSocketClient       *client,
                         GError             **error)
 {
   GSocketConnection *connection = NULL;
-  GSocketAddressEnumerator *enumerator;
+  GSocketAddressEnumerator *enumerator = NULL;
   GError *last_error, *tmp_error;
 
   last_error = NULL;
-  enumerator = g_socket_connectable_enumerate (connectable);
+
+  if (can_use_proxy (client))
+    enumerator = g_socket_connectable_proxy_enumerate (connectable);
+  else
+    enumerator = g_socket_connectable_enumerate (connectable);
+
   while (connection == NULL)
     {
-      GSocketAddress *address;
+      GSocketAddress *address = NULL;
       GSocket *socket;
 
       if (g_cancellable_is_cancelled (cancellable))
@@ -495,7 +656,8 @@ g_socket_client_connect (GSocketClient       *client,
 
       tmp_error = NULL;
       address = g_socket_address_enumerator_next (enumerator, cancellable,
-                                                 &tmp_error);
+                                                 &tmp_error);
+
       if (address == NULL)
        {
          if (tmp_error)
@@ -505,11 +667,11 @@ g_socket_client_connect (GSocketClient       *client,
            }
          else if (last_error)
            {
-             g_propagate_error (error, tmp_error);
+             g_propagate_error (error, last_error);
            }
          else
-           g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
-                        _("Unknown error on connect"));
+            g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                                 _("Unknown error on connect"));
          break;
        }
 
@@ -519,12 +681,78 @@ g_socket_client_connect (GSocketClient       *client,
       socket = create_socket (client, address, &last_error);
       if (socket != NULL)
        {
-         if (g_socket_connect (socket, address, &last_error))
+         if (g_socket_connect (socket, address, cancellable, &last_error))
            connection = g_socket_connection_factory_create_connection (socket);
 
          g_object_unref (socket);
        }
 
+      if (connection &&
+         G_IS_PROXY_ADDRESS (address) &&
+         client->priv->enable_proxy)
+       {
+         GProxyAddress *proxy_addr = G_PROXY_ADDRESS (address);
+         const gchar *protocol;
+         GProxy *proxy;
+
+         protocol = g_proxy_address_get_protocol (proxy_addr);
+         proxy = g_proxy_get_default_for_protocol (protocol);
+
+          /* The connection should not be anything else then TCP Connection,
+           * but let's put a safety guard in case
+          */
+          if (!G_IS_TCP_CONNECTION (connection))
+            {
+              g_critical ("Trying to proxy over non-TCP connection, this is "
+                          "most likely a bug in GLib IO library.");
+
+              g_set_error_literal (&last_error,
+                  G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
+                  _("Trying to proxy over non-TCP connection is not supported."));
+
+             g_object_unref (connection);
+             connection = NULL;
+            }
+          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);
+             g_object_unref (proxy);
+           }
+         else if (!g_hash_table_lookup_extended (client->priv->app_proxies,
+                                                 protocol, NULL, NULL))
+           {
+             g_set_error (&last_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
+                          _("Proxy protocol '%s' is not supported."),
+                          protocol);
+             g_object_unref (connection);
+             connection = NULL;
+           }
+       }
+
       g_object_unref (address);
     }
   g_object_unref (enumerator);
@@ -534,7 +762,7 @@ g_socket_client_connect (GSocketClient       *client,
 
 /**
  * g_socket_client_connect_to_host:
- * @client: a #SocketClient
+ * @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
@@ -544,7 +772,7 @@ g_socket_client_connect (GSocketClient       *client,
  *
  * 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 recognised 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
@@ -557,7 +785,7 @@ g_socket_client_connect (GSocketClient       *client,
  * 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
  * @default_port is expected to be provided by the application.
-
+ *
  * In the case that an IP address is given, a single connection
  * attempt is made.  In the case that a name is given, multiple
  * connection attempts may be made, in turn and according to the
@@ -571,16 +799,16 @@ g_socket_client_connect (GSocketClient       *client,
  * connectable) %NULL is returned and @error (if non-%NULL) is set
  * accordingly.
  *
Returns: a #GSocketConnection on success, %NULL on error.
* Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
  *
  * Since: 2.22
- **/
+ */
 GSocketConnection *
-g_socket_client_connect_to_host (GSocketClient        *client,
-                                const char           *host_and_port,
-                                int                   default_port,
-                                GCancellable         *cancellable,
-                                GError              **error)
+g_socket_client_connect_to_host (GSocketClient  *client,
+                                const gchar    *host_and_port,
+                                guint16         default_port,
+                                GCancellable   *cancellable,
+                                GError        **error)
 {
   GSocketConnectable *connectable;
   GSocketConnection *connection;
@@ -596,6 +824,101 @@ g_socket_client_connect_to_host (GSocketClient        *client,
   return connection;
 }
 
+/**
+ * g_socket_client_connect_to_service:
+ * @client: a #GSocketConnection
+ * @domain: a domain name
+ * @service: the name of the service to connect to
+ * @cancellable: 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.
+ *
+ * This call looks up the SRV record for @service at @domain for the
+ * "tcp" protocol.  It then attempts to connect, in turn, to each of
+ * the hosts providing the service until either a connection succeeds
+ * or there are no hosts remaining.
+ *
+ * Upon a successful connection, a new #GSocketConnection is constructed
+ * and returned.  The caller owns this new object and must drop their
+ * reference to it when finished with it.
+ *
+ * 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.
+ */
+GSocketConnection *
+g_socket_client_connect_to_service (GSocketClient  *client,
+                                   const gchar    *domain,
+                                   const gchar    *service,
+                                   GCancellable   *cancellable,
+                                   GError        **error)
+{
+  GSocketConnectable *connectable;
+  GSocketConnection *connection;
+
+  connectable = g_network_service_new (service, "tcp", domain);
+  connection = g_socket_client_connect (client, connectable,
+                                       cancellable, error);
+  g_object_unref (connectable);
+
+  return connection;
+}
+
+/**
+ * g_socket_client_connect_to_uri:
+ * @client: a #GSocketClient
+ * @uri: A network URI
+ * @default_port: the default port to connect to
+ * @cancellable: 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 with a network URI.
+ *
+ * @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.
+ *
+ * Using this rather than g_socket_client_connect() or
+ * g_socket_client_connect_to_host() allows #GSocketClient to
+ * determine when to use application-specific proxy protocols.
+ *
+ * Upon a successful connection, a new #GSocketConnection is constructed
+ * and returned.  The caller owns this new object and must drop their
+ * reference to it when finished with it.
+ *
+ * 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 on success, %NULL on error.
+ *
+ * Since: 2.26
+ */
+GSocketConnection *
+g_socket_client_connect_to_uri (GSocketClient  *client,
+                               const gchar    *uri,
+                               guint16         default_port,
+                               GCancellable   *cancellable,
+                               GError        **error)
+{
+  GSocketConnectable *connectable;
+  GSocketConnection *connection;
+
+  connectable = g_network_address_parse_uri (uri, default_port, error);
+  if (connectable == NULL)
+    return NULL;
+
+  connection = g_socket_client_connect (client, connectable,
+                                       cancellable, error);
+  g_object_unref (connectable);
+
+  return connection;
+}
+
 typedef struct
 {
   GSimpleAsyncResult *result;
@@ -603,7 +926,9 @@ typedef struct
   GSocketClient *client;
 
   GSocketAddressEnumerator *enumerator;
+  GProxyAddress *proxy_addr;
   GSocket *current_socket;
+  GSocketConnection *connection;
 
   GError *last_error;
 } GSocketClientAsyncConnectData;
@@ -611,27 +936,29 @@ typedef struct
 static void
 g_socket_client_async_connect_complete (GSocketClientAsyncConnectData *data)
 {
-  GSocketConnection *connection;
-
   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->current_socket);
+      g_assert (data->connection);
 
-      g_socket_set_blocking (data->current_socket, TRUE);
-
-      connection = g_socket_connection_factory_create_connection (data->current_socket);
       g_simple_async_result_set_op_res_gpointer (data->result,
-                                                connection,
+                                                data->connection,
                                                 g_object_unref);
     }
 
   g_simple_async_result_complete (data->result);
   g_object_unref (data->result);
+  g_object_unref (data->enumerator);
+  if (data->cancellable)
+    g_object_unref (data->cancellable);
+  if (data->current_socket)
+    g_object_unref (data->current_socket);
+  if (data->proxy_addr)
+    g_object_unref (data->proxy_addr);
+  g_slice_free (GSocketClientAsyncConnectData, data);
 }
 
 
@@ -648,6 +975,114 @@ set_last_error (GSocketClientAsyncConnectData *data,
   data->last_error = error;
 }
 
+static void
+enumerator_next_async (GSocketClientAsyncConnectData *data)
+{
+  g_socket_address_enumerator_next_async (data->enumerator,
+                                         data->cancellable,
+                                         g_socket_client_enumerator_callback,
+                                         data);
+}
+
+static void
+g_socket_client_proxy_connect_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 (io_stream)
+    {
+      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);
+    }
+  else
+    {
+      data->connection = NULL;
+    }
+
+  g_object_unref (old_connection);
+
+  g_socket_client_async_connect_complete (data);
+}
+
+static void
+g_socket_client_proxy_connect (GSocketClientAsyncConnectData *data)
+{
+  GProxy *proxy;
+  const gchar *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,
+   * but let's put a safety guard in case
+   */
+  if (!G_IS_TCP_CONNECTION (data->connection))
+    {
+      g_critical ("Trying to proxy over non-TCP connection, this is "
+          "most likely a bug in GLib IO library.");
+
+      g_set_error_literal (&data->last_error,
+          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->proxy_addr,
+                             data->cancellable,
+                             g_socket_client_proxy_connect_callback,
+                             data);
+      g_object_unref (proxy);
+    }
+  else if (!g_hash_table_lookup_extended (data->client->priv->app_proxies,
+                                         protocol, NULL, NULL))
+    {
+      g_clear_error (&data->last_error);
+
+      g_set_error (&data->last_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
+          _("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,
@@ -663,6 +1098,9 @@ g_socket_client_socket_callback (GSocket *socket,
       data->current_socket = NULL;
       g_cancellable_set_error_if_cancelled (data->cancellable,
                                            &data->last_error);
+
+      g_socket_client_async_connect_complete (data);
+      return FALSE;
     }
   else
     {
@@ -670,19 +1108,17 @@ g_socket_client_socket_callback (GSocket *socket,
       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 */
-         g_socket_address_enumerator_next_async (data->enumerator,
-                                                 data->cancellable,
-                                                 g_socket_client_enumerator_callback,
-                                                 data);
+         enumerator_next_async (data);
 
          return FALSE;
        }
     }
 
-  g_socket_client_async_connect_complete (data);
-
+  g_socket_client_socket_connected (data);
   return FALSE;
 }
 
@@ -692,7 +1128,7 @@ g_socket_client_enumerator_callback (GObject      *object,
                                     gpointer      user_data)
 {
   GSocketClientAsyncConnectData *data = user_data;
-  GSocketAddress *address;
+  GSocketAddress *address = NULL;
   GSocket *socket;
   GError *tmp_error = NULL;
 
@@ -712,23 +1148,27 @@ g_socket_client_enumerator_callback (GObject      *object,
       if (tmp_error)
        set_last_error (data, tmp_error);
       else if (data->last_error == NULL)
-       g_set_error (&data->last_error, G_IO_ERROR, G_IO_ERROR_FAILED,
-                    _("Unknown error on connect"));
+        g_set_error_literal (&data->last_error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                             _("Unknown error on connect"));
 
       g_socket_client_async_connect_complete (data);
       return;
     }
 
+  if (G_IS_PROXY_ADDRESS (address) &&
+      data->client->priv->enable_proxy)
+    data->proxy_addr = g_object_ref (G_PROXY_ADDRESS (address));
+
   g_clear_error (&data->last_error);
 
   socket = create_socket (data->client, address, &data->last_error);
   if (socket != NULL)
     {
       g_socket_set_blocking (socket, FALSE);
-      if (g_socket_connect (socket, address, &tmp_error))
+      if (g_socket_connect (socket, address, data->cancellable, &tmp_error))
        {
          data->current_socket = socket;
-         g_socket_client_async_connect_complete (data);
+         g_socket_client_socket_connected (data);
 
          g_object_unref (address);
          return;
@@ -745,7 +1185,7 @@ g_socket_client_enumerator_callback (GObject      *object,
          g_source_set_callback (source,
                                 (GSourceFunc) g_socket_client_socket_callback,
                                 data, NULL);
-         g_source_attach (source, NULL);
+         g_source_attach (source, g_main_context_get_thread_default ());
          g_source_unref (source);
 
          g_object_unref (address);
@@ -756,13 +1196,10 @@ g_socket_client_enumerator_callback (GObject      *object,
          data->last_error = tmp_error;
          g_object_unref (socket);
        }
-      g_object_unref (address);
     }
 
-  g_socket_address_enumerator_next_async (data->enumerator,
-                                         data->cancellable,
-                                         g_socket_client_enumerator_callback,
-                                         data);
+  g_object_unref (address);
+  enumerator_next_async (data);
 }
 
 /**
@@ -780,7 +1217,7 @@ g_socket_client_enumerator_callback (GObject      *object,
  * the result of the operation.
  *
  * Since: 2.22
- **/
+ */
 void
 g_socket_client_connect_async (GSocketClient       *client,
                               GSocketConnectable  *connectable,
@@ -792,7 +1229,7 @@ g_socket_client_connect_async (GSocketClient       *client,
 
   g_return_if_fail (G_IS_SOCKET_CLIENT (client));
 
-  data = g_slice_new (GSocketClientAsyncConnectData);
+  data = g_slice_new0 (GSocketClientAsyncConnectData);
 
   data->result = g_simple_async_result_new (G_OBJECT (client),
                                            callback, user_data,
@@ -800,14 +1237,13 @@ g_socket_client_connect_async (GSocketClient       *client,
   data->client = client;
   if (cancellable)
     data->cancellable = g_object_ref (cancellable);
+
+  if (can_use_proxy (client))
+      data->enumerator = g_socket_connectable_proxy_enumerate (connectable);
   else
-    data->cancellable = NULL;
-  data->last_error = NULL;
-  data->enumerator = g_socket_connectable_enumerate (connectable);
+      data->enumerator = g_socket_connectable_enumerate (connectable);
 
-  g_socket_address_enumerator_next_async (data->enumerator, cancellable,
-                                         g_socket_client_enumerator_callback,
-                                         data);
+  enumerator_next_async (data);
 }
 
 /**
@@ -826,11 +1262,11 @@ g_socket_client_connect_async (GSocketClient       *client,
  * the result of the operation.
  *
  * Since: 2.22
- **/
+ */
 void
 g_socket_client_connect_to_host_async (GSocketClient        *client,
-                                      const char           *host_and_port,
-                                      int                   default_port,
+                                      const gchar          *host_and_port,
+                                      guint16               default_port,
                                       GCancellable         *cancellable,
                                       GAsyncReadyCallback   callback,
                                       gpointer              user_data)
@@ -857,6 +1293,83 @@ g_socket_client_connect_to_host_async (GSocketClient        *client,
 }
 
 /**
+ * g_socket_client_connect_to_service_async:
+ * @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
+ *
+ * This is the asynchronous version of
+ * g_socket_client_connect_to_service().
+ *
+ * Since: 2.22
+ */
+void
+g_socket_client_connect_to_service_async (GSocketClient       *client,
+                                         const gchar         *domain,
+                                         const gchar         *service,
+                                         GCancellable        *cancellable,
+                                         GAsyncReadyCallback  callback,
+                                         gpointer             user_data)
+{
+  GSocketConnectable *connectable;
+
+  connectable = g_network_service_new (service, "tcp", domain);
+  g_socket_client_connect_async (client,
+                                connectable, cancellable,
+                                callback, user_data);
+  g_object_unref (connectable);
+}
+
+/**
+ * g_socket_client_connect_to_uri_async:
+ * @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
+ *
+ * This is the asynchronous version of g_socket_client_connect_to_uri().
+ *
+ * When the operation is finished @callback will be
+ * called. You can then call g_socket_client_connect_to_uri_finish() to get
+ * the result of the operation.
+ *
+ * Since: 2.26
+ */
+void
+g_socket_client_connect_to_uri_async (GSocketClient        *client,
+                                     const gchar          *uri,
+                                     guint16               default_port,
+                                     GCancellable         *cancellable,
+                                     GAsyncReadyCallback   callback,
+                                     gpointer              user_data)
+{
+  GSocketConnectable *connectable;
+  GError *error;
+
+  error = NULL;
+  connectable = g_network_address_parse_uri (uri, default_port, &error);
+  if (connectable == NULL)
+    {
+      g_simple_async_report_gerror_in_idle (G_OBJECT (client),
+                                           callback, user_data, error);
+      g_error_free (error);
+    }
+  else
+    {
+      g_socket_client_connect_async (client,
+                                    connectable, cancellable,
+                                    callback, user_data);
+      g_object_unref (connectable);
+    }
+}
+
+
+/**
  * g_socket_client_connect_finish:
  * @client: a #GSocketClient.
  * @result: a #GAsyncResult.
@@ -865,10 +1378,10 @@ g_socket_client_connect_to_host_async (GSocketClient        *client,
  *
  * Finishes an async connect operation. See g_socket_client_connect_async()
  *
- * Returns: a #GSocketConnection on success, %NULL on error.
+ * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
  *
  * Since: 2.22
- **/
+ */
 GSocketConnection *
 g_socket_client_connect_finish (GSocketClient  *client,
                                GAsyncResult   *result,
@@ -891,17 +1404,84 @@ g_socket_client_connect_finish (GSocketClient  *client,
  *
  * Finishes an async connect operation. See g_socket_client_connect_to_host_async()
  *
- * Returns: a #GSocketConnection on success, %NULL on error.
+ * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
  *
  * Since: 2.22
- **/
+ */
 GSocketConnection *
-g_socket_client_connect_to_host_finish (GSocketClient        *client,
-                                       GAsyncResult         *result,
-                                       GError              **error)
+g_socket_client_connect_to_host_finish (GSocketClient  *client,
+                                       GAsyncResult   *result,
+                                       GError        **error)
 {
   return g_socket_client_connect_finish (client, result, error);
 }
 
-#define __G_SOCKET_CLIENT_C__
-#include "gioaliasdef.c"
+/**
+ * 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
+ * ignore.
+ *
+ * Finishes an async connect operation. See g_socket_client_connect_to_service_async()
+ *
+ * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
+ *
+ * Since: 2.22
+ */
+GSocketConnection *
+g_socket_client_connect_to_service_finish (GSocketClient  *client,
+                                          GAsyncResult   *result,
+                                          GError        **error)
+{
+  return g_socket_client_connect_finish (client, result, error);
+}
+
+/**
+ * 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
+ * ignore.
+ *
+ * Finishes an async connect operation. See g_socket_client_connect_to_uri_async()
+ *
+ * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
+ *
+ * Since: 2.26
+ */
+GSocketConnection *
+g_socket_client_connect_to_uri_finish (GSocketClient  *client,
+                                      GAsyncResult   *result,
+                                      GError        **error)
+{
+  return g_socket_client_connect_finish (client, result, error);
+}
+
+/**
+ * g_socket_client_add_application_proxy:
+ * @client: a #GSocketClient
+ * @protocol: The proxy protocol
+ *
+ * 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
+ * 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
+ * appropriate type, to determine whether or not it needs to handle
+ * the proxy handshaking itself.
+ *
+ * This should be used for proxy protocols that are dialects of
+ * another protocol such as HTTP proxy. It also allows cohabitation of
+ * 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.
+ */
+void
+g_socket_client_add_application_proxy (GSocketClient *client,
+                                      const gchar   *protocol)
+{
+  g_hash_table_insert (client->priv->app_proxies, g_strdup (protocol), NULL);
+}