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