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