Imported Upstream version 2.60.0
[platform/upstream/glib.git] / gio / gsocketclient.c
1 /*  GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright © 2008, 2009 codethink
4  * Copyright © 2009 Red Hat, Inc
5  * Copyright © 2018 Igalia S.L.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General
18  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19  *
20  * Authors: Ryan Lortie <desrt@desrt.ca>
21  *          Alexander Larsson <alexl@redhat.com>
22  */
23
24 #include "config.h"
25 #include "gsocketclient.h"
26
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <gio/gioenumtypes.h>
31 #include <gio/gsocketaddressenumerator.h>
32 #include <gio/gsocketconnectable.h>
33 #include <gio/gsocketconnection.h>
34 #include <gio/gioprivate.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/gproxyresolver.h>
45 #include <gio/gsocketaddress.h>
46 #include <gio/gtcpconnection.h>
47 #include <gio/gtcpwrapperconnection.h>
48 #include <gio/gtlscertificate.h>
49 #include <gio/gtlsclientconnection.h>
50 #include <gio/ginetaddress.h>
51 #include "glibintl.h"
52
53 /* As recommended by RFC 8305 this is the time it waits
54  * on a connection before starting another concurrent attempt.
55  */
56 #define HAPPY_EYEBALLS_CONNECTION_ATTEMPT_TIMEOUT_MS 250
57
58 /**
59  * SECTION:gsocketclient
60  * @short_description: Helper for connecting to a network service
61  * @include: gio/gio.h
62  * @see_also: #GSocketConnection, #GSocketListener
63  *
64  * #GSocketClient is a lightweight high-level utility class for connecting to
65  * a network host using a connection oriented socket type.
66  *
67  * You create a #GSocketClient object, set any options you want, and then
68  * call a sync or async connect operation, which returns a #GSocketConnection
69  * subclass on success.
70  *
71  * The type of the #GSocketConnection object returned depends on the type of
72  * the underlying socket that is in use. For instance, for a TCP/IP connection
73  * it will be a #GTcpConnection.
74  *
75  * As #GSocketClient is a lightweight object, you don't need to cache it. You
76  * can just create a new one any time you need one.
77  *
78  * Since: 2.22
79  */
80
81
82 enum
83 {
84   EVENT,
85   LAST_SIGNAL
86 };
87
88 static guint signals[LAST_SIGNAL] = { 0 };
89
90 enum
91 {
92   PROP_NONE,
93   PROP_FAMILY,
94   PROP_TYPE,
95   PROP_PROTOCOL,
96   PROP_LOCAL_ADDRESS,
97   PROP_TIMEOUT,
98   PROP_ENABLE_PROXY,
99   PROP_TLS,
100   PROP_TLS_VALIDATION_FLAGS,
101   PROP_PROXY_RESOLVER
102 };
103
104 struct _GSocketClientPrivate
105 {
106   GSocketFamily family;
107   GSocketType type;
108   GSocketProtocol protocol;
109   GSocketAddress *local_address;
110   guint timeout;
111   gboolean enable_proxy;
112   GHashTable *app_proxies;
113   gboolean tls;
114   GTlsCertificateFlags tls_validation_flags;
115   GProxyResolver *proxy_resolver;
116 };
117
118 G_DEFINE_TYPE_WITH_PRIVATE (GSocketClient, g_socket_client, G_TYPE_OBJECT)
119
120 static GSocket *
121 create_socket (GSocketClient  *client,
122                GSocketAddress *dest_address,
123                GError        **error)
124 {
125   GSocketFamily family;
126   GSocket *socket;
127
128   family = client->priv->family;
129   if (family == G_SOCKET_FAMILY_INVALID &&
130       client->priv->local_address != NULL)
131     family = g_socket_address_get_family (client->priv->local_address);
132   if (family == G_SOCKET_FAMILY_INVALID)
133     family = g_socket_address_get_family (dest_address);
134
135   socket = g_socket_new (family,
136                          client->priv->type,
137                          client->priv->protocol,
138                          error);
139   if (socket == NULL)
140     return NULL;
141
142   if (client->priv->local_address)
143     {
144       if (!g_socket_bind (socket,
145                           client->priv->local_address,
146                           FALSE,
147                           error))
148         {
149           g_object_unref (socket);
150           return NULL;
151         }
152     }
153
154   if (client->priv->timeout)
155     g_socket_set_timeout (socket, client->priv->timeout);
156
157   return socket;
158 }
159
160 static gboolean
161 can_use_proxy (GSocketClient *client)
162 {
163   GSocketClientPrivate *priv = client->priv;
164
165   return priv->enable_proxy
166           && priv->type == G_SOCKET_TYPE_STREAM;
167 }
168
169 static void
170 clarify_connect_error (GError             *error,
171                        GSocketConnectable *connectable,
172                        GSocketAddress     *address)
173 {
174   const char *name;
175   char *tmp_name = NULL;
176
177   if (G_IS_PROXY_ADDRESS (address))
178     {
179       name = tmp_name = g_inet_address_to_string (g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address)));
180
181       g_prefix_error (&error, _("Could not connect to proxy server %s: "), name);
182     }
183   else
184     {
185       if (G_IS_NETWORK_ADDRESS (connectable))
186         name = g_network_address_get_hostname (G_NETWORK_ADDRESS (connectable));
187       else if (G_IS_NETWORK_SERVICE (connectable))
188         name = g_network_service_get_domain (G_NETWORK_SERVICE (connectable));
189       else if (G_IS_INET_SOCKET_ADDRESS (connectable))
190         name = tmp_name = g_inet_address_to_string (g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (connectable)));
191       else
192         name = NULL;
193
194       if (name)
195         g_prefix_error (&error, _("Could not connect to %s: "), name);
196       else
197         g_prefix_error (&error, _("Could not connect: "));
198     }
199
200   g_free (tmp_name);
201 }
202
203 static void
204 g_socket_client_init (GSocketClient *client)
205 {
206   client->priv = g_socket_client_get_instance_private (client);
207   client->priv->type = G_SOCKET_TYPE_STREAM;
208   client->priv->app_proxies = g_hash_table_new_full (g_str_hash,
209                                                      g_str_equal,
210                                                      g_free,
211                                                      NULL);
212 }
213
214 /**
215  * g_socket_client_new:
216  *
217  * Creates a new #GSocketClient with the default options.
218  *
219  * Returns: a #GSocketClient.
220  *     Free the returned object with g_object_unref().
221  *
222  * Since: 2.22
223  */
224 GSocketClient *
225 g_socket_client_new (void)
226 {
227   return g_object_new (G_TYPE_SOCKET_CLIENT, NULL);
228 }
229
230 static void
231 g_socket_client_finalize (GObject *object)
232 {
233   GSocketClient *client = G_SOCKET_CLIENT (object);
234
235   g_clear_object (&client->priv->local_address);
236   g_clear_object (&client->priv->proxy_resolver);
237
238   G_OBJECT_CLASS (g_socket_client_parent_class)->finalize (object);
239
240   g_hash_table_unref (client->priv->app_proxies);
241 }
242
243 static void
244 g_socket_client_get_property (GObject    *object,
245                               guint       prop_id,
246                               GValue     *value,
247                               GParamSpec *pspec)
248 {
249   GSocketClient *client = G_SOCKET_CLIENT (object);
250
251   switch (prop_id)
252     {
253       case PROP_FAMILY:
254         g_value_set_enum (value, client->priv->family);
255         break;
256
257       case PROP_TYPE:
258         g_value_set_enum (value, client->priv->type);
259         break;
260
261       case PROP_PROTOCOL:
262         g_value_set_enum (value, client->priv->protocol);
263         break;
264
265       case PROP_LOCAL_ADDRESS:
266         g_value_set_object (value, client->priv->local_address);
267         break;
268
269       case PROP_TIMEOUT:
270         g_value_set_uint (value, client->priv->timeout);
271         break;
272
273       case PROP_ENABLE_PROXY:
274         g_value_set_boolean (value, client->priv->enable_proxy);
275         break;
276
277       case PROP_TLS:
278         g_value_set_boolean (value, g_socket_client_get_tls (client));
279         break;
280
281       case PROP_TLS_VALIDATION_FLAGS:
282         g_value_set_flags (value, g_socket_client_get_tls_validation_flags (client));
283         break;
284
285       case PROP_PROXY_RESOLVER:
286         g_value_set_object (value, g_socket_client_get_proxy_resolver (client));
287         break;
288
289       default:
290         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
291     }
292 }
293
294 static void
295 g_socket_client_set_property (GObject      *object,
296                               guint         prop_id,
297                               const GValue *value,
298                               GParamSpec   *pspec)
299 {
300   GSocketClient *client = G_SOCKET_CLIENT (object);
301
302   switch (prop_id)
303     {
304     case PROP_FAMILY:
305       g_socket_client_set_family (client, g_value_get_enum (value));
306       break;
307
308     case PROP_TYPE:
309       g_socket_client_set_socket_type (client, g_value_get_enum (value));
310       break;
311
312     case PROP_PROTOCOL:
313       g_socket_client_set_protocol (client, g_value_get_enum (value));
314       break;
315
316     case PROP_LOCAL_ADDRESS:
317       g_socket_client_set_local_address (client, g_value_get_object (value));
318       break;
319
320     case PROP_TIMEOUT:
321       g_socket_client_set_timeout (client, g_value_get_uint (value));
322       break;
323
324     case PROP_ENABLE_PROXY:
325       g_socket_client_set_enable_proxy (client, g_value_get_boolean (value));
326       break;
327
328     case PROP_TLS:
329       g_socket_client_set_tls (client, g_value_get_boolean (value));
330       break;
331
332     case PROP_TLS_VALIDATION_FLAGS:
333       g_socket_client_set_tls_validation_flags (client, g_value_get_flags (value));
334       break;
335
336     case PROP_PROXY_RESOLVER:
337       g_socket_client_set_proxy_resolver (client, g_value_get_object (value));
338       break;
339
340     default:
341       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
342     }
343 }
344
345 /**
346  * g_socket_client_get_family:
347  * @client: a #GSocketClient.
348  *
349  * Gets the socket family of the socket client.
350  *
351  * See g_socket_client_set_family() for details.
352  *
353  * Returns: a #GSocketFamily
354  *
355  * Since: 2.22
356  */
357 GSocketFamily
358 g_socket_client_get_family (GSocketClient *client)
359 {
360   return client->priv->family;
361 }
362
363 /**
364  * g_socket_client_set_family:
365  * @client: a #GSocketClient.
366  * @family: a #GSocketFamily
367  *
368  * Sets the socket family of the socket client.
369  * If this is set to something other than %G_SOCKET_FAMILY_INVALID
370  * then the sockets created by this object will be of the specified
371  * family.
372  *
373  * This might be useful for instance if you want to force the local
374  * connection to be an ipv4 socket, even though the address might
375  * be an ipv6 mapped to ipv4 address.
376  *
377  * Since: 2.22
378  */
379 void
380 g_socket_client_set_family (GSocketClient *client,
381                             GSocketFamily  family)
382 {
383   if (client->priv->family == family)
384     return;
385
386   client->priv->family = family;
387   g_object_notify (G_OBJECT (client), "family");
388 }
389
390 /**
391  * g_socket_client_get_socket_type:
392  * @client: a #GSocketClient.
393  *
394  * Gets the socket type of the socket client.
395  *
396  * See g_socket_client_set_socket_type() for details.
397  *
398  * Returns: a #GSocketFamily
399  *
400  * Since: 2.22
401  */
402 GSocketType
403 g_socket_client_get_socket_type (GSocketClient *client)
404 {
405   return client->priv->type;
406 }
407
408 /**
409  * g_socket_client_set_socket_type:
410  * @client: a #GSocketClient.
411  * @type: a #GSocketType
412  *
413  * Sets the socket type of the socket client.
414  * The sockets created by this object will be of the specified
415  * type.
416  *
417  * It doesn't make sense to specify a type of %G_SOCKET_TYPE_DATAGRAM,
418  * as GSocketClient is used for connection oriented services.
419  *
420  * Since: 2.22
421  */
422 void
423 g_socket_client_set_socket_type (GSocketClient *client,
424                                  GSocketType    type)
425 {
426   if (client->priv->type == type)
427     return;
428
429   client->priv->type = type;
430   g_object_notify (G_OBJECT (client), "type");
431 }
432
433 /**
434  * g_socket_client_get_protocol:
435  * @client: a #GSocketClient
436  *
437  * Gets the protocol name type of the socket client.
438  *
439  * See g_socket_client_set_protocol() for details.
440  *
441  * Returns: a #GSocketProtocol
442  *
443  * Since: 2.22
444  */
445 GSocketProtocol
446 g_socket_client_get_protocol (GSocketClient *client)
447 {
448   return client->priv->protocol;
449 }
450
451 /**
452  * g_socket_client_set_protocol:
453  * @client: a #GSocketClient.
454  * @protocol: a #GSocketProtocol
455  *
456  * Sets the protocol of the socket client.
457  * The sockets created by this object will use of the specified
458  * protocol.
459  *
460  * If @protocol is %0 that means to use the default
461  * protocol for the socket family and type.
462  *
463  * Since: 2.22
464  */
465 void
466 g_socket_client_set_protocol (GSocketClient   *client,
467                               GSocketProtocol  protocol)
468 {
469   if (client->priv->protocol == protocol)
470     return;
471
472   client->priv->protocol = protocol;
473   g_object_notify (G_OBJECT (client), "protocol");
474 }
475
476 /**
477  * g_socket_client_get_local_address:
478  * @client: a #GSocketClient.
479  *
480  * Gets the local address of the socket client.
481  *
482  * See g_socket_client_set_local_address() for details.
483  *
484  * Returns: (transfer none): a #GSocketAddress or %NULL. Do not free.
485  *
486  * Since: 2.22
487  */
488 GSocketAddress *
489 g_socket_client_get_local_address (GSocketClient *client)
490 {
491   return client->priv->local_address;
492 }
493
494 /**
495  * g_socket_client_set_local_address:
496  * @client: a #GSocketClient.
497  * @address: (nullable): a #GSocketAddress, or %NULL
498  *
499  * Sets the local address of the socket client.
500  * The sockets created by this object will bound to the
501  * specified address (if not %NULL) before connecting.
502  *
503  * This is useful if you want to ensure that the local
504  * side of the connection is on a specific port, or on
505  * a specific interface.
506  *
507  * Since: 2.22
508  */
509 void
510 g_socket_client_set_local_address (GSocketClient  *client,
511                                    GSocketAddress *address)
512 {
513   if (address)
514     g_object_ref (address);
515
516   if (client->priv->local_address)
517     {
518       g_object_unref (client->priv->local_address);
519     }
520   client->priv->local_address = address;
521   g_object_notify (G_OBJECT (client), "local-address");
522 }
523
524 /**
525  * g_socket_client_get_timeout:
526  * @client: a #GSocketClient
527  *
528  * Gets the I/O timeout time for sockets created by @client.
529  *
530  * See g_socket_client_set_timeout() for details.
531  *
532  * Returns: the timeout in seconds
533  *
534  * Since: 2.26
535  */
536 guint
537 g_socket_client_get_timeout (GSocketClient *client)
538 {
539   return client->priv->timeout;
540 }
541
542
543 /**
544  * g_socket_client_set_timeout:
545  * @client: a #GSocketClient.
546  * @timeout: the timeout
547  *
548  * Sets the I/O timeout for sockets created by @client. @timeout is a
549  * time in seconds, or 0 for no timeout (the default).
550  *
551  * The timeout value affects the initial connection attempt as well,
552  * so setting this may cause calls to g_socket_client_connect(), etc,
553  * to fail with %G_IO_ERROR_TIMED_OUT.
554  *
555  * Since: 2.26
556  */
557 void
558 g_socket_client_set_timeout (GSocketClient *client,
559                              guint          timeout)
560 {
561   if (client->priv->timeout == timeout)
562     return;
563
564   client->priv->timeout = timeout;
565   g_object_notify (G_OBJECT (client), "timeout");
566 }
567
568 /**
569  * g_socket_client_get_enable_proxy:
570  * @client: a #GSocketClient.
571  *
572  * Gets the proxy enable state; see g_socket_client_set_enable_proxy()
573  *
574  * Returns: whether proxying is enabled
575  *
576  * Since: 2.26
577  */
578 gboolean
579 g_socket_client_get_enable_proxy (GSocketClient *client)
580 {
581   return client->priv->enable_proxy;
582 }
583
584 /**
585  * g_socket_client_set_enable_proxy:
586  * @client: a #GSocketClient.
587  * @enable: whether to enable proxies
588  *
589  * Sets whether or not @client attempts to make connections via a
590  * proxy server. When enabled (the default), #GSocketClient will use a
591  * #GProxyResolver to determine if a proxy protocol such as SOCKS is
592  * needed, and automatically do the necessary proxy negotiation.
593  *
594  * See also g_socket_client_set_proxy_resolver().
595  *
596  * Since: 2.26
597  */
598 void
599 g_socket_client_set_enable_proxy (GSocketClient *client,
600                                   gboolean       enable)
601 {
602   enable = !!enable;
603   if (client->priv->enable_proxy == enable)
604     return;
605
606   client->priv->enable_proxy = enable;
607   g_object_notify (G_OBJECT (client), "enable-proxy");
608 }
609
610 /**
611  * g_socket_client_get_tls:
612  * @client: a #GSocketClient.
613  *
614  * Gets whether @client creates TLS connections. See
615  * g_socket_client_set_tls() for details.
616  *
617  * Returns: whether @client uses TLS
618  *
619  * Since: 2.28
620  */
621 gboolean
622 g_socket_client_get_tls (GSocketClient *client)
623 {
624   return client->priv->tls;
625 }
626
627 /**
628  * g_socket_client_set_tls:
629  * @client: a #GSocketClient.
630  * @tls: whether to use TLS
631  *
632  * Sets whether @client creates TLS (aka SSL) connections. If @tls is
633  * %TRUE, @client will wrap its connections in a #GTlsClientConnection
634  * and perform a TLS handshake when connecting.
635  *
636  * Note that since #GSocketClient must return a #GSocketConnection,
637  * but #GTlsClientConnection is not a #GSocketConnection, this
638  * actually wraps the resulting #GTlsClientConnection in a
639  * #GTcpWrapperConnection when returning it. You can use
640  * g_tcp_wrapper_connection_get_base_io_stream() on the return value
641  * to extract the #GTlsClientConnection.
642  *
643  * If you need to modify the behavior of the TLS handshake (eg, by
644  * setting a client-side certificate to use, or connecting to the
645  * #GTlsConnection::accept-certificate signal), you can connect to
646  * @client's #GSocketClient::event signal and wait for it to be
647  * emitted with %G_SOCKET_CLIENT_TLS_HANDSHAKING, which will give you
648  * a chance to see the #GTlsClientConnection before the handshake
649  * starts.
650  *
651  * Since: 2.28
652  */
653 void
654 g_socket_client_set_tls (GSocketClient *client,
655                          gboolean       tls)
656 {
657   tls = !!tls;
658   if (tls == client->priv->tls)
659     return;
660
661   client->priv->tls = tls;
662   g_object_notify (G_OBJECT (client), "tls");
663 }
664
665 /**
666  * g_socket_client_get_tls_validation_flags:
667  * @client: a #GSocketClient.
668  *
669  * Gets the TLS validation flags used creating TLS connections via
670  * @client.
671  *
672  * Returns: the TLS validation flags
673  *
674  * Since: 2.28
675  */
676 GTlsCertificateFlags
677 g_socket_client_get_tls_validation_flags (GSocketClient *client)
678 {
679   return client->priv->tls_validation_flags;
680 }
681
682 /**
683  * g_socket_client_set_tls_validation_flags:
684  * @client: a #GSocketClient.
685  * @flags: the validation flags
686  *
687  * Sets the TLS validation flags used when creating TLS connections
688  * via @client. The default value is %G_TLS_CERTIFICATE_VALIDATE_ALL.
689  *
690  * Since: 2.28
691  */
692 void
693 g_socket_client_set_tls_validation_flags (GSocketClient        *client,
694                                           GTlsCertificateFlags  flags)
695 {
696   if (client->priv->tls_validation_flags != flags)
697     {
698       client->priv->tls_validation_flags = flags;
699       g_object_notify (G_OBJECT (client), "tls-validation-flags");
700     }
701 }
702
703 /**
704  * g_socket_client_get_proxy_resolver:
705  * @client: a #GSocketClient.
706  *
707  * Gets the #GProxyResolver being used by @client. Normally, this will
708  * be the resolver returned by g_proxy_resolver_get_default(), but you
709  * can override it with g_socket_client_set_proxy_resolver().
710  *
711  * Returns: (transfer none): The #GProxyResolver being used by
712  *   @client.
713  *
714  * Since: 2.36
715  */
716 GProxyResolver *
717 g_socket_client_get_proxy_resolver (GSocketClient *client)
718 {
719   if (client->priv->proxy_resolver)
720     return client->priv->proxy_resolver;
721   else
722     return g_proxy_resolver_get_default ();
723 }
724
725 /**
726  * g_socket_client_set_proxy_resolver:
727  * @client: a #GSocketClient.
728  * @proxy_resolver: (nullable): a #GProxyResolver, or %NULL for the
729  *   default.
730  *
731  * Overrides the #GProxyResolver used by @client. You can call this if
732  * you want to use specific proxies, rather than using the system
733  * default proxy settings.
734  *
735  * Note that whether or not the proxy resolver is actually used
736  * depends on the setting of #GSocketClient:enable-proxy, which is not
737  * changed by this function (but which is %TRUE by default)
738  *
739  * Since: 2.36
740  */
741 void
742 g_socket_client_set_proxy_resolver (GSocketClient  *client,
743                                     GProxyResolver *proxy_resolver)
744 {
745   /* We have to be careful to avoid calling
746    * g_proxy_resolver_get_default() until we're sure we need it,
747    * because trying to load the default proxy resolver module will
748    * break some test programs that aren't expecting it (eg,
749    * tests/gsettings).
750    */
751
752   if (client->priv->proxy_resolver)
753     g_object_unref (client->priv->proxy_resolver);
754
755   client->priv->proxy_resolver = proxy_resolver;
756
757   if (client->priv->proxy_resolver)
758     g_object_ref (client->priv->proxy_resolver);
759 }
760
761 static void
762 g_socket_client_class_init (GSocketClientClass *class)
763 {
764   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
765
766   gobject_class->finalize = g_socket_client_finalize;
767   gobject_class->set_property = g_socket_client_set_property;
768   gobject_class->get_property = g_socket_client_get_property;
769
770   /**
771    * GSocketClient::event:
772    * @client: the #GSocketClient
773    * @event: the event that is occurring
774    * @connectable: the #GSocketConnectable that @event is occurring on
775    * @connection: (nullable): the current representation of the connection
776    *
777    * Emitted when @client's activity on @connectable changes state.
778    * Among other things, this can be used to provide progress
779    * information about a network connection in the UI. The meanings of
780    * the different @event values are as follows:
781    *
782    * - %G_SOCKET_CLIENT_RESOLVING: @client is about to look up @connectable
783    *   in DNS. @connection will be %NULL.
784    *
785    * - %G_SOCKET_CLIENT_RESOLVED:  @client has successfully resolved
786    *   @connectable in DNS. @connection will be %NULL.
787    *
788    * - %G_SOCKET_CLIENT_CONNECTING: @client is about to make a connection
789    *   to a remote host; either a proxy server or the destination server
790    *   itself. @connection is the #GSocketConnection, which is not yet
791    *   connected.  Since GLib 2.40, you can access the remote
792    *   address via g_socket_connection_get_remote_address().
793    *
794    * - %G_SOCKET_CLIENT_CONNECTED: @client has successfully connected
795    *   to a remote host. @connection is the connected #GSocketConnection.
796    *
797    * - %G_SOCKET_CLIENT_PROXY_NEGOTIATING: @client is about to negotiate
798    *   with a proxy to get it to connect to @connectable. @connection is
799    *   the #GSocketConnection to the proxy server.
800    *
801    * - %G_SOCKET_CLIENT_PROXY_NEGOTIATED: @client has negotiated a
802    *   connection to @connectable through a proxy server. @connection is
803    *   the stream returned from g_proxy_connect(), which may or may not
804    *   be a #GSocketConnection.
805    *
806    * - %G_SOCKET_CLIENT_TLS_HANDSHAKING: @client is about to begin a TLS
807    *   handshake. @connection is a #GTlsClientConnection.
808    *
809    * - %G_SOCKET_CLIENT_TLS_HANDSHAKED: @client has successfully completed
810    *   the TLS handshake. @connection is a #GTlsClientConnection.
811    *
812    * - %G_SOCKET_CLIENT_COMPLETE: @client has either successfully connected
813    *   to @connectable (in which case @connection is the #GSocketConnection
814    *   that it will be returning to the caller) or has failed (in which
815    *   case @connection is %NULL and the client is about to return an error).
816    *
817    * Each event except %G_SOCKET_CLIENT_COMPLETE may be emitted
818    * multiple times (or not at all) for a given connectable (in
819    * particular, if @client ends up attempting to connect to more than
820    * one address). However, if @client emits the #GSocketClient::event
821    * signal at all for a given connectable, that it will always emit
822    * it with %G_SOCKET_CLIENT_COMPLETE when it is done.
823    *
824    * Note that there may be additional #GSocketClientEvent values in
825    * the future; unrecognized @event values should be ignored.
826    *
827    * Since: 2.32
828    */
829   signals[EVENT] =
830     g_signal_new (I_("event"),
831                   G_TYPE_FROM_CLASS (gobject_class),
832                   G_SIGNAL_RUN_LAST,
833                   G_STRUCT_OFFSET (GSocketClientClass, event),
834                   NULL, NULL,
835                   NULL,
836                   G_TYPE_NONE, 3,
837                   G_TYPE_SOCKET_CLIENT_EVENT,
838                   G_TYPE_SOCKET_CONNECTABLE,
839                   G_TYPE_IO_STREAM);
840
841   g_object_class_install_property (gobject_class, PROP_FAMILY,
842                                    g_param_spec_enum ("family",
843                                                       P_("Socket family"),
844                                                       P_("The sockets address family to use for socket construction"),
845                                                       G_TYPE_SOCKET_FAMILY,
846                                                       G_SOCKET_FAMILY_INVALID,
847                                                       G_PARAM_CONSTRUCT |
848                                                       G_PARAM_READWRITE |
849                                                       G_PARAM_STATIC_STRINGS));
850
851   g_object_class_install_property (gobject_class, PROP_TYPE,
852                                    g_param_spec_enum ("type",
853                                                       P_("Socket type"),
854                                                       P_("The sockets type to use for socket construction"),
855                                                       G_TYPE_SOCKET_TYPE,
856                                                       G_SOCKET_TYPE_STREAM,
857                                                       G_PARAM_CONSTRUCT |
858                                                       G_PARAM_READWRITE |
859                                                       G_PARAM_STATIC_STRINGS));
860
861   g_object_class_install_property (gobject_class, PROP_PROTOCOL,
862                                    g_param_spec_enum ("protocol",
863                                                       P_("Socket protocol"),
864                                                       P_("The protocol to use for socket construction, or 0 for default"),
865                                                       G_TYPE_SOCKET_PROTOCOL,
866                                                       G_SOCKET_PROTOCOL_DEFAULT,
867                                                       G_PARAM_CONSTRUCT |
868                                                       G_PARAM_READWRITE |
869                                                       G_PARAM_STATIC_STRINGS));
870
871   g_object_class_install_property (gobject_class, PROP_LOCAL_ADDRESS,
872                                    g_param_spec_object ("local-address",
873                                                         P_("Local address"),
874                                                         P_("The local address constructed sockets will be bound to"),
875                                                         G_TYPE_SOCKET_ADDRESS,
876                                                         G_PARAM_CONSTRUCT |
877                                                         G_PARAM_READWRITE |
878                                                         G_PARAM_STATIC_STRINGS));
879
880   g_object_class_install_property (gobject_class, PROP_TIMEOUT,
881                                    g_param_spec_uint ("timeout",
882                                                       P_("Socket timeout"),
883                                                       P_("The I/O timeout for sockets, or 0 for none"),
884                                                       0, G_MAXUINT, 0,
885                                                       G_PARAM_CONSTRUCT |
886                                                       G_PARAM_READWRITE |
887                                                       G_PARAM_STATIC_STRINGS));
888
889    g_object_class_install_property (gobject_class, PROP_ENABLE_PROXY,
890                                     g_param_spec_boolean ("enable-proxy",
891                                                           P_("Enable proxy"),
892                                                           P_("Enable proxy support"),
893                                                           TRUE,
894                                                           G_PARAM_CONSTRUCT |
895                                                           G_PARAM_READWRITE |
896                                                           G_PARAM_STATIC_STRINGS));
897
898   g_object_class_install_property (gobject_class, PROP_TLS,
899                                    g_param_spec_boolean ("tls",
900                                                          P_("TLS"),
901                                                          P_("Whether to create TLS connections"),
902                                                          FALSE,
903                                                          G_PARAM_CONSTRUCT |
904                                                          G_PARAM_READWRITE |
905                                                          G_PARAM_STATIC_STRINGS));
906   g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
907                                    g_param_spec_flags ("tls-validation-flags",
908                                                        P_("TLS validation flags"),
909                                                        P_("TLS validation flags to use"),
910                                                        G_TYPE_TLS_CERTIFICATE_FLAGS,
911                                                        G_TLS_CERTIFICATE_VALIDATE_ALL,
912                                                        G_PARAM_CONSTRUCT |
913                                                        G_PARAM_READWRITE |
914                                                        G_PARAM_STATIC_STRINGS));
915
916   /**
917    * GSocketClient:proxy-resolver:
918    *
919    * The proxy resolver to use
920    *
921    * Since: 2.36
922    */
923   g_object_class_install_property (gobject_class, PROP_PROXY_RESOLVER,
924                                    g_param_spec_object ("proxy-resolver",
925                                                         P_("Proxy resolver"),
926                                                         P_("The proxy resolver to use"),
927                                                         G_TYPE_PROXY_RESOLVER,
928                                                         G_PARAM_CONSTRUCT |
929                                                         G_PARAM_READWRITE |
930                                                         G_PARAM_STATIC_STRINGS));
931 }
932
933 static void
934 g_socket_client_emit_event (GSocketClient       *client,
935                             GSocketClientEvent  event,
936                             GSocketConnectable  *connectable,
937                             GIOStream           *connection)
938 {
939   g_signal_emit (client, signals[EVENT], 0,
940                  event, connectable, connection);
941 }
942
943 /**
944  * g_socket_client_connect:
945  * @client: a #GSocketClient.
946  * @connectable: a #GSocketConnectable specifying the remote address.
947  * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
948  * @error: #GError for error reporting, or %NULL to ignore.
949  *
950  * Tries to resolve the @connectable and make a network connection to it.
951  *
952  * Upon a successful connection, a new #GSocketConnection is constructed
953  * and returned.  The caller owns this new object and must drop their
954  * reference to it when finished with it.
955  *
956  * The type of the #GSocketConnection object returned depends on the type of
957  * the underlying socket that is used. For instance, for a TCP/IP connection
958  * it will be a #GTcpConnection.
959  *
960  * The socket created will be the same family as the address that the
961  * @connectable resolves to, unless family is set with g_socket_client_set_family()
962  * or indirectly via g_socket_client_set_local_address(). The socket type
963  * defaults to %G_SOCKET_TYPE_STREAM but can be set with
964  * g_socket_client_set_socket_type().
965  *
966  * If a local address is specified with g_socket_client_set_local_address() the
967  * socket will be bound to this address before connecting.
968  *
969  * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
970  *
971  * Since: 2.22
972  */
973 GSocketConnection *
974 g_socket_client_connect (GSocketClient       *client,
975                          GSocketConnectable  *connectable,
976                          GCancellable        *cancellable,
977                          GError             **error)
978 {
979   GIOStream *connection = NULL;
980   GSocketAddressEnumerator *enumerator = NULL;
981   GError *last_error, *tmp_error;
982
983   last_error = NULL;
984
985   if (can_use_proxy (client))
986     {
987       enumerator = g_socket_connectable_proxy_enumerate (connectable);
988       if (client->priv->proxy_resolver &&
989           G_IS_PROXY_ADDRESS_ENUMERATOR (enumerator))
990         {
991           g_object_set (G_OBJECT (enumerator),
992                         "proxy-resolver", client->priv->proxy_resolver,
993                         NULL);
994         }
995     }
996   else
997     enumerator = g_socket_connectable_enumerate (connectable);
998
999   while (connection == NULL)
1000     {
1001       GSocketAddress *address = NULL;
1002       gboolean application_proxy = FALSE;
1003       GSocket *socket;
1004       gboolean using_proxy;
1005
1006       if (g_cancellable_is_cancelled (cancellable))
1007         {
1008           g_clear_error (error);
1009           g_cancellable_set_error_if_cancelled (cancellable, error);
1010           break;
1011         }
1012
1013       tmp_error = NULL;
1014       g_socket_client_emit_event (client, G_SOCKET_CLIENT_RESOLVING,
1015                                   connectable, NULL);
1016       address = g_socket_address_enumerator_next (enumerator, cancellable,
1017                                                   &tmp_error);
1018
1019       if (address == NULL)
1020         {
1021           if (tmp_error)
1022             {
1023               g_clear_error (&last_error);
1024               g_propagate_error (error, tmp_error);
1025             }
1026           else if (last_error)
1027             {
1028               g_propagate_error (error, last_error);
1029             }
1030           else
1031             g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1032                                  _("Unknown error on connect"));
1033           break;
1034         }
1035       g_socket_client_emit_event (client, G_SOCKET_CLIENT_RESOLVED,
1036                                   connectable, NULL);
1037
1038       using_proxy = (G_IS_PROXY_ADDRESS (address) &&
1039                      client->priv->enable_proxy);
1040
1041       /* clear error from previous attempt */
1042       g_clear_error (&last_error);
1043
1044       socket = create_socket (client, address, &last_error);
1045       if (socket == NULL)
1046         {
1047           g_object_unref (address);
1048           continue;
1049         }
1050
1051       connection = (GIOStream *)g_socket_connection_factory_create_connection (socket);
1052       g_socket_connection_set_cached_remote_address ((GSocketConnection*)connection, address);
1053       g_socket_client_emit_event (client, G_SOCKET_CLIENT_CONNECTING, connectable, connection);
1054
1055       if (g_socket_connection_connect (G_SOCKET_CONNECTION (connection),
1056                                        address, cancellable, &last_error))
1057         {
1058           g_socket_connection_set_cached_remote_address ((GSocketConnection*)connection, NULL);
1059           g_socket_client_emit_event (client, G_SOCKET_CLIENT_CONNECTED, connectable, connection);
1060         }
1061       else
1062         {
1063           clarify_connect_error (last_error, connectable, address);
1064           g_object_unref (connection);
1065           connection = NULL;
1066         }
1067
1068       if (connection && using_proxy)
1069         {
1070           GProxyAddress *proxy_addr = G_PROXY_ADDRESS (address);
1071           const gchar *protocol;
1072           GProxy *proxy;
1073
1074           protocol = g_proxy_address_get_protocol (proxy_addr);
1075
1076           /* The connection should not be anything else then TCP Connection,
1077            * but let's put a safety guard in case
1078            */
1079           if (!G_IS_TCP_CONNECTION (connection))
1080             {
1081               g_critical ("Trying to proxy over non-TCP connection, this is "
1082                           "most likely a bug in GLib IO library.");
1083
1084               g_set_error_literal (&last_error,
1085                   G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
1086                   _("Proxying over a non-TCP connection is not supported."));
1087
1088               g_object_unref (connection);
1089               connection = NULL;
1090             }
1091           else if (g_hash_table_contains (client->priv->app_proxies, protocol))
1092             {
1093               application_proxy = TRUE;
1094             }
1095           else if ((proxy = g_proxy_get_default_for_protocol (protocol)))
1096             {
1097               GIOStream *proxy_connection;
1098
1099               g_socket_client_emit_event (client, G_SOCKET_CLIENT_PROXY_NEGOTIATING, connectable, connection);
1100               proxy_connection = g_proxy_connect (proxy,
1101                                                   connection,
1102                                                   proxy_addr,
1103                                                   cancellable,
1104                                                   &last_error);
1105               g_object_unref (connection);
1106               connection = proxy_connection;
1107               g_object_unref (proxy);
1108
1109               if (connection)
1110                 g_socket_client_emit_event (client, G_SOCKET_CLIENT_PROXY_NEGOTIATED, connectable, connection);
1111             }
1112           else
1113             {
1114               g_set_error (&last_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
1115                            _("Proxy protocol “%s” is not supported."),
1116                            protocol);
1117               g_object_unref (connection);
1118               connection = NULL;
1119             }
1120         }
1121
1122       if (!application_proxy && connection && client->priv->tls)
1123         {
1124           GIOStream *tlsconn;
1125
1126           tlsconn = g_tls_client_connection_new (connection, connectable, &last_error);
1127           g_object_unref (connection);
1128           connection = tlsconn;
1129
1130           if (tlsconn)
1131             {
1132               g_tls_client_connection_set_validation_flags (G_TLS_CLIENT_CONNECTION (tlsconn),
1133                                                             client->priv->tls_validation_flags);
1134               g_socket_client_emit_event (client, G_SOCKET_CLIENT_TLS_HANDSHAKING, connectable, connection);
1135               if (g_tls_connection_handshake (G_TLS_CONNECTION (tlsconn),
1136                                               cancellable, &last_error))
1137                 {
1138                   g_socket_client_emit_event (client, G_SOCKET_CLIENT_TLS_HANDSHAKED, connectable, connection);
1139                 }
1140               else
1141                 {
1142                   g_object_unref (tlsconn);
1143                   connection = NULL;
1144                 }
1145             }
1146         }
1147
1148       if (connection && !G_IS_SOCKET_CONNECTION (connection))
1149         {
1150           GSocketConnection *wrapper_connection;
1151
1152           wrapper_connection = g_tcp_wrapper_connection_new (connection, socket);
1153           g_object_unref (connection);
1154           connection = (GIOStream *)wrapper_connection;
1155         }
1156
1157       g_object_unref (socket);
1158       g_object_unref (address);
1159     }
1160   g_object_unref (enumerator);
1161
1162   g_socket_client_emit_event (client, G_SOCKET_CLIENT_COMPLETE, connectable, connection);
1163   return G_SOCKET_CONNECTION (connection);
1164 }
1165
1166 /**
1167  * g_socket_client_connect_to_host:
1168  * @client: a #GSocketClient
1169  * @host_and_port: the name and optionally port of the host to connect to
1170  * @default_port: the default port to connect to
1171  * @cancellable: (nullable): a #GCancellable, or %NULL
1172  * @error: a pointer to a #GError, or %NULL
1173  *
1174  * This is a helper function for g_socket_client_connect().
1175  *
1176  * Attempts to create a TCP connection to the named host.
1177  *
1178  * @host_and_port may be in any of a number of recognized formats; an IPv6
1179  * address, an IPv4 address, or a domain name (in which case a DNS
1180  * lookup is performed).  Quoting with [] is supported for all address
1181  * types.  A port override may be specified in the usual way with a
1182  * colon.  Ports may be given as decimal numbers or symbolic names (in
1183  * which case an /etc/services lookup is performed).
1184  *
1185  * If no port override is given in @host_and_port then @default_port will be
1186  * used as the port number to connect to.
1187  *
1188  * In general, @host_and_port is expected to be provided by the user (allowing
1189  * them to give the hostname, and a port override if necessary) and
1190  * @default_port is expected to be provided by the application.
1191  *
1192  * In the case that an IP address is given, a single connection
1193  * attempt is made.  In the case that a name is given, multiple
1194  * connection attempts may be made, in turn and according to the
1195  * number of address records in DNS, until a connection succeeds.
1196  *
1197  * Upon a successful connection, a new #GSocketConnection is constructed
1198  * and returned.  The caller owns this new object and must drop their
1199  * reference to it when finished with it.
1200  *
1201  * In the event of any failure (DNS error, service not found, no hosts
1202  * connectable) %NULL is returned and @error (if non-%NULL) is set
1203  * accordingly.
1204  *
1205  * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1206  *
1207  * Since: 2.22
1208  */
1209 GSocketConnection *
1210 g_socket_client_connect_to_host (GSocketClient  *client,
1211                                  const gchar    *host_and_port,
1212                                  guint16         default_port,
1213                                  GCancellable   *cancellable,
1214                                  GError        **error)
1215 {
1216   GSocketConnectable *connectable;
1217   GSocketConnection *connection;
1218
1219   connectable = g_network_address_parse (host_and_port, default_port, error);
1220   if (connectable == NULL)
1221     return NULL;
1222
1223   connection = g_socket_client_connect (client, connectable,
1224                                         cancellable, error);
1225   g_object_unref (connectable);
1226
1227   return connection;
1228 }
1229
1230 /**
1231  * g_socket_client_connect_to_service:
1232  * @client: a #GSocketConnection
1233  * @domain: a domain name
1234  * @service: the name of the service to connect to
1235  * @cancellable: (nullable): a #GCancellable, or %NULL
1236  * @error: a pointer to a #GError, or %NULL
1237  *
1238  * Attempts to create a TCP connection to a service.
1239  *
1240  * This call looks up the SRV record for @service at @domain for the
1241  * "tcp" protocol.  It then attempts to connect, in turn, to each of
1242  * the hosts providing the service until either a connection succeeds
1243  * or there are no hosts remaining.
1244  *
1245  * Upon a successful connection, a new #GSocketConnection is constructed
1246  * and returned.  The caller owns this new object and must drop their
1247  * reference to it when finished with it.
1248  *
1249  * In the event of any failure (DNS error, service not found, no hosts
1250  * connectable) %NULL is returned and @error (if non-%NULL) is set
1251  * accordingly.
1252  *
1253  * Returns: (transfer full): a #GSocketConnection if successful, or %NULL on error
1254  */
1255 GSocketConnection *
1256 g_socket_client_connect_to_service (GSocketClient  *client,
1257                                     const gchar    *domain,
1258                                     const gchar    *service,
1259                                     GCancellable   *cancellable,
1260                                     GError        **error)
1261 {
1262   GSocketConnectable *connectable;
1263   GSocketConnection *connection;
1264
1265   connectable = g_network_service_new (service, "tcp", domain);
1266   connection = g_socket_client_connect (client, connectable,
1267                                         cancellable, error);
1268   g_object_unref (connectable);
1269
1270   return connection;
1271 }
1272
1273 /**
1274  * g_socket_client_connect_to_uri:
1275  * @client: a #GSocketClient
1276  * @uri: A network URI
1277  * @default_port: the default port to connect to
1278  * @cancellable: (nullable): a #GCancellable, or %NULL
1279  * @error: a pointer to a #GError, or %NULL
1280  *
1281  * This is a helper function for g_socket_client_connect().
1282  *
1283  * Attempts to create a TCP connection with a network URI.
1284  *
1285  * @uri may be any valid URI containing an "authority" (hostname/port)
1286  * component. If a port is not specified in the URI, @default_port
1287  * will be used. TLS will be negotiated if #GSocketClient:tls is %TRUE.
1288  * (#GSocketClient does not know to automatically assume TLS for
1289  * certain URI schemes.)
1290  *
1291  * Using this rather than g_socket_client_connect() or
1292  * g_socket_client_connect_to_host() allows #GSocketClient to
1293  * determine when to use application-specific proxy protocols.
1294  *
1295  * Upon a successful connection, a new #GSocketConnection is constructed
1296  * and returned.  The caller owns this new object and must drop their
1297  * reference to it when finished with it.
1298  *
1299  * In the event of any failure (DNS error, service not found, no hosts
1300  * connectable) %NULL is returned and @error (if non-%NULL) is set
1301  * accordingly.
1302  *
1303  * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1304  *
1305  * Since: 2.26
1306  */
1307 GSocketConnection *
1308 g_socket_client_connect_to_uri (GSocketClient  *client,
1309                                 const gchar    *uri,
1310                                 guint16         default_port,
1311                                 GCancellable   *cancellable,
1312                                 GError        **error)
1313 {
1314   GSocketConnectable *connectable;
1315   GSocketConnection *connection;
1316
1317   connectable = g_network_address_parse_uri (uri, default_port, error);
1318   if (connectable == NULL)
1319     return NULL;
1320
1321   connection = g_socket_client_connect (client, connectable,
1322                                         cancellable, error);
1323   g_object_unref (connectable);
1324
1325   return connection;
1326 }
1327
1328 typedef struct
1329 {
1330   GTask *task; /* unowned */
1331   GSocketClient *client;
1332
1333   GSocketConnectable *connectable;
1334   GSocketAddressEnumerator *enumerator;
1335   GProxyAddress *proxy_addr;
1336   GSocket *socket;
1337   GIOStream *connection;
1338
1339   GSList *connection_attempts;
1340   GError *last_error;
1341
1342   gboolean completed;
1343 } GSocketClientAsyncConnectData;
1344
1345 static void connection_attempt_unref (gpointer attempt);
1346
1347 static void
1348 g_socket_client_async_connect_data_free (GSocketClientAsyncConnectData *data)
1349 {
1350   data->task = NULL;
1351   g_clear_object (&data->connectable);
1352   g_clear_object (&data->enumerator);
1353   g_clear_object (&data->proxy_addr);
1354   g_clear_object (&data->socket);
1355   g_clear_object (&data->connection);
1356   g_slist_free_full (data->connection_attempts, connection_attempt_unref);
1357
1358   g_clear_error (&data->last_error);
1359
1360   g_slice_free (GSocketClientAsyncConnectData, data);
1361 }
1362
1363 typedef struct
1364 {
1365   GSocketAddress *address;
1366   GSocket *socket;
1367   GIOStream *connection;
1368   GSocketClientAsyncConnectData *data; /* unowned */
1369   GSource *timeout_source;
1370   GCancellable *cancellable;
1371   grefcount ref;
1372 } ConnectionAttempt;
1373
1374 static ConnectionAttempt *
1375 connection_attempt_new (void)
1376 {
1377   ConnectionAttempt *attempt = g_new0 (ConnectionAttempt, 1);
1378   g_ref_count_init (&attempt->ref);
1379   return attempt;
1380 }
1381
1382 static ConnectionAttempt *
1383 connection_attempt_ref (ConnectionAttempt *attempt)
1384 {
1385   g_ref_count_inc (&attempt->ref);
1386   return attempt;
1387 }
1388
1389 static void
1390 connection_attempt_unref (gpointer pointer)
1391 {
1392   ConnectionAttempt *attempt = pointer;
1393   if (g_ref_count_dec (&attempt->ref))
1394     {
1395       g_clear_object (&attempt->address);
1396       g_clear_object (&attempt->socket);
1397       g_clear_object (&attempt->connection);
1398       g_clear_object (&attempt->cancellable);
1399       if (attempt->timeout_source)
1400         {
1401           g_source_destroy (attempt->timeout_source);
1402           g_source_unref (attempt->timeout_source);
1403         }
1404       g_free (attempt);
1405     }
1406 }
1407
1408 static void
1409 connection_attempt_remove (ConnectionAttempt *attempt)
1410 {
1411   attempt->data->connection_attempts = g_slist_remove (attempt->data->connection_attempts, attempt);
1412   connection_attempt_unref (attempt);
1413 }
1414
1415 static void
1416 g_socket_client_async_connect_complete (GSocketClientAsyncConnectData *data)
1417 {
1418   g_assert (data->connection);
1419
1420   if (!G_IS_SOCKET_CONNECTION (data->connection))
1421     {
1422       GSocketConnection *wrapper_connection;
1423
1424       wrapper_connection = g_tcp_wrapper_connection_new (data->connection, data->socket);
1425       g_object_unref (data->connection);
1426       data->connection = (GIOStream *)wrapper_connection;
1427     }
1428
1429   if (!g_task_return_error_if_cancelled (data->task))
1430     {
1431       g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_COMPLETE, data->connectable, data->connection);
1432       g_task_return_pointer (data->task, g_steal_pointer (&data->connection), g_object_unref);
1433     }
1434   else
1435     g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_COMPLETE, data->connectable, NULL);
1436
1437   data->completed = TRUE;
1438   g_object_unref (data->task);
1439 }
1440
1441
1442 static void
1443 g_socket_client_enumerator_callback (GObject      *object,
1444                                      GAsyncResult *result,
1445                                      gpointer      user_data);
1446
1447 static void
1448 set_last_error (GSocketClientAsyncConnectData *data,
1449                 GError *error)
1450 {
1451   g_clear_error (&data->last_error);
1452   data->last_error = error;
1453 }
1454
1455 static void
1456 enumerator_next_async (GSocketClientAsyncConnectData *data,
1457                        gboolean                       add_task_ref)
1458 {
1459   /* We need to cleanup the state */
1460   g_clear_object (&data->socket);
1461   g_clear_object (&data->proxy_addr);
1462   g_clear_object (&data->connection);
1463
1464   /* Each enumeration takes a ref. This arg just avoids repeated unrefs when
1465      an enumeration starts another enumeration */
1466   if (add_task_ref)
1467     g_object_ref (data->task);
1468
1469   g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_RESOLVING, data->connectable, NULL);
1470   g_socket_address_enumerator_next_async (data->enumerator,
1471                                           g_task_get_cancellable (data->task),
1472                                           g_socket_client_enumerator_callback,
1473                                           data);
1474 }
1475
1476 static void
1477 g_socket_client_tls_handshake_callback (GObject      *object,
1478                                         GAsyncResult *result,
1479                                         gpointer      user_data)
1480 {
1481   GSocketClientAsyncConnectData *data = user_data;
1482
1483   if (g_tls_connection_handshake_finish (G_TLS_CONNECTION (object),
1484                                          result,
1485                                          &data->last_error))
1486     {
1487       g_object_unref (data->connection);
1488       data->connection = G_IO_STREAM (object);
1489
1490       g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_TLS_HANDSHAKED, data->connectable, data->connection);
1491       g_socket_client_async_connect_complete (data);
1492     }
1493   else
1494     {
1495       g_object_unref (object);
1496       enumerator_next_async (data, FALSE);
1497     }
1498 }
1499
1500 static void
1501 g_socket_client_tls_handshake (GSocketClientAsyncConnectData *data)
1502 {
1503   GIOStream *tlsconn;
1504
1505   if (!data->client->priv->tls)
1506     {
1507       g_socket_client_async_connect_complete (data);
1508       return;
1509     }
1510
1511   tlsconn = g_tls_client_connection_new (data->connection,
1512                                          data->connectable,
1513                                          &data->last_error);
1514   if (tlsconn)
1515     {
1516       g_tls_client_connection_set_validation_flags (G_TLS_CLIENT_CONNECTION (tlsconn),
1517                                                     data->client->priv->tls_validation_flags);
1518       g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_TLS_HANDSHAKING, data->connectable, G_IO_STREAM (tlsconn));
1519       g_tls_connection_handshake_async (G_TLS_CONNECTION (tlsconn),
1520                                         G_PRIORITY_DEFAULT,
1521                                         g_task_get_cancellable (data->task),
1522                                         g_socket_client_tls_handshake_callback,
1523                                         data);
1524     }
1525   else
1526     {
1527       enumerator_next_async (data, FALSE);
1528     }
1529 }
1530
1531 static void
1532 g_socket_client_proxy_connect_callback (GObject      *object,
1533                                         GAsyncResult *result,
1534                                         gpointer      user_data)
1535 {
1536   GSocketClientAsyncConnectData *data = user_data;
1537
1538   g_object_unref (data->connection);
1539   data->connection = g_proxy_connect_finish (G_PROXY (object),
1540                                              result,
1541                                              &data->last_error);
1542   if (data->connection)
1543     {
1544       g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_PROXY_NEGOTIATED, data->connectable, data->connection);
1545     }
1546   else
1547     {
1548       enumerator_next_async (data, FALSE);
1549       return;
1550     }
1551
1552   g_socket_client_tls_handshake (data);
1553 }
1554
1555 static gboolean
1556 task_completed_or_cancelled (GSocketClientAsyncConnectData *data)
1557 {
1558   GTask *task = data->task;
1559   GCancellable *cancellable = g_task_get_cancellable (task);
1560   GError *error = NULL;
1561
1562   if (data->completed)
1563     return TRUE;
1564   else if (g_cancellable_set_error_if_cancelled (cancellable, &error))
1565     {
1566       g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_COMPLETE, data->connectable, NULL);
1567       g_task_return_error (task, g_steal_pointer (&error));
1568       data->completed = TRUE;
1569       return TRUE;
1570     }
1571   else
1572     return FALSE;
1573 }
1574
1575 static void
1576 g_socket_client_connected_callback (GObject      *source,
1577                                     GAsyncResult *result,
1578                                     gpointer      user_data)
1579 {
1580   ConnectionAttempt *attempt = user_data;
1581   GSocketClientAsyncConnectData *data = attempt->data;
1582   GSList *l;
1583   GError *error = NULL;
1584   GProxy *proxy;
1585   const gchar *protocol;
1586
1587   if (task_completed_or_cancelled (data) || g_cancellable_is_cancelled (attempt->cancellable))
1588     {
1589       g_object_unref (data->task);
1590       connection_attempt_unref (attempt);
1591       return;
1592     }
1593
1594   if (attempt->timeout_source)
1595     {
1596       g_source_destroy (attempt->timeout_source);
1597       g_clear_pointer (&attempt->timeout_source, g_source_unref);
1598     }
1599
1600   if (!g_socket_connection_connect_finish (G_SOCKET_CONNECTION (source),
1601                                            result, &error))
1602     {
1603       if (!g_cancellable_is_cancelled (attempt->cancellable))
1604         {
1605           clarify_connect_error (error, data->connectable, attempt->address);
1606           set_last_error (data, error);
1607           connection_attempt_remove (attempt);
1608           enumerator_next_async (data, FALSE);
1609         }
1610       else
1611         {
1612           g_clear_error (&error);
1613           g_object_unref (data->task);
1614           connection_attempt_unref (attempt);
1615         }
1616
1617       return;
1618     }
1619
1620   data->socket = g_steal_pointer (&attempt->socket);
1621   data->connection = g_steal_pointer (&attempt->connection);
1622
1623   for (l = data->connection_attempts; l; l = g_slist_next (l))
1624     {
1625       ConnectionAttempt *attempt_entry = l->data;
1626       g_cancellable_cancel (attempt_entry->cancellable);
1627       connection_attempt_unref (attempt_entry);
1628     }
1629   g_slist_free (data->connection_attempts);
1630   data->connection_attempts = NULL;
1631   connection_attempt_unref (attempt);
1632
1633   g_socket_connection_set_cached_remote_address ((GSocketConnection*)data->connection, NULL);
1634   g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_CONNECTED, data->connectable, data->connection);
1635
1636   /* wrong, but backward compatible */
1637   g_socket_set_blocking (data->socket, TRUE);
1638
1639   if (!data->proxy_addr)
1640     {
1641       g_socket_client_tls_handshake (data);
1642       return;
1643     }
1644
1645   protocol = g_proxy_address_get_protocol (data->proxy_addr);
1646
1647   /* The connection should not be anything other than TCP,
1648    * but let's put a safety guard in case
1649    */
1650   if (!G_IS_TCP_CONNECTION (data->connection))
1651     {
1652       g_critical ("Trying to proxy over non-TCP connection, this is "
1653           "most likely a bug in GLib IO library.");
1654
1655       g_set_error_literal (&data->last_error,
1656           G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
1657           _("Proxying over a non-TCP connection is not supported."));
1658
1659       enumerator_next_async (data, FALSE);
1660     }
1661   else if (g_hash_table_contains (data->client->priv->app_proxies, protocol))
1662     {
1663       /* Simply complete the connection, we don't want to do TLS handshake
1664        * as the application proxy handling may need proxy handshake first */
1665       g_socket_client_async_connect_complete (data);
1666     }
1667   else if ((proxy = g_proxy_get_default_for_protocol (protocol)))
1668     {
1669       g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_PROXY_NEGOTIATING, data->connectable, data->connection);
1670       g_proxy_connect_async (proxy,
1671                              data->connection,
1672                              data->proxy_addr,
1673                              g_task_get_cancellable (data->task),
1674                              g_socket_client_proxy_connect_callback,
1675                              data);
1676       g_object_unref (proxy);
1677     }
1678   else
1679     {
1680       g_clear_error (&data->last_error);
1681
1682       g_set_error (&data->last_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
1683           _("Proxy protocol “%s” is not supported."),
1684           protocol);
1685
1686       enumerator_next_async (data, FALSE);
1687     }
1688 }
1689
1690 static gboolean
1691 on_connection_attempt_timeout (gpointer data)
1692 {
1693   ConnectionAttempt *attempt = data;
1694
1695   enumerator_next_async (attempt->data, TRUE);
1696
1697   g_clear_pointer (&attempt->timeout_source, g_source_unref);
1698   return G_SOURCE_REMOVE;
1699 }
1700
1701 static void
1702 on_connection_cancelled (GCancellable *cancellable,
1703                          gpointer      data)
1704 {
1705   GCancellable *attempt_cancellable = data;
1706
1707   g_cancellable_cancel (attempt_cancellable);
1708 }
1709
1710 static void
1711 g_socket_client_enumerator_callback (GObject      *object,
1712                                      GAsyncResult *result,
1713                                      gpointer      user_data)
1714 {
1715   GSocketClientAsyncConnectData *data = user_data;
1716   GSocketAddress *address = NULL;
1717   GSocket *socket;
1718   ConnectionAttempt *attempt;
1719   GError *error = NULL;
1720
1721   if (task_completed_or_cancelled (data))
1722     {
1723       g_object_unref (data->task);
1724       return;
1725     }
1726
1727   address = g_socket_address_enumerator_next_finish (data->enumerator,
1728                                                      result, &error);
1729   if (address == NULL)
1730     {
1731       if (data->connection_attempts)
1732         {
1733           g_object_unref (data->task);
1734           return;
1735         }
1736
1737       g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_COMPLETE, data->connectable, NULL);
1738       data->completed = TRUE;
1739       if (!error)
1740         {
1741           if (data->last_error)
1742             {
1743               error = data->last_error;
1744               data->last_error = NULL;
1745             }
1746           else
1747             {
1748               g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_FAILED,
1749                                    _("Unknown error on connect"));
1750             }
1751         }
1752       g_task_return_error (data->task, error);
1753       g_object_unref (data->task);
1754       return;
1755     }
1756
1757   g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_RESOLVED,
1758                               data->connectable, NULL);
1759
1760   if (G_IS_PROXY_ADDRESS (address) &&
1761       data->client->priv->enable_proxy)
1762     data->proxy_addr = g_object_ref (G_PROXY_ADDRESS (address));
1763
1764   g_clear_error (&data->last_error);
1765
1766   socket = create_socket (data->client, address, &data->last_error);
1767   if (socket == NULL)
1768     {
1769       g_object_unref (address);
1770       enumerator_next_async (data, FALSE);
1771       return;
1772     }
1773
1774   attempt = connection_attempt_new ();
1775   attempt->data = data;
1776   attempt->socket = socket;
1777   attempt->address = address;
1778   attempt->cancellable = g_cancellable_new ();
1779   attempt->connection = (GIOStream *)g_socket_connection_factory_create_connection (socket);
1780   attempt->timeout_source = g_timeout_source_new (HAPPY_EYEBALLS_CONNECTION_ATTEMPT_TIMEOUT_MS);
1781   g_source_set_callback (attempt->timeout_source, on_connection_attempt_timeout, attempt, NULL);
1782   g_source_attach (attempt->timeout_source, g_main_context_get_thread_default ());
1783   data->connection_attempts = g_slist_append (data->connection_attempts, attempt);
1784
1785   if (g_task_get_cancellable (data->task))
1786     g_cancellable_connect (g_task_get_cancellable (data->task), G_CALLBACK (on_connection_cancelled),
1787                            g_object_ref (attempt->cancellable), g_object_unref);
1788
1789   g_socket_connection_set_cached_remote_address ((GSocketConnection *)attempt->connection, address);
1790   g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_CONNECTING, data->connectable, attempt->connection);
1791   g_socket_connection_connect_async (G_SOCKET_CONNECTION (attempt->connection),
1792                                      address,
1793                                      attempt->cancellable,
1794                                      g_socket_client_connected_callback, connection_attempt_ref (attempt));
1795 }
1796
1797 /**
1798  * g_socket_client_connect_async:
1799  * @client: a #GSocketClient
1800  * @connectable: a #GSocketConnectable specifying the remote address.
1801  * @cancellable: (nullable): a #GCancellable, or %NULL
1802  * @callback: (scope async): a #GAsyncReadyCallback
1803  * @user_data: (closure): user data for the callback
1804  *
1805  * This is the asynchronous version of g_socket_client_connect().
1806  *
1807  * When the operation is finished @callback will be
1808  * called. You can then call g_socket_client_connect_finish() to get
1809  * the result of the operation.
1810  *
1811  * Since: 2.22
1812  */
1813 void
1814 g_socket_client_connect_async (GSocketClient       *client,
1815                                GSocketConnectable  *connectable,
1816                                GCancellable        *cancellable,
1817                                GAsyncReadyCallback  callback,
1818                                gpointer             user_data)
1819 {
1820   GSocketClientAsyncConnectData *data;
1821
1822   g_return_if_fail (G_IS_SOCKET_CLIENT (client));
1823
1824   data = g_slice_new0 (GSocketClientAsyncConnectData);
1825   data->client = client;
1826   data->connectable = g_object_ref (connectable);
1827
1828   if (can_use_proxy (client))
1829     {
1830       data->enumerator = g_socket_connectable_proxy_enumerate (connectable);
1831       if (client->priv->proxy_resolver &&
1832           G_IS_PROXY_ADDRESS_ENUMERATOR (data->enumerator))
1833         {
1834           g_object_set (G_OBJECT (data->enumerator),
1835                         "proxy-resolver", client->priv->proxy_resolver,
1836                         NULL);
1837         }
1838     }
1839   else
1840     data->enumerator = g_socket_connectable_enumerate (connectable);
1841
1842   /* The flow and ownership here isn't quite obvious:
1843     - The task starts an async attempt to connect.
1844       - Each attempt holds a single ref on task.
1845       - Each attempt may create new attempts by timing out (not a failure) so
1846         there are multiple attempts happening in parallel.
1847       - Upon failure an attempt will start a new attempt that steals its ref
1848         until there are no more attempts left and it drops its ref.
1849       - Upon success it will cancel all other attempts and continue on
1850         to the rest of the connection (tls, proxies, etc) which do not
1851         happen in parallel and at the very end drop its ref.
1852       - Upon cancellation an attempt drops its ref.
1853    */
1854
1855   data->task = g_task_new (client, cancellable, callback, user_data);
1856   g_task_set_check_cancellable (data->task, FALSE); /* We handle this manually */
1857   g_task_set_source_tag (data->task, g_socket_client_connect_async);
1858   g_task_set_task_data (data->task, data, (GDestroyNotify)g_socket_client_async_connect_data_free);
1859
1860   enumerator_next_async (data, FALSE);
1861 }
1862
1863 /**
1864  * g_socket_client_connect_to_host_async:
1865  * @client: a #GSocketClient
1866  * @host_and_port: the name and optionally the port of the host to connect to
1867  * @default_port: the default port to connect to
1868  * @cancellable: (nullable): a #GCancellable, or %NULL
1869  * @callback: (scope async): a #GAsyncReadyCallback
1870  * @user_data: (closure): user data for the callback
1871  *
1872  * This is the asynchronous version of g_socket_client_connect_to_host().
1873  *
1874  * When the operation is finished @callback will be
1875  * called. You can then call g_socket_client_connect_to_host_finish() to get
1876  * the result of the operation.
1877  *
1878  * Since: 2.22
1879  */
1880 void
1881 g_socket_client_connect_to_host_async (GSocketClient        *client,
1882                                        const gchar          *host_and_port,
1883                                        guint16               default_port,
1884                                        GCancellable         *cancellable,
1885                                        GAsyncReadyCallback   callback,
1886                                        gpointer              user_data)
1887 {
1888   GSocketConnectable *connectable;
1889   GError *error;
1890
1891   error = NULL;
1892   connectable = g_network_address_parse (host_and_port, default_port,
1893                                          &error);
1894   if (connectable == NULL)
1895     {
1896       g_task_report_error (client, callback, user_data,
1897                            g_socket_client_connect_to_host_async,
1898                            error);
1899     }
1900   else
1901     {
1902       g_socket_client_connect_async (client,
1903                                      connectable, cancellable,
1904                                      callback, user_data);
1905       g_object_unref (connectable);
1906     }
1907 }
1908
1909 /**
1910  * g_socket_client_connect_to_service_async:
1911  * @client: a #GSocketClient
1912  * @domain: a domain name
1913  * @service: the name of the service to connect to
1914  * @cancellable: (nullable): a #GCancellable, or %NULL
1915  * @callback: (scope async): a #GAsyncReadyCallback
1916  * @user_data: (closure): user data for the callback
1917  *
1918  * This is the asynchronous version of
1919  * g_socket_client_connect_to_service().
1920  *
1921  * Since: 2.22
1922  */
1923 void
1924 g_socket_client_connect_to_service_async (GSocketClient       *client,
1925                                           const gchar         *domain,
1926                                           const gchar         *service,
1927                                           GCancellable        *cancellable,
1928                                           GAsyncReadyCallback  callback,
1929                                           gpointer             user_data)
1930 {
1931   GSocketConnectable *connectable;
1932
1933   connectable = g_network_service_new (service, "tcp", domain);
1934   g_socket_client_connect_async (client,
1935                                  connectable, cancellable,
1936                                  callback, user_data);
1937   g_object_unref (connectable);
1938 }
1939
1940 /**
1941  * g_socket_client_connect_to_uri_async:
1942  * @client: a #GSocketClient
1943  * @uri: a network uri
1944  * @default_port: the default port to connect to
1945  * @cancellable: (nullable): a #GCancellable, or %NULL
1946  * @callback: (scope async): a #GAsyncReadyCallback
1947  * @user_data: (closure): user data for the callback
1948  *
1949  * This is the asynchronous version of g_socket_client_connect_to_uri().
1950  *
1951  * When the operation is finished @callback will be
1952  * called. You can then call g_socket_client_connect_to_uri_finish() to get
1953  * the result of the operation.
1954  *
1955  * Since: 2.26
1956  */
1957 void
1958 g_socket_client_connect_to_uri_async (GSocketClient        *client,
1959                                       const gchar          *uri,
1960                                       guint16               default_port,
1961                                       GCancellable         *cancellable,
1962                                       GAsyncReadyCallback   callback,
1963                                       gpointer              user_data)
1964 {
1965   GSocketConnectable *connectable;
1966   GError *error;
1967
1968   error = NULL;
1969   connectable = g_network_address_parse_uri (uri, default_port, &error);
1970   if (connectable == NULL)
1971     {
1972       g_task_report_error (client, callback, user_data,
1973                            g_socket_client_connect_to_uri_async,
1974                            error);
1975     }
1976   else
1977     {
1978       g_socket_client_connect_async (client,
1979                                      connectable, cancellable,
1980                                      callback, user_data);
1981       g_object_unref (connectable);
1982     }
1983 }
1984
1985
1986 /**
1987  * g_socket_client_connect_finish:
1988  * @client: a #GSocketClient.
1989  * @result: a #GAsyncResult.
1990  * @error: a #GError location to store the error occurring, or %NULL to
1991  * ignore.
1992  *
1993  * Finishes an async connect operation. See g_socket_client_connect_async()
1994  *
1995  * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1996  *
1997  * Since: 2.22
1998  */
1999 GSocketConnection *
2000 g_socket_client_connect_finish (GSocketClient  *client,
2001                                 GAsyncResult   *result,
2002                                 GError        **error)
2003 {
2004   g_return_val_if_fail (g_task_is_valid (result, client), NULL);
2005
2006   return g_task_propagate_pointer (G_TASK (result), error);
2007 }
2008
2009 /**
2010  * g_socket_client_connect_to_host_finish:
2011  * @client: a #GSocketClient.
2012  * @result: a #GAsyncResult.
2013  * @error: a #GError location to store the error occurring, or %NULL to
2014  * ignore.
2015  *
2016  * Finishes an async connect operation. See g_socket_client_connect_to_host_async()
2017  *
2018  * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
2019  *
2020  * Since: 2.22
2021  */
2022 GSocketConnection *
2023 g_socket_client_connect_to_host_finish (GSocketClient  *client,
2024                                         GAsyncResult   *result,
2025                                         GError        **error)
2026 {
2027   return g_socket_client_connect_finish (client, result, error);
2028 }
2029
2030 /**
2031  * g_socket_client_connect_to_service_finish:
2032  * @client: a #GSocketClient.
2033  * @result: a #GAsyncResult.
2034  * @error: a #GError location to store the error occurring, or %NULL to
2035  * ignore.
2036  *
2037  * Finishes an async connect operation. See g_socket_client_connect_to_service_async()
2038  *
2039  * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
2040  *
2041  * Since: 2.22
2042  */
2043 GSocketConnection *
2044 g_socket_client_connect_to_service_finish (GSocketClient  *client,
2045                                            GAsyncResult   *result,
2046                                            GError        **error)
2047 {
2048   return g_socket_client_connect_finish (client, result, error);
2049 }
2050
2051 /**
2052  * g_socket_client_connect_to_uri_finish:
2053  * @client: a #GSocketClient.
2054  * @result: a #GAsyncResult.
2055  * @error: a #GError location to store the error occurring, or %NULL to
2056  * ignore.
2057  *
2058  * Finishes an async connect operation. See g_socket_client_connect_to_uri_async()
2059  *
2060  * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
2061  *
2062  * Since: 2.26
2063  */
2064 GSocketConnection *
2065 g_socket_client_connect_to_uri_finish (GSocketClient  *client,
2066                                        GAsyncResult   *result,
2067                                        GError        **error)
2068 {
2069   return g_socket_client_connect_finish (client, result, error);
2070 }
2071
2072 /**
2073  * g_socket_client_add_application_proxy:
2074  * @client: a #GSocketClient
2075  * @protocol: The proxy protocol
2076  *
2077  * Enable proxy protocols to be handled by the application. When the
2078  * indicated proxy protocol is returned by the #GProxyResolver,
2079  * #GSocketClient will consider this protocol as supported but will
2080  * not try to find a #GProxy instance to handle handshaking. The
2081  * application must check for this case by calling
2082  * g_socket_connection_get_remote_address() on the returned
2083  * #GSocketConnection, and seeing if it's a #GProxyAddress of the
2084  * appropriate type, to determine whether or not it needs to handle
2085  * the proxy handshaking itself.
2086  *
2087  * This should be used for proxy protocols that are dialects of
2088  * another protocol such as HTTP proxy. It also allows cohabitation of
2089  * proxy protocols that are reused between protocols. A good example
2090  * is HTTP. It can be used to proxy HTTP, FTP and Gopher and can also
2091  * be use as generic socket proxy through the HTTP CONNECT method.
2092  *
2093  * When the proxy is detected as being an application proxy, TLS handshake
2094  * will be skipped. This is required to let the application do the proxy
2095  * specific handshake.
2096  */
2097 void
2098 g_socket_client_add_application_proxy (GSocketClient *client,
2099                                        const gchar   *protocol)
2100 {
2101   g_hash_table_add (client->priv->app_proxies, g_strdup (protocol));
2102 }