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>
49 #include <gio/ginetaddress.h>
54 * SECTION:gsocketclient
55 * @short_description: Helper for connecting to a network service
57 * @see_also: #GSocketConnection, #GSocketListener
59 * #GSocketClient is a high-level utility class for connecting to a
60 * network host using a connection oriented socket type.
62 * You create a #GSocketClient object, set any options you want, and then
63 * call a sync or async connect operation, which returns a #GSocketConnection
64 * subclass on success.
66 * The type of the #GSocketConnection object returned depends on the type of
67 * the underlying socket that is in use. For instance, for a TCP/IP connection
68 * it will be a #GTcpConnection.
74 G_DEFINE_TYPE (GSocketClient, g_socket_client, G_TYPE_OBJECT);
86 PROP_TLS_VALIDATION_FLAGS
89 struct _GSocketClientPrivate
93 GSocketProtocol protocol;
94 GSocketAddress *local_address;
96 gboolean enable_proxy;
97 GHashTable *app_proxies;
99 GTlsCertificateFlags tls_validation_flags;
103 create_socket (GSocketClient *client,
104 GSocketAddress *dest_address,
107 GSocketFamily family;
110 family = client->priv->family;
111 if (family == G_SOCKET_FAMILY_INVALID &&
112 client->priv->local_address != NULL)
113 family = g_socket_address_get_family (client->priv->local_address);
114 if (family == G_SOCKET_FAMILY_INVALID)
115 family = g_socket_address_get_family (dest_address);
117 socket = g_socket_new (family,
119 client->priv->protocol,
124 if (client->priv->local_address)
126 if (!g_socket_bind (socket,
127 client->priv->local_address,
131 g_object_unref (socket);
136 if (client->priv->timeout)
137 g_socket_set_timeout (socket, client->priv->timeout);
143 can_use_proxy (GSocketClient *client)
145 GSocketClientPrivate *priv = client->priv;
147 return priv->enable_proxy
148 && priv->type == G_SOCKET_TYPE_STREAM;
152 clarify_connect_error (GError *error,
153 GSocketConnectable *connectable,
154 GSocketAddress *address)
157 char *tmp_name = NULL;
159 if (G_IS_PROXY_ADDRESS (address))
161 name = tmp_name = g_inet_address_to_string (g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address)));
163 g_prefix_error (&error, _("Could not connect to proxy server %s: "), name);
167 if (G_IS_NETWORK_ADDRESS (connectable))
168 name = g_network_address_get_hostname (G_NETWORK_ADDRESS (connectable));
169 else if (G_IS_NETWORK_SERVICE (connectable))
170 name = g_network_service_get_domain (G_NETWORK_SERVICE (connectable));
171 else if (G_IS_INET_SOCKET_ADDRESS (connectable))
172 name = tmp_name = g_inet_address_to_string (g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (connectable)));
177 g_prefix_error (&error, _("Could not connect to %s: "), name);
179 g_prefix_error (&error, _("Could not connect: "));
186 g_socket_client_init (GSocketClient *client)
188 client->priv = G_TYPE_INSTANCE_GET_PRIVATE (client,
189 G_TYPE_SOCKET_CLIENT,
190 GSocketClientPrivate);
191 client->priv->type = G_SOCKET_TYPE_STREAM;
192 client->priv->app_proxies = g_hash_table_new_full (g_str_hash,
199 * g_socket_client_new:
201 * Creates a new #GSocketClient with the default options.
203 * Returns: a #GSocketClient.
204 * Free the returned object with g_object_unref().
209 g_socket_client_new (void)
211 return g_object_new (G_TYPE_SOCKET_CLIENT, NULL);
215 g_socket_client_finalize (GObject *object)
217 GSocketClient *client = G_SOCKET_CLIENT (object);
219 if (client->priv->local_address)
220 g_object_unref (client->priv->local_address);
222 if (G_OBJECT_CLASS (g_socket_client_parent_class)->finalize)
223 (*G_OBJECT_CLASS (g_socket_client_parent_class)->finalize) (object);
225 g_hash_table_unref (client->priv->app_proxies);
229 g_socket_client_get_property (GObject *object,
234 GSocketClient *client = G_SOCKET_CLIENT (object);
239 g_value_set_enum (value, client->priv->family);
243 g_value_set_enum (value, client->priv->type);
247 g_value_set_enum (value, client->priv->protocol);
250 case PROP_LOCAL_ADDRESS:
251 g_value_set_object (value, client->priv->local_address);
255 g_value_set_uint (value, client->priv->timeout);
258 case PROP_ENABLE_PROXY:
259 g_value_set_boolean (value, client->priv->enable_proxy);
263 g_value_set_boolean (value, g_socket_client_get_tls (client));
266 case PROP_TLS_VALIDATION_FLAGS:
267 g_value_set_flags (value, g_socket_client_get_tls_validation_flags (client));
271 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
276 g_socket_client_set_property (GObject *object,
281 GSocketClient *client = G_SOCKET_CLIENT (object);
286 g_socket_client_set_family (client, g_value_get_enum (value));
290 g_socket_client_set_socket_type (client, g_value_get_enum (value));
294 g_socket_client_set_protocol (client, g_value_get_enum (value));
297 case PROP_LOCAL_ADDRESS:
298 g_socket_client_set_local_address (client, g_value_get_object (value));
302 g_socket_client_set_timeout (client, g_value_get_uint (value));
305 case PROP_ENABLE_PROXY:
306 g_socket_client_set_enable_proxy (client, g_value_get_boolean (value));
310 g_socket_client_set_tls (client, g_value_get_boolean (value));
313 case PROP_TLS_VALIDATION_FLAGS:
314 g_socket_client_set_tls_validation_flags (client, g_value_get_flags (value));
318 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
323 * g_socket_client_get_family:
324 * @client: a #GSocketClient.
326 * Gets the socket family of the socket client.
328 * See g_socket_client_set_family() for details.
330 * Returns: a #GSocketFamily
335 g_socket_client_get_family (GSocketClient *client)
337 return client->priv->family;
341 * g_socket_client_set_family:
342 * @client: a #GSocketClient.
343 * @family: a #GSocketFamily
345 * Sets the socket family of the socket client.
346 * If this is set to something other than %G_SOCKET_FAMILY_INVALID
347 * then the sockets created by this object will be of the specified
350 * This might be useful for instance if you want to force the local
351 * connection to be an ipv4 socket, even though the address might
352 * be an ipv6 mapped to ipv4 address.
357 g_socket_client_set_family (GSocketClient *client,
358 GSocketFamily family)
360 if (client->priv->family == family)
363 client->priv->family = family;
364 g_object_notify (G_OBJECT (client), "family");
368 * g_socket_client_get_socket_type:
369 * @client: a #GSocketClient.
371 * Gets the socket type of the socket client.
373 * See g_socket_client_set_socket_type() for details.
375 * Returns: a #GSocketFamily
380 g_socket_client_get_socket_type (GSocketClient *client)
382 return client->priv->type;
386 * g_socket_client_set_socket_type:
387 * @client: a #GSocketClient.
388 * @type: a #GSocketType
390 * Sets the socket type of the socket client.
391 * The sockets created by this object will be of the specified
394 * It doesn't make sense to specify a type of %G_SOCKET_TYPE_DATAGRAM,
395 * as GSocketClient is used for connection oriented services.
400 g_socket_client_set_socket_type (GSocketClient *client,
403 if (client->priv->type == type)
406 client->priv->type = type;
407 g_object_notify (G_OBJECT (client), "type");
411 * g_socket_client_get_protocol:
412 * @client: a #GSocketClient
414 * Gets the protocol name type of the socket client.
416 * See g_socket_client_set_protocol() for details.
418 * Returns: a #GSocketProtocol
423 g_socket_client_get_protocol (GSocketClient *client)
425 return client->priv->protocol;
429 * g_socket_client_set_protocol:
430 * @client: a #GSocketClient.
431 * @protocol: a #GSocketProtocol
433 * Sets the protocol of the socket client.
434 * The sockets created by this object will use of the specified
437 * If @protocol is %0 that means to use the default
438 * protocol for the socket family and type.
443 g_socket_client_set_protocol (GSocketClient *client,
444 GSocketProtocol protocol)
446 if (client->priv->protocol == protocol)
449 client->priv->protocol = protocol;
450 g_object_notify (G_OBJECT (client), "protocol");
454 * g_socket_client_get_local_address:
455 * @client: a #GSocketClient.
457 * Gets the local address of the socket client.
459 * See g_socket_client_set_local_address() for details.
461 * Returns: (transfer none): a #GSocketAddress or %NULL. Do not free.
466 g_socket_client_get_local_address (GSocketClient *client)
468 return client->priv->local_address;
472 * g_socket_client_set_local_address:
473 * @client: a #GSocketClient.
474 * @address: a #GSocketAddress, or %NULL
476 * Sets the local address of the socket client.
477 * The sockets created by this object will bound to the
478 * specified address (if not %NULL) before connecting.
480 * This is useful if you want to ensure that the local
481 * side of the connection is on a specific port, or on
482 * a specific interface.
487 g_socket_client_set_local_address (GSocketClient *client,
488 GSocketAddress *address)
491 g_object_ref (address);
493 if (client->priv->local_address)
495 g_object_unref (client->priv->local_address);
497 client->priv->local_address = address;
498 g_object_notify (G_OBJECT (client), "local-address");
502 * g_socket_client_get_timeout:
503 * @client: a #GSocketClient
505 * Gets the I/O timeout time for sockets created by @client.
507 * See g_socket_client_set_timeout() for details.
509 * Returns: the timeout in seconds
514 g_socket_client_get_timeout (GSocketClient *client)
516 return client->priv->timeout;
521 * g_socket_client_set_timeout:
522 * @client: a #GSocketClient.
523 * @timeout: the timeout
525 * Sets the I/O timeout for sockets created by @client. @timeout is a
526 * time in seconds, or 0 for no timeout (the default).
528 * The timeout value affects the initial connection attempt as well,
529 * so setting this may cause calls to g_socket_client_connect(), etc,
530 * to fail with %G_IO_ERROR_TIMED_OUT.
535 g_socket_client_set_timeout (GSocketClient *client,
538 if (client->priv->timeout == timeout)
541 client->priv->timeout = timeout;
542 g_object_notify (G_OBJECT (client), "timeout");
546 * g_socket_client_get_enable_proxy:
547 * @client: a #GSocketClient.
549 * Gets the proxy enable state; see g_socket_client_set_enable_proxy()
551 * Returns: whether proxying is enabled
556 g_socket_client_get_enable_proxy (GSocketClient *client)
558 return client->priv->enable_proxy;
562 * g_socket_client_set_enable_proxy:
563 * @client: a #GSocketClient.
564 * @enable: whether to enable proxies
566 * Sets whether or not @client attempts to make connections via a
567 * proxy server. When enabled (the default), #GSocketClient will use a
568 * #GProxyResolver to determine if a proxy protocol such as SOCKS is
569 * needed, and automatically do the necessary proxy negotiation.
574 g_socket_client_set_enable_proxy (GSocketClient *client,
578 if (client->priv->enable_proxy == enable)
581 client->priv->enable_proxy = enable;
582 g_object_notify (G_OBJECT (client), "enable-proxy");
586 * g_socket_client_get_tls:
587 * @client: a #GSocketClient.
589 * Gets whether @client creates TLS connections. See
590 * g_socket_client_set_tls() for details.
592 * Returns: whether @client uses TLS
597 g_socket_client_get_tls (GSocketClient *client)
599 return client->priv->tls;
603 * g_socket_client_set_tls:
604 * @client: a #GSocketClient.
605 * @tls: whether to use TLS
607 * Sets whether @client creates TLS (aka SSL) connections. If @tls is
608 * %TRUE, @client will wrap its connections in a #GTlsClientConnection
609 * and perform a TLS handshake when connecting.
611 * Note that since #GSocketClient must return a #GSocketConnection,
612 * but #GTlsClientConnection is not a #GSocketConnection, this
613 * actually wraps the resulting #GTlsClientConnection in a
614 * #GTcpWrapperConnection when returning it. You can use
615 * g_tcp_wrapper_connection_get_base_io_stream() on the return value
616 * to extract the #GTlsClientConnection.
621 g_socket_client_set_tls (GSocketClient *client,
625 if (tls == client->priv->tls)
628 client->priv->tls = tls;
629 g_object_notify (G_OBJECT (client), "tls");
633 * g_socket_client_get_tls_validation_flags:
634 * @client: a #GSocketClient.
636 * Gets the TLS validation flags used creating TLS connections via
639 * Returns: the TLS validation flags
644 g_socket_client_get_tls_validation_flags (GSocketClient *client)
646 return client->priv->tls_validation_flags;
650 * g_socket_client_set_tls_validation_flags:
651 * @client: a #GSocketClient.
652 * @flags: the validation flags
654 * Sets the TLS validation flags used when creating TLS connections
655 * via @client. The default value is %G_TLS_CERTIFICATE_VALIDATE_ALL.
660 g_socket_client_set_tls_validation_flags (GSocketClient *client,
661 GTlsCertificateFlags flags)
663 if (client->priv->tls_validation_flags != flags)
665 client->priv->tls_validation_flags = flags;
666 g_object_notify (G_OBJECT (client), "tls-validation-flags");
671 g_socket_client_class_init (GSocketClientClass *class)
673 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
675 g_type_class_add_private (class, sizeof (GSocketClientPrivate));
677 gobject_class->finalize = g_socket_client_finalize;
678 gobject_class->set_property = g_socket_client_set_property;
679 gobject_class->get_property = g_socket_client_get_property;
681 g_object_class_install_property (gobject_class, PROP_FAMILY,
682 g_param_spec_enum ("family",
684 P_("The sockets address family to use for socket construction"),
685 G_TYPE_SOCKET_FAMILY,
686 G_SOCKET_FAMILY_INVALID,
689 G_PARAM_STATIC_STRINGS));
691 g_object_class_install_property (gobject_class, PROP_TYPE,
692 g_param_spec_enum ("type",
694 P_("The sockets type to use for socket construction"),
696 G_SOCKET_TYPE_STREAM,
699 G_PARAM_STATIC_STRINGS));
701 g_object_class_install_property (gobject_class, PROP_PROTOCOL,
702 g_param_spec_enum ("protocol",
703 P_("Socket protocol"),
704 P_("The protocol to use for socket construction, or 0 for default"),
705 G_TYPE_SOCKET_PROTOCOL,
706 G_SOCKET_PROTOCOL_DEFAULT,
709 G_PARAM_STATIC_STRINGS));
711 g_object_class_install_property (gobject_class, PROP_LOCAL_ADDRESS,
712 g_param_spec_object ("local-address",
714 P_("The local address constructed sockets will be bound to"),
715 G_TYPE_SOCKET_ADDRESS,
718 G_PARAM_STATIC_STRINGS));
720 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
721 g_param_spec_uint ("timeout",
722 P_("Socket timeout"),
723 P_("The I/O timeout for sockets, or 0 for none"),
727 G_PARAM_STATIC_STRINGS));
729 g_object_class_install_property (gobject_class, PROP_ENABLE_PROXY,
730 g_param_spec_boolean ("enable-proxy",
732 P_("Enable proxy support"),
736 G_PARAM_STATIC_STRINGS));
738 g_object_class_install_property (gobject_class, PROP_TLS,
739 g_param_spec_boolean ("tls",
741 P_("Whether to create TLS connections"),
745 G_PARAM_STATIC_STRINGS));
746 g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
747 g_param_spec_flags ("tls-validation-flags",
748 P_("TLS validation flags"),
749 P_("TLS validation flags to use"),
750 G_TYPE_TLS_CERTIFICATE_FLAGS,
751 G_TLS_CERTIFICATE_VALIDATE_ALL,
754 G_PARAM_STATIC_STRINGS));
758 * g_socket_client_connect:
759 * @client: a #GSocketClient.
760 * @connectable: a #GSocketConnectable specifying the remote address.
761 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
762 * @error: #GError for error reporting, or %NULL to ignore.
764 * Tries to resolve the @connectable and make a network connection to it.
766 * Upon a successful connection, a new #GSocketConnection is constructed
767 * and returned. The caller owns this new object and must drop their
768 * reference to it when finished with it.
770 * The type of the #GSocketConnection object returned depends on the type of
771 * the underlying socket that is used. For instance, for a TCP/IP connection
772 * it will be a #GTcpConnection.
774 * The socket created will be the same family as the address that the
775 * @connectable resolves to, unless family is set with g_socket_client_set_family()
776 * or indirectly via g_socket_client_set_local_address(). The socket type
777 * defaults to %G_SOCKET_TYPE_STREAM but can be set with
778 * g_socket_client_set_socket_type().
780 * If a local address is specified with g_socket_client_set_local_address() the
781 * socket will be bound to this address before connecting.
783 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
788 g_socket_client_connect (GSocketClient *client,
789 GSocketConnectable *connectable,
790 GCancellable *cancellable,
793 GIOStream *connection = NULL;
794 GSocketAddressEnumerator *enumerator = NULL;
795 GError *last_error, *tmp_error;
799 if (can_use_proxy (client))
800 enumerator = g_socket_connectable_proxy_enumerate (connectable);
802 enumerator = g_socket_connectable_enumerate (connectable);
804 while (connection == NULL)
806 GSocketAddress *address = NULL;
807 gboolean application_proxy = FALSE;
810 if (g_cancellable_is_cancelled (cancellable))
812 g_clear_error (error);
813 g_cancellable_set_error_if_cancelled (cancellable, error);
818 address = g_socket_address_enumerator_next (enumerator, cancellable,
825 g_clear_error (&last_error);
826 g_propagate_error (error, tmp_error);
830 g_propagate_error (error, last_error);
833 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
834 _("Unknown error on connect"));
838 /* clear error from previous attempt */
839 g_clear_error (&last_error);
841 socket = create_socket (client, address, &last_error);
844 g_object_unref (address);
848 connection = (GIOStream *)g_socket_connection_factory_create_connection (socket);
849 if (!g_socket_connection_connect (G_SOCKET_CONNECTION (connection),
850 address, cancellable, &last_error))
852 clarify_connect_error (last_error, connectable, address);
853 g_object_unref (connection);
858 G_IS_PROXY_ADDRESS (address) &&
859 client->priv->enable_proxy)
861 GProxyAddress *proxy_addr = G_PROXY_ADDRESS (address);
862 const gchar *protocol;
865 protocol = g_proxy_address_get_protocol (proxy_addr);
866 proxy = g_proxy_get_default_for_protocol (protocol);
868 /* The connection should not be anything else then TCP Connection,
869 * but let's put a safety guard in case
871 if (!G_IS_TCP_CONNECTION (connection))
873 g_critical ("Trying to proxy over non-TCP connection, this is "
874 "most likely a bug in GLib IO library.");
876 g_set_error_literal (&last_error,
877 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
878 _("Trying to proxy over non-TCP connection is not supported."));
880 g_object_unref (connection);
885 GIOStream *proxy_connection;
887 proxy_connection = g_proxy_connect (proxy,
892 g_object_unref (connection);
893 connection = proxy_connection;
894 g_object_unref (proxy);
896 else if (!g_hash_table_lookup_extended (client->priv->app_proxies,
897 protocol, NULL, NULL))
899 g_set_error (&last_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
900 _("Proxy protocol '%s' is not supported."),
902 g_object_unref (connection);
907 application_proxy = TRUE;
911 if (!application_proxy && connection && client->priv->tls)
915 tlsconn = g_tls_client_connection_new (connection, connectable, &last_error);
916 g_object_unref (connection);
917 connection = tlsconn;
921 g_tls_client_connection_set_validation_flags (G_TLS_CLIENT_CONNECTION (tlsconn),
922 client->priv->tls_validation_flags);
923 if (!g_tls_connection_handshake (G_TLS_CONNECTION (tlsconn),
924 cancellable, &last_error))
926 g_object_unref (tlsconn);
932 if (connection && !G_IS_SOCKET_CONNECTION (connection))
934 GSocketConnection *wrapper_connection;
936 wrapper_connection = g_tcp_wrapper_connection_new (connection, socket);
937 g_object_unref (connection);
938 connection = (GIOStream *)wrapper_connection;
941 g_object_unref (socket);
942 g_object_unref (address);
944 g_object_unref (enumerator);
946 return G_SOCKET_CONNECTION (connection);
950 * g_socket_client_connect_to_host:
951 * @client: a #GSocketClient
952 * @host_and_port: the name and optionally port of the host to connect to
953 * @default_port: the default port to connect to
954 * @cancellable: (allow-none): a #GCancellable, or %NULL
955 * @error: a pointer to a #GError, or %NULL
957 * This is a helper function for g_socket_client_connect().
959 * Attempts to create a TCP connection to the named host.
961 * @host_and_port may be in any of a number of recognized formats; an IPv6
962 * address, an IPv4 address, or a domain name (in which case a DNS
963 * lookup is performed). Quoting with [] is supported for all address
964 * types. A port override may be specified in the usual way with a
965 * colon. Ports may be given as decimal numbers or symbolic names (in
966 * which case an /etc/services lookup is performed).
968 * If no port override is given in @host_and_port then @default_port will be
969 * used as the port number to connect to.
971 * In general, @host_and_port is expected to be provided by the user (allowing
972 * them to give the hostname, and a port override if necessary) and
973 * @default_port is expected to be provided by the application.
975 * In the case that an IP address is given, a single connection
976 * attempt is made. In the case that a name is given, multiple
977 * connection attempts may be made, in turn and according to the
978 * number of address records in DNS, until a connection succeeds.
980 * Upon a successful connection, a new #GSocketConnection is constructed
981 * and returned. The caller owns this new object and must drop their
982 * reference to it when finished with it.
984 * In the event of any failure (DNS error, service not found, no hosts
985 * connectable) %NULL is returned and @error (if non-%NULL) is set
988 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
993 g_socket_client_connect_to_host (GSocketClient *client,
994 const gchar *host_and_port,
995 guint16 default_port,
996 GCancellable *cancellable,
999 GSocketConnectable *connectable;
1000 GSocketConnection *connection;
1002 connectable = g_network_address_parse (host_and_port, default_port, error);
1003 if (connectable == NULL)
1006 connection = g_socket_client_connect (client, connectable,
1007 cancellable, error);
1008 g_object_unref (connectable);
1014 * g_socket_client_connect_to_service:
1015 * @client: a #GSocketConnection
1016 * @domain: a domain name
1017 * @service: the name of the service to connect to
1018 * @cancellable: (allow-none): a #GCancellable, or %NULL
1019 * @error: a pointer to a #GError, or %NULL
1021 * Attempts to create a TCP connection to a service.
1023 * This call looks up the SRV record for @service at @domain for the
1024 * "tcp" protocol. It then attempts to connect, in turn, to each of
1025 * the hosts providing the service until either a connection succeeds
1026 * or there are no hosts remaining.
1028 * Upon a successful connection, a new #GSocketConnection is constructed
1029 * and returned. The caller owns this new object and must drop their
1030 * reference to it when finished with it.
1032 * In the event of any failure (DNS error, service not found, no hosts
1033 * connectable) %NULL is returned and @error (if non-%NULL) is set
1036 * Returns: (transfer full): a #GSocketConnection if successful, or %NULL on error
1039 g_socket_client_connect_to_service (GSocketClient *client,
1040 const gchar *domain,
1041 const gchar *service,
1042 GCancellable *cancellable,
1045 GSocketConnectable *connectable;
1046 GSocketConnection *connection;
1048 connectable = g_network_service_new (service, "tcp", domain);
1049 connection = g_socket_client_connect (client, connectable,
1050 cancellable, error);
1051 g_object_unref (connectable);
1057 * g_socket_client_connect_to_uri:
1058 * @client: a #GSocketClient
1059 * @uri: A network URI
1060 * @default_port: the default port to connect to
1061 * @cancellable: (allow-none): a #GCancellable, or %NULL
1062 * @error: a pointer to a #GError, or %NULL
1064 * This is a helper function for g_socket_client_connect().
1066 * Attempts to create a TCP connection with a network URI.
1068 * @uri may be any valid URI containing an "authority" (hostname/port)
1069 * component. If a port is not specified in the URI, @default_port
1070 * will be used. TLS will be negotiated if #GSocketClient:tls is %TRUE.
1071 * (#GSocketClient does not know to automatically assume TLS for
1072 * certain URI schemes.)
1074 * Using this rather than g_socket_client_connect() or
1075 * g_socket_client_connect_to_host() allows #GSocketClient to
1076 * determine when to use application-specific proxy protocols.
1078 * Upon a successful connection, a new #GSocketConnection is constructed
1079 * and returned. The caller owns this new object and must drop their
1080 * reference to it when finished with it.
1082 * In the event of any failure (DNS error, service not found, no hosts
1083 * connectable) %NULL is returned and @error (if non-%NULL) is set
1086 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1091 g_socket_client_connect_to_uri (GSocketClient *client,
1093 guint16 default_port,
1094 GCancellable *cancellable,
1097 GSocketConnectable *connectable;
1098 GSocketConnection *connection;
1100 connectable = g_network_address_parse_uri (uri, default_port, error);
1101 if (connectable == NULL)
1104 connection = g_socket_client_connect (client, connectable,
1105 cancellable, error);
1106 g_object_unref (connectable);
1113 GSimpleAsyncResult *result;
1114 GCancellable *cancellable;
1115 GSocketClient *client;
1117 GSocketConnectable *connectable;
1118 GSocketAddressEnumerator *enumerator;
1119 GProxyAddress *proxy_addr;
1120 GSocketAddress *current_addr;
1121 GSocket *current_socket;
1122 GIOStream *connection;
1125 } GSocketClientAsyncConnectData;
1128 g_socket_client_async_connect_complete (GSocketClientAsyncConnectData *data)
1130 if (data->last_error)
1132 g_simple_async_result_take_error (data->result, data->last_error);
1136 g_assert (data->connection);
1138 if (!G_IS_SOCKET_CONNECTION (data->connection))
1140 GSocketConnection *wrapper_connection;
1142 wrapper_connection = g_tcp_wrapper_connection_new (data->connection,
1143 data->current_socket);
1144 g_object_unref (data->connection);
1145 data->connection = (GIOStream *)wrapper_connection;
1148 g_simple_async_result_set_op_res_gpointer (data->result,
1153 g_simple_async_result_complete (data->result);
1154 g_object_unref (data->result);
1155 g_object_unref (data->connectable);
1156 g_object_unref (data->enumerator);
1157 if (data->cancellable)
1158 g_object_unref (data->cancellable);
1159 if (data->current_addr)
1160 g_object_unref (data->current_addr);
1161 if (data->current_socket)
1162 g_object_unref (data->current_socket);
1163 if (data->proxy_addr)
1164 g_object_unref (data->proxy_addr);
1165 g_slice_free (GSocketClientAsyncConnectData, data);
1170 g_socket_client_enumerator_callback (GObject *object,
1171 GAsyncResult *result,
1172 gpointer user_data);
1175 set_last_error (GSocketClientAsyncConnectData *data,
1178 g_clear_error (&data->last_error);
1179 data->last_error = error;
1183 enumerator_next_async (GSocketClientAsyncConnectData *data)
1185 /* We need to cleanup the state */
1186 g_clear_object (&data->current_socket);
1187 g_clear_object (&data->current_addr);
1188 g_clear_object (&data->proxy_addr);
1189 g_clear_object (&data->connection);
1191 g_socket_address_enumerator_next_async (data->enumerator,
1193 g_socket_client_enumerator_callback,
1198 g_socket_client_tls_handshake_callback (GObject *object,
1199 GAsyncResult *result,
1202 GSocketClientAsyncConnectData *data = user_data;
1204 if (g_tls_connection_handshake_finish (G_TLS_CONNECTION (object),
1208 g_object_unref (data->connection);
1209 data->connection = G_IO_STREAM (object);
1211 g_socket_client_async_connect_complete (data);
1215 g_object_unref (object);
1216 enumerator_next_async (data);
1221 g_socket_client_tls_handshake (GSocketClientAsyncConnectData *data)
1225 if (!data->client->priv->tls)
1227 g_socket_client_async_connect_complete (data);
1231 tlsconn = g_tls_client_connection_new (data->connection,
1236 g_tls_client_connection_set_validation_flags (G_TLS_CLIENT_CONNECTION (tlsconn),
1237 data->client->priv->tls_validation_flags);
1238 g_tls_connection_handshake_async (G_TLS_CONNECTION (tlsconn),
1241 g_socket_client_tls_handshake_callback,
1246 enumerator_next_async (data);
1251 g_socket_client_proxy_connect_callback (GObject *object,
1252 GAsyncResult *result,
1255 GSocketClientAsyncConnectData *data = user_data;
1257 g_object_unref (data->connection);
1258 data->connection = g_proxy_connect_finish (G_PROXY (object),
1261 if (!data->connection)
1263 enumerator_next_async (data);
1267 g_socket_client_tls_handshake (data);
1271 g_socket_client_connected_callback (GObject *source,
1272 GAsyncResult *result,
1275 GSocketClientAsyncConnectData *data = user_data;
1276 GError *error = NULL;
1278 const gchar *protocol;
1280 if (!g_socket_connection_connect_finish (G_SOCKET_CONNECTION (source),
1283 clarify_connect_error (error, data->connectable,
1284 data->current_addr);
1285 set_last_error (data, error);
1288 enumerator_next_async (data);
1292 /* wrong, but backward compatible */
1293 g_socket_set_blocking (data->current_socket, TRUE);
1295 if (!data->proxy_addr)
1297 g_socket_client_tls_handshake (data);
1301 protocol = g_proxy_address_get_protocol (data->proxy_addr);
1302 proxy = g_proxy_get_default_for_protocol (protocol);
1304 /* The connection should not be anything other than TCP,
1305 * but let's put a safety guard in case
1307 if (!G_IS_TCP_CONNECTION (data->connection))
1309 g_critical ("Trying to proxy over non-TCP connection, this is "
1310 "most likely a bug in GLib IO library.");
1312 g_set_error_literal (&data->last_error,
1313 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
1314 _("Trying to proxy over non-TCP connection is not supported."));
1316 enumerator_next_async (data);
1320 g_proxy_connect_async (proxy,
1324 g_socket_client_proxy_connect_callback,
1326 g_object_unref (proxy);
1328 else if (!g_hash_table_lookup_extended (data->client->priv->app_proxies,
1329 protocol, NULL, NULL))
1331 g_clear_error (&data->last_error);
1333 g_set_error (&data->last_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
1334 _("Proxy protocol '%s' is not supported."),
1337 enumerator_next_async (data);
1341 /* Simply complete the connection, we don't want to do TLS handshake
1342 * as the application proxy handling may need proxy handshake first */
1343 g_socket_client_async_connect_complete (data);
1348 g_socket_client_enumerator_callback (GObject *object,
1349 GAsyncResult *result,
1352 GSocketClientAsyncConnectData *data = user_data;
1353 GSocketAddress *address = NULL;
1355 GError *tmp_error = NULL;
1357 if (g_cancellable_is_cancelled (data->cancellable))
1359 g_clear_error (&data->last_error);
1360 g_cancellable_set_error_if_cancelled (data->cancellable, &data->last_error);
1361 g_socket_client_async_connect_complete (data);
1365 address = g_socket_address_enumerator_next_finish (data->enumerator,
1366 result, &tmp_error);
1368 if (address == NULL)
1371 set_last_error (data, tmp_error);
1372 else if (data->last_error == NULL)
1373 g_set_error_literal (&data->last_error, G_IO_ERROR, G_IO_ERROR_FAILED,
1374 _("Unknown error on connect"));
1376 g_socket_client_async_connect_complete (data);
1380 if (G_IS_PROXY_ADDRESS (address) &&
1381 data->client->priv->enable_proxy)
1382 data->proxy_addr = g_object_ref (G_PROXY_ADDRESS (address));
1384 g_clear_error (&data->last_error);
1386 socket = create_socket (data->client, address, &data->last_error);
1389 g_object_unref (address);
1390 enumerator_next_async (data);
1394 data->current_socket = socket;
1395 data->current_addr = address;
1396 data->connection = (GIOStream *) g_socket_connection_factory_create_connection (socket);
1398 g_socket_connection_connect_async (G_SOCKET_CONNECTION (data->connection),
1399 address, data->cancellable,
1400 g_socket_client_connected_callback, data);
1404 * g_socket_client_connect_async:
1405 * @client: a #GSocketClient
1406 * @connectable: a #GSocketConnectable specifying the remote address.
1407 * @cancellable: (allow-none): a #GCancellable, or %NULL
1408 * @callback: (scope async): a #GAsyncReadyCallback
1409 * @user_data: (closure): user data for the callback
1411 * This is the asynchronous version of g_socket_client_connect().
1413 * When the operation is finished @callback will be
1414 * called. You can then call g_socket_client_connect_finish() to get
1415 * the result of the operation.
1420 g_socket_client_connect_async (GSocketClient *client,
1421 GSocketConnectable *connectable,
1422 GCancellable *cancellable,
1423 GAsyncReadyCallback callback,
1426 GSocketClientAsyncConnectData *data;
1428 g_return_if_fail (G_IS_SOCKET_CLIENT (client));
1430 data = g_slice_new0 (GSocketClientAsyncConnectData);
1432 data->result = g_simple_async_result_new (G_OBJECT (client),
1433 callback, user_data,
1434 g_socket_client_connect_async);
1435 data->client = client;
1437 data->cancellable = g_object_ref (cancellable);
1439 data->cancellable = NULL;
1440 data->last_error = NULL;
1441 data->connectable = g_object_ref (connectable);
1443 if (can_use_proxy (client))
1444 data->enumerator = g_socket_connectable_proxy_enumerate (connectable);
1446 data->enumerator = g_socket_connectable_enumerate (connectable);
1448 enumerator_next_async (data);
1452 * g_socket_client_connect_to_host_async:
1453 * @client: a #GSocketClient
1454 * @host_and_port: the name and optionally the port of the host to connect to
1455 * @default_port: the default port to connect to
1456 * @cancellable: (allow-none): a #GCancellable, or %NULL
1457 * @callback: (scope async): a #GAsyncReadyCallback
1458 * @user_data: (closure): user data for the callback
1460 * This is the asynchronous version of g_socket_client_connect_to_host().
1462 * When the operation is finished @callback will be
1463 * called. You can then call g_socket_client_connect_to_host_finish() to get
1464 * the result of the operation.
1469 g_socket_client_connect_to_host_async (GSocketClient *client,
1470 const gchar *host_and_port,
1471 guint16 default_port,
1472 GCancellable *cancellable,
1473 GAsyncReadyCallback callback,
1476 GSocketConnectable *connectable;
1480 connectable = g_network_address_parse (host_and_port, default_port,
1482 if (connectable == NULL)
1484 g_simple_async_report_take_gerror_in_idle (G_OBJECT (client),
1485 callback, user_data, error);
1489 g_socket_client_connect_async (client,
1490 connectable, cancellable,
1491 callback, user_data);
1492 g_object_unref (connectable);
1497 * g_socket_client_connect_to_service_async:
1498 * @client: a #GSocketClient
1499 * @domain: a domain name
1500 * @service: the name of the service to connect to
1501 * @cancellable: (allow-none): a #GCancellable, or %NULL
1502 * @callback: (scope async): a #GAsyncReadyCallback
1503 * @user_data: (closure): user data for the callback
1505 * This is the asynchronous version of
1506 * g_socket_client_connect_to_service().
1511 g_socket_client_connect_to_service_async (GSocketClient *client,
1512 const gchar *domain,
1513 const gchar *service,
1514 GCancellable *cancellable,
1515 GAsyncReadyCallback callback,
1518 GSocketConnectable *connectable;
1520 connectable = g_network_service_new (service, "tcp", domain);
1521 g_socket_client_connect_async (client,
1522 connectable, cancellable,
1523 callback, user_data);
1524 g_object_unref (connectable);
1528 * g_socket_client_connect_to_uri_async:
1529 * @client: a #GSocketClient
1530 * @uri: a network uri
1531 * @default_port: the default port to connect to
1532 * @cancellable: (allow-none): a #GCancellable, or %NULL
1533 * @callback: (scope async): a #GAsyncReadyCallback
1534 * @user_data: (closure): user data for the callback
1536 * This is the asynchronous version of g_socket_client_connect_to_uri().
1538 * When the operation is finished @callback will be
1539 * called. You can then call g_socket_client_connect_to_uri_finish() to get
1540 * the result of the operation.
1545 g_socket_client_connect_to_uri_async (GSocketClient *client,
1547 guint16 default_port,
1548 GCancellable *cancellable,
1549 GAsyncReadyCallback callback,
1552 GSocketConnectable *connectable;
1556 connectable = g_network_address_parse_uri (uri, default_port, &error);
1557 if (connectable == NULL)
1559 g_simple_async_report_take_gerror_in_idle (G_OBJECT (client),
1560 callback, user_data, error);
1564 g_socket_client_connect_async (client,
1565 connectable, cancellable,
1566 callback, user_data);
1567 g_object_unref (connectable);
1573 * g_socket_client_connect_finish:
1574 * @client: a #GSocketClient.
1575 * @result: a #GAsyncResult.
1576 * @error: a #GError location to store the error occurring, or %NULL to
1579 * Finishes an async connect operation. See g_socket_client_connect_async()
1581 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1586 g_socket_client_connect_finish (GSocketClient *client,
1587 GAsyncResult *result,
1590 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1592 if (g_simple_async_result_propagate_error (simple, error))
1595 return g_object_ref (g_simple_async_result_get_op_res_gpointer (simple));
1599 * g_socket_client_connect_to_host_finish:
1600 * @client: a #GSocketClient.
1601 * @result: a #GAsyncResult.
1602 * @error: a #GError location to store the error occurring, or %NULL to
1605 * Finishes an async connect operation. See g_socket_client_connect_to_host_async()
1607 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1612 g_socket_client_connect_to_host_finish (GSocketClient *client,
1613 GAsyncResult *result,
1616 return g_socket_client_connect_finish (client, result, error);
1620 * g_socket_client_connect_to_service_finish:
1621 * @client: a #GSocketClient.
1622 * @result: a #GAsyncResult.
1623 * @error: a #GError location to store the error occurring, or %NULL to
1626 * Finishes an async connect operation. See g_socket_client_connect_to_service_async()
1628 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1633 g_socket_client_connect_to_service_finish (GSocketClient *client,
1634 GAsyncResult *result,
1637 return g_socket_client_connect_finish (client, result, error);
1641 * g_socket_client_connect_to_uri_finish:
1642 * @client: a #GSocketClient.
1643 * @result: a #GAsyncResult.
1644 * @error: a #GError location to store the error occurring, or %NULL to
1647 * Finishes an async connect operation. See g_socket_client_connect_to_uri_async()
1649 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1654 g_socket_client_connect_to_uri_finish (GSocketClient *client,
1655 GAsyncResult *result,
1658 return g_socket_client_connect_finish (client, result, error);
1662 * g_socket_client_add_application_proxy:
1663 * @client: a #GSocketClient
1664 * @protocol: The proxy protocol
1666 * Enable proxy protocols to be handled by the application. When the
1667 * indicated proxy protocol is returned by the #GProxyResolver,
1668 * #GSocketClient will consider this protocol as supported but will
1669 * not try to find a #GProxy instance to handle handshaking. The
1670 * application must check for this case by calling
1671 * g_socket_connection_get_remote_address() on the returned
1672 * #GSocketConnection, and seeing if it's a #GProxyAddress of the
1673 * appropriate type, to determine whether or not it needs to handle
1674 * the proxy handshaking itself.
1676 * This should be used for proxy protocols that are dialects of
1677 * another protocol such as HTTP proxy. It also allows cohabitation of
1678 * proxy protocols that are reused between protocols. A good example
1679 * is HTTP. It can be used to proxy HTTP, FTP and Gopher and can also
1680 * be use as generic socket proxy through the HTTP CONNECT method.
1682 * When the proxy is detected as being an application proxy, TLS handshake
1683 * will be skipped. This is required to let the application do the proxy
1684 * specific handshake.
1687 g_socket_client_add_application_proxy (GSocketClient *client,
1688 const gchar *protocol)
1690 g_hash_table_insert (client->priv->app_proxies, g_strdup (protocol), NULL);