2010-08-10 21:25:11 +02:00
|
|
|
/* GIO - GLib Input, Output and Streaming Library
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Collabora, Ltd.
|
|
|
|
*
|
2022-05-18 10:12:45 +02:00
|
|
|
* SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
*
|
2010-08-10 21:25:11 +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.
|
2010-08-10 21:25:11 +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/>.
|
2010-08-10 21:25:11 +02:00
|
|
|
*
|
|
|
|
* Author: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "gproxyaddressenumerator.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "gasyncresult.h"
|
|
|
|
#include "ginetaddress.h"
|
2022-06-09 20:30:02 +02:00
|
|
|
#include "gioerror.h"
|
2010-08-10 21:25:11 +02:00
|
|
|
#include "glibintl.h"
|
2023-05-25 00:37:57 +02:00
|
|
|
#include "glib-private.h"
|
2010-08-10 21:25:11 +02:00
|
|
|
#include "gnetworkaddress.h"
|
|
|
|
#include "gnetworkingprivate.h"
|
|
|
|
#include "gproxy.h"
|
|
|
|
#include "gproxyaddress.h"
|
|
|
|
#include "gproxyresolver.h"
|
2012-08-02 21:48:22 +02:00
|
|
|
#include "gtask.h"
|
2010-08-10 21:25:11 +02:00
|
|
|
#include "gresolver.h"
|
|
|
|
#include "gsocketaddress.h"
|
|
|
|
#include "gsocketaddressenumerator.h"
|
|
|
|
#include "gsocketconnectable.h"
|
|
|
|
|
2018-11-28 14:10:55 +01:00
|
|
|
/**
|
2023-10-25 16:04:40 +02:00
|
|
|
* GProxyAddressEnumerator:
|
2018-11-28 14:10:55 +01:00
|
|
|
*
|
2023-10-25 16:04:40 +02:00
|
|
|
* `GProxyAddressEnumerator` is a wrapper around
|
|
|
|
* [class@Gio.SocketAddressEnumerator] which takes the [class@Gio.SocketAddress]
|
|
|
|
* instances returned by the [class@Gio.SocketAddressEnumerator]
|
|
|
|
* and wraps them in [class@Gio.ProxyAddress] instances, using the given
|
|
|
|
* [property@Gio.ProxyAddressEnumerator:proxy-resolver].
|
2018-11-28 14:10:55 +01:00
|
|
|
*
|
|
|
|
* This enumerator will be returned (for example, by
|
2023-10-25 16:04:40 +02:00
|
|
|
* [method@Gio.SocketConnectable.enumerate]) as appropriate when a proxy is
|
|
|
|
* configured; there should be no need to manually wrap a
|
|
|
|
* [class@Gio.SocketAddressEnumerator] instance with one.
|
2018-11-28 14:10:55 +01:00
|
|
|
*/
|
|
|
|
|
2010-08-10 21:25:11 +02:00
|
|
|
#define GET_PRIVATE(o) (G_PROXY_ADDRESS_ENUMERATOR (o)->priv)
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_URI,
|
2013-05-05 22:50:43 +02:00
|
|
|
PROP_DEFAULT_PORT,
|
2013-01-27 19:53:36 +01:00
|
|
|
PROP_CONNECTABLE,
|
|
|
|
PROP_PROXY_RESOLVER
|
2010-08-10 21:25:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _GProxyAddressEnumeratorPrivate
|
|
|
|
{
|
|
|
|
/* Destination address */
|
|
|
|
GSocketConnectable *connectable;
|
2013-05-05 22:50:43 +02:00
|
|
|
gchar *dest_uri;
|
|
|
|
guint16 default_port;
|
|
|
|
gchar *dest_hostname;
|
|
|
|
guint16 dest_port;
|
2010-08-10 21:25:11 +02:00
|
|
|
GList *dest_ips;
|
|
|
|
|
|
|
|
/* Proxy enumeration */
|
2013-01-27 19:53:36 +01:00
|
|
|
GProxyResolver *proxy_resolver;
|
2013-05-05 22:50:43 +02:00
|
|
|
gchar **proxies;
|
|
|
|
gchar **next_proxy;
|
2010-08-10 21:25:11 +02:00
|
|
|
GSocketAddressEnumerator *addr_enum;
|
|
|
|
GSocketAddress *proxy_address;
|
2012-04-21 06:25:53 +02:00
|
|
|
const gchar *proxy_uri;
|
2013-05-05 22:50:43 +02:00
|
|
|
gchar *proxy_type;
|
|
|
|
gchar *proxy_username;
|
|
|
|
gchar *proxy_password;
|
2010-08-10 21:25:11 +02:00
|
|
|
gboolean supports_hostname;
|
2013-05-05 22:50:43 +02:00
|
|
|
GList *next_dest_ip;
|
2012-04-22 21:20:14 +02:00
|
|
|
GError *last_error;
|
2022-06-09 20:30:02 +02:00
|
|
|
|
|
|
|
/* ever_enumerated is TRUE after we've returned a result for the first time
|
|
|
|
* via g_proxy_address_enumerator_next() or _next_async(). If FALSE, we have
|
|
|
|
* never returned yet, and should return an error if returning NULL because
|
|
|
|
* it does not make sense for a proxy resolver to return NULL except on error.
|
|
|
|
* (Whereas a DNS resolver would return NULL with no error to indicate "no
|
|
|
|
* results", a proxy resolver would want to return "direct://" instead, so
|
|
|
|
* NULL without error does not make sense for us.)
|
|
|
|
*
|
|
|
|
* But if ever_enumerated is TRUE, then we must not report any further errors
|
|
|
|
* (except for G_IO_ERROR_CANCELLED), because this is an API contract of
|
|
|
|
* GSocketAddressEnumerator.
|
|
|
|
*/
|
|
|
|
gboolean ever_enumerated;
|
2010-08-10 21:25:11 +02:00
|
|
|
};
|
|
|
|
|
2013-06-11 01:29:58 +02:00
|
|
|
G_DEFINE_TYPE_WITH_PRIVATE (GProxyAddressEnumerator, g_proxy_address_enumerator, G_TYPE_SOCKET_ADDRESS_ENUMERATOR)
|
|
|
|
|
2010-08-10 21:25:11 +02:00
|
|
|
static void
|
|
|
|
save_userinfo (GProxyAddressEnumeratorPrivate *priv,
|
2020-07-07 11:02:50 +02:00
|
|
|
const gchar *proxy)
|
2010-08-10 21:25:11 +02:00
|
|
|
{
|
2020-07-07 11:02:50 +02:00
|
|
|
g_clear_pointer (&priv->proxy_username, g_free);
|
|
|
|
g_clear_pointer (&priv->proxy_password, g_free);
|
2010-08-10 21:25:11 +02:00
|
|
|
|
2020-07-07 11:02:50 +02:00
|
|
|
g_uri_split_with_user (proxy, G_URI_FLAGS_HAS_PASSWORD, NULL,
|
|
|
|
&priv->proxy_username, &priv->proxy_password,
|
|
|
|
NULL, NULL, NULL, NULL, NULL, NULL, NULL);
|
2010-08-10 21:25:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
next_enumerator (GProxyAddressEnumeratorPrivate *priv)
|
|
|
|
{
|
|
|
|
if (priv->proxy_address)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (priv->addr_enum == NULL && *priv->next_proxy)
|
|
|
|
{
|
|
|
|
GSocketConnectable *connectable = NULL;
|
|
|
|
GProxy *proxy;
|
|
|
|
|
2012-04-21 06:25:53 +02:00
|
|
|
priv->proxy_uri = *priv->next_proxy++;
|
2010-08-10 21:25:11 +02:00
|
|
|
g_free (priv->proxy_type);
|
2012-04-21 06:25:53 +02:00
|
|
|
priv->proxy_type = g_uri_parse_scheme (priv->proxy_uri);
|
2010-08-10 21:25:11 +02:00
|
|
|
|
|
|
|
if (priv->proxy_type == NULL)
|
|
|
|
continue;
|
|
|
|
|
2011-08-29 20:49:32 +02:00
|
|
|
/* Assumes hostnames are supported for unknown protocols */
|
2010-08-10 21:25:11 +02:00
|
|
|
priv->supports_hostname = TRUE;
|
|
|
|
proxy = g_proxy_get_default_for_protocol (priv->proxy_type);
|
|
|
|
if (proxy)
|
|
|
|
{
|
|
|
|
priv->supports_hostname = g_proxy_supports_hostname (proxy);
|
|
|
|
g_object_unref (proxy);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp ("direct", priv->proxy_type) == 0)
|
|
|
|
{
|
|
|
|
if (priv->connectable)
|
|
|
|
connectable = g_object_ref (priv->connectable);
|
|
|
|
else
|
|
|
|
connectable = g_network_address_new (priv->dest_hostname,
|
|
|
|
priv->dest_port);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GError *error = NULL;
|
2023-05-25 00:37:57 +02:00
|
|
|
int default_port;
|
2010-08-10 21:25:11 +02:00
|
|
|
|
2023-05-25 00:37:57 +02:00
|
|
|
default_port = GLIB_PRIVATE_CALL (g_uri_get_default_scheme_port) (priv->proxy_type);
|
|
|
|
if (default_port == -1)
|
|
|
|
default_port = 0;
|
2010-08-10 21:25:11 +02:00
|
|
|
|
2023-05-25 00:37:57 +02:00
|
|
|
connectable = g_network_address_parse_uri (priv->proxy_uri, default_port, &error);
|
2010-08-10 21:25:11 +02:00
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
g_warning ("Invalid proxy URI '%s': %s",
|
2012-04-21 06:25:53 +02:00
|
|
|
priv->proxy_uri, error->message);
|
2010-08-10 21:25:11 +02:00
|
|
|
g_error_free (error);
|
|
|
|
}
|
|
|
|
|
2012-04-21 06:25:53 +02:00
|
|
|
save_userinfo (priv, priv->proxy_uri);
|
2010-08-10 21:25:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (connectable)
|
|
|
|
{
|
|
|
|
priv->addr_enum = g_socket_connectable_enumerate (connectable);
|
|
|
|
g_object_unref (connectable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static GSocketAddress *
|
|
|
|
g_proxy_address_enumerator_next (GSocketAddressEnumerator *enumerator,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
GProxyAddressEnumeratorPrivate *priv = GET_PRIVATE (enumerator);
|
|
|
|
GSocketAddress *result = NULL;
|
|
|
|
GError *first_error = NULL;
|
|
|
|
|
2022-06-09 20:30:02 +02:00
|
|
|
if (!priv->ever_enumerated)
|
2010-08-10 21:25:11 +02:00
|
|
|
{
|
2022-06-09 20:30:02 +02:00
|
|
|
g_assert (priv->proxies == NULL);
|
2013-01-27 19:53:36 +01:00
|
|
|
priv->proxies = g_proxy_resolver_lookup (priv->proxy_resolver,
|
2010-08-10 21:25:11 +02:00
|
|
|
priv->dest_uri,
|
|
|
|
cancellable,
|
|
|
|
error);
|
|
|
|
priv->next_proxy = priv->proxies;
|
|
|
|
|
|
|
|
if (priv->proxies == NULL)
|
2022-06-09 20:30:02 +02:00
|
|
|
{
|
|
|
|
priv->ever_enumerated = TRUE;
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-08-10 21:25:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while (result == NULL && (*priv->next_proxy || priv->addr_enum))
|
|
|
|
{
|
|
|
|
gchar *dest_hostname;
|
2012-04-21 06:25:53 +02:00
|
|
|
gchar *dest_protocol;
|
2010-08-10 21:25:11 +02:00
|
|
|
GInetSocketAddress *inetsaddr;
|
|
|
|
GInetAddress *inetaddr;
|
|
|
|
guint16 port;
|
|
|
|
|
|
|
|
next_enumerator (priv);
|
|
|
|
|
|
|
|
if (!priv->addr_enum)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (priv->proxy_address == NULL)
|
|
|
|
{
|
|
|
|
priv->proxy_address = g_socket_address_enumerator_next (
|
|
|
|
priv->addr_enum,
|
|
|
|
cancellable,
|
|
|
|
first_error ? NULL : &first_error);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->proxy_address == NULL)
|
|
|
|
{
|
|
|
|
g_object_unref (priv->addr_enum);
|
|
|
|
priv->addr_enum = NULL;
|
|
|
|
|
|
|
|
if (priv->dest_ips)
|
|
|
|
{
|
|
|
|
g_resolver_free_addresses (priv->dest_ips);
|
|
|
|
priv->dest_ips = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp ("direct", priv->proxy_type) == 0)
|
|
|
|
{
|
|
|
|
result = priv->proxy_address;
|
|
|
|
priv->proxy_address = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!priv->supports_hostname)
|
|
|
|
{
|
|
|
|
GInetAddress *dest_ip;
|
|
|
|
|
|
|
|
if (!priv->dest_ips)
|
|
|
|
{
|
|
|
|
GResolver *resolver;
|
|
|
|
|
|
|
|
resolver = g_resolver_get_default();
|
|
|
|
priv->dest_ips = g_resolver_lookup_by_name (resolver,
|
|
|
|
priv->dest_hostname,
|
|
|
|
cancellable,
|
|
|
|
first_error ? NULL : &first_error);
|
|
|
|
g_object_unref (resolver);
|
|
|
|
|
|
|
|
if (!priv->dest_ips)
|
|
|
|
{
|
|
|
|
g_object_unref (priv->proxy_address);
|
|
|
|
priv->proxy_address = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!priv->next_dest_ip)
|
|
|
|
priv->next_dest_ip = priv->dest_ips;
|
|
|
|
|
|
|
|
dest_ip = G_INET_ADDRESS (priv->next_dest_ip->data);
|
|
|
|
dest_hostname = g_inet_address_to_string (dest_ip);
|
|
|
|
|
|
|
|
priv->next_dest_ip = g_list_next (priv->next_dest_ip);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dest_hostname = g_strdup (priv->dest_hostname);
|
|
|
|
}
|
2012-04-21 06:25:53 +02:00
|
|
|
dest_protocol = g_uri_parse_scheme (priv->dest_uri);
|
2010-08-10 21:25:11 +02:00
|
|
|
|
2021-10-11 13:02:33 +02:00
|
|
|
if (!G_IS_INET_SOCKET_ADDRESS (priv->proxy_address))
|
|
|
|
{
|
|
|
|
g_free (dest_hostname);
|
|
|
|
g_free (dest_protocol);
|
|
|
|
}
|
|
|
|
g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (priv->proxy_address), NULL);
|
2010-08-10 21:25:11 +02:00
|
|
|
|
|
|
|
inetsaddr = G_INET_SOCKET_ADDRESS (priv->proxy_address);
|
|
|
|
inetaddr = g_inet_socket_address_get_address (inetsaddr);
|
|
|
|
port = g_inet_socket_address_get_port (inetsaddr);
|
|
|
|
|
2012-04-21 06:25:53 +02:00
|
|
|
result = g_object_new (G_TYPE_PROXY_ADDRESS,
|
|
|
|
"address", inetaddr,
|
|
|
|
"port", port,
|
|
|
|
"protocol", priv->proxy_type,
|
|
|
|
"destination-protocol", dest_protocol,
|
|
|
|
"destination-hostname", dest_hostname,
|
|
|
|
"destination-port", priv->dest_port,
|
|
|
|
"username", priv->proxy_username,
|
|
|
|
"password", priv->proxy_password,
|
|
|
|
"uri", priv->proxy_uri,
|
|
|
|
NULL);
|
2010-08-10 21:25:11 +02:00
|
|
|
g_free (dest_hostname);
|
2012-04-21 06:25:53 +02:00
|
|
|
g_free (dest_protocol);
|
2010-08-10 21:25:11 +02:00
|
|
|
|
|
|
|
if (priv->supports_hostname || priv->next_dest_ip == NULL)
|
|
|
|
{
|
|
|
|
g_object_unref (priv->proxy_address);
|
|
|
|
priv->proxy_address = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-09 20:30:02 +02:00
|
|
|
if (result == NULL && first_error && (!priv->ever_enumerated || g_error_matches (first_error, G_IO_ERROR, G_IO_ERROR_CANCELLED)))
|
2010-08-10 21:25:11 +02:00
|
|
|
g_propagate_error (error, first_error);
|
|
|
|
else if (first_error)
|
|
|
|
g_error_free (first_error);
|
|
|
|
|
2022-06-09 20:30:02 +02:00
|
|
|
if (result == NULL && error != NULL && *error == NULL && !priv->ever_enumerated)
|
|
|
|
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, _("Unspecified proxy lookup failure"));
|
2010-08-10 21:25:11 +02:00
|
|
|
|
2022-06-09 20:30:02 +02:00
|
|
|
priv->ever_enumerated = TRUE;
|
2010-08-10 21:25:11 +02:00
|
|
|
|
2022-06-09 20:30:02 +02:00
|
|
|
return result;
|
|
|
|
}
|
2010-08-10 21:25:11 +02:00
|
|
|
|
|
|
|
static void
|
2012-08-02 21:48:22 +02:00
|
|
|
complete_async (GTask *task)
|
2010-08-10 21:25:11 +02:00
|
|
|
{
|
2012-08-02 21:48:22 +02:00
|
|
|
GProxyAddressEnumeratorPrivate *priv = g_task_get_task_data (task);
|
2012-04-22 21:20:14 +02:00
|
|
|
|
2022-06-09 20:30:02 +02:00
|
|
|
if (priv->last_error && (!priv->ever_enumerated || g_error_matches (priv->last_error, G_IO_ERROR, G_IO_ERROR_CANCELLED)))
|
2012-04-22 21:20:14 +02:00
|
|
|
{
|
2012-08-02 21:48:22 +02:00
|
|
|
g_task_return_error (task, priv->last_error);
|
2012-04-22 21:20:14 +02:00
|
|
|
priv->last_error = NULL;
|
|
|
|
}
|
2022-06-09 20:30:02 +02:00
|
|
|
else if (!priv->ever_enumerated)
|
2023-12-19 21:03:28 +01:00
|
|
|
{
|
|
|
|
g_task_return_new_error_literal (task, G_IO_ERROR, G_IO_ERROR_FAILED,
|
|
|
|
_("Unspecified proxy lookup failure"));
|
|
|
|
}
|
2012-08-02 21:48:22 +02:00
|
|
|
else
|
2023-12-19 21:03:28 +01:00
|
|
|
{
|
|
|
|
g_task_return_pointer (task, NULL, NULL);
|
|
|
|
}
|
2012-04-22 21:20:14 +02:00
|
|
|
|
2022-06-09 20:30:02 +02:00
|
|
|
priv->ever_enumerated = TRUE;
|
|
|
|
|
|
|
|
g_clear_error (&priv->last_error);
|
2012-08-02 21:48:22 +02:00
|
|
|
g_object_unref (task);
|
2010-08-10 21:25:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-08-02 21:48:22 +02:00
|
|
|
return_result (GTask *task)
|
2010-08-10 21:25:11 +02:00
|
|
|
{
|
2012-08-02 21:48:22 +02:00
|
|
|
GProxyAddressEnumeratorPrivate *priv = g_task_get_task_data (task);
|
2010-08-10 21:25:11 +02:00
|
|
|
GSocketAddress *result;
|
2022-05-10 17:23:46 +02:00
|
|
|
gboolean is_inet_socket_address;
|
2010-08-10 21:25:11 +02:00
|
|
|
|
|
|
|
if (strcmp ("direct", priv->proxy_type) == 0)
|
|
|
|
{
|
|
|
|
result = priv->proxy_address;
|
|
|
|
priv->proxy_address = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-21 06:25:53 +02:00
|
|
|
gchar *dest_hostname, *dest_protocol;
|
2010-08-10 21:25:11 +02:00
|
|
|
GInetSocketAddress *inetsaddr;
|
|
|
|
GInetAddress *inetaddr;
|
|
|
|
guint16 port;
|
|
|
|
|
|
|
|
if (!priv->supports_hostname)
|
|
|
|
{
|
|
|
|
GInetAddress *dest_ip;
|
|
|
|
|
|
|
|
if (!priv->next_dest_ip)
|
|
|
|
priv->next_dest_ip = priv->dest_ips;
|
|
|
|
|
|
|
|
dest_ip = G_INET_ADDRESS (priv->next_dest_ip->data);
|
|
|
|
dest_hostname = g_inet_address_to_string (dest_ip);
|
|
|
|
|
|
|
|
priv->next_dest_ip = g_list_next (priv->next_dest_ip);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dest_hostname = g_strdup (priv->dest_hostname);
|
|
|
|
}
|
2012-04-21 06:25:53 +02:00
|
|
|
dest_protocol = g_uri_parse_scheme (priv->dest_uri);
|
2010-08-10 21:25:11 +02:00
|
|
|
|
2022-05-10 17:23:46 +02:00
|
|
|
is_inet_socket_address = G_IS_INET_SOCKET_ADDRESS (priv->proxy_address);
|
|
|
|
if (!is_inet_socket_address)
|
2021-10-11 13:02:33 +02:00
|
|
|
{
|
|
|
|
g_free (dest_hostname);
|
|
|
|
g_free (dest_protocol);
|
|
|
|
}
|
2022-05-10 17:23:46 +02:00
|
|
|
g_return_if_fail (is_inet_socket_address);
|
2010-08-10 21:25:11 +02:00
|
|
|
|
|
|
|
inetsaddr = G_INET_SOCKET_ADDRESS (priv->proxy_address);
|
|
|
|
inetaddr = g_inet_socket_address_get_address (inetsaddr);
|
|
|
|
port = g_inet_socket_address_get_port (inetsaddr);
|
|
|
|
|
2012-04-21 06:25:53 +02:00
|
|
|
result = g_object_new (G_TYPE_PROXY_ADDRESS,
|
|
|
|
"address", inetaddr,
|
|
|
|
"port", port,
|
|
|
|
"protocol", priv->proxy_type,
|
|
|
|
"destination-protocol", dest_protocol,
|
|
|
|
"destination-hostname", dest_hostname,
|
|
|
|
"destination-port", priv->dest_port,
|
|
|
|
"username", priv->proxy_username,
|
|
|
|
"password", priv->proxy_password,
|
|
|
|
"uri", priv->proxy_uri,
|
|
|
|
NULL);
|
2010-08-10 21:25:11 +02:00
|
|
|
g_free (dest_hostname);
|
2012-04-21 06:25:53 +02:00
|
|
|
g_free (dest_protocol);
|
2010-08-10 21:25:11 +02:00
|
|
|
|
|
|
|
if (priv->supports_hostname || priv->next_dest_ip == NULL)
|
|
|
|
{
|
|
|
|
g_object_unref (priv->proxy_address);
|
|
|
|
priv->proxy_address = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-09 20:30:02 +02:00
|
|
|
priv->ever_enumerated = TRUE;
|
2012-08-02 21:48:22 +02:00
|
|
|
g_task_return_pointer (task, result, g_object_unref);
|
|
|
|
g_object_unref (task);
|
2010-08-10 21:25:11 +02:00
|
|
|
}
|
|
|
|
|
2012-04-22 21:20:14 +02:00
|
|
|
static void address_enumerate_cb (GObject *object,
|
|
|
|
GAsyncResult *result,
|
|
|
|
gpointer user_data);
|
|
|
|
|
|
|
|
static void
|
2012-08-02 21:48:22 +02:00
|
|
|
next_proxy (GTask *task)
|
2012-04-22 21:20:14 +02:00
|
|
|
{
|
2012-08-02 21:48:22 +02:00
|
|
|
GProxyAddressEnumeratorPrivate *priv = g_task_get_task_data (task);
|
|
|
|
|
2012-04-22 21:20:14 +02:00
|
|
|
if (*priv->next_proxy)
|
|
|
|
{
|
|
|
|
g_object_unref (priv->addr_enum);
|
|
|
|
priv->addr_enum = NULL;
|
|
|
|
|
|
|
|
if (priv->dest_ips)
|
|
|
|
{
|
|
|
|
g_resolver_free_addresses (priv->dest_ips);
|
|
|
|
priv->dest_ips = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
next_enumerator (priv);
|
|
|
|
|
|
|
|
if (priv->addr_enum)
|
|
|
|
{
|
|
|
|
g_socket_address_enumerator_next_async (priv->addr_enum,
|
2012-08-02 21:48:22 +02:00
|
|
|
g_task_get_cancellable (task),
|
2012-04-22 21:20:14 +02:00
|
|
|
address_enumerate_cb,
|
2012-08-02 21:48:22 +02:00
|
|
|
task);
|
2012-04-22 21:20:14 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-02 21:48:22 +02:00
|
|
|
complete_async (task);
|
2012-04-22 21:20:14 +02:00
|
|
|
}
|
|
|
|
|
2010-08-10 21:25:11 +02:00
|
|
|
static void
|
|
|
|
dest_hostname_lookup_cb (GObject *object,
|
|
|
|
GAsyncResult *result,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
2012-08-02 21:48:22 +02:00
|
|
|
GTask *task = user_data;
|
|
|
|
GProxyAddressEnumeratorPrivate *priv = g_task_get_task_data (task);
|
2010-08-10 21:25:11 +02:00
|
|
|
|
2012-04-22 21:20:14 +02:00
|
|
|
g_clear_error (&priv->last_error);
|
2010-08-10 21:25:11 +02:00
|
|
|
priv->dest_ips = g_resolver_lookup_by_name_finish (G_RESOLVER (object),
|
|
|
|
result,
|
2012-04-22 21:20:14 +02:00
|
|
|
&priv->last_error);
|
2010-08-10 21:25:11 +02:00
|
|
|
if (priv->dest_ips)
|
2012-08-02 21:48:22 +02:00
|
|
|
return_result (task);
|
2010-08-10 21:25:11 +02:00
|
|
|
else
|
2012-04-22 21:20:14 +02:00
|
|
|
{
|
|
|
|
g_clear_object (&priv->proxy_address);
|
2012-08-02 21:48:22 +02:00
|
|
|
next_proxy (task);
|
2012-04-22 21:20:14 +02:00
|
|
|
}
|
2010-08-10 21:25:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
address_enumerate_cb (GObject *object,
|
|
|
|
GAsyncResult *result,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
2012-08-02 21:48:22 +02:00
|
|
|
GTask *task = user_data;
|
|
|
|
GProxyAddressEnumeratorPrivate *priv = g_task_get_task_data (task);
|
2010-08-10 21:25:11 +02:00
|
|
|
|
2012-04-22 21:20:14 +02:00
|
|
|
g_clear_error (&priv->last_error);
|
2010-08-10 21:25:11 +02:00
|
|
|
priv->proxy_address =
|
|
|
|
g_socket_address_enumerator_next_finish (priv->addr_enum,
|
|
|
|
result,
|
2012-04-22 21:20:14 +02:00
|
|
|
&priv->last_error);
|
2010-08-10 21:25:11 +02:00
|
|
|
if (priv->proxy_address)
|
|
|
|
{
|
|
|
|
if (!priv->supports_hostname && !priv->dest_ips)
|
|
|
|
{
|
|
|
|
GResolver *resolver;
|
|
|
|
resolver = g_resolver_get_default();
|
|
|
|
g_resolver_lookup_by_name_async (resolver,
|
|
|
|
priv->dest_hostname,
|
2012-08-02 21:48:22 +02:00
|
|
|
g_task_get_cancellable (task),
|
2010-08-10 21:25:11 +02:00
|
|
|
dest_hostname_lookup_cb,
|
2012-08-02 21:48:22 +02:00
|
|
|
task);
|
2010-08-10 21:25:11 +02:00
|
|
|
g_object_unref (resolver);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-02 21:48:22 +02:00
|
|
|
return_result (task);
|
2010-08-10 21:25:11 +02:00
|
|
|
}
|
2012-04-22 21:20:14 +02:00
|
|
|
else
|
2012-08-02 21:48:22 +02:00
|
|
|
next_proxy (task);
|
2010-08-10 21:25:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
proxy_lookup_cb (GObject *object,
|
|
|
|
GAsyncResult *result,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
2012-08-02 21:48:22 +02:00
|
|
|
GTask *task = user_data;
|
|
|
|
GProxyAddressEnumeratorPrivate *priv = g_task_get_task_data (task);
|
2010-08-10 21:25:11 +02:00
|
|
|
|
2012-08-02 21:48:22 +02:00
|
|
|
g_clear_error (&priv->last_error);
|
2010-08-10 21:25:11 +02:00
|
|
|
priv->proxies = g_proxy_resolver_lookup_finish (G_PROXY_RESOLVER (object),
|
|
|
|
result,
|
2012-08-02 21:48:22 +02:00
|
|
|
&priv->last_error);
|
2010-08-10 21:25:11 +02:00
|
|
|
priv->next_proxy = priv->proxies;
|
|
|
|
|
2012-08-02 21:48:22 +02:00
|
|
|
if (priv->last_error)
|
2010-08-10 21:25:11 +02:00
|
|
|
{
|
2012-08-02 21:48:22 +02:00
|
|
|
complete_async (task);
|
|
|
|
return;
|
2010-08-10 21:25:11 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
next_enumerator (priv);
|
|
|
|
if (priv->addr_enum)
|
|
|
|
{
|
|
|
|
g_socket_address_enumerator_next_async (priv->addr_enum,
|
2012-08-02 21:48:22 +02:00
|
|
|
g_task_get_cancellable (task),
|
2010-08-10 21:25:11 +02:00
|
|
|
address_enumerate_cb,
|
2012-08-02 21:48:22 +02:00
|
|
|
task);
|
2010-08-10 21:25:11 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-02 21:48:22 +02:00
|
|
|
complete_async (task);
|
2010-08-10 21:25:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_proxy_address_enumerator_next_async (GSocketAddressEnumerator *enumerator,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GProxyAddressEnumeratorPrivate *priv = GET_PRIVATE (enumerator);
|
2012-08-02 21:48:22 +02:00
|
|
|
GTask *task;
|
2010-08-10 21:25:11 +02:00
|
|
|
|
2012-08-02 21:48:22 +02:00
|
|
|
task = g_task_new (enumerator, cancellable, callback, user_data);
|
2016-06-17 01:39:38 +02:00
|
|
|
g_task_set_source_tag (task, g_proxy_address_enumerator_next_async);
|
2012-08-02 21:48:22 +02:00
|
|
|
g_task_set_task_data (task, priv, NULL);
|
2010-08-10 21:25:11 +02:00
|
|
|
|
|
|
|
if (priv->proxies == NULL)
|
|
|
|
{
|
2013-01-27 19:53:36 +01:00
|
|
|
g_proxy_resolver_lookup_async (priv->proxy_resolver,
|
2010-08-10 21:25:11 +02:00
|
|
|
priv->dest_uri,
|
|
|
|
cancellable,
|
|
|
|
proxy_lookup_cb,
|
2012-08-02 21:48:22 +02:00
|
|
|
task);
|
2010-08-10 21:25:11 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->addr_enum)
|
|
|
|
{
|
|
|
|
if (priv->proxy_address)
|
|
|
|
{
|
2012-08-02 21:48:22 +02:00
|
|
|
return_result (task);
|
|
|
|
return;
|
2010-08-10 21:25:11 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_socket_address_enumerator_next_async (priv->addr_enum,
|
|
|
|
cancellable,
|
|
|
|
address_enumerate_cb,
|
2012-08-02 21:48:22 +02:00
|
|
|
task);
|
2010-08-10 21:25:11 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-02 21:48:22 +02:00
|
|
|
complete_async (task);
|
2010-08-10 21:25:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static GSocketAddress *
|
|
|
|
g_proxy_address_enumerator_next_finish (GSocketAddressEnumerator *enumerator,
|
|
|
|
GAsyncResult *result,
|
|
|
|
GError **error)
|
|
|
|
{
|
2012-08-02 21:48:22 +02:00
|
|
|
g_return_val_if_fail (g_task_is_valid (result, enumerator), NULL);
|
2010-08-10 21:25:11 +02:00
|
|
|
|
2012-08-02 21:48:22 +02:00
|
|
|
return g_task_propagate_pointer (G_TASK (result), error);
|
2010-08-10 21:25:11 +02:00
|
|
|
}
|
|
|
|
|
2013-05-05 22:50:43 +02:00
|
|
|
static void
|
|
|
|
g_proxy_address_enumerator_constructed (GObject *object)
|
|
|
|
{
|
|
|
|
GProxyAddressEnumeratorPrivate *priv = GET_PRIVATE (object);
|
|
|
|
GSocketConnectable *conn;
|
|
|
|
guint port;
|
|
|
|
|
|
|
|
if (priv->dest_uri)
|
|
|
|
{
|
|
|
|
conn = g_network_address_parse_uri (priv->dest_uri, priv->default_port, NULL);
|
|
|
|
if (conn)
|
|
|
|
{
|
|
|
|
g_object_get (conn,
|
|
|
|
"hostname", &priv->dest_hostname,
|
|
|
|
"port", &port,
|
|
|
|
NULL);
|
|
|
|
priv->dest_port = port;
|
|
|
|
|
|
|
|
g_object_unref (conn);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
g_warning ("Invalid URI '%s'", priv->dest_uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (g_proxy_address_enumerator_parent_class)->constructed (object);
|
|
|
|
}
|
|
|
|
|
2010-08-10 21:25:11 +02:00
|
|
|
static void
|
|
|
|
g_proxy_address_enumerator_get_property (GObject *object,
|
2013-05-05 22:50:43 +02:00
|
|
|
guint property_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
2010-08-10 21:25:11 +02:00
|
|
|
{
|
|
|
|
GProxyAddressEnumeratorPrivate *priv = GET_PRIVATE (object);
|
|
|
|
switch (property_id)
|
|
|
|
{
|
2013-05-05 22:50:43 +02:00
|
|
|
case PROP_URI:
|
|
|
|
g_value_set_string (value, priv->dest_uri);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_DEFAULT_PORT:
|
|
|
|
g_value_set_uint (value, priv->default_port);
|
|
|
|
break;
|
2010-08-10 21:25:11 +02:00
|
|
|
|
2013-05-05 22:50:43 +02:00
|
|
|
case PROP_CONNECTABLE:
|
|
|
|
g_value_set_object (value, priv->connectable);
|
|
|
|
break;
|
2010-08-10 21:25:11 +02:00
|
|
|
|
2013-05-05 22:50:43 +02:00
|
|
|
case PROP_PROXY_RESOLVER:
|
|
|
|
g_value_set_object (value, priv->proxy_resolver);
|
|
|
|
break;
|
2013-01-27 19:53:36 +01:00
|
|
|
|
2013-05-05 22:50:43 +02:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
2010-08-10 21:25:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_proxy_address_enumerator_set_property (GObject *object,
|
2013-05-05 22:50:43 +02:00
|
|
|
guint property_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
2010-08-10 21:25:11 +02:00
|
|
|
{
|
|
|
|
GProxyAddressEnumeratorPrivate *priv = GET_PRIVATE (object);
|
|
|
|
switch (property_id)
|
|
|
|
{
|
2013-05-05 22:50:43 +02:00
|
|
|
case PROP_URI:
|
|
|
|
priv->dest_uri = g_value_dup_string (value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_DEFAULT_PORT:
|
|
|
|
priv->default_port = g_value_get_uint (value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_CONNECTABLE:
|
|
|
|
priv->connectable = g_value_dup_object (value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_PROXY_RESOLVER:
|
|
|
|
if (priv->proxy_resolver)
|
|
|
|
g_object_unref (priv->proxy_resolver);
|
|
|
|
priv->proxy_resolver = g_value_get_object (value);
|
|
|
|
if (!priv->proxy_resolver)
|
|
|
|
priv->proxy_resolver = g_proxy_resolver_get_default ();
|
|
|
|
g_object_ref (priv->proxy_resolver);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
2010-08-10 21:25:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_proxy_address_enumerator_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
GProxyAddressEnumeratorPrivate *priv = GET_PRIVATE (object);
|
|
|
|
|
|
|
|
if (priv->connectable)
|
|
|
|
g_object_unref (priv->connectable);
|
|
|
|
|
2013-01-27 19:53:36 +01:00
|
|
|
if (priv->proxy_resolver)
|
|
|
|
g_object_unref (priv->proxy_resolver);
|
|
|
|
|
2010-08-10 21:25:11 +02:00
|
|
|
g_free (priv->dest_uri);
|
|
|
|
g_free (priv->dest_hostname);
|
|
|
|
|
|
|
|
if (priv->dest_ips)
|
|
|
|
g_resolver_free_addresses (priv->dest_ips);
|
|
|
|
|
|
|
|
g_strfreev (priv->proxies);
|
|
|
|
|
|
|
|
if (priv->addr_enum)
|
|
|
|
g_object_unref (priv->addr_enum);
|
|
|
|
|
|
|
|
g_free (priv->proxy_type);
|
|
|
|
g_free (priv->proxy_username);
|
|
|
|
g_free (priv->proxy_password);
|
|
|
|
|
2012-04-22 21:20:14 +02:00
|
|
|
g_clear_error (&priv->last_error);
|
|
|
|
|
2010-08-10 21:25:11 +02:00
|
|
|
G_OBJECT_CLASS (g_proxy_address_enumerator_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_proxy_address_enumerator_init (GProxyAddressEnumerator *self)
|
|
|
|
{
|
2013-06-24 16:43:04 +02:00
|
|
|
self->priv = g_proxy_address_enumerator_get_instance_private (self);
|
2010-08-10 21:25:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_proxy_address_enumerator_class_init (GProxyAddressEnumeratorClass *proxy_enumerator_class)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (proxy_enumerator_class);
|
|
|
|
GSocketAddressEnumeratorClass *enumerator_class = G_SOCKET_ADDRESS_ENUMERATOR_CLASS (proxy_enumerator_class);
|
|
|
|
|
2013-05-05 22:50:43 +02:00
|
|
|
object_class->constructed = g_proxy_address_enumerator_constructed;
|
2010-08-10 21:25:11 +02:00
|
|
|
object_class->set_property = g_proxy_address_enumerator_set_property;
|
|
|
|
object_class->get_property = g_proxy_address_enumerator_get_property;
|
|
|
|
object_class->finalize = g_proxy_address_enumerator_finalize;
|
|
|
|
|
|
|
|
enumerator_class->next = g_proxy_address_enumerator_next;
|
|
|
|
enumerator_class->next_async = g_proxy_address_enumerator_next_async;
|
|
|
|
enumerator_class->next_finish = g_proxy_address_enumerator_next_finish;
|
|
|
|
|
2023-11-29 14:23:09 +01:00
|
|
|
/**
|
|
|
|
* GProxyAddressEnumerator:uri:
|
|
|
|
*
|
|
|
|
* The destination URI. Use `none://` for a generic socket.
|
|
|
|
*/
|
2010-08-10 21:25:11 +02:00
|
|
|
g_object_class_install_property (object_class,
|
|
|
|
PROP_URI,
|
2023-04-28 01:59:26 +02:00
|
|
|
g_param_spec_string ("uri", NULL, NULL,
|
2010-08-10 21:25:11 +02:00
|
|
|
NULL,
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
G_PARAM_CONSTRUCT_ONLY |
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
|
|
|
|
2013-05-05 22:50:43 +02:00
|
|
|
/**
|
|
|
|
* GProxyAddressEnumerator:default-port:
|
|
|
|
*
|
|
|
|
* The default port to use if #GProxyAddressEnumerator:uri does not
|
|
|
|
* specify one.
|
|
|
|
*
|
|
|
|
* Since: 2.38
|
|
|
|
*/
|
|
|
|
g_object_class_install_property (object_class,
|
|
|
|
PROP_DEFAULT_PORT,
|
2023-04-28 01:59:26 +02:00
|
|
|
g_param_spec_uint ("default-port", NULL, NULL,
|
2013-05-05 22:50:43 +02:00
|
|
|
0, 65535, 0,
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
G_PARAM_CONSTRUCT_ONLY |
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
|
|
|
|
2023-11-29 14:23:09 +01:00
|
|
|
/**
|
|
|
|
* GProxyAddressEnumerator:connectable:
|
|
|
|
*
|
|
|
|
* The connectable being enumerated.
|
|
|
|
*/
|
2010-08-10 21:25:11 +02:00
|
|
|
g_object_class_install_property (object_class,
|
|
|
|
PROP_CONNECTABLE,
|
2023-04-28 01:59:26 +02:00
|
|
|
g_param_spec_object ("connectable", NULL, NULL,
|
2010-08-10 21:25:11 +02:00
|
|
|
G_TYPE_SOCKET_CONNECTABLE,
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
G_PARAM_CONSTRUCT_ONLY |
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
2013-01-27 19:53:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GProxyAddressEnumerator:proxy-resolver:
|
|
|
|
*
|
|
|
|
* The proxy resolver to use.
|
|
|
|
*
|
|
|
|
* Since: 2.36
|
|
|
|
*/
|
|
|
|
g_object_class_install_property (object_class,
|
|
|
|
PROP_PROXY_RESOLVER,
|
2023-04-28 01:59:26 +02:00
|
|
|
g_param_spec_object ("proxy-resolver", NULL, NULL,
|
2013-01-27 19:53:36 +01:00
|
|
|
G_TYPE_PROXY_RESOLVER,
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
G_PARAM_CONSTRUCT |
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
2010-08-10 21:25:11 +02:00
|
|
|
}
|