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: High-level client network helper
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 * @socket: a #GSocket.
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 * @socket: a #GSocket.
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 * @socket: a #GSocket.
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 * @socket: a #GSocket.
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 * @socket: a #GSocket.
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 * @socket: a #GSocket.
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 * @socket: a #GSocket.
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 * @socket: a #GSocket.
377 * @addres: 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 #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
548 * This is a helper function for g_socket_client_connect().
550 * Attempts to create a TCP connection to the named host.
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).
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.
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.
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.
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.
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
579 Returns: a #GSocketConnection on success, %NULL on error.
584 g_socket_client_connect_to_host (GSocketClient *client,
585 const char *host_and_port,
587 GCancellable *cancellable,
590 GSocketConnectable *connectable;
591 GSocketConnection *connection;
593 connectable = g_network_address_parse (host_and_port, default_port, error);
594 if (connectable == NULL)
597 connection = g_socket_client_connect (client, connectable,
599 g_object_unref (connectable);
606 GSimpleAsyncResult *result;
607 GCancellable *cancellable;
608 GSocketClient *client;
610 GSocketAddressEnumerator *enumerator;
611 GSocket *current_socket;
614 } GSocketClientAsyncConnectData;
617 g_socket_client_async_connect_complete (GSocketClientAsyncConnectData *data)
619 GSocketConnection *connection;
621 if (data->last_error)
623 g_simple_async_result_set_from_error (data->result, data->last_error);
624 g_error_free (data->last_error);
628 g_assert (data->current_socket);
630 g_socket_set_blocking (data->current_socket, TRUE);
632 connection = g_socket_connection_factory_create_connection (data->current_socket);
633 g_simple_async_result_set_op_res_gpointer (data->result,
638 g_simple_async_result_complete (data->result);
639 g_object_unref (data->result);
644 g_socket_client_enumerator_callback (GObject *object,
645 GAsyncResult *result,
649 set_last_error (GSocketClientAsyncConnectData *data,
652 g_clear_error (&data->last_error);
653 data->last_error = error;
657 g_socket_client_socket_callback (GSocket *socket,
658 GIOCondition condition,
659 GSocketClientAsyncConnectData *data)
661 GError *error = NULL;
663 if (g_cancellable_is_cancelled (data->cancellable))
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,
674 /* socket is ready for writing means connect done, did it succeed? */
675 if (!g_socket_check_pending_error (data->current_socket, &error))
677 set_last_error (data, error);
680 g_socket_address_enumerator_next_async (data->enumerator,
682 g_socket_client_enumerator_callback,
689 g_socket_client_async_connect_complete (data);
695 g_socket_client_enumerator_callback (GObject *object,
696 GAsyncResult *result,
699 GSocketClientAsyncConnectData *data = user_data;
700 GSocketAddress *address;
702 GError *tmp_error = NULL;
704 if (g_cancellable_is_cancelled (data->cancellable))
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);
712 address = g_socket_address_enumerator_next_finish (data->enumerator,
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"));
723 g_socket_client_async_connect_complete (data);
727 g_clear_error (&data->last_error);
729 socket = create_socket (data->client, address, &data->last_error);
732 g_socket_set_blocking (socket, FALSE);
733 if (g_socket_connect (socket, address, &tmp_error))
735 data->current_socket = socket;
736 g_socket_client_async_connect_complete (data);
738 g_object_unref (address);
741 else if (g_error_matches (tmp_error, G_IO_ERROR, G_IO_ERROR_PENDING))
745 data->current_socket = socket;
746 g_error_free (tmp_error);
748 source = g_socket_create_source (socket, G_IO_OUT,
750 g_source_set_callback (source,
751 (GSourceFunc) g_socket_client_socket_callback,
753 g_source_attach (source, NULL);
754 g_source_unref (source);
756 g_object_unref (address);
761 data->last_error = tmp_error;
762 g_object_unref (socket);
764 g_object_unref (address);
767 g_socket_address_enumerator_next_async (data->enumerator,
769 g_socket_client_enumerator_callback,
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
781 * This is the asynchronous version of g_socket_client_connect().
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.
790 g_socket_client_connect_async (GSocketClient *client,
791 GSocketConnectable *connectable,
792 GCancellable *cancellable,
793 GAsyncReadyCallback callback,
796 GSocketClientAsyncConnectData *data;
798 g_return_if_fail (G_IS_SOCKET_CLIENT (client));
800 data = g_slice_new (GSocketClientAsyncConnectData);
802 data->result = g_simple_async_result_new (G_OBJECT (client),
804 g_socket_client_connect_async);
805 data->client = client;
807 data->cancellable = g_object_ref (cancellable);
809 data->cancellable = NULL;
810 data->last_error = NULL;
811 data->enumerator = g_socket_connectable_enumerate (connectable);
813 g_socket_address_enumerator_next_async (data->enumerator, cancellable,
814 g_socket_client_enumerator_callback,
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
827 * This is the asynchronous version of g_socket_client_connect_to_host().
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.
836 g_socket_client_connect_to_host_async (GSocketClient *client,
837 const char *host_and_port,
839 GCancellable *cancellable,
840 GAsyncReadyCallback callback,
843 GSocketConnectable *connectable;
847 connectable = g_network_address_parse (host_and_port, default_port,
849 if (connectable == NULL)
851 g_simple_async_report_gerror_in_idle (G_OBJECT (client),
852 callback, user_data, error);
853 g_error_free (error);
857 g_socket_client_connect_async (client,
858 connectable, cancellable,
859 callback, user_data);
860 g_object_unref (connectable);
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
871 * Finishes an async connect operation. See g_socket_client_connect_async()
873 * Returns: a #GSocketConnection on success, %NULL on error.
878 g_socket_client_connect_finish (GSocketClient *client,
879 GAsyncResult *result,
882 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
884 if (g_simple_async_result_propagate_error (simple, error))
887 return g_object_ref (g_simple_async_result_get_op_res_gpointer (simple));
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
897 * Finishes an async connect operation. See g_socket_client_connect_to_host_async()
899 * Returns: a #GSocketConnection on success, %NULL on error.
904 g_socket_client_connect_to_host_finish (GSocketClient *client,
905 GAsyncResult *result,
908 return g_socket_client_connect_finish (client, result, error);
911 #define __G_SOCKET_CLIENT_C__
912 #include "gioaliasdef.c"