Blame gio/gsocketclient.c

Packit ae235b
/*  GIO - GLib Input, Output and Streaming Library
Packit ae235b
 *
Packit ae235b
 * Copyright © 2008, 2009 codethink
Packit ae235b
 * Copyright © 2009 Red Hat, Inc
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Authors: Ryan Lortie <desrt@desrt.ca>
Packit ae235b
 *          Alexander Larsson <alexl@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
#include "gsocketclient.h"
Packit ae235b
Packit ae235b
#include <stdlib.h>
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
#include <gio/gioenumtypes.h>
Packit ae235b
#include <gio/gsocketaddressenumerator.h>
Packit ae235b
#include <gio/gsocketconnectable.h>
Packit ae235b
#include <gio/gsocketconnection.h>
Packit ae235b
#include <gio/gioprivate.h>
Packit ae235b
#include <gio/gproxyaddressenumerator.h>
Packit ae235b
#include <gio/gproxyaddress.h>
Packit ae235b
#include <gio/gtask.h>
Packit ae235b
#include <gio/gcancellable.h>
Packit ae235b
#include <gio/gioerror.h>
Packit ae235b
#include <gio/gsocket.h>
Packit ae235b
#include <gio/gnetworkaddress.h>
Packit ae235b
#include <gio/gnetworkservice.h>
Packit ae235b
#include <gio/gproxy.h>
Packit ae235b
#include <gio/gproxyresolver.h>
Packit ae235b
#include <gio/gsocketaddress.h>
Packit ae235b
#include <gio/gtcpconnection.h>
Packit ae235b
#include <gio/gtcpwrapperconnection.h>
Packit ae235b
#include <gio/gtlscertificate.h>
Packit ae235b
#include <gio/gtlsclientconnection.h>
Packit ae235b
#include <gio/ginetaddress.h>
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gsocketclient
Packit ae235b
 * @short_description: Helper for connecting to a network service
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 * @see_also: #GSocketConnection, #GSocketListener
Packit ae235b
 *
Packit ae235b
 * #GSocketClient is a lightweight high-level utility class for connecting to
Packit ae235b
 * a network host using a connection oriented socket type.
Packit ae235b
 *
Packit ae235b
 * You create a #GSocketClient object, set any options you want, and then
Packit ae235b
 * call a sync or async connect operation, which returns a #GSocketConnection
Packit ae235b
 * subclass on success.
Packit ae235b
 *
Packit ae235b
 * The type of the #GSocketConnection object returned depends on the type of
Packit ae235b
 * the underlying socket that is in use. For instance, for a TCP/IP connection
Packit ae235b
 * it will be a #GTcpConnection.
Packit ae235b
 *
Packit ae235b
 * As #GSocketClient is a lightweight object, you don't need to cache it. You
Packit ae235b
 * can just create a new one any time you need one.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
Packit ae235b
Packit ae235b
enum
Packit ae235b
{
Packit ae235b
  EVENT,
Packit ae235b
  LAST_SIGNAL
Packit ae235b
};
Packit ae235b
Packit ae235b
static guint signals[LAST_SIGNAL] = { 0 };
Packit ae235b
Packit ae235b
enum
Packit ae235b
{
Packit ae235b
  PROP_NONE,
Packit ae235b
  PROP_FAMILY,
Packit ae235b
  PROP_TYPE,
Packit ae235b
  PROP_PROTOCOL,
Packit ae235b
  PROP_LOCAL_ADDRESS,
Packit ae235b
  PROP_TIMEOUT,
Packit ae235b
  PROP_ENABLE_PROXY,
Packit ae235b
  PROP_TLS,
Packit ae235b
  PROP_TLS_VALIDATION_FLAGS,
Packit ae235b
  PROP_PROXY_RESOLVER
Packit ae235b
};
Packit ae235b
Packit ae235b
struct _GSocketClientPrivate
Packit ae235b
{
Packit ae235b
  GSocketFamily family;
Packit ae235b
  GSocketType type;
Packit ae235b
  GSocketProtocol protocol;
Packit ae235b
  GSocketAddress *local_address;
Packit ae235b
  guint timeout;
Packit ae235b
  gboolean enable_proxy;
Packit ae235b
  GHashTable *app_proxies;
Packit ae235b
  gboolean tls;
Packit ae235b
  GTlsCertificateFlags tls_validation_flags;
Packit ae235b
  GProxyResolver *proxy_resolver;
Packit ae235b
};
Packit ae235b
Packit ae235b
G_DEFINE_TYPE_WITH_PRIVATE (GSocketClient, g_socket_client, G_TYPE_OBJECT)
Packit ae235b
Packit ae235b
static GSocket *
Packit ae235b
create_socket (GSocketClient  *client,
Packit ae235b
	       GSocketAddress *dest_address,
Packit ae235b
	       GError        **error)
Packit ae235b
{
Packit ae235b
  GSocketFamily family;
Packit ae235b
  GSocket *socket;
Packit ae235b
Packit ae235b
  family = client->priv->family;
Packit ae235b
  if (family == G_SOCKET_FAMILY_INVALID &&
Packit ae235b
      client->priv->local_address != NULL)
Packit ae235b
    family = g_socket_address_get_family (client->priv->local_address);
Packit ae235b
  if (family == G_SOCKET_FAMILY_INVALID)
Packit ae235b
    family = g_socket_address_get_family (dest_address);
Packit ae235b
Packit ae235b
  socket = g_socket_new (family,
Packit ae235b
			 client->priv->type,
Packit ae235b
			 client->priv->protocol,
Packit ae235b
			 error);
Packit ae235b
  if (socket == NULL)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  if (client->priv->local_address)
Packit ae235b
    {
Packit ae235b
      if (!g_socket_bind (socket,
Packit ae235b
			  client->priv->local_address,
Packit ae235b
			  FALSE,
Packit ae235b
			  error))
Packit ae235b
	{
Packit ae235b
	  g_object_unref (socket);
Packit ae235b
	  return NULL;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (client->priv->timeout)
Packit ae235b
    g_socket_set_timeout (socket, client->priv->timeout);
Packit ae235b
Packit ae235b
  return socket;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
can_use_proxy (GSocketClient *client)
Packit ae235b
{
Packit ae235b
  GSocketClientPrivate *priv = client->priv;
Packit ae235b
Packit ae235b
  return priv->enable_proxy
Packit ae235b
          && priv->type == G_SOCKET_TYPE_STREAM;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
clarify_connect_error (GError             *error,
Packit ae235b
		       GSocketConnectable *connectable,
Packit ae235b
		       GSocketAddress     *address)
Packit ae235b
{
Packit ae235b
  const char *name;
Packit ae235b
  char *tmp_name = NULL;
Packit ae235b
Packit ae235b
  if (G_IS_PROXY_ADDRESS (address))
Packit ae235b
    {
Packit ae235b
      name = tmp_name = g_inet_address_to_string (g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address)));
Packit ae235b
Packit ae235b
      g_prefix_error (&error, _("Could not connect to proxy server %s: "), name);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      if (G_IS_NETWORK_ADDRESS (connectable))
Packit ae235b
	name = g_network_address_get_hostname (G_NETWORK_ADDRESS (connectable));
Packit ae235b
      else if (G_IS_NETWORK_SERVICE (connectable))
Packit ae235b
	name = g_network_service_get_domain (G_NETWORK_SERVICE (connectable));
Packit ae235b
      else if (G_IS_INET_SOCKET_ADDRESS (connectable))
Packit ae235b
	name = tmp_name = g_inet_address_to_string (g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (connectable)));
Packit ae235b
      else
Packit ae235b
	name = NULL;
Packit ae235b
Packit ae235b
      if (name)
Packit ae235b
	g_prefix_error (&error, _("Could not connect to %s: "), name);
Packit ae235b
      else
Packit ae235b
	g_prefix_error (&error, _("Could not connect: "));
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_free (tmp_name);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_socket_client_init (GSocketClient *client)
Packit ae235b
{
Packit ae235b
  client->priv = g_socket_client_get_instance_private (client);
Packit ae235b
  client->priv->type = G_SOCKET_TYPE_STREAM;
Packit ae235b
  client->priv->app_proxies = g_hash_table_new_full (g_str_hash,
Packit ae235b
						     g_str_equal,
Packit ae235b
						     g_free,
Packit ae235b
						     NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_socket_client_new:
Packit ae235b
 *
Packit ae235b
 * Creates a new #GSocketClient with the default options.
Packit ae235b
 *
Packit ae235b
 * Returns: a #GSocketClient.
Packit ae235b
 *     Free the returned object with g_object_unref().
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
GSocketClient *
Packit ae235b
g_socket_client_new (void)
Packit ae235b
{
Packit ae235b
  return g_object_new (G_TYPE_SOCKET_CLIENT, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_socket_client_finalize (GObject *object)
Packit ae235b
{
Packit ae235b
  GSocketClient *client = G_SOCKET_CLIENT (object);
Packit ae235b
Packit ae235b
  g_clear_object (&client->priv->local_address);
Packit ae235b
  g_clear_object (&client->priv->proxy_resolver);
Packit ae235b
Packit ae235b
  G_OBJECT_CLASS (g_socket_client_parent_class)->finalize (object);
Packit ae235b
Packit ae235b
  g_hash_table_unref (client->priv->app_proxies);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_socket_client_get_property (GObject    *object,
Packit ae235b
			      guint       prop_id,
Packit ae235b
			      GValue     *value,
Packit ae235b
			      GParamSpec *pspec)
Packit ae235b
{
Packit ae235b
  GSocketClient *client = G_SOCKET_CLIENT (object);
Packit ae235b
Packit ae235b
  switch (prop_id)
Packit ae235b
    {
Packit ae235b
      case PROP_FAMILY:
Packit ae235b
	g_value_set_enum (value, client->priv->family);
Packit ae235b
	break;
Packit ae235b
Packit ae235b
      case PROP_TYPE:
Packit ae235b
	g_value_set_enum (value, client->priv->type);
Packit ae235b
	break;
Packit ae235b
Packit ae235b
      case PROP_PROTOCOL:
Packit ae235b
	g_value_set_enum (value, client->priv->protocol);
Packit ae235b
	break;
Packit ae235b
Packit ae235b
      case PROP_LOCAL_ADDRESS:
Packit ae235b
	g_value_set_object (value, client->priv->local_address);
Packit ae235b
	break;
Packit ae235b
Packit ae235b
      case PROP_TIMEOUT:
Packit ae235b
	g_value_set_uint (value, client->priv->timeout);
Packit ae235b
	break;
Packit ae235b
Packit ae235b
      case PROP_ENABLE_PROXY:
Packit ae235b
	g_value_set_boolean (value, client->priv->enable_proxy);
Packit ae235b
	break;
Packit ae235b
Packit ae235b
      case PROP_TLS:
Packit ae235b
	g_value_set_boolean (value, g_socket_client_get_tls (client));
Packit ae235b
	break;
Packit ae235b
Packit ae235b
      case PROP_TLS_VALIDATION_FLAGS:
Packit ae235b
	g_value_set_flags (value, g_socket_client_get_tls_validation_flags (client));
Packit ae235b
	break;
Packit ae235b
Packit ae235b
      case PROP_PROXY_RESOLVER:
Packit ae235b
	g_value_set_object (value, g_socket_client_get_proxy_resolver (client));
Packit ae235b
	break;
Packit ae235b
Packit ae235b
      default:
Packit ae235b
	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_socket_client_set_property (GObject      *object,
Packit ae235b
			      guint         prop_id,
Packit ae235b
			      const GValue *value,
Packit ae235b
			      GParamSpec   *pspec)
Packit ae235b
{
Packit ae235b
  GSocketClient *client = G_SOCKET_CLIENT (object);
Packit ae235b
Packit ae235b
  switch (prop_id)
Packit ae235b
    {
Packit ae235b
    case PROP_FAMILY:
Packit ae235b
      g_socket_client_set_family (client, g_value_get_enum (value));
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_TYPE:
Packit ae235b
      g_socket_client_set_socket_type (client, g_value_get_enum (value));
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_PROTOCOL:
Packit ae235b
      g_socket_client_set_protocol (client, g_value_get_enum (value));
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_LOCAL_ADDRESS:
Packit ae235b
      g_socket_client_set_local_address (client, g_value_get_object (value));
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_TIMEOUT:
Packit ae235b
      g_socket_client_set_timeout (client, g_value_get_uint (value));
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_ENABLE_PROXY:
Packit ae235b
      g_socket_client_set_enable_proxy (client, g_value_get_boolean (value));
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_TLS:
Packit ae235b
      g_socket_client_set_tls (client, g_value_get_boolean (value));
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_TLS_VALIDATION_FLAGS:
Packit ae235b
      g_socket_client_set_tls_validation_flags (client, g_value_get_flags (value));
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_PROXY_RESOLVER:
Packit ae235b
      g_socket_client_set_proxy_resolver (client, g_value_get_object (value));
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    default:
Packit ae235b
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_socket_client_get_family:
Packit ae235b
 * @client: a #GSocketClient.
Packit ae235b
 *
Packit ae235b
 * Gets the socket family of the socket client.
Packit ae235b
 *
Packit ae235b
 * See g_socket_client_set_family() for details.
Packit ae235b
 *
Packit ae235b
 * Returns: a #GSocketFamily
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
GSocketFamily
Packit ae235b
g_socket_client_get_family (GSocketClient *client)
Packit ae235b
{
Packit ae235b
  return client->priv->family;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_socket_client_set_family:
Packit ae235b
 * @client: a #GSocketClient.
Packit ae235b
 * @family: a #GSocketFamily
Packit ae235b
 *
Packit ae235b
 * Sets the socket family of the socket client.
Packit ae235b
 * If this is set to something other than %G_SOCKET_FAMILY_INVALID
Packit ae235b
 * then the sockets created by this object will be of the specified
Packit ae235b
 * family.
Packit ae235b
 *
Packit ae235b
 * This might be useful for instance if you want to force the local
Packit ae235b
 * connection to be an ipv4 socket, even though the address might
Packit ae235b
 * be an ipv6 mapped to ipv4 address.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_socket_client_set_family (GSocketClient *client,
Packit ae235b
			    GSocketFamily  family)
Packit ae235b
{
Packit ae235b
  if (client->priv->family == family)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  client->priv->family = family;
Packit ae235b
  g_object_notify (G_OBJECT (client), "family");
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_socket_client_get_socket_type:
Packit ae235b
 * @client: a #GSocketClient.
Packit ae235b
 *
Packit ae235b
 * Gets the socket type of the socket client.
Packit ae235b
 *
Packit ae235b
 * See g_socket_client_set_socket_type() for details.
Packit ae235b
 *
Packit ae235b
 * Returns: a #GSocketFamily
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
GSocketType
Packit ae235b
g_socket_client_get_socket_type (GSocketClient *client)
Packit ae235b
{
Packit ae235b
  return client->priv->type;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_socket_client_set_socket_type:
Packit ae235b
 * @client: a #GSocketClient.
Packit ae235b
 * @type: a #GSocketType
Packit ae235b
 *
Packit ae235b
 * Sets the socket type of the socket client.
Packit ae235b
 * The sockets created by this object will be of the specified
Packit ae235b
 * type.
Packit ae235b
 *
Packit ae235b
 * It doesn't make sense to specify a type of %G_SOCKET_TYPE_DATAGRAM,
Packit ae235b
 * as GSocketClient is used for connection oriented services.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_socket_client_set_socket_type (GSocketClient *client,
Packit ae235b
				 GSocketType    type)
Packit ae235b
{
Packit ae235b
  if (client->priv->type == type)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  client->priv->type = type;
Packit ae235b
  g_object_notify (G_OBJECT (client), "type");
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_socket_client_get_protocol:
Packit ae235b
 * @client: a #GSocketClient
Packit ae235b
 *
Packit ae235b
 * Gets the protocol name type of the socket client.
Packit ae235b
 *
Packit ae235b
 * See g_socket_client_set_protocol() for details.
Packit ae235b
 *
Packit ae235b
 * Returns: a #GSocketProtocol
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
GSocketProtocol
Packit ae235b
g_socket_client_get_protocol (GSocketClient *client)
Packit ae235b
{
Packit ae235b
  return client->priv->protocol;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_socket_client_set_protocol:
Packit ae235b
 * @client: a #GSocketClient.
Packit ae235b
 * @protocol: a #GSocketProtocol
Packit ae235b
 *
Packit ae235b
 * Sets the protocol of the socket client.
Packit ae235b
 * The sockets created by this object will use of the specified
Packit ae235b
 * protocol.
Packit ae235b
 *
Packit ae235b
 * If @protocol is %0 that means to use the default
Packit ae235b
 * protocol for the socket family and type.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_socket_client_set_protocol (GSocketClient   *client,
Packit ae235b
			      GSocketProtocol  protocol)
Packit ae235b
{
Packit ae235b
  if (client->priv->protocol == protocol)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  client->priv->protocol = protocol;
Packit ae235b
  g_object_notify (G_OBJECT (client), "protocol");
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_socket_client_get_local_address:
Packit ae235b
 * @client: a #GSocketClient.
Packit ae235b
 *
Packit ae235b
 * Gets the local address of the socket client.
Packit ae235b
 *
Packit ae235b
 * See g_socket_client_set_local_address() for details.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): a #GSocketAddress or %NULL. Do not free.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
GSocketAddress *
Packit ae235b
g_socket_client_get_local_address (GSocketClient *client)
Packit ae235b
{
Packit ae235b
  return client->priv->local_address;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_socket_client_set_local_address:
Packit ae235b
 * @client: a #GSocketClient.
Packit ae235b
 * @address: (nullable): a #GSocketAddress, or %NULL
Packit ae235b
 *
Packit ae235b
 * Sets the local address of the socket client.
Packit ae235b
 * The sockets created by this object will bound to the
Packit ae235b
 * specified address (if not %NULL) before connecting.
Packit ae235b
 *
Packit ae235b
 * This is useful if you want to ensure that the local
Packit ae235b
 * side of the connection is on a specific port, or on
Packit ae235b
 * a specific interface.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_socket_client_set_local_address (GSocketClient  *client,
Packit ae235b
				   GSocketAddress *address)
Packit ae235b
{
Packit ae235b
  if (address)
Packit ae235b
    g_object_ref (address);
Packit ae235b
Packit ae235b
  if (client->priv->local_address)
Packit ae235b
    {
Packit ae235b
      g_object_unref (client->priv->local_address);
Packit ae235b
    }
Packit ae235b
  client->priv->local_address = address;
Packit ae235b
  g_object_notify (G_OBJECT (client), "local-address");
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_socket_client_get_timeout:
Packit ae235b
 * @client: a #GSocketClient
Packit ae235b
 *
Packit ae235b
 * Gets the I/O timeout time for sockets created by @client.
Packit ae235b
 *
Packit ae235b
 * See g_socket_client_set_timeout() for details.
Packit ae235b
 *
Packit ae235b
 * Returns: the timeout in seconds
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
guint
Packit ae235b
g_socket_client_get_timeout (GSocketClient *client)
Packit ae235b
{
Packit ae235b
  return client->priv->timeout;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_socket_client_set_timeout:
Packit ae235b
 * @client: a #GSocketClient.
Packit ae235b
 * @timeout: the timeout
Packit ae235b
 *
Packit ae235b
 * Sets the I/O timeout for sockets created by @client. @timeout is a
Packit ae235b
 * time in seconds, or 0 for no timeout (the default).
Packit ae235b
 *
Packit ae235b
 * The timeout value affects the initial connection attempt as well,
Packit ae235b
 * so setting this may cause calls to g_socket_client_connect(), etc,
Packit ae235b
 * to fail with %G_IO_ERROR_TIMED_OUT.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_socket_client_set_timeout (GSocketClient *client,
Packit ae235b
			     guint          timeout)
Packit ae235b
{
Packit ae235b
  if (client->priv->timeout == timeout)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  client->priv->timeout = timeout;
Packit ae235b
  g_object_notify (G_OBJECT (client), "timeout");
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_socket_client_get_enable_proxy:
Packit ae235b
 * @client: a #GSocketClient.
Packit ae235b
 *
Packit ae235b
 * Gets the proxy enable state; see g_socket_client_set_enable_proxy()
Packit ae235b
 *
Packit ae235b
 * Returns: whether proxying is enabled
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_socket_client_get_enable_proxy (GSocketClient *client)
Packit ae235b
{
Packit ae235b
  return client->priv->enable_proxy;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_socket_client_set_enable_proxy:
Packit ae235b
 * @client: a #GSocketClient.
Packit ae235b
 * @enable: whether to enable proxies
Packit ae235b
 *
Packit ae235b
 * Sets whether or not @client attempts to make connections via a
Packit ae235b
 * proxy server. When enabled (the default), #GSocketClient will use a
Packit ae235b
 * #GProxyResolver to determine if a proxy protocol such as SOCKS is
Packit ae235b
 * needed, and automatically do the necessary proxy negotiation.
Packit ae235b
 *
Packit ae235b
 * See also g_socket_client_set_proxy_resolver().
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_socket_client_set_enable_proxy (GSocketClient *client,
Packit ae235b
				  gboolean       enable)
Packit ae235b
{
Packit ae235b
  enable = !!enable;
Packit ae235b
  if (client->priv->enable_proxy == enable)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  client->priv->enable_proxy = enable;
Packit ae235b
  g_object_notify (G_OBJECT (client), "enable-proxy");
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_socket_client_get_tls:
Packit ae235b
 * @client: a #GSocketClient.
Packit ae235b
 *
Packit ae235b
 * Gets whether @client creates TLS connections. See
Packit ae235b
 * g_socket_client_set_tls() for details.
Packit ae235b
 *
Packit ae235b
 * Returns: whether @client uses TLS
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_socket_client_get_tls (GSocketClient *client)
Packit ae235b
{
Packit ae235b
  return client->priv->tls;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_socket_client_set_tls:
Packit ae235b
 * @client: a #GSocketClient.
Packit ae235b
 * @tls: whether to use TLS
Packit ae235b
 *
Packit ae235b
 * Sets whether @client creates TLS (aka SSL) connections. If @tls is
Packit ae235b
 * %TRUE, @client will wrap its connections in a #GTlsClientConnection
Packit ae235b
 * and perform a TLS handshake when connecting.
Packit ae235b
 *
Packit ae235b
 * Note that since #GSocketClient must return a #GSocketConnection,
Packit ae235b
 * but #GTlsClientConnection is not a #GSocketConnection, this
Packit ae235b
 * actually wraps the resulting #GTlsClientConnection in a
Packit ae235b
 * #GTcpWrapperConnection when returning it. You can use
Packit ae235b
 * g_tcp_wrapper_connection_get_base_io_stream() on the return value
Packit ae235b
 * to extract the #GTlsClientConnection.
Packit ae235b
 *
Packit ae235b
 * If you need to modify the behavior of the TLS handshake (eg, by
Packit ae235b
 * setting a client-side certificate to use, or connecting to the
Packit ae235b
 * #GTlsConnection::accept-certificate signal), you can connect to
Packit ae235b
 * @client's #GSocketClient::event signal and wait for it to be
Packit ae235b
 * emitted with %G_SOCKET_CLIENT_TLS_HANDSHAKING, which will give you
Packit ae235b
 * a chance to see the #GTlsClientConnection before the handshake
Packit ae235b
 * starts.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_socket_client_set_tls (GSocketClient *client,
Packit ae235b
			 gboolean       tls)
Packit ae235b
{
Packit ae235b
  tls = !!tls;
Packit ae235b
  if (tls == client->priv->tls)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  client->priv->tls = tls;
Packit ae235b
  g_object_notify (G_OBJECT (client), "tls");
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_socket_client_get_tls_validation_flags:
Packit ae235b
 * @client: a #GSocketClient.
Packit ae235b
 *
Packit ae235b
 * Gets the TLS validation flags used creating TLS connections via
Packit ae235b
 * @client.
Packit ae235b
 *
Packit ae235b
 * Returns: the TLS validation flags
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
GTlsCertificateFlags
Packit ae235b
g_socket_client_get_tls_validation_flags (GSocketClient *client)
Packit ae235b
{
Packit ae235b
  return client->priv->tls_validation_flags;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_socket_client_set_tls_validation_flags:
Packit ae235b
 * @client: a #GSocketClient.
Packit ae235b
 * @flags: the validation flags
Packit ae235b
 *
Packit ae235b
 * Sets the TLS validation flags used when creating TLS connections
Packit ae235b
 * via @client. The default value is %G_TLS_CERTIFICATE_VALIDATE_ALL.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_socket_client_set_tls_validation_flags (GSocketClient        *client,
Packit ae235b
					  GTlsCertificateFlags  flags)
Packit ae235b
{
Packit ae235b
  if (client->priv->tls_validation_flags != flags)
Packit ae235b
    {
Packit ae235b
      client->priv->tls_validation_flags = flags;
Packit ae235b
      g_object_notify (G_OBJECT (client), "tls-validation-flags");
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_socket_client_get_proxy_resolver:
Packit ae235b
 * @client: a #GSocketClient.
Packit ae235b
 *
Packit ae235b
 * Gets the #GProxyResolver being used by @client. Normally, this will
Packit ae235b
 * be the resolver returned by g_proxy_resolver_get_default(), but you
Packit ae235b
 * can override it with g_socket_client_set_proxy_resolver().
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): The #GProxyResolver being used by
Packit ae235b
 *   @client.
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
GProxyResolver *
Packit ae235b
g_socket_client_get_proxy_resolver (GSocketClient *client)
Packit ae235b
{
Packit ae235b
  if (client->priv->proxy_resolver)
Packit ae235b
    return client->priv->proxy_resolver;
Packit ae235b
  else
Packit ae235b
    return g_proxy_resolver_get_default ();
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_socket_client_set_proxy_resolver:
Packit ae235b
 * @client: a #GSocketClient.
Packit ae235b
 * @proxy_resolver: (nullable): a #GProxyResolver, or %NULL for the
Packit ae235b
 *   default.
Packit ae235b
 *
Packit ae235b
 * Overrides the #GProxyResolver used by @client. You can call this if
Packit ae235b
 * you want to use specific proxies, rather than using the system
Packit ae235b
 * default proxy settings.
Packit ae235b
 *
Packit ae235b
 * Note that whether or not the proxy resolver is actually used
Packit ae235b
 * depends on the setting of #GSocketClient:enable-proxy, which is not
Packit ae235b
 * changed by this function (but which is %TRUE by default)
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_socket_client_set_proxy_resolver (GSocketClient  *client,
Packit ae235b
                                    GProxyResolver *proxy_resolver)
Packit ae235b
{
Packit ae235b
  /* We have to be careful to avoid calling
Packit ae235b
   * g_proxy_resolver_get_default() until we're sure we need it,
Packit ae235b
   * because trying to load the default proxy resolver module will
Packit ae235b
   * break some test programs that aren't expecting it (eg,
Packit ae235b
   * tests/gsettings).
Packit ae235b
   */
Packit ae235b
Packit ae235b
  if (client->priv->proxy_resolver)
Packit ae235b
    g_object_unref (client->priv->proxy_resolver);
Packit ae235b
Packit ae235b
  client->priv->proxy_resolver = proxy_resolver;
Packit ae235b
Packit ae235b
  if (client->priv->proxy_resolver)
Packit ae235b
    g_object_ref (client->priv->proxy_resolver);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_socket_client_class_init (GSocketClientClass *class)
Packit ae235b
{
Packit ae235b
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
Packit ae235b
Packit ae235b
  gobject_class->finalize = g_socket_client_finalize;
Packit ae235b
  gobject_class->set_property = g_socket_client_set_property;
Packit ae235b
  gobject_class->get_property = g_socket_client_get_property;
Packit ae235b
Packit ae235b
  /**
Packit ae235b
   * GSocketClient::event:
Packit ae235b
   * @client: the #GSocketClient
Packit ae235b
   * @event: the event that is occurring
Packit ae235b
   * @connectable: the #GSocketConnectable that @event is occurring on
Packit ae235b
   * @connection: (nullable): the current representation of the connection
Packit ae235b
   *
Packit ae235b
   * Emitted when @client's activity on @connectable changes state.
Packit ae235b
   * Among other things, this can be used to provide progress
Packit ae235b
   * information about a network connection in the UI. The meanings of
Packit ae235b
   * the different @event values are as follows:
Packit ae235b
   *
Packit ae235b
   * - %G_SOCKET_CLIENT_RESOLVING: @client is about to look up @connectable
Packit ae235b
   *   in DNS. @connection will be %NULL.
Packit ae235b
   *
Packit ae235b
   * - %G_SOCKET_CLIENT_RESOLVED:  @client has successfully resolved
Packit ae235b
   *   @connectable in DNS. @connection will be %NULL.
Packit ae235b
   *
Packit ae235b
   * - %G_SOCKET_CLIENT_CONNECTING: @client is about to make a connection
Packit ae235b
   *   to a remote host; either a proxy server or the destination server
Packit ae235b
   *   itself. @connection is the #GSocketConnection, which is not yet
Packit ae235b
   *   connected.  Since GLib 2.40, you can access the remote
Packit ae235b
   *   address via g_socket_connection_get_remote_address().
Packit ae235b
   *
Packit ae235b
   * - %G_SOCKET_CLIENT_CONNECTED: @client has successfully connected
Packit ae235b
   *   to a remote host. @connection is the connected #GSocketConnection.
Packit ae235b
   *
Packit ae235b
   * - %G_SOCKET_CLIENT_PROXY_NEGOTIATING: @client is about to negotiate
Packit ae235b
   *   with a proxy to get it to connect to @connectable. @connection is
Packit ae235b
   *   the #GSocketConnection to the proxy server.
Packit ae235b
   *
Packit ae235b
   * - %G_SOCKET_CLIENT_PROXY_NEGOTIATED: @client has negotiated a
Packit ae235b
   *   connection to @connectable through a proxy server. @connection is
Packit ae235b
   *   the stream returned from g_proxy_connect(), which may or may not
Packit ae235b
   *   be a #GSocketConnection.
Packit ae235b
   *
Packit ae235b
   * - %G_SOCKET_CLIENT_TLS_HANDSHAKING: @client is about to begin a TLS
Packit ae235b
   *   handshake. @connection is a #GTlsClientConnection.
Packit ae235b
   *
Packit ae235b
   * - %G_SOCKET_CLIENT_TLS_HANDSHAKED: @client has successfully completed
Packit ae235b
   *   the TLS handshake. @connection is a #GTlsClientConnection.
Packit ae235b
   *
Packit ae235b
   * - %G_SOCKET_CLIENT_COMPLETE: @client has either successfully connected
Packit ae235b
   *   to @connectable (in which case @connection is the #GSocketConnection
Packit ae235b
   *   that it will be returning to the caller) or has failed (in which
Packit ae235b
   *   case @connection is %NULL and the client is about to return an error).
Packit ae235b
   *
Packit ae235b
   * Each event except %G_SOCKET_CLIENT_COMPLETE may be emitted
Packit ae235b
   * multiple times (or not at all) for a given connectable (in
Packit ae235b
   * particular, if @client ends up attempting to connect to more than
Packit ae235b
   * one address). However, if @client emits the #GSocketClient::event
Packit ae235b
   * signal at all for a given connectable, that it will always emit
Packit ae235b
   * it with %G_SOCKET_CLIENT_COMPLETE when it is done.
Packit ae235b
   *
Packit ae235b
   * Note that there may be additional #GSocketClientEvent values in
Packit ae235b
   * the future; unrecognized @event values should be ignored.
Packit ae235b
   *
Packit ae235b
   * Since: 2.32
Packit ae235b
   */
Packit ae235b
  signals[EVENT] =
Packit ae235b
    g_signal_new (I_("event"),
Packit ae235b
		  G_TYPE_FROM_CLASS (gobject_class),
Packit ae235b
		  G_SIGNAL_RUN_LAST,
Packit ae235b
		  G_STRUCT_OFFSET (GSocketClientClass, event),
Packit ae235b
		  NULL, NULL,
Packit ae235b
		  NULL,
Packit ae235b
		  G_TYPE_NONE, 3,
Packit ae235b
		  G_TYPE_SOCKET_CLIENT_EVENT,
Packit ae235b
		  G_TYPE_SOCKET_CONNECTABLE,
Packit ae235b
		  G_TYPE_IO_STREAM);
Packit ae235b
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_FAMILY,
Packit ae235b
				   g_param_spec_enum ("family",
Packit ae235b
						      P_("Socket family"),
Packit ae235b
						      P_("The sockets address family to use for socket construction"),
Packit ae235b
						      G_TYPE_SOCKET_FAMILY,
Packit ae235b
						      G_SOCKET_FAMILY_INVALID,
Packit ae235b
						      G_PARAM_CONSTRUCT |
Packit ae235b
                                                      G_PARAM_READWRITE |
Packit ae235b
                                                      G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_TYPE,
Packit ae235b
				   g_param_spec_enum ("type",
Packit ae235b
						      P_("Socket type"),
Packit ae235b
						      P_("The sockets type to use for socket construction"),
Packit ae235b
						      G_TYPE_SOCKET_TYPE,
Packit ae235b
						      G_SOCKET_TYPE_STREAM,
Packit ae235b
						      G_PARAM_CONSTRUCT |
Packit ae235b
                                                      G_PARAM_READWRITE |
Packit ae235b
                                                      G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_PROTOCOL,
Packit ae235b
				   g_param_spec_enum ("protocol",
Packit ae235b
						      P_("Socket protocol"),
Packit ae235b
						      P_("The protocol to use for socket construction, or 0 for default"),
Packit ae235b
						      G_TYPE_SOCKET_PROTOCOL,
Packit ae235b
						      G_SOCKET_PROTOCOL_DEFAULT,
Packit ae235b
						      G_PARAM_CONSTRUCT |
Packit ae235b
                                                      G_PARAM_READWRITE |
Packit ae235b
                                                      G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_LOCAL_ADDRESS,
Packit ae235b
				   g_param_spec_object ("local-address",
Packit ae235b
							P_("Local address"),
Packit ae235b
							P_("The local address constructed sockets will be bound to"),
Packit ae235b
							G_TYPE_SOCKET_ADDRESS,
Packit ae235b
							G_PARAM_CONSTRUCT |
Packit ae235b
                                                        G_PARAM_READWRITE |
Packit ae235b
                                                        G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_TIMEOUT,
Packit ae235b
				   g_param_spec_uint ("timeout",
Packit ae235b
						      P_("Socket timeout"),
Packit ae235b
						      P_("The I/O timeout for sockets, or 0 for none"),
Packit ae235b
						      0, G_MAXUINT, 0,
Packit ae235b
						      G_PARAM_CONSTRUCT |
Packit ae235b
                                                      G_PARAM_READWRITE |
Packit ae235b
                                                      G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
   g_object_class_install_property (gobject_class, PROP_ENABLE_PROXY,
Packit ae235b
				    g_param_spec_boolean ("enable-proxy",
Packit ae235b
							  P_("Enable proxy"),
Packit ae235b
							  P_("Enable proxy support"),
Packit ae235b
							  TRUE,
Packit ae235b
							  G_PARAM_CONSTRUCT |
Packit ae235b
							  G_PARAM_READWRITE |
Packit ae235b
							  G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_TLS,
Packit ae235b
				   g_param_spec_boolean ("tls",
Packit ae235b
							 P_("TLS"),
Packit ae235b
							 P_("Whether to create TLS connections"),
Packit ae235b
							 FALSE,
Packit ae235b
							 G_PARAM_CONSTRUCT |
Packit ae235b
							 G_PARAM_READWRITE |
Packit ae235b
							 G_PARAM_STATIC_STRINGS));
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
Packit ae235b
				   g_param_spec_flags ("tls-validation-flags",
Packit ae235b
						       P_("TLS validation flags"),
Packit ae235b
						       P_("TLS validation flags to use"),
Packit ae235b
						       G_TYPE_TLS_CERTIFICATE_FLAGS,
Packit ae235b
						       G_TLS_CERTIFICATE_VALIDATE_ALL,
Packit ae235b
						       G_PARAM_CONSTRUCT |
Packit ae235b
						       G_PARAM_READWRITE |
Packit ae235b
						       G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
  /**
Packit ae235b
   * GSocketClient:proxy-resolver:
Packit ae235b
   *
Packit ae235b
   * The proxy resolver to use
Packit ae235b
   *
Packit ae235b
   * Since: 2.36
Packit ae235b
   */
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_PROXY_RESOLVER,
Packit ae235b
                                   g_param_spec_object ("proxy-resolver",
Packit ae235b
                                                        P_("Proxy resolver"),
Packit ae235b
                                                        P_("The proxy resolver to use"),
Packit ae235b
                                                        G_TYPE_PROXY_RESOLVER,
Packit ae235b
                                                        G_PARAM_CONSTRUCT |
Packit ae235b
                                                        G_PARAM_READWRITE |
Packit ae235b
                                                        G_PARAM_STATIC_STRINGS));
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_socket_client_emit_event (GSocketClient       *client,
Packit ae235b
			    GSocketClientEvent  event,
Packit ae235b
			    GSocketConnectable  *connectable,
Packit ae235b
			    GIOStream           *connection)
Packit ae235b
{
Packit ae235b
  g_signal_emit (client, signals[EVENT], 0,
Packit ae235b
		 event, connectable, connection);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_socket_client_connect:
Packit ae235b
 * @client: a #GSocketClient.
Packit ae235b
 * @connectable: a #GSocketConnectable specifying the remote address.
Packit ae235b
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
Packit ae235b
 * @error: #GError for error reporting, or %NULL to ignore.
Packit ae235b
 *
Packit ae235b
 * Tries to resolve the @connectable and make a network connection to it.
Packit ae235b
 *
Packit ae235b
 * Upon a successful connection, a new #GSocketConnection is constructed
Packit ae235b
 * and returned.  The caller owns this new object and must drop their
Packit ae235b
 * reference to it when finished with it.
Packit ae235b
 *
Packit ae235b
 * The type of the #GSocketConnection object returned depends on the type of
Packit ae235b
 * the underlying socket that is used. For instance, for a TCP/IP connection
Packit ae235b
 * it will be a #GTcpConnection.
Packit ae235b
 *
Packit ae235b
 * The socket created will be the same family as the address that the
Packit ae235b
 * @connectable resolves to, unless family is set with g_socket_client_set_family()
Packit ae235b
 * or indirectly via g_socket_client_set_local_address(). The socket type
Packit ae235b
 * defaults to %G_SOCKET_TYPE_STREAM but can be set with
Packit ae235b
 * g_socket_client_set_socket_type().
Packit ae235b
 *
Packit ae235b
 * If a local address is specified with g_socket_client_set_local_address() the
Packit ae235b
 * socket will be bound to this address before connecting.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
GSocketConnection *
Packit ae235b
g_socket_client_connect (GSocketClient       *client,
Packit ae235b
			 GSocketConnectable  *connectable,
Packit ae235b
			 GCancellable        *cancellable,
Packit ae235b
			 GError             **error)
Packit ae235b
{
Packit ae235b
  GIOStream *connection = NULL;
Packit ae235b
  GSocketAddressEnumerator *enumerator = NULL;
Packit ae235b
  GError *last_error, *tmp_error;
Packit ae235b
Packit ae235b
  last_error = NULL;
Packit ae235b
Packit ae235b
  if (can_use_proxy (client))
Packit ae235b
    {
Packit ae235b
      enumerator = g_socket_connectable_proxy_enumerate (connectable);
Packit ae235b
      if (client->priv->proxy_resolver &&
Packit ae235b
          G_IS_PROXY_ADDRESS_ENUMERATOR (enumerator))
Packit ae235b
        {
Packit ae235b
          g_object_set (G_OBJECT (enumerator),
Packit ae235b
                        "proxy-resolver", client->priv->proxy_resolver,
Packit ae235b
                        NULL);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    enumerator = g_socket_connectable_enumerate (connectable);
Packit ae235b
Packit ae235b
  while (connection == NULL)
Packit ae235b
    {
Packit ae235b
      GSocketAddress *address = NULL;
Packit ae235b
      gboolean application_proxy = FALSE;
Packit ae235b
      GSocket *socket;
Packit ae235b
      gboolean using_proxy;
Packit ae235b
Packit ae235b
      if (g_cancellable_is_cancelled (cancellable))
Packit ae235b
	{
Packit ae235b
	  g_clear_error (error);
Packit ae235b
	  g_cancellable_set_error_if_cancelled (cancellable, error);
Packit ae235b
	  break;
Packit ae235b
	}
Packit ae235b
Packit ae235b
      tmp_error = NULL;
Packit ae235b
      g_socket_client_emit_event (client, G_SOCKET_CLIENT_RESOLVING,
Packit ae235b
				  connectable, NULL);
Packit ae235b
      address = g_socket_address_enumerator_next (enumerator, cancellable,
Packit ae235b
	      					  &tmp_error);
Packit ae235b
Packit ae235b
      if (address == NULL)
Packit ae235b
	{
Packit ae235b
	  if (tmp_error)
Packit ae235b
	    {
Packit ae235b
	      g_clear_error (&last_error);
Packit ae235b
	      g_propagate_error (error, tmp_error);
Packit ae235b
	    }
Packit ae235b
	  else if (last_error)
Packit ae235b
	    {
Packit ae235b
	      g_propagate_error (error, last_error);
Packit ae235b
	    }
Packit ae235b
	  else
Packit ae235b
            g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
Packit ae235b
                                 _("Unknown error on connect"));
Packit ae235b
	  break;
Packit ae235b
	}
Packit ae235b
      g_socket_client_emit_event (client, G_SOCKET_CLIENT_RESOLVED,
Packit ae235b
				  connectable, NULL);
Packit ae235b
Packit ae235b
      using_proxy = (G_IS_PROXY_ADDRESS (address) &&
Packit ae235b
		     client->priv->enable_proxy);
Packit ae235b
Packit ae235b
      /* clear error from previous attempt */
Packit ae235b
      g_clear_error (&last_error);
Packit ae235b
Packit ae235b
      socket = create_socket (client, address, &last_error);
Packit ae235b
      if (socket == NULL)
Packit ae235b
	{
Packit ae235b
	  g_object_unref (address);
Packit ae235b
	  continue;
Packit ae235b
	}
Packit ae235b
Packit ae235b
      connection = (GIOStream *)g_socket_connection_factory_create_connection (socket);
Packit ae235b
      g_socket_connection_set_cached_remote_address ((GSocketConnection*)connection, address);
Packit ae235b
      g_socket_client_emit_event (client, G_SOCKET_CLIENT_CONNECTING, connectable, connection);
Packit ae235b
Packit ae235b
      if (g_socket_connection_connect (G_SOCKET_CONNECTION (connection),
Packit ae235b
				       address, cancellable, &last_error))
Packit ae235b
	{
Packit ae235b
          g_socket_connection_set_cached_remote_address ((GSocketConnection*)connection, NULL);
Packit ae235b
	  g_socket_client_emit_event (client, G_SOCKET_CLIENT_CONNECTED, connectable, connection);
Packit ae235b
	}
Packit ae235b
      else
Packit ae235b
	{
Packit ae235b
	  clarify_connect_error (last_error, connectable, address);
Packit ae235b
	  g_object_unref (connection);
Packit ae235b
	  connection = NULL;
Packit ae235b
	}
Packit ae235b
Packit ae235b
      if (connection && using_proxy)
Packit ae235b
	{
Packit ae235b
	  GProxyAddress *proxy_addr = G_PROXY_ADDRESS (address);
Packit ae235b
	  const gchar *protocol;
Packit ae235b
	  GProxy *proxy;
Packit ae235b
Packit ae235b
	  protocol = g_proxy_address_get_protocol (proxy_addr);
Packit ae235b
Packit ae235b
          /* The connection should not be anything else then TCP Connection,
Packit ae235b
           * but let's put a safety guard in case
Packit ae235b
	   */
Packit ae235b
          if (!G_IS_TCP_CONNECTION (connection))
Packit ae235b
            {
Packit ae235b
              g_critical ("Trying to proxy over non-TCP connection, this is "
Packit ae235b
                          "most likely a bug in GLib IO library.");
Packit ae235b
Packit ae235b
              g_set_error_literal (&last_error,
Packit ae235b
                  G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
Packit ae235b
                  _("Proxying over a non-TCP connection is not supported."));
Packit ae235b
Packit ae235b
	      g_object_unref (connection);
Packit ae235b
	      connection = NULL;
Packit ae235b
            }
Packit ae235b
	  else if (g_hash_table_contains (client->priv->app_proxies, protocol))
Packit ae235b
	    {
Packit ae235b
	      application_proxy = TRUE;
Packit ae235b
	    }
Packit ae235b
          else if ((proxy = g_proxy_get_default_for_protocol (protocol)))
Packit ae235b
	    {
Packit ae235b
	      GIOStream *proxy_connection;
Packit ae235b
Packit ae235b
	      g_socket_client_emit_event (client, G_SOCKET_CLIENT_PROXY_NEGOTIATING, connectable, connection);
Packit ae235b
	      proxy_connection = g_proxy_connect (proxy,
Packit ae235b
						  connection,
Packit ae235b
						  proxy_addr,
Packit ae235b
						  cancellable,
Packit ae235b
						  &last_error);
Packit ae235b
	      g_object_unref (connection);
Packit ae235b
	      connection = proxy_connection;
Packit ae235b
	      g_object_unref (proxy);
Packit ae235b
Packit ae235b
	      if (connection)
Packit ae235b
		g_socket_client_emit_event (client, G_SOCKET_CLIENT_PROXY_NEGOTIATED, connectable, connection);
Packit ae235b
	    }
Packit ae235b
	  else
Packit ae235b
	    {
Packit ae235b
	      g_set_error (&last_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
Packit ae235b
			   _("Proxy protocol “%s” is not supported."),
Packit ae235b
			   protocol);
Packit ae235b
	      g_object_unref (connection);
Packit ae235b
	      connection = NULL;
Packit ae235b
	    }
Packit ae235b
	}
Packit ae235b
Packit ae235b
      if (!application_proxy && connection && client->priv->tls)
Packit ae235b
	{
Packit ae235b
	  GIOStream *tlsconn;
Packit ae235b
Packit ae235b
	  tlsconn = g_tls_client_connection_new (connection, connectable, &last_error);
Packit ae235b
	  g_object_unref (connection);
Packit ae235b
	  connection = tlsconn;
Packit ae235b
Packit ae235b
	  if (tlsconn)
Packit ae235b
	    {
Packit ae235b
	      g_tls_client_connection_set_validation_flags (G_TLS_CLIENT_CONNECTION (tlsconn),
Packit ae235b
                                                            client->priv->tls_validation_flags);
Packit ae235b
	      g_socket_client_emit_event (client, G_SOCKET_CLIENT_TLS_HANDSHAKING, connectable, connection);
Packit ae235b
	      if (g_tls_connection_handshake (G_TLS_CONNECTION (tlsconn),
Packit ae235b
					      cancellable, &last_error))
Packit ae235b
		{
Packit ae235b
		  g_socket_client_emit_event (client, G_SOCKET_CLIENT_TLS_HANDSHAKED, connectable, connection);
Packit ae235b
		}
Packit ae235b
	      else
Packit ae235b
		{
Packit ae235b
		  g_object_unref (tlsconn);
Packit ae235b
		  connection = NULL;
Packit ae235b
		}
Packit ae235b
	    }
Packit ae235b
	}
Packit ae235b
Packit ae235b
      if (connection && !G_IS_SOCKET_CONNECTION (connection))
Packit ae235b
	{
Packit ae235b
	  GSocketConnection *wrapper_connection;
Packit ae235b
Packit ae235b
	  wrapper_connection = g_tcp_wrapper_connection_new (connection, socket);
Packit ae235b
	  g_object_unref (connection);
Packit ae235b
	  connection = (GIOStream *)wrapper_connection;
Packit ae235b
	}
Packit ae235b
Packit ae235b
      g_object_unref (socket);
Packit ae235b
      g_object_unref (address);
Packit ae235b
    }
Packit ae235b
  g_object_unref (enumerator);
Packit ae235b
Packit ae235b
  g_socket_client_emit_event (client, G_SOCKET_CLIENT_COMPLETE, connectable, connection);
Packit ae235b
  return G_SOCKET_CONNECTION (connection);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_socket_client_connect_to_host:
Packit ae235b
 * @client: a #GSocketClient
Packit ae235b
 * @host_and_port: the name and optionally port of the host to connect to
Packit ae235b
 * @default_port: the default port to connect to
Packit ae235b
 * @cancellable: (nullable): a #GCancellable, or %NULL
Packit ae235b
 * @error: a pointer to a #GError, or %NULL
Packit ae235b
 *
Packit ae235b
 * This is a helper function for g_socket_client_connect().
Packit ae235b
 *
Packit ae235b
 * Attempts to create a TCP connection to the named host.
Packit ae235b
 *
Packit ae235b
 * @host_and_port may be in any of a number of recognized formats; an IPv6
Packit ae235b
 * address, an IPv4 address, or a domain name (in which case a DNS
Packit ae235b
 * lookup is performed).  Quoting with [] is supported for all address
Packit ae235b
 * types.  A port override may be specified in the usual way with a
Packit ae235b
 * colon.  Ports may be given as decimal numbers or symbolic names (in
Packit ae235b
 * which case an /etc/services lookup is performed).
Packit ae235b
 *
Packit ae235b
 * If no port override is given in @host_and_port then @default_port will be
Packit ae235b
 * used as the port number to connect to.
Packit ae235b
 *
Packit ae235b
 * In general, @host_and_port is expected to be provided by the user (allowing
Packit ae235b
 * them to give the hostname, and a port override if necessary) and
Packit ae235b
 * @default_port is expected to be provided by the application.
Packit ae235b
 *
Packit ae235b
 * In the case that an IP address is given, a single connection
Packit ae235b
 * attempt is made.  In the case that a name is given, multiple
Packit ae235b
 * connection attempts may be made, in turn and according to the
Packit ae235b
 * number of address records in DNS, until a connection succeeds.
Packit ae235b
 *
Packit ae235b
 * Upon a successful connection, a new #GSocketConnection is constructed
Packit ae235b
 * and returned.  The caller owns this new object and must drop their
Packit ae235b
 * reference to it when finished with it.
Packit ae235b
 *
Packit ae235b
 * In the event of any failure (DNS error, service not found, no hosts
Packit ae235b
 * connectable) %NULL is returned and @error (if non-%NULL) is set
Packit ae235b
 * accordingly.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
GSocketConnection *
Packit ae235b
g_socket_client_connect_to_host (GSocketClient  *client,
Packit ae235b
				 const gchar    *host_and_port,
Packit ae235b
				 guint16         default_port,
Packit ae235b
				 GCancellable   *cancellable,
Packit ae235b
				 GError        **error)
Packit ae235b
{
Packit ae235b
  GSocketConnectable *connectable;
Packit ae235b
  GSocketConnection *connection;
Packit ae235b
Packit ae235b
  connectable = g_network_address_parse (host_and_port, default_port, error);
Packit ae235b
  if (connectable == NULL)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  connection = g_socket_client_connect (client, connectable,
Packit ae235b
					cancellable, error);
Packit ae235b
  g_object_unref (connectable);
Packit ae235b
Packit ae235b
  return connection;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_socket_client_connect_to_service:
Packit ae235b
 * @client: a #GSocketConnection
Packit ae235b
 * @domain: a domain name
Packit ae235b
 * @service: the name of the service to connect to
Packit ae235b
 * @cancellable: (nullable): a #GCancellable, or %NULL
Packit ae235b
 * @error: a pointer to a #GError, or %NULL
Packit ae235b
 *
Packit ae235b
 * Attempts to create a TCP connection to a service.
Packit ae235b
 *
Packit ae235b
 * This call looks up the SRV record for @service at @domain for the
Packit ae235b
 * "tcp" protocol.  It then attempts to connect, in turn, to each of
Packit ae235b
 * the hosts providing the service until either a connection succeeds
Packit ae235b
 * or there are no hosts remaining.
Packit ae235b
 *
Packit ae235b
 * Upon a successful connection, a new #GSocketConnection is constructed
Packit ae235b
 * and returned.  The caller owns this new object and must drop their
Packit ae235b
 * reference to it when finished with it.
Packit ae235b
 *
Packit ae235b
 * In the event of any failure (DNS error, service not found, no hosts
Packit ae235b
 * connectable) %NULL is returned and @error (if non-%NULL) is set
Packit ae235b
 * accordingly.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a #GSocketConnection if successful, or %NULL on error
Packit ae235b
 */
Packit ae235b
GSocketConnection *
Packit ae235b
g_socket_client_connect_to_service (GSocketClient  *client,
Packit ae235b
				    const gchar    *domain,
Packit ae235b
				    const gchar    *service,
Packit ae235b
				    GCancellable   *cancellable,
Packit ae235b
				    GError        **error)
Packit ae235b
{
Packit ae235b
  GSocketConnectable *connectable;
Packit ae235b
  GSocketConnection *connection;
Packit ae235b
Packit ae235b
  connectable = g_network_service_new (service, "tcp", domain);
Packit ae235b
  connection = g_socket_client_connect (client, connectable,
Packit ae235b
					cancellable, error);
Packit ae235b
  g_object_unref (connectable);
Packit ae235b
Packit ae235b
  return connection;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_socket_client_connect_to_uri:
Packit ae235b
 * @client: a #GSocketClient
Packit ae235b
 * @uri: A network URI
Packit ae235b
 * @default_port: the default port to connect to
Packit ae235b
 * @cancellable: (nullable): a #GCancellable, or %NULL
Packit ae235b
 * @error: a pointer to a #GError, or %NULL
Packit ae235b
 *
Packit ae235b
 * This is a helper function for g_socket_client_connect().
Packit ae235b
 *
Packit ae235b
 * Attempts to create a TCP connection with a network URI.
Packit ae235b
 *
Packit ae235b
 * @uri may be any valid URI containing an "authority" (hostname/port)
Packit ae235b
 * component. If a port is not specified in the URI, @default_port
Packit ae235b
 * will be used. TLS will be negotiated if #GSocketClient:tls is %TRUE.
Packit ae235b
 * (#GSocketClient does not know to automatically assume TLS for
Packit ae235b
 * certain URI schemes.)
Packit ae235b
 *
Packit ae235b
 * Using this rather than g_socket_client_connect() or
Packit ae235b
 * g_socket_client_connect_to_host() allows #GSocketClient to
Packit ae235b
 * determine when to use application-specific proxy protocols.
Packit ae235b
 *
Packit ae235b
 * Upon a successful connection, a new #GSocketConnection is constructed
Packit ae235b
 * and returned.  The caller owns this new object and must drop their
Packit ae235b
 * reference to it when finished with it.
Packit ae235b
 *
Packit ae235b
 * In the event of any failure (DNS error, service not found, no hosts
Packit ae235b
 * connectable) %NULL is returned and @error (if non-%NULL) is set
Packit ae235b
 * accordingly.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
GSocketConnection *
Packit ae235b
g_socket_client_connect_to_uri (GSocketClient  *client,
Packit ae235b
				const gchar    *uri,
Packit ae235b
				guint16         default_port,
Packit ae235b
				GCancellable   *cancellable,
Packit ae235b
				GError        **error)
Packit ae235b
{
Packit ae235b
  GSocketConnectable *connectable;
Packit ae235b
  GSocketConnection *connection;
Packit ae235b
Packit ae235b
  connectable = g_network_address_parse_uri (uri, default_port, error);
Packit ae235b
  if (connectable == NULL)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  connection = g_socket_client_connect (client, connectable,
Packit ae235b
					cancellable, error);
Packit ae235b
  g_object_unref (connectable);
Packit ae235b
Packit ae235b
  return connection;
Packit ae235b
}
Packit ae235b
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  GTask *task;
Packit ae235b
  GSocketClient *client;
Packit ae235b
Packit ae235b
  GSocketConnectable *connectable;
Packit ae235b
  GSocketAddressEnumerator *enumerator;
Packit ae235b
  GProxyAddress *proxy_addr;
Packit ae235b
  GSocketAddress *current_addr;
Packit ae235b
  GSocket *current_socket;
Packit ae235b
  GIOStream *connection;
Packit ae235b
Packit ae235b
  GError *last_error;
Packit ae235b
} GSocketClientAsyncConnectData;
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_socket_client_async_connect_data_free (GSocketClientAsyncConnectData *data)
Packit ae235b
{
Packit ae235b
  g_clear_object (&data->connectable);
Packit ae235b
  g_clear_object (&data->enumerator);
Packit ae235b
  g_clear_object (&data->proxy_addr);
Packit ae235b
  g_clear_object (&data->current_addr);
Packit ae235b
  g_clear_object (&data->current_socket);
Packit ae235b
  g_clear_object (&data->connection);
Packit ae235b
Packit ae235b
  g_clear_error (&data->last_error);
Packit ae235b
Packit ae235b
  g_slice_free (GSocketClientAsyncConnectData, data);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_socket_client_async_connect_complete (GSocketClientAsyncConnectData *data)
Packit ae235b
{
Packit ae235b
  g_assert (data->connection);
Packit ae235b
Packit ae235b
  if (!G_IS_SOCKET_CONNECTION (data->connection))
Packit ae235b
    {
Packit ae235b
      GSocketConnection *wrapper_connection;
Packit ae235b
Packit ae235b
      wrapper_connection = g_tcp_wrapper_connection_new (data->connection,
Packit ae235b
							 data->current_socket);
Packit ae235b
      g_object_unref (data->connection);
Packit ae235b
      data->connection = (GIOStream *)wrapper_connection;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_COMPLETE, data->connectable, data->connection);
Packit ae235b
  g_task_return_pointer (data->task, data->connection, g_object_unref);
Packit ae235b
  data->connection = NULL;
Packit ae235b
  g_object_unref (data->task);
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_socket_client_enumerator_callback (GObject      *object,
Packit ae235b
				     GAsyncResult *result,
Packit ae235b
				     gpointer      user_data);
Packit ae235b
Packit ae235b
static void
Packit ae235b
set_last_error (GSocketClientAsyncConnectData *data,
Packit ae235b
		GError *error)
Packit ae235b
{
Packit ae235b
  g_clear_error (&data->last_error);
Packit ae235b
  data->last_error = error;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
enumerator_next_async (GSocketClientAsyncConnectData *data)
Packit ae235b
{
Packit ae235b
  /* We need to cleanup the state */
Packit ae235b
  g_clear_object (&data->current_socket);
Packit ae235b
  g_clear_object (&data->current_addr);
Packit ae235b
  g_clear_object (&data->proxy_addr);
Packit ae235b
  g_clear_object (&data->connection);
Packit ae235b
Packit ae235b
  g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_RESOLVING, data->connectable, NULL);
Packit ae235b
  g_socket_address_enumerator_next_async (data->enumerator,
Packit ae235b
					  g_task_get_cancellable (data->task),
Packit ae235b
					  g_socket_client_enumerator_callback,
Packit ae235b
					  data);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_socket_client_tls_handshake_callback (GObject      *object,
Packit ae235b
					GAsyncResult *result,
Packit ae235b
					gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GSocketClientAsyncConnectData *data = user_data;
Packit ae235b
Packit ae235b
  if (g_tls_connection_handshake_finish (G_TLS_CONNECTION (object),
Packit ae235b
					 result,
Packit ae235b
					 &data->last_error))
Packit ae235b
    {
Packit ae235b
      g_object_unref (data->connection);
Packit ae235b
      data->connection = G_IO_STREAM (object);
Packit ae235b
Packit ae235b
      g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_TLS_HANDSHAKED, data->connectable, data->connection);
Packit ae235b
      g_socket_client_async_connect_complete (data);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      g_object_unref (object);
Packit ae235b
      enumerator_next_async (data);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_socket_client_tls_handshake (GSocketClientAsyncConnectData *data)
Packit ae235b
{
Packit ae235b
  GIOStream *tlsconn;
Packit ae235b
Packit ae235b
  if (!data->client->priv->tls)
Packit ae235b
    {
Packit ae235b
      g_socket_client_async_connect_complete (data);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  tlsconn = g_tls_client_connection_new (data->connection,
Packit ae235b
					 data->connectable,
Packit ae235b
					 &data->last_error);
Packit ae235b
  if (tlsconn)
Packit ae235b
    {
Packit ae235b
      g_tls_client_connection_set_validation_flags (G_TLS_CLIENT_CONNECTION (tlsconn),
Packit ae235b
                                                    data->client->priv->tls_validation_flags);
Packit ae235b
      g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_TLS_HANDSHAKING, data->connectable, G_IO_STREAM (tlsconn));
Packit ae235b
      g_tls_connection_handshake_async (G_TLS_CONNECTION (tlsconn),
Packit ae235b
					G_PRIORITY_DEFAULT,
Packit ae235b
					g_task_get_cancellable (data->task),
Packit ae235b
					g_socket_client_tls_handshake_callback,
Packit ae235b
					data);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      enumerator_next_async (data);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_socket_client_proxy_connect_callback (GObject      *object,
Packit ae235b
					GAsyncResult *result,
Packit ae235b
					gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GSocketClientAsyncConnectData *data = user_data;
Packit ae235b
Packit ae235b
  g_object_unref (data->connection);
Packit ae235b
  data->connection = g_proxy_connect_finish (G_PROXY (object),
Packit ae235b
					     result,
Packit ae235b
					     &data->last_error);
Packit ae235b
  if (data->connection)
Packit ae235b
    {
Packit ae235b
      g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_PROXY_NEGOTIATED, data->connectable, data->connection);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      enumerator_next_async (data);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_socket_client_tls_handshake (data);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_socket_client_connected_callback (GObject      *source,
Packit ae235b
				    GAsyncResult *result,
Packit ae235b
				    gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GSocketClientAsyncConnectData *data = user_data;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GProxy *proxy;
Packit ae235b
  const gchar *protocol;
Packit ae235b
Packit ae235b
  if (g_task_return_error_if_cancelled (data->task))
Packit ae235b
    {
Packit ae235b
      g_object_unref (data->task);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!g_socket_connection_connect_finish (G_SOCKET_CONNECTION (source),
Packit ae235b
					   result, &error))
Packit ae235b
    {
Packit ae235b
      clarify_connect_error (error, data->connectable,
Packit ae235b
			     data->current_addr);
Packit ae235b
      set_last_error (data, error);
Packit ae235b
Packit ae235b
      /* try next one */
Packit ae235b
      enumerator_next_async (data);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_socket_connection_set_cached_remote_address ((GSocketConnection*)data->connection, NULL);
Packit ae235b
  g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_CONNECTED, data->connectable, data->connection);
Packit ae235b
Packit ae235b
  /* wrong, but backward compatible */
Packit ae235b
  g_socket_set_blocking (data->current_socket, TRUE);
Packit ae235b
Packit ae235b
  if (!data->proxy_addr)
Packit ae235b
    {
Packit ae235b
      g_socket_client_tls_handshake (data);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  protocol = g_proxy_address_get_protocol (data->proxy_addr);
Packit ae235b
Packit ae235b
  /* The connection should not be anything other than TCP,
Packit ae235b
   * but let's put a safety guard in case
Packit ae235b
   */
Packit ae235b
  if (!G_IS_TCP_CONNECTION (data->connection))
Packit ae235b
    {
Packit ae235b
      g_critical ("Trying to proxy over non-TCP connection, this is "
Packit ae235b
          "most likely a bug in GLib IO library.");
Packit ae235b
Packit ae235b
      g_set_error_literal (&data->last_error,
Packit ae235b
          G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
Packit ae235b
          _("Proxying over a non-TCP connection is not supported."));
Packit ae235b
Packit ae235b
      enumerator_next_async (data);
Packit ae235b
    }
Packit ae235b
  else if (g_hash_table_contains (data->client->priv->app_proxies, protocol))
Packit ae235b
    {
Packit ae235b
      /* Simply complete the connection, we don't want to do TLS handshake
Packit ae235b
       * as the application proxy handling may need proxy handshake first */
Packit ae235b
      g_socket_client_async_connect_complete (data);
Packit ae235b
    }
Packit ae235b
  else if ((proxy = g_proxy_get_default_for_protocol (protocol)))
Packit ae235b
    {
Packit ae235b
      g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_PROXY_NEGOTIATING, data->connectable, data->connection);
Packit ae235b
      g_proxy_connect_async (proxy,
Packit ae235b
                             data->connection,
Packit ae235b
                             data->proxy_addr,
Packit ae235b
                             g_task_get_cancellable (data->task),
Packit ae235b
                             g_socket_client_proxy_connect_callback,
Packit ae235b
                             data);
Packit ae235b
      g_object_unref (proxy);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      g_clear_error (&data->last_error);
Packit ae235b
Packit ae235b
      g_set_error (&data->last_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
Packit ae235b
          _("Proxy protocol “%s” is not supported."),
Packit ae235b
          protocol);
Packit ae235b
Packit ae235b
      enumerator_next_async (data);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_socket_client_enumerator_callback (GObject      *object,
Packit ae235b
				     GAsyncResult *result,
Packit ae235b
				     gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GSocketClientAsyncConnectData *data = user_data;
Packit ae235b
  GSocketAddress *address = NULL;
Packit ae235b
  GSocket *socket;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  if (g_task_return_error_if_cancelled (data->task))
Packit ae235b
    {
Packit ae235b
      g_object_unref (data->task);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  address = g_socket_address_enumerator_next_finish (data->enumerator,
Packit ae235b
						     result, &error);
Packit ae235b
  if (address == NULL)
Packit ae235b
    {
Packit ae235b
      g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_COMPLETE, data->connectable, NULL);
Packit ae235b
      if (!error)
Packit ae235b
	{
Packit ae235b
	  if (data->last_error)
Packit ae235b
	    {
Packit ae235b
	      error = data->last_error;
Packit ae235b
	      data->last_error = NULL;
Packit ae235b
	    }
Packit ae235b
	  else
Packit ae235b
	    {
Packit ae235b
	      g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_FAILED,
Packit ae235b
				   _("Unknown error on connect"));
Packit ae235b
	    }
Packit ae235b
	}
Packit ae235b
      g_task_return_error (data->task, error);
Packit ae235b
      g_object_unref (data->task);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_RESOLVED,
Packit ae235b
			      data->connectable, NULL);
Packit ae235b
Packit ae235b
  if (G_IS_PROXY_ADDRESS (address) &&
Packit ae235b
      data->client->priv->enable_proxy)
Packit ae235b
    data->proxy_addr = g_object_ref (G_PROXY_ADDRESS (address));
Packit ae235b
Packit ae235b
  g_clear_error (&data->last_error);
Packit ae235b
Packit ae235b
  socket = create_socket (data->client, address, &data->last_error);
Packit ae235b
  if (socket == NULL)
Packit ae235b
    {
Packit ae235b
      g_object_unref (address);
Packit ae235b
      enumerator_next_async (data);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  data->current_socket = socket;
Packit ae235b
  data->current_addr = address;
Packit ae235b
  data->connection = (GIOStream *) g_socket_connection_factory_create_connection (socket);
Packit ae235b
Packit ae235b
  g_socket_connection_set_cached_remote_address ((GSocketConnection*)data->connection, address);
Packit ae235b
  g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_CONNECTING, data->connectable, data->connection);
Packit ae235b
  g_socket_connection_connect_async (G_SOCKET_CONNECTION (data->connection),
Packit ae235b
				     address,
Packit ae235b
				     g_task_get_cancellable (data->task),
Packit ae235b
				     g_socket_client_connected_callback, data);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_socket_client_connect_async:
Packit ae235b
 * @client: a #GSocketClient
Packit ae235b
 * @connectable: a #GSocketConnectable specifying the remote address.
Packit ae235b
 * @cancellable: (nullable): a #GCancellable, or %NULL
Packit ae235b
 * @callback: (scope async): a #GAsyncReadyCallback
Packit ae235b
 * @user_data: (closure): user data for the callback
Packit ae235b
 *
Packit ae235b
 * This is the asynchronous version of g_socket_client_connect().
Packit ae235b
 *
Packit ae235b
 * When the operation is finished @callback will be
Packit ae235b
 * called. You can then call g_socket_client_connect_finish() to get
Packit ae235b
 * the result of the operation.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_socket_client_connect_async (GSocketClient       *client,
Packit ae235b
			       GSocketConnectable  *connectable,
Packit ae235b
			       GCancellable        *cancellable,
Packit ae235b
			       GAsyncReadyCallback  callback,
Packit ae235b
			       gpointer             user_data)
Packit ae235b
{
Packit ae235b
  GSocketClientAsyncConnectData *data;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_SOCKET_CLIENT (client));
Packit ae235b
Packit ae235b
  data = g_slice_new0 (GSocketClientAsyncConnectData);
Packit ae235b
  data->client = client;
Packit ae235b
  data->connectable = g_object_ref (connectable);
Packit ae235b
Packit ae235b
  if (can_use_proxy (client))
Packit ae235b
    {
Packit ae235b
      data->enumerator = g_socket_connectable_proxy_enumerate (connectable);
Packit ae235b
      if (client->priv->proxy_resolver &&
Packit ae235b
          G_IS_PROXY_ADDRESS_ENUMERATOR (data->enumerator))
Packit ae235b
        {
Packit ae235b
          g_object_set (G_OBJECT (data->enumerator),
Packit ae235b
                        "proxy-resolver", client->priv->proxy_resolver,
Packit ae235b
                        NULL);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    data->enumerator = g_socket_connectable_enumerate (connectable);
Packit ae235b
Packit ae235b
  data->task = g_task_new (client, cancellable, callback, user_data);
Packit ae235b
  g_task_set_source_tag (data->task, g_socket_client_connect_async);
Packit ae235b
  g_task_set_task_data (data->task, data, (GDestroyNotify)g_socket_client_async_connect_data_free);
Packit ae235b
Packit ae235b
  enumerator_next_async (data);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_socket_client_connect_to_host_async:
Packit ae235b
 * @client: a #GSocketClient
Packit ae235b
 * @host_and_port: the name and optionally the port of the host to connect to
Packit ae235b
 * @default_port: the default port to connect to
Packit ae235b
 * @cancellable: (nullable): a #GCancellable, or %NULL
Packit ae235b
 * @callback: (scope async): a #GAsyncReadyCallback
Packit ae235b
 * @user_data: (closure): user data for the callback
Packit ae235b
 *
Packit ae235b
 * This is the asynchronous version of g_socket_client_connect_to_host().
Packit ae235b
 *
Packit ae235b
 * When the operation is finished @callback will be
Packit ae235b
 * called. You can then call g_socket_client_connect_to_host_finish() to get
Packit ae235b
 * the result of the operation.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_socket_client_connect_to_host_async (GSocketClient        *client,
Packit ae235b
				       const gchar          *host_and_port,
Packit ae235b
				       guint16               default_port,
Packit ae235b
				       GCancellable         *cancellable,
Packit ae235b
				       GAsyncReadyCallback   callback,
Packit ae235b
				       gpointer              user_data)
Packit ae235b
{
Packit ae235b
  GSocketConnectable *connectable;
Packit ae235b
  GError *error;
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  connectable = g_network_address_parse (host_and_port, default_port,
Packit ae235b
					 &error);
Packit ae235b
  if (connectable == NULL)
Packit ae235b
    {
Packit ae235b
      g_task_report_error (client, callback, user_data,
Packit ae235b
                           g_socket_client_connect_to_host_async,
Packit ae235b
                           error);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      g_socket_client_connect_async (client,
Packit ae235b
				     connectable, cancellable,
Packit ae235b
				     callback, user_data);
Packit ae235b
      g_object_unref (connectable);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_socket_client_connect_to_service_async:
Packit ae235b
 * @client: a #GSocketClient
Packit ae235b
 * @domain: a domain name
Packit ae235b
 * @service: the name of the service to connect to
Packit ae235b
 * @cancellable: (nullable): a #GCancellable, or %NULL
Packit ae235b
 * @callback: (scope async): a #GAsyncReadyCallback
Packit ae235b
 * @user_data: (closure): user data for the callback
Packit ae235b
 *
Packit ae235b
 * This is the asynchronous version of
Packit ae235b
 * g_socket_client_connect_to_service().
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_socket_client_connect_to_service_async (GSocketClient       *client,
Packit ae235b
					  const gchar         *domain,
Packit ae235b
					  const gchar         *service,
Packit ae235b
					  GCancellable        *cancellable,
Packit ae235b
					  GAsyncReadyCallback  callback,
Packit ae235b
					  gpointer             user_data)
Packit ae235b
{
Packit ae235b
  GSocketConnectable *connectable;
Packit ae235b
Packit ae235b
  connectable = g_network_service_new (service, "tcp", domain);
Packit ae235b
  g_socket_client_connect_async (client,
Packit ae235b
				 connectable, cancellable,
Packit ae235b
				 callback, user_data);
Packit ae235b
  g_object_unref (connectable);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_socket_client_connect_to_uri_async:
Packit ae235b
 * @client: a #GSocketClient
Packit ae235b
 * @uri: a network uri
Packit ae235b
 * @default_port: the default port to connect to
Packit ae235b
 * @cancellable: (nullable): a #GCancellable, or %NULL
Packit ae235b
 * @callback: (scope async): a #GAsyncReadyCallback
Packit ae235b
 * @user_data: (closure): user data for the callback
Packit ae235b
 *
Packit ae235b
 * This is the asynchronous version of g_socket_client_connect_to_uri().
Packit ae235b
 *
Packit ae235b
 * When the operation is finished @callback will be
Packit ae235b
 * called. You can then call g_socket_client_connect_to_uri_finish() to get
Packit ae235b
 * the result of the operation.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_socket_client_connect_to_uri_async (GSocketClient        *client,
Packit ae235b
				      const gchar          *uri,
Packit ae235b
				      guint16               default_port,
Packit ae235b
				      GCancellable         *cancellable,
Packit ae235b
				      GAsyncReadyCallback   callback,
Packit ae235b
				      gpointer              user_data)
Packit ae235b
{
Packit ae235b
  GSocketConnectable *connectable;
Packit ae235b
  GError *error;
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  connectable = g_network_address_parse_uri (uri, default_port, &error);
Packit ae235b
  if (connectable == NULL)
Packit ae235b
    {
Packit ae235b
      g_task_report_error (client, callback, user_data,
Packit ae235b
                           g_socket_client_connect_to_uri_async,
Packit ae235b
                           error);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      g_socket_client_connect_async (client,
Packit ae235b
				     connectable, cancellable,
Packit ae235b
				     callback, user_data);
Packit ae235b
      g_object_unref (connectable);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_socket_client_connect_finish:
Packit ae235b
 * @client: a #GSocketClient.
Packit ae235b
 * @result: a #GAsyncResult.
Packit ae235b
 * @error: a #GError location to store the error occurring, or %NULL to
Packit ae235b
 * ignore.
Packit ae235b
 *
Packit ae235b
 * Finishes an async connect operation. See g_socket_client_connect_async()
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
GSocketConnection *
Packit ae235b
g_socket_client_connect_finish (GSocketClient  *client,
Packit ae235b
				GAsyncResult   *result,
Packit ae235b
				GError        **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (g_task_is_valid (result, client), NULL);
Packit ae235b
Packit ae235b
  return g_task_propagate_pointer (G_TASK (result), error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_socket_client_connect_to_host_finish:
Packit ae235b
 * @client: a #GSocketClient.
Packit ae235b
 * @result: a #GAsyncResult.
Packit ae235b
 * @error: a #GError location to store the error occurring, or %NULL to
Packit ae235b
 * ignore.
Packit ae235b
 *
Packit ae235b
 * Finishes an async connect operation. See g_socket_client_connect_to_host_async()
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
GSocketConnection *
Packit ae235b
g_socket_client_connect_to_host_finish (GSocketClient  *client,
Packit ae235b
					GAsyncResult   *result,
Packit ae235b
					GError        **error)
Packit ae235b
{
Packit ae235b
  return g_socket_client_connect_finish (client, result, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_socket_client_connect_to_service_finish:
Packit ae235b
 * @client: a #GSocketClient.
Packit ae235b
 * @result: a #GAsyncResult.
Packit ae235b
 * @error: a #GError location to store the error occurring, or %NULL to
Packit ae235b
 * ignore.
Packit ae235b
 *
Packit ae235b
 * Finishes an async connect operation. See g_socket_client_connect_to_service_async()
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
GSocketConnection *
Packit ae235b
g_socket_client_connect_to_service_finish (GSocketClient  *client,
Packit ae235b
					   GAsyncResult   *result,
Packit ae235b
					   GError        **error)
Packit ae235b
{
Packit ae235b
  return g_socket_client_connect_finish (client, result, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_socket_client_connect_to_uri_finish:
Packit ae235b
 * @client: a #GSocketClient.
Packit ae235b
 * @result: a #GAsyncResult.
Packit ae235b
 * @error: a #GError location to store the error occurring, or %NULL to
Packit ae235b
 * ignore.
Packit ae235b
 *
Packit ae235b
 * Finishes an async connect operation. See g_socket_client_connect_to_uri_async()
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
GSocketConnection *
Packit ae235b
g_socket_client_connect_to_uri_finish (GSocketClient  *client,
Packit ae235b
				       GAsyncResult   *result,
Packit ae235b
				       GError        **error)
Packit ae235b
{
Packit ae235b
  return g_socket_client_connect_finish (client, result, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_socket_client_add_application_proxy:
Packit ae235b
 * @client: a #GSocketClient
Packit ae235b
 * @protocol: The proxy protocol
Packit ae235b
 *
Packit ae235b
 * Enable proxy protocols to be handled by the application. When the
Packit ae235b
 * indicated proxy protocol is returned by the #GProxyResolver,
Packit ae235b
 * #GSocketClient will consider this protocol as supported but will
Packit ae235b
 * not try to find a #GProxy instance to handle handshaking. The
Packit ae235b
 * application must check for this case by calling
Packit ae235b
 * g_socket_connection_get_remote_address() on the returned
Packit ae235b
 * #GSocketConnection, and seeing if it's a #GProxyAddress of the
Packit ae235b
 * appropriate type, to determine whether or not it needs to handle
Packit ae235b
 * the proxy handshaking itself.
Packit ae235b
 *
Packit ae235b
 * This should be used for proxy protocols that are dialects of
Packit ae235b
 * another protocol such as HTTP proxy. It also allows cohabitation of
Packit ae235b
 * proxy protocols that are reused between protocols. A good example
Packit ae235b
 * is HTTP. It can be used to proxy HTTP, FTP and Gopher and can also
Packit ae235b
 * be use as generic socket proxy through the HTTP CONNECT method.
Packit ae235b
 *
Packit ae235b
 * When the proxy is detected as being an application proxy, TLS handshake
Packit ae235b
 * will be skipped. This is required to let the application do the proxy
Packit ae235b
 * specific handshake.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_socket_client_add_application_proxy (GSocketClient *client,
Packit ae235b
			               const gchar   *protocol)
Packit ae235b
{
Packit ae235b
  g_hash_table_add (client->priv->app_proxies, g_strdup (protocol));
Packit ae235b
}