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/gnetworkservice.h>
40 #include <gio/gsocketaddress.h>
46 * SECTION:gsocketclient
47 * @short_description: Helper for connecting to a network service
49 * @see_also: #GSocketConnection, #GSocketListener
51 * #GSocketClient is a high-level utility class for connecting to a
52 * network host using a connection oriented socket type.
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.
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.
66 G_DEFINE_TYPE (GSocketClient, g_socket_client, G_TYPE_OBJECT);
77 struct _GSocketClientPrivate
81 GSocketProtocol protocol;
82 GSocketAddress *local_address;
86 create_socket (GSocketClient *client,
87 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 socket = g_socket_new (family,
102 client->priv->protocol,
107 if (client->priv->local_address)
109 if (!g_socket_bind (socket,
110 client->priv->local_address,
114 g_object_unref (socket);
123 g_socket_client_init (GSocketClient *client)
125 client->priv = G_TYPE_INSTANCE_GET_PRIVATE (client,
126 G_TYPE_SOCKET_CLIENT,
127 GSocketClientPrivate);
128 client->priv->type = G_SOCKET_TYPE_STREAM;
132 * g_socket_client_new:
134 * Creates a new #GSocketClient with the default options.
136 * Returns: a #GSocketClient.
137 * Free the returned object with g_object_unref().
142 g_socket_client_new (void)
144 return g_object_new (G_TYPE_SOCKET_CLIENT, NULL);
148 g_socket_client_finalize (GObject *object)
150 GSocketClient *client = G_SOCKET_CLIENT (object);
152 if (client->priv->local_address)
153 g_object_unref (client->priv->local_address);
155 if (G_OBJECT_CLASS (g_socket_client_parent_class)->finalize)
156 (*G_OBJECT_CLASS (g_socket_client_parent_class)->finalize) (object);
160 g_socket_client_get_property (GObject *object,
165 GSocketClient *client = G_SOCKET_CLIENT (object);
170 g_value_set_enum (value, client->priv->family);
174 g_value_set_enum (value, client->priv->type);
178 g_value_set_enum (value, client->priv->protocol);
181 case PROP_LOCAL_ADDRESS:
182 g_value_set_object (value, client->priv->local_address);
186 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
191 g_socket_client_set_property (GObject *object,
196 GSocketClient *client = G_SOCKET_CLIENT (object);
201 g_socket_client_set_family (client, g_value_get_enum (value));
205 g_socket_client_set_socket_type (client, g_value_get_enum (value));
209 g_socket_client_set_protocol (client, g_value_get_enum (value));
212 case PROP_LOCAL_ADDRESS:
213 g_socket_client_set_local_address (client, g_value_get_object (value));
217 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
222 * g_socket_client_get_family:
223 * @client: a #GSocketClient.
225 * Gets the socket family of the socket client.
227 * See g_socket_client_set_family() for details.
229 * Returns: a #GSocketFamily
234 g_socket_client_get_family (GSocketClient *client)
236 return client->priv->family;
240 * g_socket_client_set_family:
241 * @client: a #GSocketClient.
242 * @family: a #GSocketFamily
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
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.
256 g_socket_client_set_family (GSocketClient *client,
257 GSocketFamily family)
259 if (client->priv->family == family)
262 client->priv->family = family;
263 g_object_notify (G_OBJECT (client), "family");
267 * g_socket_client_get_socket_type:
268 * @client: a #GSocketClient.
270 * Gets the socket type of the socket client.
272 * See g_socket_client_set_socket_type() for details.
274 * Returns: a #GSocketFamily
279 g_socket_client_get_socket_type (GSocketClient *client)
281 return client->priv->type;
285 * g_socket_client_set_socket_type:
286 * @client: a #GSocketClient.
287 * @type: a #GSocketType
289 * Sets the socket type of the socket client.
290 * The sockets created by this object will be of the specified
293 * It doesn't make sense to specify a type of %G_SOCKET_TYPE_DATAGRAM,
294 * as GSocketClient is used for connection oriented services.
299 g_socket_client_set_socket_type (GSocketClient *client,
302 if (client->priv->type == type)
305 client->priv->type = type;
306 g_object_notify (G_OBJECT (client), "type");
310 * g_socket_client_get_protocol:
311 * @client: a #GSocketClient
313 * Gets the protocol name type of the socket client.
315 * See g_socket_client_set_protocol() for details.
317 * Returns: a #GSocketProtocol
322 g_socket_client_get_protocol (GSocketClient *client)
324 return client->priv->protocol;
328 * g_socket_client_set_protocol:
329 * @client: a #GSocketClient.
330 * @protocol: a #GSocketProtocol
332 * Sets the protocol of the socket client.
333 * The sockets created by this object will use of the specified
336 * If @protocol is %0 that means to use the default
337 * protocol for the socket family and type.
342 g_socket_client_set_protocol (GSocketClient *client,
343 GSocketProtocol protocol)
345 if (client->priv->protocol == protocol)
348 client->priv->protocol = protocol;
349 g_object_notify (G_OBJECT (client), "protocol");
353 * g_socket_client_get_local_address:
354 * @client: a #GSocketClient.
356 * Gets the local address of the socket client.
358 * See g_socket_client_set_local_address() for details.
360 * Returns: a #GSocketAddres or %NULL. don't free
365 g_socket_client_get_local_address (GSocketClient *client)
367 return client->priv->local_address;
371 * g_socket_client_set_local_address:
372 * @client: a #GSocketClient.
373 * @address: a #GSocketAddress, or %NULL
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.
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.
386 g_socket_client_set_local_address (GSocketClient *client,
387 GSocketAddress *address)
390 g_object_ref (address);
392 if (client->priv->local_address)
394 g_object_unref (client->priv->local_address);
396 client->priv->local_address = address;
397 g_object_notify (G_OBJECT (client), "local-address");
401 g_socket_client_class_init (GSocketClientClass *class)
403 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
405 g_type_class_add_private (class, sizeof (GSocketClientPrivate));
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;
411 g_object_class_install_property (gobject_class, PROP_FAMILY,
412 g_param_spec_enum ("family",
414 P_("The sockets address family to use for socket construction"),
415 G_TYPE_SOCKET_FAMILY,
416 G_SOCKET_FAMILY_INVALID,
419 G_PARAM_STATIC_STRINGS));
421 g_object_class_install_property (gobject_class, PROP_TYPE,
422 g_param_spec_enum ("type",
424 P_("The sockets type to use for socket construction"),
426 G_SOCKET_TYPE_STREAM,
429 G_PARAM_STATIC_STRINGS));
431 g_object_class_install_property (gobject_class, PROP_PROTOCOL,
432 g_param_spec_enum ("protocol",
433 P_("Socket protocol"),
434 P_("The protocol to use for socket construction, or 0 for default"),
435 G_TYPE_SOCKET_PROTOCOL,
436 G_SOCKET_PROTOCOL_DEFAULT,
439 G_PARAM_STATIC_STRINGS));
441 g_object_class_install_property (gobject_class, PROP_LOCAL_ADDRESS,
442 g_param_spec_object ("local-address",
444 P_("The local address constructed sockets will be bound to"),
445 G_TYPE_SOCKET_ADDRESS,
448 G_PARAM_STATIC_STRINGS));
452 * g_socket_client_connect:
453 * @client: a #GSocketClient.
454 * @connectable: a #GSocketConnectable specifying the remote address.
455 * @cancellable: optional #GCancellable object, %NULL to ignore.
456 * @error: #GError for error reporting, or %NULL to ignore.
458 * Tries to resolve the @connectable and make a network connection to it..
460 * Upon a successful connection, a new #GSocketConnection is constructed
461 * and returned. The caller owns this new object and must drop their
462 * reference to it when finished with it.
464 * The type of the #GSocketConnection object returned depends on the type of
465 * the underlying socket that is used. For instance, for a TCP/IP connection
466 * it will be a #GTcpConnection.
468 * The socket created will be the same family as the the address that the
469 * @connectable resolves to, unless family is set with g_socket_client_set_family()
470 * or indirectly via g_socket_client_set_local_address(). The socket type
471 * defaults to %G_SOCKET_TYPE_STREAM but can be set with
472 * g_socket_client_set_socket_type().
474 * If a local address is specified with g_socket_client_set_local_address() the
475 * socket will be bound to this address before connecting.
477 * Returns: a #GSocketConnection on success, %NULL on error.
482 g_socket_client_connect (GSocketClient *client,
483 GSocketConnectable *connectable,
484 GCancellable *cancellable,
487 GSocketConnection *connection = NULL;
488 GSocketAddressEnumerator *enumerator;
489 GError *last_error, *tmp_error;
492 enumerator = g_socket_connectable_enumerate (connectable);
493 while (connection == NULL)
495 GSocketAddress *address;
498 if (g_cancellable_is_cancelled (cancellable))
500 g_clear_error (error);
501 g_cancellable_set_error_if_cancelled (cancellable, error);
506 address = g_socket_address_enumerator_next (enumerator, cancellable,
512 g_clear_error (&last_error);
513 g_propagate_error (error, tmp_error);
517 g_propagate_error (error, last_error);
520 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
521 _("Unknown error on connect"));
525 /* clear error from previous attempt */
526 g_clear_error (&last_error);
528 socket = create_socket (client, address, &last_error);
531 if (g_socket_connect (socket, address, cancellable, &last_error))
532 connection = g_socket_connection_factory_create_connection (socket);
534 g_object_unref (socket);
537 g_object_unref (address);
539 g_object_unref (enumerator);
545 * g_socket_client_connect_to_host:
546 * @client: a #SocketClient
547 * @host_and_port: the name and optionally port of the host to connect to
548 * @default_port: the default port to connect to
549 * @cancellable: a #GCancellable, or %NULL
550 * @error: a pointer to a #GError, or %NULL
552 * This is a helper function for g_socket_client_connect().
554 * Attempts to create a TCP connection to the named host.
556 * @host_and_port may be in any of a number of recognised formats: an IPv6
557 * address, an IPv4 address, or a domain name (in which case a DNS
558 * lookup is performed). Quoting with [] is supported for all address
559 * types. A port override may be specified in the usual way with a
560 * colon. Ports may be given as decimal numbers or symbolic names (in
561 * which case an /etc/services lookup is performed).
563 * If no port override is given in @host_and_port then @default_port will be
564 * used as the port number to connect to.
566 * In general, @host_and_port is expected to be provided by the user (allowing
567 * them to give the hostname, and a port overide if necessary) and
568 * @default_port is expected to be provided by the application.
570 * In the case that an IP address is given, a single connection
571 * attempt is made. In the case that a name is given, multiple
572 * connection attempts may be made, in turn and according to the
573 * number of address records in DNS, until a connection succeeds.
575 * Upon a successful connection, a new #GSocketConnection is constructed
576 * and returned. The caller owns this new object and must drop their
577 * reference to it when finished with it.
579 * In the event of any failure (DNS error, service not found, no hosts
580 * connectable) %NULL is returned and @error (if non-%NULL) is set
583 Returns: a #GSocketConnection on success, %NULL on error.
588 g_socket_client_connect_to_host (GSocketClient *client,
589 const gchar *host_and_port,
590 guint16 default_port,
591 GCancellable *cancellable,
594 GSocketConnectable *connectable;
595 GSocketConnection *connection;
597 connectable = g_network_address_parse (host_and_port, default_port, error);
598 if (connectable == NULL)
601 connection = g_socket_client_connect (client, connectable,
603 g_object_unref (connectable);
609 * g_socket_client_connect_to_service:
610 * @client: a #GSocketConnection
611 * @domain: a domain name
612 * @service: the name of the service to connect to
613 * @cancellable: a #GCancellable, or %NULL
614 * @error: a pointer to a #GError, or %NULL
615 * @returns: a #GSocketConnection if successful, or %NULL on error
617 * Attempts to create a TCP connection to a service.
619 * This call looks up the SRV record for @service at @domain for the
620 * "tcp" protocol. It then attempts to connect, in turn, to each of
621 * the hosts providing the service until either a connection succeeds
622 * or there are no hosts remaining.
624 * Upon a successful connection, a new #GSocketConnection is constructed
625 * and returned. The caller owns this new object and must drop their
626 * reference to it when finished with it.
628 * In the event of any failure (DNS error, service not found, no hosts
629 * connectable) %NULL is returned and @error (if non-%NULL) is set
633 g_socket_client_connect_to_service (GSocketClient *client,
635 const gchar *service,
636 GCancellable *cancellable,
639 GSocketConnectable *connectable;
640 GSocketConnection *connection;
642 connectable = g_network_service_new (service, "tcp", domain);
643 connection = g_socket_client_connect (client, connectable,
645 g_object_unref (connectable);
652 GSimpleAsyncResult *result;
653 GCancellable *cancellable;
654 GSocketClient *client;
656 GSocketAddressEnumerator *enumerator;
657 GSocket *current_socket;
660 } GSocketClientAsyncConnectData;
663 g_socket_client_async_connect_complete (GSocketClientAsyncConnectData *data)
665 GSocketConnection *connection;
667 if (data->last_error)
669 g_simple_async_result_set_from_error (data->result, data->last_error);
670 g_error_free (data->last_error);
674 g_assert (data->current_socket);
676 g_socket_set_blocking (data->current_socket, TRUE);
678 connection = g_socket_connection_factory_create_connection (data->current_socket);
679 g_object_unref (data->current_socket);
680 g_simple_async_result_set_op_res_gpointer (data->result,
685 g_simple_async_result_complete (data->result);
686 g_object_unref (data->result);
687 g_object_unref (data->enumerator);
688 g_slice_free (GSocketClientAsyncConnectData, data);
693 g_socket_client_enumerator_callback (GObject *object,
694 GAsyncResult *result,
698 set_last_error (GSocketClientAsyncConnectData *data,
701 g_clear_error (&data->last_error);
702 data->last_error = error;
706 g_socket_client_socket_callback (GSocket *socket,
707 GIOCondition condition,
708 GSocketClientAsyncConnectData *data)
710 GError *error = NULL;
712 if (g_cancellable_is_cancelled (data->cancellable))
714 /* Cancelled, return done with last error being cancelled */
715 g_clear_error (&data->last_error);
716 g_object_unref (data->current_socket);
717 data->current_socket = NULL;
718 g_cancellable_set_error_if_cancelled (data->cancellable,
723 /* socket is ready for writing means connect done, did it succeed? */
724 if (!g_socket_check_connect_result (data->current_socket, &error))
726 set_last_error (data, error);
729 g_socket_address_enumerator_next_async (data->enumerator,
731 g_socket_client_enumerator_callback,
738 g_socket_client_async_connect_complete (data);
744 g_socket_client_enumerator_callback (GObject *object,
745 GAsyncResult *result,
748 GSocketClientAsyncConnectData *data = user_data;
749 GSocketAddress *address;
751 GError *tmp_error = NULL;
753 if (g_cancellable_is_cancelled (data->cancellable))
755 g_clear_error (&data->last_error);
756 g_cancellable_set_error_if_cancelled (data->cancellable, &data->last_error);
757 g_socket_client_async_connect_complete (data);
761 address = g_socket_address_enumerator_next_finish (data->enumerator,
767 set_last_error (data, tmp_error);
768 else if (data->last_error == NULL)
769 g_set_error_literal (&data->last_error, G_IO_ERROR, G_IO_ERROR_FAILED,
770 _("Unknown error on connect"));
772 g_socket_client_async_connect_complete (data);
776 g_clear_error (&data->last_error);
778 socket = create_socket (data->client, address, &data->last_error);
781 g_socket_set_blocking (socket, FALSE);
782 if (g_socket_connect (socket, address, data->cancellable, &tmp_error))
784 data->current_socket = socket;
785 g_socket_client_async_connect_complete (data);
787 g_object_unref (address);
790 else if (g_error_matches (tmp_error, G_IO_ERROR, G_IO_ERROR_PENDING))
794 data->current_socket = socket;
795 g_error_free (tmp_error);
797 source = g_socket_create_source (socket, G_IO_OUT,
799 g_source_set_callback (source,
800 (GSourceFunc) g_socket_client_socket_callback,
802 g_source_attach (source, g_main_context_get_thread_default ());
803 g_source_unref (source);
805 g_object_unref (address);
810 data->last_error = tmp_error;
811 g_object_unref (socket);
813 g_object_unref (address);
816 g_socket_address_enumerator_next_async (data->enumerator,
818 g_socket_client_enumerator_callback,
823 * g_socket_client_connect_async:
824 * @client: a #GTcpClient
825 * @connectable: a #GSocketConnectable specifying the remote address.
826 * @cancellable: a #GCancellable, or %NULL
827 * @callback: a #GAsyncReadyCallback
828 * @user_data: user data for the callback
830 * This is the asynchronous version of g_socket_client_connect().
832 * When the operation is finished @callback will be
833 * called. You can then call g_socket_client_connect_finish() to get
834 * the result of the operation.
839 g_socket_client_connect_async (GSocketClient *client,
840 GSocketConnectable *connectable,
841 GCancellable *cancellable,
842 GAsyncReadyCallback callback,
845 GSocketClientAsyncConnectData *data;
847 g_return_if_fail (G_IS_SOCKET_CLIENT (client));
849 data = g_slice_new (GSocketClientAsyncConnectData);
851 data->result = g_simple_async_result_new (G_OBJECT (client),
853 g_socket_client_connect_async);
854 data->client = client;
856 data->cancellable = g_object_ref (cancellable);
858 data->cancellable = NULL;
859 data->last_error = NULL;
860 data->enumerator = g_socket_connectable_enumerate (connectable);
862 g_socket_address_enumerator_next_async (data->enumerator, cancellable,
863 g_socket_client_enumerator_callback,
868 * g_socket_client_connect_to_host_async:
869 * @client: a #GTcpClient
870 * @host_and_port: the name and optionally the port of the host to connect to
871 * @default_port: the default port to connect to
872 * @cancellable: a #GCancellable, or %NULL
873 * @callback: a #GAsyncReadyCallback
874 * @user_data: user data for the callback
876 * This is the asynchronous version of g_socket_client_connect_to_host().
878 * When the operation is finished @callback will be
879 * called. You can then call g_socket_client_connect_to_host_finish() to get
880 * the result of the operation.
885 g_socket_client_connect_to_host_async (GSocketClient *client,
886 const gchar *host_and_port,
887 guint16 default_port,
888 GCancellable *cancellable,
889 GAsyncReadyCallback callback,
892 GSocketConnectable *connectable;
896 connectable = g_network_address_parse (host_and_port, default_port,
898 if (connectable == NULL)
900 g_simple_async_report_gerror_in_idle (G_OBJECT (client),
901 callback, user_data, error);
902 g_error_free (error);
906 g_socket_client_connect_async (client,
907 connectable, cancellable,
908 callback, user_data);
909 g_object_unref (connectable);
914 * g_socket_client_connect_to_service_async:
915 * @client: a #GSocketClient
916 * @domain: a domain name
917 * @service: the name of the service to connect to
918 * @cancellable: a #GCancellable, or %NULL
919 * @callback: a #GAsyncReadyCallback
920 * @user_data: user data for the callback
922 * This is the asynchronous version of
923 * g_socket_client_connect_to_service().
928 g_socket_client_connect_to_service_async (GSocketClient *client,
930 const gchar *service,
931 GCancellable *cancellable,
932 GAsyncReadyCallback callback,
935 GSocketConnectable *connectable;
937 connectable = g_network_service_new (service, "tcp", domain);
938 g_socket_client_connect_async (client,
939 connectable, cancellable,
940 callback, user_data);
941 g_object_unref (connectable);
945 * g_socket_client_connect_finish:
946 * @client: a #GSocketClient.
947 * @result: a #GAsyncResult.
948 * @error: a #GError location to store the error occuring, or %NULL to
951 * Finishes an async connect operation. See g_socket_client_connect_async()
953 * Returns: a #GSocketConnection on success, %NULL on error.
958 g_socket_client_connect_finish (GSocketClient *client,
959 GAsyncResult *result,
962 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
964 if (g_simple_async_result_propagate_error (simple, error))
967 return g_object_ref (g_simple_async_result_get_op_res_gpointer (simple));
971 * g_socket_client_connect_to_host_finish:
972 * @client: a #GSocketClient.
973 * @result: a #GAsyncResult.
974 * @error: a #GError location to store the error occuring, or %NULL to
977 * Finishes an async connect operation. See g_socket_client_connect_to_host_async()
979 * Returns: a #GSocketConnection on success, %NULL on error.
984 g_socket_client_connect_to_host_finish (GSocketClient *client,
985 GAsyncResult *result,
988 return g_socket_client_connect_finish (client, result, error);
992 * g_socket_client_connect_to_service_finish:
993 * @client: a #GSocketClient.
994 * @result: a #GAsyncResult.
995 * @error: a #GError location to store the error occuring, or %NULL to
998 * Finishes an async connect operation. See g_socket_client_connect_to_service_async()
1000 * Returns: a #GSocketConnection on success, %NULL on error.
1005 g_socket_client_connect_to_service_finish (GSocketClient *client,
1006 GAsyncResult *result,
1009 return g_socket_client_connect_finish (client, result, error);
1012 #define __G_SOCKET_CLIENT_C__
1013 #include "gioaliasdef.c"