2008-12-29 19:38:28 +01:00
|
|
|
|
/* GIO - GLib Input, Output and Streaming Library
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2008 Red Hat, Inc.
|
|
|
|
|
*
|
2022-05-18 10:12:45 +02:00
|
|
|
|
* SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
|
*
|
2008-12-29 19:38:28 +01: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.
|
2008-12-29 19:38:28 +01: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/>.
|
2008-12-29 19:38:28 +01:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
#include "gsocketconnectable.h"
|
|
|
|
|
#include "glibintl.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-11-14 15:27:18 +01:00
|
|
|
|
* GSocketConnectable:
|
2008-12-29 19:38:28 +01:00
|
|
|
|
*
|
|
|
|
|
* Objects that describe one or more potential socket endpoints
|
2023-11-14 15:27:18 +01:00
|
|
|
|
* implement `GSocketConnectable`. Callers can then use
|
|
|
|
|
* [method@Gio.SocketConnectable.enumerate] to get a
|
|
|
|
|
* [class@Gio.SocketAddressEnumerator] to try out each socket address in turn
|
|
|
|
|
* until one succeeds, as shown in the sample code below.
|
2008-12-29 19:38:28 +01:00
|
|
|
|
*
|
2023-11-14 15:27:18 +01:00
|
|
|
|
* ```c
|
2008-12-29 19:38:28 +01:00
|
|
|
|
* MyConnectionType *
|
|
|
|
|
* connect_to_host (const char *hostname,
|
|
|
|
|
* guint16 port,
|
|
|
|
|
* GCancellable *cancellable,
|
|
|
|
|
* GError **error)
|
|
|
|
|
* {
|
|
|
|
|
* MyConnection *conn = NULL;
|
|
|
|
|
* GSocketConnectable *addr;
|
|
|
|
|
* GSocketAddressEnumerator *enumerator;
|
|
|
|
|
* GSocketAddress *sockaddr;
|
|
|
|
|
* GError *conn_error = NULL;
|
|
|
|
|
*
|
2012-03-20 21:40:32 +01:00
|
|
|
|
* addr = g_network_address_new (hostname, port);
|
2008-12-29 19:38:28 +01:00
|
|
|
|
* enumerator = g_socket_connectable_enumerate (addr);
|
|
|
|
|
* g_object_unref (addr);
|
|
|
|
|
*
|
2014-02-15 03:33:36 +01:00
|
|
|
|
* // Try each sockaddr until we succeed. Record the first connection error,
|
|
|
|
|
* // but not any further ones (since they'll probably be basically the same
|
|
|
|
|
* // as the first).
|
2008-12-29 19:38:28 +01:00
|
|
|
|
* while (!conn && (sockaddr = g_socket_address_enumerator_next (enumerator, cancellable, error))
|
|
|
|
|
* {
|
|
|
|
|
* conn = connect_to_sockaddr (sockaddr, conn_error ? NULL : &conn_error);
|
|
|
|
|
* g_object_unref (sockaddr);
|
|
|
|
|
* }
|
|
|
|
|
* g_object_unref (enumerator);
|
|
|
|
|
*
|
|
|
|
|
* if (conn)
|
|
|
|
|
* {
|
|
|
|
|
* if (conn_error)
|
|
|
|
|
* {
|
2014-02-15 03:33:36 +01:00
|
|
|
|
* // We couldn't connect to the first address, but we succeeded
|
|
|
|
|
* // in connecting to a later address.
|
2008-12-29 19:38:28 +01:00
|
|
|
|
* g_error_free (conn_error);
|
|
|
|
|
* }
|
|
|
|
|
* return conn;
|
|
|
|
|
* }
|
|
|
|
|
* else if (error)
|
|
|
|
|
* {
|
2014-02-15 03:33:36 +01:00
|
|
|
|
* /// Either initial lookup failed, or else the caller cancelled us.
|
2008-12-29 19:38:28 +01:00
|
|
|
|
* if (conn_error)
|
|
|
|
|
* g_error_free (conn_error);
|
|
|
|
|
* return NULL;
|
|
|
|
|
* }
|
|
|
|
|
* else
|
|
|
|
|
* {
|
|
|
|
|
* g_error_propagate (error, conn_error);
|
|
|
|
|
* return NULL;
|
|
|
|
|
* }
|
|
|
|
|
* }
|
2023-11-14 15:27:18 +01:00
|
|
|
|
* ```
|
2009-05-28 00:20:08 +02:00
|
|
|
|
*/
|
2008-12-29 19:38:28 +01:00
|
|
|
|
|
|
|
|
|
|
2010-04-20 05:38:02 +02:00
|
|
|
|
typedef GSocketConnectableIface GSocketConnectableInterface;
|
|
|
|
|
G_DEFINE_INTERFACE (GSocketConnectable, g_socket_connectable, G_TYPE_OBJECT)
|
2008-12-29 19:38:28 +01:00
|
|
|
|
|
2010-04-20 05:38:02 +02:00
|
|
|
|
static void
|
|
|
|
|
g_socket_connectable_default_init (GSocketConnectableInterface *iface)
|
|
|
|
|
{
|
2008-12-29 19:38:28 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_socket_connectable_enumerate:
|
|
|
|
|
* @connectable: a #GSocketConnectable
|
|
|
|
|
*
|
|
|
|
|
* Creates a #GSocketAddressEnumerator for @connectable.
|
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
|
* Returns: (transfer full): a new #GSocketAddressEnumerator.
|
2008-12-29 19:38:28 +01:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.22
|
2009-05-28 00:20:08 +02:00
|
|
|
|
*/
|
2008-12-29 19:38:28 +01:00
|
|
|
|
GSocketAddressEnumerator *
|
|
|
|
|
g_socket_connectable_enumerate (GSocketConnectable *connectable)
|
|
|
|
|
{
|
|
|
|
|
GSocketConnectableIface *iface;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_SOCKET_CONNECTABLE (connectable), NULL);
|
|
|
|
|
|
|
|
|
|
iface = G_SOCKET_CONNECTABLE_GET_IFACE (connectable);
|
|
|
|
|
|
|
|
|
|
return (* iface->enumerate) (connectable);
|
|
|
|
|
}
|
2010-08-10 22:53:25 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_socket_connectable_proxy_enumerate:
|
|
|
|
|
* @connectable: a #GSocketConnectable
|
|
|
|
|
*
|
|
|
|
|
* Creates a #GSocketAddressEnumerator for @connectable that will
|
2019-03-07 18:30:31 +01:00
|
|
|
|
* return a #GProxyAddress for each of its addresses that you must connect
|
2010-08-10 22:53:25 +02:00
|
|
|
|
* to via a proxy.
|
|
|
|
|
*
|
|
|
|
|
* If @connectable does not implement
|
|
|
|
|
* g_socket_connectable_proxy_enumerate(), this will fall back to
|
|
|
|
|
* calling g_socket_connectable_enumerate().
|
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
|
* Returns: (transfer full): a new #GSocketAddressEnumerator.
|
2010-08-10 22:53:25 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
|
|
|
|
*/
|
|
|
|
|
GSocketAddressEnumerator *
|
|
|
|
|
g_socket_connectable_proxy_enumerate (GSocketConnectable *connectable)
|
|
|
|
|
{
|
|
|
|
|
GSocketConnectableIface *iface;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_SOCKET_CONNECTABLE (connectable), NULL);
|
|
|
|
|
|
|
|
|
|
iface = G_SOCKET_CONNECTABLE_GET_IFACE (connectable);
|
|
|
|
|
|
|
|
|
|
if (iface->proxy_enumerate)
|
|
|
|
|
return (* iface->proxy_enumerate) (connectable);
|
|
|
|
|
else
|
|
|
|
|
return (* iface->enumerate) (connectable);
|
|
|
|
|
}
|
2015-10-04 16:24:24 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_socket_connectable_to_string:
|
|
|
|
|
* @connectable: a #GSocketConnectable
|
|
|
|
|
*
|
|
|
|
|
* Format a #GSocketConnectable as a string. This is a human-readable format for
|
|
|
|
|
* use in debugging output, and is not a stable serialization format. It is not
|
|
|
|
|
* suitable for use in user interfaces as it exposes too much information for a
|
|
|
|
|
* user.
|
|
|
|
|
*
|
|
|
|
|
* If the #GSocketConnectable implementation does not support string formatting,
|
|
|
|
|
* the implementation’s type name will be returned as a fallback.
|
|
|
|
|
*
|
|
|
|
|
* Returns: (transfer full): the formatted string
|
|
|
|
|
*
|
2015-10-25 19:43:54 +01:00
|
|
|
|
* Since: 2.48
|
2015-10-04 16:24:24 +02:00
|
|
|
|
*/
|
|
|
|
|
gchar *
|
|
|
|
|
g_socket_connectable_to_string (GSocketConnectable *connectable)
|
|
|
|
|
{
|
|
|
|
|
GSocketConnectableIface *iface;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_SOCKET_CONNECTABLE (connectable), NULL);
|
|
|
|
|
|
|
|
|
|
iface = G_SOCKET_CONNECTABLE_GET_IFACE (connectable);
|
|
|
|
|
|
|
|
|
|
if (iface->to_string != NULL)
|
|
|
|
|
return iface->to_string (connectable);
|
|
|
|
|
else
|
|
|
|
|
return g_strdup (G_OBJECT_TYPE_NAME (connectable));
|
|
|
|
|
}
|