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): 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 G_DEFINE_TYPE (GNetworkServiceAddressEnumerator, _g_network_service_address_enumerator, G_TYPE_SOCKET_ADDRESS_ENUMERATOR)
410 static GSocketAddress *
411 g_network_service_address_enumerator_next (GSocketAddressEnumerator *enumerator,
412 GCancellable *cancellable,
415 GNetworkServiceAddressEnumerator *srv_enum =
416 G_NETWORK_SERVICE_ADDRESS_ENUMERATOR (enumerator);
417 GSocketAddress *ret = NULL;
419 /* If we haven't yet resolved srv, do that */
420 if (!srv_enum->srv->priv->targets)
423 GError *my_error = NULL;
425 targets = g_resolver_lookup_service (srv_enum->resolver,
426 srv_enum->srv->priv->service,
427 srv_enum->srv->priv->protocol,
428 srv_enum->srv->priv->domain,
429 cancellable, &my_error);
430 if (!targets && g_error_matches (my_error, G_RESOLVER_ERROR,
431 G_RESOLVER_ERROR_NOT_FOUND))
433 targets = g_network_service_fallback_targets (srv_enum->srv);
435 g_clear_error (&my_error);
440 g_propagate_error (error, my_error);
444 srv_enum->srv->priv->targets = targets;
445 srv_enum->t = srv_enum->srv->priv->targets;
448 /* Delegate to GNetworkAddress */
451 if (srv_enum->addr_enum == NULL && srv_enum->t)
453 GError *error = NULL;
456 GSocketConnectable *addr;
457 GSrvTarget *target = srv_enum->t->data;
459 srv_enum->t = g_list_next (srv_enum->t);
461 hostname = g_hostname_to_ascii (g_srv_target_get_hostname (target));
463 if (hostname == NULL)
465 if (srv_enum->error == NULL)
467 g_error_new (G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
468 "Received invalid hostname '%s' from GSrvTarget",
469 g_srv_target_get_hostname (target));
473 uri = _g_uri_from_authority (g_network_service_get_scheme (srv_enum->srv),
475 g_srv_target_get_port (target),
479 addr = g_network_address_parse_uri (uri,
480 g_srv_target_get_port (target),
486 if (srv_enum->error == NULL)
487 srv_enum->error = error;
491 if (srv_enum->use_proxy)
492 srv_enum->addr_enum = g_socket_connectable_proxy_enumerate (addr);
494 srv_enum->addr_enum = g_socket_connectable_enumerate (addr);
495 g_object_unref (addr);
498 if (srv_enum->addr_enum)
500 GError *error = NULL;
502 ret = g_socket_address_enumerator_next (srv_enum->addr_enum,
508 if (srv_enum->error == NULL)
509 srv_enum->error = error;
511 g_error_free (error);
516 g_object_unref (srv_enum->addr_enum);
517 srv_enum->addr_enum = NULL;
521 while (srv_enum->addr_enum == NULL && srv_enum->t);
523 if (ret == NULL && srv_enum->error)
525 g_propagate_error (error, srv_enum->error);
526 srv_enum->error = NULL;
532 static void next_async_resolved_targets (GObject *source_object,
533 GAsyncResult *result,
535 static void next_async_have_targets (GNetworkServiceAddressEnumerator *srv_enum);
536 static void next_async_have_address (GObject *source_object,
537 GAsyncResult *result,
541 g_network_service_address_enumerator_next_async (GSocketAddressEnumerator *enumerator,
542 GCancellable *cancellable,
543 GAsyncReadyCallback callback,
546 GNetworkServiceAddressEnumerator *srv_enum =
547 G_NETWORK_SERVICE_ADDRESS_ENUMERATOR (enumerator);
549 g_return_if_fail (srv_enum->result == NULL);
551 srv_enum->result = g_simple_async_result_new (G_OBJECT (enumerator),
553 g_network_service_address_enumerator_next_async);
556 srv_enum->cancellable = g_object_ref (cancellable);
558 /* If we haven't yet resolved srv, do that */
559 if (!srv_enum->srv->priv->targets)
561 g_resolver_lookup_service_async (srv_enum->resolver,
562 srv_enum->srv->priv->service,
563 srv_enum->srv->priv->protocol,
564 srv_enum->srv->priv->domain,
566 next_async_resolved_targets,
570 next_async_have_targets (srv_enum);
574 next_async_resolved_targets (GObject *source_object,
575 GAsyncResult *result,
578 GNetworkServiceAddressEnumerator *srv_enum = user_data;
579 GError *error = NULL;
582 targets = g_resolver_lookup_service_finish (srv_enum->resolver,
585 if (!targets && g_error_matches (error, G_RESOLVER_ERROR,
586 G_RESOLVER_ERROR_NOT_FOUND))
588 targets = g_network_service_fallback_targets (srv_enum->srv);
590 g_clear_error (&error);
595 GSimpleAsyncResult *simple = srv_enum->result;
597 srv_enum->result = NULL;
598 g_simple_async_result_take_error (simple, error);
599 g_simple_async_result_complete (simple);
600 g_object_unref (simple);
604 srv_enum->t = srv_enum->srv->priv->targets = targets;
605 next_async_have_targets (srv_enum);
610 next_async_have_targets (GNetworkServiceAddressEnumerator *srv_enum)
612 /* Delegate to GNetworkAddress */
613 if (srv_enum->addr_enum == NULL && srv_enum->t)
615 GSocketConnectable *addr;
616 GSrvTarget *target = srv_enum->t->data;
618 srv_enum->t = g_list_next (srv_enum->t);
619 addr = g_network_address_new (g_srv_target_get_hostname (target),
620 (guint16) g_srv_target_get_port (target));
622 if (srv_enum->use_proxy)
623 srv_enum->addr_enum = g_socket_connectable_proxy_enumerate (addr);
625 srv_enum->addr_enum = g_socket_connectable_enumerate (addr);
627 g_object_unref (addr);
630 if (srv_enum->addr_enum)
632 g_socket_address_enumerator_next_async (srv_enum->addr_enum,
633 srv_enum->cancellable,
634 next_async_have_address,
639 GSimpleAsyncResult *simple = srv_enum->result;
640 srv_enum->result = NULL;
644 g_simple_async_result_take_error (simple, srv_enum->error);
645 srv_enum->error = NULL;
648 g_simple_async_result_complete (simple);
649 g_object_unref (simple);
654 next_async_have_address (GObject *source_object,
655 GAsyncResult *result,
658 GNetworkServiceAddressEnumerator *srv_enum = user_data;
659 GSocketAddress *address;
660 GError *error = NULL;
662 address = g_socket_address_enumerator_next_finish (srv_enum->addr_enum,
668 if (srv_enum->error == NULL)
669 srv_enum->error = error;
671 g_error_free (error);
676 g_object_unref (srv_enum->addr_enum);
677 srv_enum->addr_enum = NULL;
679 next_async_have_targets (srv_enum);
683 GSimpleAsyncResult *simple = srv_enum->result;
685 srv_enum->result = NULL;
688 g_simple_async_result_set_op_res_gpointer (simple,
689 address, g_object_unref);
691 g_simple_async_result_complete (simple);
692 g_object_unref (simple);
696 static GSocketAddress *
697 g_network_service_address_enumerator_next_finish (GSocketAddressEnumerator *enumerator,
698 GAsyncResult *result,
701 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
702 GSocketAddress *sockaddr;
704 if (g_simple_async_result_propagate_error (simple, error))
707 sockaddr = g_simple_async_result_get_op_res_gpointer (simple);
708 return sockaddr ? g_object_ref (sockaddr) : NULL;
712 _g_network_service_address_enumerator_init (GNetworkServiceAddressEnumerator *enumerator)
717 g_network_service_address_enumerator_finalize (GObject *object)
719 GNetworkServiceAddressEnumerator *srv_enum =
720 G_NETWORK_SERVICE_ADDRESS_ENUMERATOR (object);
723 g_object_unref (srv_enum->srv);
725 if (srv_enum->addr_enum)
726 g_object_unref (srv_enum->addr_enum);
728 if (srv_enum->resolver)
729 g_object_unref (srv_enum->resolver);
731 if (srv_enum->result)
732 g_object_unref (srv_enum->result);
734 if (srv_enum->cancellable)
735 g_object_unref (srv_enum->cancellable);
738 g_error_free (srv_enum->error);
740 G_OBJECT_CLASS (_g_network_service_address_enumerator_parent_class)->finalize (object);
744 _g_network_service_address_enumerator_class_init (GNetworkServiceAddressEnumeratorClass *srvenum_class)
746 GObjectClass *object_class = G_OBJECT_CLASS (srvenum_class);
747 GSocketAddressEnumeratorClass *enumerator_class =
748 G_SOCKET_ADDRESS_ENUMERATOR_CLASS (srvenum_class);
750 enumerator_class->next = g_network_service_address_enumerator_next;
751 enumerator_class->next_async = g_network_service_address_enumerator_next_async;
752 enumerator_class->next_finish = g_network_service_address_enumerator_next_finish;
754 object_class->finalize = g_network_service_address_enumerator_finalize;
757 static GSocketAddressEnumerator *
758 g_network_service_connectable_enumerate (GSocketConnectable *connectable)
760 GNetworkServiceAddressEnumerator *srv_enum;
762 srv_enum = g_object_new (G_TYPE_NETWORK_SERVICE_ADDRESS_ENUMERATOR, NULL);
763 srv_enum->srv = g_object_ref (connectable);
764 srv_enum->resolver = g_resolver_get_default ();
765 srv_enum->use_proxy = FALSE;
767 return G_SOCKET_ADDRESS_ENUMERATOR (srv_enum);
770 static GSocketAddressEnumerator *
771 g_network_service_connectable_proxy_enumerate (GSocketConnectable *connectable)
773 GSocketAddressEnumerator *addr_enum;
774 GNetworkServiceAddressEnumerator *srv_enum;
776 addr_enum = g_network_service_connectable_enumerate (connectable);
777 srv_enum = G_NETWORK_SERVICE_ADDRESS_ENUMERATOR (addr_enum);
778 srv_enum->use_proxy = TRUE;