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, see <http://www.gnu.org/licenses/>.
25 #include "gnetworkservice.h"
27 #include "gcancellable.h"
28 #include "ginetaddress.h"
29 #include "ginetsocketaddress.h"
31 #include "gnetworkaddress.h"
32 #include "gnetworkingprivate.h"
33 #include "gresolver.h"
35 #include "gsocketaddressenumerator.h"
36 #include "gsocketconnectable.h"
37 #include "gsrvtarget.h"
44 * SECTION:gnetworkservice
45 * @short_description: A GSocketConnectable for resolving SRV records
48 * Like #GNetworkAddress does with hostnames, #GNetworkService
49 * provides an easy way to resolve a SRV record, and then attempt to
50 * connect to one of the hosts that implements that service, handling
51 * service priority/weighting, multiple IP addresses, and multiple
54 * See #GSrvTarget for more information about SRV records, and see
55 * #GSocketConnectable for and example of using the connectable
62 * A #GSocketConnectable for resolving a SRV record and connecting to
66 struct _GNetworkServicePrivate
68 gchar *service, *protocol, *domain, *scheme;
80 static void g_network_service_set_property (GObject *object,
84 static void g_network_service_get_property (GObject *object,
89 static void g_network_service_connectable_iface_init (GSocketConnectableIface *iface);
90 static GSocketAddressEnumerator *g_network_service_connectable_enumerate (GSocketConnectable *connectable);
91 static GSocketAddressEnumerator *g_network_service_connectable_proxy_enumerate (GSocketConnectable *connectable);
93 G_DEFINE_TYPE_WITH_CODE (GNetworkService, g_network_service, G_TYPE_OBJECT,
94 G_ADD_PRIVATE (GNetworkService)
95 G_IMPLEMENT_INTERFACE (G_TYPE_SOCKET_CONNECTABLE,
96 g_network_service_connectable_iface_init))
99 g_network_service_finalize (GObject *object)
101 GNetworkService *srv = G_NETWORK_SERVICE (object);
103 g_free (srv->priv->service);
104 g_free (srv->priv->protocol);
105 g_free (srv->priv->domain);
106 g_free (srv->priv->scheme);
108 if (srv->priv->targets)
109 g_resolver_free_targets (srv->priv->targets);
111 G_OBJECT_CLASS (g_network_service_parent_class)->finalize (object);
115 g_network_service_class_init (GNetworkServiceClass *klass)
117 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
119 gobject_class->set_property = g_network_service_set_property;
120 gobject_class->get_property = g_network_service_get_property;
121 gobject_class->finalize = g_network_service_finalize;
123 g_object_class_install_property (gobject_class, PROP_SERVICE,
124 g_param_spec_string ("service",
126 P_("Service name, eg \"ldap\""),
129 G_PARAM_CONSTRUCT_ONLY |
130 G_PARAM_STATIC_STRINGS));
131 g_object_class_install_property (gobject_class, PROP_PROTOCOL,
132 g_param_spec_string ("protocol",
134 P_("Network protocol, eg \"tcp\""),
137 G_PARAM_CONSTRUCT_ONLY |
138 G_PARAM_STATIC_STRINGS));
139 g_object_class_install_property (gobject_class, PROP_DOMAIN,
140 g_param_spec_string ("domain",
142 P_("Network domain, eg, \"example.com\""),
145 G_PARAM_CONSTRUCT_ONLY |
146 G_PARAM_STATIC_STRINGS));
147 g_object_class_install_property (gobject_class, PROP_DOMAIN,
148 g_param_spec_string ("scheme",
150 P_("Network scheme (default is to use service)"),
153 G_PARAM_STATIC_STRINGS));
158 g_network_service_connectable_iface_init (GSocketConnectableIface *connectable_iface)
160 connectable_iface->enumerate = g_network_service_connectable_enumerate;
161 connectable_iface->proxy_enumerate = g_network_service_connectable_proxy_enumerate;
165 g_network_service_init (GNetworkService *srv)
167 srv->priv = g_network_service_get_instance_private (srv);
171 g_network_service_set_property (GObject *object,
176 GNetworkService *srv = G_NETWORK_SERVICE (object);
181 srv->priv->service = g_value_dup_string (value);
185 srv->priv->protocol = g_value_dup_string (value);
189 srv->priv->domain = g_value_dup_string (value);
193 g_network_service_set_scheme (srv, g_value_get_string (value));
197 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
203 g_network_service_get_property (GObject *object,
208 GNetworkService *srv = G_NETWORK_SERVICE (object);
213 g_value_set_string (value, g_network_service_get_service (srv));
217 g_value_set_string (value, g_network_service_get_protocol (srv));
221 g_value_set_string (value, g_network_service_get_domain (srv));
225 g_value_set_string (value, g_network_service_get_scheme (srv));
229 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
235 * g_network_service_new:
236 * @service: the service type to look up (eg, "ldap")
237 * @protocol: the networking protocol to use for @service (eg, "tcp")
238 * @domain: the DNS domain to look up the service in
240 * Creates a new #GNetworkService representing the given @service,
241 * @protocol, and @domain. This will initially be unresolved; use the
242 * #GSocketConnectable interface to resolve it.
244 * Returns: (transfer full) (type GNetworkService): a new #GNetworkService
249 g_network_service_new (const gchar *service,
250 const gchar *protocol,
253 return g_object_new (G_TYPE_NETWORK_SERVICE,
255 "protocol", protocol,
261 * g_network_service_get_service:
262 * @srv: a #GNetworkService
264 * Gets @srv's service name (eg, "ldap").
266 * Returns: @srv's service name
271 g_network_service_get_service (GNetworkService *srv)
273 g_return_val_if_fail (G_IS_NETWORK_SERVICE (srv), NULL);
275 return srv->priv->service;
279 * g_network_service_get_protocol:
280 * @srv: a #GNetworkService
282 * Gets @srv's protocol name (eg, "tcp").
284 * Returns: @srv's protocol name
289 g_network_service_get_protocol (GNetworkService *srv)
291 g_return_val_if_fail (G_IS_NETWORK_SERVICE (srv), NULL);
293 return srv->priv->protocol;
297 * g_network_service_get_domain:
298 * @srv: a #GNetworkService
300 * Gets the domain that @srv serves. This might be either UTF-8 or
301 * ASCII-encoded, depending on what @srv was created with.
303 * Returns: @srv's domain name
308 g_network_service_get_domain (GNetworkService *srv)
310 g_return_val_if_fail (G_IS_NETWORK_SERVICE (srv), NULL);
312 return srv->priv->domain;
316 * g_network_service_get_scheme:
317 * @srv: a #GNetworkService
319 * Get's the URI scheme used to resolve proxies. By default, the service name
322 * Returns: @srv's scheme name
327 g_network_service_get_scheme (GNetworkService *srv)
329 g_return_val_if_fail (G_IS_NETWORK_SERVICE (srv), NULL);
331 if (srv->priv->scheme)
332 return srv->priv->scheme;
334 return srv->priv->service;
338 * g_network_service_set_scheme:
339 * @srv: a #GNetworkService
340 * @scheme: a URI scheme
342 * Set's the URI scheme used to resolve proxies. By default, the service name
348 g_network_service_set_scheme (GNetworkService *srv,
351 g_return_if_fail (G_IS_NETWORK_SERVICE (srv));
353 g_free (srv->priv->scheme);
354 srv->priv->scheme = g_strdup (scheme);
356 g_object_notify (G_OBJECT (srv), "scheme");
360 g_network_service_fallback_targets (GNetworkService *srv)
363 struct servent *entry;
366 entry = getservbyname (srv->priv->service, "tcp");
367 port = entry ? g_ntohs (entry->s_port) : 0;
368 #ifdef HAVE_ENDSERVENT
375 target = g_srv_target_new (srv->priv->domain, port, 0, 0);
376 return g_list_append (NULL, target);
379 #define G_TYPE_NETWORK_SERVICE_ADDRESS_ENUMERATOR (_g_network_service_address_enumerator_get_type ())
380 #define G_NETWORK_SERVICE_ADDRESS_ENUMERATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_NETWORK_SERVICE_ADDRESS_ENUMERATOR, GNetworkServiceAddressEnumerator))
383 GSocketAddressEnumerator parent_instance;
386 GNetworkService *srv;
387 GSocketAddressEnumerator *addr_enum;
393 } GNetworkServiceAddressEnumerator;
396 GSocketAddressEnumeratorClass parent_class;
398 } GNetworkServiceAddressEnumeratorClass;
400 static GType _g_network_service_address_enumerator_get_type (void);
401 G_DEFINE_TYPE (GNetworkServiceAddressEnumerator, _g_network_service_address_enumerator, G_TYPE_SOCKET_ADDRESS_ENUMERATOR)
403 static GSocketAddress *
404 g_network_service_address_enumerator_next (GSocketAddressEnumerator *enumerator,
405 GCancellable *cancellable,
408 GNetworkServiceAddressEnumerator *srv_enum =
409 G_NETWORK_SERVICE_ADDRESS_ENUMERATOR (enumerator);
410 GSocketAddress *ret = NULL;
412 /* If we haven't yet resolved srv, do that */
413 if (!srv_enum->srv->priv->targets)
416 GError *my_error = NULL;
418 targets = g_resolver_lookup_service (srv_enum->resolver,
419 srv_enum->srv->priv->service,
420 srv_enum->srv->priv->protocol,
421 srv_enum->srv->priv->domain,
422 cancellable, &my_error);
423 if (!targets && g_error_matches (my_error, G_RESOLVER_ERROR,
424 G_RESOLVER_ERROR_NOT_FOUND))
426 targets = g_network_service_fallback_targets (srv_enum->srv);
428 g_clear_error (&my_error);
433 g_propagate_error (error, my_error);
437 srv_enum->srv->priv->targets = targets;
438 srv_enum->t = srv_enum->srv->priv->targets;
441 /* Delegate to GNetworkAddress */
444 if (srv_enum->addr_enum == NULL && srv_enum->t)
446 GError *error = NULL;
449 GSocketConnectable *addr;
450 GSrvTarget *target = srv_enum->t->data;
452 srv_enum->t = g_list_next (srv_enum->t);
454 hostname = g_hostname_to_ascii (g_srv_target_get_hostname (target));
456 if (hostname == NULL)
458 if (srv_enum->error == NULL)
460 g_error_new (G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
461 "Received invalid hostname '%s' from GSrvTarget",
462 g_srv_target_get_hostname (target));
466 uri = _g_uri_from_authority (g_network_service_get_scheme (srv_enum->srv),
468 g_srv_target_get_port (target),
472 addr = g_network_address_parse_uri (uri,
473 g_srv_target_get_port (target),
479 if (srv_enum->error == NULL)
480 srv_enum->error = error;
484 if (srv_enum->use_proxy)
485 srv_enum->addr_enum = g_socket_connectable_proxy_enumerate (addr);
487 srv_enum->addr_enum = g_socket_connectable_enumerate (addr);
488 g_object_unref (addr);
491 if (srv_enum->addr_enum)
493 GError *error = NULL;
495 ret = g_socket_address_enumerator_next (srv_enum->addr_enum,
501 if (srv_enum->error == NULL)
502 srv_enum->error = error;
504 g_error_free (error);
509 g_object_unref (srv_enum->addr_enum);
510 srv_enum->addr_enum = NULL;
514 while (srv_enum->addr_enum == NULL && srv_enum->t);
516 if (ret == NULL && srv_enum->error)
518 g_propagate_error (error, srv_enum->error);
519 srv_enum->error = NULL;
525 static void next_async_resolved_targets (GObject *source_object,
526 GAsyncResult *result,
528 static void next_async_have_targets (GTask *srv_enum);
529 static void next_async_have_address (GObject *source_object,
530 GAsyncResult *result,
534 g_network_service_address_enumerator_next_async (GSocketAddressEnumerator *enumerator,
535 GCancellable *cancellable,
536 GAsyncReadyCallback callback,
539 GNetworkServiceAddressEnumerator *srv_enum =
540 G_NETWORK_SERVICE_ADDRESS_ENUMERATOR (enumerator);
543 task = g_task_new (enumerator, cancellable, callback, user_data);
545 /* If we haven't yet resolved srv, do that */
546 if (!srv_enum->srv->priv->targets)
548 g_resolver_lookup_service_async (srv_enum->resolver,
549 srv_enum->srv->priv->service,
550 srv_enum->srv->priv->protocol,
551 srv_enum->srv->priv->domain,
553 next_async_resolved_targets,
557 next_async_have_targets (task);
561 next_async_resolved_targets (GObject *source_object,
562 GAsyncResult *result,
565 GTask *task = user_data;
566 GNetworkServiceAddressEnumerator *srv_enum = g_task_get_source_object (task);
567 GError *error = NULL;
570 targets = g_resolver_lookup_service_finish (srv_enum->resolver,
573 if (!targets && g_error_matches (error, G_RESOLVER_ERROR,
574 G_RESOLVER_ERROR_NOT_FOUND))
576 targets = g_network_service_fallback_targets (srv_enum->srv);
578 g_clear_error (&error);
583 g_task_return_error (task, error);
584 g_object_unref (task);
588 srv_enum->t = srv_enum->srv->priv->targets = targets;
589 next_async_have_targets (task);
594 next_async_have_targets (GTask *task)
596 GNetworkServiceAddressEnumerator *srv_enum = g_task_get_source_object (task);
598 /* Delegate to GNetworkAddress */
599 if (srv_enum->addr_enum == NULL && srv_enum->t)
601 GSocketConnectable *addr;
602 GSrvTarget *target = srv_enum->t->data;
604 srv_enum->t = g_list_next (srv_enum->t);
605 addr = g_network_address_new (g_srv_target_get_hostname (target),
606 (guint16) g_srv_target_get_port (target));
608 if (srv_enum->use_proxy)
609 srv_enum->addr_enum = g_socket_connectable_proxy_enumerate (addr);
611 srv_enum->addr_enum = g_socket_connectable_enumerate (addr);
613 g_object_unref (addr);
616 if (srv_enum->addr_enum)
618 g_socket_address_enumerator_next_async (srv_enum->addr_enum,
619 g_task_get_cancellable (task),
620 next_async_have_address,
627 g_task_return_error (task, srv_enum->error);
628 srv_enum->error = NULL;
631 g_task_return_pointer (task, NULL, NULL);
633 g_object_unref (task);
638 next_async_have_address (GObject *source_object,
639 GAsyncResult *result,
642 GTask *task = user_data;
643 GNetworkServiceAddressEnumerator *srv_enum = g_task_get_source_object (task);
644 GSocketAddress *address;
645 GError *error = NULL;
647 address = g_socket_address_enumerator_next_finish (srv_enum->addr_enum,
653 if (srv_enum->error == NULL)
654 srv_enum->error = error;
656 g_error_free (error);
661 g_object_unref (srv_enum->addr_enum);
662 srv_enum->addr_enum = NULL;
664 next_async_have_targets (task);
668 g_task_return_pointer (task, address, g_object_unref);
669 g_object_unref (task);
673 static GSocketAddress *
674 g_network_service_address_enumerator_next_finish (GSocketAddressEnumerator *enumerator,
675 GAsyncResult *result,
678 return g_task_propagate_pointer (G_TASK (result), error);
682 _g_network_service_address_enumerator_init (GNetworkServiceAddressEnumerator *enumerator)
687 g_network_service_address_enumerator_finalize (GObject *object)
689 GNetworkServiceAddressEnumerator *srv_enum =
690 G_NETWORK_SERVICE_ADDRESS_ENUMERATOR (object);
693 g_object_unref (srv_enum->srv);
695 if (srv_enum->addr_enum)
696 g_object_unref (srv_enum->addr_enum);
698 if (srv_enum->resolver)
699 g_object_unref (srv_enum->resolver);
702 g_error_free (srv_enum->error);
704 G_OBJECT_CLASS (_g_network_service_address_enumerator_parent_class)->finalize (object);
708 _g_network_service_address_enumerator_class_init (GNetworkServiceAddressEnumeratorClass *srvenum_class)
710 GObjectClass *object_class = G_OBJECT_CLASS (srvenum_class);
711 GSocketAddressEnumeratorClass *enumerator_class =
712 G_SOCKET_ADDRESS_ENUMERATOR_CLASS (srvenum_class);
714 enumerator_class->next = g_network_service_address_enumerator_next;
715 enumerator_class->next_async = g_network_service_address_enumerator_next_async;
716 enumerator_class->next_finish = g_network_service_address_enumerator_next_finish;
718 object_class->finalize = g_network_service_address_enumerator_finalize;
721 static GSocketAddressEnumerator *
722 g_network_service_connectable_enumerate (GSocketConnectable *connectable)
724 GNetworkServiceAddressEnumerator *srv_enum;
726 srv_enum = g_object_new (G_TYPE_NETWORK_SERVICE_ADDRESS_ENUMERATOR, NULL);
727 srv_enum->srv = g_object_ref (connectable);
728 srv_enum->resolver = g_resolver_get_default ();
729 srv_enum->use_proxy = FALSE;
731 return G_SOCKET_ADDRESS_ENUMERATOR (srv_enum);
734 static GSocketAddressEnumerator *
735 g_network_service_connectable_proxy_enumerate (GSocketConnectable *connectable)
737 GSocketAddressEnumerator *addr_enum;
738 GNetworkServiceAddressEnumerator *srv_enum;
740 addr_enum = g_network_service_connectable_enumerate (connectable);
741 srv_enum = G_NETWORK_SERVICE_ADDRESS_ENUMERATOR (addr_enum);
742 srv_enum->use_proxy = TRUE;