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 * Return value: (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 * Return value: @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 * Return value: @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 * Return value: @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 * Return value: @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 if (srv->priv->scheme)
354 g_free (srv->priv->scheme);
355 srv->priv->scheme = g_strdup (scheme);
357 g_object_notify (G_OBJECT (srv), "scheme");
361 g_network_service_fallback_targets (GNetworkService *srv)
364 struct servent *entry;
367 entry = getservbyname (srv->priv->service, "tcp");
368 port = entry ? g_ntohs (entry->s_port) : 0;
369 #ifdef HAVE_ENDSERVENT
376 target = g_srv_target_new (srv->priv->domain, port, 0, 0);
377 return g_list_append (NULL, target);
380 #define G_TYPE_NETWORK_SERVICE_ADDRESS_ENUMERATOR (_g_network_service_address_enumerator_get_type ())
381 #define G_NETWORK_SERVICE_ADDRESS_ENUMERATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_NETWORK_SERVICE_ADDRESS_ENUMERATOR, GNetworkServiceAddressEnumerator))
384 GSocketAddressEnumerator parent_instance;
387 GNetworkService *srv;
388 GSocketAddressEnumerator *addr_enum;
394 } GNetworkServiceAddressEnumerator;
397 GSocketAddressEnumeratorClass parent_class;
399 } GNetworkServiceAddressEnumeratorClass;
401 static GType _g_network_service_address_enumerator_get_type (void);
402 G_DEFINE_TYPE (GNetworkServiceAddressEnumerator, _g_network_service_address_enumerator, G_TYPE_SOCKET_ADDRESS_ENUMERATOR)
404 static GSocketAddress *
405 g_network_service_address_enumerator_next (GSocketAddressEnumerator *enumerator,
406 GCancellable *cancellable,
409 GNetworkServiceAddressEnumerator *srv_enum =
410 G_NETWORK_SERVICE_ADDRESS_ENUMERATOR (enumerator);
411 GSocketAddress *ret = NULL;
413 /* If we haven't yet resolved srv, do that */
414 if (!srv_enum->srv->priv->targets)
417 GError *my_error = NULL;
419 targets = g_resolver_lookup_service (srv_enum->resolver,
420 srv_enum->srv->priv->service,
421 srv_enum->srv->priv->protocol,
422 srv_enum->srv->priv->domain,
423 cancellable, &my_error);
424 if (!targets && g_error_matches (my_error, G_RESOLVER_ERROR,
425 G_RESOLVER_ERROR_NOT_FOUND))
427 targets = g_network_service_fallback_targets (srv_enum->srv);
429 g_clear_error (&my_error);
434 g_propagate_error (error, my_error);
438 srv_enum->srv->priv->targets = targets;
439 srv_enum->t = srv_enum->srv->priv->targets;
442 /* Delegate to GNetworkAddress */
445 if (srv_enum->addr_enum == NULL && srv_enum->t)
447 GError *error = NULL;
450 GSocketConnectable *addr;
451 GSrvTarget *target = srv_enum->t->data;
453 srv_enum->t = g_list_next (srv_enum->t);
455 hostname = g_hostname_to_ascii (g_srv_target_get_hostname (target));
457 if (hostname == NULL)
459 if (srv_enum->error == NULL)
461 g_error_new (G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
462 "Received invalid hostname '%s' from GSrvTarget",
463 g_srv_target_get_hostname (target));
467 uri = _g_uri_from_authority (g_network_service_get_scheme (srv_enum->srv),
469 g_srv_target_get_port (target),
473 addr = g_network_address_parse_uri (uri,
474 g_srv_target_get_port (target),
480 if (srv_enum->error == NULL)
481 srv_enum->error = error;
485 if (srv_enum->use_proxy)
486 srv_enum->addr_enum = g_socket_connectable_proxy_enumerate (addr);
488 srv_enum->addr_enum = g_socket_connectable_enumerate (addr);
489 g_object_unref (addr);
492 if (srv_enum->addr_enum)
494 GError *error = NULL;
496 ret = g_socket_address_enumerator_next (srv_enum->addr_enum,
502 if (srv_enum->error == NULL)
503 srv_enum->error = error;
505 g_error_free (error);
510 g_object_unref (srv_enum->addr_enum);
511 srv_enum->addr_enum = NULL;
515 while (srv_enum->addr_enum == NULL && srv_enum->t);
517 if (ret == NULL && srv_enum->error)
519 g_propagate_error (error, srv_enum->error);
520 srv_enum->error = NULL;
526 static void next_async_resolved_targets (GObject *source_object,
527 GAsyncResult *result,
529 static void next_async_have_targets (GTask *srv_enum);
530 static void next_async_have_address (GObject *source_object,
531 GAsyncResult *result,
535 g_network_service_address_enumerator_next_async (GSocketAddressEnumerator *enumerator,
536 GCancellable *cancellable,
537 GAsyncReadyCallback callback,
540 GNetworkServiceAddressEnumerator *srv_enum =
541 G_NETWORK_SERVICE_ADDRESS_ENUMERATOR (enumerator);
544 task = g_task_new (enumerator, cancellable, callback, user_data);
546 /* If we haven't yet resolved srv, do that */
547 if (!srv_enum->srv->priv->targets)
549 g_resolver_lookup_service_async (srv_enum->resolver,
550 srv_enum->srv->priv->service,
551 srv_enum->srv->priv->protocol,
552 srv_enum->srv->priv->domain,
554 next_async_resolved_targets,
558 next_async_have_targets (task);
562 next_async_resolved_targets (GObject *source_object,
563 GAsyncResult *result,
566 GTask *task = user_data;
567 GNetworkServiceAddressEnumerator *srv_enum = g_task_get_source_object (task);
568 GError *error = NULL;
571 targets = g_resolver_lookup_service_finish (srv_enum->resolver,
574 if (!targets && g_error_matches (error, G_RESOLVER_ERROR,
575 G_RESOLVER_ERROR_NOT_FOUND))
577 targets = g_network_service_fallback_targets (srv_enum->srv);
579 g_clear_error (&error);
584 g_task_return_error (task, error);
585 g_object_unref (task);
589 srv_enum->t = srv_enum->srv->priv->targets = targets;
590 next_async_have_targets (task);
595 next_async_have_targets (GTask *task)
597 GNetworkServiceAddressEnumerator *srv_enum = g_task_get_source_object (task);
599 /* Delegate to GNetworkAddress */
600 if (srv_enum->addr_enum == NULL && srv_enum->t)
602 GSocketConnectable *addr;
603 GSrvTarget *target = srv_enum->t->data;
605 srv_enum->t = g_list_next (srv_enum->t);
606 addr = g_network_address_new (g_srv_target_get_hostname (target),
607 (guint16) g_srv_target_get_port (target));
609 if (srv_enum->use_proxy)
610 srv_enum->addr_enum = g_socket_connectable_proxy_enumerate (addr);
612 srv_enum->addr_enum = g_socket_connectable_enumerate (addr);
614 g_object_unref (addr);
617 if (srv_enum->addr_enum)
619 g_socket_address_enumerator_next_async (srv_enum->addr_enum,
620 g_task_get_cancellable (task),
621 next_async_have_address,
628 g_task_return_error (task, srv_enum->error);
629 srv_enum->error = NULL;
632 g_task_return_pointer (task, NULL, NULL);
634 g_object_unref (task);
639 next_async_have_address (GObject *source_object,
640 GAsyncResult *result,
643 GTask *task = user_data;
644 GNetworkServiceAddressEnumerator *srv_enum = g_task_get_source_object (task);
645 GSocketAddress *address;
646 GError *error = NULL;
648 address = g_socket_address_enumerator_next_finish (srv_enum->addr_enum,
654 if (srv_enum->error == NULL)
655 srv_enum->error = error;
657 g_error_free (error);
662 g_object_unref (srv_enum->addr_enum);
663 srv_enum->addr_enum = NULL;
665 next_async_have_targets (task);
669 g_task_return_pointer (task, address, g_object_unref);
670 g_object_unref (task);
674 static GSocketAddress *
675 g_network_service_address_enumerator_next_finish (GSocketAddressEnumerator *enumerator,
676 GAsyncResult *result,
679 return g_task_propagate_pointer (G_TASK (result), error);
683 _g_network_service_address_enumerator_init (GNetworkServiceAddressEnumerator *enumerator)
688 g_network_service_address_enumerator_finalize (GObject *object)
690 GNetworkServiceAddressEnumerator *srv_enum =
691 G_NETWORK_SERVICE_ADDRESS_ENUMERATOR (object);
694 g_object_unref (srv_enum->srv);
696 if (srv_enum->addr_enum)
697 g_object_unref (srv_enum->addr_enum);
699 if (srv_enum->resolver)
700 g_object_unref (srv_enum->resolver);
703 g_error_free (srv_enum->error);
705 G_OBJECT_CLASS (_g_network_service_address_enumerator_parent_class)->finalize (object);
709 _g_network_service_address_enumerator_class_init (GNetworkServiceAddressEnumeratorClass *srvenum_class)
711 GObjectClass *object_class = G_OBJECT_CLASS (srvenum_class);
712 GSocketAddressEnumeratorClass *enumerator_class =
713 G_SOCKET_ADDRESS_ENUMERATOR_CLASS (srvenum_class);
715 enumerator_class->next = g_network_service_address_enumerator_next;
716 enumerator_class->next_async = g_network_service_address_enumerator_next_async;
717 enumerator_class->next_finish = g_network_service_address_enumerator_next_finish;
719 object_class->finalize = g_network_service_address_enumerator_finalize;
722 static GSocketAddressEnumerator *
723 g_network_service_connectable_enumerate (GSocketConnectable *connectable)
725 GNetworkServiceAddressEnumerator *srv_enum;
727 srv_enum = g_object_new (G_TYPE_NETWORK_SERVICE_ADDRESS_ENUMERATOR, NULL);
728 srv_enum->srv = g_object_ref (connectable);
729 srv_enum->resolver = g_resolver_get_default ();
730 srv_enum->use_proxy = FALSE;
732 return G_SOCKET_ADDRESS_ENUMERATOR (srv_enum);
735 static GSocketAddressEnumerator *
736 g_network_service_connectable_proxy_enumerate (GSocketConnectable *connectable)
738 GSocketAddressEnumerator *addr_enum;
739 GNetworkServiceAddressEnumerator *srv_enum;
741 addr_enum = g_network_service_connectable_enumerate (connectable);
742 srv_enum = G_NETWORK_SERVICE_ADDRESS_ENUMERATOR (addr_enum);
743 srv_enum->use_proxy = TRUE;