1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2008 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
27 #include "gnetworkservice.h"
29 #include "gcancellable.h"
30 #include "ginetaddress.h"
31 #include "ginetsocketaddress.h"
33 #include "gnetworkaddress.h"
34 #include "gnetworkingprivate.h"
35 #include "gresolver.h"
36 #include "gsimpleasyncresult.h"
37 #include "gsocketaddressenumerator.h"
38 #include "gsocketconnectable.h"
39 #include "gsrvtarget.h"
46 * SECTION:gnetworkservice
47 * @short_description: A GSocketConnectable for resolving SRV records
50 * Like #GNetworkAddress does with hostnames, #GNetworkService
51 * provides an easy way to resolve a SRV record, and then attempt to
52 * connect to one of the hosts that implements that service, handling
53 * service priority/weighting, multiple IP addresses, and multiple
56 * See #GSrvTarget for more information about SRV records, and see
57 * #GSocketConnectable for and example of using the connectable
64 * A #GSocketConnectable for resolving a SRV record and connecting to
68 struct _GNetworkServicePrivate
70 gchar *service, *protocol, *domain, *scheme;
82 static void g_network_service_set_property (GObject *object,
86 static void g_network_service_get_property (GObject *object,
91 static void g_network_service_connectable_iface_init (GSocketConnectableIface *iface);
92 static GSocketAddressEnumerator *g_network_service_connectable_enumerate (GSocketConnectable *connectable);
93 static GSocketAddressEnumerator *g_network_service_connectable_proxy_enumerate (GSocketConnectable *connectable);
95 G_DEFINE_TYPE_WITH_CODE (GNetworkService, g_network_service, G_TYPE_OBJECT,
96 G_IMPLEMENT_INTERFACE (G_TYPE_SOCKET_CONNECTABLE,
97 g_network_service_connectable_iface_init))
100 g_network_service_finalize (GObject *object)
102 GNetworkService *srv = G_NETWORK_SERVICE (object);
104 g_free (srv->priv->service);
105 g_free (srv->priv->protocol);
106 g_free (srv->priv->domain);
107 g_free (srv->priv->scheme);
109 if (srv->priv->targets)
110 g_resolver_free_targets (srv->priv->targets);
112 G_OBJECT_CLASS (g_network_service_parent_class)->finalize (object);
116 g_network_service_class_init (GNetworkServiceClass *klass)
118 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
120 g_type_class_add_private (klass, sizeof (GNetworkServicePrivate));
122 gobject_class->set_property = g_network_service_set_property;
123 gobject_class->get_property = g_network_service_get_property;
124 gobject_class->finalize = g_network_service_finalize;
126 g_object_class_install_property (gobject_class, PROP_SERVICE,
127 g_param_spec_string ("service",
129 P_("Service name, eg \"ldap\""),
132 G_PARAM_CONSTRUCT_ONLY |
133 G_PARAM_STATIC_STRINGS));
134 g_object_class_install_property (gobject_class, PROP_PROTOCOL,
135 g_param_spec_string ("protocol",
137 P_("Network protocol, eg \"tcp\""),
140 G_PARAM_CONSTRUCT_ONLY |
141 G_PARAM_STATIC_STRINGS));
142 g_object_class_install_property (gobject_class, PROP_DOMAIN,
143 g_param_spec_string ("domain",
145 P_("Network domain, eg, \"example.com\""),
148 G_PARAM_CONSTRUCT_ONLY |
149 G_PARAM_STATIC_STRINGS));
150 g_object_class_install_property (gobject_class, PROP_DOMAIN,
151 g_param_spec_string ("scheme",
153 P_("Network scheme (default is to use service)"),
156 G_PARAM_STATIC_STRINGS));
161 g_network_service_connectable_iface_init (GSocketConnectableIface *connectable_iface)
163 connectable_iface->enumerate = g_network_service_connectable_enumerate;
164 connectable_iface->proxy_enumerate = g_network_service_connectable_proxy_enumerate;
168 g_network_service_init (GNetworkService *srv)
170 srv->priv = G_TYPE_INSTANCE_GET_PRIVATE (srv, G_TYPE_NETWORK_SERVICE,
171 GNetworkServicePrivate);
175 g_network_service_set_property (GObject *object,
180 GNetworkService *srv = G_NETWORK_SERVICE (object);
185 srv->priv->service = g_value_dup_string (value);
189 srv->priv->protocol = g_value_dup_string (value);
193 srv->priv->domain = g_value_dup_string (value);
197 g_network_service_set_scheme (srv, g_value_get_string (value));
201 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
207 g_network_service_get_property (GObject *object,
212 GNetworkService *srv = G_NETWORK_SERVICE (object);
217 g_value_set_string (value, g_network_service_get_service (srv));
221 g_value_set_string (value, g_network_service_get_protocol (srv));
225 g_value_set_string (value, g_network_service_get_domain (srv));
229 g_value_set_string (value, g_network_service_get_scheme (srv));
233 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
239 * g_network_service_new:
240 * @service: the service type to look up (eg, "ldap")
241 * @protocol: the networking protocol to use for @service (eg, "tcp")
242 * @domain: the DNS domain to look up the service in
244 * Creates a new #GNetworkService representing the given @service,
245 * @protocol, and @domain. This will initially be unresolved; use the
246 * #GSocketConnectable interface to resolve it.
248 * Return value: (transfer full) (type GNetworkService): a new #GNetworkService
253 g_network_service_new (const gchar *service,
254 const gchar *protocol,
257 return g_object_new (G_TYPE_NETWORK_SERVICE,
259 "protocol", protocol,
265 * g_network_service_get_service:
266 * @srv: a #GNetworkService
268 * Gets @srv's service name (eg, "ldap").
270 * Return value: @srv's service name
275 g_network_service_get_service (GNetworkService *srv)
277 g_return_val_if_fail (G_IS_NETWORK_SERVICE (srv), NULL);
279 return srv->priv->service;
283 * g_network_service_get_protocol:
284 * @srv: a #GNetworkService
286 * Gets @srv's protocol name (eg, "tcp").
288 * Return value: @srv's protocol name
293 g_network_service_get_protocol (GNetworkService *srv)
295 g_return_val_if_fail (G_IS_NETWORK_SERVICE (srv), NULL);
297 return srv->priv->protocol;
301 * g_network_service_get_domain:
302 * @srv: a #GNetworkService
304 * Gets the domain that @srv serves. This might be either UTF-8 or
305 * ASCII-encoded, depending on what @srv was created with.
307 * Return value: @srv's domain name
312 g_network_service_get_domain (GNetworkService *srv)
314 g_return_val_if_fail (G_IS_NETWORK_SERVICE (srv), NULL);
316 return srv->priv->domain;
320 * g_network_service_get_scheme:
321 * @srv: a #GNetworkService
323 * Get's the URI scheme used to resolve proxies. By default, the service name
326 * Return value: @srv's scheme name
331 g_network_service_get_scheme (GNetworkService *srv)
333 g_return_val_if_fail (G_IS_NETWORK_SERVICE (srv), NULL);
335 if (srv->priv->scheme)
336 return srv->priv->scheme;
338 return srv->priv->service;
342 * g_network_service_set_scheme:
343 * @srv: a #GNetworkService
344 * @scheme: a URI scheme
346 * Set's the URI scheme used to resolve proxies. By default, the service name
352 g_network_service_set_scheme (GNetworkService *srv,
355 g_return_if_fail (G_IS_NETWORK_SERVICE (srv));
357 if (srv->priv->scheme)
358 g_free (srv->priv->scheme);
359 srv->priv->scheme = g_strdup (scheme);
361 g_object_notify (G_OBJECT (srv), "scheme");
365 g_network_service_fallback_targets (GNetworkService *srv)
368 struct servent *entry;
371 entry = getservbyname (srv->priv->service, "tcp");
372 port = entry ? g_ntohs (entry->s_port) : 0;
373 #ifdef HAVE_ENDSERVENT
380 target = g_srv_target_new (srv->priv->domain, port, 0, 0);
381 return g_list_append (NULL, target);
384 #define G_TYPE_NETWORK_SERVICE_ADDRESS_ENUMERATOR (_g_network_service_address_enumerator_get_type ())
385 #define G_NETWORK_SERVICE_ADDRESS_ENUMERATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_NETWORK_SERVICE_ADDRESS_ENUMERATOR, GNetworkServiceAddressEnumerator))
388 GSocketAddressEnumerator parent_instance;
391 GNetworkService *srv;
392 GSocketAddressEnumerator *addr_enum;
398 /* For async operation */
399 GCancellable *cancellable;
400 GSimpleAsyncResult *result;
401 } GNetworkServiceAddressEnumerator;
404 GSocketAddressEnumeratorClass parent_class;
406 } GNetworkServiceAddressEnumeratorClass;
408 static GType _g_network_service_address_enumerator_get_type (void);
409 G_DEFINE_TYPE (GNetworkServiceAddressEnumerator, _g_network_service_address_enumerator, G_TYPE_SOCKET_ADDRESS_ENUMERATOR)
411 static GSocketAddress *
412 g_network_service_address_enumerator_next (GSocketAddressEnumerator *enumerator,
413 GCancellable *cancellable,
416 GNetworkServiceAddressEnumerator *srv_enum =
417 G_NETWORK_SERVICE_ADDRESS_ENUMERATOR (enumerator);
418 GSocketAddress *ret = NULL;
420 /* If we haven't yet resolved srv, do that */
421 if (!srv_enum->srv->priv->targets)
424 GError *my_error = NULL;
426 targets = g_resolver_lookup_service (srv_enum->resolver,
427 srv_enum->srv->priv->service,
428 srv_enum->srv->priv->protocol,
429 srv_enum->srv->priv->domain,
430 cancellable, &my_error);
431 if (!targets && g_error_matches (my_error, G_RESOLVER_ERROR,
432 G_RESOLVER_ERROR_NOT_FOUND))
434 targets = g_network_service_fallback_targets (srv_enum->srv);
436 g_clear_error (&my_error);
441 g_propagate_error (error, my_error);
445 srv_enum->srv->priv->targets = targets;
446 srv_enum->t = srv_enum->srv->priv->targets;
449 /* Delegate to GNetworkAddress */
452 if (srv_enum->addr_enum == NULL && srv_enum->t)
454 GError *error = NULL;
457 GSocketConnectable *addr;
458 GSrvTarget *target = srv_enum->t->data;
460 srv_enum->t = g_list_next (srv_enum->t);
462 hostname = g_hostname_to_ascii (g_srv_target_get_hostname (target));
464 if (hostname == NULL)
466 if (srv_enum->error == NULL)
468 g_error_new (G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
469 "Received invalid hostname '%s' from GSrvTarget",
470 g_srv_target_get_hostname (target));
474 uri = _g_uri_from_authority (g_network_service_get_scheme (srv_enum->srv),
476 g_srv_target_get_port (target),
480 addr = g_network_address_parse_uri (uri,
481 g_srv_target_get_port (target),
487 if (srv_enum->error == NULL)
488 srv_enum->error = error;
492 if (srv_enum->use_proxy)
493 srv_enum->addr_enum = g_socket_connectable_proxy_enumerate (addr);
495 srv_enum->addr_enum = g_socket_connectable_enumerate (addr);
496 g_object_unref (addr);
499 if (srv_enum->addr_enum)
501 GError *error = NULL;
503 ret = g_socket_address_enumerator_next (srv_enum->addr_enum,
509 if (srv_enum->error == NULL)
510 srv_enum->error = error;
512 g_error_free (error);
517 g_object_unref (srv_enum->addr_enum);
518 srv_enum->addr_enum = NULL;
522 while (srv_enum->addr_enum == NULL && srv_enum->t);
524 if (ret == NULL && srv_enum->error)
526 g_propagate_error (error, srv_enum->error);
527 srv_enum->error = NULL;
533 static void next_async_resolved_targets (GObject *source_object,
534 GAsyncResult *result,
536 static void next_async_have_targets (GNetworkServiceAddressEnumerator *srv_enum);
537 static void next_async_have_address (GObject *source_object,
538 GAsyncResult *result,
542 g_network_service_address_enumerator_next_async (GSocketAddressEnumerator *enumerator,
543 GCancellable *cancellable,
544 GAsyncReadyCallback callback,
547 GNetworkServiceAddressEnumerator *srv_enum =
548 G_NETWORK_SERVICE_ADDRESS_ENUMERATOR (enumerator);
550 g_return_if_fail (srv_enum->result == NULL);
552 srv_enum->result = g_simple_async_result_new (G_OBJECT (enumerator),
554 g_network_service_address_enumerator_next_async);
557 srv_enum->cancellable = g_object_ref (cancellable);
559 /* If we haven't yet resolved srv, do that */
560 if (!srv_enum->srv->priv->targets)
562 g_resolver_lookup_service_async (srv_enum->resolver,
563 srv_enum->srv->priv->service,
564 srv_enum->srv->priv->protocol,
565 srv_enum->srv->priv->domain,
567 next_async_resolved_targets,
571 next_async_have_targets (srv_enum);
575 next_async_resolved_targets (GObject *source_object,
576 GAsyncResult *result,
579 GNetworkServiceAddressEnumerator *srv_enum = user_data;
580 GError *error = NULL;
583 targets = g_resolver_lookup_service_finish (srv_enum->resolver,
586 if (!targets && g_error_matches (error, G_RESOLVER_ERROR,
587 G_RESOLVER_ERROR_NOT_FOUND))
589 targets = g_network_service_fallback_targets (srv_enum->srv);
591 g_clear_error (&error);
596 GSimpleAsyncResult *simple = srv_enum->result;
598 srv_enum->result = NULL;
599 g_simple_async_result_take_error (simple, error);
600 g_simple_async_result_complete (simple);
601 g_object_unref (simple);
605 srv_enum->t = srv_enum->srv->priv->targets = targets;
606 next_async_have_targets (srv_enum);
611 next_async_have_targets (GNetworkServiceAddressEnumerator *srv_enum)
613 /* Delegate to GNetworkAddress */
614 if (srv_enum->addr_enum == NULL && srv_enum->t)
616 GSocketConnectable *addr;
617 GSrvTarget *target = srv_enum->t->data;
619 srv_enum->t = g_list_next (srv_enum->t);
620 addr = g_network_address_new (g_srv_target_get_hostname (target),
621 (guint16) g_srv_target_get_port (target));
623 if (srv_enum->use_proxy)
624 srv_enum->addr_enum = g_socket_connectable_proxy_enumerate (addr);
626 srv_enum->addr_enum = g_socket_connectable_enumerate (addr);
628 g_object_unref (addr);
631 if (srv_enum->addr_enum)
633 g_socket_address_enumerator_next_async (srv_enum->addr_enum,
634 srv_enum->cancellable,
635 next_async_have_address,
640 GSimpleAsyncResult *simple = srv_enum->result;
641 srv_enum->result = NULL;
645 g_simple_async_result_take_error (simple, srv_enum->error);
646 srv_enum->error = NULL;
649 g_simple_async_result_complete (simple);
650 g_object_unref (simple);
655 next_async_have_address (GObject *source_object,
656 GAsyncResult *result,
659 GNetworkServiceAddressEnumerator *srv_enum = user_data;
660 GSocketAddress *address;
661 GError *error = NULL;
663 address = g_socket_address_enumerator_next_finish (srv_enum->addr_enum,
669 if (srv_enum->error == NULL)
670 srv_enum->error = error;
672 g_error_free (error);
677 g_object_unref (srv_enum->addr_enum);
678 srv_enum->addr_enum = NULL;
680 next_async_have_targets (srv_enum);
684 GSimpleAsyncResult *simple = srv_enum->result;
686 srv_enum->result = NULL;
689 g_simple_async_result_set_op_res_gpointer (simple,
690 address, g_object_unref);
692 g_simple_async_result_complete (simple);
693 g_object_unref (simple);
697 static GSocketAddress *
698 g_network_service_address_enumerator_next_finish (GSocketAddressEnumerator *enumerator,
699 GAsyncResult *result,
702 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
703 GSocketAddress *sockaddr;
705 if (g_simple_async_result_propagate_error (simple, error))
708 sockaddr = g_simple_async_result_get_op_res_gpointer (simple);
709 return sockaddr ? g_object_ref (sockaddr) : NULL;
713 _g_network_service_address_enumerator_init (GNetworkServiceAddressEnumerator *enumerator)
718 g_network_service_address_enumerator_finalize (GObject *object)
720 GNetworkServiceAddressEnumerator *srv_enum =
721 G_NETWORK_SERVICE_ADDRESS_ENUMERATOR (object);
724 g_object_unref (srv_enum->srv);
726 if (srv_enum->addr_enum)
727 g_object_unref (srv_enum->addr_enum);
729 if (srv_enum->resolver)
730 g_object_unref (srv_enum->resolver);
732 if (srv_enum->result)
733 g_object_unref (srv_enum->result);
735 if (srv_enum->cancellable)
736 g_object_unref (srv_enum->cancellable);
739 g_error_free (srv_enum->error);
741 G_OBJECT_CLASS (_g_network_service_address_enumerator_parent_class)->finalize (object);
745 _g_network_service_address_enumerator_class_init (GNetworkServiceAddressEnumeratorClass *srvenum_class)
747 GObjectClass *object_class = G_OBJECT_CLASS (srvenum_class);
748 GSocketAddressEnumeratorClass *enumerator_class =
749 G_SOCKET_ADDRESS_ENUMERATOR_CLASS (srvenum_class);
751 enumerator_class->next = g_network_service_address_enumerator_next;
752 enumerator_class->next_async = g_network_service_address_enumerator_next_async;
753 enumerator_class->next_finish = g_network_service_address_enumerator_next_finish;
755 object_class->finalize = g_network_service_address_enumerator_finalize;
758 static GSocketAddressEnumerator *
759 g_network_service_connectable_enumerate (GSocketConnectable *connectable)
761 GNetworkServiceAddressEnumerator *srv_enum;
763 srv_enum = g_object_new (G_TYPE_NETWORK_SERVICE_ADDRESS_ENUMERATOR, NULL);
764 srv_enum->srv = g_object_ref (connectable);
765 srv_enum->resolver = g_resolver_get_default ();
766 srv_enum->use_proxy = FALSE;
768 return G_SOCKET_ADDRESS_ENUMERATOR (srv_enum);
771 static GSocketAddressEnumerator *
772 g_network_service_connectable_proxy_enumerate (GSocketConnectable *connectable)
774 GSocketAddressEnumerator *addr_enum;
775 GNetworkServiceAddressEnumerator *srv_enum;
777 addr_enum = g_network_service_connectable_enumerate (connectable);
778 srv_enum = G_NETWORK_SERVICE_ADDRESS_ENUMERATOR (addr_enum);
779 srv_enum->use_proxy = TRUE;