Add g_socket_connection_connect(), etc
[platform/upstream/glib.git] / gio / gsocketclient.c
index 0519346..88f2b36 100644 (file)
@@ -46,6 +46,7 @@
 #include <gio/gtcpwrapperconnection.h>
 #include <gio/gtlscertificate.h>
 #include <gio/gtlsclientconnection.h>
+#include <gio/ginetaddress.h>
 #include "glibintl.h"
 
 
@@ -138,7 +139,7 @@ create_socket (GSocketClient  *client,
   return socket;
 }
 
-gboolean
+static gboolean
 can_use_proxy (GSocketClient *client)
 {
   GSocketClientPrivate *priv = client->priv;
@@ -148,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,
@@ -423,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
  */
@@ -769,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))
@@ -809,8 +845,14 @@ g_socket_client_connect (GSocketClient       *client,
          continue;
        }
 
-      if (g_socket_connect (socket, address, cancellable, &last_error))
-       connection = (GIOStream *)g_socket_connection_factory_create_connection (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 &&
          G_IS_PROXY_ADDRESS (address) &&
@@ -860,9 +902,13 @@ g_socket_client_connect (GSocketClient       *client,
              g_object_unref (connection);
              connection = NULL;
            }
+         else
+           {
+             application_proxy = TRUE;
+           }
        }
 
-      if (connection && client->priv->tls)
+      if (!application_proxy && connection && client->priv->tls)
        {
          GIOStream *tlsconn;
 
@@ -971,7 +1017,6 @@ g_socket_client_connect_to_host (GSocketClient  *client,
  * @service: the name of the service to connect to
  * @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.
  *
@@ -987,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,
@@ -1070,6 +1117,7 @@ typedef struct
   GSocketConnectable *connectable;
   GSocketAddressEnumerator *enumerator;
   GProxyAddress *proxy_addr;
+  GSocketAddress *current_addr;
   GSocket *current_socket;
   GIOStream *connection;
 
@@ -1108,6 +1156,8 @@ g_socket_client_async_connect_complete (GSocketClientAsyncConnectData *data)
   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)
@@ -1132,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,
@@ -1151,19 +1207,14 @@ g_socket_client_tls_handshake_callback (GObject      *object,
     {
       g_object_unref (data->connection);
       data->connection = G_IO_STREAM (object);
+
+      g_socket_client_async_connect_complete (data);
     }
   else
     {
       g_object_unref (object);
-      g_object_unref (data->current_socket);
-      data->current_socket = NULL;
-      g_object_unref (data->connection);
-      data->connection = NULL;
-
       enumerator_next_async (data);
     }
-
-  g_socket_client_async_connect_complete (data);
 }
 
 static void
@@ -1192,11 +1243,6 @@ g_socket_client_tls_handshake (GSocketClientAsyncConnectData *data)
     }
   else
     {
-      g_object_unref (data->current_socket);
-      data->current_socket = NULL;
-      g_object_unref (data->connection);
-      data->connection = NULL;
-
       enumerator_next_async (data);
     }
 }
@@ -1214,9 +1260,6 @@ g_socket_client_proxy_connect_callback (GObject      *object,
                                             &data->last_error);
   if (!data->connection)
     {
-      g_object_unref (data->current_socket);
-      data->current_socket = NULL;
-
       enumerator_next_async (data);
       return;
     }
@@ -1225,11 +1268,30 @@ g_socket_client_proxy_connect_callback (GObject      *object,
 }
 
 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;
 
+  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);
@@ -1239,7 +1301,7 @@ g_socket_client_proxy_connect (GSocketClientAsyncConnectData *data)
   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))
@@ -1251,11 +1313,6 @@ 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;
-      g_object_unref (data->current_socket);
-      data->current_socket = NULL;
-
       enumerator_next_async (data);
     }
   else if (proxy)
@@ -1277,65 +1334,14 @@ g_socket_client_proxy_connect (GSocketClientAsyncConnectData *data)
           _("Proxy protocol '%s' is not supported."),
           protocol);
 
-      g_object_unref (data->current_socket);
-      data->current_socket = NULL;
-      g_object_unref (data->connection);
-      data->connection = NULL;
-      g_object_unref (data->current_socket);
-      data->current_socket = NULL;
-
       enumerator_next_async (data);
     }
-}
-
-static void
-g_socket_client_socket_connected (GSocketClientAsyncConnectData *data)
-{
-  g_socket_set_blocking (data->current_socket, TRUE);
-
-  data->connection = (GIOStream *)
-    g_socket_connection_factory_create_connection (data->current_socket);
-
-  g_socket_client_proxy_connect (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);
-
-      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;
-       }
+      /* 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);
     }
-
-  g_socket_client_socket_connected (data);
-  return FALSE;
 }
 
 static void
@@ -1378,49 +1384,25 @@ 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: (allow-none): a #GCancellable, or %NULL
  * @callback: (scope async): a #GAsyncReadyCallback
@@ -1468,7 +1450,7 @@ 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: (allow-none): a #GCancellable, or %NULL
@@ -1696,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,