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, 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 #GSocketAddres or %NULL. don't 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 the 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: 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 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)
867 GTlsClientConnection *tlsconn;
869 tlsconn = g_tls_client_connection_new (connection, connectable, &last_error);
870 g_object_unref (connection);
871 connection = (GIOStream *)tlsconn;
875 g_tls_client_connection_set_validation_flags (tlsconn, client->priv->tls_validation_flags);
876 if (!g_tls_connection_handshake (G_TLS_CONNECTION (tlsconn),
877 cancellable, &last_error))
879 g_object_unref (tlsconn);
885 if (connection && !G_IS_SOCKET_CONNECTION (connection))
887 GSocketConnection *wrapper_connection;
889 wrapper_connection = g_tcp_wrapper_connection_new (connection, socket);
890 g_object_unref (connection);
891 connection = (GIOStream *)wrapper_connection;
894 g_object_unref (socket);
895 g_object_unref (address);
897 g_object_unref (enumerator);
899 return G_SOCKET_CONNECTION (connection);
903 * g_socket_client_connect_to_host:
904 * @client: a #GSocketClient
905 * @host_and_port: the name and optionally port of the host to connect to
906 * @default_port: the default port to connect to
907 * @cancellable: a #GCancellable, or %NULL
908 * @error: a pointer to a #GError, or %NULL
910 * This is a helper function for g_socket_client_connect().
912 * Attempts to create a TCP connection to the named host.
914 * @host_and_port may be in any of a number of recognised formats; an IPv6
915 * address, an IPv4 address, or a domain name (in which case a DNS
916 * lookup is performed). Quoting with [] is supported for all address
917 * types. A port override may be specified in the usual way with a
918 * colon. Ports may be given as decimal numbers or symbolic names (in
919 * which case an /etc/services lookup is performed).
921 * If no port override is given in @host_and_port then @default_port will be
922 * used as the port number to connect to.
924 * In general, @host_and_port is expected to be provided by the user (allowing
925 * them to give the hostname, and a port overide if necessary) and
926 * @default_port is expected to be provided by the application.
928 * In the case that an IP address is given, a single connection
929 * attempt is made. In the case that a name is given, multiple
930 * connection attempts may be made, in turn and according to the
931 * number of address records in DNS, until a connection succeeds.
933 * Upon a successful connection, a new #GSocketConnection is constructed
934 * and returned. The caller owns this new object and must drop their
935 * reference to it when finished with it.
937 * In the event of any failure (DNS error, service not found, no hosts
938 * connectable) %NULL is returned and @error (if non-%NULL) is set
941 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
946 g_socket_client_connect_to_host (GSocketClient *client,
947 const gchar *host_and_port,
948 guint16 default_port,
949 GCancellable *cancellable,
952 GSocketConnectable *connectable;
953 GSocketConnection *connection;
955 connectable = g_network_address_parse (host_and_port, default_port, error);
956 if (connectable == NULL)
959 connection = g_socket_client_connect (client, connectable,
961 g_object_unref (connectable);
967 * g_socket_client_connect_to_service:
968 * @client: a #GSocketConnection
969 * @domain: a domain name
970 * @service: the name of the service to connect to
971 * @cancellable: a #GCancellable, or %NULL
972 * @error: a pointer to a #GError, or %NULL
973 * @returns: (transfer full): a #GSocketConnection if successful, or %NULL on error
975 * Attempts to create a TCP connection to a service.
977 * This call looks up the SRV record for @service at @domain for the
978 * "tcp" protocol. It then attempts to connect, in turn, to each of
979 * the hosts providing the service until either a connection succeeds
980 * or there are no hosts remaining.
982 * Upon a successful connection, a new #GSocketConnection is constructed
983 * and returned. The caller owns this new object and must drop their
984 * reference to it when finished with it.
986 * In the event of any failure (DNS error, service not found, no hosts
987 * connectable) %NULL is returned and @error (if non-%NULL) is set
991 g_socket_client_connect_to_service (GSocketClient *client,
993 const gchar *service,
994 GCancellable *cancellable,
997 GSocketConnectable *connectable;
998 GSocketConnection *connection;
1000 connectable = g_network_service_new (service, "tcp", domain);
1001 connection = g_socket_client_connect (client, connectable,
1002 cancellable, error);
1003 g_object_unref (connectable);
1009 * g_socket_client_connect_to_uri:
1010 * @client: a #GSocketClient
1011 * @uri: A network URI
1012 * @default_port: the default port to connect to
1013 * @cancellable: a #GCancellable, or %NULL
1014 * @error: a pointer to a #GError, or %NULL
1016 * This is a helper function for g_socket_client_connect().
1018 * Attempts to create a TCP connection with a network URI.
1020 * @uri may be any valid URI containing an "authority" (hostname/port)
1021 * component. If a port is not specified in the URI, @default_port
1022 * will be used. TLS will be negotiated if #GSocketClient:tls is %TRUE.
1023 * (#GSocketClient does not know to automatically assume TLS for
1024 * certain URI schemes.)
1026 * Using this rather than g_socket_client_connect() or
1027 * g_socket_client_connect_to_host() allows #GSocketClient to
1028 * determine when to use application-specific proxy protocols.
1030 * Upon a successful connection, a new #GSocketConnection is constructed
1031 * and returned. The caller owns this new object and must drop their
1032 * reference to it when finished with it.
1034 * In the event of any failure (DNS error, service not found, no hosts
1035 * connectable) %NULL is returned and @error (if non-%NULL) is set
1038 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1043 g_socket_client_connect_to_uri (GSocketClient *client,
1045 guint16 default_port,
1046 GCancellable *cancellable,
1049 GSocketConnectable *connectable;
1050 GSocketConnection *connection;
1052 connectable = g_network_address_parse_uri (uri, default_port, error);
1053 if (connectable == NULL)
1056 connection = g_socket_client_connect (client, connectable,
1057 cancellable, error);
1058 g_object_unref (connectable);
1065 GSimpleAsyncResult *result;
1066 GCancellable *cancellable;
1067 GSocketClient *client;
1069 GSocketConnectable *connectable;
1070 GSocketAddressEnumerator *enumerator;
1071 GProxyAddress *proxy_addr;
1072 GSocket *current_socket;
1073 GIOStream *connection;
1076 } GSocketClientAsyncConnectData;
1079 g_socket_client_async_connect_complete (GSocketClientAsyncConnectData *data)
1081 if (data->last_error)
1083 g_simple_async_result_take_error (data->result, data->last_error);
1087 g_assert (data->connection);
1089 if (!G_IS_SOCKET_CONNECTION (data->connection))
1091 GSocketConnection *wrapper_connection;
1093 wrapper_connection = g_tcp_wrapper_connection_new (data->connection,
1094 data->current_socket);
1095 g_object_unref (data->connection);
1096 data->connection = (GIOStream *)wrapper_connection;
1099 g_simple_async_result_set_op_res_gpointer (data->result,
1104 g_simple_async_result_complete (data->result);
1105 g_object_unref (data->result);
1106 g_object_unref (data->connectable);
1107 g_object_unref (data->enumerator);
1108 if (data->cancellable)
1109 g_object_unref (data->cancellable);
1110 if (data->current_socket)
1111 g_object_unref (data->current_socket);
1112 if (data->proxy_addr)
1113 g_object_unref (data->proxy_addr);
1114 g_slice_free (GSocketClientAsyncConnectData, data);
1119 g_socket_client_enumerator_callback (GObject *object,
1120 GAsyncResult *result,
1121 gpointer user_data);
1124 set_last_error (GSocketClientAsyncConnectData *data,
1127 g_clear_error (&data->last_error);
1128 data->last_error = error;
1132 enumerator_next_async (GSocketClientAsyncConnectData *data)
1134 g_socket_address_enumerator_next_async (data->enumerator,
1136 g_socket_client_enumerator_callback,
1141 g_socket_client_tls_handshake_callback (GObject *object,
1142 GAsyncResult *result,
1145 GSocketClientAsyncConnectData *data = user_data;
1147 if (g_tls_connection_handshake_finish (G_TLS_CONNECTION (object),
1151 g_object_unref (data->connection);
1152 data->connection = G_IO_STREAM (object);
1156 g_object_unref (object);
1157 g_object_unref (data->current_socket);
1158 data->current_socket = NULL;
1159 g_object_unref (data->connection);
1160 data->connection = NULL;
1162 enumerator_next_async (data);
1165 g_socket_client_async_connect_complete (data);
1169 g_socket_client_tls_handshake (GSocketClientAsyncConnectData *data)
1171 GTlsClientConnection *tlsconn;
1173 if (!data->client->priv->tls)
1175 g_socket_client_async_connect_complete (data);
1179 tlsconn = g_tls_client_connection_new (data->connection,
1184 g_tls_client_connection_set_validation_flags (tlsconn, data->client->priv->tls_validation_flags);
1185 g_tls_connection_handshake_async (G_TLS_CONNECTION (tlsconn),
1188 g_socket_client_tls_handshake_callback,
1193 g_object_unref (data->current_socket);
1194 data->current_socket = NULL;
1195 g_object_unref (data->connection);
1196 data->connection = NULL;
1198 enumerator_next_async (data);
1203 g_socket_client_proxy_connect_callback (GObject *object,
1204 GAsyncResult *result,
1207 GSocketClientAsyncConnectData *data = user_data;
1209 g_object_unref (data->connection);
1210 data->connection = g_proxy_connect_finish (G_PROXY (object),
1213 if (!data->connection)
1215 g_object_unref (data->current_socket);
1216 data->current_socket = NULL;
1218 enumerator_next_async (data);
1222 g_socket_client_tls_handshake (data);
1226 g_socket_client_proxy_connect (GSocketClientAsyncConnectData *data)
1229 const gchar *protocol;
1231 if (!data->proxy_addr)
1233 g_socket_client_tls_handshake (data);
1237 protocol = g_proxy_address_get_protocol (data->proxy_addr);
1238 proxy = g_proxy_get_default_for_protocol (protocol);
1240 /* The connection should not be anything else then TCP Connection,
1241 * but let's put a safety guard in case
1243 if (!G_IS_TCP_CONNECTION (data->connection))
1245 g_critical ("Trying to proxy over non-TCP connection, this is "
1246 "most likely a bug in GLib IO library.");
1248 g_set_error_literal (&data->last_error,
1249 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
1250 _("Trying to proxy over non-TCP connection is not supported."));
1252 g_object_unref (data->connection);
1253 data->connection = NULL;
1254 g_object_unref (data->current_socket);
1255 data->current_socket = NULL;
1257 enumerator_next_async (data);
1261 g_proxy_connect_async (proxy,
1265 g_socket_client_proxy_connect_callback,
1267 g_object_unref (proxy);
1269 else if (!g_hash_table_lookup_extended (data->client->priv->app_proxies,
1270 protocol, NULL, NULL))
1272 g_clear_error (&data->last_error);
1274 g_set_error (&data->last_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
1275 _("Proxy protocol '%s' is not supported."),
1278 g_object_unref (data->current_socket);
1279 data->current_socket = NULL;
1280 g_object_unref (data->connection);
1281 data->connection = NULL;
1282 g_object_unref (data->current_socket);
1283 data->current_socket = NULL;
1285 enumerator_next_async (data);
1290 g_socket_client_socket_connected (GSocketClientAsyncConnectData *data)
1292 g_socket_set_blocking (data->current_socket, TRUE);
1294 data->connection = (GIOStream *)
1295 g_socket_connection_factory_create_connection (data->current_socket);
1297 g_socket_client_proxy_connect (data);
1301 g_socket_client_socket_callback (GSocket *socket,
1302 GIOCondition condition,
1303 GSocketClientAsyncConnectData *data)
1305 GError *error = NULL;
1307 if (g_cancellable_is_cancelled (data->cancellable))
1309 /* Cancelled, return done with last error being cancelled */
1310 g_clear_error (&data->last_error);
1311 g_object_unref (data->current_socket);
1312 data->current_socket = NULL;
1313 g_cancellable_set_error_if_cancelled (data->cancellable,
1316 g_socket_client_async_connect_complete (data);
1321 /* socket is ready for writing means connect done, did it succeed? */
1322 if (!g_socket_check_connect_result (data->current_socket, &error))
1324 set_last_error (data, error);
1325 g_object_unref (data->current_socket);
1326 data->current_socket = NULL;
1329 enumerator_next_async (data);
1335 g_socket_client_socket_connected (data);
1340 g_socket_client_enumerator_callback (GObject *object,
1341 GAsyncResult *result,
1344 GSocketClientAsyncConnectData *data = user_data;
1345 GSocketAddress *address = NULL;
1347 GError *tmp_error = NULL;
1349 if (g_cancellable_is_cancelled (data->cancellable))
1351 g_clear_error (&data->last_error);
1352 g_cancellable_set_error_if_cancelled (data->cancellable, &data->last_error);
1353 g_socket_client_async_connect_complete (data);
1357 address = g_socket_address_enumerator_next_finish (data->enumerator,
1358 result, &tmp_error);
1360 if (address == NULL)
1363 set_last_error (data, tmp_error);
1364 else if (data->last_error == NULL)
1365 g_set_error_literal (&data->last_error, G_IO_ERROR, G_IO_ERROR_FAILED,
1366 _("Unknown error on connect"));
1368 g_socket_client_async_connect_complete (data);
1372 if (G_IS_PROXY_ADDRESS (address) &&
1373 data->client->priv->enable_proxy)
1374 data->proxy_addr = g_object_ref (G_PROXY_ADDRESS (address));
1376 g_clear_error (&data->last_error);
1378 socket = create_socket (data->client, address, &data->last_error);
1381 g_socket_set_blocking (socket, FALSE);
1382 if (g_socket_connect (socket, address, data->cancellable, &tmp_error))
1384 data->current_socket = socket;
1385 g_socket_client_socket_connected (data);
1387 g_object_unref (address);
1390 else if (g_error_matches (tmp_error, G_IO_ERROR, G_IO_ERROR_PENDING))
1394 data->current_socket = socket;
1395 g_error_free (tmp_error);
1397 source = g_socket_create_source (socket, G_IO_OUT,
1399 g_source_set_callback (source,
1400 (GSourceFunc) g_socket_client_socket_callback,
1402 g_source_attach (source, g_main_context_get_thread_default ());
1403 g_source_unref (source);
1405 g_object_unref (address);
1410 data->last_error = tmp_error;
1411 g_object_unref (socket);
1415 g_object_unref (address);
1416 enumerator_next_async (data);
1420 * g_socket_client_connect_async:
1421 * @client: a #GTcpClient
1422 * @connectable: a #GSocketConnectable specifying the remote address.
1423 * @cancellable: a #GCancellable, or %NULL
1424 * @callback: a #GAsyncReadyCallback
1425 * @user_data: user data for the callback
1427 * This is the asynchronous version of g_socket_client_connect().
1429 * When the operation is finished @callback will be
1430 * called. You can then call g_socket_client_connect_finish() to get
1431 * the result of the operation.
1436 g_socket_client_connect_async (GSocketClient *client,
1437 GSocketConnectable *connectable,
1438 GCancellable *cancellable,
1439 GAsyncReadyCallback callback,
1442 GSocketClientAsyncConnectData *data;
1444 g_return_if_fail (G_IS_SOCKET_CLIENT (client));
1446 data = g_slice_new0 (GSocketClientAsyncConnectData);
1448 data->result = g_simple_async_result_new (G_OBJECT (client),
1449 callback, user_data,
1450 g_socket_client_connect_async);
1451 data->client = client;
1453 data->cancellable = g_object_ref (cancellable);
1455 data->cancellable = NULL;
1456 data->last_error = NULL;
1457 data->connectable = g_object_ref (connectable);
1459 if (can_use_proxy (client))
1460 data->enumerator = g_socket_connectable_proxy_enumerate (connectable);
1462 data->enumerator = g_socket_connectable_enumerate (connectable);
1464 enumerator_next_async (data);
1468 * g_socket_client_connect_to_host_async:
1469 * @client: a #GTcpClient
1470 * @host_and_port: the name and optionally the port of the host to connect to
1471 * @default_port: the default port to connect to
1472 * @cancellable: a #GCancellable, or %NULL
1473 * @callback: a #GAsyncReadyCallback
1474 * @user_data: user data for the callback
1476 * This is the asynchronous version of g_socket_client_connect_to_host().
1478 * When the operation is finished @callback will be
1479 * called. You can then call g_socket_client_connect_to_host_finish() to get
1480 * the result of the operation.
1485 g_socket_client_connect_to_host_async (GSocketClient *client,
1486 const gchar *host_and_port,
1487 guint16 default_port,
1488 GCancellable *cancellable,
1489 GAsyncReadyCallback callback,
1492 GSocketConnectable *connectable;
1496 connectable = g_network_address_parse (host_and_port, default_port,
1498 if (connectable == NULL)
1500 g_simple_async_report_take_gerror_in_idle (G_OBJECT (client),
1501 callback, user_data, error);
1505 g_socket_client_connect_async (client,
1506 connectable, cancellable,
1507 callback, user_data);
1508 g_object_unref (connectable);
1513 * g_socket_client_connect_to_service_async:
1514 * @client: a #GSocketClient
1515 * @domain: a domain name
1516 * @service: the name of the service to connect to
1517 * @cancellable: a #GCancellable, or %NULL
1518 * @callback: a #GAsyncReadyCallback
1519 * @user_data: user data for the callback
1521 * This is the asynchronous version of
1522 * g_socket_client_connect_to_service().
1527 g_socket_client_connect_to_service_async (GSocketClient *client,
1528 const gchar *domain,
1529 const gchar *service,
1530 GCancellable *cancellable,
1531 GAsyncReadyCallback callback,
1534 GSocketConnectable *connectable;
1536 connectable = g_network_service_new (service, "tcp", domain);
1537 g_socket_client_connect_async (client,
1538 connectable, cancellable,
1539 callback, user_data);
1540 g_object_unref (connectable);
1544 * g_socket_client_connect_to_uri_async:
1545 * @client: a #GSocketClient
1546 * @uri: a network uri
1547 * @default_port: the default port to connect to
1548 * @cancellable: a #GCancellable, or %NULL
1549 * @callback: a #GAsyncReadyCallback
1550 * @user_data: user data for the callback
1552 * This is the asynchronous version of g_socket_client_connect_to_uri().
1554 * When the operation is finished @callback will be
1555 * called. You can then call g_socket_client_connect_to_uri_finish() to get
1556 * the result of the operation.
1561 g_socket_client_connect_to_uri_async (GSocketClient *client,
1563 guint16 default_port,
1564 GCancellable *cancellable,
1565 GAsyncReadyCallback callback,
1568 GSocketConnectable *connectable;
1572 connectable = g_network_address_parse_uri (uri, default_port, &error);
1573 if (connectable == NULL)
1575 g_simple_async_report_take_gerror_in_idle (G_OBJECT (client),
1576 callback, user_data, error);
1580 g_socket_client_connect_async (client,
1581 connectable, cancellable,
1582 callback, user_data);
1583 g_object_unref (connectable);
1589 * g_socket_client_connect_finish:
1590 * @client: a #GSocketClient.
1591 * @result: a #GAsyncResult.
1592 * @error: a #GError location to store the error occuring, or %NULL to
1595 * Finishes an async connect operation. See g_socket_client_connect_async()
1597 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1602 g_socket_client_connect_finish (GSocketClient *client,
1603 GAsyncResult *result,
1606 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1608 if (g_simple_async_result_propagate_error (simple, error))
1611 return g_object_ref (g_simple_async_result_get_op_res_gpointer (simple));
1615 * g_socket_client_connect_to_host_finish:
1616 * @client: a #GSocketClient.
1617 * @result: a #GAsyncResult.
1618 * @error: a #GError location to store the error occuring, or %NULL to
1621 * Finishes an async connect operation. See g_socket_client_connect_to_host_async()
1623 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1628 g_socket_client_connect_to_host_finish (GSocketClient *client,
1629 GAsyncResult *result,
1632 return g_socket_client_connect_finish (client, result, error);
1636 * g_socket_client_connect_to_service_finish:
1637 * @client: a #GSocketClient.
1638 * @result: a #GAsyncResult.
1639 * @error: a #GError location to store the error occuring, or %NULL to
1642 * Finishes an async connect operation. See g_socket_client_connect_to_service_async()
1644 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1649 g_socket_client_connect_to_service_finish (GSocketClient *client,
1650 GAsyncResult *result,
1653 return g_socket_client_connect_finish (client, result, error);
1657 * g_socket_client_connect_to_uri_finish:
1658 * @client: a #GSocketClient.
1659 * @result: a #GAsyncResult.
1660 * @error: a #GError location to store the error occuring, or %NULL to
1663 * Finishes an async connect operation. See g_socket_client_connect_to_uri_async()
1665 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1670 g_socket_client_connect_to_uri_finish (GSocketClient *client,
1671 GAsyncResult *result,
1674 return g_socket_client_connect_finish (client, result, error);
1678 * g_socket_client_add_application_proxy:
1679 * @client: a #GSocketClient
1680 * @protocol: The proxy protocol
1682 * Enable proxy protocols to be handled by the application. When the
1683 * indicated proxy protocol is returned by the #GProxyResolver,
1684 * #GSocketClient will consider this protocol as supported but will
1685 * not try find a #GProxy instance to handle handshaking. The
1686 * application must check for this case by calling
1687 * g_socket_connection_get_remote_address() on the returned
1688 * #GSocketConnection, and seeing if it's a #GProxyAddress of the
1689 * appropriate type, to determine whether or not it needs to handle
1690 * the proxy handshaking itself.
1692 * This should be used for proxy protocols that are dialects of
1693 * another protocol such as HTTP proxy. It also allows cohabitation of
1694 * proxy protocols that are reused between protocols. A good example
1695 * is HTTP. It can be used to proxy HTTP, FTP and Gopher and can also
1696 * be use as generic socket proxy through the HTTP CONNECT method.
1699 g_socket_client_add_application_proxy (GSocketClient *client,
1700 const gchar *protocol)
1702 g_hash_table_insert (client->priv->app_proxies, g_strdup (protocol), NULL);