GIO: Add G-I annotations for several methods
[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
30 #include <gio/gioenumtypes.h>
31 #include <gio/gsocketaddressenumerator.h>
32 #include <gio/gsocketconnectable.h>
33 #include <gio/gsocketconnection.h>
34 #include <gio/gsimpleasyncresult.h>
35 #include <gio/gcancellable.h>
36 #include <gio/gioerror.h>
37 #include <gio/gsocket.h>
38 #include <gio/gnetworkaddress.h>
39 #include <gio/gnetworkservice.h>
40 #include <gio/gsocketaddress.h>
41 #include "glibintl.h"
42
43
44 /**
45  * SECTION:gsocketclient
46  * @short_description: Helper for connecting to a network service
47  * @include: gio/gio.h
48  * @see_also: #GSocketConnection, #GSocketListener
49  *
50  * #GSocketClient is a high-level utility class for connecting to a
51  * network host using a connection oriented socket type.
52  *
53  * You create a #GSocketClient object, set any options you want, then
54  * call a sync or async connect operation, which returns a #GSocketConnection
55  * subclass on success.
56  *
57  * The type of the #GSocketConnection object returned depends on the type of
58  * the underlying socket that is in use. For instance, for a TCP/IP connection
59  * it will be a #GTcpConnection.
60  *
61  * Since: 2.22
62  */
63
64
65 G_DEFINE_TYPE (GSocketClient, g_socket_client, G_TYPE_OBJECT);
66
67 enum
68 {
69   PROP_NONE,
70   PROP_FAMILY,
71   PROP_TYPE,
72   PROP_PROTOCOL,
73   PROP_LOCAL_ADDRESS
74 };
75
76 struct _GSocketClientPrivate
77 {
78   GSocketFamily family;
79   GSocketType type;
80   GSocketProtocol protocol;
81   GSocketAddress *local_address;
82 };
83
84 static GSocket *
85 create_socket (GSocketClient  *client,
86                GSocketAddress *dest_address,
87                GError        **error)
88 {
89   GSocketFamily family;
90   GSocket *socket;
91
92   family = client->priv->family;
93   if (family == G_SOCKET_FAMILY_INVALID &&
94       client->priv->local_address != NULL)
95     family = g_socket_address_get_family (client->priv->local_address);
96   if (family == G_SOCKET_FAMILY_INVALID)
97     family = g_socket_address_get_family (dest_address);
98
99   socket = g_socket_new (family,
100                          client->priv->type,
101                          client->priv->protocol,
102                          error);
103   if (socket == NULL)
104     return NULL;
105
106   if (client->priv->local_address)
107     {
108       if (!g_socket_bind (socket,
109                           client->priv->local_address,
110                           FALSE,
111                           error))
112         {
113           g_object_unref (socket);
114           return NULL;
115         }
116     }
117
118   return socket;
119 }
120
121 static void
122 g_socket_client_init (GSocketClient *client)
123 {
124   client->priv = G_TYPE_INSTANCE_GET_PRIVATE (client,
125                                               G_TYPE_SOCKET_CLIENT,
126                                               GSocketClientPrivate);
127   client->priv->type = G_SOCKET_TYPE_STREAM;
128 }
129
130 /**
131  * g_socket_client_new:
132  *
133  * Creates a new #GSocketClient with the default options.
134  *
135  * Returns: a #GSocketClient.
136  *     Free the returned object with g_object_unref().
137  *
138  * Since: 2.22
139  */
140 GSocketClient *
141 g_socket_client_new (void)
142 {
143   return g_object_new (G_TYPE_SOCKET_CLIENT, NULL);
144 }
145
146 static void
147 g_socket_client_finalize (GObject *object)
148 {
149   GSocketClient *client = G_SOCKET_CLIENT (object);
150
151   if (client->priv->local_address)
152     g_object_unref (client->priv->local_address);
153
154   if (G_OBJECT_CLASS (g_socket_client_parent_class)->finalize)
155     (*G_OBJECT_CLASS (g_socket_client_parent_class)->finalize) (object);
156 }
157
158 static void
159 g_socket_client_get_property (GObject    *object,
160                               guint       prop_id,
161                               GValue     *value,
162                               GParamSpec *pspec)
163 {
164   GSocketClient *client = G_SOCKET_CLIENT (object);
165
166   switch (prop_id)
167     {
168       case PROP_FAMILY:
169         g_value_set_enum (value, client->priv->family);
170         break;
171
172       case PROP_TYPE:
173         g_value_set_enum (value, client->priv->type);
174         break;
175
176       case PROP_PROTOCOL:
177         g_value_set_enum (value, client->priv->protocol);
178         break;
179
180       case PROP_LOCAL_ADDRESS:
181         g_value_set_object (value, client->priv->local_address);
182         break;
183
184       default:
185         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
186     }
187 }
188
189 static void
190 g_socket_client_set_property (GObject      *object,
191                               guint         prop_id,
192                               const GValue *value,
193                               GParamSpec   *pspec)
194 {
195   GSocketClient *client = G_SOCKET_CLIENT (object);
196
197   switch (prop_id)
198     {
199     case PROP_FAMILY:
200       g_socket_client_set_family (client, g_value_get_enum (value));
201       break;
202
203     case PROP_TYPE:
204       g_socket_client_set_socket_type (client, g_value_get_enum (value));
205       break;
206
207     case PROP_PROTOCOL:
208       g_socket_client_set_protocol (client, g_value_get_enum (value));
209       break;
210
211     case PROP_LOCAL_ADDRESS:
212       g_socket_client_set_local_address (client, g_value_get_object (value));
213       break;
214
215     default:
216       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
217     }
218 }
219
220 /**
221  * g_socket_client_get_family:
222  * @client: a #GSocketClient.
223  *
224  * Gets the socket family of the socket client.
225  *
226  * See g_socket_client_set_family() for details.
227  *
228  * Returns: a #GSocketFamily
229  *
230  * Since: 2.22
231  */
232 GSocketFamily
233 g_socket_client_get_family (GSocketClient *client)
234 {
235   return client->priv->family;
236 }
237
238 /**
239  * g_socket_client_set_family:
240  * @client: a #GSocketClient.
241  * @family: a #GSocketFamily
242  *
243  * Sets the socket family of the socket client.
244  * If this is set to something other than %G_SOCKET_FAMILY_INVALID
245  * then the sockets created by this object will be of the specified
246  * family.
247  *
248  * This might be useful for instance if you want to force the local
249  * connection to be an ipv4 socket, even though the address might
250  * be an ipv6 mapped to ipv4 address.
251  *
252  * Since: 2.22
253  */
254 void
255 g_socket_client_set_family (GSocketClient *client,
256                             GSocketFamily  family)
257 {
258   if (client->priv->family == family)
259     return;
260
261   client->priv->family = family;
262   g_object_notify (G_OBJECT (client), "family");
263 }
264
265 /**
266  * g_socket_client_get_socket_type:
267  * @client: a #GSocketClient.
268  *
269  * Gets the socket type of the socket client.
270  *
271  * See g_socket_client_set_socket_type() for details.
272  *
273  * Returns: a #GSocketFamily
274  *
275  * Since: 2.22
276  */
277 GSocketType
278 g_socket_client_get_socket_type (GSocketClient *client)
279 {
280   return client->priv->type;
281 }
282
283 /**
284  * g_socket_client_set_socket_type:
285  * @client: a #GSocketClient.
286  * @type: a #GSocketType
287  *
288  * Sets the socket type of the socket client.
289  * The sockets created by this object will be of the specified
290  * type.
291  *
292  * It doesn't make sense to specify a type of %G_SOCKET_TYPE_DATAGRAM,
293  * as GSocketClient is used for connection oriented services.
294  *
295  * Since: 2.22
296  */
297 void
298 g_socket_client_set_socket_type (GSocketClient *client,
299                                  GSocketType    type)
300 {
301   if (client->priv->type == type)
302     return;
303
304   client->priv->type = type;
305   g_object_notify (G_OBJECT (client), "type");
306 }
307
308 /**
309  * g_socket_client_get_protocol:
310  * @client: a #GSocketClient
311  *
312  * Gets the protocol name type of the socket client.
313  *
314  * See g_socket_client_set_protocol() for details.
315  *
316  * Returns: a #GSocketProtocol
317  *
318  * Since: 2.22
319  */
320 GSocketProtocol
321 g_socket_client_get_protocol (GSocketClient *client)
322 {
323   return client->priv->protocol;
324 }
325
326 /**
327  * g_socket_client_set_protocol:
328  * @client: a #GSocketClient.
329  * @protocol: a #GSocketProtocol
330  *
331  * Sets the protocol of the socket client.
332  * The sockets created by this object will use of the specified
333  * protocol.
334  *
335  * If @protocol is %0 that means to use the default
336  * protocol for the socket family and type.
337  *
338  * Since: 2.22
339  */
340 void
341 g_socket_client_set_protocol (GSocketClient   *client,
342                               GSocketProtocol  protocol)
343 {
344   if (client->priv->protocol == protocol)
345     return;
346
347   client->priv->protocol = protocol;
348   g_object_notify (G_OBJECT (client), "protocol");
349 }
350
351 /**
352  * g_socket_client_get_local_address:
353  * @client: a #GSocketClient.
354  *
355  * Gets the local address of the socket client.
356  *
357  * See g_socket_client_set_local_address() for details.
358  *
359  * Returns: (transfer none): a #GSocketAddres or %NULL. don't free
360  *
361  * Since: 2.22
362  */
363 GSocketAddress *
364 g_socket_client_get_local_address (GSocketClient *client)
365 {
366   return client->priv->local_address;
367 }
368
369 /**
370  * g_socket_client_set_local_address:
371  * @client: a #GSocketClient.
372  * @address: a #GSocketAddress, or %NULL
373  *
374  * Sets the local address of the socket client.
375  * The sockets created by this object will bound to the
376  * specified address (if not %NULL) before connecting.
377  *
378  * This is useful if you want to ensure the the local
379  * side of the connection is on a specific port, or on
380  * a specific interface.
381  *
382  * Since: 2.22
383  */
384 void
385 g_socket_client_set_local_address (GSocketClient  *client,
386                                    GSocketAddress *address)
387 {
388   if (address)
389   g_object_ref (address);
390
391   if (client->priv->local_address)
392     {
393       g_object_unref (client->priv->local_address);
394     }
395   client->priv->local_address = address;
396   g_object_notify (G_OBJECT (client), "local-address");
397 }
398
399 static void
400 g_socket_client_class_init (GSocketClientClass *class)
401 {
402   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
403
404   g_type_class_add_private (class, sizeof (GSocketClientPrivate));
405
406   gobject_class->finalize = g_socket_client_finalize;
407   gobject_class->set_property = g_socket_client_set_property;
408   gobject_class->get_property = g_socket_client_get_property;
409
410   g_object_class_install_property (gobject_class, PROP_FAMILY,
411                                    g_param_spec_enum ("family",
412                                                       P_("Socket family"),
413                                                       P_("The sockets address family to use for socket construction"),
414                                                       G_TYPE_SOCKET_FAMILY,
415                                                       G_SOCKET_FAMILY_INVALID,
416                                                       G_PARAM_CONSTRUCT |
417                                                       G_PARAM_READWRITE |
418                                                       G_PARAM_STATIC_STRINGS));
419
420   g_object_class_install_property (gobject_class, PROP_TYPE,
421                                    g_param_spec_enum ("type",
422                                                       P_("Socket type"),
423                                                       P_("The sockets type to use for socket construction"),
424                                                       G_TYPE_SOCKET_TYPE,
425                                                       G_SOCKET_TYPE_STREAM,
426                                                       G_PARAM_CONSTRUCT |
427                                                       G_PARAM_READWRITE |
428                                                       G_PARAM_STATIC_STRINGS));
429
430   g_object_class_install_property (gobject_class, PROP_PROTOCOL,
431                                    g_param_spec_enum ("protocol",
432                                                       P_("Socket protocol"),
433                                                       P_("The protocol to use for socket construction, or 0 for default"),
434                                                       G_TYPE_SOCKET_PROTOCOL,
435                                                       G_SOCKET_PROTOCOL_DEFAULT,
436                                                       G_PARAM_CONSTRUCT |
437                                                       G_PARAM_READWRITE |
438                                                       G_PARAM_STATIC_STRINGS));
439
440   g_object_class_install_property (gobject_class, PROP_LOCAL_ADDRESS,
441                                    g_param_spec_object ("local-address",
442                                                         P_("Local address"),
443                                                         P_("The local address constructed sockets will be bound to"),
444                                                         G_TYPE_SOCKET_ADDRESS,
445                                                         G_PARAM_CONSTRUCT |
446                                                         G_PARAM_READWRITE |
447                                                         G_PARAM_STATIC_STRINGS));
448 }
449
450 /**
451  * g_socket_client_connect:
452  * @client: a #GSocketClient.
453  * @connectable: a #GSocketConnectable specifying the remote address.
454  * @cancellable: optional #GCancellable object, %NULL to ignore.
455  * @error: #GError for error reporting, or %NULL to ignore.
456  *
457  * Tries to resolve the @connectable and make a network connection to it..
458  *
459  * Upon a successful connection, a new #GSocketConnection is constructed
460  * and returned.  The caller owns this new object and must drop their
461  * reference to it when finished with it.
462  *
463  * The type of the #GSocketConnection object returned depends on the type of
464  * the underlying socket that is used. For instance, for a TCP/IP connection
465  * it will be a #GTcpConnection.
466  *
467  * The socket created will be the same family as the the address that the
468  * @connectable resolves to, unless family is set with g_socket_client_set_family()
469  * or indirectly via g_socket_client_set_local_address(). The socket type
470  * defaults to %G_SOCKET_TYPE_STREAM but can be set with
471  * g_socket_client_set_socket_type().
472  *
473  * If a local address is specified with g_socket_client_set_local_address() the
474  * socket will be bound to this address before connecting.
475  *
476  * Returns: a #GSocketConnection on success, %NULL on error.
477  *
478  * Since: 2.22
479  */
480 GSocketConnection *
481 g_socket_client_connect (GSocketClient       *client,
482                          GSocketConnectable  *connectable,
483                          GCancellable        *cancellable,
484                          GError             **error)
485 {
486   GSocketConnection *connection = NULL;
487   GSocketAddressEnumerator *enumerator;
488   GError *last_error, *tmp_error;
489
490   last_error = NULL;
491   enumerator = g_socket_connectable_enumerate (connectable);
492   while (connection == NULL)
493     {
494       GSocketAddress *address;
495       GSocket *socket;
496
497       if (g_cancellable_is_cancelled (cancellable))
498         {
499           g_clear_error (error);
500           g_cancellable_set_error_if_cancelled (cancellable, error);
501           break;
502         }
503
504       tmp_error = NULL;
505       address = g_socket_address_enumerator_next (enumerator, cancellable,
506                                                   &tmp_error);
507       if (address == NULL)
508         {
509           if (tmp_error)
510             {
511               g_clear_error (&last_error);
512               g_propagate_error (error, tmp_error);
513             }
514           else if (last_error)
515             {
516               g_propagate_error (error, last_error);
517             }
518           else
519             g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
520                                  _("Unknown error on connect"));
521           break;
522         }
523
524       /* clear error from previous attempt */
525       g_clear_error (&last_error);
526
527       socket = create_socket (client, address, &last_error);
528       if (socket != NULL)
529         {
530           if (g_socket_connect (socket, address, cancellable, &last_error))
531             connection = g_socket_connection_factory_create_connection (socket);
532
533           g_object_unref (socket);
534         }
535
536       g_object_unref (address);
537     }
538   g_object_unref (enumerator);
539
540   return connection;
541 }
542
543 /**
544  * g_socket_client_connect_to_host:
545  * @client: a #SocketClient
546  * @host_and_port: the name and optionally port of the host to connect to
547  * @default_port: the default port to connect to
548  * @cancellable: a #GCancellable, or %NULL
549  * @error: a pointer to a #GError, or %NULL
550  *
551  * This is a helper function for g_socket_client_connect().
552  *
553  * Attempts to create a TCP connection to the named host.
554  *
555  * @host_and_port may be in any of a number of recognised formats: an IPv6
556  * address, an IPv4 address, or a domain name (in which case a DNS
557  * lookup is performed).  Quoting with [] is supported for all address
558  * types.  A port override may be specified in the usual way with a
559  * colon.  Ports may be given as decimal numbers or symbolic names (in
560  * which case an /etc/services lookup is performed).
561  *
562  * If no port override is given in @host_and_port then @default_port will be
563  * used as the port number to connect to.
564  *
565  * In general, @host_and_port is expected to be provided by the user (allowing
566  * them to give the hostname, and a port overide if necessary) and
567  * @default_port is expected to be provided by the application.
568  *
569  * In the case that an IP address is given, a single connection
570  * attempt is made.  In the case that a name is given, multiple
571  * connection attempts may be made, in turn and according to the
572  * number of address records in DNS, until a connection succeeds.
573  *
574  * Upon a successful connection, a new #GSocketConnection is constructed
575  * and returned.  The caller owns this new object and must drop their
576  * reference to it when finished with it.
577  *
578  * In the event of any failure (DNS error, service not found, no hosts
579  * connectable) %NULL is returned and @error (if non-%NULL) is set
580  * accordingly.
581  *
582  Returns: a #GSocketConnection on success, %NULL on error.
583  *
584  * Since: 2.22
585  */
586 GSocketConnection *
587 g_socket_client_connect_to_host (GSocketClient  *client,
588                                  const gchar    *host_and_port,
589                                  guint16         default_port,
590                                  GCancellable   *cancellable,
591                                  GError        **error)
592 {
593   GSocketConnectable *connectable;
594   GSocketConnection *connection;
595
596   connectable = g_network_address_parse (host_and_port, default_port, error);
597   if (connectable == NULL)
598     return NULL;
599
600   connection = g_socket_client_connect (client, connectable,
601                                         cancellable, error);
602   g_object_unref (connectable);
603
604   return connection;
605 }
606
607 /**
608  * g_socket_client_connect_to_service:
609  * @client: a #GSocketConnection
610  * @domain: a domain name
611  * @service: the name of the service to connect to
612  * @cancellable: a #GCancellable, or %NULL
613  * @error: a pointer to a #GError, or %NULL
614  * @returns: a #GSocketConnection if successful, or %NULL on error
615  *
616  * Attempts to create a TCP connection to a service.
617  *
618  * This call looks up the SRV record for @service at @domain for the
619  * "tcp" protocol.  It then attempts to connect, in turn, to each of
620  * the hosts providing the service until either a connection succeeds
621  * or there are no hosts remaining.
622  *
623  * Upon a successful connection, a new #GSocketConnection is constructed
624  * and returned.  The caller owns this new object and must drop their
625  * reference to it when finished with it.
626  *
627  * In the event of any failure (DNS error, service not found, no hosts
628  * connectable) %NULL is returned and @error (if non-%NULL) is set
629  * accordingly.
630  */
631 GSocketConnection *
632 g_socket_client_connect_to_service (GSocketClient  *client,
633                                     const gchar    *domain,
634                                     const gchar    *service,
635                                     GCancellable   *cancellable,
636                                     GError        **error)
637 {
638   GSocketConnectable *connectable;
639   GSocketConnection *connection;
640
641   connectable = g_network_service_new (service, "tcp", domain);
642   connection = g_socket_client_connect (client, connectable,
643                                         cancellable, error);
644   g_object_unref (connectable);
645
646   return connection;
647 }
648
649 typedef struct
650 {
651   GSimpleAsyncResult *result;
652   GCancellable *cancellable;
653   GSocketClient *client;
654
655   GSocketAddressEnumerator *enumerator;
656   GSocket *current_socket;
657
658   GError *last_error;
659 } GSocketClientAsyncConnectData;
660
661 static void
662 g_socket_client_async_connect_complete (GSocketClientAsyncConnectData *data)
663 {
664   GSocketConnection *connection;
665
666   if (data->last_error)
667     {
668       g_simple_async_result_set_from_error (data->result, data->last_error);
669       g_error_free (data->last_error);
670     }
671   else
672     {
673       g_assert (data->current_socket);
674
675       g_socket_set_blocking (data->current_socket, TRUE);
676
677       connection = g_socket_connection_factory_create_connection (data->current_socket);
678       g_object_unref (data->current_socket);
679       g_simple_async_result_set_op_res_gpointer (data->result,
680                                                  connection,
681                                                  g_object_unref);
682     }
683
684   g_simple_async_result_complete (data->result);
685   g_object_unref (data->result);
686   g_object_unref (data->enumerator);
687   g_slice_free (GSocketClientAsyncConnectData, data);
688 }
689
690
691 static void
692 g_socket_client_enumerator_callback (GObject      *object,
693                                      GAsyncResult *result,
694                                      gpointer      user_data);
695
696 static void
697 set_last_error (GSocketClientAsyncConnectData *data,
698                 GError *error)
699 {
700   g_clear_error (&data->last_error);
701   data->last_error = error;
702 }
703
704 static gboolean
705 g_socket_client_socket_callback (GSocket *socket,
706                                  GIOCondition condition,
707                                  GSocketClientAsyncConnectData *data)
708 {
709   GError *error = NULL;
710
711   if (g_cancellable_is_cancelled (data->cancellable))
712     {
713       /* Cancelled, return done with last error being cancelled */
714       g_clear_error (&data->last_error);
715       g_object_unref (data->current_socket);
716       data->current_socket = NULL;
717       g_cancellable_set_error_if_cancelled (data->cancellable,
718                                             &data->last_error);
719     }
720   else
721     {
722       /* socket is ready for writing means connect done, did it succeed? */
723       if (!g_socket_check_connect_result (data->current_socket, &error))
724         {
725           set_last_error (data, error);
726
727           /* try next one */
728           g_socket_address_enumerator_next_async (data->enumerator,
729                                                   data->cancellable,
730                                                   g_socket_client_enumerator_callback,
731                                                   data);
732
733           return FALSE;
734         }
735     }
736
737   g_socket_client_async_connect_complete (data);
738
739   return FALSE;
740 }
741
742 static void
743 g_socket_client_enumerator_callback (GObject      *object,
744                                      GAsyncResult *result,
745                                      gpointer      user_data)
746 {
747   GSocketClientAsyncConnectData *data = user_data;
748   GSocketAddress *address;
749   GSocket *socket;
750   GError *tmp_error = NULL;
751
752   if (g_cancellable_is_cancelled (data->cancellable))
753     {
754       g_clear_error (&data->last_error);
755       g_cancellable_set_error_if_cancelled (data->cancellable, &data->last_error);
756       g_socket_client_async_connect_complete (data);
757       return;
758     }
759
760   address = g_socket_address_enumerator_next_finish (data->enumerator,
761                                                      result, &tmp_error);
762
763   if (address == NULL)
764     {
765       if (tmp_error)
766         set_last_error (data, tmp_error);
767       else if (data->last_error == NULL)
768         g_set_error_literal (&data->last_error, G_IO_ERROR, G_IO_ERROR_FAILED,
769                              _("Unknown error on connect"));
770
771       g_socket_client_async_connect_complete (data);
772       return;
773     }
774
775   g_clear_error (&data->last_error);
776
777   socket = create_socket (data->client, address, &data->last_error);
778   if (socket != NULL)
779     {
780       g_socket_set_blocking (socket, FALSE);
781       if (g_socket_connect (socket, address, data->cancellable, &tmp_error))
782         {
783           data->current_socket = socket;
784           g_socket_client_async_connect_complete (data);
785
786           g_object_unref (address);
787           return;
788         }
789       else if (g_error_matches (tmp_error, G_IO_ERROR, G_IO_ERROR_PENDING))
790         {
791           GSource *source;
792
793           data->current_socket = socket;
794           g_error_free (tmp_error);
795
796           source = g_socket_create_source (socket, G_IO_OUT,
797                                            data->cancellable);
798           g_source_set_callback (source,
799                                  (GSourceFunc) g_socket_client_socket_callback,
800                                  data, NULL);
801           g_source_attach (source, g_main_context_get_thread_default ());
802           g_source_unref (source);
803
804           g_object_unref (address);
805           return;
806         }
807       else
808         {
809           data->last_error = tmp_error;
810           g_object_unref (socket);
811         }
812       g_object_unref (address);
813     }
814
815   g_socket_address_enumerator_next_async (data->enumerator,
816                                           data->cancellable,
817                                           g_socket_client_enumerator_callback,
818                                           data);
819 }
820
821 /**
822  * g_socket_client_connect_async:
823  * @client: a #GTcpClient
824  * @connectable: a #GSocketConnectable specifying the remote address.
825  * @cancellable: a #GCancellable, or %NULL
826  * @callback: a #GAsyncReadyCallback
827  * @user_data: user data for the callback
828  *
829  * This is the asynchronous version of g_socket_client_connect().
830  *
831  * When the operation is finished @callback will be
832  * called. You can then call g_socket_client_connect_finish() to get
833  * the result of the operation.
834  *
835  * Since: 2.22
836  */
837 void
838 g_socket_client_connect_async (GSocketClient       *client,
839                                GSocketConnectable  *connectable,
840                                GCancellable        *cancellable,
841                                GAsyncReadyCallback  callback,
842                                gpointer             user_data)
843 {
844   GSocketClientAsyncConnectData *data;
845
846   g_return_if_fail (G_IS_SOCKET_CLIENT (client));
847
848   data = g_slice_new (GSocketClientAsyncConnectData);
849
850   data->result = g_simple_async_result_new (G_OBJECT (client),
851                                             callback, user_data,
852                                             g_socket_client_connect_async);
853   data->client = client;
854   if (cancellable)
855     data->cancellable = g_object_ref (cancellable);
856   else
857     data->cancellable = NULL;
858   data->last_error = NULL;
859   data->enumerator = g_socket_connectable_enumerate (connectable);
860
861   g_socket_address_enumerator_next_async (data->enumerator, cancellable,
862                                           g_socket_client_enumerator_callback,
863                                           data);
864 }
865
866 /**
867  * g_socket_client_connect_to_host_async:
868  * @client: a #GTcpClient
869  * @host_and_port: the name and optionally the port of the host to connect to
870  * @default_port: the default port to connect to
871  * @cancellable: a #GCancellable, or %NULL
872  * @callback: a #GAsyncReadyCallback
873  * @user_data: user data for the callback
874  *
875  * This is the asynchronous version of g_socket_client_connect_to_host().
876  *
877  * When the operation is finished @callback will be
878  * called. You can then call g_socket_client_connect_to_host_finish() to get
879  * the result of the operation.
880  *
881  * Since: 2.22
882  */
883 void
884 g_socket_client_connect_to_host_async (GSocketClient        *client,
885                                        const gchar          *host_and_port,
886                                        guint16               default_port,
887                                        GCancellable         *cancellable,
888                                        GAsyncReadyCallback   callback,
889                                        gpointer              user_data)
890 {
891   GSocketConnectable *connectable;
892   GError *error;
893
894   error = NULL;
895   connectable = g_network_address_parse (host_and_port, default_port,
896                                          &error);
897   if (connectable == NULL)
898     {
899       g_simple_async_report_gerror_in_idle (G_OBJECT (client),
900                                             callback, user_data, error);
901       g_error_free (error);
902     }
903   else
904     {
905       g_socket_client_connect_async (client,
906                                      connectable, cancellable,
907                                      callback, user_data);
908       g_object_unref (connectable);
909     }
910 }
911
912 /**
913  * g_socket_client_connect_to_service_async:
914  * @client: a #GSocketClient
915  * @domain: a domain name
916  * @service: the name of the service to connect to
917  * @cancellable: a #GCancellable, or %NULL
918  * @callback: a #GAsyncReadyCallback
919  * @user_data: user data for the callback
920  *
921  * This is the asynchronous version of
922  * g_socket_client_connect_to_service().
923  *
924  * Since: 2.22
925  */
926 void
927 g_socket_client_connect_to_service_async (GSocketClient       *client,
928                                           const gchar         *domain,
929                                           const gchar         *service,
930                                           GCancellable        *cancellable,
931                                           GAsyncReadyCallback  callback,
932                                           gpointer             user_data)
933 {
934   GSocketConnectable *connectable;
935
936   connectable = g_network_service_new (service, "tcp", domain);
937   g_socket_client_connect_async (client,
938                                  connectable, cancellable,
939                                  callback, user_data);
940   g_object_unref (connectable);
941 }
942
943 /**
944  * g_socket_client_connect_finish:
945  * @client: a #GSocketClient.
946  * @result: a #GAsyncResult.
947  * @error: a #GError location to store the error occuring, or %NULL to
948  * ignore.
949  *
950  * Finishes an async connect operation. See g_socket_client_connect_async()
951  *
952  * Returns: a #GSocketConnection on success, %NULL on error.
953  *
954  * Since: 2.22
955  */
956 GSocketConnection *
957 g_socket_client_connect_finish (GSocketClient  *client,
958                                 GAsyncResult   *result,
959                                 GError        **error)
960 {
961   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
962
963   if (g_simple_async_result_propagate_error (simple, error))
964     return NULL;
965
966   return g_object_ref (g_simple_async_result_get_op_res_gpointer (simple));
967 }
968
969 /**
970  * g_socket_client_connect_to_host_finish:
971  * @client: a #GSocketClient.
972  * @result: a #GAsyncResult.
973  * @error: a #GError location to store the error occuring, or %NULL to
974  * ignore.
975  *
976  * Finishes an async connect operation. See g_socket_client_connect_to_host_async()
977  *
978  * Returns: a #GSocketConnection on success, %NULL on error.
979  *
980  * Since: 2.22
981  */
982 GSocketConnection *
983 g_socket_client_connect_to_host_finish (GSocketClient  *client,
984                                         GAsyncResult   *result,
985                                         GError        **error)
986 {
987   return g_socket_client_connect_finish (client, result, error);
988 }
989
990 /**
991  * g_socket_client_connect_to_service_finish:
992  * @client: a #GSocketClient.
993  * @result: a #GAsyncResult.
994  * @error: a #GError location to store the error occuring, or %NULL to
995  * ignore.
996  *
997  * Finishes an async connect operation. See g_socket_client_connect_to_service_async()
998  *
999  * Returns: a #GSocketConnection on success, %NULL on error.
1000  *
1001  * Since: 2.22
1002  */
1003 GSocketConnection *
1004 g_socket_client_connect_to_service_finish (GSocketClient  *client,
1005                                            GAsyncResult   *result,
1006                                            GError        **error)
1007 {
1008   return g_socket_client_connect_finish (client, result, error);
1009 }