1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2008, 2009 codethink
4 * Copyright © 2009 Red Hat, Inc
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Authors: Ryan Lortie <desrt@desrt.ca>
22 * Alexander Larsson <alexl@redhat.com>
26 #include "gsocketclient.h"
31 #include <gio/gioenumtypes.h>
32 #include <gio/gsocketaddressenumerator.h>
33 #include <gio/gsocketconnectable.h>
34 #include <gio/gsocketconnection.h>
35 #include <gio/gproxyaddressenumerator.h>
36 #include <gio/gproxyaddress.h>
37 #include <gio/gsimpleasyncresult.h>
38 #include <gio/gcancellable.h>
39 #include <gio/gioerror.h>
40 #include <gio/gsocket.h>
41 #include <gio/gnetworkaddress.h>
42 #include <gio/gnetworkservice.h>
43 #include <gio/gproxy.h>
44 #include <gio/gsocketaddress.h>
45 #include <gio/gtcpconnection.h>
46 #include <gio/gtcpwrapperconnection.h>
47 #include <gio/gtlscertificate.h>
48 #include <gio/gtlsclientconnection.h>
53 * SECTION:gsocketclient
54 * @short_description: Helper for connecting to a network service
56 * @see_also: #GSocketConnection, #GSocketListener
58 * #GSocketClient is a high-level utility class for connecting to a
59 * network host using a connection oriented socket type.
61 * You create a #GSocketClient object, set any options you want, and then
62 * call a sync or async connect operation, which returns a #GSocketConnection
63 * subclass on success.
65 * The type of the #GSocketConnection object returned depends on the type of
66 * the underlying socket that is in use. For instance, for a TCP/IP connection
67 * it will be a #GTcpConnection.
73 G_DEFINE_TYPE (GSocketClient, g_socket_client, G_TYPE_OBJECT);
85 PROP_TLS_VALIDATION_FLAGS
88 struct _GSocketClientPrivate
92 GSocketProtocol protocol;
93 GSocketAddress *local_address;
95 gboolean enable_proxy;
96 GHashTable *app_proxies;
98 GTlsCertificateFlags tls_validation_flags;
102 create_socket (GSocketClient *client,
103 GSocketAddress *dest_address,
106 GSocketFamily family;
109 family = client->priv->family;
110 if (family == G_SOCKET_FAMILY_INVALID &&
111 client->priv->local_address != NULL)
112 family = g_socket_address_get_family (client->priv->local_address);
113 if (family == G_SOCKET_FAMILY_INVALID)
114 family = g_socket_address_get_family (dest_address);
116 socket = g_socket_new (family,
118 client->priv->protocol,
123 if (client->priv->local_address)
125 if (!g_socket_bind (socket,
126 client->priv->local_address,
130 g_object_unref (socket);
135 if (client->priv->timeout)
136 g_socket_set_timeout (socket, client->priv->timeout);
142 can_use_proxy (GSocketClient *client)
144 GSocketClientPrivate *priv = client->priv;
146 return priv->enable_proxy
147 && priv->type == G_SOCKET_TYPE_STREAM;
151 g_socket_client_init (GSocketClient *client)
153 client->priv = G_TYPE_INSTANCE_GET_PRIVATE (client,
154 G_TYPE_SOCKET_CLIENT,
155 GSocketClientPrivate);
156 client->priv->type = G_SOCKET_TYPE_STREAM;
157 client->priv->app_proxies = g_hash_table_new_full (g_str_hash,
164 * g_socket_client_new:
166 * Creates a new #GSocketClient with the default options.
168 * Returns: a #GSocketClient.
169 * Free the returned object with g_object_unref().
174 g_socket_client_new (void)
176 return g_object_new (G_TYPE_SOCKET_CLIENT, NULL);
180 g_socket_client_finalize (GObject *object)
182 GSocketClient *client = G_SOCKET_CLIENT (object);
184 if (client->priv->local_address)
185 g_object_unref (client->priv->local_address);
187 if (G_OBJECT_CLASS (g_socket_client_parent_class)->finalize)
188 (*G_OBJECT_CLASS (g_socket_client_parent_class)->finalize) (object);
190 g_hash_table_unref (client->priv->app_proxies);
194 g_socket_client_get_property (GObject *object,
199 GSocketClient *client = G_SOCKET_CLIENT (object);
204 g_value_set_enum (value, client->priv->family);
208 g_value_set_enum (value, client->priv->type);
212 g_value_set_enum (value, client->priv->protocol);
215 case PROP_LOCAL_ADDRESS:
216 g_value_set_object (value, client->priv->local_address);
220 g_value_set_uint (value, client->priv->timeout);
223 case PROP_ENABLE_PROXY:
224 g_value_set_boolean (value, client->priv->enable_proxy);
228 g_value_set_boolean (value, g_socket_client_get_tls (client));
231 case PROP_TLS_VALIDATION_FLAGS:
232 g_value_set_flags (value, g_socket_client_get_tls_validation_flags (client));
236 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
241 g_socket_client_set_property (GObject *object,
246 GSocketClient *client = G_SOCKET_CLIENT (object);
251 g_socket_client_set_family (client, g_value_get_enum (value));
255 g_socket_client_set_socket_type (client, g_value_get_enum (value));
259 g_socket_client_set_protocol (client, g_value_get_enum (value));
262 case PROP_LOCAL_ADDRESS:
263 g_socket_client_set_local_address (client, g_value_get_object (value));
267 g_socket_client_set_timeout (client, g_value_get_uint (value));
270 case PROP_ENABLE_PROXY:
271 g_socket_client_set_enable_proxy (client, g_value_get_boolean (value));
275 g_socket_client_set_tls (client, g_value_get_boolean (value));
278 case PROP_TLS_VALIDATION_FLAGS:
279 g_socket_client_set_tls_validation_flags (client, g_value_get_flags (value));
283 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
288 * g_socket_client_get_family:
289 * @client: a #GSocketClient.
291 * Gets the socket family of the socket client.
293 * See g_socket_client_set_family() for details.
295 * Returns: a #GSocketFamily
300 g_socket_client_get_family (GSocketClient *client)
302 return client->priv->family;
306 * g_socket_client_set_family:
307 * @client: a #GSocketClient.
308 * @family: a #GSocketFamily
310 * Sets the socket family of the socket client.
311 * If this is set to something other than %G_SOCKET_FAMILY_INVALID
312 * then the sockets created by this object will be of the specified
315 * This might be useful for instance if you want to force the local
316 * connection to be an ipv4 socket, even though the address might
317 * be an ipv6 mapped to ipv4 address.
322 g_socket_client_set_family (GSocketClient *client,
323 GSocketFamily family)
325 if (client->priv->family == family)
328 client->priv->family = family;
329 g_object_notify (G_OBJECT (client), "family");
333 * g_socket_client_get_socket_type:
334 * @client: a #GSocketClient.
336 * Gets the socket type of the socket client.
338 * See g_socket_client_set_socket_type() for details.
340 * Returns: a #GSocketFamily
345 g_socket_client_get_socket_type (GSocketClient *client)
347 return client->priv->type;
351 * g_socket_client_set_socket_type:
352 * @client: a #GSocketClient.
353 * @type: a #GSocketType
355 * Sets the socket type of the socket client.
356 * The sockets created by this object will be of the specified
359 * It doesn't make sense to specify a type of %G_SOCKET_TYPE_DATAGRAM,
360 * as GSocketClient is used for connection oriented services.
365 g_socket_client_set_socket_type (GSocketClient *client,
368 if (client->priv->type == type)
371 client->priv->type = type;
372 g_object_notify (G_OBJECT (client), "type");
376 * g_socket_client_get_protocol:
377 * @client: a #GSocketClient
379 * Gets the protocol name type of the socket client.
381 * See g_socket_client_set_protocol() for details.
383 * Returns: a #GSocketProtocol
388 g_socket_client_get_protocol (GSocketClient *client)
390 return client->priv->protocol;
394 * g_socket_client_set_protocol:
395 * @client: a #GSocketClient.
396 * @protocol: a #GSocketProtocol
398 * Sets the protocol of the socket client.
399 * The sockets created by this object will use of the specified
402 * If @protocol is %0 that means to use the default
403 * protocol for the socket family and type.
408 g_socket_client_set_protocol (GSocketClient *client,
409 GSocketProtocol protocol)
411 if (client->priv->protocol == protocol)
414 client->priv->protocol = protocol;
415 g_object_notify (G_OBJECT (client), "protocol");
419 * g_socket_client_get_local_address:
420 * @client: a #GSocketClient.
422 * Gets the local address of the socket client.
424 * See g_socket_client_set_local_address() for details.
426 * Returns: (transfer none): a #GSocketAddress or %NULL. Do not free.
431 g_socket_client_get_local_address (GSocketClient *client)
433 return client->priv->local_address;
437 * g_socket_client_set_local_address:
438 * @client: a #GSocketClient.
439 * @address: a #GSocketAddress, or %NULL
441 * Sets the local address of the socket client.
442 * The sockets created by this object will bound to the
443 * specified address (if not %NULL) before connecting.
445 * This is useful if you want to ensure that the local
446 * side of the connection is on a specific port, or on
447 * a specific interface.
452 g_socket_client_set_local_address (GSocketClient *client,
453 GSocketAddress *address)
456 g_object_ref (address);
458 if (client->priv->local_address)
460 g_object_unref (client->priv->local_address);
462 client->priv->local_address = address;
463 g_object_notify (G_OBJECT (client), "local-address");
467 * g_socket_client_get_timeout:
468 * @client: a #GSocketClient
470 * Gets the I/O timeout time for sockets created by @client.
472 * See g_socket_client_set_timeout() for details.
474 * Returns: the timeout in seconds
479 g_socket_client_get_timeout (GSocketClient *client)
481 return client->priv->timeout;
486 * g_socket_client_set_timeout:
487 * @client: a #GSocketClient.
488 * @timeout: the timeout
490 * Sets the I/O timeout for sockets created by @client. @timeout is a
491 * time in seconds, or 0 for no timeout (the default).
493 * The timeout value affects the initial connection attempt as well,
494 * so setting this may cause calls to g_socket_client_connect(), etc,
495 * to fail with %G_IO_ERROR_TIMED_OUT.
500 g_socket_client_set_timeout (GSocketClient *client,
503 if (client->priv->timeout == timeout)
506 client->priv->timeout = timeout;
507 g_object_notify (G_OBJECT (client), "timeout");
511 * g_socket_client_get_enable_proxy:
512 * @client: a #GSocketClient.
514 * Gets the proxy enable state; see g_socket_client_set_enable_proxy()
516 * Returns: whether proxying is enabled
521 g_socket_client_get_enable_proxy (GSocketClient *client)
523 return client->priv->enable_proxy;
527 * g_socket_client_set_enable_proxy:
528 * @client: a #GSocketClient.
529 * @enable: whether to enable proxies
531 * Sets whether or not @client attempts to make connections via a
532 * proxy server. When enabled (the default), #GSocketClient will use a
533 * #GProxyResolver to determine if a proxy protocol such as SOCKS is
534 * needed, and automatically do the necessary proxy negotiation.
539 g_socket_client_set_enable_proxy (GSocketClient *client,
543 if (client->priv->enable_proxy == enable)
546 client->priv->enable_proxy = enable;
547 g_object_notify (G_OBJECT (client), "enable-proxy");
551 * g_socket_client_get_tls:
552 * @client: a #GSocketClient.
554 * Gets whether @client creates TLS connections. See
555 * g_socket_client_set_tls() for details.
557 * Returns: whether @client uses TLS
562 g_socket_client_get_tls (GSocketClient *client)
564 return client->priv->tls;
568 * g_socket_client_set_tls:
569 * @client: a #GSocketClient.
570 * @tls: whether to use TLS
572 * Sets whether @client creates TLS (aka SSL) connections. If @tls is
573 * %TRUE, @client will wrap its connections in a #GTlsClientConnection
574 * and perform a TLS handshake when connecting.
576 * Note that since #GSocketClient must return a #GSocketConnection,
577 * but #GTlsClientConnection is not a #GSocketConnection, this
578 * actually wraps the resulting #GTlsClientConnection in a
579 * #GTcpWrapperConnection when returning it. You can use
580 * g_tcp_wrapper_connection_get_base_io_stream() on the return value
581 * to extract the #GTlsClientConnection.
586 g_socket_client_set_tls (GSocketClient *client,
590 if (tls == client->priv->tls)
593 client->priv->tls = tls;
594 g_object_notify (G_OBJECT (client), "tls");
598 * g_socket_client_get_tls_validation_flags:
599 * @client: a #GSocketClient.
601 * Gets the TLS validation flags used creating TLS connections via
604 * Returns: the TLS validation flags
609 g_socket_client_get_tls_validation_flags (GSocketClient *client)
611 return client->priv->tls_validation_flags;
615 * g_socket_client_set_tls_validation_flags:
616 * @client: a #GSocketClient.
617 * @flags: the validation flags
619 * Sets the TLS validation flags used when creating TLS connections
620 * via @client. The default value is %G_TLS_CERTIFICATE_VALIDATE_ALL.
625 g_socket_client_set_tls_validation_flags (GSocketClient *client,
626 GTlsCertificateFlags flags)
628 if (client->priv->tls_validation_flags != flags)
630 client->priv->tls_validation_flags = flags;
631 g_object_notify (G_OBJECT (client), "tls-validation-flags");
636 g_socket_client_class_init (GSocketClientClass *class)
638 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
640 g_type_class_add_private (class, sizeof (GSocketClientPrivate));
642 gobject_class->finalize = g_socket_client_finalize;
643 gobject_class->set_property = g_socket_client_set_property;
644 gobject_class->get_property = g_socket_client_get_property;
646 g_object_class_install_property (gobject_class, PROP_FAMILY,
647 g_param_spec_enum ("family",
649 P_("The sockets address family to use for socket construction"),
650 G_TYPE_SOCKET_FAMILY,
651 G_SOCKET_FAMILY_INVALID,
654 G_PARAM_STATIC_STRINGS));
656 g_object_class_install_property (gobject_class, PROP_TYPE,
657 g_param_spec_enum ("type",
659 P_("The sockets type to use for socket construction"),
661 G_SOCKET_TYPE_STREAM,
664 G_PARAM_STATIC_STRINGS));
666 g_object_class_install_property (gobject_class, PROP_PROTOCOL,
667 g_param_spec_enum ("protocol",
668 P_("Socket protocol"),
669 P_("The protocol to use for socket construction, or 0 for default"),
670 G_TYPE_SOCKET_PROTOCOL,
671 G_SOCKET_PROTOCOL_DEFAULT,
674 G_PARAM_STATIC_STRINGS));
676 g_object_class_install_property (gobject_class, PROP_LOCAL_ADDRESS,
677 g_param_spec_object ("local-address",
679 P_("The local address constructed sockets will be bound to"),
680 G_TYPE_SOCKET_ADDRESS,
683 G_PARAM_STATIC_STRINGS));
685 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
686 g_param_spec_uint ("timeout",
687 P_("Socket timeout"),
688 P_("The I/O timeout for sockets, or 0 for none"),
692 G_PARAM_STATIC_STRINGS));
694 g_object_class_install_property (gobject_class, PROP_ENABLE_PROXY,
695 g_param_spec_boolean ("enable-proxy",
697 P_("Enable proxy support"),
701 G_PARAM_STATIC_STRINGS));
703 g_object_class_install_property (gobject_class, PROP_TLS,
704 g_param_spec_boolean ("tls",
706 P_("Whether to create TLS connections"),
710 G_PARAM_STATIC_STRINGS));
711 g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
712 g_param_spec_flags ("tls-validation-flags",
713 P_("TLS validation flags"),
714 P_("TLS validation flags to use"),
715 G_TYPE_TLS_CERTIFICATE_FLAGS,
716 G_TLS_CERTIFICATE_VALIDATE_ALL,
719 G_PARAM_STATIC_STRINGS));
723 * g_socket_client_connect:
724 * @client: a #GSocketClient.
725 * @connectable: a #GSocketConnectable specifying the remote address.
726 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
727 * @error: #GError for error reporting, or %NULL to ignore.
729 * Tries to resolve the @connectable and make a network connection to it.
731 * Upon a successful connection, a new #GSocketConnection is constructed
732 * and returned. The caller owns this new object and must drop their
733 * reference to it when finished with it.
735 * The type of the #GSocketConnection object returned depends on the type of
736 * the underlying socket that is used. For instance, for a TCP/IP connection
737 * it will be a #GTcpConnection.
739 * The socket created will be the same family as the address that the
740 * @connectable resolves to, unless family is set with g_socket_client_set_family()
741 * or indirectly via g_socket_client_set_local_address(). The socket type
742 * defaults to %G_SOCKET_TYPE_STREAM but can be set with
743 * g_socket_client_set_socket_type().
745 * If a local address is specified with g_socket_client_set_local_address() the
746 * socket will be bound to this address before connecting.
748 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
753 g_socket_client_connect (GSocketClient *client,
754 GSocketConnectable *connectable,
755 GCancellable *cancellable,
758 GIOStream *connection = NULL;
759 GSocketAddressEnumerator *enumerator = NULL;
760 GError *last_error, *tmp_error;
764 if (can_use_proxy (client))
765 enumerator = g_socket_connectable_proxy_enumerate (connectable);
767 enumerator = g_socket_connectable_enumerate (connectable);
769 while (connection == NULL)
771 GSocketAddress *address = NULL;
774 if (g_cancellable_is_cancelled (cancellable))
776 g_clear_error (error);
777 g_cancellable_set_error_if_cancelled (cancellable, error);
782 address = g_socket_address_enumerator_next (enumerator, cancellable,
789 g_clear_error (&last_error);
790 g_propagate_error (error, tmp_error);
794 g_propagate_error (error, last_error);
797 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
798 _("Unknown error on connect"));
802 /* clear error from previous attempt */
803 g_clear_error (&last_error);
805 socket = create_socket (client, address, &last_error);
808 g_object_unref (address);
812 if (g_socket_connect (socket, address, cancellable, &last_error))
813 connection = (GIOStream *)g_socket_connection_factory_create_connection (socket);
816 G_IS_PROXY_ADDRESS (address) &&
817 client->priv->enable_proxy)
819 GProxyAddress *proxy_addr = G_PROXY_ADDRESS (address);
820 const gchar *protocol;
823 protocol = g_proxy_address_get_protocol (proxy_addr);
824 proxy = g_proxy_get_default_for_protocol (protocol);
826 /* The connection should not be anything else then TCP Connection,
827 * but let's put a safety guard in case
829 if (!G_IS_TCP_CONNECTION (connection))
831 g_critical ("Trying to proxy over non-TCP connection, this is "
832 "most likely a bug in GLib IO library.");
834 g_set_error_literal (&last_error,
835 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
836 _("Trying to proxy over non-TCP connection is not supported."));
838 g_object_unref (connection);
843 GIOStream *proxy_connection;
845 proxy_connection = g_proxy_connect (proxy,
850 g_object_unref (connection);
851 connection = proxy_connection;
852 g_object_unref (proxy);
854 else if (!g_hash_table_lookup_extended (client->priv->app_proxies,
855 protocol, NULL, NULL))
857 g_set_error (&last_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
858 _("Proxy protocol '%s' is not supported."),
860 g_object_unref (connection);
865 if (connection && client->priv->tls)
869 tlsconn = g_tls_client_connection_new (connection, connectable, &last_error);
870 g_object_unref (connection);
871 connection = tlsconn;
875 g_tls_client_connection_set_validation_flags (G_TLS_CLIENT_CONNECTION (tlsconn),
876 client->priv->tls_validation_flags);
877 if (!g_tls_connection_handshake (G_TLS_CONNECTION (tlsconn),
878 cancellable, &last_error))
880 g_object_unref (tlsconn);
886 if (connection && !G_IS_SOCKET_CONNECTION (connection))
888 GSocketConnection *wrapper_connection;
890 wrapper_connection = g_tcp_wrapper_connection_new (connection, socket);
891 g_object_unref (connection);
892 connection = (GIOStream *)wrapper_connection;
895 g_object_unref (socket);
896 g_object_unref (address);
898 g_object_unref (enumerator);
900 return G_SOCKET_CONNECTION (connection);
904 * g_socket_client_connect_to_host:
905 * @client: a #GSocketClient
906 * @host_and_port: the name and optionally port of the host to connect to
907 * @default_port: the default port to connect to
908 * @cancellable: (allow-none): a #GCancellable, or %NULL
909 * @error: a pointer to a #GError, or %NULL
911 * This is a helper function for g_socket_client_connect().
913 * Attempts to create a TCP connection to the named host.
915 * @host_and_port may be in any of a number of recognized formats; an IPv6
916 * address, an IPv4 address, or a domain name (in which case a DNS
917 * lookup is performed). Quoting with [] is supported for all address
918 * types. A port override may be specified in the usual way with a
919 * colon. Ports may be given as decimal numbers or symbolic names (in
920 * which case an /etc/services lookup is performed).
922 * If no port override is given in @host_and_port then @default_port will be
923 * used as the port number to connect to.
925 * In general, @host_and_port is expected to be provided by the user (allowing
926 * them to give the hostname, and a port override if necessary) and
927 * @default_port is expected to be provided by the application.
929 * In the case that an IP address is given, a single connection
930 * attempt is made. In the case that a name is given, multiple
931 * connection attempts may be made, in turn and according to the
932 * number of address records in DNS, until a connection succeeds.
934 * Upon a successful connection, a new #GSocketConnection is constructed
935 * and returned. The caller owns this new object and must drop their
936 * reference to it when finished with it.
938 * In the event of any failure (DNS error, service not found, no hosts
939 * connectable) %NULL is returned and @error (if non-%NULL) is set
942 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
947 g_socket_client_connect_to_host (GSocketClient *client,
948 const gchar *host_and_port,
949 guint16 default_port,
950 GCancellable *cancellable,
953 GSocketConnectable *connectable;
954 GSocketConnection *connection;
956 connectable = g_network_address_parse (host_and_port, default_port, error);
957 if (connectable == NULL)
960 connection = g_socket_client_connect (client, connectable,
962 g_object_unref (connectable);
968 * g_socket_client_connect_to_service:
969 * @client: a #GSocketConnection
970 * @domain: a domain name
971 * @service: the name of the service to connect to
972 * @cancellable: (allow-none): a #GCancellable, or %NULL
973 * @error: a pointer to a #GError, or %NULL
974 * @returns: (transfer full): a #GSocketConnection if successful, or %NULL on error
976 * Attempts to create a TCP connection to a service.
978 * This call looks up the SRV record for @service at @domain for the
979 * "tcp" protocol. It then attempts to connect, in turn, to each of
980 * the hosts providing the service until either a connection succeeds
981 * or there are no hosts remaining.
983 * Upon a successful connection, a new #GSocketConnection is constructed
984 * and returned. The caller owns this new object and must drop their
985 * reference to it when finished with it.
987 * In the event of any failure (DNS error, service not found, no hosts
988 * connectable) %NULL is returned and @error (if non-%NULL) is set
992 g_socket_client_connect_to_service (GSocketClient *client,
994 const gchar *service,
995 GCancellable *cancellable,
998 GSocketConnectable *connectable;
999 GSocketConnection *connection;
1001 connectable = g_network_service_new (service, "tcp", domain);
1002 connection = g_socket_client_connect (client, connectable,
1003 cancellable, error);
1004 g_object_unref (connectable);
1010 * g_socket_client_connect_to_uri:
1011 * @client: a #GSocketClient
1012 * @uri: A network URI
1013 * @default_port: the default port to connect to
1014 * @cancellable: (allow-none): a #GCancellable, or %NULL
1015 * @error: a pointer to a #GError, or %NULL
1017 * This is a helper function for g_socket_client_connect().
1019 * Attempts to create a TCP connection with a network URI.
1021 * @uri may be any valid URI containing an "authority" (hostname/port)
1022 * component. If a port is not specified in the URI, @default_port
1023 * will be used. TLS will be negotiated if #GSocketClient:tls is %TRUE.
1024 * (#GSocketClient does not know to automatically assume TLS for
1025 * certain URI schemes.)
1027 * Using this rather than g_socket_client_connect() or
1028 * g_socket_client_connect_to_host() allows #GSocketClient to
1029 * determine when to use application-specific proxy protocols.
1031 * Upon a successful connection, a new #GSocketConnection is constructed
1032 * and returned. The caller owns this new object and must drop their
1033 * reference to it when finished with it.
1035 * In the event of any failure (DNS error, service not found, no hosts
1036 * connectable) %NULL is returned and @error (if non-%NULL) is set
1039 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1044 g_socket_client_connect_to_uri (GSocketClient *client,
1046 guint16 default_port,
1047 GCancellable *cancellable,
1050 GSocketConnectable *connectable;
1051 GSocketConnection *connection;
1053 connectable = g_network_address_parse_uri (uri, default_port, error);
1054 if (connectable == NULL)
1057 connection = g_socket_client_connect (client, connectable,
1058 cancellable, error);
1059 g_object_unref (connectable);
1066 GSimpleAsyncResult *result;
1067 GCancellable *cancellable;
1068 GSocketClient *client;
1070 GSocketConnectable *connectable;
1071 GSocketAddressEnumerator *enumerator;
1072 GProxyAddress *proxy_addr;
1073 GSocket *current_socket;
1074 GIOStream *connection;
1077 } GSocketClientAsyncConnectData;
1080 g_socket_client_async_connect_complete (GSocketClientAsyncConnectData *data)
1082 if (data->last_error)
1084 g_simple_async_result_take_error (data->result, data->last_error);
1088 g_assert (data->connection);
1090 if (!G_IS_SOCKET_CONNECTION (data->connection))
1092 GSocketConnection *wrapper_connection;
1094 wrapper_connection = g_tcp_wrapper_connection_new (data->connection,
1095 data->current_socket);
1096 g_object_unref (data->connection);
1097 data->connection = (GIOStream *)wrapper_connection;
1100 g_simple_async_result_set_op_res_gpointer (data->result,
1105 g_simple_async_result_complete (data->result);
1106 g_object_unref (data->result);
1107 g_object_unref (data->connectable);
1108 g_object_unref (data->enumerator);
1109 if (data->cancellable)
1110 g_object_unref (data->cancellable);
1111 if (data->current_socket)
1112 g_object_unref (data->current_socket);
1113 if (data->proxy_addr)
1114 g_object_unref (data->proxy_addr);
1115 g_slice_free (GSocketClientAsyncConnectData, data);
1120 g_socket_client_enumerator_callback (GObject *object,
1121 GAsyncResult *result,
1122 gpointer user_data);
1125 set_last_error (GSocketClientAsyncConnectData *data,
1128 g_clear_error (&data->last_error);
1129 data->last_error = error;
1133 enumerator_next_async (GSocketClientAsyncConnectData *data)
1135 g_socket_address_enumerator_next_async (data->enumerator,
1137 g_socket_client_enumerator_callback,
1142 g_socket_client_tls_handshake_callback (GObject *object,
1143 GAsyncResult *result,
1146 GSocketClientAsyncConnectData *data = user_data;
1148 if (g_tls_connection_handshake_finish (G_TLS_CONNECTION (object),
1152 g_object_unref (data->connection);
1153 data->connection = G_IO_STREAM (object);
1157 g_object_unref (object);
1158 g_object_unref (data->current_socket);
1159 data->current_socket = NULL;
1160 g_object_unref (data->connection);
1161 data->connection = NULL;
1163 enumerator_next_async (data);
1166 g_socket_client_async_connect_complete (data);
1170 g_socket_client_tls_handshake (GSocketClientAsyncConnectData *data)
1174 if (!data->client->priv->tls)
1176 g_socket_client_async_connect_complete (data);
1180 tlsconn = g_tls_client_connection_new (data->connection,
1185 g_tls_client_connection_set_validation_flags (G_TLS_CLIENT_CONNECTION (tlsconn),
1186 data->client->priv->tls_validation_flags);
1187 g_tls_connection_handshake_async (G_TLS_CONNECTION (tlsconn),
1190 g_socket_client_tls_handshake_callback,
1195 g_object_unref (data->current_socket);
1196 data->current_socket = NULL;
1197 g_object_unref (data->connection);
1198 data->connection = NULL;
1200 enumerator_next_async (data);
1205 g_socket_client_proxy_connect_callback (GObject *object,
1206 GAsyncResult *result,
1209 GSocketClientAsyncConnectData *data = user_data;
1211 g_object_unref (data->connection);
1212 data->connection = g_proxy_connect_finish (G_PROXY (object),
1215 if (!data->connection)
1217 g_object_unref (data->current_socket);
1218 data->current_socket = NULL;
1220 enumerator_next_async (data);
1224 g_socket_client_tls_handshake (data);
1228 g_socket_client_proxy_connect (GSocketClientAsyncConnectData *data)
1231 const gchar *protocol;
1233 if (!data->proxy_addr)
1235 g_socket_client_tls_handshake (data);
1239 protocol = g_proxy_address_get_protocol (data->proxy_addr);
1240 proxy = g_proxy_get_default_for_protocol (protocol);
1242 /* The connection should not be anything else then TCP Connection,
1243 * but let's put a safety guard in case
1245 if (!G_IS_TCP_CONNECTION (data->connection))
1247 g_critical ("Trying to proxy over non-TCP connection, this is "
1248 "most likely a bug in GLib IO library.");
1250 g_set_error_literal (&data->last_error,
1251 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
1252 _("Trying to proxy over non-TCP connection is not supported."));
1254 g_object_unref (data->connection);
1255 data->connection = NULL;
1256 g_object_unref (data->current_socket);
1257 data->current_socket = NULL;
1259 enumerator_next_async (data);
1263 g_proxy_connect_async (proxy,
1267 g_socket_client_proxy_connect_callback,
1269 g_object_unref (proxy);
1271 else if (!g_hash_table_lookup_extended (data->client->priv->app_proxies,
1272 protocol, NULL, NULL))
1274 g_clear_error (&data->last_error);
1276 g_set_error (&data->last_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
1277 _("Proxy protocol '%s' is not supported."),
1280 g_object_unref (data->current_socket);
1281 data->current_socket = NULL;
1282 g_object_unref (data->connection);
1283 data->connection = NULL;
1284 g_object_unref (data->current_socket);
1285 data->current_socket = NULL;
1287 enumerator_next_async (data);
1292 g_socket_client_socket_connected (GSocketClientAsyncConnectData *data)
1294 g_socket_set_blocking (data->current_socket, TRUE);
1296 data->connection = (GIOStream *)
1297 g_socket_connection_factory_create_connection (data->current_socket);
1299 g_socket_client_proxy_connect (data);
1303 g_socket_client_socket_callback (GSocket *socket,
1304 GIOCondition condition,
1305 GSocketClientAsyncConnectData *data)
1307 GError *error = NULL;
1309 if (g_cancellable_is_cancelled (data->cancellable))
1311 /* Cancelled, return done with last error being cancelled */
1312 g_clear_error (&data->last_error);
1313 g_object_unref (data->current_socket);
1314 data->current_socket = NULL;
1315 g_cancellable_set_error_if_cancelled (data->cancellable,
1318 g_socket_client_async_connect_complete (data);
1323 /* socket is ready for writing means connect done, did it succeed? */
1324 if (!g_socket_check_connect_result (data->current_socket, &error))
1326 set_last_error (data, error);
1327 g_object_unref (data->current_socket);
1328 data->current_socket = NULL;
1331 enumerator_next_async (data);
1337 g_socket_client_socket_connected (data);
1342 g_socket_client_enumerator_callback (GObject *object,
1343 GAsyncResult *result,
1346 GSocketClientAsyncConnectData *data = user_data;
1347 GSocketAddress *address = NULL;
1349 GError *tmp_error = NULL;
1351 if (g_cancellable_is_cancelled (data->cancellable))
1353 g_clear_error (&data->last_error);
1354 g_cancellable_set_error_if_cancelled (data->cancellable, &data->last_error);
1355 g_socket_client_async_connect_complete (data);
1359 address = g_socket_address_enumerator_next_finish (data->enumerator,
1360 result, &tmp_error);
1362 if (address == NULL)
1365 set_last_error (data, tmp_error);
1366 else if (data->last_error == NULL)
1367 g_set_error_literal (&data->last_error, G_IO_ERROR, G_IO_ERROR_FAILED,
1368 _("Unknown error on connect"));
1370 g_socket_client_async_connect_complete (data);
1374 if (G_IS_PROXY_ADDRESS (address) &&
1375 data->client->priv->enable_proxy)
1376 data->proxy_addr = g_object_ref (G_PROXY_ADDRESS (address));
1378 g_clear_error (&data->last_error);
1380 socket = create_socket (data->client, address, &data->last_error);
1383 g_socket_set_blocking (socket, FALSE);
1384 if (g_socket_connect (socket, address, data->cancellable, &tmp_error))
1386 data->current_socket = socket;
1387 g_socket_client_socket_connected (data);
1389 g_object_unref (address);
1392 else if (g_error_matches (tmp_error, G_IO_ERROR, G_IO_ERROR_PENDING))
1396 data->current_socket = socket;
1397 g_error_free (tmp_error);
1399 source = g_socket_create_source (socket, G_IO_OUT,
1401 g_source_set_callback (source,
1402 (GSourceFunc) g_socket_client_socket_callback,
1404 g_source_attach (source, g_main_context_get_thread_default ());
1405 g_source_unref (source);
1407 g_object_unref (address);
1412 data->last_error = tmp_error;
1413 g_object_unref (socket);
1417 g_object_unref (address);
1418 enumerator_next_async (data);
1422 * g_socket_client_connect_async:
1423 * @client: a #GSocketClient
1424 * @connectable: a #GSocketConnectable specifying the remote address.
1425 * @cancellable: (allow-none): a #GCancellable, or %NULL
1426 * @callback: (scope async): a #GAsyncReadyCallback
1427 * @user_data: (closure): user data for the callback
1429 * This is the asynchronous version of g_socket_client_connect().
1431 * When the operation is finished @callback will be
1432 * called. You can then call g_socket_client_connect_finish() to get
1433 * the result of the operation.
1438 g_socket_client_connect_async (GSocketClient *client,
1439 GSocketConnectable *connectable,
1440 GCancellable *cancellable,
1441 GAsyncReadyCallback callback,
1444 GSocketClientAsyncConnectData *data;
1446 g_return_if_fail (G_IS_SOCKET_CLIENT (client));
1448 data = g_slice_new0 (GSocketClientAsyncConnectData);
1450 data->result = g_simple_async_result_new (G_OBJECT (client),
1451 callback, user_data,
1452 g_socket_client_connect_async);
1453 data->client = client;
1455 data->cancellable = g_object_ref (cancellable);
1457 data->cancellable = NULL;
1458 data->last_error = NULL;
1459 data->connectable = g_object_ref (connectable);
1461 if (can_use_proxy (client))
1462 data->enumerator = g_socket_connectable_proxy_enumerate (connectable);
1464 data->enumerator = g_socket_connectable_enumerate (connectable);
1466 enumerator_next_async (data);
1470 * g_socket_client_connect_to_host_async:
1471 * @client: a #GSocketClient
1472 * @host_and_port: the name and optionally the port of the host to connect to
1473 * @default_port: the default port to connect to
1474 * @cancellable: (allow-none): a #GCancellable, or %NULL
1475 * @callback: (scope async): a #GAsyncReadyCallback
1476 * @user_data: (closure): user data for the callback
1478 * This is the asynchronous version of g_socket_client_connect_to_host().
1480 * When the operation is finished @callback will be
1481 * called. You can then call g_socket_client_connect_to_host_finish() to get
1482 * the result of the operation.
1487 g_socket_client_connect_to_host_async (GSocketClient *client,
1488 const gchar *host_and_port,
1489 guint16 default_port,
1490 GCancellable *cancellable,
1491 GAsyncReadyCallback callback,
1494 GSocketConnectable *connectable;
1498 connectable = g_network_address_parse (host_and_port, default_port,
1500 if (connectable == NULL)
1502 g_simple_async_report_take_gerror_in_idle (G_OBJECT (client),
1503 callback, user_data, error);
1507 g_socket_client_connect_async (client,
1508 connectable, cancellable,
1509 callback, user_data);
1510 g_object_unref (connectable);
1515 * g_socket_client_connect_to_service_async:
1516 * @client: a #GSocketClient
1517 * @domain: a domain name
1518 * @service: the name of the service to connect to
1519 * @cancellable: (allow-none): a #GCancellable, or %NULL
1520 * @callback: (scope async): a #GAsyncReadyCallback
1521 * @user_data: (closure): user data for the callback
1523 * This is the asynchronous version of
1524 * g_socket_client_connect_to_service().
1529 g_socket_client_connect_to_service_async (GSocketClient *client,
1530 const gchar *domain,
1531 const gchar *service,
1532 GCancellable *cancellable,
1533 GAsyncReadyCallback callback,
1536 GSocketConnectable *connectable;
1538 connectable = g_network_service_new (service, "tcp", domain);
1539 g_socket_client_connect_async (client,
1540 connectable, cancellable,
1541 callback, user_data);
1542 g_object_unref (connectable);
1546 * g_socket_client_connect_to_uri_async:
1547 * @client: a #GSocketClient
1548 * @uri: a network uri
1549 * @default_port: the default port to connect to
1550 * @cancellable: (allow-none): a #GCancellable, or %NULL
1551 * @callback: (scope async): a #GAsyncReadyCallback
1552 * @user_data: (closure): user data for the callback
1554 * This is the asynchronous version of g_socket_client_connect_to_uri().
1556 * When the operation is finished @callback will be
1557 * called. You can then call g_socket_client_connect_to_uri_finish() to get
1558 * the result of the operation.
1563 g_socket_client_connect_to_uri_async (GSocketClient *client,
1565 guint16 default_port,
1566 GCancellable *cancellable,
1567 GAsyncReadyCallback callback,
1570 GSocketConnectable *connectable;
1574 connectable = g_network_address_parse_uri (uri, default_port, &error);
1575 if (connectable == NULL)
1577 g_simple_async_report_take_gerror_in_idle (G_OBJECT (client),
1578 callback, user_data, error);
1582 g_socket_client_connect_async (client,
1583 connectable, cancellable,
1584 callback, user_data);
1585 g_object_unref (connectable);
1591 * g_socket_client_connect_finish:
1592 * @client: a #GSocketClient.
1593 * @result: a #GAsyncResult.
1594 * @error: a #GError location to store the error occurring, or %NULL to
1597 * Finishes an async connect operation. See g_socket_client_connect_async()
1599 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1604 g_socket_client_connect_finish (GSocketClient *client,
1605 GAsyncResult *result,
1608 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1610 if (g_simple_async_result_propagate_error (simple, error))
1613 return g_object_ref (g_simple_async_result_get_op_res_gpointer (simple));
1617 * g_socket_client_connect_to_host_finish:
1618 * @client: a #GSocketClient.
1619 * @result: a #GAsyncResult.
1620 * @error: a #GError location to store the error occurring, or %NULL to
1623 * Finishes an async connect operation. See g_socket_client_connect_to_host_async()
1625 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1630 g_socket_client_connect_to_host_finish (GSocketClient *client,
1631 GAsyncResult *result,
1634 return g_socket_client_connect_finish (client, result, error);
1638 * g_socket_client_connect_to_service_finish:
1639 * @client: a #GSocketClient.
1640 * @result: a #GAsyncResult.
1641 * @error: a #GError location to store the error occurring, or %NULL to
1644 * Finishes an async connect operation. See g_socket_client_connect_to_service_async()
1646 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1651 g_socket_client_connect_to_service_finish (GSocketClient *client,
1652 GAsyncResult *result,
1655 return g_socket_client_connect_finish (client, result, error);
1659 * g_socket_client_connect_to_uri_finish:
1660 * @client: a #GSocketClient.
1661 * @result: a #GAsyncResult.
1662 * @error: a #GError location to store the error occurring, or %NULL to
1665 * Finishes an async connect operation. See g_socket_client_connect_to_uri_async()
1667 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1672 g_socket_client_connect_to_uri_finish (GSocketClient *client,
1673 GAsyncResult *result,
1676 return g_socket_client_connect_finish (client, result, error);
1680 * g_socket_client_add_application_proxy:
1681 * @client: a #GSocketClient
1682 * @protocol: The proxy protocol
1684 * Enable proxy protocols to be handled by the application. When the
1685 * indicated proxy protocol is returned by the #GProxyResolver,
1686 * #GSocketClient will consider this protocol as supported but will
1687 * not try to find a #GProxy instance to handle handshaking. The
1688 * application must check for this case by calling
1689 * g_socket_connection_get_remote_address() on the returned
1690 * #GSocketConnection, and seeing if it's a #GProxyAddress of the
1691 * appropriate type, to determine whether or not it needs to handle
1692 * the proxy handshaking itself.
1694 * This should be used for proxy protocols that are dialects of
1695 * another protocol such as HTTP proxy. It also allows cohabitation of
1696 * proxy protocols that are reused between protocols. A good example
1697 * is HTTP. It can be used to proxy HTTP, FTP and Gopher and can also
1698 * be use as generic socket proxy through the HTTP CONNECT method.
1701 g_socket_client_add_application_proxy (GSocketClient *client,
1702 const gchar *protocol)
1704 g_hash_table_insert (client->priv->app_proxies, g_strdup (protocol), NULL);