2009-05-15 21:26:24 +02:00
|
|
|
/* GIO - GLib Input, Output and Streaming Library
|
|
|
|
*
|
|
|
|
* Copyright © 2008, 2009 codethink
|
|
|
|
* Copyright © 2009 Red Hat, Inc
|
2018-10-29 14:53:07 +01:00
|
|
|
* Copyright © 2018 Igalia S.L.
|
2009-05-15 21:26:24 +02:00
|
|
|
*
|
2022-05-18 10:12:45 +02:00
|
|
|
* SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
*
|
2009-05-15 21:26:24 +02:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2017-05-27 18:21:30 +02:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2009-05-15 21:26:24 +02:00
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General
|
2014-01-23 12:58:29 +01:00
|
|
|
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
2009-05-15 21:26:24 +02:00
|
|
|
*
|
|
|
|
* Authors: Ryan Lortie <desrt@desrt.ca>
|
|
|
|
* Alexander Larsson <alexl@redhat.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "gsocketclient.h"
|
|
|
|
|
2020-07-29 18:10:08 +02:00
|
|
|
#ifndef G_OS_WIN32
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#endif
|
|
|
|
|
2009-05-15 21:26:24 +02:00
|
|
|
#include <stdlib.h>
|
2010-08-10 22:48:45 +02:00
|
|
|
#include <string.h>
|
2009-05-15 21:26:24 +02:00
|
|
|
|
|
|
|
#include <gio/gioenumtypes.h>
|
|
|
|
#include <gio/gsocketaddressenumerator.h>
|
|
|
|
#include <gio/gsocketconnectable.h>
|
|
|
|
#include <gio/gsocketconnection.h>
|
2013-11-18 22:15:34 +01:00
|
|
|
#include <gio/gioprivate.h>
|
2010-08-10 22:48:45 +02:00
|
|
|
#include <gio/gproxyaddressenumerator.h>
|
|
|
|
#include <gio/gproxyaddress.h>
|
2012-08-02 21:48:22 +02:00
|
|
|
#include <gio/gtask.h>
|
2009-05-15 21:26:24 +02:00
|
|
|
#include <gio/gcancellable.h>
|
|
|
|
#include <gio/gioerror.h>
|
|
|
|
#include <gio/gsocket.h>
|
|
|
|
#include <gio/gnetworkaddress.h>
|
2020-07-29 18:10:08 +02:00
|
|
|
#include <gio/gnetworking.h>
|
2009-05-20 12:41:50 +02:00
|
|
|
#include <gio/gnetworkservice.h>
|
2010-08-10 22:48:45 +02:00
|
|
|
#include <gio/gproxy.h>
|
2013-01-27 19:53:36 +01:00
|
|
|
#include <gio/gproxyresolver.h>
|
2009-05-15 21:26:24 +02:00
|
|
|
#include <gio/gsocketaddress.h>
|
2010-08-10 22:48:45 +02:00
|
|
|
#include <gio/gtcpconnection.h>
|
make GProxyConnection public, as GTcpWrapperConnection
GProxyConnection is a class that was added for proxy support;
g_socket_client_connect() returns a GSocketConnection, but in some
cases (eg, encrypted SOCKS), GProxy might return a GIOStream that is
not a GSocketConnection. In that case, GSocketClient would wrap the
stream up in a GProxyConnection, which is a subclass of
GSocketConnection but uses the input/output streams of the wrapped
connection.
GTlsConnection is not a GSocketConnection, so it has the same problem,
so it will need the same treatment. Rename the class to
GTcpWrapperStream, and make it public, so people can extract the base
stream from it when necessary.
(This is not ideal and GSocketClient will need to be revisited as an
API at some point...)
https://bugzilla.gnome.org/show_bug.cgi?id=588189
2010-11-02 01:22:24 +01:00
|
|
|
#include <gio/gtcpwrapperconnection.h>
|
2009-12-21 20:50:32 +01:00
|
|
|
#include <gio/gtlscertificate.h>
|
|
|
|
#include <gio/gtlsclientconnection.h>
|
2011-11-15 01:46:02 +01:00
|
|
|
#include <gio/ginetaddress.h>
|
2009-05-15 21:26:24 +02:00
|
|
|
#include "glibintl.h"
|
2019-05-31 04:29:18 +02:00
|
|
|
#include "gmarshal-internal.h"
|
2009-05-15 21:26:24 +02:00
|
|
|
|
2018-10-29 14:53:07 +01:00
|
|
|
/* As recommended by RFC 8305 this is the time it waits
|
|
|
|
* on a connection before starting another concurrent attempt.
|
|
|
|
*/
|
|
|
|
#define HAPPY_EYEBALLS_CONNECTION_ATTEMPT_TIMEOUT_MS 250
|
2009-05-15 21:26:24 +02:00
|
|
|
|
|
|
|
/**
|
2023-10-24 11:58:10 +02:00
|
|
|
* GSocketClient:
|
2009-05-15 21:26:24 +02:00
|
|
|
*
|
2023-10-24 11:58:10 +02:00
|
|
|
* `GSocketClient` is a lightweight high-level utility class for connecting to
|
2012-02-01 16:25:01 +01:00
|
|
|
* a network host using a connection oriented socket type.
|
2009-05-15 21:26:24 +02:00
|
|
|
*
|
2023-10-24 11:58:10 +02:00
|
|
|
* You create a `GSocketClient` object, set any options you want, and then
|
|
|
|
* call a sync or async connect operation, which returns a
|
|
|
|
* [class@Gio.SocketConnection] subclass on success.
|
2009-05-15 21:26:24 +02:00
|
|
|
*
|
2023-10-24 11:58:10 +02:00
|
|
|
* The type of the [class@Gio.SocketConnection] object returned depends on the
|
|
|
|
* type of the underlying socket that is in use. For instance, for a TCP/IP
|
|
|
|
* connection it will be a [class@Gio.TcpConnection].
|
2009-05-15 21:26:24 +02:00
|
|
|
*
|
2023-10-24 11:58:10 +02:00
|
|
|
* As `GSocketClient` is a lightweight object, you don't need to cache it. You
|
2012-02-01 16:25:01 +01:00
|
|
|
* can just create a new one any time you need one.
|
|
|
|
*
|
2009-05-15 21:26:24 +02:00
|
|
|
* Since: 2.22
|
2009-05-28 00:20:08 +02:00
|
|
|
*/
|
2009-05-15 21:26:24 +02:00
|
|
|
|
|
|
|
|
2010-08-20 19:04:19 +02:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
EVENT,
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
|
|
|
static guint signals[LAST_SIGNAL] = { 0 };
|
|
|
|
|
2009-05-15 21:26:24 +02:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_NONE,
|
|
|
|
PROP_FAMILY,
|
|
|
|
PROP_TYPE,
|
|
|
|
PROP_PROTOCOL,
|
2010-08-14 21:04:24 +02:00
|
|
|
PROP_LOCAL_ADDRESS,
|
2010-08-10 22:48:45 +02:00
|
|
|
PROP_TIMEOUT,
|
|
|
|
PROP_ENABLE_PROXY,
|
2009-12-21 20:50:32 +01:00
|
|
|
PROP_TLS,
|
2013-01-27 19:53:36 +01:00
|
|
|
PROP_TLS_VALIDATION_FLAGS,
|
|
|
|
PROP_PROXY_RESOLVER
|
2009-05-15 21:26:24 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _GSocketClientPrivate
|
|
|
|
{
|
|
|
|
GSocketFamily family;
|
|
|
|
GSocketType type;
|
2009-05-20 12:01:29 +02:00
|
|
|
GSocketProtocol protocol;
|
2009-05-15 21:26:24 +02:00
|
|
|
GSocketAddress *local_address;
|
2010-08-14 21:04:24 +02:00
|
|
|
guint timeout;
|
2010-08-10 22:48:45 +02:00
|
|
|
gboolean enable_proxy;
|
2010-08-19 22:23:50 +02:00
|
|
|
GHashTable *app_proxies;
|
2009-12-21 20:50:32 +01:00
|
|
|
gboolean tls;
|
|
|
|
GTlsCertificateFlags tls_validation_flags;
|
2013-01-27 19:53:36 +01:00
|
|
|
GProxyResolver *proxy_resolver;
|
2009-05-15 21:26:24 +02:00
|
|
|
};
|
|
|
|
|
2013-06-11 01:29:58 +02:00
|
|
|
G_DEFINE_TYPE_WITH_PRIVATE (GSocketClient, g_socket_client, G_TYPE_OBJECT)
|
|
|
|
|
2009-05-15 21:26:24 +02:00
|
|
|
static GSocket *
|
|
|
|
create_socket (GSocketClient *client,
|
|
|
|
GSocketAddress *dest_address,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
GSocketFamily family;
|
|
|
|
GSocket *socket;
|
|
|
|
|
|
|
|
family = client->priv->family;
|
|
|
|
if (family == G_SOCKET_FAMILY_INVALID &&
|
|
|
|
client->priv->local_address != NULL)
|
|
|
|
family = g_socket_address_get_family (client->priv->local_address);
|
|
|
|
if (family == G_SOCKET_FAMILY_INVALID)
|
|
|
|
family = g_socket_address_get_family (dest_address);
|
|
|
|
|
|
|
|
socket = g_socket_new (family,
|
|
|
|
client->priv->type,
|
2009-05-20 12:01:29 +02:00
|
|
|
client->priv->protocol,
|
2009-05-15 21:26:24 +02:00
|
|
|
error);
|
|
|
|
if (socket == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (client->priv->local_address)
|
|
|
|
{
|
2020-07-29 18:10:08 +02:00
|
|
|
#ifdef IP_BIND_ADDRESS_NO_PORT
|
|
|
|
g_socket_set_option (socket, IPPROTO_IP, IP_BIND_ADDRESS_NO_PORT, 1, NULL);
|
|
|
|
#endif
|
|
|
|
|
2009-05-15 21:26:24 +02:00
|
|
|
if (!g_socket_bind (socket,
|
|
|
|
client->priv->local_address,
|
|
|
|
FALSE,
|
|
|
|
error))
|
|
|
|
{
|
|
|
|
g_object_unref (socket);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-14 21:04:24 +02:00
|
|
|
if (client->priv->timeout)
|
|
|
|
g_socket_set_timeout (socket, client->priv->timeout);
|
|
|
|
|
2009-05-15 21:26:24 +02:00
|
|
|
return socket;
|
|
|
|
}
|
|
|
|
|
2011-10-17 03:24:45 +02:00
|
|
|
static gboolean
|
2010-08-10 22:48:45 +02:00
|
|
|
can_use_proxy (GSocketClient *client)
|
|
|
|
{
|
|
|
|
GSocketClientPrivate *priv = client->priv;
|
|
|
|
|
|
|
|
return priv->enable_proxy
|
|
|
|
&& priv->type == G_SOCKET_TYPE_STREAM;
|
|
|
|
}
|
|
|
|
|
2011-11-15 01:46:02 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2009-05-15 21:26:24 +02:00
|
|
|
static void
|
|
|
|
g_socket_client_init (GSocketClient *client)
|
|
|
|
{
|
2013-06-24 16:43:04 +02:00
|
|
|
client->priv = g_socket_client_get_instance_private (client);
|
2009-05-15 21:26:24 +02:00
|
|
|
client->priv->type = G_SOCKET_TYPE_STREAM;
|
2010-08-19 22:23:50 +02:00
|
|
|
client->priv->app_proxies = g_hash_table_new_full (g_str_hash,
|
|
|
|
g_str_equal,
|
|
|
|
g_free,
|
|
|
|
NULL);
|
2009-05-15 21:26:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_socket_client_new:
|
|
|
|
*
|
|
|
|
* Creates a new #GSocketClient with the default options.
|
|
|
|
*
|
|
|
|
* Returns: a #GSocketClient.
|
|
|
|
* Free the returned object with g_object_unref().
|
|
|
|
*
|
|
|
|
* Since: 2.22
|
2009-05-28 00:20:08 +02:00
|
|
|
*/
|
2009-05-15 21:26:24 +02:00
|
|
|
GSocketClient *
|
|
|
|
g_socket_client_new (void)
|
|
|
|
{
|
|
|
|
return g_object_new (G_TYPE_SOCKET_CLIENT, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_socket_client_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
GSocketClient *client = G_SOCKET_CLIENT (object);
|
|
|
|
|
2013-01-27 19:53:36 +01:00
|
|
|
g_clear_object (&client->priv->local_address);
|
|
|
|
g_clear_object (&client->priv->proxy_resolver);
|
2009-05-15 21:26:24 +02:00
|
|
|
|
2013-06-11 01:29:58 +02:00
|
|
|
G_OBJECT_CLASS (g_socket_client_parent_class)->finalize (object);
|
2010-08-19 22:23:50 +02:00
|
|
|
|
|
|
|
g_hash_table_unref (client->priv->app_proxies);
|
2009-05-15 21:26:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_socket_client_get_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GSocketClient *client = G_SOCKET_CLIENT (object);
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_FAMILY:
|
|
|
|
g_value_set_enum (value, client->priv->family);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_TYPE:
|
|
|
|
g_value_set_enum (value, client->priv->type);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_PROTOCOL:
|
2009-05-20 12:01:29 +02:00
|
|
|
g_value_set_enum (value, client->priv->protocol);
|
2009-05-15 21:26:24 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_LOCAL_ADDRESS:
|
|
|
|
g_value_set_object (value, client->priv->local_address);
|
|
|
|
break;
|
|
|
|
|
2010-08-14 21:04:24 +02:00
|
|
|
case PROP_TIMEOUT:
|
|
|
|
g_value_set_uint (value, client->priv->timeout);
|
|
|
|
break;
|
|
|
|
|
2010-08-10 22:48:45 +02:00
|
|
|
case PROP_ENABLE_PROXY:
|
|
|
|
g_value_set_boolean (value, client->priv->enable_proxy);
|
|
|
|
break;
|
|
|
|
|
2009-12-21 20:50:32 +01:00
|
|
|
case PROP_TLS:
|
|
|
|
g_value_set_boolean (value, g_socket_client_get_tls (client));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_TLS_VALIDATION_FLAGS:
|
2021-08-04 22:20:41 +02:00
|
|
|
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
|
2009-12-21 20:50:32 +01:00
|
|
|
g_value_set_flags (value, g_socket_client_get_tls_validation_flags (client));
|
2021-08-04 22:20:41 +02:00
|
|
|
G_GNUC_END_IGNORE_DEPRECATIONS
|
2009-12-21 20:50:32 +01:00
|
|
|
break;
|
|
|
|
|
2013-01-27 19:53:36 +01:00
|
|
|
case PROP_PROXY_RESOLVER:
|
|
|
|
g_value_set_object (value, g_socket_client_get_proxy_resolver (client));
|
|
|
|
break;
|
|
|
|
|
2009-05-15 21:26:24 +02:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_socket_client_set_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GSocketClient *client = G_SOCKET_CLIENT (object);
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_FAMILY:
|
|
|
|
g_socket_client_set_family (client, g_value_get_enum (value));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_TYPE:
|
|
|
|
g_socket_client_set_socket_type (client, g_value_get_enum (value));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_PROTOCOL:
|
2009-05-20 12:01:29 +02:00
|
|
|
g_socket_client_set_protocol (client, g_value_get_enum (value));
|
2009-05-15 21:26:24 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_LOCAL_ADDRESS:
|
|
|
|
g_socket_client_set_local_address (client, g_value_get_object (value));
|
|
|
|
break;
|
|
|
|
|
2010-08-14 21:04:24 +02:00
|
|
|
case PROP_TIMEOUT:
|
|
|
|
g_socket_client_set_timeout (client, g_value_get_uint (value));
|
|
|
|
break;
|
|
|
|
|
2010-08-10 22:48:45 +02:00
|
|
|
case PROP_ENABLE_PROXY:
|
|
|
|
g_socket_client_set_enable_proxy (client, g_value_get_boolean (value));
|
|
|
|
break;
|
|
|
|
|
2009-12-21 20:50:32 +01:00
|
|
|
case PROP_TLS:
|
|
|
|
g_socket_client_set_tls (client, g_value_get_boolean (value));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_TLS_VALIDATION_FLAGS:
|
2021-08-04 22:20:41 +02:00
|
|
|
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
|
2009-12-21 20:50:32 +01:00
|
|
|
g_socket_client_set_tls_validation_flags (client, g_value_get_flags (value));
|
2021-08-04 22:20:41 +02:00
|
|
|
G_GNUC_END_IGNORE_DEPRECATIONS
|
2009-12-21 20:50:32 +01:00
|
|
|
break;
|
|
|
|
|
2013-01-27 19:53:36 +01:00
|
|
|
case PROP_PROXY_RESOLVER:
|
|
|
|
g_socket_client_set_proxy_resolver (client, g_value_get_object (value));
|
|
|
|
break;
|
|
|
|
|
2009-05-15 21:26:24 +02:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_socket_client_get_family:
|
2009-05-18 13:02:11 +02:00
|
|
|
* @client: a #GSocketClient.
|
2009-05-15 21:26:24 +02:00
|
|
|
*
|
|
|
|
* Gets the socket family of the socket client.
|
|
|
|
*
|
|
|
|
* See g_socket_client_set_family() for details.
|
|
|
|
*
|
|
|
|
* Returns: a #GSocketFamily
|
|
|
|
*
|
|
|
|
* Since: 2.22
|
2009-05-28 00:20:08 +02:00
|
|
|
*/
|
2009-05-15 21:26:24 +02:00
|
|
|
GSocketFamily
|
|
|
|
g_socket_client_get_family (GSocketClient *client)
|
|
|
|
{
|
|
|
|
return client->priv->family;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_socket_client_set_family:
|
2009-05-18 13:02:11 +02:00
|
|
|
* @client: a #GSocketClient.
|
2009-05-15 21:26:24 +02:00
|
|
|
* @family: a #GSocketFamily
|
|
|
|
*
|
|
|
|
* Sets the socket family of the socket client.
|
|
|
|
* If this is set to something other than %G_SOCKET_FAMILY_INVALID
|
|
|
|
* then the sockets created by this object will be of the specified
|
|
|
|
* family.
|
|
|
|
*
|
|
|
|
* This might be useful for instance if you want to force the local
|
|
|
|
* connection to be an ipv4 socket, even though the address might
|
|
|
|
* be an ipv6 mapped to ipv4 address.
|
|
|
|
*
|
|
|
|
* Since: 2.22
|
2009-05-28 00:20:08 +02:00
|
|
|
*/
|
2009-05-15 21:26:24 +02:00
|
|
|
void
|
|
|
|
g_socket_client_set_family (GSocketClient *client,
|
2009-05-28 00:20:08 +02:00
|
|
|
GSocketFamily family)
|
2009-05-15 21:26:24 +02:00
|
|
|
{
|
|
|
|
if (client->priv->family == family)
|
|
|
|
return;
|
|
|
|
|
|
|
|
client->priv->family = family;
|
|
|
|
g_object_notify (G_OBJECT (client), "family");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_socket_client_get_socket_type:
|
2009-05-18 13:02:11 +02:00
|
|
|
* @client: a #GSocketClient.
|
2009-05-15 21:26:24 +02:00
|
|
|
*
|
|
|
|
* Gets the socket type of the socket client.
|
|
|
|
*
|
|
|
|
* See g_socket_client_set_socket_type() for details.
|
|
|
|
*
|
|
|
|
* Returns: a #GSocketFamily
|
|
|
|
*
|
|
|
|
* Since: 2.22
|
2009-05-28 00:20:08 +02:00
|
|
|
*/
|
2009-05-15 21:26:24 +02:00
|
|
|
GSocketType
|
|
|
|
g_socket_client_get_socket_type (GSocketClient *client)
|
|
|
|
{
|
|
|
|
return client->priv->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_socket_client_set_socket_type:
|
2009-05-18 13:02:11 +02:00
|
|
|
* @client: a #GSocketClient.
|
2009-05-15 21:26:24 +02:00
|
|
|
* @type: a #GSocketType
|
|
|
|
*
|
|
|
|
* Sets the socket type of the socket client.
|
|
|
|
* The sockets created by this object will be of the specified
|
|
|
|
* type.
|
|
|
|
*
|
|
|
|
* It doesn't make sense to specify a type of %G_SOCKET_TYPE_DATAGRAM,
|
|
|
|
* as GSocketClient is used for connection oriented services.
|
|
|
|
*
|
|
|
|
* Since: 2.22
|
2009-05-28 00:20:08 +02:00
|
|
|
*/
|
2009-05-15 21:26:24 +02:00
|
|
|
void
|
|
|
|
g_socket_client_set_socket_type (GSocketClient *client,
|
2009-05-28 00:20:08 +02:00
|
|
|
GSocketType type)
|
2009-05-15 21:26:24 +02:00
|
|
|
{
|
|
|
|
if (client->priv->type == type)
|
|
|
|
return;
|
|
|
|
|
|
|
|
client->priv->type = type;
|
|
|
|
g_object_notify (G_OBJECT (client), "type");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_socket_client_get_protocol:
|
2009-05-28 00:20:08 +02:00
|
|
|
* @client: a #GSocketClient
|
2009-05-15 21:26:24 +02:00
|
|
|
*
|
|
|
|
* Gets the protocol name type of the socket client.
|
|
|
|
*
|
|
|
|
* See g_socket_client_set_protocol() for details.
|
|
|
|
*
|
2009-05-20 12:01:29 +02:00
|
|
|
* Returns: a #GSocketProtocol
|
2009-05-15 21:26:24 +02:00
|
|
|
*
|
|
|
|
* Since: 2.22
|
2009-05-28 00:20:08 +02:00
|
|
|
*/
|
2009-05-20 12:01:29 +02:00
|
|
|
GSocketProtocol
|
2009-05-15 21:26:24 +02:00
|
|
|
g_socket_client_get_protocol (GSocketClient *client)
|
|
|
|
{
|
|
|
|
return client->priv->protocol;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_socket_client_set_protocol:
|
2009-05-18 13:02:11 +02:00
|
|
|
* @client: a #GSocketClient.
|
2009-05-20 12:01:29 +02:00
|
|
|
* @protocol: a #GSocketProtocol
|
2009-05-15 21:26:24 +02:00
|
|
|
*
|
|
|
|
* Sets the protocol of the socket client.
|
|
|
|
* The sockets created by this object will use of the specified
|
|
|
|
* protocol.
|
|
|
|
*
|
2019-03-07 18:29:44 +01:00
|
|
|
* If @protocol is %G_SOCKET_PROTOCOL_DEFAULT that means to use the default
|
2009-05-15 21:26:24 +02:00
|
|
|
* protocol for the socket family and type.
|
|
|
|
*
|
|
|
|
* Since: 2.22
|
2009-05-28 00:20:08 +02:00
|
|
|
*/
|
2009-05-15 21:26:24 +02:00
|
|
|
void
|
2009-05-28 00:20:08 +02:00
|
|
|
g_socket_client_set_protocol (GSocketClient *client,
|
|
|
|
GSocketProtocol protocol)
|
2009-05-15 21:26:24 +02:00
|
|
|
{
|
2009-05-20 12:01:29 +02:00
|
|
|
if (client->priv->protocol == protocol)
|
2009-05-15 21:26:24 +02:00
|
|
|
return;
|
|
|
|
|
2009-05-20 12:01:29 +02:00
|
|
|
client->priv->protocol = protocol;
|
2009-05-15 21:26:24 +02:00
|
|
|
g_object_notify (G_OBJECT (client), "protocol");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_socket_client_get_local_address:
|
2009-05-18 13:02:11 +02:00
|
|
|
* @client: a #GSocketClient.
|
2009-05-15 21:26:24 +02:00
|
|
|
*
|
|
|
|
* Gets the local address of the socket client.
|
|
|
|
*
|
|
|
|
* See g_socket_client_set_local_address() for details.
|
|
|
|
*
|
2020-10-18 10:15:54 +02:00
|
|
|
* Returns: (nullable) (transfer none): a #GSocketAddress or %NULL. Do not free.
|
2009-05-15 21:26:24 +02:00
|
|
|
*
|
|
|
|
* Since: 2.22
|
2009-05-28 00:20:08 +02:00
|
|
|
*/
|
2009-05-15 21:26:24 +02:00
|
|
|
GSocketAddress *
|
|
|
|
g_socket_client_get_local_address (GSocketClient *client)
|
|
|
|
{
|
|
|
|
return client->priv->local_address;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_socket_client_set_local_address:
|
2009-05-18 13:02:11 +02:00
|
|
|
* @client: a #GSocketClient.
|
2016-10-29 03:29:02 +02:00
|
|
|
* @address: (nullable): a #GSocketAddress, or %NULL
|
2009-05-15 21:26:24 +02:00
|
|
|
*
|
|
|
|
* Sets the local address of the socket client.
|
|
|
|
* The sockets created by this object will bound to the
|
|
|
|
* specified address (if not %NULL) before connecting.
|
|
|
|
*
|
2011-04-20 19:08:06 +02:00
|
|
|
* This is useful if you want to ensure that the local
|
2009-05-15 21:26:24 +02:00
|
|
|
* side of the connection is on a specific port, or on
|
|
|
|
* a specific interface.
|
|
|
|
*
|
|
|
|
* Since: 2.22
|
2009-05-28 00:20:08 +02:00
|
|
|
*/
|
2009-05-15 21:26:24 +02:00
|
|
|
void
|
2009-05-28 00:20:08 +02:00
|
|
|
g_socket_client_set_local_address (GSocketClient *client,
|
|
|
|
GSocketAddress *address)
|
2009-05-15 21:26:24 +02:00
|
|
|
{
|
|
|
|
if (address)
|
2010-08-10 22:48:45 +02:00
|
|
|
g_object_ref (address);
|
2009-05-15 21:26:24 +02:00
|
|
|
|
|
|
|
if (client->priv->local_address)
|
|
|
|
{
|
|
|
|
g_object_unref (client->priv->local_address);
|
|
|
|
}
|
|
|
|
client->priv->local_address = address;
|
|
|
|
g_object_notify (G_OBJECT (client), "local-address");
|
|
|
|
}
|
|
|
|
|
2010-08-14 21:04:24 +02:00
|
|
|
/**
|
|
|
|
* g_socket_client_get_timeout:
|
|
|
|
* @client: a #GSocketClient
|
|
|
|
*
|
|
|
|
* Gets the I/O timeout time for sockets created by @client.
|
|
|
|
*
|
|
|
|
* See g_socket_client_set_timeout() for details.
|
|
|
|
*
|
|
|
|
* Returns: the timeout in seconds
|
|
|
|
*
|
|
|
|
* Since: 2.26
|
|
|
|
*/
|
|
|
|
guint
|
|
|
|
g_socket_client_get_timeout (GSocketClient *client)
|
|
|
|
{
|
|
|
|
return client->priv->timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_socket_client_set_timeout:
|
|
|
|
* @client: a #GSocketClient.
|
|
|
|
* @timeout: the timeout
|
|
|
|
*
|
|
|
|
* Sets the I/O timeout for sockets created by @client. @timeout is a
|
|
|
|
* time in seconds, or 0 for no timeout (the default).
|
|
|
|
*
|
|
|
|
* The timeout value affects the initial connection attempt as well,
|
|
|
|
* so setting this may cause calls to g_socket_client_connect(), etc,
|
|
|
|
* to fail with %G_IO_ERROR_TIMED_OUT.
|
|
|
|
*
|
|
|
|
* Since: 2.26
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
g_socket_client_set_timeout (GSocketClient *client,
|
|
|
|
guint timeout)
|
|
|
|
{
|
|
|
|
if (client->priv->timeout == timeout)
|
|
|
|
return;
|
|
|
|
|
|
|
|
client->priv->timeout = timeout;
|
|
|
|
g_object_notify (G_OBJECT (client), "timeout");
|
|
|
|
}
|
|
|
|
|
2010-08-10 22:48:45 +02:00
|
|
|
/**
|
|
|
|
* g_socket_client_get_enable_proxy:
|
|
|
|
* @client: a #GSocketClient.
|
|
|
|
*
|
|
|
|
* Gets the proxy enable state; see g_socket_client_set_enable_proxy()
|
|
|
|
*
|
|
|
|
* Returns: whether proxying is enabled
|
|
|
|
*
|
|
|
|
* Since: 2.26
|
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
g_socket_client_get_enable_proxy (GSocketClient *client)
|
|
|
|
{
|
|
|
|
return client->priv->enable_proxy;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_socket_client_set_enable_proxy:
|
|
|
|
* @client: a #GSocketClient.
|
|
|
|
* @enable: whether to enable proxies
|
|
|
|
*
|
|
|
|
* Sets whether or not @client attempts to make connections via a
|
|
|
|
* proxy server. When enabled (the default), #GSocketClient will use a
|
|
|
|
* #GProxyResolver to determine if a proxy protocol such as SOCKS is
|
|
|
|
* needed, and automatically do the necessary proxy negotiation.
|
|
|
|
*
|
2013-01-27 19:53:36 +01:00
|
|
|
* See also g_socket_client_set_proxy_resolver().
|
|
|
|
*
|
2010-08-10 22:48:45 +02:00
|
|
|
* Since: 2.26
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
g_socket_client_set_enable_proxy (GSocketClient *client,
|
|
|
|
gboolean enable)
|
|
|
|
{
|
|
|
|
enable = !!enable;
|
|
|
|
if (client->priv->enable_proxy == enable)
|
|
|
|
return;
|
|
|
|
|
|
|
|
client->priv->enable_proxy = enable;
|
|
|
|
g_object_notify (G_OBJECT (client), "enable-proxy");
|
|
|
|
}
|
|
|
|
|
2009-12-21 20:50:32 +01:00
|
|
|
/**
|
|
|
|
* g_socket_client_get_tls:
|
|
|
|
* @client: a #GSocketClient.
|
|
|
|
*
|
|
|
|
* Gets whether @client creates TLS connections. See
|
|
|
|
* g_socket_client_set_tls() for details.
|
|
|
|
*
|
|
|
|
* Returns: whether @client uses TLS
|
|
|
|
*
|
|
|
|
* Since: 2.28
|
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
g_socket_client_get_tls (GSocketClient *client)
|
|
|
|
{
|
|
|
|
return client->priv->tls;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_socket_client_set_tls:
|
|
|
|
* @client: a #GSocketClient.
|
|
|
|
* @tls: whether to use TLS
|
|
|
|
*
|
|
|
|
* Sets whether @client creates TLS (aka SSL) connections. If @tls is
|
|
|
|
* %TRUE, @client will wrap its connections in a #GTlsClientConnection
|
|
|
|
* and perform a TLS handshake when connecting.
|
|
|
|
*
|
|
|
|
* Note that since #GSocketClient must return a #GSocketConnection,
|
|
|
|
* but #GTlsClientConnection is not a #GSocketConnection, this
|
|
|
|
* actually wraps the resulting #GTlsClientConnection in a
|
|
|
|
* #GTcpWrapperConnection when returning it. You can use
|
|
|
|
* g_tcp_wrapper_connection_get_base_io_stream() on the return value
|
|
|
|
* to extract the #GTlsClientConnection.
|
|
|
|
*
|
2010-08-20 19:04:19 +02:00
|
|
|
* If you need to modify the behavior of the TLS handshake (eg, by
|
|
|
|
* setting a client-side certificate to use, or connecting to the
|
|
|
|
* #GTlsConnection::accept-certificate signal), you can connect to
|
|
|
|
* @client's #GSocketClient::event signal and wait for it to be
|
|
|
|
* emitted with %G_SOCKET_CLIENT_TLS_HANDSHAKING, which will give you
|
|
|
|
* a chance to see the #GTlsClientConnection before the handshake
|
|
|
|
* starts.
|
|
|
|
*
|
2009-12-21 20:50:32 +01:00
|
|
|
* Since: 2.28
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
g_socket_client_set_tls (GSocketClient *client,
|
|
|
|
gboolean tls)
|
|
|
|
{
|
|
|
|
tls = !!tls;
|
|
|
|
if (tls == client->priv->tls)
|
|
|
|
return;
|
|
|
|
|
|
|
|
client->priv->tls = tls;
|
|
|
|
g_object_notify (G_OBJECT (client), "tls");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_socket_client_get_tls_validation_flags:
|
|
|
|
* @client: a #GSocketClient.
|
|
|
|
*
|
|
|
|
* Gets the TLS validation flags used creating TLS connections via
|
|
|
|
* @client.
|
|
|
|
*
|
2021-08-04 22:20:41 +02:00
|
|
|
* This function does not work as originally designed and is impossible
|
|
|
|
* to use correctly. See #GSocketClient:tls-validation-flags for more
|
|
|
|
* information.
|
|
|
|
*
|
2009-12-21 20:50:32 +01:00
|
|
|
* Returns: the TLS validation flags
|
|
|
|
*
|
|
|
|
* Since: 2.28
|
2021-08-04 22:20:41 +02:00
|
|
|
*
|
|
|
|
* Deprecated: 2.72: Do not attempt to ignore validation errors.
|
2009-12-21 20:50:32 +01:00
|
|
|
*/
|
|
|
|
GTlsCertificateFlags
|
|
|
|
g_socket_client_get_tls_validation_flags (GSocketClient *client)
|
|
|
|
{
|
|
|
|
return client->priv->tls_validation_flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_socket_client_set_tls_validation_flags:
|
|
|
|
* @client: a #GSocketClient.
|
|
|
|
* @flags: the validation flags
|
|
|
|
*
|
|
|
|
* Sets the TLS validation flags used when creating TLS connections
|
|
|
|
* via @client. The default value is %G_TLS_CERTIFICATE_VALIDATE_ALL.
|
|
|
|
*
|
2021-08-04 22:20:41 +02:00
|
|
|
* This function does not work as originally designed and is impossible
|
|
|
|
* to use correctly. See #GSocketClient:tls-validation-flags for more
|
|
|
|
* information.
|
|
|
|
*
|
2009-12-21 20:50:32 +01:00
|
|
|
* Since: 2.28
|
2021-08-04 22:20:41 +02:00
|
|
|
*
|
|
|
|
* Deprecated: 2.72: Do not attempt to ignore validation errors.
|
2009-12-21 20:50:32 +01:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
g_socket_client_set_tls_validation_flags (GSocketClient *client,
|
|
|
|
GTlsCertificateFlags flags)
|
|
|
|
{
|
|
|
|
if (client->priv->tls_validation_flags != flags)
|
|
|
|
{
|
|
|
|
client->priv->tls_validation_flags = flags;
|
|
|
|
g_object_notify (G_OBJECT (client), "tls-validation-flags");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-27 19:53:36 +01:00
|
|
|
/**
|
|
|
|
* g_socket_client_get_proxy_resolver:
|
|
|
|
* @client: a #GSocketClient.
|
|
|
|
*
|
|
|
|
* Gets the #GProxyResolver being used by @client. Normally, this will
|
|
|
|
* be the resolver returned by g_proxy_resolver_get_default(), but you
|
|
|
|
* can override it with g_socket_client_set_proxy_resolver().
|
|
|
|
*
|
|
|
|
* Returns: (transfer none): The #GProxyResolver being used by
|
|
|
|
* @client.
|
|
|
|
*
|
|
|
|
* Since: 2.36
|
|
|
|
*/
|
|
|
|
GProxyResolver *
|
|
|
|
g_socket_client_get_proxy_resolver (GSocketClient *client)
|
|
|
|
{
|
|
|
|
if (client->priv->proxy_resolver)
|
|
|
|
return client->priv->proxy_resolver;
|
|
|
|
else
|
|
|
|
return g_proxy_resolver_get_default ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_socket_client_set_proxy_resolver:
|
|
|
|
* @client: a #GSocketClient.
|
2016-10-29 03:29:02 +02:00
|
|
|
* @proxy_resolver: (nullable): a #GProxyResolver, or %NULL for the
|
2013-01-27 19:53:36 +01:00
|
|
|
* default.
|
|
|
|
*
|
|
|
|
* Overrides the #GProxyResolver used by @client. You can call this if
|
|
|
|
* you want to use specific proxies, rather than using the system
|
|
|
|
* default proxy settings.
|
|
|
|
*
|
|
|
|
* Note that whether or not the proxy resolver is actually used
|
|
|
|
* depends on the setting of #GSocketClient:enable-proxy, which is not
|
|
|
|
* changed by this function (but which is %TRUE by default)
|
|
|
|
*
|
|
|
|
* Since: 2.36
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
g_socket_client_set_proxy_resolver (GSocketClient *client,
|
|
|
|
GProxyResolver *proxy_resolver)
|
|
|
|
{
|
|
|
|
/* We have to be careful to avoid calling
|
|
|
|
* g_proxy_resolver_get_default() until we're sure we need it,
|
|
|
|
* because trying to load the default proxy resolver module will
|
|
|
|
* break some test programs that aren't expecting it (eg,
|
|
|
|
* tests/gsettings).
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (client->priv->proxy_resolver)
|
|
|
|
g_object_unref (client->priv->proxy_resolver);
|
|
|
|
|
|
|
|
client->priv->proxy_resolver = proxy_resolver;
|
|
|
|
|
|
|
|
if (client->priv->proxy_resolver)
|
|
|
|
g_object_ref (client->priv->proxy_resolver);
|
|
|
|
}
|
|
|
|
|
2009-05-15 21:26:24 +02:00
|
|
|
static void
|
|
|
|
g_socket_client_class_init (GSocketClientClass *class)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (class);
|
|
|
|
|
|
|
|
gobject_class->finalize = g_socket_client_finalize;
|
|
|
|
gobject_class->set_property = g_socket_client_set_property;
|
|
|
|
gobject_class->get_property = g_socket_client_get_property;
|
|
|
|
|
2010-08-20 19:04:19 +02:00
|
|
|
/**
|
|
|
|
* GSocketClient::event:
|
|
|
|
* @client: the #GSocketClient
|
|
|
|
* @event: the event that is occurring
|
|
|
|
* @connectable: the #GSocketConnectable that @event is occurring on
|
2015-06-18 09:36:12 +02:00
|
|
|
* @connection: (nullable): the current representation of the connection
|
2010-08-20 19:04:19 +02:00
|
|
|
*
|
|
|
|
* Emitted when @client's activity on @connectable changes state.
|
|
|
|
* Among other things, this can be used to provide progress
|
|
|
|
* information about a network connection in the UI. The meanings of
|
|
|
|
* the different @event values are as follows:
|
|
|
|
*
|
2014-02-01 04:42:21 +01:00
|
|
|
* - %G_SOCKET_CLIENT_RESOLVING: @client is about to look up @connectable
|
|
|
|
* in DNS. @connection will be %NULL.
|
|
|
|
*
|
|
|
|
* - %G_SOCKET_CLIENT_RESOLVED: @client has successfully resolved
|
|
|
|
* @connectable in DNS. @connection will be %NULL.
|
|
|
|
*
|
|
|
|
* - %G_SOCKET_CLIENT_CONNECTING: @client is about to make a connection
|
|
|
|
* to a remote host; either a proxy server or the destination server
|
|
|
|
* itself. @connection is the #GSocketConnection, which is not yet
|
|
|
|
* connected. Since GLib 2.40, you can access the remote
|
|
|
|
* address via g_socket_connection_get_remote_address().
|
|
|
|
*
|
|
|
|
* - %G_SOCKET_CLIENT_CONNECTED: @client has successfully connected
|
|
|
|
* to a remote host. @connection is the connected #GSocketConnection.
|
|
|
|
*
|
|
|
|
* - %G_SOCKET_CLIENT_PROXY_NEGOTIATING: @client is about to negotiate
|
|
|
|
* with a proxy to get it to connect to @connectable. @connection is
|
|
|
|
* the #GSocketConnection to the proxy server.
|
|
|
|
*
|
|
|
|
* - %G_SOCKET_CLIENT_PROXY_NEGOTIATED: @client has negotiated a
|
|
|
|
* connection to @connectable through a proxy server. @connection is
|
|
|
|
* the stream returned from g_proxy_connect(), which may or may not
|
|
|
|
* be a #GSocketConnection.
|
|
|
|
*
|
|
|
|
* - %G_SOCKET_CLIENT_TLS_HANDSHAKING: @client is about to begin a TLS
|
|
|
|
* handshake. @connection is a #GTlsClientConnection.
|
|
|
|
*
|
|
|
|
* - %G_SOCKET_CLIENT_TLS_HANDSHAKED: @client has successfully completed
|
|
|
|
* the TLS handshake. @connection is a #GTlsClientConnection.
|
|
|
|
*
|
|
|
|
* - %G_SOCKET_CLIENT_COMPLETE: @client has either successfully connected
|
|
|
|
* to @connectable (in which case @connection is the #GSocketConnection
|
|
|
|
* that it will be returning to the caller) or has failed (in which
|
|
|
|
* case @connection is %NULL and the client is about to return an error).
|
2010-08-20 19:04:19 +02:00
|
|
|
*
|
|
|
|
* Each event except %G_SOCKET_CLIENT_COMPLETE may be emitted
|
|
|
|
* multiple times (or not at all) for a given connectable (in
|
|
|
|
* particular, if @client ends up attempting to connect to more than
|
2012-02-19 17:42:55 +01:00
|
|
|
* one address). However, if @client emits the #GSocketClient::event
|
2020-10-09 01:47:04 +02:00
|
|
|
* signal at all for a given connectable, then it will always emit
|
2010-08-20 19:04:19 +02:00
|
|
|
* it with %G_SOCKET_CLIENT_COMPLETE when it is done.
|
|
|
|
*
|
|
|
|
* Note that there may be additional #GSocketClientEvent values in
|
|
|
|
* the future; unrecognized @event values should be ignored.
|
|
|
|
*
|
|
|
|
* Since: 2.32
|
|
|
|
*/
|
|
|
|
signals[EVENT] =
|
|
|
|
g_signal_new (I_("event"),
|
|
|
|
G_TYPE_FROM_CLASS (gobject_class),
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
G_STRUCT_OFFSET (GSocketClientClass, event),
|
|
|
|
NULL, NULL,
|
2019-05-31 04:29:18 +02:00
|
|
|
_g_cclosure_marshal_VOID__ENUM_OBJECT_OBJECT,
|
2010-08-20 19:04:19 +02:00
|
|
|
G_TYPE_NONE, 3,
|
|
|
|
G_TYPE_SOCKET_CLIENT_EVENT,
|
|
|
|
G_TYPE_SOCKET_CONNECTABLE,
|
|
|
|
G_TYPE_IO_STREAM);
|
2019-05-31 04:29:18 +02:00
|
|
|
g_signal_set_va_marshaller (signals[EVENT],
|
|
|
|
G_TYPE_FROM_CLASS (class),
|
|
|
|
_g_cclosure_marshal_VOID__ENUM_OBJECT_OBJECTv);
|
2010-08-20 19:04:19 +02:00
|
|
|
|
2009-05-15 21:26:24 +02:00
|
|
|
g_object_class_install_property (gobject_class, PROP_FAMILY,
|
|
|
|
g_param_spec_enum ("family",
|
|
|
|
P_("Socket family"),
|
|
|
|
P_("The sockets address family to use for socket construction"),
|
|
|
|
G_TYPE_SOCKET_FAMILY,
|
|
|
|
G_SOCKET_FAMILY_INVALID,
|
2009-05-28 00:20:08 +02:00
|
|
|
G_PARAM_CONSTRUCT |
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
2009-05-15 21:26:24 +02:00
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_TYPE,
|
|
|
|
g_param_spec_enum ("type",
|
|
|
|
P_("Socket type"),
|
|
|
|
P_("The sockets type to use for socket construction"),
|
|
|
|
G_TYPE_SOCKET_TYPE,
|
|
|
|
G_SOCKET_TYPE_STREAM,
|
2009-05-28 00:20:08 +02:00
|
|
|
G_PARAM_CONSTRUCT |
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
2009-05-15 21:26:24 +02:00
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_PROTOCOL,
|
2009-05-20 12:01:29 +02:00
|
|
|
g_param_spec_enum ("protocol",
|
|
|
|
P_("Socket protocol"),
|
|
|
|
P_("The protocol to use for socket construction, or 0 for default"),
|
|
|
|
G_TYPE_SOCKET_PROTOCOL,
|
|
|
|
G_SOCKET_PROTOCOL_DEFAULT,
|
2009-05-28 00:20:08 +02:00
|
|
|
G_PARAM_CONSTRUCT |
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
2009-05-15 21:26:24 +02:00
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_LOCAL_ADDRESS,
|
|
|
|
g_param_spec_object ("local-address",
|
|
|
|
P_("Local address"),
|
|
|
|
P_("The local address constructed sockets will be bound to"),
|
|
|
|
G_TYPE_SOCKET_ADDRESS,
|
2009-05-28 00:20:08 +02:00
|
|
|
G_PARAM_CONSTRUCT |
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
2010-08-14 21:04:24 +02:00
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_TIMEOUT,
|
|
|
|
g_param_spec_uint ("timeout",
|
|
|
|
P_("Socket timeout"),
|
|
|
|
P_("The I/O timeout for sockets, or 0 for none"),
|
|
|
|
0, G_MAXUINT, 0,
|
|
|
|
G_PARAM_CONSTRUCT |
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
|
|
|
|
2010-08-10 22:48:45 +02:00
|
|
|
g_object_class_install_property (gobject_class, PROP_ENABLE_PROXY,
|
|
|
|
g_param_spec_boolean ("enable-proxy",
|
|
|
|
P_("Enable proxy"),
|
|
|
|
P_("Enable proxy support"),
|
|
|
|
TRUE,
|
|
|
|
G_PARAM_CONSTRUCT |
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
|
|
|
|
2009-12-21 20:50:32 +01:00
|
|
|
g_object_class_install_property (gobject_class, PROP_TLS,
|
|
|
|
g_param_spec_boolean ("tls",
|
|
|
|
P_("TLS"),
|
|
|
|
P_("Whether to create TLS connections"),
|
|
|
|
FALSE,
|
|
|
|
G_PARAM_CONSTRUCT |
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
2021-08-04 22:20:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GSocketClient:tls-validation-flags:
|
|
|
|
*
|
|
|
|
* The TLS validation flags used when creating TLS connections. The
|
|
|
|
* default value is %G_TLS_CERTIFICATE_VALIDATE_ALL.
|
|
|
|
*
|
|
|
|
* GLib guarantees that if certificate verification fails, at least one
|
|
|
|
* flag will be set, but it does not guarantee that all possible flags
|
|
|
|
* will be set. Accordingly, you may not safely decide to ignore any
|
|
|
|
* particular type of error. For example, it would be incorrect to mask
|
|
|
|
* %G_TLS_CERTIFICATE_EXPIRED if you want to allow expired certificates,
|
|
|
|
* because this could potentially be the only error flag set even if
|
|
|
|
* other problems exist with the certificate. Therefore, there is no
|
|
|
|
* safe way to use this property. This is not a horrible problem,
|
|
|
|
* though, because you should not be attempting to ignore validation
|
|
|
|
* errors anyway. If you really must ignore TLS certificate errors,
|
|
|
|
* connect to the #GSocketClient::event signal, wait for it to be
|
|
|
|
* emitted with %G_SOCKET_CLIENT_TLS_HANDSHAKING, and use that to
|
|
|
|
* connect to #GTlsConnection::accept-certificate.
|
|
|
|
*
|
|
|
|
* Deprecated: 2.72: Do not attempt to ignore validation errors.
|
|
|
|
*/
|
2009-12-21 20:50:32 +01:00
|
|
|
g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
|
|
|
|
g_param_spec_flags ("tls-validation-flags",
|
|
|
|
P_("TLS validation flags"),
|
|
|
|
P_("TLS validation flags to use"),
|
|
|
|
G_TYPE_TLS_CERTIFICATE_FLAGS,
|
|
|
|
G_TLS_CERTIFICATE_VALIDATE_ALL,
|
|
|
|
G_PARAM_CONSTRUCT |
|
|
|
|
G_PARAM_READWRITE |
|
2021-08-04 22:20:41 +02:00
|
|
|
G_PARAM_STATIC_STRINGS |
|
|
|
|
G_PARAM_DEPRECATED));
|
2013-01-27 19:53:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GSocketClient:proxy-resolver:
|
|
|
|
*
|
|
|
|
* The proxy resolver to use
|
|
|
|
*
|
|
|
|
* Since: 2.36
|
|
|
|
*/
|
|
|
|
g_object_class_install_property (gobject_class, PROP_PROXY_RESOLVER,
|
|
|
|
g_param_spec_object ("proxy-resolver",
|
|
|
|
P_("Proxy resolver"),
|
|
|
|
P_("The proxy resolver to use"),
|
|
|
|
G_TYPE_PROXY_RESOLVER,
|
|
|
|
G_PARAM_CONSTRUCT |
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
2009-05-15 21:26:24 +02:00
|
|
|
}
|
|
|
|
|
2010-08-20 19:04:19 +02:00
|
|
|
static void
|
|
|
|
g_socket_client_emit_event (GSocketClient *client,
|
2020-10-08 20:42:14 +02:00
|
|
|
GSocketClientEvent event,
|
2010-08-20 19:04:19 +02:00
|
|
|
GSocketConnectable *connectable,
|
|
|
|
GIOStream *connection)
|
|
|
|
{
|
|
|
|
g_signal_emit (client, signals[EVENT], 0,
|
|
|
|
event, connectable, connection);
|
|
|
|
}
|
|
|
|
|
2020-10-09 01:02:56 +02:00
|
|
|
/* Originally, GSocketClient returned whatever error occured last. Turns
|
|
|
|
* out this doesn't work well in practice. Consider the following case:
|
|
|
|
* DNS returns an IPv4 and IPv6 address. First we'll connect() to the
|
|
|
|
* IPv4 address, and say that succeeds, but TLS is enabled and the TLS
|
|
|
|
* handshake fails. Then we try the IPv6 address and receive ENETUNREACH
|
|
|
|
* because IPv6 isn't supported. We wind up returning NETWORK_UNREACHABLE
|
|
|
|
* even though the address can be pinged and a TLS error would be more
|
|
|
|
* appropriate. So instead, we now try to return the error corresponding
|
|
|
|
* to the latest attempted GSocketClientEvent in the connection process.
|
|
|
|
* TLS errors take precedence over proxy errors, which take precedence
|
|
|
|
* over connect() errors, which take precedence over DNS errors.
|
|
|
|
*
|
|
|
|
* Note that the example above considers a sync codepath, but this is an
|
|
|
|
* issue for the async codepath too, where events and errors may occur
|
|
|
|
* in confusing orders.
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GError *tmp_error;
|
|
|
|
GError *best_error;
|
|
|
|
GSocketClientEvent best_error_event;
|
|
|
|
} SocketClientErrorInfo;
|
|
|
|
|
|
|
|
static SocketClientErrorInfo *
|
|
|
|
socket_client_error_info_new (void)
|
|
|
|
{
|
|
|
|
return g_new0 (SocketClientErrorInfo, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
socket_client_error_info_free (SocketClientErrorInfo *info)
|
|
|
|
{
|
|
|
|
g_assert (info->tmp_error == NULL);
|
|
|
|
g_clear_error (&info->best_error);
|
|
|
|
g_free (info);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
consider_tmp_error (SocketClientErrorInfo *info,
|
|
|
|
GSocketClientEvent event)
|
|
|
|
{
|
|
|
|
if (info->tmp_error == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* If we ever add more GSocketClientEvents in the future, then we'll
|
|
|
|
* no longer be able to use >= for this comparison, because future
|
|
|
|
* events will compare greater than G_SOCKET_CLIENT_COMPLETE. Until
|
|
|
|
* then, this is convenient. Note G_SOCKET_CLIENT_RESOLVING is 0 so we
|
|
|
|
* need to use >= here or those errors would never be set. That means
|
|
|
|
* if we get two errors on the same GSocketClientEvent, we wind up
|
|
|
|
* preferring the last one, which is fine.
|
|
|
|
*/
|
|
|
|
g_assert (event <= G_SOCKET_CLIENT_COMPLETE);
|
|
|
|
if (event >= info->best_error_event)
|
|
|
|
{
|
|
|
|
g_clear_error (&info->best_error);
|
|
|
|
info->best_error = info->tmp_error;
|
|
|
|
info->tmp_error = NULL;
|
|
|
|
info->best_error_event = event;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_clear_error (&info->tmp_error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-15 21:26:24 +02:00
|
|
|
/**
|
|
|
|
* g_socket_client_connect:
|
|
|
|
* @client: a #GSocketClient.
|
|
|
|
* @connectable: a #GSocketConnectable specifying the remote address.
|
2016-10-29 03:29:02 +02:00
|
|
|
* @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
|
2009-05-15 21:26:24 +02:00
|
|
|
* @error: #GError for error reporting, or %NULL to ignore.
|
|
|
|
*
|
2011-04-20 19:08:06 +02:00
|
|
|
* Tries to resolve the @connectable and make a network connection to it.
|
2009-05-15 21:26:24 +02:00
|
|
|
*
|
|
|
|
* Upon a successful connection, a new #GSocketConnection is constructed
|
|
|
|
* and returned. The caller owns this new object and must drop their
|
|
|
|
* reference to it when finished with it.
|
|
|
|
*
|
|
|
|
* The type of the #GSocketConnection object returned depends on the type of
|
|
|
|
* the underlying socket that is used. For instance, for a TCP/IP connection
|
|
|
|
* it will be a #GTcpConnection.
|
|
|
|
*
|
2011-04-20 19:08:06 +02:00
|
|
|
* The socket created will be the same family as the address that the
|
2009-05-15 21:26:24 +02:00
|
|
|
* @connectable resolves to, unless family is set with g_socket_client_set_family()
|
|
|
|
* or indirectly via g_socket_client_set_local_address(). The socket type
|
|
|
|
* defaults to %G_SOCKET_TYPE_STREAM but can be set with
|
|
|
|
* g_socket_client_set_socket_type().
|
|
|
|
*
|
|
|
|
* If a local address is specified with g_socket_client_set_local_address() the
|
|
|
|
* socket will be bound to this address before connecting.
|
|
|
|
*
|
2010-09-24 23:24:41 +02:00
|
|
|
* Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
|
2009-05-15 21:26:24 +02:00
|
|
|
*
|
|
|
|
* Since: 2.22
|
2009-05-28 00:20:08 +02:00
|
|
|
*/
|
2009-05-15 21:26:24 +02:00
|
|
|
GSocketConnection *
|
|
|
|
g_socket_client_connect (GSocketClient *client,
|
|
|
|
GSocketConnectable *connectable,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error)
|
|
|
|
{
|
2009-12-21 20:50:32 +01:00
|
|
|
GIOStream *connection = NULL;
|
2010-08-10 22:48:45 +02:00
|
|
|
GSocketAddressEnumerator *enumerator = NULL;
|
2020-10-09 01:02:56 +02:00
|
|
|
SocketClientErrorInfo *error_info;
|
gsocketclient: emit RESOLVING/RESOLVED events only once
GSocketAddressEnumerator encapsulates the details of how DNS happens, so
we don't have to think about it. But we may have taken encapsulation a
bit too far, here. Usually, we resolve a domain name to a list of IPv4
and IPv6 addresses. Then we go through each address in the list and try
to connect to it. Name resolution happens exactly once, at the start.
It doesn't happen each time we enumerate the enumerator. In theory, it
*could*, because we've designed these APIs to be agnostic of underlying
implementation details like DNS and network protocols. But in practice,
we know that's not really what's happening. It's weird to say that we
are RESOLVING what we know to be the same name multiple times. Behind
the scenes, we're not doing that.
This also fixes #1994, where enumeration can end with a RESOLVING event,
even though this is supposed to be the first event rather than the last.
I thought this would be hard to fix, even requiring new public API in
GSocketAddressEnumerator to peek ahead to see if the next enumeration is
going to return NULL. Then I decided we should just fake it: always emit
both RESOLVING and RESOLVED at the same time right after each
enumeration. Finally, I realized we can emit them at the correct time if
we simply assume resolving only happens the first time. This seems like
the most elegant of the possible solutions.
Now, this is a behavior change, and arguably an API break, but it should
align better with reasonable expectations of how GSocketClientEvent
ought to work. I don't expect it to break anything besides tests that
check which order GSocketClientEvent events are emitted in. (Currently,
libsoup has such tests, which will need to be updated.) Ideally we would
have GLib-level tests as well, but in a concession to pragmatism, it's a
lot easier to keep network tests in libsoup.
2020-10-06 22:39:45 +02:00
|
|
|
gboolean ever_resolved = FALSE;
|
2009-05-15 21:26:24 +02:00
|
|
|
|
2020-10-09 01:02:56 +02:00
|
|
|
error_info = socket_client_error_info_new ();
|
2010-08-10 22:48:45 +02:00
|
|
|
|
|
|
|
if (can_use_proxy (client))
|
2013-01-27 19:53:36 +01:00
|
|
|
{
|
|
|
|
enumerator = g_socket_connectable_proxy_enumerate (connectable);
|
|
|
|
if (client->priv->proxy_resolver &&
|
|
|
|
G_IS_PROXY_ADDRESS_ENUMERATOR (enumerator))
|
|
|
|
{
|
2013-02-15 16:39:19 +01:00
|
|
|
g_object_set (G_OBJECT (enumerator),
|
|
|
|
"proxy-resolver", client->priv->proxy_resolver,
|
|
|
|
NULL);
|
2013-01-27 19:53:36 +01:00
|
|
|
}
|
|
|
|
}
|
2010-08-10 22:48:45 +02:00
|
|
|
else
|
|
|
|
enumerator = g_socket_connectable_enumerate (connectable);
|
|
|
|
|
2009-05-15 21:26:24 +02:00
|
|
|
while (connection == NULL)
|
|
|
|
{
|
2010-08-10 22:48:45 +02:00
|
|
|
GSocketAddress *address = NULL;
|
2011-11-17 19:04:04 +01:00
|
|
|
gboolean application_proxy = FALSE;
|
2009-05-15 21:26:24 +02:00
|
|
|
GSocket *socket;
|
2010-08-20 19:04:19 +02:00
|
|
|
gboolean using_proxy;
|
2009-05-15 21:26:24 +02:00
|
|
|
|
|
|
|
if (g_cancellable_is_cancelled (cancellable))
|
|
|
|
{
|
2020-10-09 01:02:56 +02:00
|
|
|
g_clear_error (&error_info->best_error);
|
|
|
|
g_cancellable_set_error_if_cancelled (cancellable, &error_info->best_error);
|
2009-05-15 21:26:24 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
gsocketclient: emit RESOLVING/RESOLVED events only once
GSocketAddressEnumerator encapsulates the details of how DNS happens, so
we don't have to think about it. But we may have taken encapsulation a
bit too far, here. Usually, we resolve a domain name to a list of IPv4
and IPv6 addresses. Then we go through each address in the list and try
to connect to it. Name resolution happens exactly once, at the start.
It doesn't happen each time we enumerate the enumerator. In theory, it
*could*, because we've designed these APIs to be agnostic of underlying
implementation details like DNS and network protocols. But in practice,
we know that's not really what's happening. It's weird to say that we
are RESOLVING what we know to be the same name multiple times. Behind
the scenes, we're not doing that.
This also fixes #1994, where enumeration can end with a RESOLVING event,
even though this is supposed to be the first event rather than the last.
I thought this would be hard to fix, even requiring new public API in
GSocketAddressEnumerator to peek ahead to see if the next enumeration is
going to return NULL. Then I decided we should just fake it: always emit
both RESOLVING and RESOLVED at the same time right after each
enumeration. Finally, I realized we can emit them at the correct time if
we simply assume resolving only happens the first time. This seems like
the most elegant of the possible solutions.
Now, this is a behavior change, and arguably an API break, but it should
align better with reasonable expectations of how GSocketClientEvent
ought to work. I don't expect it to break anything besides tests that
check which order GSocketClientEvent events are emitted in. (Currently,
libsoup has such tests, which will need to be updated.) Ideally we would
have GLib-level tests as well, but in a concession to pragmatism, it's a
lot easier to keep network tests in libsoup.
2020-10-06 22:39:45 +02:00
|
|
|
if (!ever_resolved)
|
|
|
|
{
|
|
|
|
g_socket_client_emit_event (client, G_SOCKET_CLIENT_RESOLVING,
|
|
|
|
connectable, NULL);
|
|
|
|
}
|
2009-05-15 21:26:24 +02:00
|
|
|
address = g_socket_address_enumerator_next (enumerator, cancellable,
|
2020-10-09 01:02:56 +02:00
|
|
|
&error_info->tmp_error);
|
|
|
|
consider_tmp_error (error_info, G_SOCKET_CLIENT_RESOLVING);
|
gsocketclient: emit RESOLVING/RESOLVED events only once
GSocketAddressEnumerator encapsulates the details of how DNS happens, so
we don't have to think about it. But we may have taken encapsulation a
bit too far, here. Usually, we resolve a domain name to a list of IPv4
and IPv6 addresses. Then we go through each address in the list and try
to connect to it. Name resolution happens exactly once, at the start.
It doesn't happen each time we enumerate the enumerator. In theory, it
*could*, because we've designed these APIs to be agnostic of underlying
implementation details like DNS and network protocols. But in practice,
we know that's not really what's happening. It's weird to say that we
are RESOLVING what we know to be the same name multiple times. Behind
the scenes, we're not doing that.
This also fixes #1994, where enumeration can end with a RESOLVING event,
even though this is supposed to be the first event rather than the last.
I thought this would be hard to fix, even requiring new public API in
GSocketAddressEnumerator to peek ahead to see if the next enumeration is
going to return NULL. Then I decided we should just fake it: always emit
both RESOLVING and RESOLVED at the same time right after each
enumeration. Finally, I realized we can emit them at the correct time if
we simply assume resolving only happens the first time. This seems like
the most elegant of the possible solutions.
Now, this is a behavior change, and arguably an API break, but it should
align better with reasonable expectations of how GSocketClientEvent
ought to work. I don't expect it to break anything besides tests that
check which order GSocketClientEvent events are emitted in. (Currently,
libsoup has such tests, which will need to be updated.) Ideally we would
have GLib-level tests as well, but in a concession to pragmatism, it's a
lot easier to keep network tests in libsoup.
2020-10-06 22:39:45 +02:00
|
|
|
if (!ever_resolved)
|
|
|
|
{
|
|
|
|
g_socket_client_emit_event (client, G_SOCKET_CLIENT_RESOLVED,
|
|
|
|
connectable, NULL);
|
|
|
|
ever_resolved = TRUE;
|
|
|
|
}
|
2010-08-10 22:48:45 +02:00
|
|
|
|
2009-05-15 21:26:24 +02:00
|
|
|
if (address == NULL)
|
|
|
|
{
|
2020-10-09 01:02:56 +02:00
|
|
|
/* Enumeration is finished. */
|
|
|
|
g_assert (&error_info->best_error != NULL);
|
2009-05-15 21:26:24 +02:00
|
|
|
break;
|
|
|
|
}
|
2010-08-20 19:04:19 +02:00
|
|
|
|
|
|
|
using_proxy = (G_IS_PROXY_ADDRESS (address) &&
|
|
|
|
client->priv->enable_proxy);
|
2009-05-15 21:26:24 +02:00
|
|
|
|
2020-10-09 01:02:56 +02:00
|
|
|
socket = create_socket (client, address, &error_info->tmp_error);
|
|
|
|
consider_tmp_error (error_info, G_SOCKET_CLIENT_CONNECTING);
|
make GProxyConnection public, as GTcpWrapperConnection
GProxyConnection is a class that was added for proxy support;
g_socket_client_connect() returns a GSocketConnection, but in some
cases (eg, encrypted SOCKS), GProxy might return a GIOStream that is
not a GSocketConnection. In that case, GSocketClient would wrap the
stream up in a GProxyConnection, which is a subclass of
GSocketConnection but uses the input/output streams of the wrapped
connection.
GTlsConnection is not a GSocketConnection, so it has the same problem,
so it will need the same treatment. Rename the class to
GTcpWrapperStream, and make it public, so people can extract the base
stream from it when necessary.
(This is not ideal and GSocketClient will need to be revisited as an
API at some point...)
https://bugzilla.gnome.org/show_bug.cgi?id=588189
2010-11-02 01:22:24 +01:00
|
|
|
if (socket == NULL)
|
2009-05-15 21:26:24 +02:00
|
|
|
{
|
make GProxyConnection public, as GTcpWrapperConnection
GProxyConnection is a class that was added for proxy support;
g_socket_client_connect() returns a GSocketConnection, but in some
cases (eg, encrypted SOCKS), GProxy might return a GIOStream that is
not a GSocketConnection. In that case, GSocketClient would wrap the
stream up in a GProxyConnection, which is a subclass of
GSocketConnection but uses the input/output streams of the wrapped
connection.
GTlsConnection is not a GSocketConnection, so it has the same problem,
so it will need the same treatment. Rename the class to
GTcpWrapperStream, and make it public, so people can extract the base
stream from it when necessary.
(This is not ideal and GSocketClient will need to be revisited as an
API at some point...)
https://bugzilla.gnome.org/show_bug.cgi?id=588189
2010-11-02 01:22:24 +01:00
|
|
|
g_object_unref (address);
|
|
|
|
continue;
|
2009-05-15 21:26:24 +02:00
|
|
|
}
|
|
|
|
|
2011-06-12 22:29:04 +02:00
|
|
|
connection = (GIOStream *)g_socket_connection_factory_create_connection (socket);
|
2013-11-18 22:15:34 +01:00
|
|
|
g_socket_connection_set_cached_remote_address ((GSocketConnection*)connection, address);
|
2010-08-20 19:04:19 +02:00
|
|
|
g_socket_client_emit_event (client, G_SOCKET_CLIENT_CONNECTING, connectable, connection);
|
|
|
|
|
|
|
|
if (g_socket_connection_connect (G_SOCKET_CONNECTION (connection),
|
2020-10-09 01:02:56 +02:00
|
|
|
address, cancellable, &error_info->tmp_error))
|
2010-08-20 19:04:19 +02:00
|
|
|
{
|
2013-11-18 22:15:34 +01:00
|
|
|
g_socket_connection_set_cached_remote_address ((GSocketConnection*)connection, NULL);
|
2010-08-20 19:04:19 +02:00
|
|
|
g_socket_client_emit_event (client, G_SOCKET_CLIENT_CONNECTED, connectable, connection);
|
|
|
|
}
|
|
|
|
else
|
2011-06-12 22:29:04 +02:00
|
|
|
{
|
2020-10-09 01:02:56 +02:00
|
|
|
clarify_connect_error (error_info->tmp_error, connectable, address);
|
|
|
|
consider_tmp_error (error_info, G_SOCKET_CLIENT_CONNECTING);
|
2011-06-12 22:29:04 +02:00
|
|
|
g_object_unref (connection);
|
|
|
|
connection = NULL;
|
|
|
|
}
|
make GProxyConnection public, as GTcpWrapperConnection
GProxyConnection is a class that was added for proxy support;
g_socket_client_connect() returns a GSocketConnection, but in some
cases (eg, encrypted SOCKS), GProxy might return a GIOStream that is
not a GSocketConnection. In that case, GSocketClient would wrap the
stream up in a GProxyConnection, which is a subclass of
GSocketConnection but uses the input/output streams of the wrapped
connection.
GTlsConnection is not a GSocketConnection, so it has the same problem,
so it will need the same treatment. Rename the class to
GTcpWrapperStream, and make it public, so people can extract the base
stream from it when necessary.
(This is not ideal and GSocketClient will need to be revisited as an
API at some point...)
https://bugzilla.gnome.org/show_bug.cgi?id=588189
2010-11-02 01:22:24 +01:00
|
|
|
|
2010-08-20 19:04:19 +02:00
|
|
|
if (connection && using_proxy)
|
2010-08-10 22:48:45 +02:00
|
|
|
{
|
|
|
|
GProxyAddress *proxy_addr = G_PROXY_ADDRESS (address);
|
|
|
|
const gchar *protocol;
|
|
|
|
GProxy *proxy;
|
|
|
|
|
|
|
|
protocol = g_proxy_address_get_protocol (proxy_addr);
|
|
|
|
|
|
|
|
/* The connection should not be anything else then TCP Connection,
|
|
|
|
* but let's put a safety guard in case
|
|
|
|
*/
|
|
|
|
if (!G_IS_TCP_CONNECTION (connection))
|
|
|
|
{
|
|
|
|
g_critical ("Trying to proxy over non-TCP connection, this is "
|
|
|
|
"most likely a bug in GLib IO library.");
|
|
|
|
|
2020-10-09 01:02:56 +02:00
|
|
|
g_set_error_literal (&error_info->tmp_error,
|
2010-08-10 22:48:45 +02:00
|
|
|
G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
|
2012-08-17 05:02:41 +02:00
|
|
|
_("Proxying over a non-TCP connection is not supported."));
|
2020-10-09 01:02:56 +02:00
|
|
|
consider_tmp_error (error_info, G_SOCKET_CLIENT_PROXY_NEGOTIATING);
|
2010-08-10 22:48:45 +02:00
|
|
|
|
|
|
|
g_object_unref (connection);
|
|
|
|
connection = NULL;
|
|
|
|
}
|
2014-12-09 13:35:06 +01:00
|
|
|
else if (g_hash_table_contains (client->priv->app_proxies, protocol))
|
|
|
|
{
|
|
|
|
application_proxy = TRUE;
|
|
|
|
}
|
|
|
|
else if ((proxy = g_proxy_get_default_for_protocol (protocol)))
|
2010-08-10 22:48:45 +02:00
|
|
|
{
|
2010-08-20 19:04:19 +02:00
|
|
|
GIOStream *proxy_connection;
|
2010-08-10 22:48:45 +02:00
|
|
|
|
2010-08-20 19:04:19 +02:00
|
|
|
g_socket_client_emit_event (client, G_SOCKET_CLIENT_PROXY_NEGOTIATING, connectable, connection);
|
2009-12-21 20:50:32 +01:00
|
|
|
proxy_connection = g_proxy_connect (proxy,
|
|
|
|
connection,
|
|
|
|
proxy_addr,
|
|
|
|
cancellable,
|
2020-10-09 01:02:56 +02:00
|
|
|
&error_info->tmp_error);
|
|
|
|
consider_tmp_error (error_info, G_SOCKET_CLIENT_PROXY_NEGOTIATING);
|
|
|
|
|
make GProxyConnection public, as GTcpWrapperConnection
GProxyConnection is a class that was added for proxy support;
g_socket_client_connect() returns a GSocketConnection, but in some
cases (eg, encrypted SOCKS), GProxy might return a GIOStream that is
not a GSocketConnection. In that case, GSocketClient would wrap the
stream up in a GProxyConnection, which is a subclass of
GSocketConnection but uses the input/output streams of the wrapped
connection.
GTlsConnection is not a GSocketConnection, so it has the same problem,
so it will need the same treatment. Rename the class to
GTcpWrapperStream, and make it public, so people can extract the base
stream from it when necessary.
(This is not ideal and GSocketClient will need to be revisited as an
API at some point...)
https://bugzilla.gnome.org/show_bug.cgi?id=588189
2010-11-02 01:22:24 +01:00
|
|
|
g_object_unref (connection);
|
2009-12-21 20:50:32 +01:00
|
|
|
connection = proxy_connection;
|
make GProxyConnection public, as GTcpWrapperConnection
GProxyConnection is a class that was added for proxy support;
g_socket_client_connect() returns a GSocketConnection, but in some
cases (eg, encrypted SOCKS), GProxy might return a GIOStream that is
not a GSocketConnection. In that case, GSocketClient would wrap the
stream up in a GProxyConnection, which is a subclass of
GSocketConnection but uses the input/output streams of the wrapped
connection.
GTlsConnection is not a GSocketConnection, so it has the same problem,
so it will need the same treatment. Rename the class to
GTcpWrapperStream, and make it public, so people can extract the base
stream from it when necessary.
(This is not ideal and GSocketClient will need to be revisited as an
API at some point...)
https://bugzilla.gnome.org/show_bug.cgi?id=588189
2010-11-02 01:22:24 +01:00
|
|
|
g_object_unref (proxy);
|
2010-08-20 19:04:19 +02:00
|
|
|
|
|
|
|
if (connection)
|
|
|
|
g_socket_client_emit_event (client, G_SOCKET_CLIENT_PROXY_NEGOTIATED, connectable, connection);
|
2010-08-10 22:48:45 +02:00
|
|
|
}
|
2014-12-09 13:35:06 +01:00
|
|
|
else
|
2010-08-10 22:48:45 +02:00
|
|
|
{
|
2020-10-09 01:02:56 +02:00
|
|
|
g_set_error (&error_info->tmp_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
|
2016-09-30 05:47:15 +02:00
|
|
|
_("Proxy protocol “%s” is not supported."),
|
2010-08-10 22:48:45 +02:00
|
|
|
protocol);
|
2020-10-09 01:02:56 +02:00
|
|
|
consider_tmp_error (error_info, G_SOCKET_CLIENT_PROXY_NEGOTIATING);
|
2010-08-10 22:48:45 +02:00
|
|
|
g_object_unref (connection);
|
|
|
|
connection = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-17 19:04:04 +01:00
|
|
|
if (!application_proxy && connection && client->priv->tls)
|
2009-12-21 20:50:32 +01:00
|
|
|
{
|
2010-12-07 16:06:38 +01:00
|
|
|
GIOStream *tlsconn;
|
2009-12-21 20:50:32 +01:00
|
|
|
|
2020-10-09 01:02:56 +02:00
|
|
|
tlsconn = g_tls_client_connection_new (connection, connectable, &error_info->tmp_error);
|
2009-12-21 20:50:32 +01:00
|
|
|
g_object_unref (connection);
|
2010-12-07 16:06:38 +01:00
|
|
|
connection = tlsconn;
|
2009-12-21 20:50:32 +01:00
|
|
|
|
|
|
|
if (tlsconn)
|
|
|
|
{
|
2021-08-04 22:20:41 +02:00
|
|
|
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
|
2010-12-07 16:06:38 +01:00
|
|
|
g_tls_client_connection_set_validation_flags (G_TLS_CLIENT_CONNECTION (tlsconn),
|
|
|
|
client->priv->tls_validation_flags);
|
2021-08-04 22:20:41 +02:00
|
|
|
G_GNUC_END_IGNORE_DEPRECATIONS
|
2010-08-20 19:04:19 +02:00
|
|
|
g_socket_client_emit_event (client, G_SOCKET_CLIENT_TLS_HANDSHAKING, connectable, connection);
|
|
|
|
if (g_tls_connection_handshake (G_TLS_CONNECTION (tlsconn),
|
2020-10-09 01:02:56 +02:00
|
|
|
cancellable, &error_info->tmp_error))
|
2010-08-20 19:04:19 +02:00
|
|
|
{
|
|
|
|
g_socket_client_emit_event (client, G_SOCKET_CLIENT_TLS_HANDSHAKED, connectable, connection);
|
|
|
|
}
|
|
|
|
else
|
2009-12-21 20:50:32 +01:00
|
|
|
{
|
2020-10-09 01:02:56 +02:00
|
|
|
consider_tmp_error (error_info, G_SOCKET_CLIENT_TLS_HANDSHAKING);
|
2009-12-21 20:50:32 +01:00
|
|
|
g_object_unref (tlsconn);
|
|
|
|
connection = NULL;
|
|
|
|
}
|
|
|
|
}
|
2020-10-09 01:02:56 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
consider_tmp_error (error_info, G_SOCKET_CLIENT_TLS_HANDSHAKING);
|
|
|
|
}
|
2009-12-21 20:50:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (connection && !G_IS_SOCKET_CONNECTION (connection))
|
|
|
|
{
|
|
|
|
GSocketConnection *wrapper_connection;
|
|
|
|
|
|
|
|
wrapper_connection = g_tcp_wrapper_connection_new (connection, socket);
|
|
|
|
g_object_unref (connection);
|
|
|
|
connection = (GIOStream *)wrapper_connection;
|
|
|
|
}
|
|
|
|
|
make GProxyConnection public, as GTcpWrapperConnection
GProxyConnection is a class that was added for proxy support;
g_socket_client_connect() returns a GSocketConnection, but in some
cases (eg, encrypted SOCKS), GProxy might return a GIOStream that is
not a GSocketConnection. In that case, GSocketClient would wrap the
stream up in a GProxyConnection, which is a subclass of
GSocketConnection but uses the input/output streams of the wrapped
connection.
GTlsConnection is not a GSocketConnection, so it has the same problem,
so it will need the same treatment. Rename the class to
GTcpWrapperStream, and make it public, so people can extract the base
stream from it when necessary.
(This is not ideal and GSocketClient will need to be revisited as an
API at some point...)
https://bugzilla.gnome.org/show_bug.cgi?id=588189
2010-11-02 01:22:24 +01:00
|
|
|
g_object_unref (socket);
|
2009-05-15 21:26:24 +02:00
|
|
|
g_object_unref (address);
|
|
|
|
}
|
|
|
|
g_object_unref (enumerator);
|
|
|
|
|
2020-10-09 01:02:56 +02:00
|
|
|
if (!connection)
|
|
|
|
g_propagate_error (error, g_steal_pointer (&error_info->best_error));
|
|
|
|
socket_client_error_info_free (error_info);
|
|
|
|
|
2010-08-20 19:04:19 +02:00
|
|
|
g_socket_client_emit_event (client, G_SOCKET_CLIENT_COMPLETE, connectable, connection);
|
2009-12-21 20:50:32 +01:00
|
|
|
return G_SOCKET_CONNECTION (connection);
|
2009-05-15 21:26:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_socket_client_connect_to_host:
|
2010-04-30 00:51:42 +02:00
|
|
|
* @client: a #GSocketClient
|
2009-05-15 21:26:24 +02:00
|
|
|
* @host_and_port: the name and optionally port of the host to connect to
|
|
|
|
* @default_port: the default port to connect to
|
2016-10-29 03:29:02 +02:00
|
|
|
* @cancellable: (nullable): a #GCancellable, or %NULL
|
2009-05-15 21:26:24 +02:00
|
|
|
* @error: a pointer to a #GError, or %NULL
|
|
|
|
*
|
|
|
|
* This is a helper function for g_socket_client_connect().
|
|
|
|
*
|
|
|
|
* Attempts to create a TCP connection to the named host.
|
|
|
|
*
|
2011-04-20 19:08:06 +02:00
|
|
|
* @host_and_port may be in any of a number of recognized formats; an IPv6
|
2009-05-15 21:26:24 +02:00
|
|
|
* address, an IPv4 address, or a domain name (in which case a DNS
|
|
|
|
* lookup is performed). Quoting with [] is supported for all address
|
|
|
|
* types. A port override may be specified in the usual way with a
|
|
|
|
* colon. Ports may be given as decimal numbers or symbolic names (in
|
|
|
|
* which case an /etc/services lookup is performed).
|
|
|
|
*
|
|
|
|
* If no port override is given in @host_and_port then @default_port will be
|
|
|
|
* used as the port number to connect to.
|
|
|
|
*
|
|
|
|
* In general, @host_and_port is expected to be provided by the user (allowing
|
2011-04-20 19:08:06 +02:00
|
|
|
* them to give the hostname, and a port override if necessary) and
|
2009-05-15 21:26:24 +02:00
|
|
|
* @default_port is expected to be provided by the application.
|
2009-05-28 00:20:08 +02:00
|
|
|
*
|
2009-05-15 21:26:24 +02:00
|
|
|
* In the case that an IP address is given, a single connection
|
|
|
|
* attempt is made. In the case that a name is given, multiple
|
|
|
|
* connection attempts may be made, in turn and according to the
|
|
|
|
* number of address records in DNS, until a connection succeeds.
|
|
|
|
*
|
|
|
|
* Upon a successful connection, a new #GSocketConnection is constructed
|
|
|
|
* and returned. The caller owns this new object and must drop their
|
|
|
|
* reference to it when finished with it.
|
|
|
|
*
|
|
|
|
* In the event of any failure (DNS error, service not found, no hosts
|
|
|
|
* connectable) %NULL is returned and @error (if non-%NULL) is set
|
|
|
|
* accordingly.
|
|
|
|
*
|
2010-09-24 23:24:41 +02:00
|
|
|
* Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
|
2009-05-15 21:26:24 +02:00
|
|
|
*
|
|
|
|
* Since: 2.22
|
2009-05-28 00:20:08 +02:00
|
|
|
*/
|
2009-05-15 21:26:24 +02:00
|
|
|
GSocketConnection *
|
2009-05-28 00:20:08 +02:00
|
|
|
g_socket_client_connect_to_host (GSocketClient *client,
|
|
|
|
const gchar *host_and_port,
|
2009-06-12 15:57:51 +02:00
|
|
|
guint16 default_port,
|
2009-05-28 00:20:08 +02:00
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error)
|
2009-05-15 21:26:24 +02:00
|
|
|
{
|
|
|
|
GSocketConnectable *connectable;
|
|
|
|
GSocketConnection *connection;
|
|
|
|
|
|
|
|
connectable = g_network_address_parse (host_and_port, default_port, error);
|
|
|
|
if (connectable == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
connection = g_socket_client_connect (client, connectable,
|
|
|
|
cancellable, error);
|
|
|
|
g_object_unref (connectable);
|
|
|
|
|
|
|
|
return connection;
|
|
|
|
}
|
|
|
|
|
2009-05-20 12:41:50 +02:00
|
|
|
/**
|
|
|
|
* g_socket_client_connect_to_service:
|
|
|
|
* @client: a #GSocketConnection
|
|
|
|
* @domain: a domain name
|
|
|
|
* @service: the name of the service to connect to
|
2016-10-29 03:29:02 +02:00
|
|
|
* @cancellable: (nullable): a #GCancellable, or %NULL
|
2009-05-20 12:41:50 +02:00
|
|
|
* @error: a pointer to a #GError, or %NULL
|
|
|
|
*
|
|
|
|
* Attempts to create a TCP connection to a service.
|
|
|
|
*
|
|
|
|
* This call looks up the SRV record for @service at @domain for the
|
|
|
|
* "tcp" protocol. It then attempts to connect, in turn, to each of
|
|
|
|
* the hosts providing the service until either a connection succeeds
|
|
|
|
* or there are no hosts remaining.
|
|
|
|
*
|
|
|
|
* Upon a successful connection, a new #GSocketConnection is constructed
|
|
|
|
* and returned. The caller owns this new object and must drop their
|
|
|
|
* reference to it when finished with it.
|
|
|
|
*
|
|
|
|
* In the event of any failure (DNS error, service not found, no hosts
|
|
|
|
* connectable) %NULL is returned and @error (if non-%NULL) is set
|
|
|
|
* accordingly.
|
2011-11-21 17:03:31 +01:00
|
|
|
*
|
|
|
|
* Returns: (transfer full): a #GSocketConnection if successful, or %NULL on error
|
2009-05-28 00:20:08 +02:00
|
|
|
*/
|
2009-05-20 12:41:50 +02:00
|
|
|
GSocketConnection *
|
|
|
|
g_socket_client_connect_to_service (GSocketClient *client,
|
2009-05-28 00:20:08 +02:00
|
|
|
const gchar *domain,
|
|
|
|
const gchar *service,
|
2009-05-20 12:41:50 +02:00
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
GSocketConnectable *connectable;
|
|
|
|
GSocketConnection *connection;
|
|
|
|
|
|
|
|
connectable = g_network_service_new (service, "tcp", domain);
|
|
|
|
connection = g_socket_client_connect (client, connectable,
|
|
|
|
cancellable, error);
|
|
|
|
g_object_unref (connectable);
|
|
|
|
|
|
|
|
return connection;
|
|
|
|
}
|
|
|
|
|
2010-04-30 00:51:42 +02:00
|
|
|
/**
|
|
|
|
* g_socket_client_connect_to_uri:
|
|
|
|
* @client: a #GSocketClient
|
|
|
|
* @uri: A network URI
|
|
|
|
* @default_port: the default port to connect to
|
2016-10-29 03:29:02 +02:00
|
|
|
* @cancellable: (nullable): a #GCancellable, or %NULL
|
2010-04-30 00:51:42 +02:00
|
|
|
* @error: a pointer to a #GError, or %NULL
|
|
|
|
*
|
|
|
|
* This is a helper function for g_socket_client_connect().
|
|
|
|
*
|
|
|
|
* Attempts to create a TCP connection with a network URI.
|
|
|
|
*
|
|
|
|
* @uri may be any valid URI containing an "authority" (hostname/port)
|
|
|
|
* component. If a port is not specified in the URI, @default_port
|
2010-12-06 17:21:16 +01:00
|
|
|
* will be used. TLS will be negotiated if #GSocketClient:tls is %TRUE.
|
|
|
|
* (#GSocketClient does not know to automatically assume TLS for
|
|
|
|
* certain URI schemes.)
|
2010-04-30 00:51:42 +02:00
|
|
|
*
|
|
|
|
* Using this rather than g_socket_client_connect() or
|
|
|
|
* g_socket_client_connect_to_host() allows #GSocketClient to
|
|
|
|
* determine when to use application-specific proxy protocols.
|
|
|
|
*
|
|
|
|
* Upon a successful connection, a new #GSocketConnection is constructed
|
|
|
|
* and returned. The caller owns this new object and must drop their
|
|
|
|
* reference to it when finished with it.
|
|
|
|
*
|
|
|
|
* In the event of any failure (DNS error, service not found, no hosts
|
|
|
|
* connectable) %NULL is returned and @error (if non-%NULL) is set
|
|
|
|
* accordingly.
|
|
|
|
*
|
2010-09-24 23:24:41 +02:00
|
|
|
* Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
|
2010-04-30 00:51:42 +02:00
|
|
|
*
|
|
|
|
* Since: 2.26
|
|
|
|
*/
|
|
|
|
GSocketConnection *
|
|
|
|
g_socket_client_connect_to_uri (GSocketClient *client,
|
|
|
|
const gchar *uri,
|
|
|
|
guint16 default_port,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
GSocketConnectable *connectable;
|
|
|
|
GSocketConnection *connection;
|
|
|
|
|
|
|
|
connectable = g_network_address_parse_uri (uri, default_port, error);
|
|
|
|
if (connectable == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
connection = g_socket_client_connect (client, connectable,
|
|
|
|
cancellable, error);
|
|
|
|
g_object_unref (connectable);
|
|
|
|
|
|
|
|
return connection;
|
|
|
|
}
|
|
|
|
|
2009-05-15 21:26:24 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
2019-01-29 16:07:06 +01:00
|
|
|
GTask *task; /* unowned */
|
2009-05-15 21:26:24 +02:00
|
|
|
GSocketClient *client;
|
|
|
|
|
2009-12-21 20:50:32 +01:00
|
|
|
GSocketConnectable *connectable;
|
2009-05-15 21:26:24 +02:00
|
|
|
GSocketAddressEnumerator *enumerator;
|
2020-01-24 04:58:41 +01:00
|
|
|
GCancellable *enumeration_cancellable;
|
2022-06-13 14:06:06 +02:00
|
|
|
GCancellable *enumeration_parent_cancellable; /* (nullable) (owned) */
|
|
|
|
gulong enumeration_cancelled_id;
|
2009-05-15 21:26:24 +02:00
|
|
|
|
2023-02-22 17:34:05 +01:00
|
|
|
GSList *connection_attempts; /* (element-type ConnectionAttempt) (owned) */
|
2020-01-24 04:58:41 +01:00
|
|
|
GSList *successful_connections;
|
2020-10-09 01:02:56 +02:00
|
|
|
SocketClientErrorInfo *error_info;
|
2019-02-21 21:31:52 +01:00
|
|
|
|
2020-01-24 04:58:41 +01:00
|
|
|
gboolean enumerated_at_least_once;
|
|
|
|
gboolean enumeration_completed;
|
|
|
|
gboolean connection_in_progress;
|
2019-02-21 21:31:52 +01:00
|
|
|
gboolean completed;
|
2009-05-15 21:26:24 +02:00
|
|
|
} GSocketClientAsyncConnectData;
|
|
|
|
|
2018-10-29 14:53:07 +01:00
|
|
|
static void connection_attempt_unref (gpointer attempt);
|
|
|
|
|
2009-05-15 21:26:24 +02:00
|
|
|
static void
|
2012-08-02 21:48:22 +02:00
|
|
|
g_socket_client_async_connect_data_free (GSocketClientAsyncConnectData *data)
|
2009-05-15 21:26:24 +02:00
|
|
|
{
|
2019-01-29 16:07:06 +01:00
|
|
|
data->task = NULL;
|
2012-08-02 21:48:22 +02:00
|
|
|
g_clear_object (&data->connectable);
|
|
|
|
g_clear_object (&data->enumerator);
|
2022-06-13 14:06:06 +02:00
|
|
|
|
|
|
|
g_cancellable_disconnect (data->enumeration_parent_cancellable, data->enumeration_cancelled_id);
|
|
|
|
g_clear_object (&data->enumeration_parent_cancellable);
|
|
|
|
data->enumeration_cancelled_id = 0;
|
2020-01-24 04:58:41 +01:00
|
|
|
g_clear_object (&data->enumeration_cancellable);
|
2022-06-13 14:06:06 +02:00
|
|
|
|
2018-10-29 14:53:07 +01:00
|
|
|
g_slist_free_full (data->connection_attempts, connection_attempt_unref);
|
2020-01-24 04:58:41 +01:00
|
|
|
g_slist_free_full (data->successful_connections, connection_attempt_unref);
|
2010-08-20 19:04:19 +02:00
|
|
|
|
2020-10-09 01:02:56 +02:00
|
|
|
g_clear_pointer (&data->error_info, socket_client_error_info_free);
|
2009-05-15 21:26:24 +02:00
|
|
|
|
2012-08-02 21:48:22 +02:00
|
|
|
g_slice_free (GSocketClientAsyncConnectData, data);
|
|
|
|
}
|
2009-12-21 20:50:32 +01:00
|
|
|
|
2018-10-29 14:53:07 +01:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GSocketAddress *address;
|
|
|
|
GSocket *socket;
|
|
|
|
GIOStream *connection;
|
2020-01-24 04:58:41 +01:00
|
|
|
GProxyAddress *proxy_addr;
|
2018-10-29 14:53:07 +01:00
|
|
|
GSocketClientAsyncConnectData *data; /* unowned */
|
|
|
|
GSource *timeout_source;
|
|
|
|
GCancellable *cancellable;
|
2022-07-12 17:07:23 +02:00
|
|
|
GCancellable *task_cancellable; /* (owned); this is equal to g_task_get_cancellable (ConnectionAttempt.data->task), but with a longer lifetime */
|
2022-06-13 14:06:06 +02:00
|
|
|
gulong cancelled_id;
|
2018-10-29 14:53:07 +01:00
|
|
|
grefcount ref;
|
|
|
|
} ConnectionAttempt;
|
|
|
|
|
|
|
|
static ConnectionAttempt *
|
|
|
|
connection_attempt_new (void)
|
|
|
|
{
|
|
|
|
ConnectionAttempt *attempt = g_new0 (ConnectionAttempt, 1);
|
|
|
|
g_ref_count_init (&attempt->ref);
|
|
|
|
return attempt;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ConnectionAttempt *
|
|
|
|
connection_attempt_ref (ConnectionAttempt *attempt)
|
|
|
|
{
|
|
|
|
g_ref_count_inc (&attempt->ref);
|
|
|
|
return attempt;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
connection_attempt_unref (gpointer pointer)
|
|
|
|
{
|
|
|
|
ConnectionAttempt *attempt = pointer;
|
|
|
|
if (g_ref_count_dec (&attempt->ref))
|
|
|
|
{
|
|
|
|
g_clear_object (&attempt->address);
|
|
|
|
g_clear_object (&attempt->socket);
|
|
|
|
g_clear_object (&attempt->connection);
|
2022-07-12 17:07:23 +02:00
|
|
|
g_cancellable_disconnect (attempt->task_cancellable, attempt->cancelled_id);
|
|
|
|
g_clear_object (&attempt->task_cancellable);
|
2022-06-13 14:06:06 +02:00
|
|
|
attempt->cancelled_id = 0;
|
2018-10-29 14:53:07 +01:00
|
|
|
g_clear_object (&attempt->cancellable);
|
2020-01-24 04:58:41 +01:00
|
|
|
g_clear_object (&attempt->proxy_addr);
|
2018-10-29 14:53:07 +01:00
|
|
|
if (attempt->timeout_source)
|
|
|
|
{
|
|
|
|
g_source_destroy (attempt->timeout_source);
|
|
|
|
g_source_unref (attempt->timeout_source);
|
|
|
|
}
|
|
|
|
g_free (attempt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
connection_attempt_remove (ConnectionAttempt *attempt)
|
|
|
|
{
|
|
|
|
attempt->data->connection_attempts = g_slist_remove (attempt->data->connection_attempts, attempt);
|
|
|
|
connection_attempt_unref (attempt);
|
|
|
|
}
|
|
|
|
|
2012-08-02 21:48:22 +02:00
|
|
|
static void
|
2020-01-24 04:58:41 +01:00
|
|
|
cancel_all_attempts (GSocketClientAsyncConnectData *data)
|
2012-08-02 21:48:22 +02:00
|
|
|
{
|
2020-01-24 04:58:41 +01:00
|
|
|
GSList *l;
|
2012-08-02 21:48:22 +02:00
|
|
|
|
2020-01-24 04:58:41 +01:00
|
|
|
for (l = data->connection_attempts; l; l = g_slist_next (l))
|
2012-08-02 21:48:22 +02:00
|
|
|
{
|
2020-01-24 04:58:41 +01:00
|
|
|
ConnectionAttempt *attempt_entry = l->data;
|
|
|
|
g_cancellable_cancel (attempt_entry->cancellable);
|
|
|
|
connection_attempt_unref (attempt_entry);
|
2009-05-15 21:26:24 +02:00
|
|
|
}
|
2020-01-24 04:58:41 +01:00
|
|
|
g_slist_free (data->connection_attempts);
|
|
|
|
data->connection_attempts = NULL;
|
2009-05-15 21:26:24 +02:00
|
|
|
|
2020-01-24 04:58:41 +01:00
|
|
|
g_slist_free_full (data->successful_connections, connection_attempt_unref);
|
|
|
|
data->successful_connections = NULL;
|
|
|
|
|
|
|
|
g_cancellable_cancel (data->enumeration_cancellable);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_socket_client_async_connect_complete (ConnectionAttempt *attempt)
|
|
|
|
{
|
|
|
|
GSocketClientAsyncConnectData *data = attempt->data;
|
|
|
|
GError *error = NULL;
|
|
|
|
g_assert (attempt->connection);
|
|
|
|
g_assert (!data->completed);
|
|
|
|
|
|
|
|
if (!G_IS_SOCKET_CONNECTION (attempt->connection))
|
2019-02-21 21:31:52 +01:00
|
|
|
{
|
2020-01-24 04:58:41 +01:00
|
|
|
GSocketConnection *wrapper_connection;
|
2019-04-16 19:22:35 +02:00
|
|
|
|
2020-01-24 04:58:41 +01:00
|
|
|
wrapper_connection = g_tcp_wrapper_connection_new (attempt->connection, attempt->socket);
|
|
|
|
g_object_unref (attempt->connection);
|
|
|
|
attempt->connection = (GIOStream *)wrapper_connection;
|
|
|
|
}
|
2019-04-16 19:22:35 +02:00
|
|
|
|
2020-01-24 04:58:41 +01:00
|
|
|
data->completed = TRUE;
|
|
|
|
cancel_all_attempts (data);
|
|
|
|
|
|
|
|
if (g_cancellable_set_error_if_cancelled (g_task_get_cancellable (data->task), &error))
|
|
|
|
{
|
|
|
|
g_debug ("GSocketClient: Connection cancelled!");
|
|
|
|
g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_COMPLETE, data->connectable, NULL);
|
|
|
|
g_task_return_error (data->task, g_steal_pointer (&error));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_debug ("GSocketClient: Connection successful!");
|
|
|
|
g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_COMPLETE, data->connectable, attempt->connection);
|
|
|
|
g_task_return_pointer (data->task, g_steal_pointer (&attempt->connection), g_object_unref);
|
2019-02-21 21:31:52 +01:00
|
|
|
}
|
|
|
|
|
2020-01-24 04:58:41 +01:00
|
|
|
connection_attempt_unref (attempt);
|
2012-08-02 21:48:22 +02:00
|
|
|
g_object_unref (data->task);
|
2009-05-15 21:26:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_socket_client_enumerator_callback (GObject *object,
|
|
|
|
GAsyncResult *result,
|
|
|
|
gpointer user_data);
|
|
|
|
|
2010-08-10 22:48:45 +02:00
|
|
|
static void
|
2019-01-29 16:07:06 +01:00
|
|
|
enumerator_next_async (GSocketClientAsyncConnectData *data,
|
|
|
|
gboolean add_task_ref)
|
2010-08-10 22:48:45 +02:00
|
|
|
{
|
2019-01-29 16:07:06 +01:00
|
|
|
/* Each enumeration takes a ref. This arg just avoids repeated unrefs when
|
|
|
|
an enumeration starts another enumeration */
|
|
|
|
if (add_task_ref)
|
|
|
|
g_object_ref (data->task);
|
|
|
|
|
gsocketclient: emit RESOLVING/RESOLVED events only once
GSocketAddressEnumerator encapsulates the details of how DNS happens, so
we don't have to think about it. But we may have taken encapsulation a
bit too far, here. Usually, we resolve a domain name to a list of IPv4
and IPv6 addresses. Then we go through each address in the list and try
to connect to it. Name resolution happens exactly once, at the start.
It doesn't happen each time we enumerate the enumerator. In theory, it
*could*, because we've designed these APIs to be agnostic of underlying
implementation details like DNS and network protocols. But in practice,
we know that's not really what's happening. It's weird to say that we
are RESOLVING what we know to be the same name multiple times. Behind
the scenes, we're not doing that.
This also fixes #1994, where enumeration can end with a RESOLVING event,
even though this is supposed to be the first event rather than the last.
I thought this would be hard to fix, even requiring new public API in
GSocketAddressEnumerator to peek ahead to see if the next enumeration is
going to return NULL. Then I decided we should just fake it: always emit
both RESOLVING and RESOLVED at the same time right after each
enumeration. Finally, I realized we can emit them at the correct time if
we simply assume resolving only happens the first time. This seems like
the most elegant of the possible solutions.
Now, this is a behavior change, and arguably an API break, but it should
align better with reasonable expectations of how GSocketClientEvent
ought to work. I don't expect it to break anything besides tests that
check which order GSocketClientEvent events are emitted in. (Currently,
libsoup has such tests, which will need to be updated.) Ideally we would
have GLib-level tests as well, but in a concession to pragmatism, it's a
lot easier to keep network tests in libsoup.
2020-10-06 22:39:45 +02:00
|
|
|
if (!data->enumerated_at_least_once)
|
|
|
|
g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_RESOLVING, data->connectable, NULL);
|
2020-01-24 04:58:41 +01:00
|
|
|
g_debug ("GSocketClient: Starting new address enumeration");
|
2010-08-10 22:48:45 +02:00
|
|
|
g_socket_address_enumerator_next_async (data->enumerator,
|
2020-01-24 04:58:41 +01:00
|
|
|
data->enumeration_cancellable,
|
2010-08-10 22:48:45 +02:00
|
|
|
g_socket_client_enumerator_callback,
|
|
|
|
data);
|
|
|
|
}
|
|
|
|
|
2020-01-24 04:58:41 +01:00
|
|
|
static void try_next_connection_or_finish (GSocketClientAsyncConnectData *, gboolean);
|
|
|
|
|
2010-08-10 22:48:45 +02:00
|
|
|
static void
|
2009-12-21 20:50:32 +01:00
|
|
|
g_socket_client_tls_handshake_callback (GObject *object,
|
2010-08-10 22:48:45 +02:00
|
|
|
GAsyncResult *result,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
2020-01-24 04:58:41 +01:00
|
|
|
ConnectionAttempt *attempt = user_data;
|
|
|
|
GSocketClientAsyncConnectData *data = attempt->data;
|
2010-08-10 22:48:45 +02:00
|
|
|
|
2009-12-21 20:50:32 +01:00
|
|
|
if (g_tls_connection_handshake_finish (G_TLS_CONNECTION (object),
|
|
|
|
result,
|
2020-10-09 01:02:56 +02:00
|
|
|
&data->error_info->tmp_error))
|
2009-12-21 20:50:32 +01:00
|
|
|
{
|
2020-01-24 04:58:41 +01:00
|
|
|
g_object_unref (attempt->connection);
|
|
|
|
attempt->connection = G_IO_STREAM (object);
|
2011-08-10 15:56:13 +02:00
|
|
|
|
2020-01-24 04:58:41 +01:00
|
|
|
g_debug ("GSocketClient: TLS handshake succeeded");
|
|
|
|
g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_TLS_HANDSHAKED, data->connectable, attempt->connection);
|
|
|
|
g_socket_client_async_connect_complete (attempt);
|
2009-12-21 20:50:32 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_object_unref (object);
|
2020-01-24 04:58:41 +01:00
|
|
|
connection_attempt_unref (attempt);
|
2020-10-09 01:02:56 +02:00
|
|
|
|
|
|
|
g_debug ("GSocketClient: TLS handshake failed: %s", data->error_info->tmp_error->message);
|
|
|
|
consider_tmp_error (data->error_info, G_SOCKET_CLIENT_TLS_HANDSHAKING);
|
2020-01-24 04:58:41 +01:00
|
|
|
try_next_connection_or_finish (data, TRUE);
|
2009-12-21 20:50:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-01-24 04:58:41 +01:00
|
|
|
g_socket_client_tls_handshake (ConnectionAttempt *attempt)
|
2009-12-21 20:50:32 +01:00
|
|
|
{
|
2020-01-24 04:58:41 +01:00
|
|
|
GSocketClientAsyncConnectData *data = attempt->data;
|
2010-12-07 16:06:38 +01:00
|
|
|
GIOStream *tlsconn;
|
2009-12-21 20:50:32 +01:00
|
|
|
|
|
|
|
if (!data->client->priv->tls)
|
2010-08-10 22:48:45 +02:00
|
|
|
{
|
2020-01-24 04:58:41 +01:00
|
|
|
g_socket_client_async_connect_complete (attempt);
|
2009-12-21 20:50:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-24 04:58:41 +01:00
|
|
|
g_debug ("GSocketClient: Starting TLS handshake");
|
|
|
|
tlsconn = g_tls_client_connection_new (attempt->connection,
|
2009-12-21 20:50:32 +01:00
|
|
|
data->connectable,
|
2020-10-09 01:02:56 +02:00
|
|
|
&data->error_info->tmp_error);
|
2009-12-21 20:50:32 +01:00
|
|
|
if (tlsconn)
|
|
|
|
{
|
2021-08-04 22:20:41 +02:00
|
|
|
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
|
2010-12-07 16:06:38 +01:00
|
|
|
g_tls_client_connection_set_validation_flags (G_TLS_CLIENT_CONNECTION (tlsconn),
|
|
|
|
data->client->priv->tls_validation_flags);
|
2021-08-04 22:20:41 +02:00
|
|
|
G_GNUC_END_IGNORE_DEPRECATIONS
|
2010-08-20 19:04:19 +02:00
|
|
|
g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_TLS_HANDSHAKING, data->connectable, G_IO_STREAM (tlsconn));
|
2009-12-21 20:50:32 +01:00
|
|
|
g_tls_connection_handshake_async (G_TLS_CONNECTION (tlsconn),
|
|
|
|
G_PRIORITY_DEFAULT,
|
2012-08-02 21:48:22 +02:00
|
|
|
g_task_get_cancellable (data->task),
|
2009-12-21 20:50:32 +01:00
|
|
|
g_socket_client_tls_handshake_callback,
|
2020-01-24 04:58:41 +01:00
|
|
|
attempt);
|
2010-08-10 22:48:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-01-24 04:58:41 +01:00
|
|
|
connection_attempt_unref (attempt);
|
2020-10-09 01:02:56 +02:00
|
|
|
|
|
|
|
consider_tmp_error (data->error_info, G_SOCKET_CLIENT_TLS_HANDSHAKING);
|
2020-01-24 04:58:41 +01:00
|
|
|
try_next_connection_or_finish (data, TRUE);
|
2009-12-21 20:50:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_socket_client_proxy_connect_callback (GObject *object,
|
|
|
|
GAsyncResult *result,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
2020-01-24 04:58:41 +01:00
|
|
|
ConnectionAttempt *attempt = user_data;
|
|
|
|
GSocketClientAsyncConnectData *data = attempt->data;
|
2009-12-21 20:50:32 +01:00
|
|
|
|
2020-01-24 04:58:41 +01:00
|
|
|
g_object_unref (attempt->connection);
|
|
|
|
attempt->connection = g_proxy_connect_finish (G_PROXY (object),
|
|
|
|
result,
|
2020-10-09 01:02:56 +02:00
|
|
|
&data->error_info->tmp_error);
|
2020-01-24 04:58:41 +01:00
|
|
|
if (attempt->connection)
|
2010-08-20 19:04:19 +02:00
|
|
|
{
|
2020-01-24 04:58:41 +01:00
|
|
|
g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_PROXY_NEGOTIATED, data->connectable, attempt->connection);
|
2020-10-09 01:02:56 +02:00
|
|
|
g_socket_client_tls_handshake (attempt);
|
2010-08-20 19:04:19 +02:00
|
|
|
}
|
|
|
|
else
|
2009-12-21 20:50:32 +01:00
|
|
|
{
|
2020-01-24 04:58:41 +01:00
|
|
|
connection_attempt_unref (attempt);
|
2020-10-09 01:02:56 +02:00
|
|
|
|
|
|
|
consider_tmp_error (data->error_info, G_SOCKET_CLIENT_PROXY_NEGOTIATING);
|
2020-01-24 04:58:41 +01:00
|
|
|
try_next_connection_or_finish (data, TRUE);
|
2010-08-10 22:48:45 +02:00
|
|
|
}
|
2020-01-24 04:58:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
complete_connection_with_error (GSocketClientAsyncConnectData *data,
|
|
|
|
GError *error)
|
|
|
|
{
|
|
|
|
g_debug ("GSocketClient: Connection failed: %s", error->message);
|
|
|
|
g_assert (!data->completed);
|
|
|
|
|
|
|
|
g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_COMPLETE, data->connectable, NULL);
|
|
|
|
data->completed = TRUE;
|
|
|
|
cancel_all_attempts (data);
|
|
|
|
g_task_return_error (data->task, error);
|
2010-08-10 22:48:45 +02:00
|
|
|
}
|
|
|
|
|
2019-01-29 16:07:06 +01:00
|
|
|
static gboolean
|
2019-02-21 21:31:52 +01:00
|
|
|
task_completed_or_cancelled (GSocketClientAsyncConnectData *data)
|
2019-01-29 16:07:06 +01:00
|
|
|
{
|
2019-02-21 21:31:52 +01:00
|
|
|
GTask *task = data->task;
|
|
|
|
GCancellable *cancellable = g_task_get_cancellable (task);
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
if (data->completed)
|
2019-01-29 16:07:06 +01:00
|
|
|
return TRUE;
|
2019-02-21 21:31:52 +01:00
|
|
|
else if (g_cancellable_set_error_if_cancelled (cancellable, &error))
|
|
|
|
{
|
2020-01-24 04:58:41 +01:00
|
|
|
complete_connection_with_error (data, g_steal_pointer (&error));
|
2019-01-29 16:07:06 +01:00
|
|
|
return TRUE;
|
2019-02-21 21:31:52 +01:00
|
|
|
}
|
2019-01-29 16:07:06 +01:00
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2020-01-24 04:58:41 +01:00
|
|
|
static gboolean
|
|
|
|
try_next_successful_connection (GSocketClientAsyncConnectData *data)
|
|
|
|
{
|
|
|
|
ConnectionAttempt *attempt;
|
|
|
|
const gchar *protocol;
|
|
|
|
GProxy *proxy;
|
|
|
|
|
|
|
|
if (data->connection_in_progress)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
g_assert (data->successful_connections != NULL);
|
|
|
|
attempt = data->successful_connections->data;
|
|
|
|
g_assert (attempt != NULL);
|
|
|
|
data->successful_connections = g_slist_remove (data->successful_connections, attempt);
|
|
|
|
data->connection_in_progress = TRUE;
|
|
|
|
|
|
|
|
g_debug ("GSocketClient: Starting application layer connection");
|
|
|
|
|
|
|
|
if (!attempt->proxy_addr)
|
|
|
|
{
|
|
|
|
g_socket_client_tls_handshake (g_steal_pointer (&attempt));
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
protocol = g_proxy_address_get_protocol (attempt->proxy_addr);
|
|
|
|
|
|
|
|
/* The connection should not be anything other than TCP,
|
|
|
|
* but let's put a safety guard in case
|
|
|
|
*/
|
|
|
|
if (!G_IS_TCP_CONNECTION (attempt->connection))
|
|
|
|
{
|
|
|
|
g_critical ("Trying to proxy over non-TCP connection, this is "
|
|
|
|
"most likely a bug in GLib IO library.");
|
|
|
|
|
2020-10-09 01:02:56 +02:00
|
|
|
g_set_error_literal (&data->error_info->tmp_error,
|
2020-01-24 04:58:41 +01:00
|
|
|
G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
|
|
|
|
_("Proxying over a non-TCP connection is not supported."));
|
2020-10-09 01:02:56 +02:00
|
|
|
consider_tmp_error (data->error_info, G_SOCKET_CLIENT_PROXY_NEGOTIATING);
|
2020-01-24 04:58:41 +01:00
|
|
|
}
|
|
|
|
else if (g_hash_table_contains (data->client->priv->app_proxies, protocol))
|
|
|
|
{
|
|
|
|
/* 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 (g_steal_pointer (&attempt));
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else if ((proxy = g_proxy_get_default_for_protocol (protocol)))
|
|
|
|
{
|
|
|
|
GIOStream *connection = attempt->connection;
|
|
|
|
GProxyAddress *proxy_addr = attempt->proxy_addr;
|
|
|
|
|
|
|
|
g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_PROXY_NEGOTIATING, data->connectable, attempt->connection);
|
|
|
|
g_debug ("GSocketClient: Starting proxy connection");
|
|
|
|
g_proxy_connect_async (proxy,
|
|
|
|
connection,
|
|
|
|
proxy_addr,
|
|
|
|
g_task_get_cancellable (data->task),
|
|
|
|
g_socket_client_proxy_connect_callback,
|
|
|
|
g_steal_pointer (&attempt));
|
|
|
|
g_object_unref (proxy);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-09 01:02:56 +02:00
|
|
|
g_set_error (&data->error_info->tmp_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
|
2020-01-24 04:58:41 +01:00
|
|
|
_("Proxy protocol “%s” is not supported."),
|
|
|
|
protocol);
|
2020-10-09 01:02:56 +02:00
|
|
|
consider_tmp_error (data->error_info, G_SOCKET_CLIENT_PROXY_NEGOTIATING);
|
2020-01-24 04:58:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
data->connection_in_progress = FALSE;
|
|
|
|
g_clear_pointer (&attempt, connection_attempt_unref);
|
|
|
|
return FALSE; /* All non-return paths are failures */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
try_next_connection_or_finish (GSocketClientAsyncConnectData *data,
|
|
|
|
gboolean end_current_connection)
|
|
|
|
{
|
|
|
|
if (end_current_connection)
|
|
|
|
data->connection_in_progress = FALSE;
|
|
|
|
|
|
|
|
if (data->connection_in_progress)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Keep trying successful connections until one works, each iteration pops one */
|
|
|
|
while (data->successful_connections)
|
|
|
|
{
|
|
|
|
if (try_next_successful_connection (data))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!data->enumeration_completed)
|
|
|
|
{
|
|
|
|
enumerator_next_async (data, FALSE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-09 01:02:56 +02:00
|
|
|
complete_connection_with_error (data, g_steal_pointer (&data->error_info->best_error));
|
2020-01-24 04:58:41 +01:00
|
|
|
}
|
|
|
|
|
2010-08-10 22:48:45 +02:00
|
|
|
static void
|
2011-06-12 22:29:04 +02:00
|
|
|
g_socket_client_connected_callback (GObject *source,
|
|
|
|
GAsyncResult *result,
|
|
|
|
gpointer user_data)
|
2010-08-10 22:48:45 +02:00
|
|
|
{
|
2023-02-22 17:34:05 +01:00
|
|
|
ConnectionAttempt *attempt = g_steal_pointer (&user_data);
|
2018-10-29 14:53:07 +01:00
|
|
|
GSocketClientAsyncConnectData *data = attempt->data;
|
2010-08-10 22:48:45 +02:00
|
|
|
|
2019-02-21 21:31:52 +01:00
|
|
|
if (task_completed_or_cancelled (data) || g_cancellable_is_cancelled (attempt->cancellable))
|
2014-08-21 20:00:13 +02:00
|
|
|
{
|
|
|
|
g_object_unref (data->task);
|
2018-10-29 14:53:07 +01:00
|
|
|
connection_attempt_unref (attempt);
|
2014-08-21 20:00:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-29 14:53:07 +01:00
|
|
|
if (attempt->timeout_source)
|
|
|
|
{
|
|
|
|
g_source_destroy (attempt->timeout_source);
|
|
|
|
g_clear_pointer (&attempt->timeout_source, g_source_unref);
|
|
|
|
}
|
|
|
|
|
2011-06-12 22:29:04 +02:00
|
|
|
if (!g_socket_connection_connect_finish (G_SOCKET_CONNECTION (source),
|
2020-10-09 01:02:56 +02:00
|
|
|
result, &data->error_info->tmp_error))
|
2011-06-12 22:29:04 +02:00
|
|
|
{
|
2018-10-29 14:53:07 +01:00
|
|
|
if (!g_cancellable_is_cancelled (attempt->cancellable))
|
|
|
|
{
|
2020-10-28 16:41:13 +01:00
|
|
|
g_debug ("GSocketClient: Connection attempt failed: %s", data->error_info->tmp_error->message);
|
2020-10-09 01:02:56 +02:00
|
|
|
clarify_connect_error (data->error_info->tmp_error, data->connectable, attempt->address);
|
|
|
|
consider_tmp_error (data->error_info, G_SOCKET_CLIENT_CONNECTING);
|
2019-01-29 16:07:06 +01:00
|
|
|
connection_attempt_remove (attempt);
|
2019-05-09 12:21:52 +02:00
|
|
|
connection_attempt_unref (attempt);
|
2020-01-24 04:58:41 +01:00
|
|
|
try_next_connection_or_finish (data, FALSE);
|
2018-10-29 14:53:07 +01:00
|
|
|
}
|
2020-01-24 04:58:41 +01:00
|
|
|
else /* Silently ignore cancelled attempts */
|
2018-10-29 14:53:07 +01:00
|
|
|
{
|
2020-10-09 01:02:56 +02:00
|
|
|
g_clear_error (&data->error_info->tmp_error);
|
2019-01-29 16:07:06 +01:00
|
|
|
g_object_unref (data->task);
|
|
|
|
connection_attempt_unref (attempt);
|
2018-10-29 14:53:07 +01:00
|
|
|
}
|
2011-06-12 22:29:04 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-24 04:58:41 +01:00
|
|
|
g_socket_connection_set_cached_remote_address ((GSocketConnection*)attempt->connection, NULL);
|
|
|
|
g_debug ("GSocketClient: TCP connection successful");
|
|
|
|
g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_CONNECTED, data->connectable, attempt->connection);
|
2010-08-20 19:04:19 +02:00
|
|
|
|
2011-06-12 22:29:04 +02:00
|
|
|
/* wrong, but backward compatible */
|
2020-01-24 04:58:41 +01:00
|
|
|
g_socket_set_blocking (attempt->socket, TRUE);
|
2011-06-12 22:29:04 +02:00
|
|
|
|
2020-01-24 04:58:41 +01:00
|
|
|
/* This ends the parallel "happy eyeballs" portion of connecting.
|
|
|
|
Now that we have a successful tcp connection we will attempt to connect
|
|
|
|
at the TLS/Proxy layer. If those layers fail we will move on to the next
|
|
|
|
connection.
|
2010-08-10 22:48:45 +02:00
|
|
|
*/
|
2020-01-24 04:58:41 +01:00
|
|
|
connection_attempt_remove (attempt);
|
|
|
|
data->successful_connections = g_slist_append (data->successful_connections, g_steal_pointer (&attempt));
|
|
|
|
try_next_connection_or_finish (data, FALSE);
|
2010-08-10 22:48:45 +02:00
|
|
|
}
|
|
|
|
|
2018-10-29 14:53:07 +01:00
|
|
|
static gboolean
|
|
|
|
on_connection_attempt_timeout (gpointer data)
|
|
|
|
{
|
|
|
|
ConnectionAttempt *attempt = data;
|
|
|
|
|
2020-01-24 04:58:41 +01:00
|
|
|
if (!attempt->data->enumeration_completed)
|
|
|
|
{
|
|
|
|
g_debug ("GSocketClient: Timeout reached, trying another enumeration");
|
|
|
|
enumerator_next_async (attempt->data, TRUE);
|
|
|
|
}
|
2018-10-29 14:53:07 +01:00
|
|
|
|
|
|
|
g_clear_pointer (&attempt->timeout_source, g_source_unref);
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_connection_cancelled (GCancellable *cancellable,
|
|
|
|
gpointer data)
|
|
|
|
{
|
2020-01-24 04:58:41 +01:00
|
|
|
GCancellable *linked_cancellable = G_CANCELLABLE (data);
|
2018-10-29 14:53:07 +01:00
|
|
|
|
2020-01-24 04:58:41 +01:00
|
|
|
g_cancellable_cancel (linked_cancellable);
|
2018-10-29 14:53:07 +01:00
|
|
|
}
|
|
|
|
|
2009-05-15 21:26:24 +02:00
|
|
|
static void
|
|
|
|
g_socket_client_enumerator_callback (GObject *object,
|
|
|
|
GAsyncResult *result,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GSocketClientAsyncConnectData *data = user_data;
|
2010-08-10 22:48:45 +02:00
|
|
|
GSocketAddress *address = NULL;
|
2009-05-15 21:26:24 +02:00
|
|
|
GSocket *socket;
|
2018-10-29 14:53:07 +01:00
|
|
|
ConnectionAttempt *attempt;
|
2009-05-15 21:26:24 +02:00
|
|
|
|
2019-02-21 21:31:52 +01:00
|
|
|
if (task_completed_or_cancelled (data))
|
2014-03-18 04:50:30 +01:00
|
|
|
{
|
|
|
|
g_object_unref (data->task);
|
|
|
|
return;
|
|
|
|
}
|
2009-05-15 21:26:24 +02:00
|
|
|
|
|
|
|
address = g_socket_address_enumerator_next_finish (data->enumerator,
|
2020-10-09 01:02:56 +02:00
|
|
|
result, &data->error_info->tmp_error);
|
2009-05-15 21:26:24 +02:00
|
|
|
if (address == NULL)
|
|
|
|
{
|
2020-01-24 04:58:41 +01:00
|
|
|
if (G_UNLIKELY (data->enumeration_completed))
|
|
|
|
return;
|
|
|
|
|
|
|
|
data->enumeration_completed = TRUE;
|
|
|
|
g_debug ("GSocketClient: Address enumeration completed (out of addresses)");
|
|
|
|
|
2020-10-09 01:02:56 +02:00
|
|
|
/* As per API docs: We only care about error if it's the first call,
|
2020-01-24 04:58:41 +01:00
|
|
|
after that the enumerator is done.
|
|
|
|
|
|
|
|
Note that we don't care about cancellation errors because
|
|
|
|
task_completed_or_cancelled() above should handle that.
|
|
|
|
|
|
|
|
If this fails and nothing is in progress then we will complete task here.
|
|
|
|
*/
|
|
|
|
if ((data->enumerated_at_least_once && !data->connection_attempts && !data->connection_in_progress) ||
|
|
|
|
!data->enumerated_at_least_once)
|
2019-01-29 16:07:06 +01:00
|
|
|
{
|
2020-10-09 01:02:56 +02:00
|
|
|
g_debug ("GSocketClient: Address enumeration failed: %s",
|
|
|
|
data->error_info->tmp_error ? data->error_info->tmp_error->message : NULL);
|
|
|
|
consider_tmp_error (data->error_info, G_SOCKET_CLIENT_RESOLVING);
|
|
|
|
g_assert (data->error_info->best_error);
|
|
|
|
complete_connection_with_error (data, g_steal_pointer (&data->error_info->best_error));
|
2019-01-29 16:07:06 +01:00
|
|
|
}
|
2018-10-29 14:53:07 +01:00
|
|
|
|
2020-01-24 04:58:41 +01:00
|
|
|
/* Enumeration should never trigger again, drop our ref */
|
2012-08-02 21:48:22 +02:00
|
|
|
g_object_unref (data->task);
|
2009-05-15 21:26:24 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-24 04:58:41 +01:00
|
|
|
g_debug ("GSocketClient: Address enumeration succeeded");
|
gsocketclient: emit RESOLVING/RESOLVED events only once
GSocketAddressEnumerator encapsulates the details of how DNS happens, so
we don't have to think about it. But we may have taken encapsulation a
bit too far, here. Usually, we resolve a domain name to a list of IPv4
and IPv6 addresses. Then we go through each address in the list and try
to connect to it. Name resolution happens exactly once, at the start.
It doesn't happen each time we enumerate the enumerator. In theory, it
*could*, because we've designed these APIs to be agnostic of underlying
implementation details like DNS and network protocols. But in practice,
we know that's not really what's happening. It's weird to say that we
are RESOLVING what we know to be the same name multiple times. Behind
the scenes, we're not doing that.
This also fixes #1994, where enumeration can end with a RESOLVING event,
even though this is supposed to be the first event rather than the last.
I thought this would be hard to fix, even requiring new public API in
GSocketAddressEnumerator to peek ahead to see if the next enumeration is
going to return NULL. Then I decided we should just fake it: always emit
both RESOLVING and RESOLVED at the same time right after each
enumeration. Finally, I realized we can emit them at the correct time if
we simply assume resolving only happens the first time. This seems like
the most elegant of the possible solutions.
Now, this is a behavior change, and arguably an API break, but it should
align better with reasonable expectations of how GSocketClientEvent
ought to work. I don't expect it to break anything besides tests that
check which order GSocketClientEvent events are emitted in. (Currently,
libsoup has such tests, which will need to be updated.) Ideally we would
have GLib-level tests as well, but in a concession to pragmatism, it's a
lot easier to keep network tests in libsoup.
2020-10-06 22:39:45 +02:00
|
|
|
if (!data->enumerated_at_least_once)
|
|
|
|
{
|
|
|
|
g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_RESOLVED,
|
|
|
|
data->connectable, NULL);
|
|
|
|
data->enumerated_at_least_once = TRUE;
|
|
|
|
}
|
2010-08-20 19:04:19 +02:00
|
|
|
|
2020-10-09 01:02:56 +02:00
|
|
|
socket = create_socket (data->client, address, &data->error_info->tmp_error);
|
2011-06-12 22:29:04 +02:00
|
|
|
if (socket == NULL)
|
2009-05-15 21:26:24 +02:00
|
|
|
{
|
2011-06-12 22:29:04 +02:00
|
|
|
g_object_unref (address);
|
2020-10-09 01:02:56 +02:00
|
|
|
consider_tmp_error (data->error_info, G_SOCKET_CLIENT_CONNECTING);
|
2019-01-29 16:07:06 +01:00
|
|
|
enumerator_next_async (data, FALSE);
|
2011-06-12 22:29:04 +02:00
|
|
|
return;
|
2009-05-15 21:26:24 +02:00
|
|
|
}
|
|
|
|
|
2018-10-29 14:53:07 +01:00
|
|
|
attempt = connection_attempt_new ();
|
|
|
|
attempt->data = data;
|
|
|
|
attempt->socket = socket;
|
|
|
|
attempt->address = address;
|
|
|
|
attempt->cancellable = g_cancellable_new ();
|
|
|
|
attempt->connection = (GIOStream *)g_socket_connection_factory_create_connection (socket);
|
|
|
|
attempt->timeout_source = g_timeout_source_new (HAPPY_EYEBALLS_CONNECTION_ATTEMPT_TIMEOUT_MS);
|
2020-01-24 04:58:41 +01:00
|
|
|
|
|
|
|
if (G_IS_PROXY_ADDRESS (address) && data->client->priv->enable_proxy)
|
|
|
|
attempt->proxy_addr = g_object_ref (G_PROXY_ADDRESS (address));
|
|
|
|
|
2018-10-29 14:53:07 +01:00
|
|
|
g_source_set_callback (attempt->timeout_source, on_connection_attempt_timeout, attempt, NULL);
|
2020-01-05 03:46:25 +01:00
|
|
|
g_source_attach (attempt->timeout_source, g_task_get_context (data->task));
|
2023-02-22 17:34:05 +01:00
|
|
|
data->connection_attempts = g_slist_append (data->connection_attempts, connection_attempt_ref (attempt));
|
2018-10-29 14:53:07 +01:00
|
|
|
|
|
|
|
if (g_task_get_cancellable (data->task))
|
2022-07-12 17:07:23 +02:00
|
|
|
{
|
|
|
|
attempt->task_cancellable = g_object_ref (g_task_get_cancellable (data->task));
|
|
|
|
attempt->cancelled_id =
|
|
|
|
g_cancellable_connect (attempt->task_cancellable, G_CALLBACK (on_connection_cancelled),
|
|
|
|
g_object_ref (attempt->cancellable), g_object_unref);
|
|
|
|
}
|
2018-10-29 14:53:07 +01:00
|
|
|
|
|
|
|
g_socket_connection_set_cached_remote_address ((GSocketConnection *)attempt->connection, address);
|
2020-01-24 04:58:41 +01:00
|
|
|
g_debug ("GSocketClient: Starting TCP connection attempt");
|
2018-10-29 14:53:07 +01:00
|
|
|
g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_CONNECTING, data->connectable, attempt->connection);
|
|
|
|
g_socket_connection_connect_async (G_SOCKET_CONNECTION (attempt->connection),
|
2012-08-02 21:48:22 +02:00
|
|
|
address,
|
2018-10-29 14:53:07 +01:00
|
|
|
attempt->cancellable,
|
2023-02-22 17:34:05 +01:00
|
|
|
g_socket_client_connected_callback, attempt /* transfer full */);
|
2009-05-15 21:26:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-05-18 13:02:11 +02:00
|
|
|
* g_socket_client_connect_async:
|
2011-06-05 00:44:44 +02:00
|
|
|
* @client: a #GSocketClient
|
2009-05-15 21:26:24 +02:00
|
|
|
* @connectable: a #GSocketConnectable specifying the remote address.
|
2016-10-29 03:29:02 +02:00
|
|
|
* @cancellable: (nullable): a #GCancellable, or %NULL
|
2010-12-29 16:01:12 +01:00
|
|
|
* @callback: (scope async): a #GAsyncReadyCallback
|
2023-02-20 21:43:02 +01:00
|
|
|
* @user_data: user data for the callback
|
2009-05-15 21:26:24 +02:00
|
|
|
*
|
|
|
|
* This is the asynchronous version of g_socket_client_connect().
|
|
|
|
*
|
2020-10-09 02:08:34 +02:00
|
|
|
* You may wish to prefer the asynchronous version even in synchronous
|
|
|
|
* command line programs because, since 2.60, it implements
|
|
|
|
* [RFC 8305](https://tools.ietf.org/html/rfc8305) "Happy Eyeballs"
|
|
|
|
* recommendations to work around long connection timeouts in networks
|
|
|
|
* where IPv6 is broken by performing an IPv4 connection simultaneously
|
|
|
|
* without waiting for IPv6 to time out, which is not supported by the
|
|
|
|
* synchronous call. (This is not an API guarantee, and may change in
|
|
|
|
* the future.)
|
|
|
|
*
|
2009-05-15 21:26:24 +02:00
|
|
|
* When the operation is finished @callback will be
|
|
|
|
* called. You can then call g_socket_client_connect_finish() to get
|
|
|
|
* the result of the operation.
|
|
|
|
*
|
|
|
|
* Since: 2.22
|
2009-05-28 00:20:08 +02:00
|
|
|
*/
|
2009-05-15 21:26:24 +02:00
|
|
|
void
|
|
|
|
g_socket_client_connect_async (GSocketClient *client,
|
|
|
|
GSocketConnectable *connectable,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GSocketClientAsyncConnectData *data;
|
|
|
|
|
|
|
|
g_return_if_fail (G_IS_SOCKET_CLIENT (client));
|
|
|
|
|
2010-08-10 22:48:45 +02:00
|
|
|
data = g_slice_new0 (GSocketClientAsyncConnectData);
|
2009-05-15 21:26:24 +02:00
|
|
|
data->client = client;
|
2009-12-21 20:50:32 +01:00
|
|
|
data->connectable = g_object_ref (connectable);
|
2020-10-09 01:02:56 +02:00
|
|
|
data->error_info = socket_client_error_info_new ();
|
2010-08-10 22:48:45 +02:00
|
|
|
|
|
|
|
if (can_use_proxy (client))
|
2013-01-27 19:53:36 +01:00
|
|
|
{
|
2010-08-10 22:48:45 +02:00
|
|
|
data->enumerator = g_socket_connectable_proxy_enumerate (connectable);
|
2013-01-27 19:53:36 +01:00
|
|
|
if (client->priv->proxy_resolver &&
|
|
|
|
G_IS_PROXY_ADDRESS_ENUMERATOR (data->enumerator))
|
|
|
|
{
|
2013-02-15 16:39:19 +01:00
|
|
|
g_object_set (G_OBJECT (data->enumerator),
|
|
|
|
"proxy-resolver", client->priv->proxy_resolver,
|
|
|
|
NULL);
|
2013-01-27 19:53:36 +01:00
|
|
|
}
|
|
|
|
}
|
2009-05-15 21:26:24 +02:00
|
|
|
else
|
2013-01-27 19:53:36 +01:00
|
|
|
data->enumerator = g_socket_connectable_enumerate (connectable);
|
2009-05-15 21:26:24 +02:00
|
|
|
|
2020-01-24 04:58:41 +01:00
|
|
|
/* This function tries to match the behavior of g_socket_client_connect ()
|
|
|
|
which is simple enough but much of it is done in parallel to be as responsive
|
|
|
|
as possible as per Happy Eyeballs (RFC 8305). This complicates flow quite a
|
|
|
|
bit but we can describe it in 3 sections:
|
|
|
|
|
|
|
|
Firstly we have address enumeration (DNS):
|
|
|
|
- This may be triggered multiple times by enumerator_next_async().
|
|
|
|
- It also has its own cancellable (data->enumeration_cancellable).
|
|
|
|
- Enumeration is done lazily because GNetworkAddressAddressEnumerator
|
|
|
|
also does work in parallel and may lazily add new addresses.
|
|
|
|
- If the first enumeration errors then the task errors. Otherwise all enumerations
|
|
|
|
will potentially be used (until task or enumeration is cancelled).
|
|
|
|
|
|
|
|
Then we start attempting connections (TCP):
|
|
|
|
- Each connection is independent and kept in a ConnectionAttempt object.
|
|
|
|
- They each hold a ref on the main task and have their own cancellable.
|
|
|
|
- Multiple attempts may happen in parallel as per Happy Eyeballs.
|
|
|
|
- Upon failure or timeouts more connection attempts are made.
|
|
|
|
- If no connections succeed the task errors.
|
|
|
|
- Upon success they are kept in a list of successful connections.
|
|
|
|
|
|
|
|
Lastly we connect at the application layer (TLS, Proxies):
|
|
|
|
- These are done in serial.
|
|
|
|
- The reasoning here is that Happy Eyeballs is about making bad connections responsive
|
|
|
|
at the IP/TCP layers. Issues at the application layer are generally not due to
|
|
|
|
connectivity issues but rather misconfiguration.
|
|
|
|
- Upon failure it will try the next TCP connection until it runs out and
|
|
|
|
the task errors.
|
|
|
|
- Upon success it cancels everything remaining (enumeration and connections)
|
|
|
|
and returns the connection.
|
|
|
|
*/
|
2019-01-29 16:07:06 +01:00
|
|
|
|
2012-08-02 21:48:22 +02:00
|
|
|
data->task = g_task_new (client, cancellable, callback, user_data);
|
2019-02-21 21:31:52 +01:00
|
|
|
g_task_set_check_cancellable (data->task, FALSE); /* We handle this manually */
|
2016-06-17 01:39:38 +02:00
|
|
|
g_task_set_source_tag (data->task, g_socket_client_connect_async);
|
2012-08-02 21:48:22 +02:00
|
|
|
g_task_set_task_data (data->task, data, (GDestroyNotify)g_socket_client_async_connect_data_free);
|
|
|
|
|
2020-01-24 04:58:41 +01:00
|
|
|
data->enumeration_cancellable = g_cancellable_new ();
|
|
|
|
if (cancellable)
|
2022-06-13 14:06:06 +02:00
|
|
|
{
|
|
|
|
data->enumeration_parent_cancellable = g_object_ref (cancellable);
|
|
|
|
data->enumeration_cancelled_id =
|
|
|
|
g_cancellable_connect (cancellable, G_CALLBACK (on_connection_cancelled),
|
|
|
|
g_object_ref (data->enumeration_cancellable), g_object_unref);
|
|
|
|
}
|
2020-01-24 04:58:41 +01:00
|
|
|
|
2019-01-29 16:07:06 +01:00
|
|
|
enumerator_next_async (data, FALSE);
|
2009-05-15 21:26:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_socket_client_connect_to_host_async:
|
2011-06-05 00:44:44 +02:00
|
|
|
* @client: a #GSocketClient
|
2009-05-15 21:26:24 +02:00
|
|
|
* @host_and_port: the name and optionally the port of the host to connect to
|
|
|
|
* @default_port: the default port to connect to
|
2016-10-29 03:29:02 +02:00
|
|
|
* @cancellable: (nullable): a #GCancellable, or %NULL
|
2010-12-29 16:01:12 +01:00
|
|
|
* @callback: (scope async): a #GAsyncReadyCallback
|
2023-02-20 21:43:02 +01:00
|
|
|
* @user_data: user data for the callback
|
2009-05-15 21:26:24 +02:00
|
|
|
*
|
|
|
|
* This is the asynchronous version of g_socket_client_connect_to_host().
|
|
|
|
*
|
|
|
|
* When the operation is finished @callback will be
|
|
|
|
* called. You can then call g_socket_client_connect_to_host_finish() to get
|
|
|
|
* the result of the operation.
|
|
|
|
*
|
|
|
|
* Since: 2.22
|
2009-05-28 00:20:08 +02:00
|
|
|
*/
|
2009-05-15 21:26:24 +02:00
|
|
|
void
|
|
|
|
g_socket_client_connect_to_host_async (GSocketClient *client,
|
2009-05-28 00:20:08 +02:00
|
|
|
const gchar *host_and_port,
|
2009-06-12 15:57:51 +02:00
|
|
|
guint16 default_port,
|
2009-05-15 21:26:24 +02:00
|
|
|
GCancellable *cancellable,
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GSocketConnectable *connectable;
|
|
|
|
GError *error;
|
|
|
|
|
|
|
|
error = NULL;
|
|
|
|
connectable = g_network_address_parse (host_and_port, default_port,
|
|
|
|
&error);
|
|
|
|
if (connectable == NULL)
|
|
|
|
{
|
2012-08-02 21:48:22 +02:00
|
|
|
g_task_report_error (client, callback, user_data,
|
|
|
|
g_socket_client_connect_to_host_async,
|
|
|
|
error);
|
2009-05-15 21:26:24 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_socket_client_connect_async (client,
|
|
|
|
connectable, cancellable,
|
|
|
|
callback, user_data);
|
|
|
|
g_object_unref (connectable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-20 12:41:50 +02:00
|
|
|
/**
|
|
|
|
* g_socket_client_connect_to_service_async:
|
|
|
|
* @client: a #GSocketClient
|
|
|
|
* @domain: a domain name
|
|
|
|
* @service: the name of the service to connect to
|
2016-10-29 03:29:02 +02:00
|
|
|
* @cancellable: (nullable): a #GCancellable, or %NULL
|
2010-12-29 16:01:12 +01:00
|
|
|
* @callback: (scope async): a #GAsyncReadyCallback
|
2023-02-20 21:43:02 +01:00
|
|
|
* @user_data: user data for the callback
|
2009-05-20 12:41:50 +02:00
|
|
|
*
|
|
|
|
* This is the asynchronous version of
|
|
|
|
* g_socket_client_connect_to_service().
|
2009-05-28 00:20:08 +02:00
|
|
|
*
|
|
|
|
* Since: 2.22
|
|
|
|
*/
|
2009-05-20 12:41:50 +02:00
|
|
|
void
|
|
|
|
g_socket_client_connect_to_service_async (GSocketClient *client,
|
2009-05-28 00:20:08 +02:00
|
|
|
const gchar *domain,
|
|
|
|
const gchar *service,
|
2009-05-20 12:41:50 +02:00
|
|
|
GCancellable *cancellable,
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GSocketConnectable *connectable;
|
|
|
|
|
|
|
|
connectable = g_network_service_new (service, "tcp", domain);
|
|
|
|
g_socket_client_connect_async (client,
|
|
|
|
connectable, cancellable,
|
|
|
|
callback, user_data);
|
|
|
|
g_object_unref (connectable);
|
|
|
|
}
|
|
|
|
|
2010-04-30 00:51:42 +02:00
|
|
|
/**
|
|
|
|
* g_socket_client_connect_to_uri_async:
|
|
|
|
* @client: a #GSocketClient
|
|
|
|
* @uri: a network uri
|
|
|
|
* @default_port: the default port to connect to
|
2016-10-29 03:29:02 +02:00
|
|
|
* @cancellable: (nullable): a #GCancellable, or %NULL
|
2010-12-29 16:01:12 +01:00
|
|
|
* @callback: (scope async): a #GAsyncReadyCallback
|
2023-02-20 21:43:02 +01:00
|
|
|
* @user_data: user data for the callback
|
2010-04-30 00:51:42 +02:00
|
|
|
*
|
|
|
|
* This is the asynchronous version of g_socket_client_connect_to_uri().
|
|
|
|
*
|
|
|
|
* When the operation is finished @callback will be
|
|
|
|
* called. You can then call g_socket_client_connect_to_uri_finish() to get
|
|
|
|
* the result of the operation.
|
|
|
|
*
|
|
|
|
* Since: 2.26
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
g_socket_client_connect_to_uri_async (GSocketClient *client,
|
|
|
|
const gchar *uri,
|
|
|
|
guint16 default_port,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GSocketConnectable *connectable;
|
|
|
|
GError *error;
|
|
|
|
|
|
|
|
error = NULL;
|
|
|
|
connectable = g_network_address_parse_uri (uri, default_port, &error);
|
|
|
|
if (connectable == NULL)
|
|
|
|
{
|
2012-08-02 21:48:22 +02:00
|
|
|
g_task_report_error (client, callback, user_data,
|
|
|
|
g_socket_client_connect_to_uri_async,
|
|
|
|
error);
|
2010-04-30 00:51:42 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-01-24 04:58:41 +01:00
|
|
|
g_debug("g_socket_client_connect_to_uri_async");
|
2010-04-30 00:51:42 +02:00
|
|
|
g_socket_client_connect_async (client,
|
|
|
|
connectable, cancellable,
|
|
|
|
callback, user_data);
|
|
|
|
g_object_unref (connectable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-15 21:26:24 +02:00
|
|
|
/**
|
|
|
|
* g_socket_client_connect_finish:
|
|
|
|
* @client: a #GSocketClient.
|
|
|
|
* @result: a #GAsyncResult.
|
2011-04-20 19:08:06 +02:00
|
|
|
* @error: a #GError location to store the error occurring, or %NULL to
|
2009-05-15 21:26:24 +02:00
|
|
|
* ignore.
|
|
|
|
*
|
|
|
|
* Finishes an async connect operation. See g_socket_client_connect_async()
|
|
|
|
*
|
2010-09-24 23:24:41 +02:00
|
|
|
* Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
|
2009-05-15 21:26:24 +02:00
|
|
|
*
|
|
|
|
* Since: 2.22
|
2009-05-28 00:20:08 +02:00
|
|
|
*/
|
2009-05-15 21:26:24 +02:00
|
|
|
GSocketConnection *
|
|
|
|
g_socket_client_connect_finish (GSocketClient *client,
|
|
|
|
GAsyncResult *result,
|
|
|
|
GError **error)
|
|
|
|
{
|
2012-08-02 21:48:22 +02:00
|
|
|
g_return_val_if_fail (g_task_is_valid (result, client), NULL);
|
2009-05-15 21:26:24 +02:00
|
|
|
|
2012-08-02 21:48:22 +02:00
|
|
|
return g_task_propagate_pointer (G_TASK (result), error);
|
2009-05-15 21:26:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_socket_client_connect_to_host_finish:
|
|
|
|
* @client: a #GSocketClient.
|
|
|
|
* @result: a #GAsyncResult.
|
2011-04-20 19:08:06 +02:00
|
|
|
* @error: a #GError location to store the error occurring, or %NULL to
|
2009-05-15 21:26:24 +02:00
|
|
|
* ignore.
|
|
|
|
*
|
|
|
|
* Finishes an async connect operation. See g_socket_client_connect_to_host_async()
|
|
|
|
*
|
2010-09-24 23:24:41 +02:00
|
|
|
* Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
|
2009-05-15 21:26:24 +02:00
|
|
|
*
|
|
|
|
* Since: 2.22
|
2009-05-28 00:20:08 +02:00
|
|
|
*/
|
2009-05-15 21:26:24 +02:00
|
|
|
GSocketConnection *
|
2009-05-28 00:20:08 +02:00
|
|
|
g_socket_client_connect_to_host_finish (GSocketClient *client,
|
|
|
|
GAsyncResult *result,
|
|
|
|
GError **error)
|
2009-05-15 21:26:24 +02:00
|
|
|
{
|
|
|
|
return g_socket_client_connect_finish (client, result, error);
|
|
|
|
}
|
|
|
|
|
2009-05-20 12:41:50 +02:00
|
|
|
/**
|
|
|
|
* g_socket_client_connect_to_service_finish:
|
|
|
|
* @client: a #GSocketClient.
|
|
|
|
* @result: a #GAsyncResult.
|
2011-04-20 19:08:06 +02:00
|
|
|
* @error: a #GError location to store the error occurring, or %NULL to
|
2009-05-20 12:41:50 +02:00
|
|
|
* ignore.
|
|
|
|
*
|
|
|
|
* Finishes an async connect operation. See g_socket_client_connect_to_service_async()
|
|
|
|
*
|
2010-09-24 23:24:41 +02:00
|
|
|
* Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
|
2009-05-20 12:41:50 +02:00
|
|
|
*
|
|
|
|
* Since: 2.22
|
2009-05-28 00:20:08 +02:00
|
|
|
*/
|
2009-05-20 12:41:50 +02:00
|
|
|
GSocketConnection *
|
2009-05-28 00:20:08 +02:00
|
|
|
g_socket_client_connect_to_service_finish (GSocketClient *client,
|
|
|
|
GAsyncResult *result,
|
|
|
|
GError **error)
|
2009-05-20 12:41:50 +02:00
|
|
|
{
|
|
|
|
return g_socket_client_connect_finish (client, result, error);
|
|
|
|
}
|
2010-04-30 00:51:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* g_socket_client_connect_to_uri_finish:
|
|
|
|
* @client: a #GSocketClient.
|
|
|
|
* @result: a #GAsyncResult.
|
2011-04-20 19:08:06 +02:00
|
|
|
* @error: a #GError location to store the error occurring, or %NULL to
|
2010-04-30 00:51:42 +02:00
|
|
|
* ignore.
|
|
|
|
*
|
|
|
|
* Finishes an async connect operation. See g_socket_client_connect_to_uri_async()
|
|
|
|
*
|
2010-09-24 23:24:41 +02:00
|
|
|
* Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
|
2010-04-30 00:51:42 +02:00
|
|
|
*
|
|
|
|
* Since: 2.26
|
|
|
|
*/
|
|
|
|
GSocketConnection *
|
|
|
|
g_socket_client_connect_to_uri_finish (GSocketClient *client,
|
|
|
|
GAsyncResult *result,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
return g_socket_client_connect_finish (client, result, error);
|
|
|
|
}
|
2010-08-19 22:23:50 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* g_socket_client_add_application_proxy:
|
|
|
|
* @client: a #GSocketClient
|
|
|
|
* @protocol: The proxy protocol
|
|
|
|
*
|
|
|
|
* Enable proxy protocols to be handled by the application. When the
|
|
|
|
* indicated proxy protocol is returned by the #GProxyResolver,
|
|
|
|
* #GSocketClient will consider this protocol as supported but will
|
2011-04-20 19:08:06 +02:00
|
|
|
* not try to find a #GProxy instance to handle handshaking. The
|
2010-08-19 22:23:50 +02:00
|
|
|
* application must check for this case by calling
|
|
|
|
* g_socket_connection_get_remote_address() on the returned
|
|
|
|
* #GSocketConnection, and seeing if it's a #GProxyAddress of the
|
|
|
|
* appropriate type, to determine whether or not it needs to handle
|
|
|
|
* the proxy handshaking itself.
|
|
|
|
*
|
|
|
|
* This should be used for proxy protocols that are dialects of
|
|
|
|
* another protocol such as HTTP proxy. It also allows cohabitation of
|
|
|
|
* proxy protocols that are reused between protocols. A good example
|
|
|
|
* is HTTP. It can be used to proxy HTTP, FTP and Gopher and can also
|
|
|
|
* be use as generic socket proxy through the HTTP CONNECT method.
|
2011-11-17 19:04:04 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2010-08-19 22:23:50 +02:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
g_socket_client_add_application_proxy (GSocketClient *client,
|
|
|
|
const gchar *protocol)
|
|
|
|
{
|
2014-12-09 13:35:06 +01:00
|
|
|
g_hash_table_add (client->priv->app_proxies, g_strdup (protocol));
|
2010-08-19 22:23:50 +02:00
|
|
|
}
|