Import all the highlevel socket classes from gnio
[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: High-level client network helper
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  * @socket: a #GSocket.
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  * @socket: a #GSocket.
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  * @socket: a #GSocket.
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  * @socket: a #GSocket.
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  * @socket: a #GSocket.
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  * @socket: a #GSocket.
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  * @socket: a #GSocket.
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  * @socket: a #GSocket.
377  * @addres: 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 #GTcpClient
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  * @returns: a #GSocketConnection if successful, or %NULL on error
547  *
548  * This is a helper function for g_socket_client_connect().
549  *
550  * Attempts to create a TCP connection to the named host.
551  *
552  * @host_and_port may be in any of a number of recognised formats: an IPv6
553  * address, an IPv4 address, or a domain name (in which case a DNS
554  * lookup is performed).  Quoting with [] is supported for all address
555  * types.  A port override may be specified in the usual way with a
556  * colon.  Ports may be given as decimal numbers or symbolic names (in
557  * which case an /etc/services lookup is performed).
558  *
559  * If no port override is given in @host_and_port then @default_port will be
560  * used as the port number to connect to.
561  *
562  * In general, @host_and_port is expected to be provided by the user (allowing
563  * them to give the hostname, and a port overide if necessary) and
564  * @default_port is expected to be provided by the application.
565
566  * In the case that an IP address is given, a single connection
567  * attempt is made.  In the case that a name is given, multiple
568  * connection attempts may be made, in turn and according to the
569  * number of address records in DNS, until a connection succeeds.
570  *
571  * Upon a successful connection, a new #GSocketConnection is constructed
572  * and returned.  The caller owns this new object and must drop their
573  * reference to it when finished with it.
574  *
575  * In the event of any failure (DNS error, service not found, no hosts
576  * connectable) %NULL is returned and @error (if non-%NULL) is set
577  * accordingly.
578  *
579  Returns: a #GSocketConnection on success, %NULL on error.
580  *
581  * Since: 2.22
582  **/
583 GSocketConnection *
584 g_socket_client_connect_to_host (GSocketClient        *client,
585                                  const char           *host_and_port,
586                                  int                   default_port,
587                                  GCancellable         *cancellable,
588                                  GError              **error)
589 {
590   GSocketConnectable *connectable;
591   GSocketConnection *connection;
592
593   connectable = g_network_address_parse (host_and_port, default_port, error);
594   if (connectable == NULL)
595     return NULL;
596
597   connection = g_socket_client_connect (client, connectable,
598                                         cancellable, error);
599   g_object_unref (connectable);
600
601   return connection;
602 }
603
604 typedef struct
605 {
606   GSimpleAsyncResult *result;
607   GCancellable *cancellable;
608   GSocketClient *client;
609
610   GSocketAddressEnumerator *enumerator;
611   GSocket *current_socket;
612
613   GError *last_error;
614 } GSocketClientAsyncConnectData;
615
616 static void
617 g_socket_client_async_connect_complete (GSocketClientAsyncConnectData *data)
618 {
619   GSocketConnection *connection;
620
621   if (data->last_error)
622     {
623       g_simple_async_result_set_from_error (data->result, data->last_error);
624       g_error_free (data->last_error);
625     }
626   else
627     {
628       g_assert (data->current_socket);
629
630       g_socket_set_blocking (data->current_socket, TRUE);
631
632       connection = g_socket_connection_factory_create_connection (data->current_socket);
633       g_simple_async_result_set_op_res_gpointer (data->result,
634                                                  connection,
635                                                  g_object_unref);
636     }
637
638   g_simple_async_result_complete (data->result);
639   g_object_unref (data->result);
640 }
641
642
643 static void
644 g_socket_client_enumerator_callback (GObject      *object,
645                                      GAsyncResult *result,
646                                      gpointer      user_data);
647
648 static void
649 set_last_error (GSocketClientAsyncConnectData *data,
650                 GError *error)
651 {
652   g_clear_error (&data->last_error);
653   data->last_error = error;
654 }
655
656 static gboolean
657 g_socket_client_socket_callback (GSocket *socket,
658                                  GIOCondition condition,
659                                  GSocketClientAsyncConnectData *data)
660 {
661   GError *error = NULL;
662
663   if (g_cancellable_is_cancelled (data->cancellable))
664     {
665       /* Cancelled, return done with last error being cancelled */
666       g_clear_error (&data->last_error);
667       g_object_unref (data->current_socket);
668       data->current_socket = NULL;
669       g_cancellable_set_error_if_cancelled (data->cancellable,
670                                             &data->last_error);
671     }
672   else
673     {
674       /* socket is ready for writing means connect done, did it succeed? */
675       if (!g_socket_check_pending_error (data->current_socket, &error))
676         {
677           set_last_error (data, error);
678
679           /* try next one */
680           g_socket_address_enumerator_next_async (data->enumerator,
681                                                   data->cancellable,
682                                                   g_socket_client_enumerator_callback,
683                                                   data);
684
685           return FALSE;
686         }
687     }
688
689   g_socket_client_async_connect_complete (data);
690
691   return FALSE;
692 }
693
694 static void
695 g_socket_client_enumerator_callback (GObject      *object,
696                                      GAsyncResult *result,
697                                      gpointer      user_data)
698 {
699   GSocketClientAsyncConnectData *data = user_data;
700   GSocketAddress *address;
701   GSocket *socket;
702   GError *tmp_error = NULL;
703
704   if (g_cancellable_is_cancelled (data->cancellable))
705     {
706       g_clear_error (&data->last_error);
707       g_cancellable_set_error_if_cancelled (data->cancellable, &data->last_error);
708       g_socket_client_async_connect_complete (data);
709       return;
710     }
711
712   address = g_socket_address_enumerator_next_finish (data->enumerator,
713                                                      result, &tmp_error);
714
715   if (address == NULL)
716     {
717       if (tmp_error)
718         set_last_error (data, tmp_error);
719       else if (data->last_error == NULL)
720         g_set_error (&data->last_error, G_IO_ERROR, G_IO_ERROR_FAILED,
721                      _("Unknown error on connect"));
722
723       g_socket_client_async_connect_complete (data);
724       return;
725     }
726
727   g_clear_error (&data->last_error);
728
729   socket = create_socket (data->client, address, &data->last_error);
730   if (socket != NULL)
731     {
732       g_socket_set_blocking (socket, FALSE);
733       if (g_socket_connect (socket, address, &tmp_error))
734         {
735           data->current_socket = socket;
736           g_socket_client_async_connect_complete (data);
737
738           g_object_unref (address);
739           return;
740         }
741       else if (g_error_matches (tmp_error, G_IO_ERROR, G_IO_ERROR_PENDING))
742         {
743           GSource *source;
744
745           data->current_socket = socket;
746           g_error_free (tmp_error);
747
748           source = g_socket_create_source (socket, G_IO_OUT,
749                                            data->cancellable);
750           g_source_set_callback (source,
751                                  (GSourceFunc) g_socket_client_socket_callback,
752                                  data, NULL);
753           g_source_attach (source, NULL);
754           g_source_unref (source);
755
756           g_object_unref (address);
757           return;
758         }
759       else
760         {
761           data->last_error = tmp_error;
762           g_object_unref (socket);
763         }
764       g_object_unref (address);
765     }
766
767   g_socket_address_enumerator_next_async (data->enumerator,
768                                           data->cancellable,
769                                           g_socket_client_enumerator_callback,
770                                           data);
771 }
772
773 /**
774  * g_socket_client_connect_to_host_async:
775  * @client: a #GTcpClient
776  * @connectable: a #GSocketConnectable specifying the remote address.
777  * @cancellable: a #GCancellable, or %NULL
778  * @callback: a #GAsyncReadyCallback
779  * @user_data: user data for the callback
780  *
781  * This is the asynchronous version of g_socket_client_connect().
782  *
783  * When the operation is finished @callback will be
784  * called. You can then call g_socket_client_connect_finish() to get
785  * the result of the operation.
786  *
787  * Since: 2.22
788  **/
789 void
790 g_socket_client_connect_async (GSocketClient       *client,
791                                GSocketConnectable  *connectable,
792                                GCancellable        *cancellable,
793                                GAsyncReadyCallback  callback,
794                                gpointer             user_data)
795 {
796   GSocketClientAsyncConnectData *data;
797
798   g_return_if_fail (G_IS_SOCKET_CLIENT (client));
799
800   data = g_slice_new (GSocketClientAsyncConnectData);
801
802   data->result = g_simple_async_result_new (G_OBJECT (client),
803                                             callback, user_data,
804                                             g_socket_client_connect_async);
805   data->client = client;
806   if (cancellable)
807     data->cancellable = g_object_ref (cancellable);
808   else
809     data->cancellable = NULL;
810   data->last_error = NULL;
811   data->enumerator = g_socket_connectable_enumerate (connectable);
812
813   g_socket_address_enumerator_next_async (data->enumerator, cancellable,
814                                           g_socket_client_enumerator_callback,
815                                           data);
816 }
817
818 /**
819  * g_socket_client_connect_to_host_async:
820  * @client: a #GTcpClient
821  * @host_and_port: the name and optionally the port of the host to connect to
822  * @default_port: the default port to connect to
823  * @cancellable: a #GCancellable, or %NULL
824  * @callback: a #GAsyncReadyCallback
825  * @user_data: user data for the callback
826  *
827  * This is the asynchronous version of g_socket_client_connect_to_host().
828  *
829  * When the operation is finished @callback will be
830  * called. You can then call g_socket_client_connect_to_host_finish() to get
831  * the result of the operation.
832  *
833  * Since: 2.22
834  **/
835 void
836 g_socket_client_connect_to_host_async (GSocketClient        *client,
837                                        const char           *host_and_port,
838                                        int                   default_port,
839                                        GCancellable         *cancellable,
840                                        GAsyncReadyCallback   callback,
841                                        gpointer              user_data)
842 {
843   GSocketConnectable *connectable;
844   GError *error;
845
846   error = NULL;
847   connectable = g_network_address_parse (host_and_port, default_port,
848                                          &error);
849   if (connectable == NULL)
850     {
851       g_simple_async_report_gerror_in_idle (G_OBJECT (client),
852                                             callback, user_data, error);
853       g_error_free (error);
854     }
855   else
856     {
857       g_socket_client_connect_async (client,
858                                      connectable, cancellable,
859                                      callback, user_data);
860       g_object_unref (connectable);
861     }
862 }
863
864 /**
865  * g_socket_client_connect_finish:
866  * @client: a #GSocketClient.
867  * @result: a #GAsyncResult.
868  * @error: a #GError location to store the error occuring, or %NULL to
869  * ignore.
870  *
871  * Finishes an async connect operation. See g_socket_client_connect_async()
872  *
873  * Returns: a #GSocketConnection on success, %NULL on error.
874  *
875  * Since: 2.22
876  **/
877 GSocketConnection *
878 g_socket_client_connect_finish (GSocketClient  *client,
879                                 GAsyncResult   *result,
880                                 GError        **error)
881 {
882   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
883
884   if (g_simple_async_result_propagate_error (simple, error))
885     return NULL;
886
887   return g_object_ref (g_simple_async_result_get_op_res_gpointer (simple));
888 }
889
890 /**
891  * g_socket_client_connect_to_host_finish:
892  * @client: a #GSocketClient.
893  * @result: a #GAsyncResult.
894  * @error: a #GError location to store the error occuring, or %NULL to
895  * ignore.
896  *
897  * Finishes an async connect operation. See g_socket_client_connect_to_host_async()
898  *
899  * Returns: a #GSocketConnection on success, %NULL on error.
900  *
901  * Since: 2.22
902  **/
903 GSocketConnection *
904 g_socket_client_connect_to_host_finish (GSocketClient        *client,
905                                         GAsyncResult         *result,
906                                         GError              **error)
907 {
908   return g_socket_client_connect_finish (client, result, error);
909 }
910
911 #define __G_SOCKET_CLIENT_C__
912 #include "gioaliasdef.c"