1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2008, 2009 codethink
4 * Copyright © 2009 Red Hat, Inc
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.
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.
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.
21 * Authors: Ryan Lortie <desrt@desrt.ca>
22 * Alexander Larsson <alexl@redhat.com>
26 #include "gsocketclient.h"
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>
45 * SECTION:gsocketclient
46 * @short_description: Helper for connecting to a network service
48 * @see_also: #GSocketConnection, #GSocketListener
50 * #GSocketClient is a high-level utility class for connecting to a
51 * network host using a connection oriented socket type.
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.
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.
65 G_DEFINE_TYPE (GSocketClient, g_socket_client, G_TYPE_OBJECT);
76 struct _GSocketClientPrivate
81 GSocketAddress *local_address;
85 create_socket (GSocketClient *client,
86 GSocketAddress *dest_address,
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);
100 proto = g_socket_protocol_id_lookup_by_name (client->priv->protocol);
101 socket = g_socket_new (family,
108 if (client->priv->local_address)
110 if (!g_socket_bind (socket,
111 client->priv->local_address,
115 g_object_unref (socket);
124 g_socket_client_init (GSocketClient *client)
126 client->priv = G_TYPE_INSTANCE_GET_PRIVATE (client,
127 G_TYPE_SOCKET_CLIENT,
128 GSocketClientPrivate);
129 client->priv->type = G_SOCKET_TYPE_STREAM;
133 * g_socket_client_new:
135 * Creates a new #GSocketClient with the default options.
137 * Returns: a #GSocketClient.
138 * Free the returned object with g_object_unref().
143 g_socket_client_new (void)
145 return g_object_new (G_TYPE_SOCKET_CLIENT, NULL);
149 g_socket_client_finalize (GObject *object)
151 GSocketClient *client = G_SOCKET_CLIENT (object);
153 if (client->priv->local_address)
154 g_object_unref (client->priv->local_address);
156 g_free (client->priv->protocol);
158 if (G_OBJECT_CLASS (g_socket_client_parent_class)->finalize)
159 (*G_OBJECT_CLASS (g_socket_client_parent_class)->finalize) (object);
163 g_socket_client_get_property (GObject *object,
168 GSocketClient *client = G_SOCKET_CLIENT (object);
173 g_value_set_enum (value, client->priv->family);
177 g_value_set_enum (value, client->priv->type);
181 g_value_set_string (value, client->priv->protocol);
184 case PROP_LOCAL_ADDRESS:
185 g_value_set_object (value, client->priv->local_address);
189 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
194 g_socket_client_set_property (GObject *object,
199 GSocketClient *client = G_SOCKET_CLIENT (object);
204 g_socket_client_set_family (client, g_value_get_enum (value));
208 g_socket_client_set_socket_type (client, g_value_get_enum (value));
212 g_socket_client_set_protocol (client, g_value_get_string (value));
215 case PROP_LOCAL_ADDRESS:
216 g_socket_client_set_local_address (client, g_value_get_object (value));
220 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
225 * g_socket_client_get_family:
226 * @client: a #GSocketClient.
228 * Gets the socket family of the socket client.
230 * See g_socket_client_set_family() for details.
232 * Returns: a #GSocketFamily
237 g_socket_client_get_family (GSocketClient *client)
239 return client->priv->family;
243 * g_socket_client_set_family:
244 * @client: a #GSocketClient.
245 * @family: a #GSocketFamily
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
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.
259 g_socket_client_set_family (GSocketClient *client,
260 GSocketFamily family)
262 if (client->priv->family == family)
265 client->priv->family = family;
266 g_object_notify (G_OBJECT (client), "family");
270 * g_socket_client_get_socket_type:
271 * @client: a #GSocketClient.
273 * Gets the socket type of the socket client.
275 * See g_socket_client_set_socket_type() for details.
277 * Returns: a #GSocketFamily
282 g_socket_client_get_socket_type (GSocketClient *client)
284 return client->priv->type;
288 * g_socket_client_set_socket_type:
289 * @client: a #GSocketClient.
290 * @type: a #GSocketType
292 * Sets the socket type of the socket client.
293 * The sockets created by this object will be of the specified
296 * It doesn't make sense to specify a type of %G_SOCKET_TYPE_DATAGRAM,
297 * as GSocketClient is used for connection oriented services.
302 g_socket_client_set_socket_type (GSocketClient *client,
305 if (client->priv->type == type)
308 client->priv->type = type;
309 g_object_notify (G_OBJECT (client), "type");
313 * g_socket_client_get_protocol:
314 * @client: a #GSocketClient.
316 * Gets the protocol name type of the socket client.
318 * See g_socket_client_set_protocol() for details.
320 * Returns: a string or %NULL. don't free
325 g_socket_client_get_protocol (GSocketClient *client)
327 return client->priv->protocol;
331 * g_socket_client_set_protocol:
332 * @client: a #GSocketClient.
333 * @protocol: a string, or %NULL
335 * Sets the protocol of the socket client.
336 * The sockets created by this object will use of the specified
339 * If @protocol is %NULL that means to use the default
340 * protocol for the socket family and type.
345 g_socket_client_set_protocol (GSocketClient *client,
346 const char *protocol)
348 if (g_strcmp0 (client->priv->protocol, protocol) == 0)
351 g_free (client->priv->protocol);
352 client->priv->protocol = g_strdup (protocol);
353 g_object_notify (G_OBJECT (client), "protocol");
357 * g_socket_client_get_local_address:
358 * @client: a #GSocketClient.
360 * Gets the local address of the socket client.
362 * See g_socket_client_set_local_address() for details.
364 * Returns: a #GSocketAddres or %NULL. don't free
369 g_socket_client_get_local_address (GSocketClient *client)
371 return client->priv->local_address;
375 * g_socket_client_set_local_address:
376 * @client: a #GSocketClient.
377 * @address: a #GSocketAddress, or %NULL
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.
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.
390 g_socket_client_set_local_address (GSocketClient *client,
391 GSocketAddress *address)
394 g_object_ref (address);
396 if (client->priv->local_address)
398 g_object_unref (client->priv->local_address);
400 client->priv->local_address = address;
401 g_object_notify (G_OBJECT (client), "local-address");
405 g_socket_client_class_init (GSocketClientClass *class)
407 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
409 g_type_class_add_private (class, sizeof (GSocketClientPrivate));
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;
415 g_object_class_install_property (gobject_class, PROP_FAMILY,
416 g_param_spec_enum ("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));
423 g_object_class_install_property (gobject_class, PROP_TYPE,
424 g_param_spec_enum ("type",
426 P_("The sockets type to use for socket construction"),
428 G_SOCKET_TYPE_STREAM,
429 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
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"),
436 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
438 g_object_class_install_property (gobject_class, PROP_LOCAL_ADDRESS,
439 g_param_spec_object ("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));
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.
453 * Tries to resolve the @connectable and make a network connection to it..
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.
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.
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().
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.
472 * Returns: a #GSocketConnection on success, %NULL on error.
477 g_socket_client_connect (GSocketClient *client,
478 GSocketConnectable *connectable,
479 GCancellable *cancellable,
482 GSocketConnection *connection = NULL;
483 GSocketAddressEnumerator *enumerator;
484 GError *last_error, *tmp_error;
487 enumerator = g_socket_connectable_enumerate (connectable);
488 while (connection == NULL)
490 GSocketAddress *address;
493 if (g_cancellable_is_cancelled (cancellable))
495 g_clear_error (error);
496 g_cancellable_set_error_if_cancelled (cancellable, error);
501 address = g_socket_address_enumerator_next (enumerator, cancellable,
507 g_clear_error (&last_error);
508 g_propagate_error (error, tmp_error);
512 g_propagate_error (error, tmp_error);
515 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
516 _("Unknown error on connect"));
520 /* clear error from previous attempt */
521 g_clear_error (&last_error);
523 socket = create_socket (client, address, &last_error);
526 if (g_socket_connect (socket, address, &last_error))
527 connection = g_socket_connection_factory_create_connection (socket);
529 g_object_unref (socket);
532 g_object_unref (address);
534 g_object_unref (enumerator);
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
547 * This is a helper function for g_socket_client_connect().
549 * Attempts to create a TCP connection to the named host.
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).
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.
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.
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.
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.
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
578 Returns: a #GSocketConnection on success, %NULL on error.
583 g_socket_client_connect_to_host (GSocketClient *client,
584 const char *host_and_port,
586 GCancellable *cancellable,
589 GSocketConnectable *connectable;
590 GSocketConnection *connection;
592 connectable = g_network_address_parse (host_and_port, default_port, error);
593 if (connectable == NULL)
596 connection = g_socket_client_connect (client, connectable,
598 g_object_unref (connectable);
605 GSimpleAsyncResult *result;
606 GCancellable *cancellable;
607 GSocketClient *client;
609 GSocketAddressEnumerator *enumerator;
610 GSocket *current_socket;
613 } GSocketClientAsyncConnectData;
616 g_socket_client_async_connect_complete (GSocketClientAsyncConnectData *data)
618 GSocketConnection *connection;
620 if (data->last_error)
622 g_simple_async_result_set_from_error (data->result, data->last_error);
623 g_error_free (data->last_error);
627 g_assert (data->current_socket);
629 g_socket_set_blocking (data->current_socket, TRUE);
631 connection = g_socket_connection_factory_create_connection (data->current_socket);
632 g_simple_async_result_set_op_res_gpointer (data->result,
637 g_simple_async_result_complete (data->result);
638 g_object_unref (data->result);
643 g_socket_client_enumerator_callback (GObject *object,
644 GAsyncResult *result,
648 set_last_error (GSocketClientAsyncConnectData *data,
651 g_clear_error (&data->last_error);
652 data->last_error = error;
656 g_socket_client_socket_callback (GSocket *socket,
657 GIOCondition condition,
658 GSocketClientAsyncConnectData *data)
660 GError *error = NULL;
662 if (g_cancellable_is_cancelled (data->cancellable))
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,
673 /* socket is ready for writing means connect done, did it succeed? */
674 if (!g_socket_check_connect_result (data->current_socket, &error))
676 set_last_error (data, error);
679 g_socket_address_enumerator_next_async (data->enumerator,
681 g_socket_client_enumerator_callback,
688 g_socket_client_async_connect_complete (data);
694 g_socket_client_enumerator_callback (GObject *object,
695 GAsyncResult *result,
698 GSocketClientAsyncConnectData *data = user_data;
699 GSocketAddress *address;
701 GError *tmp_error = NULL;
703 if (g_cancellable_is_cancelled (data->cancellable))
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);
711 address = g_socket_address_enumerator_next_finish (data->enumerator,
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"));
722 g_socket_client_async_connect_complete (data);
726 g_clear_error (&data->last_error);
728 socket = create_socket (data->client, address, &data->last_error);
731 g_socket_set_blocking (socket, FALSE);
732 if (g_socket_connect (socket, address, &tmp_error))
734 data->current_socket = socket;
735 g_socket_client_async_connect_complete (data);
737 g_object_unref (address);
740 else if (g_error_matches (tmp_error, G_IO_ERROR, G_IO_ERROR_PENDING))
744 data->current_socket = socket;
745 g_error_free (tmp_error);
747 source = g_socket_create_source (socket, G_IO_OUT,
749 g_source_set_callback (source,
750 (GSourceFunc) g_socket_client_socket_callback,
752 g_source_attach (source, NULL);
753 g_source_unref (source);
755 g_object_unref (address);
760 data->last_error = tmp_error;
761 g_object_unref (socket);
763 g_object_unref (address);
766 g_socket_address_enumerator_next_async (data->enumerator,
768 g_socket_client_enumerator_callback,
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
780 * This is the asynchronous version of g_socket_client_connect().
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.
789 g_socket_client_connect_async (GSocketClient *client,
790 GSocketConnectable *connectable,
791 GCancellable *cancellable,
792 GAsyncReadyCallback callback,
795 GSocketClientAsyncConnectData *data;
797 g_return_if_fail (G_IS_SOCKET_CLIENT (client));
799 data = g_slice_new (GSocketClientAsyncConnectData);
801 data->result = g_simple_async_result_new (G_OBJECT (client),
803 g_socket_client_connect_async);
804 data->client = client;
806 data->cancellable = g_object_ref (cancellable);
808 data->cancellable = NULL;
809 data->last_error = NULL;
810 data->enumerator = g_socket_connectable_enumerate (connectable);
812 g_socket_address_enumerator_next_async (data->enumerator, cancellable,
813 g_socket_client_enumerator_callback,
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
826 * This is the asynchronous version of g_socket_client_connect_to_host().
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.
835 g_socket_client_connect_to_host_async (GSocketClient *client,
836 const char *host_and_port,
838 GCancellable *cancellable,
839 GAsyncReadyCallback callback,
842 GSocketConnectable *connectable;
846 connectable = g_network_address_parse (host_and_port, default_port,
848 if (connectable == NULL)
850 g_simple_async_report_gerror_in_idle (G_OBJECT (client),
851 callback, user_data, error);
852 g_error_free (error);
856 g_socket_client_connect_async (client,
857 connectable, cancellable,
858 callback, user_data);
859 g_object_unref (connectable);
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
870 * Finishes an async connect operation. See g_socket_client_connect_async()
872 * Returns: a #GSocketConnection on success, %NULL on error.
877 g_socket_client_connect_finish (GSocketClient *client,
878 GAsyncResult *result,
881 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
883 if (g_simple_async_result_propagate_error (simple, error))
886 return g_object_ref (g_simple_async_result_get_op_res_gpointer (simple));
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
896 * Finishes an async connect operation. See g_socket_client_connect_to_host_async()
898 * Returns: a #GSocketConnection on success, %NULL on error.
903 g_socket_client_connect_to_host_finish (GSocketClient *client,
904 GAsyncResult *result,
907 return g_socket_client_connect_finish (client, result, error);
910 #define __G_SOCKET_CLIENT_C__
911 #include "gioaliasdef.c"