Fix links in gio docs
[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  *
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.
10  *
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.
15  *
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.
20  *
21  * Authors: Ryan Lortie <desrt@desrt.ca>
22  *          Alexander Larsson <alexl@redhat.com>
23  */
24
25 #include "config.h"
26 #include "gsocketclient.h"
27
28 #include <stdlib.h>
29 #include <string.h>
30
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 "glibintl.h"
50
51
52 /**
53  * SECTION:gsocketclient
54  * @short_description: Helper for connecting to a network service
55  * @include: gio/gio.h
56  * @see_also: #GSocketConnection, #GSocketListener
57  *
58  * #GSocketClient is a high-level utility class for connecting to a
59  * network host using a connection oriented socket type.
60  *
61  * You create a #GSocketClient object, set any options you want, and then
62  * call a sync or async connect operation, which returns a #GSocketConnection
63  * subclass on success.
64  *
65  * The type of the #GSocketConnection object returned depends on the type of
66  * the underlying socket that is in use. For instance, for a TCP/IP connection
67  * it will be a #GTcpConnection.
68  *
69  * Since: 2.22
70  */
71
72
73 G_DEFINE_TYPE (GSocketClient, g_socket_client, G_TYPE_OBJECT);
74
75 enum
76 {
77   PROP_NONE,
78   PROP_FAMILY,
79   PROP_TYPE,
80   PROP_PROTOCOL,
81   PROP_LOCAL_ADDRESS,
82   PROP_TIMEOUT,
83   PROP_ENABLE_PROXY,
84   PROP_TLS,
85   PROP_TLS_VALIDATION_FLAGS
86 };
87
88 struct _GSocketClientPrivate
89 {
90   GSocketFamily family;
91   GSocketType type;
92   GSocketProtocol protocol;
93   GSocketAddress *local_address;
94   guint timeout;
95   gboolean enable_proxy;
96   GHashTable *app_proxies;
97   gboolean tls;
98   GTlsCertificateFlags tls_validation_flags;
99 };
100
101 static GSocket *
102 create_socket (GSocketClient  *client,
103                GSocketAddress *dest_address,
104                GError        **error)
105 {
106   GSocketFamily family;
107   GSocket *socket;
108
109   family = client->priv->family;
110   if (family == G_SOCKET_FAMILY_INVALID &&
111       client->priv->local_address != NULL)
112     family = g_socket_address_get_family (client->priv->local_address);
113   if (family == G_SOCKET_FAMILY_INVALID)
114     family = g_socket_address_get_family (dest_address);
115
116   socket = g_socket_new (family,
117                          client->priv->type,
118                          client->priv->protocol,
119                          error);
120   if (socket == NULL)
121     return NULL;
122
123   if (client->priv->local_address)
124     {
125       if (!g_socket_bind (socket,
126                           client->priv->local_address,
127                           FALSE,
128                           error))
129         {
130           g_object_unref (socket);
131           return NULL;
132         }
133     }
134
135   if (client->priv->timeout)
136     g_socket_set_timeout (socket, client->priv->timeout);
137
138   return socket;
139 }
140
141 gboolean
142 can_use_proxy (GSocketClient *client)
143 {
144   GSocketClientPrivate *priv = client->priv;
145
146   return priv->enable_proxy
147           && priv->type == G_SOCKET_TYPE_STREAM;
148 }
149
150 static void
151 g_socket_client_init (GSocketClient *client)
152 {
153   client->priv = G_TYPE_INSTANCE_GET_PRIVATE (client,
154                                               G_TYPE_SOCKET_CLIENT,
155                                               GSocketClientPrivate);
156   client->priv->type = G_SOCKET_TYPE_STREAM;
157   client->priv->app_proxies = g_hash_table_new_full (g_str_hash,
158                                                      g_str_equal,
159                                                      g_free,
160                                                      NULL);
161 }
162
163 /**
164  * g_socket_client_new:
165  *
166  * Creates a new #GSocketClient with the default options.
167  *
168  * Returns: a #GSocketClient.
169  *     Free the returned object with g_object_unref().
170  *
171  * Since: 2.22
172  */
173 GSocketClient *
174 g_socket_client_new (void)
175 {
176   return g_object_new (G_TYPE_SOCKET_CLIENT, NULL);
177 }
178
179 static void
180 g_socket_client_finalize (GObject *object)
181 {
182   GSocketClient *client = G_SOCKET_CLIENT (object);
183
184   if (client->priv->local_address)
185     g_object_unref (client->priv->local_address);
186
187   if (G_OBJECT_CLASS (g_socket_client_parent_class)->finalize)
188     (*G_OBJECT_CLASS (g_socket_client_parent_class)->finalize) (object);
189
190   g_hash_table_unref (client->priv->app_proxies);
191 }
192
193 static void
194 g_socket_client_get_property (GObject    *object,
195                               guint       prop_id,
196                               GValue     *value,
197                               GParamSpec *pspec)
198 {
199   GSocketClient *client = G_SOCKET_CLIENT (object);
200
201   switch (prop_id)
202     {
203       case PROP_FAMILY:
204         g_value_set_enum (value, client->priv->family);
205         break;
206
207       case PROP_TYPE:
208         g_value_set_enum (value, client->priv->type);
209         break;
210
211       case PROP_PROTOCOL:
212         g_value_set_enum (value, client->priv->protocol);
213         break;
214
215       case PROP_LOCAL_ADDRESS:
216         g_value_set_object (value, client->priv->local_address);
217         break;
218
219       case PROP_TIMEOUT:
220         g_value_set_uint (value, client->priv->timeout);
221         break;
222
223       case PROP_ENABLE_PROXY:
224         g_value_set_boolean (value, client->priv->enable_proxy);
225         break;
226
227       case PROP_TLS:
228         g_value_set_boolean (value, g_socket_client_get_tls (client));
229         break;
230
231       case PROP_TLS_VALIDATION_FLAGS:
232         g_value_set_flags (value, g_socket_client_get_tls_validation_flags (client));
233         break;
234
235       default:
236         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
237     }
238 }
239
240 static void
241 g_socket_client_set_property (GObject      *object,
242                               guint         prop_id,
243                               const GValue *value,
244                               GParamSpec   *pspec)
245 {
246   GSocketClient *client = G_SOCKET_CLIENT (object);
247
248   switch (prop_id)
249     {
250     case PROP_FAMILY:
251       g_socket_client_set_family (client, g_value_get_enum (value));
252       break;
253
254     case PROP_TYPE:
255       g_socket_client_set_socket_type (client, g_value_get_enum (value));
256       break;
257
258     case PROP_PROTOCOL:
259       g_socket_client_set_protocol (client, g_value_get_enum (value));
260       break;
261
262     case PROP_LOCAL_ADDRESS:
263       g_socket_client_set_local_address (client, g_value_get_object (value));
264       break;
265
266     case PROP_TIMEOUT:
267       g_socket_client_set_timeout (client, g_value_get_uint (value));
268       break;
269
270     case PROP_ENABLE_PROXY:
271       g_socket_client_set_enable_proxy (client, g_value_get_boolean (value));
272       break;
273
274     case PROP_TLS:
275       g_socket_client_set_tls (client, g_value_get_boolean (value));
276       break;
277
278     case PROP_TLS_VALIDATION_FLAGS:
279       g_socket_client_set_tls_validation_flags (client, g_value_get_flags (value));
280       break;
281
282     default:
283       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
284     }
285 }
286
287 /**
288  * g_socket_client_get_family:
289  * @client: a #GSocketClient.
290  *
291  * Gets the socket family of the socket client.
292  *
293  * See g_socket_client_set_family() for details.
294  *
295  * Returns: a #GSocketFamily
296  *
297  * Since: 2.22
298  */
299 GSocketFamily
300 g_socket_client_get_family (GSocketClient *client)
301 {
302   return client->priv->family;
303 }
304
305 /**
306  * g_socket_client_set_family:
307  * @client: a #GSocketClient.
308  * @family: a #GSocketFamily
309  *
310  * Sets the socket family of the socket client.
311  * If this is set to something other than %G_SOCKET_FAMILY_INVALID
312  * then the sockets created by this object will be of the specified
313  * family.
314  *
315  * This might be useful for instance if you want to force the local
316  * connection to be an ipv4 socket, even though the address might
317  * be an ipv6 mapped to ipv4 address.
318  *
319  * Since: 2.22
320  */
321 void
322 g_socket_client_set_family (GSocketClient *client,
323                             GSocketFamily  family)
324 {
325   if (client->priv->family == family)
326     return;
327
328   client->priv->family = family;
329   g_object_notify (G_OBJECT (client), "family");
330 }
331
332 /**
333  * g_socket_client_get_socket_type:
334  * @client: a #GSocketClient.
335  *
336  * Gets the socket type of the socket client.
337  *
338  * See g_socket_client_set_socket_type() for details.
339  *
340  * Returns: a #GSocketFamily
341  *
342  * Since: 2.22
343  */
344 GSocketType
345 g_socket_client_get_socket_type (GSocketClient *client)
346 {
347   return client->priv->type;
348 }
349
350 /**
351  * g_socket_client_set_socket_type:
352  * @client: a #GSocketClient.
353  * @type: a #GSocketType
354  *
355  * Sets the socket type of the socket client.
356  * The sockets created by this object will be of the specified
357  * type.
358  *
359  * It doesn't make sense to specify a type of %G_SOCKET_TYPE_DATAGRAM,
360  * as GSocketClient is used for connection oriented services.
361  *
362  * Since: 2.22
363  */
364 void
365 g_socket_client_set_socket_type (GSocketClient *client,
366                                  GSocketType    type)
367 {
368   if (client->priv->type == type)
369     return;
370
371   client->priv->type = type;
372   g_object_notify (G_OBJECT (client), "type");
373 }
374
375 /**
376  * g_socket_client_get_protocol:
377  * @client: a #GSocketClient
378  *
379  * Gets the protocol name type of the socket client.
380  *
381  * See g_socket_client_set_protocol() for details.
382  *
383  * Returns: a #GSocketProtocol
384  *
385  * Since: 2.22
386  */
387 GSocketProtocol
388 g_socket_client_get_protocol (GSocketClient *client)
389 {
390   return client->priv->protocol;
391 }
392
393 /**
394  * g_socket_client_set_protocol:
395  * @client: a #GSocketClient.
396  * @protocol: a #GSocketProtocol
397  *
398  * Sets the protocol of the socket client.
399  * The sockets created by this object will use of the specified
400  * protocol.
401  *
402  * If @protocol is %0 that means to use the default
403  * protocol for the socket family and type.
404  *
405  * Since: 2.22
406  */
407 void
408 g_socket_client_set_protocol (GSocketClient   *client,
409                               GSocketProtocol  protocol)
410 {
411   if (client->priv->protocol == protocol)
412     return;
413
414   client->priv->protocol = protocol;
415   g_object_notify (G_OBJECT (client), "protocol");
416 }
417
418 /**
419  * g_socket_client_get_local_address:
420  * @client: a #GSocketClient.
421  *
422  * Gets the local address of the socket client.
423  *
424  * See g_socket_client_set_local_address() for details.
425  *
426  * Returns: (transfer none): a #GSocketAddress or %NULL. Do not free.
427  *
428  * Since: 2.22
429  */
430 GSocketAddress *
431 g_socket_client_get_local_address (GSocketClient *client)
432 {
433   return client->priv->local_address;
434 }
435
436 /**
437  * g_socket_client_set_local_address:
438  * @client: a #GSocketClient.
439  * @address: a #GSocketAddress, or %NULL
440  *
441  * Sets the local address of the socket client.
442  * The sockets created by this object will bound to the
443  * specified address (if not %NULL) before connecting.
444  *
445  * This is useful if you want to ensure that the local
446  * side of the connection is on a specific port, or on
447  * a specific interface.
448  *
449  * Since: 2.22
450  */
451 void
452 g_socket_client_set_local_address (GSocketClient  *client,
453                                    GSocketAddress *address)
454 {
455   if (address)
456     g_object_ref (address);
457
458   if (client->priv->local_address)
459     {
460       g_object_unref (client->priv->local_address);
461     }
462   client->priv->local_address = address;
463   g_object_notify (G_OBJECT (client), "local-address");
464 }
465
466 /**
467  * g_socket_client_get_timeout:
468  * @client: a #GSocketClient
469  *
470  * Gets the I/O timeout time for sockets created by @client.
471  *
472  * See g_socket_client_set_timeout() for details.
473  *
474  * Returns: the timeout in seconds
475  *
476  * Since: 2.26
477  */
478 guint
479 g_socket_client_get_timeout (GSocketClient *client)
480 {
481   return client->priv->timeout;
482 }
483
484
485 /**
486  * g_socket_client_set_timeout:
487  * @client: a #GSocketClient.
488  * @timeout: the timeout
489  *
490  * Sets the I/O timeout for sockets created by @client. @timeout is a
491  * time in seconds, or 0 for no timeout (the default).
492  *
493  * The timeout value affects the initial connection attempt as well,
494  * so setting this may cause calls to g_socket_client_connect(), etc,
495  * to fail with %G_IO_ERROR_TIMED_OUT.
496  *
497  * Since: 2.26
498  */
499 void
500 g_socket_client_set_timeout (GSocketClient *client,
501                              guint          timeout)
502 {
503   if (client->priv->timeout == timeout)
504     return;
505
506   client->priv->timeout = timeout;
507   g_object_notify (G_OBJECT (client), "timeout");
508 }
509
510 /**
511  * g_socket_client_get_enable_proxy:
512  * @client: a #GSocketClient.
513  *
514  * Gets the proxy enable state; see g_socket_client_set_enable_proxy()
515  *
516  * Returns: whether proxying is enabled
517  *
518  * Since: 2.26
519  */
520 gboolean
521 g_socket_client_get_enable_proxy (GSocketClient *client)
522 {
523   return client->priv->enable_proxy;
524 }
525
526 /**
527  * g_socket_client_set_enable_proxy:
528  * @client: a #GSocketClient.
529  * @enable: whether to enable proxies
530  *
531  * Sets whether or not @client attempts to make connections via a
532  * proxy server. When enabled (the default), #GSocketClient will use a
533  * #GProxyResolver to determine if a proxy protocol such as SOCKS is
534  * needed, and automatically do the necessary proxy negotiation.
535  *
536  * Since: 2.26
537  */
538 void
539 g_socket_client_set_enable_proxy (GSocketClient *client,
540                                   gboolean       enable)
541 {
542   enable = !!enable;
543   if (client->priv->enable_proxy == enable)
544     return;
545
546   client->priv->enable_proxy = enable;
547   g_object_notify (G_OBJECT (client), "enable-proxy");
548 }
549
550 /**
551  * g_socket_client_get_tls:
552  * @client: a #GSocketClient.
553  *
554  * Gets whether @client creates TLS connections. See
555  * g_socket_client_set_tls() for details.
556  *
557  * Returns: whether @client uses TLS
558  *
559  * Since: 2.28
560  */
561 gboolean
562 g_socket_client_get_tls (GSocketClient *client)
563 {
564   return client->priv->tls;
565 }
566
567 /**
568  * g_socket_client_set_tls:
569  * @client: a #GSocketClient.
570  * @tls: whether to use TLS
571  *
572  * Sets whether @client creates TLS (aka SSL) connections. If @tls is
573  * %TRUE, @client will wrap its connections in a #GTlsClientConnection
574  * and perform a TLS handshake when connecting.
575  *
576  * Note that since #GSocketClient must return a #GSocketConnection,
577  * but #GTlsClientConnection is not a #GSocketConnection, this
578  * actually wraps the resulting #GTlsClientConnection in a
579  * #GTcpWrapperConnection when returning it. You can use
580  * g_tcp_wrapper_connection_get_base_io_stream() on the return value
581  * to extract the #GTlsClientConnection.
582  *
583  * Since: 2.28
584  */
585 void
586 g_socket_client_set_tls (GSocketClient *client,
587                          gboolean       tls)
588 {
589   tls = !!tls;
590   if (tls == client->priv->tls)
591     return;
592
593   client->priv->tls = tls;
594   g_object_notify (G_OBJECT (client), "tls");
595 }
596
597 /**
598  * g_socket_client_get_tls_validation_flags:
599  * @client: a #GSocketClient.
600  *
601  * Gets the TLS validation flags used creating TLS connections via
602  * @client.
603  *
604  * Returns: the TLS validation flags
605  *
606  * Since: 2.28
607  */
608 GTlsCertificateFlags
609 g_socket_client_get_tls_validation_flags (GSocketClient *client)
610 {
611   return client->priv->tls_validation_flags;
612 }
613
614 /**
615  * g_socket_client_set_tls_validation_flags:
616  * @client: a #GSocketClient.
617  * @flags: the validation flags
618  *
619  * Sets the TLS validation flags used when creating TLS connections
620  * via @client. The default value is %G_TLS_CERTIFICATE_VALIDATE_ALL.
621  *
622  * Since: 2.28
623  */
624 void
625 g_socket_client_set_tls_validation_flags (GSocketClient        *client,
626                                           GTlsCertificateFlags  flags)
627 {
628   if (client->priv->tls_validation_flags != flags)
629     {
630       client->priv->tls_validation_flags = flags;
631       g_object_notify (G_OBJECT (client), "tls-validation-flags");
632     }
633 }
634
635 static void
636 g_socket_client_class_init (GSocketClientClass *class)
637 {
638   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
639
640   g_type_class_add_private (class, sizeof (GSocketClientPrivate));
641
642   gobject_class->finalize = g_socket_client_finalize;
643   gobject_class->set_property = g_socket_client_set_property;
644   gobject_class->get_property = g_socket_client_get_property;
645
646   g_object_class_install_property (gobject_class, PROP_FAMILY,
647                                    g_param_spec_enum ("family",
648                                                       P_("Socket family"),
649                                                       P_("The sockets address family to use for socket construction"),
650                                                       G_TYPE_SOCKET_FAMILY,
651                                                       G_SOCKET_FAMILY_INVALID,
652                                                       G_PARAM_CONSTRUCT |
653                                                       G_PARAM_READWRITE |
654                                                       G_PARAM_STATIC_STRINGS));
655
656   g_object_class_install_property (gobject_class, PROP_TYPE,
657                                    g_param_spec_enum ("type",
658                                                       P_("Socket type"),
659                                                       P_("The sockets type to use for socket construction"),
660                                                       G_TYPE_SOCKET_TYPE,
661                                                       G_SOCKET_TYPE_STREAM,
662                                                       G_PARAM_CONSTRUCT |
663                                                       G_PARAM_READWRITE |
664                                                       G_PARAM_STATIC_STRINGS));
665
666   g_object_class_install_property (gobject_class, PROP_PROTOCOL,
667                                    g_param_spec_enum ("protocol",
668                                                       P_("Socket protocol"),
669                                                       P_("The protocol to use for socket construction, or 0 for default"),
670                                                       G_TYPE_SOCKET_PROTOCOL,
671                                                       G_SOCKET_PROTOCOL_DEFAULT,
672                                                       G_PARAM_CONSTRUCT |
673                                                       G_PARAM_READWRITE |
674                                                       G_PARAM_STATIC_STRINGS));
675
676   g_object_class_install_property (gobject_class, PROP_LOCAL_ADDRESS,
677                                    g_param_spec_object ("local-address",
678                                                         P_("Local address"),
679                                                         P_("The local address constructed sockets will be bound to"),
680                                                         G_TYPE_SOCKET_ADDRESS,
681                                                         G_PARAM_CONSTRUCT |
682                                                         G_PARAM_READWRITE |
683                                                         G_PARAM_STATIC_STRINGS));
684
685   g_object_class_install_property (gobject_class, PROP_TIMEOUT,
686                                    g_param_spec_uint ("timeout",
687                                                       P_("Socket timeout"),
688                                                       P_("The I/O timeout for sockets, or 0 for none"),
689                                                       0, G_MAXUINT, 0,
690                                                       G_PARAM_CONSTRUCT |
691                                                       G_PARAM_READWRITE |
692                                                       G_PARAM_STATIC_STRINGS));
693
694    g_object_class_install_property (gobject_class, PROP_ENABLE_PROXY,
695                                     g_param_spec_boolean ("enable-proxy",
696                                                           P_("Enable proxy"),
697                                                           P_("Enable proxy support"),
698                                                           TRUE,
699                                                           G_PARAM_CONSTRUCT |
700                                                           G_PARAM_READWRITE |
701                                                           G_PARAM_STATIC_STRINGS));
702
703   g_object_class_install_property (gobject_class, PROP_TLS,
704                                    g_param_spec_boolean ("tls",
705                                                          P_("TLS"),
706                                                          P_("Whether to create TLS connections"),
707                                                          FALSE,
708                                                          G_PARAM_CONSTRUCT |
709                                                          G_PARAM_READWRITE |
710                                                          G_PARAM_STATIC_STRINGS));
711   g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
712                                    g_param_spec_flags ("tls-validation-flags",
713                                                        P_("TLS validation flags"),
714                                                        P_("TLS validation flags to use"),
715                                                        G_TYPE_TLS_CERTIFICATE_FLAGS,
716                                                        G_TLS_CERTIFICATE_VALIDATE_ALL,
717                                                        G_PARAM_CONSTRUCT |
718                                                        G_PARAM_READWRITE |
719                                                        G_PARAM_STATIC_STRINGS));
720 }
721
722 /**
723  * g_socket_client_connect:
724  * @client: a #GSocketClient.
725  * @connectable: a #GSocketConnectable specifying the remote address.
726  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
727  * @error: #GError for error reporting, or %NULL to ignore.
728  *
729  * Tries to resolve the @connectable and make a network connection to it.
730  *
731  * Upon a successful connection, a new #GSocketConnection is constructed
732  * and returned.  The caller owns this new object and must drop their
733  * reference to it when finished with it.
734  *
735  * The type of the #GSocketConnection object returned depends on the type of
736  * the underlying socket that is used. For instance, for a TCP/IP connection
737  * it will be a #GTcpConnection.
738  *
739  * The socket created will be the same family as the address that the
740  * @connectable resolves to, unless family is set with g_socket_client_set_family()
741  * or indirectly via g_socket_client_set_local_address(). The socket type
742  * defaults to %G_SOCKET_TYPE_STREAM but can be set with
743  * g_socket_client_set_socket_type().
744  *
745  * If a local address is specified with g_socket_client_set_local_address() the
746  * socket will be bound to this address before connecting.
747  *
748  * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
749  *
750  * Since: 2.22
751  */
752 GSocketConnection *
753 g_socket_client_connect (GSocketClient       *client,
754                          GSocketConnectable  *connectable,
755                          GCancellable        *cancellable,
756                          GError             **error)
757 {
758   GIOStream *connection = NULL;
759   GSocketAddressEnumerator *enumerator = NULL;
760   GError *last_error, *tmp_error;
761
762   last_error = NULL;
763
764   if (can_use_proxy (client))
765     enumerator = g_socket_connectable_proxy_enumerate (connectable);
766   else
767     enumerator = g_socket_connectable_enumerate (connectable);
768
769   while (connection == NULL)
770     {
771       GSocketAddress *address = NULL;
772       GSocket *socket;
773
774       if (g_cancellable_is_cancelled (cancellable))
775         {
776           g_clear_error (error);
777           g_cancellable_set_error_if_cancelled (cancellable, error);
778           break;
779         }
780
781       tmp_error = NULL;
782       address = g_socket_address_enumerator_next (enumerator, cancellable,
783                                                   &tmp_error);
784
785       if (address == NULL)
786         {
787           if (tmp_error)
788             {
789               g_clear_error (&last_error);
790               g_propagate_error (error, tmp_error);
791             }
792           else if (last_error)
793             {
794               g_propagate_error (error, last_error);
795             }
796           else
797             g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
798                                  _("Unknown error on connect"));
799           break;
800         }
801
802       /* clear error from previous attempt */
803       g_clear_error (&last_error);
804
805       socket = create_socket (client, address, &last_error);
806       if (socket == NULL)
807         {
808           g_object_unref (address);
809           continue;
810         }
811
812       if (g_socket_connect (socket, address, cancellable, &last_error))
813         connection = (GIOStream *)g_socket_connection_factory_create_connection (socket);
814
815       if (connection &&
816           G_IS_PROXY_ADDRESS (address) &&
817           client->priv->enable_proxy)
818         {
819           GProxyAddress *proxy_addr = G_PROXY_ADDRESS (address);
820           const gchar *protocol;
821           GProxy *proxy;
822
823           protocol = g_proxy_address_get_protocol (proxy_addr);
824           proxy = g_proxy_get_default_for_protocol (protocol);
825
826           /* The connection should not be anything else then TCP Connection,
827            * but let's put a safety guard in case
828            */
829           if (!G_IS_TCP_CONNECTION (connection))
830             {
831               g_critical ("Trying to proxy over non-TCP connection, this is "
832                           "most likely a bug in GLib IO library.");
833
834               g_set_error_literal (&last_error,
835                   G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
836                   _("Trying to proxy over non-TCP connection is not supported."));
837
838               g_object_unref (connection);
839               connection = NULL;
840             }
841           else if (proxy)
842             {
843               GIOStream *proxy_connection;
844
845               proxy_connection = g_proxy_connect (proxy,
846                                                   connection,
847                                                   proxy_addr,
848                                                   cancellable,
849                                                   &last_error);
850               g_object_unref (connection);
851               connection = proxy_connection;
852               g_object_unref (proxy);
853             }
854           else if (!g_hash_table_lookup_extended (client->priv->app_proxies,
855                                                   protocol, NULL, NULL))
856             {
857               g_set_error (&last_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
858                            _("Proxy protocol '%s' is not supported."),
859                            protocol);
860               g_object_unref (connection);
861               connection = NULL;
862             }
863         }
864
865       if (connection && client->priv->tls)
866         {
867           GIOStream *tlsconn;
868
869           tlsconn = g_tls_client_connection_new (connection, connectable, &last_error);
870           g_object_unref (connection);
871           connection = tlsconn;
872
873           if (tlsconn)
874             {
875               g_tls_client_connection_set_validation_flags (G_TLS_CLIENT_CONNECTION (tlsconn),
876                                                             client->priv->tls_validation_flags);
877               if (!g_tls_connection_handshake (G_TLS_CONNECTION (tlsconn),
878                                                cancellable, &last_error))
879                 {
880                   g_object_unref (tlsconn);
881                   connection = NULL;
882                 }
883             }
884         }
885
886       if (connection && !G_IS_SOCKET_CONNECTION (connection))
887         {
888           GSocketConnection *wrapper_connection;
889
890           wrapper_connection = g_tcp_wrapper_connection_new (connection, socket);
891           g_object_unref (connection);
892           connection = (GIOStream *)wrapper_connection;
893         }
894
895       g_object_unref (socket);
896       g_object_unref (address);
897     }
898   g_object_unref (enumerator);
899
900   return G_SOCKET_CONNECTION (connection);
901 }
902
903 /**
904  * g_socket_client_connect_to_host:
905  * @client: a #GSocketClient
906  * @host_and_port: the name and optionally port of the host to connect to
907  * @default_port: the default port to connect to
908  * @cancellable: (allow-none): a #GCancellable, or %NULL
909  * @error: a pointer to a #GError, or %NULL
910  *
911  * This is a helper function for g_socket_client_connect().
912  *
913  * Attempts to create a TCP connection to the named host.
914  *
915  * @host_and_port may be in any of a number of recognized formats; an IPv6
916  * address, an IPv4 address, or a domain name (in which case a DNS
917  * lookup is performed).  Quoting with [] is supported for all address
918  * types.  A port override may be specified in the usual way with a
919  * colon.  Ports may be given as decimal numbers or symbolic names (in
920  * which case an /etc/services lookup is performed).
921  *
922  * If no port override is given in @host_and_port then @default_port will be
923  * used as the port number to connect to.
924  *
925  * In general, @host_and_port is expected to be provided by the user (allowing
926  * them to give the hostname, and a port override if necessary) and
927  * @default_port is expected to be provided by the application.
928  *
929  * In the case that an IP address is given, a single connection
930  * attempt is made.  In the case that a name is given, multiple
931  * connection attempts may be made, in turn and according to the
932  * number of address records in DNS, until a connection succeeds.
933  *
934  * Upon a successful connection, a new #GSocketConnection is constructed
935  * and returned.  The caller owns this new object and must drop their
936  * reference to it when finished with it.
937  *
938  * In the event of any failure (DNS error, service not found, no hosts
939  * connectable) %NULL is returned and @error (if non-%NULL) is set
940  * accordingly.
941  *
942  * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
943  *
944  * Since: 2.22
945  */
946 GSocketConnection *
947 g_socket_client_connect_to_host (GSocketClient  *client,
948                                  const gchar    *host_and_port,
949                                  guint16         default_port,
950                                  GCancellable   *cancellable,
951                                  GError        **error)
952 {
953   GSocketConnectable *connectable;
954   GSocketConnection *connection;
955
956   connectable = g_network_address_parse (host_and_port, default_port, error);
957   if (connectable == NULL)
958     return NULL;
959
960   connection = g_socket_client_connect (client, connectable,
961                                         cancellable, error);
962   g_object_unref (connectable);
963
964   return connection;
965 }
966
967 /**
968  * g_socket_client_connect_to_service:
969  * @client: a #GSocketConnection
970  * @domain: a domain name
971  * @service: the name of the service to connect to
972  * @cancellable: (allow-none): a #GCancellable, or %NULL
973  * @error: a pointer to a #GError, or %NULL
974  * @returns: (transfer full): a #GSocketConnection if successful, or %NULL on error
975  *
976  * Attempts to create a TCP connection to a service.
977  *
978  * This call looks up the SRV record for @service at @domain for the
979  * "tcp" protocol.  It then attempts to connect, in turn, to each of
980  * the hosts providing the service until either a connection succeeds
981  * or there are no hosts remaining.
982  *
983  * Upon a successful connection, a new #GSocketConnection is constructed
984  * and returned.  The caller owns this new object and must drop their
985  * reference to it when finished with it.
986  *
987  * In the event of any failure (DNS error, service not found, no hosts
988  * connectable) %NULL is returned and @error (if non-%NULL) is set
989  * accordingly.
990  */
991 GSocketConnection *
992 g_socket_client_connect_to_service (GSocketClient  *client,
993                                     const gchar    *domain,
994                                     const gchar    *service,
995                                     GCancellable   *cancellable,
996                                     GError        **error)
997 {
998   GSocketConnectable *connectable;
999   GSocketConnection *connection;
1000
1001   connectable = g_network_service_new (service, "tcp", domain);
1002   connection = g_socket_client_connect (client, connectable,
1003                                         cancellable, error);
1004   g_object_unref (connectable);
1005
1006   return connection;
1007 }
1008
1009 /**
1010  * g_socket_client_connect_to_uri:
1011  * @client: a #GSocketClient
1012  * @uri: A network URI
1013  * @default_port: the default port to connect to
1014  * @cancellable: (allow-none): a #GCancellable, or %NULL
1015  * @error: a pointer to a #GError, or %NULL
1016  *
1017  * This is a helper function for g_socket_client_connect().
1018  *
1019  * Attempts to create a TCP connection with a network URI.
1020  *
1021  * @uri may be any valid URI containing an "authority" (hostname/port)
1022  * component. If a port is not specified in the URI, @default_port
1023  * will be used. TLS will be negotiated if #GSocketClient:tls is %TRUE.
1024  * (#GSocketClient does not know to automatically assume TLS for
1025  * certain URI schemes.)
1026  *
1027  * Using this rather than g_socket_client_connect() or
1028  * g_socket_client_connect_to_host() allows #GSocketClient to
1029  * determine when to use application-specific proxy protocols.
1030  *
1031  * Upon a successful connection, a new #GSocketConnection is constructed
1032  * and returned.  The caller owns this new object and must drop their
1033  * reference to it when finished with it.
1034  *
1035  * In the event of any failure (DNS error, service not found, no hosts
1036  * connectable) %NULL is returned and @error (if non-%NULL) is set
1037  * accordingly.
1038  *
1039  * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1040  *
1041  * Since: 2.26
1042  */
1043 GSocketConnection *
1044 g_socket_client_connect_to_uri (GSocketClient  *client,
1045                                 const gchar    *uri,
1046                                 guint16         default_port,
1047                                 GCancellable   *cancellable,
1048                                 GError        **error)
1049 {
1050   GSocketConnectable *connectable;
1051   GSocketConnection *connection;
1052
1053   connectable = g_network_address_parse_uri (uri, default_port, error);
1054   if (connectable == NULL)
1055     return NULL;
1056
1057   connection = g_socket_client_connect (client, connectable,
1058                                         cancellable, error);
1059   g_object_unref (connectable);
1060
1061   return connection;
1062 }
1063
1064 typedef struct
1065 {
1066   GSimpleAsyncResult *result;
1067   GCancellable *cancellable;
1068   GSocketClient *client;
1069
1070   GSocketConnectable *connectable;
1071   GSocketAddressEnumerator *enumerator;
1072   GProxyAddress *proxy_addr;
1073   GSocket *current_socket;
1074   GIOStream *connection;
1075
1076   GError *last_error;
1077 } GSocketClientAsyncConnectData;
1078
1079 static void
1080 g_socket_client_async_connect_complete (GSocketClientAsyncConnectData *data)
1081 {
1082   if (data->last_error)
1083     {
1084       g_simple_async_result_take_error (data->result, data->last_error);
1085     }
1086   else
1087     {
1088       g_assert (data->connection);
1089
1090       if (!G_IS_SOCKET_CONNECTION (data->connection))
1091         {
1092           GSocketConnection *wrapper_connection;
1093
1094           wrapper_connection = g_tcp_wrapper_connection_new (data->connection,
1095                                                              data->current_socket);
1096           g_object_unref (data->connection);
1097           data->connection = (GIOStream *)wrapper_connection;
1098         }
1099
1100       g_simple_async_result_set_op_res_gpointer (data->result,
1101                                                  data->connection,
1102                                                  g_object_unref);
1103     }
1104
1105   g_simple_async_result_complete (data->result);
1106   g_object_unref (data->result);
1107   g_object_unref (data->connectable);
1108   g_object_unref (data->enumerator);
1109   if (data->cancellable)
1110     g_object_unref (data->cancellable);
1111   if (data->current_socket)
1112     g_object_unref (data->current_socket);
1113   if (data->proxy_addr)
1114     g_object_unref (data->proxy_addr);
1115   g_slice_free (GSocketClientAsyncConnectData, data);
1116 }
1117
1118
1119 static void
1120 g_socket_client_enumerator_callback (GObject      *object,
1121                                      GAsyncResult *result,
1122                                      gpointer      user_data);
1123
1124 static void
1125 set_last_error (GSocketClientAsyncConnectData *data,
1126                 GError *error)
1127 {
1128   g_clear_error (&data->last_error);
1129   data->last_error = error;
1130 }
1131
1132 static void
1133 enumerator_next_async (GSocketClientAsyncConnectData *data)
1134 {
1135   g_socket_address_enumerator_next_async (data->enumerator,
1136                                           data->cancellable,
1137                                           g_socket_client_enumerator_callback,
1138                                           data);
1139 }
1140
1141 static void
1142 g_socket_client_tls_handshake_callback (GObject      *object,
1143                                         GAsyncResult *result,
1144                                         gpointer      user_data)
1145 {
1146   GSocketClientAsyncConnectData *data = user_data;
1147
1148   if (g_tls_connection_handshake_finish (G_TLS_CONNECTION (object),
1149                                          result,
1150                                          &data->last_error))
1151     {
1152       g_object_unref (data->connection);
1153       data->connection = G_IO_STREAM (object);
1154     }
1155   else
1156     {
1157       g_object_unref (object);
1158       g_object_unref (data->current_socket);
1159       data->current_socket = NULL;
1160       g_object_unref (data->connection);
1161       data->connection = NULL;
1162
1163       enumerator_next_async (data);
1164     }
1165
1166   g_socket_client_async_connect_complete (data);
1167 }
1168
1169 static void
1170 g_socket_client_tls_handshake (GSocketClientAsyncConnectData *data)
1171 {
1172   GIOStream *tlsconn;
1173
1174   if (!data->client->priv->tls)
1175     {
1176       g_socket_client_async_connect_complete (data);
1177       return;
1178     }
1179
1180   tlsconn = g_tls_client_connection_new (data->connection,
1181                                          data->connectable,
1182                                          &data->last_error);
1183   if (tlsconn)
1184     {
1185       g_tls_client_connection_set_validation_flags (G_TLS_CLIENT_CONNECTION (tlsconn),
1186                                                     data->client->priv->tls_validation_flags);
1187       g_tls_connection_handshake_async (G_TLS_CONNECTION (tlsconn),
1188                                         G_PRIORITY_DEFAULT,
1189                                         data->cancellable,
1190                                         g_socket_client_tls_handshake_callback,
1191                                         data);
1192     }
1193   else
1194     {
1195       g_object_unref (data->current_socket);
1196       data->current_socket = NULL;
1197       g_object_unref (data->connection);
1198       data->connection = NULL;
1199
1200       enumerator_next_async (data);
1201     }
1202 }
1203
1204 static void
1205 g_socket_client_proxy_connect_callback (GObject      *object,
1206                                         GAsyncResult *result,
1207                                         gpointer      user_data)
1208 {
1209   GSocketClientAsyncConnectData *data = user_data;
1210
1211   g_object_unref (data->connection);
1212   data->connection = g_proxy_connect_finish (G_PROXY (object),
1213                                              result,
1214                                              &data->last_error);
1215   if (!data->connection)
1216     {
1217       g_object_unref (data->current_socket);
1218       data->current_socket = NULL;
1219
1220       enumerator_next_async (data);
1221       return;
1222     }
1223
1224   g_socket_client_tls_handshake (data);
1225 }
1226
1227 static void
1228 g_socket_client_proxy_connect (GSocketClientAsyncConnectData *data)
1229 {
1230   GProxy *proxy;
1231   const gchar *protocol;
1232
1233   if (!data->proxy_addr)
1234     {
1235       g_socket_client_tls_handshake (data);
1236       return;
1237     }
1238
1239   protocol  = g_proxy_address_get_protocol (data->proxy_addr);
1240   proxy = g_proxy_get_default_for_protocol (protocol);
1241
1242   /* The connection should not be anything else then TCP Connection,
1243    * but let's put a safety guard in case
1244    */
1245   if (!G_IS_TCP_CONNECTION (data->connection))
1246     {
1247       g_critical ("Trying to proxy over non-TCP connection, this is "
1248           "most likely a bug in GLib IO library.");
1249
1250       g_set_error_literal (&data->last_error,
1251           G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
1252           _("Trying to proxy over non-TCP connection is not supported."));
1253
1254       g_object_unref (data->connection);
1255       data->connection = NULL;
1256       g_object_unref (data->current_socket);
1257       data->current_socket = NULL;
1258
1259       enumerator_next_async (data);
1260     }
1261   else if (proxy)
1262     {
1263       g_proxy_connect_async (proxy,
1264                              data->connection,
1265                              data->proxy_addr,
1266                              data->cancellable,
1267                              g_socket_client_proxy_connect_callback,
1268                              data);
1269       g_object_unref (proxy);
1270     }
1271   else if (!g_hash_table_lookup_extended (data->client->priv->app_proxies,
1272                                           protocol, NULL, NULL))
1273     {
1274       g_clear_error (&data->last_error);
1275
1276       g_set_error (&data->last_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
1277           _("Proxy protocol '%s' is not supported."),
1278           protocol);
1279
1280       g_object_unref (data->current_socket);
1281       data->current_socket = NULL;
1282       g_object_unref (data->connection);
1283       data->connection = NULL;
1284       g_object_unref (data->current_socket);
1285       data->current_socket = NULL;
1286
1287       enumerator_next_async (data);
1288     }
1289 }
1290
1291 static void
1292 g_socket_client_socket_connected (GSocketClientAsyncConnectData *data)
1293 {
1294   g_socket_set_blocking (data->current_socket, TRUE);
1295
1296   data->connection = (GIOStream *)
1297     g_socket_connection_factory_create_connection (data->current_socket);
1298
1299   g_socket_client_proxy_connect (data);
1300 }
1301
1302 static gboolean
1303 g_socket_client_socket_callback (GSocket *socket,
1304                                  GIOCondition condition,
1305                                  GSocketClientAsyncConnectData *data)
1306 {
1307   GError *error = NULL;
1308
1309   if (g_cancellable_is_cancelled (data->cancellable))
1310     {
1311       /* Cancelled, return done with last error being cancelled */
1312       g_clear_error (&data->last_error);
1313       g_object_unref (data->current_socket);
1314       data->current_socket = NULL;
1315       g_cancellable_set_error_if_cancelled (data->cancellable,
1316                                             &data->last_error);
1317
1318       g_socket_client_async_connect_complete (data);
1319       return FALSE;
1320     }
1321   else
1322     {
1323       /* socket is ready for writing means connect done, did it succeed? */
1324       if (!g_socket_check_connect_result (data->current_socket, &error))
1325         {
1326           set_last_error (data, error);
1327           g_object_unref (data->current_socket);
1328           data->current_socket = NULL;
1329
1330           /* try next one */
1331           enumerator_next_async (data);
1332
1333           return FALSE;
1334         }
1335     }
1336
1337   g_socket_client_socket_connected (data);
1338   return FALSE;
1339 }
1340
1341 static void
1342 g_socket_client_enumerator_callback (GObject      *object,
1343                                      GAsyncResult *result,
1344                                      gpointer      user_data)
1345 {
1346   GSocketClientAsyncConnectData *data = user_data;
1347   GSocketAddress *address = NULL;
1348   GSocket *socket;
1349   GError *tmp_error = NULL;
1350
1351   if (g_cancellable_is_cancelled (data->cancellable))
1352     {
1353       g_clear_error (&data->last_error);
1354       g_cancellable_set_error_if_cancelled (data->cancellable, &data->last_error);
1355       g_socket_client_async_connect_complete (data);
1356       return;
1357     }
1358
1359   address = g_socket_address_enumerator_next_finish (data->enumerator,
1360                                                      result, &tmp_error);
1361
1362   if (address == NULL)
1363     {
1364       if (tmp_error)
1365         set_last_error (data, tmp_error);
1366       else if (data->last_error == NULL)
1367         g_set_error_literal (&data->last_error, G_IO_ERROR, G_IO_ERROR_FAILED,
1368                              _("Unknown error on connect"));
1369
1370       g_socket_client_async_connect_complete (data);
1371       return;
1372     }
1373
1374   if (G_IS_PROXY_ADDRESS (address) &&
1375       data->client->priv->enable_proxy)
1376     data->proxy_addr = g_object_ref (G_PROXY_ADDRESS (address));
1377
1378   g_clear_error (&data->last_error);
1379
1380   socket = create_socket (data->client, address, &data->last_error);
1381   if (socket != NULL)
1382     {
1383       g_socket_set_blocking (socket, FALSE);
1384       if (g_socket_connect (socket, address, data->cancellable, &tmp_error))
1385         {
1386           data->current_socket = socket;
1387           g_socket_client_socket_connected (data);
1388
1389           g_object_unref (address);
1390           return;
1391         }
1392       else if (g_error_matches (tmp_error, G_IO_ERROR, G_IO_ERROR_PENDING))
1393         {
1394           GSource *source;
1395
1396           data->current_socket = socket;
1397           g_error_free (tmp_error);
1398
1399           source = g_socket_create_source (socket, G_IO_OUT,
1400                                            data->cancellable);
1401           g_source_set_callback (source,
1402                                  (GSourceFunc) g_socket_client_socket_callback,
1403                                  data, NULL);
1404           g_source_attach (source, g_main_context_get_thread_default ());
1405           g_source_unref (source);
1406
1407           g_object_unref (address);
1408           return;
1409         }
1410       else
1411         {
1412           data->last_error = tmp_error;
1413           g_object_unref (socket);
1414         }
1415     }
1416
1417   g_object_unref (address);
1418   enumerator_next_async (data);
1419 }
1420
1421 /**
1422  * g_socket_client_connect_async:
1423  * @client: a #GSocketClient
1424  * @connectable: a #GSocketConnectable specifying the remote address.
1425  * @cancellable: (allow-none): a #GCancellable, or %NULL
1426  * @callback: (scope async): a #GAsyncReadyCallback
1427  * @user_data: (closure): user data for the callback
1428  *
1429  * This is the asynchronous version of g_socket_client_connect().
1430  *
1431  * When the operation is finished @callback will be
1432  * called. You can then call g_socket_client_connect_finish() to get
1433  * the result of the operation.
1434  *
1435  * Since: 2.22
1436  */
1437 void
1438 g_socket_client_connect_async (GSocketClient       *client,
1439                                GSocketConnectable  *connectable,
1440                                GCancellable        *cancellable,
1441                                GAsyncReadyCallback  callback,
1442                                gpointer             user_data)
1443 {
1444   GSocketClientAsyncConnectData *data;
1445
1446   g_return_if_fail (G_IS_SOCKET_CLIENT (client));
1447
1448   data = g_slice_new0 (GSocketClientAsyncConnectData);
1449
1450   data->result = g_simple_async_result_new (G_OBJECT (client),
1451                                             callback, user_data,
1452                                             g_socket_client_connect_async);
1453   data->client = client;
1454   if (cancellable)
1455     data->cancellable = g_object_ref (cancellable);
1456   else
1457     data->cancellable = NULL;
1458   data->last_error = NULL;
1459   data->connectable = g_object_ref (connectable);
1460
1461   if (can_use_proxy (client))
1462       data->enumerator = g_socket_connectable_proxy_enumerate (connectable);
1463   else
1464       data->enumerator = g_socket_connectable_enumerate (connectable);
1465
1466   enumerator_next_async (data);
1467 }
1468
1469 /**
1470  * g_socket_client_connect_to_host_async:
1471  * @client: a #GSocketClient
1472  * @host_and_port: the name and optionally the port of the host to connect to
1473  * @default_port: the default port to connect to
1474  * @cancellable: (allow-none): a #GCancellable, or %NULL
1475  * @callback: (scope async): a #GAsyncReadyCallback
1476  * @user_data: (closure): user data for the callback
1477  *
1478  * This is the asynchronous version of g_socket_client_connect_to_host().
1479  *
1480  * When the operation is finished @callback will be
1481  * called. You can then call g_socket_client_connect_to_host_finish() to get
1482  * the result of the operation.
1483  *
1484  * Since: 2.22
1485  */
1486 void
1487 g_socket_client_connect_to_host_async (GSocketClient        *client,
1488                                        const gchar          *host_and_port,
1489                                        guint16               default_port,
1490                                        GCancellable         *cancellable,
1491                                        GAsyncReadyCallback   callback,
1492                                        gpointer              user_data)
1493 {
1494   GSocketConnectable *connectable;
1495   GError *error;
1496
1497   error = NULL;
1498   connectable = g_network_address_parse (host_and_port, default_port,
1499                                          &error);
1500   if (connectable == NULL)
1501     {
1502       g_simple_async_report_take_gerror_in_idle (G_OBJECT (client),
1503                                             callback, user_data, error);
1504     }
1505   else
1506     {
1507       g_socket_client_connect_async (client,
1508                                      connectable, cancellable,
1509                                      callback, user_data);
1510       g_object_unref (connectable);
1511     }
1512 }
1513
1514 /**
1515  * g_socket_client_connect_to_service_async:
1516  * @client: a #GSocketClient
1517  * @domain: a domain name
1518  * @service: the name of the service to connect to
1519  * @cancellable: (allow-none): a #GCancellable, or %NULL
1520  * @callback: (scope async): a #GAsyncReadyCallback
1521  * @user_data: (closure): user data for the callback
1522  *
1523  * This is the asynchronous version of
1524  * g_socket_client_connect_to_service().
1525  *
1526  * Since: 2.22
1527  */
1528 void
1529 g_socket_client_connect_to_service_async (GSocketClient       *client,
1530                                           const gchar         *domain,
1531                                           const gchar         *service,
1532                                           GCancellable        *cancellable,
1533                                           GAsyncReadyCallback  callback,
1534                                           gpointer             user_data)
1535 {
1536   GSocketConnectable *connectable;
1537
1538   connectable = g_network_service_new (service, "tcp", domain);
1539   g_socket_client_connect_async (client,
1540                                  connectable, cancellable,
1541                                  callback, user_data);
1542   g_object_unref (connectable);
1543 }
1544
1545 /**
1546  * g_socket_client_connect_to_uri_async:
1547  * @client: a #GSocketClient
1548  * @uri: a network uri
1549  * @default_port: the default port to connect to
1550  * @cancellable: (allow-none): a #GCancellable, or %NULL
1551  * @callback: (scope async): a #GAsyncReadyCallback
1552  * @user_data: (closure): user data for the callback
1553  *
1554  * This is the asynchronous version of g_socket_client_connect_to_uri().
1555  *
1556  * When the operation is finished @callback will be
1557  * called. You can then call g_socket_client_connect_to_uri_finish() to get
1558  * the result of the operation.
1559  *
1560  * Since: 2.26
1561  */
1562 void
1563 g_socket_client_connect_to_uri_async (GSocketClient        *client,
1564                                       const gchar          *uri,
1565                                       guint16               default_port,
1566                                       GCancellable         *cancellable,
1567                                       GAsyncReadyCallback   callback,
1568                                       gpointer              user_data)
1569 {
1570   GSocketConnectable *connectable;
1571   GError *error;
1572
1573   error = NULL;
1574   connectable = g_network_address_parse_uri (uri, default_port, &error);
1575   if (connectable == NULL)
1576     {
1577       g_simple_async_report_take_gerror_in_idle (G_OBJECT (client),
1578                                             callback, user_data, error);
1579     }
1580   else
1581     {
1582       g_socket_client_connect_async (client,
1583                                      connectable, cancellable,
1584                                      callback, user_data);
1585       g_object_unref (connectable);
1586     }
1587 }
1588
1589
1590 /**
1591  * g_socket_client_connect_finish:
1592  * @client: a #GSocketClient.
1593  * @result: a #GAsyncResult.
1594  * @error: a #GError location to store the error occurring, or %NULL to
1595  * ignore.
1596  *
1597  * Finishes an async connect operation. See g_socket_client_connect_async()
1598  *
1599  * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1600  *
1601  * Since: 2.22
1602  */
1603 GSocketConnection *
1604 g_socket_client_connect_finish (GSocketClient  *client,
1605                                 GAsyncResult   *result,
1606                                 GError        **error)
1607 {
1608   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1609
1610   if (g_simple_async_result_propagate_error (simple, error))
1611     return NULL;
1612
1613   return g_object_ref (g_simple_async_result_get_op_res_gpointer (simple));
1614 }
1615
1616 /**
1617  * g_socket_client_connect_to_host_finish:
1618  * @client: a #GSocketClient.
1619  * @result: a #GAsyncResult.
1620  * @error: a #GError location to store the error occurring, or %NULL to
1621  * ignore.
1622  *
1623  * Finishes an async connect operation. See g_socket_client_connect_to_host_async()
1624  *
1625  * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1626  *
1627  * Since: 2.22
1628  */
1629 GSocketConnection *
1630 g_socket_client_connect_to_host_finish (GSocketClient  *client,
1631                                         GAsyncResult   *result,
1632                                         GError        **error)
1633 {
1634   return g_socket_client_connect_finish (client, result, error);
1635 }
1636
1637 /**
1638  * g_socket_client_connect_to_service_finish:
1639  * @client: a #GSocketClient.
1640  * @result: a #GAsyncResult.
1641  * @error: a #GError location to store the error occurring, or %NULL to
1642  * ignore.
1643  *
1644  * Finishes an async connect operation. See g_socket_client_connect_to_service_async()
1645  *
1646  * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1647  *
1648  * Since: 2.22
1649  */
1650 GSocketConnection *
1651 g_socket_client_connect_to_service_finish (GSocketClient  *client,
1652                                            GAsyncResult   *result,
1653                                            GError        **error)
1654 {
1655   return g_socket_client_connect_finish (client, result, error);
1656 }
1657
1658 /**
1659  * g_socket_client_connect_to_uri_finish:
1660  * @client: a #GSocketClient.
1661  * @result: a #GAsyncResult.
1662  * @error: a #GError location to store the error occurring, or %NULL to
1663  * ignore.
1664  *
1665  * Finishes an async connect operation. See g_socket_client_connect_to_uri_async()
1666  *
1667  * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1668  *
1669  * Since: 2.26
1670  */
1671 GSocketConnection *
1672 g_socket_client_connect_to_uri_finish (GSocketClient  *client,
1673                                        GAsyncResult   *result,
1674                                        GError        **error)
1675 {
1676   return g_socket_client_connect_finish (client, result, error);
1677 }
1678
1679 /**
1680  * g_socket_client_add_application_proxy:
1681  * @client: a #GSocketClient
1682  * @protocol: The proxy protocol
1683  *
1684  * Enable proxy protocols to be handled by the application. When the
1685  * indicated proxy protocol is returned by the #GProxyResolver,
1686  * #GSocketClient will consider this protocol as supported but will
1687  * not try to find a #GProxy instance to handle handshaking. The
1688  * application must check for this case by calling
1689  * g_socket_connection_get_remote_address() on the returned
1690  * #GSocketConnection, and seeing if it's a #GProxyAddress of the
1691  * appropriate type, to determine whether or not it needs to handle
1692  * the proxy handshaking itself.
1693  *
1694  * This should be used for proxy protocols that are dialects of
1695  * another protocol such as HTTP proxy. It also allows cohabitation of
1696  * proxy protocols that are reused between protocols. A good example
1697  * is HTTP. It can be used to proxy HTTP, FTP and Gopher and can also
1698  * be use as generic socket proxy through the HTTP CONNECT method.
1699  */
1700 void
1701 g_socket_client_add_application_proxy (GSocketClient *client,
1702                                        const gchar   *protocol)
1703 {
1704   g_hash_table_insert (client->priv->app_proxies, g_strdup (protocol), NULL);
1705 }