GCredentials: add getter/setter for the Unix process ID
[glib.git] / gio / gsocketclient.c
blobddbe6974d238d2d078e36b4f05b4775e149449fa
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>
25 #include "config.h"
26 #include "gsocketclient.h"
28 #include <stdlib.h>
29 #include <string.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/gtask.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>
50 #include "glibintl.h"
53 /**
54 * SECTION:gsocketclient
55 * @short_description: Helper for connecting to a network service
56 * @include: gio/gio.h
57 * @see_also: #GSocketConnection, #GSocketListener
59 * #GSocketClient is a lightweight high-level utility class for connecting to
60 * a 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.
70 * As #GSocketClient is a lightweight object, you don't need to cache it. You
71 * can just create a new one any time you need one.
73 * Since: 2.22
77 G_DEFINE_TYPE (GSocketClient, g_socket_client, G_TYPE_OBJECT);
79 enum
81 EVENT,
82 LAST_SIGNAL
85 static guint signals[LAST_SIGNAL] = { 0 };
87 enum
89 PROP_NONE,
90 PROP_FAMILY,
91 PROP_TYPE,
92 PROP_PROTOCOL,
93 PROP_LOCAL_ADDRESS,
94 PROP_TIMEOUT,
95 PROP_ENABLE_PROXY,
96 PROP_TLS,
97 PROP_TLS_VALIDATION_FLAGS
100 struct _GSocketClientPrivate
102 GSocketFamily family;
103 GSocketType type;
104 GSocketProtocol protocol;
105 GSocketAddress *local_address;
106 guint timeout;
107 gboolean enable_proxy;
108 GHashTable *app_proxies;
109 gboolean tls;
110 GTlsCertificateFlags tls_validation_flags;
113 static GSocket *
114 create_socket (GSocketClient *client,
115 GSocketAddress *dest_address,
116 GError **error)
118 GSocketFamily family;
119 GSocket *socket;
121 family = client->priv->family;
122 if (family == G_SOCKET_FAMILY_INVALID &&
123 client->priv->local_address != NULL)
124 family = g_socket_address_get_family (client->priv->local_address);
125 if (family == G_SOCKET_FAMILY_INVALID)
126 family = g_socket_address_get_family (dest_address);
128 socket = g_socket_new (family,
129 client->priv->type,
130 client->priv->protocol,
131 error);
132 if (socket == NULL)
133 return NULL;
135 if (client->priv->local_address)
137 if (!g_socket_bind (socket,
138 client->priv->local_address,
139 FALSE,
140 error))
142 g_object_unref (socket);
143 return NULL;
147 if (client->priv->timeout)
148 g_socket_set_timeout (socket, client->priv->timeout);
150 return socket;
153 static gboolean
154 can_use_proxy (GSocketClient *client)
156 GSocketClientPrivate *priv = client->priv;
158 return priv->enable_proxy
159 && priv->type == G_SOCKET_TYPE_STREAM;
162 static void
163 clarify_connect_error (GError *error,
164 GSocketConnectable *connectable,
165 GSocketAddress *address)
167 const char *name;
168 char *tmp_name = NULL;
170 if (G_IS_PROXY_ADDRESS (address))
172 name = tmp_name = g_inet_address_to_string (g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address)));
174 g_prefix_error (&error, _("Could not connect to proxy server %s: "), name);
176 else
178 if (G_IS_NETWORK_ADDRESS (connectable))
179 name = g_network_address_get_hostname (G_NETWORK_ADDRESS (connectable));
180 else if (G_IS_NETWORK_SERVICE (connectable))
181 name = g_network_service_get_domain (G_NETWORK_SERVICE (connectable));
182 else if (G_IS_INET_SOCKET_ADDRESS (connectable))
183 name = tmp_name = g_inet_address_to_string (g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (connectable)));
184 else
185 name = NULL;
187 if (name)
188 g_prefix_error (&error, _("Could not connect to %s: "), name);
189 else
190 g_prefix_error (&error, _("Could not connect: "));
193 g_free (tmp_name);
196 static void
197 g_socket_client_init (GSocketClient *client)
199 client->priv = G_TYPE_INSTANCE_GET_PRIVATE (client,
200 G_TYPE_SOCKET_CLIENT,
201 GSocketClientPrivate);
202 client->priv->type = G_SOCKET_TYPE_STREAM;
203 client->priv->app_proxies = g_hash_table_new_full (g_str_hash,
204 g_str_equal,
205 g_free,
206 NULL);
210 * g_socket_client_new:
212 * Creates a new #GSocketClient with the default options.
214 * Returns: a #GSocketClient.
215 * Free the returned object with g_object_unref().
217 * Since: 2.22
219 GSocketClient *
220 g_socket_client_new (void)
222 return g_object_new (G_TYPE_SOCKET_CLIENT, NULL);
225 static void
226 g_socket_client_finalize (GObject *object)
228 GSocketClient *client = G_SOCKET_CLIENT (object);
230 if (client->priv->local_address)
231 g_object_unref (client->priv->local_address);
233 if (G_OBJECT_CLASS (g_socket_client_parent_class)->finalize)
234 (*G_OBJECT_CLASS (g_socket_client_parent_class)->finalize) (object);
236 g_hash_table_unref (client->priv->app_proxies);
239 static void
240 g_socket_client_get_property (GObject *object,
241 guint prop_id,
242 GValue *value,
243 GParamSpec *pspec)
245 GSocketClient *client = G_SOCKET_CLIENT (object);
247 switch (prop_id)
249 case PROP_FAMILY:
250 g_value_set_enum (value, client->priv->family);
251 break;
253 case PROP_TYPE:
254 g_value_set_enum (value, client->priv->type);
255 break;
257 case PROP_PROTOCOL:
258 g_value_set_enum (value, client->priv->protocol);
259 break;
261 case PROP_LOCAL_ADDRESS:
262 g_value_set_object (value, client->priv->local_address);
263 break;
265 case PROP_TIMEOUT:
266 g_value_set_uint (value, client->priv->timeout);
267 break;
269 case PROP_ENABLE_PROXY:
270 g_value_set_boolean (value, client->priv->enable_proxy);
271 break;
273 case PROP_TLS:
274 g_value_set_boolean (value, g_socket_client_get_tls (client));
275 break;
277 case PROP_TLS_VALIDATION_FLAGS:
278 g_value_set_flags (value, g_socket_client_get_tls_validation_flags (client));
279 break;
281 default:
282 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
286 static void
287 g_socket_client_set_property (GObject *object,
288 guint prop_id,
289 const GValue *value,
290 GParamSpec *pspec)
292 GSocketClient *client = G_SOCKET_CLIENT (object);
294 switch (prop_id)
296 case PROP_FAMILY:
297 g_socket_client_set_family (client, g_value_get_enum (value));
298 break;
300 case PROP_TYPE:
301 g_socket_client_set_socket_type (client, g_value_get_enum (value));
302 break;
304 case PROP_PROTOCOL:
305 g_socket_client_set_protocol (client, g_value_get_enum (value));
306 break;
308 case PROP_LOCAL_ADDRESS:
309 g_socket_client_set_local_address (client, g_value_get_object (value));
310 break;
312 case PROP_TIMEOUT:
313 g_socket_client_set_timeout (client, g_value_get_uint (value));
314 break;
316 case PROP_ENABLE_PROXY:
317 g_socket_client_set_enable_proxy (client, g_value_get_boolean (value));
318 break;
320 case PROP_TLS:
321 g_socket_client_set_tls (client, g_value_get_boolean (value));
322 break;
324 case PROP_TLS_VALIDATION_FLAGS:
325 g_socket_client_set_tls_validation_flags (client, g_value_get_flags (value));
326 break;
328 default:
329 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
334 * g_socket_client_get_family:
335 * @client: a #GSocketClient.
337 * Gets the socket family of the socket client.
339 * See g_socket_client_set_family() for details.
341 * Returns: a #GSocketFamily
343 * Since: 2.22
345 GSocketFamily
346 g_socket_client_get_family (GSocketClient *client)
348 return client->priv->family;
352 * g_socket_client_set_family:
353 * @client: a #GSocketClient.
354 * @family: a #GSocketFamily
356 * Sets the socket family of the socket client.
357 * If this is set to something other than %G_SOCKET_FAMILY_INVALID
358 * then the sockets created by this object will be of the specified
359 * family.
361 * This might be useful for instance if you want to force the local
362 * connection to be an ipv4 socket, even though the address might
363 * be an ipv6 mapped to ipv4 address.
365 * Since: 2.22
367 void
368 g_socket_client_set_family (GSocketClient *client,
369 GSocketFamily family)
371 if (client->priv->family == family)
372 return;
374 client->priv->family = family;
375 g_object_notify (G_OBJECT (client), "family");
379 * g_socket_client_get_socket_type:
380 * @client: a #GSocketClient.
382 * Gets the socket type of the socket client.
384 * See g_socket_client_set_socket_type() for details.
386 * Returns: a #GSocketFamily
388 * Since: 2.22
390 GSocketType
391 g_socket_client_get_socket_type (GSocketClient *client)
393 return client->priv->type;
397 * g_socket_client_set_socket_type:
398 * @client: a #GSocketClient.
399 * @type: a #GSocketType
401 * Sets the socket type of the socket client.
402 * The sockets created by this object will be of the specified
403 * type.
405 * It doesn't make sense to specify a type of %G_SOCKET_TYPE_DATAGRAM,
406 * as GSocketClient is used for connection oriented services.
408 * Since: 2.22
410 void
411 g_socket_client_set_socket_type (GSocketClient *client,
412 GSocketType type)
414 if (client->priv->type == type)
415 return;
417 client->priv->type = type;
418 g_object_notify (G_OBJECT (client), "type");
422 * g_socket_client_get_protocol:
423 * @client: a #GSocketClient
425 * Gets the protocol name type of the socket client.
427 * See g_socket_client_set_protocol() for details.
429 * Returns: a #GSocketProtocol
431 * Since: 2.22
433 GSocketProtocol
434 g_socket_client_get_protocol (GSocketClient *client)
436 return client->priv->protocol;
440 * g_socket_client_set_protocol:
441 * @client: a #GSocketClient.
442 * @protocol: a #GSocketProtocol
444 * Sets the protocol of the socket client.
445 * The sockets created by this object will use of the specified
446 * protocol.
448 * If @protocol is %0 that means to use the default
449 * protocol for the socket family and type.
451 * Since: 2.22
453 void
454 g_socket_client_set_protocol (GSocketClient *client,
455 GSocketProtocol protocol)
457 if (client->priv->protocol == protocol)
458 return;
460 client->priv->protocol = protocol;
461 g_object_notify (G_OBJECT (client), "protocol");
465 * g_socket_client_get_local_address:
466 * @client: a #GSocketClient.
468 * Gets the local address of the socket client.
470 * See g_socket_client_set_local_address() for details.
472 * Returns: (transfer none): a #GSocketAddress or %NULL. Do not free.
474 * Since: 2.22
476 GSocketAddress *
477 g_socket_client_get_local_address (GSocketClient *client)
479 return client->priv->local_address;
483 * g_socket_client_set_local_address:
484 * @client: a #GSocketClient.
485 * @address: (allow-none): a #GSocketAddress, or %NULL
487 * Sets the local address of the socket client.
488 * The sockets created by this object will bound to the
489 * specified address (if not %NULL) before connecting.
491 * This is useful if you want to ensure that the local
492 * side of the connection is on a specific port, or on
493 * a specific interface.
495 * Since: 2.22
497 void
498 g_socket_client_set_local_address (GSocketClient *client,
499 GSocketAddress *address)
501 if (address)
502 g_object_ref (address);
504 if (client->priv->local_address)
506 g_object_unref (client->priv->local_address);
508 client->priv->local_address = address;
509 g_object_notify (G_OBJECT (client), "local-address");
513 * g_socket_client_get_timeout:
514 * @client: a #GSocketClient
516 * Gets the I/O timeout time for sockets created by @client.
518 * See g_socket_client_set_timeout() for details.
520 * Returns: the timeout in seconds
522 * Since: 2.26
524 guint
525 g_socket_client_get_timeout (GSocketClient *client)
527 return client->priv->timeout;
532 * g_socket_client_set_timeout:
533 * @client: a #GSocketClient.
534 * @timeout: the timeout
536 * Sets the I/O timeout for sockets created by @client. @timeout is a
537 * time in seconds, or 0 for no timeout (the default).
539 * The timeout value affects the initial connection attempt as well,
540 * so setting this may cause calls to g_socket_client_connect(), etc,
541 * to fail with %G_IO_ERROR_TIMED_OUT.
543 * Since: 2.26
545 void
546 g_socket_client_set_timeout (GSocketClient *client,
547 guint timeout)
549 if (client->priv->timeout == timeout)
550 return;
552 client->priv->timeout = timeout;
553 g_object_notify (G_OBJECT (client), "timeout");
557 * g_socket_client_get_enable_proxy:
558 * @client: a #GSocketClient.
560 * Gets the proxy enable state; see g_socket_client_set_enable_proxy()
562 * Returns: whether proxying is enabled
564 * Since: 2.26
566 gboolean
567 g_socket_client_get_enable_proxy (GSocketClient *client)
569 return client->priv->enable_proxy;
573 * g_socket_client_set_enable_proxy:
574 * @client: a #GSocketClient.
575 * @enable: whether to enable proxies
577 * Sets whether or not @client attempts to make connections via a
578 * proxy server. When enabled (the default), #GSocketClient will use a
579 * #GProxyResolver to determine if a proxy protocol such as SOCKS is
580 * needed, and automatically do the necessary proxy negotiation.
582 * Since: 2.26
584 void
585 g_socket_client_set_enable_proxy (GSocketClient *client,
586 gboolean enable)
588 enable = !!enable;
589 if (client->priv->enable_proxy == enable)
590 return;
592 client->priv->enable_proxy = enable;
593 g_object_notify (G_OBJECT (client), "enable-proxy");
597 * g_socket_client_get_tls:
598 * @client: a #GSocketClient.
600 * Gets whether @client creates TLS connections. See
601 * g_socket_client_set_tls() for details.
603 * Returns: whether @client uses TLS
605 * Since: 2.28
607 gboolean
608 g_socket_client_get_tls (GSocketClient *client)
610 return client->priv->tls;
614 * g_socket_client_set_tls:
615 * @client: a #GSocketClient.
616 * @tls: whether to use TLS
618 * Sets whether @client creates TLS (aka SSL) connections. If @tls is
619 * %TRUE, @client will wrap its connections in a #GTlsClientConnection
620 * and perform a TLS handshake when connecting.
622 * Note that since #GSocketClient must return a #GSocketConnection,
623 * but #GTlsClientConnection is not a #GSocketConnection, this
624 * actually wraps the resulting #GTlsClientConnection in a
625 * #GTcpWrapperConnection when returning it. You can use
626 * g_tcp_wrapper_connection_get_base_io_stream() on the return value
627 * to extract the #GTlsClientConnection.
629 * If you need to modify the behavior of the TLS handshake (eg, by
630 * setting a client-side certificate to use, or connecting to the
631 * #GTlsConnection::accept-certificate signal), you can connect to
632 * @client's #GSocketClient::event signal and wait for it to be
633 * emitted with %G_SOCKET_CLIENT_TLS_HANDSHAKING, which will give you
634 * a chance to see the #GTlsClientConnection before the handshake
635 * starts.
637 * Since: 2.28
639 void
640 g_socket_client_set_tls (GSocketClient *client,
641 gboolean tls)
643 tls = !!tls;
644 if (tls == client->priv->tls)
645 return;
647 client->priv->tls = tls;
648 g_object_notify (G_OBJECT (client), "tls");
652 * g_socket_client_get_tls_validation_flags:
653 * @client: a #GSocketClient.
655 * Gets the TLS validation flags used creating TLS connections via
656 * @client.
658 * Returns: the TLS validation flags
660 * Since: 2.28
662 GTlsCertificateFlags
663 g_socket_client_get_tls_validation_flags (GSocketClient *client)
665 return client->priv->tls_validation_flags;
669 * g_socket_client_set_tls_validation_flags:
670 * @client: a #GSocketClient.
671 * @flags: the validation flags
673 * Sets the TLS validation flags used when creating TLS connections
674 * via @client. The default value is %G_TLS_CERTIFICATE_VALIDATE_ALL.
676 * Since: 2.28
678 void
679 g_socket_client_set_tls_validation_flags (GSocketClient *client,
680 GTlsCertificateFlags flags)
682 if (client->priv->tls_validation_flags != flags)
684 client->priv->tls_validation_flags = flags;
685 g_object_notify (G_OBJECT (client), "tls-validation-flags");
689 static void
690 g_socket_client_class_init (GSocketClientClass *class)
692 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
694 g_type_class_add_private (class, sizeof (GSocketClientPrivate));
696 gobject_class->finalize = g_socket_client_finalize;
697 gobject_class->set_property = g_socket_client_set_property;
698 gobject_class->get_property = g_socket_client_get_property;
701 * GSocketClient::event:
702 * @client: the #GSocketClient
703 * @event: the event that is occurring
704 * @connectable: the #GSocketConnectable that @event is occurring on
705 * @connection: the current representation of the connection
707 * Emitted when @client's activity on @connectable changes state.
708 * Among other things, this can be used to provide progress
709 * information about a network connection in the UI. The meanings of
710 * the different @event values are as follows:
712 * <variablelist>
713 * <varlistentry>
714 * <term>%G_SOCKET_CLIENT_RESOLVING:</term>
715 * <listitem><para>
716 * @client is about to look up @connectable in DNS.
717 * @connection will be %NULL.
718 * </para></listitem>
719 * </varlistentry>
720 * <varlistentry>
721 * <term>%G_SOCKET_CLIENT_RESOLVED:</term>
722 * <listitem><para>
723 * @client has successfully resolved @connectable in DNS.
724 * @connection will be %NULL.
725 * </para></listitem>
726 * </varlistentry>
727 * <varlistentry>
728 * <term>%G_SOCKET_CLIENT_CONNECTING:</term>
729 * <listitem><para>
730 * @client is about to make a connection to a remote host;
731 * either a proxy server or the destination server itself.
732 * @connection is the #GSocketConnection, which is not yet
733 * connected.
734 * </para></listitem>
735 * </varlistentry>
736 * <varlistentry>
737 * <term>%G_SOCKET_CLIENT_CONNECTED:</term>
738 * <listitem><para>
739 * @client has successfully connected to a remote host.
740 * @connection is the connected #GSocketConnection.
741 * </para></listitem>
742 * </varlistentry>
743 * <varlistentry>
744 * <term>%G_SOCKET_CLIENT_PROXY_NEGOTIATING:</term>
745 * <listitem><para>
746 * @client is about to negotiate with a proxy to get it to
747 * connect to @connectable. @connection is the
748 * #GSocketConnection to the proxy server.
749 * </para></listitem>
750 * </varlistentry>
751 * <varlistentry>
752 * <term>%G_SOCKET_CLIENT_PROXY_NEGOTIATED:</term>
753 * <listitem><para>
754 * @client has negotiated a connection to @connectable through
755 * a proxy server. @connection is the stream returned from
756 * g_proxy_connect(), which may or may not be a
757 * #GSocketConnection.
758 * </para></listitem>
759 * </varlistentry>
760 * <varlistentry>
761 * <term>%G_SOCKET_CLIENT_TLS_HANDSHAKING:</term>
762 * <listitem><para>
763 * @client is about to begin a TLS handshake. @connection is a
764 * #GTlsClientConnection.
765 * </para></listitem>
766 * </varlistentry>
767 * <varlistentry>
768 * <term>%G_SOCKET_CLIENT_TLS_HANDSHAKED:</term>
769 * <listitem><para>
770 * @client has successfully completed the TLS handshake.
771 * @connection is a #GTlsClientConnection.
772 * </para></listitem>
773 * </varlistentry>
774 * <varlistentry>
775 * <term>%G_SOCKET_CLIENT_COMPLETE:</term>
776 * <listitem><para>
777 * @client has either successfully connected to @connectable
778 * (in which case @connection is the #GSocketConnection that
779 * it will be returning to the caller) or has failed (in which
780 * case @connection is %NULL and the client is about to return
781 * an error).
782 * </para></listitem>
783 * </varlistentry>
784 * </variablelist>
786 * Each event except %G_SOCKET_CLIENT_COMPLETE may be emitted
787 * multiple times (or not at all) for a given connectable (in
788 * particular, if @client ends up attempting to connect to more than
789 * one address). However, if @client emits the #GSocketClient::event
790 * signal at all for a given connectable, that it will always emit
791 * it with %G_SOCKET_CLIENT_COMPLETE when it is done.
793 * Note that there may be additional #GSocketClientEvent values in
794 * the future; unrecognized @event values should be ignored.
796 * Since: 2.32
798 signals[EVENT] =
799 g_signal_new (I_("event"),
800 G_TYPE_FROM_CLASS (gobject_class),
801 G_SIGNAL_RUN_LAST,
802 G_STRUCT_OFFSET (GSocketClientClass, event),
803 NULL, NULL,
804 NULL,
805 G_TYPE_NONE, 3,
806 G_TYPE_SOCKET_CLIENT_EVENT,
807 G_TYPE_SOCKET_CONNECTABLE,
808 G_TYPE_IO_STREAM);
810 g_object_class_install_property (gobject_class, PROP_FAMILY,
811 g_param_spec_enum ("family",
812 P_("Socket family"),
813 P_("The sockets address family to use for socket construction"),
814 G_TYPE_SOCKET_FAMILY,
815 G_SOCKET_FAMILY_INVALID,
816 G_PARAM_CONSTRUCT |
817 G_PARAM_READWRITE |
818 G_PARAM_STATIC_STRINGS));
820 g_object_class_install_property (gobject_class, PROP_TYPE,
821 g_param_spec_enum ("type",
822 P_("Socket type"),
823 P_("The sockets type to use for socket construction"),
824 G_TYPE_SOCKET_TYPE,
825 G_SOCKET_TYPE_STREAM,
826 G_PARAM_CONSTRUCT |
827 G_PARAM_READWRITE |
828 G_PARAM_STATIC_STRINGS));
830 g_object_class_install_property (gobject_class, PROP_PROTOCOL,
831 g_param_spec_enum ("protocol",
832 P_("Socket protocol"),
833 P_("The protocol to use for socket construction, or 0 for default"),
834 G_TYPE_SOCKET_PROTOCOL,
835 G_SOCKET_PROTOCOL_DEFAULT,
836 G_PARAM_CONSTRUCT |
837 G_PARAM_READWRITE |
838 G_PARAM_STATIC_STRINGS));
840 g_object_class_install_property (gobject_class, PROP_LOCAL_ADDRESS,
841 g_param_spec_object ("local-address",
842 P_("Local address"),
843 P_("The local address constructed sockets will be bound to"),
844 G_TYPE_SOCKET_ADDRESS,
845 G_PARAM_CONSTRUCT |
846 G_PARAM_READWRITE |
847 G_PARAM_STATIC_STRINGS));
849 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
850 g_param_spec_uint ("timeout",
851 P_("Socket timeout"),
852 P_("The I/O timeout for sockets, or 0 for none"),
853 0, G_MAXUINT, 0,
854 G_PARAM_CONSTRUCT |
855 G_PARAM_READWRITE |
856 G_PARAM_STATIC_STRINGS));
858 g_object_class_install_property (gobject_class, PROP_ENABLE_PROXY,
859 g_param_spec_boolean ("enable-proxy",
860 P_("Enable proxy"),
861 P_("Enable proxy support"),
862 TRUE,
863 G_PARAM_CONSTRUCT |
864 G_PARAM_READWRITE |
865 G_PARAM_STATIC_STRINGS));
867 g_object_class_install_property (gobject_class, PROP_TLS,
868 g_param_spec_boolean ("tls",
869 P_("TLS"),
870 P_("Whether to create TLS connections"),
871 FALSE,
872 G_PARAM_CONSTRUCT |
873 G_PARAM_READWRITE |
874 G_PARAM_STATIC_STRINGS));
875 g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
876 g_param_spec_flags ("tls-validation-flags",
877 P_("TLS validation flags"),
878 P_("TLS validation flags to use"),
879 G_TYPE_TLS_CERTIFICATE_FLAGS,
880 G_TLS_CERTIFICATE_VALIDATE_ALL,
881 G_PARAM_CONSTRUCT |
882 G_PARAM_READWRITE |
883 G_PARAM_STATIC_STRINGS));
886 static void
887 g_socket_client_emit_event (GSocketClient *client,
888 GSocketClientEvent event,
889 GSocketConnectable *connectable,
890 GIOStream *connection)
892 g_signal_emit (client, signals[EVENT], 0,
893 event, connectable, connection);
897 * g_socket_client_connect:
898 * @client: a #GSocketClient.
899 * @connectable: a #GSocketConnectable specifying the remote address.
900 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
901 * @error: #GError for error reporting, or %NULL to ignore.
903 * Tries to resolve the @connectable and make a network connection to it.
905 * Upon a successful connection, a new #GSocketConnection is constructed
906 * and returned. The caller owns this new object and must drop their
907 * reference to it when finished with it.
909 * The type of the #GSocketConnection object returned depends on the type of
910 * the underlying socket that is used. For instance, for a TCP/IP connection
911 * it will be a #GTcpConnection.
913 * The socket created will be the same family as the address that the
914 * @connectable resolves to, unless family is set with g_socket_client_set_family()
915 * or indirectly via g_socket_client_set_local_address(). The socket type
916 * defaults to %G_SOCKET_TYPE_STREAM but can be set with
917 * g_socket_client_set_socket_type().
919 * If a local address is specified with g_socket_client_set_local_address() the
920 * socket will be bound to this address before connecting.
922 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
924 * Since: 2.22
926 GSocketConnection *
927 g_socket_client_connect (GSocketClient *client,
928 GSocketConnectable *connectable,
929 GCancellable *cancellable,
930 GError **error)
932 GIOStream *connection = NULL;
933 GSocketAddressEnumerator *enumerator = NULL;
934 GError *last_error, *tmp_error;
936 last_error = NULL;
938 if (can_use_proxy (client))
939 enumerator = g_socket_connectable_proxy_enumerate (connectable);
940 else
941 enumerator = g_socket_connectable_enumerate (connectable);
943 while (connection == NULL)
945 GSocketAddress *address = NULL;
946 gboolean application_proxy = FALSE;
947 GSocket *socket;
948 gboolean using_proxy;
950 if (g_cancellable_is_cancelled (cancellable))
952 g_clear_error (error);
953 g_cancellable_set_error_if_cancelled (cancellable, error);
954 break;
957 tmp_error = NULL;
958 g_socket_client_emit_event (client, G_SOCKET_CLIENT_RESOLVING,
959 connectable, NULL);
960 address = g_socket_address_enumerator_next (enumerator, cancellable,
961 &tmp_error);
963 if (address == NULL)
965 if (tmp_error)
967 g_clear_error (&last_error);
968 g_propagate_error (error, tmp_error);
970 else if (last_error)
972 g_propagate_error (error, last_error);
974 else
975 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
976 _("Unknown error on connect"));
977 break;
979 g_socket_client_emit_event (client, G_SOCKET_CLIENT_RESOLVED,
980 connectable, NULL);
982 using_proxy = (G_IS_PROXY_ADDRESS (address) &&
983 client->priv->enable_proxy);
985 /* clear error from previous attempt */
986 g_clear_error (&last_error);
988 socket = create_socket (client, address, &last_error);
989 if (socket == NULL)
991 g_object_unref (address);
992 continue;
995 connection = (GIOStream *)g_socket_connection_factory_create_connection (socket);
996 g_socket_client_emit_event (client, G_SOCKET_CLIENT_CONNECTING, connectable, connection);
998 if (g_socket_connection_connect (G_SOCKET_CONNECTION (connection),
999 address, cancellable, &last_error))
1001 g_socket_client_emit_event (client, G_SOCKET_CLIENT_CONNECTED, connectable, connection);
1003 else
1005 clarify_connect_error (last_error, connectable, address);
1006 g_object_unref (connection);
1007 connection = NULL;
1010 if (connection && using_proxy)
1012 GProxyAddress *proxy_addr = G_PROXY_ADDRESS (address);
1013 const gchar *protocol;
1014 GProxy *proxy;
1016 protocol = g_proxy_address_get_protocol (proxy_addr);
1017 proxy = g_proxy_get_default_for_protocol (protocol);
1019 /* The connection should not be anything else then TCP Connection,
1020 * but let's put a safety guard in case
1022 if (!G_IS_TCP_CONNECTION (connection))
1024 g_critical ("Trying to proxy over non-TCP connection, this is "
1025 "most likely a bug in GLib IO library.");
1027 g_set_error_literal (&last_error,
1028 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
1029 _("Proxying over a non-TCP connection is not supported."));
1031 g_object_unref (connection);
1032 connection = NULL;
1034 else if (proxy)
1036 GIOStream *proxy_connection;
1038 g_socket_client_emit_event (client, G_SOCKET_CLIENT_PROXY_NEGOTIATING, connectable, connection);
1039 proxy_connection = g_proxy_connect (proxy,
1040 connection,
1041 proxy_addr,
1042 cancellable,
1043 &last_error);
1044 g_object_unref (connection);
1045 connection = proxy_connection;
1046 g_object_unref (proxy);
1048 if (connection)
1049 g_socket_client_emit_event (client, G_SOCKET_CLIENT_PROXY_NEGOTIATED, connectable, connection);
1051 else if (!g_hash_table_lookup_extended (client->priv->app_proxies,
1052 protocol, NULL, NULL))
1054 g_set_error (&last_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
1055 _("Proxy protocol '%s' is not supported."),
1056 protocol);
1057 g_object_unref (connection);
1058 connection = NULL;
1060 else
1062 application_proxy = TRUE;
1066 if (!application_proxy && connection && client->priv->tls)
1068 GIOStream *tlsconn;
1070 tlsconn = g_tls_client_connection_new (connection, connectable, &last_error);
1071 g_object_unref (connection);
1072 connection = tlsconn;
1074 if (tlsconn)
1076 g_tls_client_connection_set_validation_flags (G_TLS_CLIENT_CONNECTION (tlsconn),
1077 client->priv->tls_validation_flags);
1078 g_socket_client_emit_event (client, G_SOCKET_CLIENT_TLS_HANDSHAKING, connectable, connection);
1079 if (g_tls_connection_handshake (G_TLS_CONNECTION (tlsconn),
1080 cancellable, &last_error))
1082 g_socket_client_emit_event (client, G_SOCKET_CLIENT_TLS_HANDSHAKED, connectable, connection);
1084 else
1086 g_object_unref (tlsconn);
1087 connection = NULL;
1092 if (connection && !G_IS_SOCKET_CONNECTION (connection))
1094 GSocketConnection *wrapper_connection;
1096 wrapper_connection = g_tcp_wrapper_connection_new (connection, socket);
1097 g_object_unref (connection);
1098 connection = (GIOStream *)wrapper_connection;
1101 g_object_unref (socket);
1102 g_object_unref (address);
1104 g_object_unref (enumerator);
1106 g_socket_client_emit_event (client, G_SOCKET_CLIENT_COMPLETE, connectable, connection);
1107 return G_SOCKET_CONNECTION (connection);
1111 * g_socket_client_connect_to_host:
1112 * @client: a #GSocketClient
1113 * @host_and_port: the name and optionally port of the host to connect to
1114 * @default_port: the default port to connect to
1115 * @cancellable: (allow-none): a #GCancellable, or %NULL
1116 * @error: a pointer to a #GError, or %NULL
1118 * This is a helper function for g_socket_client_connect().
1120 * Attempts to create a TCP connection to the named host.
1122 * @host_and_port may be in any of a number of recognized formats; an IPv6
1123 * address, an IPv4 address, or a domain name (in which case a DNS
1124 * lookup is performed). Quoting with [] is supported for all address
1125 * types. A port override may be specified in the usual way with a
1126 * colon. Ports may be given as decimal numbers or symbolic names (in
1127 * which case an /etc/services lookup is performed).
1129 * If no port override is given in @host_and_port then @default_port will be
1130 * used as the port number to connect to.
1132 * In general, @host_and_port is expected to be provided by the user (allowing
1133 * them to give the hostname, and a port override if necessary) and
1134 * @default_port is expected to be provided by the application.
1136 * In the case that an IP address is given, a single connection
1137 * attempt is made. In the case that a name is given, multiple
1138 * connection attempts may be made, in turn and according to the
1139 * number of address records in DNS, until a connection succeeds.
1141 * Upon a successful connection, a new #GSocketConnection is constructed
1142 * and returned. The caller owns this new object and must drop their
1143 * reference to it when finished with it.
1145 * In the event of any failure (DNS error, service not found, no hosts
1146 * connectable) %NULL is returned and @error (if non-%NULL) is set
1147 * accordingly.
1149 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1151 * Since: 2.22
1153 GSocketConnection *
1154 g_socket_client_connect_to_host (GSocketClient *client,
1155 const gchar *host_and_port,
1156 guint16 default_port,
1157 GCancellable *cancellable,
1158 GError **error)
1160 GSocketConnectable *connectable;
1161 GSocketConnection *connection;
1163 connectable = g_network_address_parse (host_and_port, default_port, error);
1164 if (connectable == NULL)
1165 return NULL;
1167 connection = g_socket_client_connect (client, connectable,
1168 cancellable, error);
1169 g_object_unref (connectable);
1171 return connection;
1175 * g_socket_client_connect_to_service:
1176 * @client: a #GSocketConnection
1177 * @domain: a domain name
1178 * @service: the name of the service to connect to
1179 * @cancellable: (allow-none): a #GCancellable, or %NULL
1180 * @error: a pointer to a #GError, or %NULL
1182 * Attempts to create a TCP connection to a service.
1184 * This call looks up the SRV record for @service at @domain for the
1185 * "tcp" protocol. It then attempts to connect, in turn, to each of
1186 * the hosts providing the service until either a connection succeeds
1187 * or there are no hosts remaining.
1189 * Upon a successful connection, a new #GSocketConnection is constructed
1190 * and returned. The caller owns this new object and must drop their
1191 * reference to it when finished with it.
1193 * In the event of any failure (DNS error, service not found, no hosts
1194 * connectable) %NULL is returned and @error (if non-%NULL) is set
1195 * accordingly.
1197 * Returns: (transfer full): a #GSocketConnection if successful, or %NULL on error
1199 GSocketConnection *
1200 g_socket_client_connect_to_service (GSocketClient *client,
1201 const gchar *domain,
1202 const gchar *service,
1203 GCancellable *cancellable,
1204 GError **error)
1206 GSocketConnectable *connectable;
1207 GSocketConnection *connection;
1209 connectable = g_network_service_new (service, "tcp", domain);
1210 connection = g_socket_client_connect (client, connectable,
1211 cancellable, error);
1212 g_object_unref (connectable);
1214 return connection;
1218 * g_socket_client_connect_to_uri:
1219 * @client: a #GSocketClient
1220 * @uri: A network URI
1221 * @default_port: the default port to connect to
1222 * @cancellable: (allow-none): a #GCancellable, or %NULL
1223 * @error: a pointer to a #GError, or %NULL
1225 * This is a helper function for g_socket_client_connect().
1227 * Attempts to create a TCP connection with a network URI.
1229 * @uri may be any valid URI containing an "authority" (hostname/port)
1230 * component. If a port is not specified in the URI, @default_port
1231 * will be used. TLS will be negotiated if #GSocketClient:tls is %TRUE.
1232 * (#GSocketClient does not know to automatically assume TLS for
1233 * certain URI schemes.)
1235 * Using this rather than g_socket_client_connect() or
1236 * g_socket_client_connect_to_host() allows #GSocketClient to
1237 * determine when to use application-specific proxy protocols.
1239 * Upon a successful connection, a new #GSocketConnection is constructed
1240 * and returned. The caller owns this new object and must drop their
1241 * reference to it when finished with it.
1243 * In the event of any failure (DNS error, service not found, no hosts
1244 * connectable) %NULL is returned and @error (if non-%NULL) is set
1245 * accordingly.
1247 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1249 * Since: 2.26
1251 GSocketConnection *
1252 g_socket_client_connect_to_uri (GSocketClient *client,
1253 const gchar *uri,
1254 guint16 default_port,
1255 GCancellable *cancellable,
1256 GError **error)
1258 GSocketConnectable *connectable;
1259 GSocketConnection *connection;
1261 connectable = g_network_address_parse_uri (uri, default_port, error);
1262 if (connectable == NULL)
1263 return NULL;
1265 connection = g_socket_client_connect (client, connectable,
1266 cancellable, error);
1267 g_object_unref (connectable);
1269 return connection;
1272 typedef struct
1274 GTask *task;
1275 GSocketClient *client;
1277 GSocketConnectable *connectable;
1278 GSocketAddressEnumerator *enumerator;
1279 GProxyAddress *proxy_addr;
1280 GSocketAddress *current_addr;
1281 GSocket *current_socket;
1282 GIOStream *connection;
1284 GError *last_error;
1285 } GSocketClientAsyncConnectData;
1287 static void
1288 g_socket_client_async_connect_data_free (GSocketClientAsyncConnectData *data)
1290 g_clear_object (&data->connectable);
1291 g_clear_object (&data->enumerator);
1292 g_clear_object (&data->proxy_addr);
1293 g_clear_object (&data->current_addr);
1294 g_clear_object (&data->current_socket);
1295 g_clear_object (&data->connection);
1297 g_clear_error (&data->last_error);
1299 g_slice_free (GSocketClientAsyncConnectData, data);
1302 static void
1303 g_socket_client_async_connect_complete (GSocketClientAsyncConnectData *data)
1305 g_assert (data->connection);
1307 if (!G_IS_SOCKET_CONNECTION (data->connection))
1309 GSocketConnection *wrapper_connection;
1311 wrapper_connection = g_tcp_wrapper_connection_new (data->connection,
1312 data->current_socket);
1313 g_object_unref (data->connection);
1314 data->connection = (GIOStream *)wrapper_connection;
1317 g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_COMPLETE, data->connectable, data->connection);
1318 g_task_return_pointer (data->task, data->connection, g_object_unref);
1319 data->connection = NULL;
1320 g_object_unref (data->task);
1324 static void
1325 g_socket_client_enumerator_callback (GObject *object,
1326 GAsyncResult *result,
1327 gpointer user_data);
1329 static void
1330 set_last_error (GSocketClientAsyncConnectData *data,
1331 GError *error)
1333 g_clear_error (&data->last_error);
1334 data->last_error = error;
1337 static void
1338 enumerator_next_async (GSocketClientAsyncConnectData *data)
1340 /* We need to cleanup the state */
1341 g_clear_object (&data->current_socket);
1342 g_clear_object (&data->current_addr);
1343 g_clear_object (&data->proxy_addr);
1344 g_clear_object (&data->connection);
1346 g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_RESOLVING, data->connectable, NULL);
1347 g_socket_address_enumerator_next_async (data->enumerator,
1348 g_task_get_cancellable (data->task),
1349 g_socket_client_enumerator_callback,
1350 data);
1353 static void
1354 g_socket_client_tls_handshake_callback (GObject *object,
1355 GAsyncResult *result,
1356 gpointer user_data)
1358 GSocketClientAsyncConnectData *data = user_data;
1360 if (g_tls_connection_handshake_finish (G_TLS_CONNECTION (object),
1361 result,
1362 &data->last_error))
1364 g_object_unref (data->connection);
1365 data->connection = G_IO_STREAM (object);
1367 g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_TLS_HANDSHAKED, data->connectable, data->connection);
1368 g_socket_client_async_connect_complete (data);
1370 else
1372 g_object_unref (object);
1373 enumerator_next_async (data);
1377 static void
1378 g_socket_client_tls_handshake (GSocketClientAsyncConnectData *data)
1380 GIOStream *tlsconn;
1382 if (!data->client->priv->tls)
1384 g_socket_client_async_connect_complete (data);
1385 return;
1388 tlsconn = g_tls_client_connection_new (data->connection,
1389 data->connectable,
1390 &data->last_error);
1391 if (tlsconn)
1393 g_tls_client_connection_set_validation_flags (G_TLS_CLIENT_CONNECTION (tlsconn),
1394 data->client->priv->tls_validation_flags);
1395 g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_TLS_HANDSHAKING, data->connectable, G_IO_STREAM (tlsconn));
1396 g_tls_connection_handshake_async (G_TLS_CONNECTION (tlsconn),
1397 G_PRIORITY_DEFAULT,
1398 g_task_get_cancellable (data->task),
1399 g_socket_client_tls_handshake_callback,
1400 data);
1402 else
1404 enumerator_next_async (data);
1408 static void
1409 g_socket_client_proxy_connect_callback (GObject *object,
1410 GAsyncResult *result,
1411 gpointer user_data)
1413 GSocketClientAsyncConnectData *data = user_data;
1415 g_object_unref (data->connection);
1416 data->connection = g_proxy_connect_finish (G_PROXY (object),
1417 result,
1418 &data->last_error);
1419 if (data->connection)
1421 g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_PROXY_NEGOTIATED, data->connectable, data->connection);
1423 else
1425 enumerator_next_async (data);
1426 return;
1429 g_socket_client_tls_handshake (data);
1432 static void
1433 g_socket_client_connected_callback (GObject *source,
1434 GAsyncResult *result,
1435 gpointer user_data)
1437 GSocketClientAsyncConnectData *data = user_data;
1438 GError *error = NULL;
1439 GProxy *proxy;
1440 const gchar *protocol;
1442 if (!g_socket_connection_connect_finish (G_SOCKET_CONNECTION (source),
1443 result, &error))
1445 clarify_connect_error (error, data->connectable,
1446 data->current_addr);
1447 set_last_error (data, error);
1449 /* try next one */
1450 enumerator_next_async (data);
1451 return;
1454 g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_CONNECTED, data->connectable, data->connection);
1456 /* wrong, but backward compatible */
1457 g_socket_set_blocking (data->current_socket, TRUE);
1459 if (!data->proxy_addr)
1461 g_socket_client_tls_handshake (data);
1462 return;
1465 protocol = g_proxy_address_get_protocol (data->proxy_addr);
1466 proxy = g_proxy_get_default_for_protocol (protocol);
1468 /* The connection should not be anything other than TCP,
1469 * but let's put a safety guard in case
1471 if (!G_IS_TCP_CONNECTION (data->connection))
1473 g_critical ("Trying to proxy over non-TCP connection, this is "
1474 "most likely a bug in GLib IO library.");
1476 g_set_error_literal (&data->last_error,
1477 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
1478 _("Proxying over a non-TCP connection is not supported."));
1480 enumerator_next_async (data);
1482 else if (proxy)
1484 g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_PROXY_NEGOTIATING, data->connectable, data->connection);
1485 g_proxy_connect_async (proxy,
1486 data->connection,
1487 data->proxy_addr,
1488 g_task_get_cancellable (data->task),
1489 g_socket_client_proxy_connect_callback,
1490 data);
1491 g_object_unref (proxy);
1493 else if (!g_hash_table_lookup_extended (data->client->priv->app_proxies,
1494 protocol, NULL, NULL))
1496 g_clear_error (&data->last_error);
1498 g_set_error (&data->last_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
1499 _("Proxy protocol '%s' is not supported."),
1500 protocol);
1502 enumerator_next_async (data);
1504 else
1506 /* Simply complete the connection, we don't want to do TLS handshake
1507 * as the application proxy handling may need proxy handshake first */
1508 g_socket_client_async_connect_complete (data);
1512 static void
1513 g_socket_client_enumerator_callback (GObject *object,
1514 GAsyncResult *result,
1515 gpointer user_data)
1517 GSocketClientAsyncConnectData *data = user_data;
1518 GSocketAddress *address = NULL;
1519 GSocket *socket;
1520 GError *error = NULL;
1522 if (g_task_return_error_if_cancelled (data->task))
1523 return;
1525 address = g_socket_address_enumerator_next_finish (data->enumerator,
1526 result, &error);
1527 if (address == NULL)
1529 g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_COMPLETE, data->connectable, NULL);
1530 if (!error)
1532 if (data->last_error)
1534 error = data->last_error;
1535 data->last_error = NULL;
1537 else
1539 g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_FAILED,
1540 _("Unknown error on connect"));
1543 g_task_return_error (data->task, error);
1544 g_object_unref (data->task);
1545 return;
1548 g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_RESOLVED,
1549 data->connectable, NULL);
1551 if (G_IS_PROXY_ADDRESS (address) &&
1552 data->client->priv->enable_proxy)
1553 data->proxy_addr = g_object_ref (G_PROXY_ADDRESS (address));
1555 g_clear_error (&data->last_error);
1557 socket = create_socket (data->client, address, &data->last_error);
1558 if (socket == NULL)
1560 g_object_unref (address);
1561 enumerator_next_async (data);
1562 return;
1565 data->current_socket = socket;
1566 data->current_addr = address;
1567 data->connection = (GIOStream *) g_socket_connection_factory_create_connection (socket);
1569 g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_CONNECTING, data->connectable, data->connection);
1570 g_socket_connection_connect_async (G_SOCKET_CONNECTION (data->connection),
1571 address,
1572 g_task_get_cancellable (data->task),
1573 g_socket_client_connected_callback, data);
1577 * g_socket_client_connect_async:
1578 * @client: a #GSocketClient
1579 * @connectable: a #GSocketConnectable specifying the remote address.
1580 * @cancellable: (allow-none): a #GCancellable, or %NULL
1581 * @callback: (scope async): a #GAsyncReadyCallback
1582 * @user_data: (closure): user data for the callback
1584 * This is the asynchronous version of g_socket_client_connect().
1586 * When the operation is finished @callback will be
1587 * called. You can then call g_socket_client_connect_finish() to get
1588 * the result of the operation.
1590 * Since: 2.22
1592 void
1593 g_socket_client_connect_async (GSocketClient *client,
1594 GSocketConnectable *connectable,
1595 GCancellable *cancellable,
1596 GAsyncReadyCallback callback,
1597 gpointer user_data)
1599 GSocketClientAsyncConnectData *data;
1601 g_return_if_fail (G_IS_SOCKET_CLIENT (client));
1603 data = g_slice_new0 (GSocketClientAsyncConnectData);
1604 data->client = client;
1605 data->connectable = g_object_ref (connectable);
1607 if (can_use_proxy (client))
1608 data->enumerator = g_socket_connectable_proxy_enumerate (connectable);
1609 else
1610 data->enumerator = g_socket_connectable_enumerate (connectable);
1612 data->task = g_task_new (client, cancellable, callback, user_data);
1613 g_task_set_task_data (data->task, data, (GDestroyNotify)g_socket_client_async_connect_data_free);
1615 enumerator_next_async (data);
1619 * g_socket_client_connect_to_host_async:
1620 * @client: a #GSocketClient
1621 * @host_and_port: the name and optionally the port of the host to connect to
1622 * @default_port: the default port to connect to
1623 * @cancellable: (allow-none): a #GCancellable, or %NULL
1624 * @callback: (scope async): a #GAsyncReadyCallback
1625 * @user_data: (closure): user data for the callback
1627 * This is the asynchronous version of g_socket_client_connect_to_host().
1629 * When the operation is finished @callback will be
1630 * called. You can then call g_socket_client_connect_to_host_finish() to get
1631 * the result of the operation.
1633 * Since: 2.22
1635 void
1636 g_socket_client_connect_to_host_async (GSocketClient *client,
1637 const gchar *host_and_port,
1638 guint16 default_port,
1639 GCancellable *cancellable,
1640 GAsyncReadyCallback callback,
1641 gpointer user_data)
1643 GSocketConnectable *connectable;
1644 GError *error;
1646 error = NULL;
1647 connectable = g_network_address_parse (host_and_port, default_port,
1648 &error);
1649 if (connectable == NULL)
1651 g_task_report_error (client, callback, user_data,
1652 g_socket_client_connect_to_host_async,
1653 error);
1655 else
1657 g_socket_client_connect_async (client,
1658 connectable, cancellable,
1659 callback, user_data);
1660 g_object_unref (connectable);
1665 * g_socket_client_connect_to_service_async:
1666 * @client: a #GSocketClient
1667 * @domain: a domain name
1668 * @service: the name of the service to connect to
1669 * @cancellable: (allow-none): a #GCancellable, or %NULL
1670 * @callback: (scope async): a #GAsyncReadyCallback
1671 * @user_data: (closure): user data for the callback
1673 * This is the asynchronous version of
1674 * g_socket_client_connect_to_service().
1676 * Since: 2.22
1678 void
1679 g_socket_client_connect_to_service_async (GSocketClient *client,
1680 const gchar *domain,
1681 const gchar *service,
1682 GCancellable *cancellable,
1683 GAsyncReadyCallback callback,
1684 gpointer user_data)
1686 GSocketConnectable *connectable;
1688 connectable = g_network_service_new (service, "tcp", domain);
1689 g_socket_client_connect_async (client,
1690 connectable, cancellable,
1691 callback, user_data);
1692 g_object_unref (connectable);
1696 * g_socket_client_connect_to_uri_async:
1697 * @client: a #GSocketClient
1698 * @uri: a network uri
1699 * @default_port: the default port to connect to
1700 * @cancellable: (allow-none): a #GCancellable, or %NULL
1701 * @callback: (scope async): a #GAsyncReadyCallback
1702 * @user_data: (closure): user data for the callback
1704 * This is the asynchronous version of g_socket_client_connect_to_uri().
1706 * When the operation is finished @callback will be
1707 * called. You can then call g_socket_client_connect_to_uri_finish() to get
1708 * the result of the operation.
1710 * Since: 2.26
1712 void
1713 g_socket_client_connect_to_uri_async (GSocketClient *client,
1714 const gchar *uri,
1715 guint16 default_port,
1716 GCancellable *cancellable,
1717 GAsyncReadyCallback callback,
1718 gpointer user_data)
1720 GSocketConnectable *connectable;
1721 GError *error;
1723 error = NULL;
1724 connectable = g_network_address_parse_uri (uri, default_port, &error);
1725 if (connectable == NULL)
1727 g_task_report_error (client, callback, user_data,
1728 g_socket_client_connect_to_uri_async,
1729 error);
1731 else
1733 g_socket_client_connect_async (client,
1734 connectable, cancellable,
1735 callback, user_data);
1736 g_object_unref (connectable);
1742 * g_socket_client_connect_finish:
1743 * @client: a #GSocketClient.
1744 * @result: a #GAsyncResult.
1745 * @error: a #GError location to store the error occurring, or %NULL to
1746 * ignore.
1748 * Finishes an async connect operation. See g_socket_client_connect_async()
1750 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1752 * Since: 2.22
1754 GSocketConnection *
1755 g_socket_client_connect_finish (GSocketClient *client,
1756 GAsyncResult *result,
1757 GError **error)
1759 g_return_val_if_fail (g_task_is_valid (result, client), NULL);
1761 return g_task_propagate_pointer (G_TASK (result), error);
1765 * g_socket_client_connect_to_host_finish:
1766 * @client: a #GSocketClient.
1767 * @result: a #GAsyncResult.
1768 * @error: a #GError location to store the error occurring, or %NULL to
1769 * ignore.
1771 * Finishes an async connect operation. See g_socket_client_connect_to_host_async()
1773 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1775 * Since: 2.22
1777 GSocketConnection *
1778 g_socket_client_connect_to_host_finish (GSocketClient *client,
1779 GAsyncResult *result,
1780 GError **error)
1782 return g_socket_client_connect_finish (client, result, error);
1786 * g_socket_client_connect_to_service_finish:
1787 * @client: a #GSocketClient.
1788 * @result: a #GAsyncResult.
1789 * @error: a #GError location to store the error occurring, or %NULL to
1790 * ignore.
1792 * Finishes an async connect operation. See g_socket_client_connect_to_service_async()
1794 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1796 * Since: 2.22
1798 GSocketConnection *
1799 g_socket_client_connect_to_service_finish (GSocketClient *client,
1800 GAsyncResult *result,
1801 GError **error)
1803 return g_socket_client_connect_finish (client, result, error);
1807 * g_socket_client_connect_to_uri_finish:
1808 * @client: a #GSocketClient.
1809 * @result: a #GAsyncResult.
1810 * @error: a #GError location to store the error occurring, or %NULL to
1811 * ignore.
1813 * Finishes an async connect operation. See g_socket_client_connect_to_uri_async()
1815 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1817 * Since: 2.26
1819 GSocketConnection *
1820 g_socket_client_connect_to_uri_finish (GSocketClient *client,
1821 GAsyncResult *result,
1822 GError **error)
1824 return g_socket_client_connect_finish (client, result, error);
1828 * g_socket_client_add_application_proxy:
1829 * @client: a #GSocketClient
1830 * @protocol: The proxy protocol
1832 * Enable proxy protocols to be handled by the application. When the
1833 * indicated proxy protocol is returned by the #GProxyResolver,
1834 * #GSocketClient will consider this protocol as supported but will
1835 * not try to find a #GProxy instance to handle handshaking. The
1836 * application must check for this case by calling
1837 * g_socket_connection_get_remote_address() on the returned
1838 * #GSocketConnection, and seeing if it's a #GProxyAddress of the
1839 * appropriate type, to determine whether or not it needs to handle
1840 * the proxy handshaking itself.
1842 * This should be used for proxy protocols that are dialects of
1843 * another protocol such as HTTP proxy. It also allows cohabitation of
1844 * proxy protocols that are reused between protocols. A good example
1845 * is HTTP. It can be used to proxy HTTP, FTP and Gopher and can also
1846 * be use as generic socket proxy through the HTTP CONNECT method.
1848 * When the proxy is detected as being an application proxy, TLS handshake
1849 * will be skipped. This is required to let the application do the proxy
1850 * specific handshake.
1852 void
1853 g_socket_client_add_application_proxy (GSocketClient *client,
1854 const gchar *protocol)
1856 g_hash_table_insert (client->priv->app_proxies, g_strdup (protocol), NULL);