Add initial TLS (SSL) support to gio
[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, 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 #GSocketAddres or %NULL. don't 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 the 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: 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 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           GTlsClientConnection *tlsconn;
868
869           tlsconn = g_tls_client_connection_new (connection, connectable, &last_error);
870           g_object_unref (connection);
871           connection = (GIOStream *)tlsconn;
872
873           if (tlsconn)
874             {
875               g_tls_client_connection_set_validation_flags (tlsconn, client->priv->tls_validation_flags);
876               if (!g_tls_connection_handshake (G_TLS_CONNECTION (tlsconn),
877                                                cancellable, &last_error))
878                 {
879                   g_object_unref (tlsconn);
880                   connection = NULL;
881                 }
882             }
883         }
884
885       if (connection && !G_IS_SOCKET_CONNECTION (connection))
886         {
887           GSocketConnection *wrapper_connection;
888
889           wrapper_connection = g_tcp_wrapper_connection_new (connection, socket);
890           g_object_unref (connection);
891           connection = (GIOStream *)wrapper_connection;
892         }
893
894       g_object_unref (socket);
895       g_object_unref (address);
896     }
897   g_object_unref (enumerator);
898
899   return G_SOCKET_CONNECTION (connection);
900 }
901
902 /**
903  * g_socket_client_connect_to_host:
904  * @client: a #GSocketClient
905  * @host_and_port: the name and optionally port of the host to connect to
906  * @default_port: the default port to connect to
907  * @cancellable: a #GCancellable, or %NULL
908  * @error: a pointer to a #GError, or %NULL
909  *
910  * This is a helper function for g_socket_client_connect().
911  *
912  * Attempts to create a TCP connection to the named host.
913  *
914  * @host_and_port may be in any of a number of recognised formats; an IPv6
915  * address, an IPv4 address, or a domain name (in which case a DNS
916  * lookup is performed).  Quoting with [] is supported for all address
917  * types.  A port override may be specified in the usual way with a
918  * colon.  Ports may be given as decimal numbers or symbolic names (in
919  * which case an /etc/services lookup is performed).
920  *
921  * If no port override is given in @host_and_port then @default_port will be
922  * used as the port number to connect to.
923  *
924  * In general, @host_and_port is expected to be provided by the user (allowing
925  * them to give the hostname, and a port overide if necessary) and
926  * @default_port is expected to be provided by the application.
927  *
928  * In the case that an IP address is given, a single connection
929  * attempt is made.  In the case that a name is given, multiple
930  * connection attempts may be made, in turn and according to the
931  * number of address records in DNS, until a connection succeeds.
932  *
933  * Upon a successful connection, a new #GSocketConnection is constructed
934  * and returned.  The caller owns this new object and must drop their
935  * reference to it when finished with it.
936  *
937  * In the event of any failure (DNS error, service not found, no hosts
938  * connectable) %NULL is returned and @error (if non-%NULL) is set
939  * accordingly.
940  *
941  * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
942  *
943  * Since: 2.22
944  */
945 GSocketConnection *
946 g_socket_client_connect_to_host (GSocketClient  *client,
947                                  const gchar    *host_and_port,
948                                  guint16         default_port,
949                                  GCancellable   *cancellable,
950                                  GError        **error)
951 {
952   GSocketConnectable *connectable;
953   GSocketConnection *connection;
954
955   connectable = g_network_address_parse (host_and_port, default_port, error);
956   if (connectable == NULL)
957     return NULL;
958
959   connection = g_socket_client_connect (client, connectable,
960                                         cancellable, error);
961   g_object_unref (connectable);
962
963   return connection;
964 }
965
966 /**
967  * g_socket_client_connect_to_service:
968  * @client: a #GSocketConnection
969  * @domain: a domain name
970  * @service: the name of the service to connect to
971  * @cancellable: a #GCancellable, or %NULL
972  * @error: a pointer to a #GError, or %NULL
973  * @returns: (transfer full): a #GSocketConnection if successful, or %NULL on error
974  *
975  * Attempts to create a TCP connection to a service.
976  *
977  * This call looks up the SRV record for @service at @domain for the
978  * "tcp" protocol.  It then attempts to connect, in turn, to each of
979  * the hosts providing the service until either a connection succeeds
980  * or there are no hosts remaining.
981  *
982  * Upon a successful connection, a new #GSocketConnection is constructed
983  * and returned.  The caller owns this new object and must drop their
984  * reference to it when finished with it.
985  *
986  * In the event of any failure (DNS error, service not found, no hosts
987  * connectable) %NULL is returned and @error (if non-%NULL) is set
988  * accordingly.
989  */
990 GSocketConnection *
991 g_socket_client_connect_to_service (GSocketClient  *client,
992                                     const gchar    *domain,
993                                     const gchar    *service,
994                                     GCancellable   *cancellable,
995                                     GError        **error)
996 {
997   GSocketConnectable *connectable;
998   GSocketConnection *connection;
999
1000   connectable = g_network_service_new (service, "tcp", domain);
1001   connection = g_socket_client_connect (client, connectable,
1002                                         cancellable, error);
1003   g_object_unref (connectable);
1004
1005   return connection;
1006 }
1007
1008 /**
1009  * g_socket_client_connect_to_uri:
1010  * @client: a #GSocketClient
1011  * @uri: A network URI
1012  * @default_port: the default port to connect to
1013  * @cancellable: a #GCancellable, or %NULL
1014  * @error: a pointer to a #GError, or %NULL
1015  *
1016  * This is a helper function for g_socket_client_connect().
1017  *
1018  * Attempts to create a TCP connection with a network URI.
1019  *
1020  * @uri may be any valid URI containing an "authority" (hostname/port)
1021  * component. If a port is not specified in the URI, @default_port
1022  * will be used.
1023  *
1024  * Using this rather than g_socket_client_connect() or
1025  * g_socket_client_connect_to_host() allows #GSocketClient to
1026  * determine when to use application-specific proxy protocols.
1027  *
1028  * Upon a successful connection, a new #GSocketConnection is constructed
1029  * and returned.  The caller owns this new object and must drop their
1030  * reference to it when finished with it.
1031  *
1032  * In the event of any failure (DNS error, service not found, no hosts
1033  * connectable) %NULL is returned and @error (if non-%NULL) is set
1034  * accordingly.
1035  *
1036  * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1037  *
1038  * Since: 2.26
1039  */
1040 GSocketConnection *
1041 g_socket_client_connect_to_uri (GSocketClient  *client,
1042                                 const gchar    *uri,
1043                                 guint16         default_port,
1044                                 GCancellable   *cancellable,
1045                                 GError        **error)
1046 {
1047   GSocketConnectable *connectable;
1048   GSocketConnection *connection;
1049
1050   connectable = g_network_address_parse_uri (uri, default_port, error);
1051   if (connectable == NULL)
1052     return NULL;
1053
1054   connection = g_socket_client_connect (client, connectable,
1055                                         cancellable, error);
1056   g_object_unref (connectable);
1057
1058   return connection;
1059 }
1060
1061 typedef struct
1062 {
1063   GSimpleAsyncResult *result;
1064   GCancellable *cancellable;
1065   GSocketClient *client;
1066
1067   GSocketConnectable *connectable;
1068   GSocketAddressEnumerator *enumerator;
1069   GProxyAddress *proxy_addr;
1070   GSocket *current_socket;
1071   GIOStream *connection;
1072
1073   GError *last_error;
1074 } GSocketClientAsyncConnectData;
1075
1076 static void
1077 g_socket_client_async_connect_complete (GSocketClientAsyncConnectData *data)
1078 {
1079   if (data->last_error)
1080     {
1081       g_simple_async_result_take_error (data->result, data->last_error);
1082     }
1083   else
1084     {
1085       g_assert (data->connection);
1086
1087       if (!G_IS_SOCKET_CONNECTION (data->connection))
1088         {
1089           GSocketConnection *wrapper_connection;
1090
1091           wrapper_connection = g_tcp_wrapper_connection_new (data->connection,
1092                                                              data->current_socket);
1093           g_object_unref (data->connection);
1094           data->connection = (GIOStream *)wrapper_connection;
1095         }
1096
1097       g_simple_async_result_set_op_res_gpointer (data->result,
1098                                                  data->connection,
1099                                                  g_object_unref);
1100     }
1101
1102   g_simple_async_result_complete (data->result);
1103   g_object_unref (data->result);
1104   g_object_unref (data->connectable);
1105   g_object_unref (data->enumerator);
1106   if (data->cancellable)
1107     g_object_unref (data->cancellable);
1108   if (data->current_socket)
1109     g_object_unref (data->current_socket);
1110   if (data->proxy_addr)
1111     g_object_unref (data->proxy_addr);
1112   g_slice_free (GSocketClientAsyncConnectData, data);
1113 }
1114
1115
1116 static void
1117 g_socket_client_enumerator_callback (GObject      *object,
1118                                      GAsyncResult *result,
1119                                      gpointer      user_data);
1120
1121 static void
1122 set_last_error (GSocketClientAsyncConnectData *data,
1123                 GError *error)
1124 {
1125   g_clear_error (&data->last_error);
1126   data->last_error = error;
1127 }
1128
1129 static void
1130 enumerator_next_async (GSocketClientAsyncConnectData *data)
1131 {
1132   g_socket_address_enumerator_next_async (data->enumerator,
1133                                           data->cancellable,
1134                                           g_socket_client_enumerator_callback,
1135                                           data);
1136 }
1137
1138 static void
1139 g_socket_client_tls_handshake_callback (GObject      *object,
1140                                         GAsyncResult *result,
1141                                         gpointer      user_data)
1142 {
1143   GSocketClientAsyncConnectData *data = user_data;
1144
1145   if (g_tls_connection_handshake_finish (G_TLS_CONNECTION (object),
1146                                          result,
1147                                          &data->last_error))
1148     {
1149       g_object_unref (data->connection);
1150       data->connection = G_IO_STREAM (object);
1151     }
1152   else
1153     {
1154       g_object_unref (object);
1155       g_object_unref (data->current_socket);
1156       data->current_socket = NULL;
1157       g_object_unref (data->connection);
1158       data->connection = NULL;
1159
1160       enumerator_next_async (data);
1161     }
1162
1163   g_socket_client_async_connect_complete (data);
1164 }
1165
1166 static void
1167 g_socket_client_tls_handshake (GSocketClientAsyncConnectData *data)
1168 {
1169   GTlsClientConnection *tlsconn;
1170
1171   if (!data->client->priv->tls)
1172     {
1173       g_socket_client_async_connect_complete (data);
1174       return;
1175     }
1176
1177   tlsconn = g_tls_client_connection_new (data->connection,
1178                                          data->connectable,
1179                                          &data->last_error);
1180   if (tlsconn)
1181     {
1182       g_tls_client_connection_set_validation_flags (tlsconn, data->client->priv->tls_validation_flags);
1183       g_tls_connection_handshake_async (G_TLS_CONNECTION (tlsconn),
1184                                         G_PRIORITY_DEFAULT,
1185                                         data->cancellable,
1186                                         g_socket_client_tls_handshake_callback,
1187                                         data);
1188     }
1189   else
1190     {
1191       g_object_unref (data->current_socket);
1192       data->current_socket = NULL;
1193       g_object_unref (data->connection);
1194       data->connection = NULL;
1195
1196       enumerator_next_async (data);
1197     }
1198 }
1199
1200 static void
1201 g_socket_client_proxy_connect_callback (GObject      *object,
1202                                         GAsyncResult *result,
1203                                         gpointer      user_data)
1204 {
1205   GSocketClientAsyncConnectData *data = user_data;
1206
1207   g_object_unref (data->connection);
1208   data->connection = g_proxy_connect_finish (G_PROXY (object),
1209                                              result,
1210                                              &data->last_error);
1211   if (!data->connection)
1212     {
1213       g_object_unref (data->current_socket);
1214       data->current_socket = NULL;
1215
1216       enumerator_next_async (data);
1217       return;
1218     }
1219
1220   g_socket_client_tls_handshake (data);
1221 }
1222
1223 static void
1224 g_socket_client_proxy_connect (GSocketClientAsyncConnectData *data)
1225 {
1226   GProxy *proxy;
1227   const gchar *protocol;
1228
1229   if (!data->proxy_addr)
1230     {
1231       g_socket_client_tls_handshake (data);
1232       return;
1233     }
1234
1235   protocol  = g_proxy_address_get_protocol (data->proxy_addr);
1236   proxy = g_proxy_get_default_for_protocol (protocol);
1237
1238   /* The connection should not be anything else then TCP Connection,
1239    * but let's put a safety guard in case
1240    */
1241   if (!G_IS_TCP_CONNECTION (data->connection))
1242     {
1243       g_critical ("Trying to proxy over non-TCP connection, this is "
1244           "most likely a bug in GLib IO library.");
1245
1246       g_set_error_literal (&data->last_error,
1247           G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
1248           _("Trying to proxy over non-TCP connection is not supported."));
1249
1250       g_object_unref (data->connection);
1251       data->connection = NULL;
1252       g_object_unref (data->current_socket);
1253       data->current_socket = NULL;
1254
1255       enumerator_next_async (data);
1256     }
1257   else if (proxy)
1258     {
1259       g_proxy_connect_async (proxy,
1260                              data->connection,
1261                              data->proxy_addr,
1262                              data->cancellable,
1263                              g_socket_client_proxy_connect_callback,
1264                              data);
1265       g_object_unref (proxy);
1266     }
1267   else if (!g_hash_table_lookup_extended (data->client->priv->app_proxies,
1268                                           protocol, NULL, NULL))
1269     {
1270       g_clear_error (&data->last_error);
1271
1272       g_set_error (&data->last_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
1273           _("Proxy protocol '%s' is not supported."),
1274           protocol);
1275
1276       g_object_unref (data->current_socket);
1277       data->current_socket = NULL;
1278       g_object_unref (data->connection);
1279       data->connection = NULL;
1280       g_object_unref (data->current_socket);
1281       data->current_socket = NULL;
1282
1283       enumerator_next_async (data);
1284     }
1285 }
1286
1287 static void
1288 g_socket_client_socket_connected (GSocketClientAsyncConnectData *data)
1289 {
1290   g_socket_set_blocking (data->current_socket, TRUE);
1291
1292   data->connection = (GIOStream *)
1293     g_socket_connection_factory_create_connection (data->current_socket);
1294
1295   g_socket_client_proxy_connect (data);
1296 }
1297
1298 static gboolean
1299 g_socket_client_socket_callback (GSocket *socket,
1300                                  GIOCondition condition,
1301                                  GSocketClientAsyncConnectData *data)
1302 {
1303   GError *error = NULL;
1304
1305   if (g_cancellable_is_cancelled (data->cancellable))
1306     {
1307       /* Cancelled, return done with last error being cancelled */
1308       g_clear_error (&data->last_error);
1309       g_object_unref (data->current_socket);
1310       data->current_socket = NULL;
1311       g_cancellable_set_error_if_cancelled (data->cancellable,
1312                                             &data->last_error);
1313
1314       g_socket_client_async_connect_complete (data);
1315       return FALSE;
1316     }
1317   else
1318     {
1319       /* socket is ready for writing means connect done, did it succeed? */
1320       if (!g_socket_check_connect_result (data->current_socket, &error))
1321         {
1322           set_last_error (data, error);
1323           g_object_unref (data->current_socket);
1324           data->current_socket = NULL;
1325
1326           /* try next one */
1327           enumerator_next_async (data);
1328
1329           return FALSE;
1330         }
1331     }
1332
1333   g_socket_client_socket_connected (data);
1334   return FALSE;
1335 }
1336
1337 static void
1338 g_socket_client_enumerator_callback (GObject      *object,
1339                                      GAsyncResult *result,
1340                                      gpointer      user_data)
1341 {
1342   GSocketClientAsyncConnectData *data = user_data;
1343   GSocketAddress *address = NULL;
1344   GSocket *socket;
1345   GError *tmp_error = NULL;
1346
1347   if (g_cancellable_is_cancelled (data->cancellable))
1348     {
1349       g_clear_error (&data->last_error);
1350       g_cancellable_set_error_if_cancelled (data->cancellable, &data->last_error);
1351       g_socket_client_async_connect_complete (data);
1352       return;
1353     }
1354
1355   address = g_socket_address_enumerator_next_finish (data->enumerator,
1356                                                      result, &tmp_error);
1357
1358   if (address == NULL)
1359     {
1360       if (tmp_error)
1361         set_last_error (data, tmp_error);
1362       else if (data->last_error == NULL)
1363         g_set_error_literal (&data->last_error, G_IO_ERROR, G_IO_ERROR_FAILED,
1364                              _("Unknown error on connect"));
1365
1366       g_socket_client_async_connect_complete (data);
1367       return;
1368     }
1369
1370   if (G_IS_PROXY_ADDRESS (address) &&
1371       data->client->priv->enable_proxy)
1372     data->proxy_addr = g_object_ref (G_PROXY_ADDRESS (address));
1373
1374   g_clear_error (&data->last_error);
1375
1376   socket = create_socket (data->client, address, &data->last_error);
1377   if (socket != NULL)
1378     {
1379       g_socket_set_blocking (socket, FALSE);
1380       if (g_socket_connect (socket, address, data->cancellable, &tmp_error))
1381         {
1382           data->current_socket = socket;
1383           g_socket_client_socket_connected (data);
1384
1385           g_object_unref (address);
1386           return;
1387         }
1388       else if (g_error_matches (tmp_error, G_IO_ERROR, G_IO_ERROR_PENDING))
1389         {
1390           GSource *source;
1391
1392           data->current_socket = socket;
1393           g_error_free (tmp_error);
1394
1395           source = g_socket_create_source (socket, G_IO_OUT,
1396                                            data->cancellable);
1397           g_source_set_callback (source,
1398                                  (GSourceFunc) g_socket_client_socket_callback,
1399                                  data, NULL);
1400           g_source_attach (source, g_main_context_get_thread_default ());
1401           g_source_unref (source);
1402
1403           g_object_unref (address);
1404           return;
1405         }
1406       else
1407         {
1408           data->last_error = tmp_error;
1409           g_object_unref (socket);
1410         }
1411     }
1412
1413   g_object_unref (address);
1414   enumerator_next_async (data);
1415 }
1416
1417 /**
1418  * g_socket_client_connect_async:
1419  * @client: a #GTcpClient
1420  * @connectable: a #GSocketConnectable specifying the remote address.
1421  * @cancellable: a #GCancellable, or %NULL
1422  * @callback: a #GAsyncReadyCallback
1423  * @user_data: user data for the callback
1424  *
1425  * This is the asynchronous version of g_socket_client_connect().
1426  *
1427  * When the operation is finished @callback will be
1428  * called. You can then call g_socket_client_connect_finish() to get
1429  * the result of the operation.
1430  *
1431  * Since: 2.22
1432  */
1433 void
1434 g_socket_client_connect_async (GSocketClient       *client,
1435                                GSocketConnectable  *connectable,
1436                                GCancellable        *cancellable,
1437                                GAsyncReadyCallback  callback,
1438                                gpointer             user_data)
1439 {
1440   GSocketClientAsyncConnectData *data;
1441
1442   g_return_if_fail (G_IS_SOCKET_CLIENT (client));
1443
1444   data = g_slice_new0 (GSocketClientAsyncConnectData);
1445
1446   data->result = g_simple_async_result_new (G_OBJECT (client),
1447                                             callback, user_data,
1448                                             g_socket_client_connect_async);
1449   data->client = client;
1450   if (cancellable)
1451     data->cancellable = g_object_ref (cancellable);
1452   else
1453     data->cancellable = NULL;
1454   data->last_error = NULL;
1455   data->connectable = g_object_ref (connectable);
1456
1457   if (can_use_proxy (client))
1458       data->enumerator = g_socket_connectable_proxy_enumerate (connectable);
1459   else
1460       data->enumerator = g_socket_connectable_enumerate (connectable);
1461
1462   enumerator_next_async (data);
1463 }
1464
1465 /**
1466  * g_socket_client_connect_to_host_async:
1467  * @client: a #GTcpClient
1468  * @host_and_port: the name and optionally the port of the host to connect to
1469  * @default_port: the default port to connect to
1470  * @cancellable: a #GCancellable, or %NULL
1471  * @callback: a #GAsyncReadyCallback
1472  * @user_data: user data for the callback
1473  *
1474  * This is the asynchronous version of g_socket_client_connect_to_host().
1475  *
1476  * When the operation is finished @callback will be
1477  * called. You can then call g_socket_client_connect_to_host_finish() to get
1478  * the result of the operation.
1479  *
1480  * Since: 2.22
1481  */
1482 void
1483 g_socket_client_connect_to_host_async (GSocketClient        *client,
1484                                        const gchar          *host_and_port,
1485                                        guint16               default_port,
1486                                        GCancellable         *cancellable,
1487                                        GAsyncReadyCallback   callback,
1488                                        gpointer              user_data)
1489 {
1490   GSocketConnectable *connectable;
1491   GError *error;
1492
1493   error = NULL;
1494   connectable = g_network_address_parse (host_and_port, default_port,
1495                                          &error);
1496   if (connectable == NULL)
1497     {
1498       g_simple_async_report_take_gerror_in_idle (G_OBJECT (client),
1499                                             callback, user_data, error);
1500     }
1501   else
1502     {
1503       g_socket_client_connect_async (client,
1504                                      connectable, cancellable,
1505                                      callback, user_data);
1506       g_object_unref (connectable);
1507     }
1508 }
1509
1510 /**
1511  * g_socket_client_connect_to_service_async:
1512  * @client: a #GSocketClient
1513  * @domain: a domain name
1514  * @service: the name of the service to connect to
1515  * @cancellable: a #GCancellable, or %NULL
1516  * @callback: a #GAsyncReadyCallback
1517  * @user_data: user data for the callback
1518  *
1519  * This is the asynchronous version of
1520  * g_socket_client_connect_to_service().
1521  *
1522  * Since: 2.22
1523  */
1524 void
1525 g_socket_client_connect_to_service_async (GSocketClient       *client,
1526                                           const gchar         *domain,
1527                                           const gchar         *service,
1528                                           GCancellable        *cancellable,
1529                                           GAsyncReadyCallback  callback,
1530                                           gpointer             user_data)
1531 {
1532   GSocketConnectable *connectable;
1533
1534   connectable = g_network_service_new (service, "tcp", domain);
1535   g_socket_client_connect_async (client,
1536                                  connectable, cancellable,
1537                                  callback, user_data);
1538   g_object_unref (connectable);
1539 }
1540
1541 /**
1542  * g_socket_client_connect_to_uri_async:
1543  * @client: a #GSocketClient
1544  * @uri: a network uri
1545  * @default_port: the default port to connect to
1546  * @cancellable: a #GCancellable, or %NULL
1547  * @callback: a #GAsyncReadyCallback
1548  * @user_data: user data for the callback
1549  *
1550  * This is the asynchronous version of g_socket_client_connect_to_uri().
1551  *
1552  * When the operation is finished @callback will be
1553  * called. You can then call g_socket_client_connect_to_uri_finish() to get
1554  * the result of the operation.
1555  *
1556  * Since: 2.26
1557  */
1558 void
1559 g_socket_client_connect_to_uri_async (GSocketClient        *client,
1560                                       const gchar          *uri,
1561                                       guint16               default_port,
1562                                       GCancellable         *cancellable,
1563                                       GAsyncReadyCallback   callback,
1564                                       gpointer              user_data)
1565 {
1566   GSocketConnectable *connectable;
1567   GError *error;
1568
1569   error = NULL;
1570   connectable = g_network_address_parse_uri (uri, default_port, &error);
1571   if (connectable == NULL)
1572     {
1573       g_simple_async_report_take_gerror_in_idle (G_OBJECT (client),
1574                                             callback, user_data, error);
1575     }
1576   else
1577     {
1578       g_socket_client_connect_async (client,
1579                                      connectable, cancellable,
1580                                      callback, user_data);
1581       g_object_unref (connectable);
1582     }
1583 }
1584
1585
1586 /**
1587  * g_socket_client_connect_finish:
1588  * @client: a #GSocketClient.
1589  * @result: a #GAsyncResult.
1590  * @error: a #GError location to store the error occuring, or %NULL to
1591  * ignore.
1592  *
1593  * Finishes an async connect operation. See g_socket_client_connect_async()
1594  *
1595  * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1596  *
1597  * Since: 2.22
1598  */
1599 GSocketConnection *
1600 g_socket_client_connect_finish (GSocketClient  *client,
1601                                 GAsyncResult   *result,
1602                                 GError        **error)
1603 {
1604   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1605
1606   if (g_simple_async_result_propagate_error (simple, error))
1607     return NULL;
1608
1609   return g_object_ref (g_simple_async_result_get_op_res_gpointer (simple));
1610 }
1611
1612 /**
1613  * g_socket_client_connect_to_host_finish:
1614  * @client: a #GSocketClient.
1615  * @result: a #GAsyncResult.
1616  * @error: a #GError location to store the error occuring, or %NULL to
1617  * ignore.
1618  *
1619  * Finishes an async connect operation. See g_socket_client_connect_to_host_async()
1620  *
1621  * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1622  *
1623  * Since: 2.22
1624  */
1625 GSocketConnection *
1626 g_socket_client_connect_to_host_finish (GSocketClient  *client,
1627                                         GAsyncResult   *result,
1628                                         GError        **error)
1629 {
1630   return g_socket_client_connect_finish (client, result, error);
1631 }
1632
1633 /**
1634  * g_socket_client_connect_to_service_finish:
1635  * @client: a #GSocketClient.
1636  * @result: a #GAsyncResult.
1637  * @error: a #GError location to store the error occuring, or %NULL to
1638  * ignore.
1639  *
1640  * Finishes an async connect operation. See g_socket_client_connect_to_service_async()
1641  *
1642  * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1643  *
1644  * Since: 2.22
1645  */
1646 GSocketConnection *
1647 g_socket_client_connect_to_service_finish (GSocketClient  *client,
1648                                            GAsyncResult   *result,
1649                                            GError        **error)
1650 {
1651   return g_socket_client_connect_finish (client, result, error);
1652 }
1653
1654 /**
1655  * g_socket_client_connect_to_uri_finish:
1656  * @client: a #GSocketClient.
1657  * @result: a #GAsyncResult.
1658  * @error: a #GError location to store the error occuring, or %NULL to
1659  * ignore.
1660  *
1661  * Finishes an async connect operation. See g_socket_client_connect_to_uri_async()
1662  *
1663  * Returns: (transfer full): a #GSocketConnection on success, %NULL on error.
1664  *
1665  * Since: 2.26
1666  */
1667 GSocketConnection *
1668 g_socket_client_connect_to_uri_finish (GSocketClient  *client,
1669                                        GAsyncResult   *result,
1670                                        GError        **error)
1671 {
1672   return g_socket_client_connect_finish (client, result, error);
1673 }
1674
1675 /**
1676  * g_socket_client_add_application_proxy:
1677  * @client: a #GSocketClient
1678  * @protocol: The proxy protocol
1679  *
1680  * Enable proxy protocols to be handled by the application. When the
1681  * indicated proxy protocol is returned by the #GProxyResolver,
1682  * #GSocketClient will consider this protocol as supported but will
1683  * not try find a #GProxy instance to handle handshaking. The
1684  * application must check for this case by calling
1685  * g_socket_connection_get_remote_address() on the returned
1686  * #GSocketConnection, and seeing if it's a #GProxyAddress of the
1687  * appropriate type, to determine whether or not it needs to handle
1688  * the proxy handshaking itself.
1689  *
1690  * This should be used for proxy protocols that are dialects of
1691  * another protocol such as HTTP proxy. It also allows cohabitation of
1692  * proxy protocols that are reused between protocols. A good example
1693  * is HTTP. It can be used to proxy HTTP, FTP and Gopher and can also
1694  * be use as generic socket proxy through the HTTP CONNECT method.
1695  */
1696 void
1697 g_socket_client_add_application_proxy (GSocketClient *client,
1698                                        const gchar   *protocol)
1699 {
1700   g_hash_table_insert (client->priv->app_proxies, g_strdup (protocol), NULL);
1701 }