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