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