Visual C++ projects: Clean/fix up
[glib.git] / gio / gsocketclient.c
blob63c26aa90561bf366c91252676ff05398313d847
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/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>
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 _("Trying to proxy over 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 GSimpleAsyncResult *result;
1275 GCancellable *cancellable;
1276 GSocketClient *client;
1278 GSocketConnectable *connectable;
1279 GSocketAddressEnumerator *enumerator;
1280 GProxyAddress *proxy_addr;
1281 GSocketAddress *current_addr;
1282 GSocket *current_socket;
1283 GIOStream *connection;
1285 GError *last_error;
1286 } GSocketClientAsyncConnectData;
1288 static void
1289 g_socket_client_async_connect_complete (GSocketClientAsyncConnectData *data)
1291 g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_COMPLETE, data->connectable, data->connection);
1293 if (data->last_error)
1295 g_simple_async_result_take_error (data->result, data->last_error);
1297 else
1299 g_assert (data->connection);
1301 if (!G_IS_SOCKET_CONNECTION (data->connection))
1303 GSocketConnection *wrapper_connection;
1305 wrapper_connection = g_tcp_wrapper_connection_new (data->connection,
1306 data->current_socket);
1307 g_object_unref (data->connection);
1308 data->connection = (GIOStream *)wrapper_connection;
1311 g_simple_async_result_set_op_res_gpointer (data->result,
1312 data->connection,
1313 g_object_unref);
1316 g_simple_async_result_complete (data->result);
1317 g_object_unref (data->result);
1318 g_object_unref (data->connectable);
1319 g_object_unref (data->enumerator);
1320 if (data->cancellable)
1321 g_object_unref (data->cancellable);
1322 if (data->current_addr)
1323 g_object_unref (data->current_addr);
1324 if (data->current_socket)
1325 g_object_unref (data->current_socket);
1326 if (data->proxy_addr)
1327 g_object_unref (data->proxy_addr);
1328 g_slice_free (GSocketClientAsyncConnectData, data);
1332 static void
1333 g_socket_client_enumerator_callback (GObject *object,
1334 GAsyncResult *result,
1335 gpointer user_data);
1337 static void
1338 set_last_error (GSocketClientAsyncConnectData *data,
1339 GError *error)
1341 g_clear_error (&data->last_error);
1342 data->last_error = error;
1345 static void
1346 enumerator_next_async (GSocketClientAsyncConnectData *data)
1348 /* We need to cleanup the state */
1349 g_clear_object (&data->current_socket);
1350 g_clear_object (&data->current_addr);
1351 g_clear_object (&data->proxy_addr);
1352 g_clear_object (&data->connection);
1354 g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_RESOLVING, data->connectable, NULL);
1355 g_socket_address_enumerator_next_async (data->enumerator,
1356 data->cancellable,
1357 g_socket_client_enumerator_callback,
1358 data);
1361 static void
1362 g_socket_client_tls_handshake_callback (GObject *object,
1363 GAsyncResult *result,
1364 gpointer user_data)
1366 GSocketClientAsyncConnectData *data = user_data;
1368 if (g_tls_connection_handshake_finish (G_TLS_CONNECTION (object),
1369 result,
1370 &data->last_error))
1372 g_object_unref (data->connection);
1373 data->connection = G_IO_STREAM (object);
1375 g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_TLS_HANDSHAKED, data->connectable, data->connection);
1376 g_socket_client_async_connect_complete (data);
1378 else
1380 g_object_unref (object);
1381 enumerator_next_async (data);
1385 static void
1386 g_socket_client_tls_handshake (GSocketClientAsyncConnectData *data)
1388 GIOStream *tlsconn;
1390 if (!data->client->priv->tls)
1392 g_socket_client_async_connect_complete (data);
1393 return;
1396 tlsconn = g_tls_client_connection_new (data->connection,
1397 data->connectable,
1398 &data->last_error);
1399 if (tlsconn)
1401 g_tls_client_connection_set_validation_flags (G_TLS_CLIENT_CONNECTION (tlsconn),
1402 data->client->priv->tls_validation_flags);
1403 g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_TLS_HANDSHAKING, data->connectable, G_IO_STREAM (tlsconn));
1404 g_tls_connection_handshake_async (G_TLS_CONNECTION (tlsconn),
1405 G_PRIORITY_DEFAULT,
1406 data->cancellable,
1407 g_socket_client_tls_handshake_callback,
1408 data);
1410 else
1412 enumerator_next_async (data);
1416 static void
1417 g_socket_client_proxy_connect_callback (GObject *object,
1418 GAsyncResult *result,
1419 gpointer user_data)
1421 GSocketClientAsyncConnectData *data = user_data;
1423 g_object_unref (data->connection);
1424 data->connection = g_proxy_connect_finish (G_PROXY (object),
1425 result,
1426 &data->last_error);
1427 if (data->connection)
1429 g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_PROXY_NEGOTIATED, data->connectable, data->connection);
1431 else
1433 enumerator_next_async (data);
1434 return;
1437 g_socket_client_tls_handshake (data);
1440 static void
1441 g_socket_client_connected_callback (GObject *source,
1442 GAsyncResult *result,
1443 gpointer user_data)
1445 GSocketClientAsyncConnectData *data = user_data;
1446 GError *error = NULL;
1447 GProxy *proxy;
1448 const gchar *protocol;
1450 if (!g_socket_connection_connect_finish (G_SOCKET_CONNECTION (source),
1451 result, &error))
1453 clarify_connect_error (error, data->connectable,
1454 data->current_addr);
1455 set_last_error (data, error);
1457 /* try next one */
1458 enumerator_next_async (data);
1459 return;
1462 g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_CONNECTED, data->connectable, data->connection);
1464 /* wrong, but backward compatible */
1465 g_socket_set_blocking (data->current_socket, TRUE);
1467 if (!data->proxy_addr)
1469 g_socket_client_tls_handshake (data);
1470 return;
1473 protocol = g_proxy_address_get_protocol (data->proxy_addr);
1474 proxy = g_proxy_get_default_for_protocol (protocol);
1476 /* The connection should not be anything other than TCP,
1477 * but let's put a safety guard in case
1479 if (!G_IS_TCP_CONNECTION (data->connection))
1481 g_critical ("Trying to proxy over non-TCP connection, this is "
1482 "most likely a bug in GLib IO library.");
1484 g_set_error_literal (&data->last_error,
1485 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
1486 _("Trying to proxy over non-TCP connection is not supported."));
1488 enumerator_next_async (data);
1490 else if (proxy)
1492 g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_PROXY_NEGOTIATING, data->connectable, data->connection);
1493 g_proxy_connect_async (proxy,
1494 data->connection,
1495 data->proxy_addr,
1496 data->cancellable,
1497 g_socket_client_proxy_connect_callback,
1498 data);
1499 g_object_unref (proxy);
1501 else if (!g_hash_table_lookup_extended (data->client->priv->app_proxies,
1502 protocol, NULL, NULL))
1504 g_clear_error (&data->last_error);
1506 g_set_error (&data->last_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
1507 _("Proxy protocol '%s' is not supported."),
1508 protocol);
1510 enumerator_next_async (data);
1512 else
1514 /* Simply complete the connection, we don't want to do TLS handshake
1515 * as the application proxy handling may need proxy handshake first */
1516 g_socket_client_async_connect_complete (data);
1520 static void
1521 g_socket_client_enumerator_callback (GObject *object,
1522 GAsyncResult *result,
1523 gpointer user_data)
1525 GSocketClientAsyncConnectData *data = user_data;
1526 GSocketAddress *address = NULL;
1527 GSocket *socket;
1528 GError *tmp_error = NULL;
1530 if (g_cancellable_is_cancelled (data->cancellable))
1532 g_clear_error (&data->last_error);
1533 g_cancellable_set_error_if_cancelled (data->cancellable, &data->last_error);
1534 g_socket_client_async_connect_complete (data);
1535 return;
1538 address = g_socket_address_enumerator_next_finish (data->enumerator,
1539 result, &tmp_error);
1541 if (address == NULL)
1543 if (tmp_error)
1544 set_last_error (data, tmp_error);
1545 else if (data->last_error == NULL)
1546 g_set_error_literal (&data->last_error, G_IO_ERROR, G_IO_ERROR_FAILED,
1547 _("Unknown error on connect"));
1549 g_socket_client_async_connect_complete (data);
1550 return;
1553 g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_RESOLVED,
1554 data->connectable, NULL);
1556 if (G_IS_PROXY_ADDRESS (address) &&
1557 data->client->priv->enable_proxy)
1558 data->proxy_addr = g_object_ref (G_PROXY_ADDRESS (address));
1560 g_clear_error (&data->last_error);
1562 socket = create_socket (data->client, address, &data->last_error);
1563 if (socket == NULL)
1565 g_object_unref (address);
1566 enumerator_next_async (data);
1567 return;
1570 data->current_socket = socket;
1571 data->current_addr = address;
1572 data->connection = (GIOStream *) g_socket_connection_factory_create_connection (socket);
1574 g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_CONNECTING, data->connectable, data->connection);
1575 g_socket_connection_connect_async (G_SOCKET_CONNECTION (data->connection),
1576 address, data->cancellable,
1577 g_socket_client_connected_callback, data);
1581 * g_socket_client_connect_async:
1582 * @client: a #GSocketClient
1583 * @connectable: a #GSocketConnectable specifying the remote address.
1584 * @cancellable: (allow-none): a #GCancellable, or %NULL
1585 * @callback: (scope async): a #GAsyncReadyCallback
1586 * @user_data: (closure): user data for the callback
1588 * This is the asynchronous version of g_socket_client_connect().
1590 * When the operation is finished @callback will be
1591 * called. You can then call g_socket_client_connect_finish() to get
1592 * the result of the operation.
1594 * Since: 2.22
1596 void
1597 g_socket_client_connect_async (GSocketClient *client,
1598 GSocketConnectable *connectable,
1599 GCancellable *cancellable,
1600 GAsyncReadyCallback callback,
1601 gpointer user_data)
1603 GSocketClientAsyncConnectData *data;
1605 g_return_if_fail (G_IS_SOCKET_CLIENT (client));
1607 data = g_slice_new0 (GSocketClientAsyncConnectData);
1609 data->result = g_simple_async_result_new (G_OBJECT (client),
1610 callback, user_data,
1611 g_socket_client_connect_async);
1612 data->client = client;
1613 if (cancellable)
1614 data->cancellable = g_object_ref (cancellable);
1615 else
1616 data->cancellable = NULL;
1617 data->last_error = NULL;
1618 data->connectable = g_object_ref (connectable);
1620 if (can_use_proxy (client))
1621 data->enumerator = g_socket_connectable_proxy_enumerate (connectable);
1622 else
1623 data->enumerator = g_socket_connectable_enumerate (connectable);
1625 enumerator_next_async (data);
1629 * g_socket_client_connect_to_host_async:
1630 * @client: a #GSocketClient
1631 * @host_and_port: the name and optionally the port of the host to connect to
1632 * @default_port: the default port to connect to
1633 * @cancellable: (allow-none): a #GCancellable, or %NULL
1634 * @callback: (scope async): a #GAsyncReadyCallback
1635 * @user_data: (closure): user data for the callback
1637 * This is the asynchronous version of g_socket_client_connect_to_host().
1639 * When the operation is finished @callback will be
1640 * called. You can then call g_socket_client_connect_to_host_finish() to get
1641 * the result of the operation.
1643 * Since: 2.22
1645 void
1646 g_socket_client_connect_to_host_async (GSocketClient *client,
1647 const gchar *host_and_port,
1648 guint16 default_port,
1649 GCancellable *cancellable,
1650 GAsyncReadyCallback callback,
1651 gpointer user_data)
1653 GSocketConnectable *connectable;
1654 GError *error;
1656 error = NULL;
1657 connectable = g_network_address_parse (host_and_port, default_port,
1658 &error);
1659 if (connectable == NULL)
1661 g_simple_async_report_take_gerror_in_idle (G_OBJECT (client),
1662 callback, user_data, error);
1664 else
1666 g_socket_client_connect_async (client,
1667 connectable, cancellable,
1668 callback, user_data);
1669 g_object_unref (connectable);
1674 * g_socket_client_connect_to_service_async:
1675 * @client: a #GSocketClient
1676 * @domain: a domain name
1677 * @service: the name of the service to connect to
1678 * @cancellable: (allow-none): a #GCancellable, or %NULL
1679 * @callback: (scope async): a #GAsyncReadyCallback
1680 * @user_data: (closure): user data for the callback
1682 * This is the asynchronous version of
1683 * g_socket_client_connect_to_service().
1685 * Since: 2.22
1687 void
1688 g_socket_client_connect_to_service_async (GSocketClient *client,
1689 const gchar *domain,
1690 const gchar *service,
1691 GCancellable *cancellable,
1692 GAsyncReadyCallback callback,
1693 gpointer user_data)
1695 GSocketConnectable *connectable;
1697 connectable = g_network_service_new (service, "tcp", domain);
1698 g_socket_client_connect_async (client,
1699 connectable, cancellable,
1700 callback, user_data);
1701 g_object_unref (connectable);
1705 * g_socket_client_connect_to_uri_async:
1706 * @client: a #GSocketClient
1707 * @uri: a network uri
1708 * @default_port: the default port to connect to
1709 * @cancellable: (allow-none): a #GCancellable, or %NULL
1710 * @callback: (scope async): a #GAsyncReadyCallback
1711 * @user_data: (closure): user data for the callback
1713 * This is the asynchronous version of g_socket_client_connect_to_uri().
1715 * When the operation is finished @callback will be
1716 * called. You can then call g_socket_client_connect_to_uri_finish() to get
1717 * the result of the operation.
1719 * Since: 2.26
1721 void
1722 g_socket_client_connect_to_uri_async (GSocketClient *client,
1723 const gchar *uri,
1724 guint16 default_port,
1725 GCancellable *cancellable,
1726 GAsyncReadyCallback callback,
1727 gpointer user_data)
1729 GSocketConnectable *connectable;
1730 GError *error;
1732 error = NULL;
1733 connectable = g_network_address_parse_uri (uri, default_port, &error);
1734 if (connectable == NULL)
1736 g_simple_async_report_take_gerror_in_idle (G_OBJECT (client),
1737 callback, user_data, error);
1739 else
1741 g_socket_client_connect_async (client,
1742 connectable, cancellable,
1743 callback, user_data);
1744 g_object_unref (connectable);
1750 * g_socket_client_connect_finish:
1751 * @client: a #GSocketClient.
1752 * @result: a #GAsyncResult.
1753 * @error: a #GError location to store the error occurring, or %NULL to
1754 * ignore.
1756 * Finishes an async connect operation. See g_socket_client_connect_async()
1758 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1760 * Since: 2.22
1762 GSocketConnection *
1763 g_socket_client_connect_finish (GSocketClient *client,
1764 GAsyncResult *result,
1765 GError **error)
1767 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1769 if (g_simple_async_result_propagate_error (simple, error))
1770 return NULL;
1772 return g_object_ref (g_simple_async_result_get_op_res_gpointer (simple));
1776 * g_socket_client_connect_to_host_finish:
1777 * @client: a #GSocketClient.
1778 * @result: a #GAsyncResult.
1779 * @error: a #GError location to store the error occurring, or %NULL to
1780 * ignore.
1782 * Finishes an async connect operation. See g_socket_client_connect_to_host_async()
1784 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1786 * Since: 2.22
1788 GSocketConnection *
1789 g_socket_client_connect_to_host_finish (GSocketClient *client,
1790 GAsyncResult *result,
1791 GError **error)
1793 return g_socket_client_connect_finish (client, result, error);
1797 * g_socket_client_connect_to_service_finish:
1798 * @client: a #GSocketClient.
1799 * @result: a #GAsyncResult.
1800 * @error: a #GError location to store the error occurring, or %NULL to
1801 * ignore.
1803 * Finishes an async connect operation. See g_socket_client_connect_to_service_async()
1805 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1807 * Since: 2.22
1809 GSocketConnection *
1810 g_socket_client_connect_to_service_finish (GSocketClient *client,
1811 GAsyncResult *result,
1812 GError **error)
1814 return g_socket_client_connect_finish (client, result, error);
1818 * g_socket_client_connect_to_uri_finish:
1819 * @client: a #GSocketClient.
1820 * @result: a #GAsyncResult.
1821 * @error: a #GError location to store the error occurring, or %NULL to
1822 * ignore.
1824 * Finishes an async connect operation. See g_socket_client_connect_to_uri_async()
1826 * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1828 * Since: 2.26
1830 GSocketConnection *
1831 g_socket_client_connect_to_uri_finish (GSocketClient *client,
1832 GAsyncResult *result,
1833 GError **error)
1835 return g_socket_client_connect_finish (client, result, error);
1839 * g_socket_client_add_application_proxy:
1840 * @client: a #GSocketClient
1841 * @protocol: The proxy protocol
1843 * Enable proxy protocols to be handled by the application. When the
1844 * indicated proxy protocol is returned by the #GProxyResolver,
1845 * #GSocketClient will consider this protocol as supported but will
1846 * not try to find a #GProxy instance to handle handshaking. The
1847 * application must check for this case by calling
1848 * g_socket_connection_get_remote_address() on the returned
1849 * #GSocketConnection, and seeing if it's a #GProxyAddress of the
1850 * appropriate type, to determine whether or not it needs to handle
1851 * the proxy handshaking itself.
1853 * This should be used for proxy protocols that are dialects of
1854 * another protocol such as HTTP proxy. It also allows cohabitation of
1855 * proxy protocols that are reused between protocols. A good example
1856 * is HTTP. It can be used to proxy HTTP, FTP and Gopher and can also
1857 * be use as generic socket proxy through the HTTP CONNECT method.
1859 * When the proxy is detected as being an application proxy, TLS handshake
1860 * will be skipped. This is required to let the application do the proxy
1861 * specific handshake.
1863 void
1864 g_socket_client_add_application_proxy (GSocketClient *client,
1865 const gchar *protocol)
1867 g_hash_table_insert (client->priv->app_proxies, g_strdup (protocol), NULL);