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"
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_ADD_PRIVATE (GNetworkService)
97 G_IMPLEMENT_INTERFACE (G_TYPE_SOCKET_CONNECTABLE,
98 g_network_service_connectable_iface_init))
101 g_network_service_finalize (GObject *object)
103 GNetworkService *srv = G_NETWORK_SERVICE (object);
105 g_free (srv->priv->service);
106 g_free (srv->priv->protocol);
107 g_free (srv->priv->domain);
108 g_free (srv->priv->scheme);
110 if (srv->priv->targets)
111 g_resolver_free_targets (srv->priv->targets);
113 G_OBJECT_CLASS (g_network_service_parent_class)->finalize (object);
117 g_network_service_class_init (GNetworkServiceClass *klass)
119 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
121 gobject_class->set_property = g_network_service_set_property;
122 gobject_class->get_property = g_network_service_get_property;
123 gobject_class->finalize = g_network_service_finalize;
125 g_object_class_install_property (gobject_class, PROP_SERVICE,
126 g_param_spec_string ("service",
128 P_("Service name, eg \"ldap\""),
131 G_PARAM_CONSTRUCT_ONLY |
132 G_PARAM_STATIC_STRINGS));
133 g_object_class_install_property (gobject_class, PROP_PROTOCOL,
134 g_param_spec_string ("protocol",
136 P_("Network protocol, eg \"tcp\""),
139 G_PARAM_CONSTRUCT_ONLY |
140 G_PARAM_STATIC_STRINGS));
141 g_object_class_install_property (gobject_class, PROP_DOMAIN,
142 g_param_spec_string ("domain",
144 P_("Network domain, eg, \"example.com\""),
147 G_PARAM_CONSTRUCT_ONLY |
148 G_PARAM_STATIC_STRINGS));
149 g_object_class_install_property (gobject_class, PROP_DOMAIN,
150 g_param_spec_string ("scheme",
152 P_("Network scheme (default is to use service)"),
155 G_PARAM_STATIC_STRINGS));
160 g_network_service_connectable_iface_init (GSocketConnectableIface *connectable_iface)
162 connectable_iface->enumerate = g_network_service_connectable_enumerate;
163 connectable_iface->proxy_enumerate = g_network_service_connectable_proxy_enumerate;
167 g_network_service_init (GNetworkService *srv)
169 srv->priv = g_network_service_get_instance_private (srv);
173 g_network_service_set_property (GObject *object,
178 GNetworkService *srv = G_NETWORK_SERVICE (object);
183 srv->priv->service = g_value_dup_string (value);
187 srv->priv->protocol = g_value_dup_string (value);
191 srv->priv->domain = g_value_dup_string (value);
195 g_network_service_set_scheme (srv, g_value_get_string (value));
199 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
205 g_network_service_get_property (GObject *object,
210 GNetworkService *srv = G_NETWORK_SERVICE (object);
215 g_value_set_string (value, g_network_service_get_service (srv));
219 g_value_set_string (value, g_network_service_get_protocol (srv));
223 g_value_set_string (value, g_network_service_get_domain (srv));
227 g_value_set_string (value, g_network_service_get_scheme (srv));
231 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
237 * g_network_service_new:
238 * @service: the service type to look up (eg, "ldap")
239 * @protocol: the networking protocol to use for @service (eg, "tcp")
240 * @domain: the DNS domain to look up the service in
242 * Creates a new #GNetworkService representing the given @service,
243 * @protocol, and @domain. This will initially be unresolved; use the
244 * #GSocketConnectable interface to resolve it.
246 * Return value: (transfer full) (type GNetworkService): a new #GNetworkService
251 g_network_service_new (const gchar *service,
252 const gchar *protocol,
255 return g_object_new (G_TYPE_NETWORK_SERVICE,
257 "protocol", protocol,
263 * g_network_service_get_service:
264 * @srv: a #GNetworkService
266 * Gets @srv's service name (eg, "ldap").
268 * Return value: @srv's service name
273 g_network_service_get_service (GNetworkService *srv)
275 g_return_val_if_fail (G_IS_NETWORK_SERVICE (srv), NULL);
277 return srv->priv->service;
281 * g_network_service_get_protocol:
282 * @srv: a #GNetworkService
284 * Gets @srv's protocol name (eg, "tcp").
286 * Return value: @srv's protocol name
291 g_network_service_get_protocol (GNetworkService *srv)
293 g_return_val_if_fail (G_IS_NETWORK_SERVICE (srv), NULL);
295 return srv->priv->protocol;
299 * g_network_service_get_domain:
300 * @srv: a #GNetworkService
302 * Gets the domain that @srv serves. This might be either UTF-8 or
303 * ASCII-encoded, depending on what @srv was created with.
305 * Return value: @srv's domain name
310 g_network_service_get_domain (GNetworkService *srv)
312 g_return_val_if_fail (G_IS_NETWORK_SERVICE (srv), NULL);
314 return srv->priv->domain;
318 * g_network_service_get_scheme:
319 * @srv: a #GNetworkService
321 * Get's the URI scheme used to resolve proxies. By default, the service name
324 * Return value: @srv's scheme name
329 g_network_service_get_scheme (GNetworkService *srv)
331 g_return_val_if_fail (G_IS_NETWORK_SERVICE (srv), NULL);
333 if (srv->priv->scheme)
334 return srv->priv->scheme;
336 return srv->priv->service;
340 * g_network_service_set_scheme:
341 * @srv: a #GNetworkService
342 * @scheme: a URI scheme
344 * Set's the URI scheme used to resolve proxies. By default, the service name
350 g_network_service_set_scheme (GNetworkService *srv,
353 g_return_if_fail (G_IS_NETWORK_SERVICE (srv));
355 if (srv->priv->scheme)
356 g_free (srv->priv->scheme);
357 srv->priv->scheme = g_strdup (scheme);
359 g_object_notify (G_OBJECT (srv), "scheme");
363 g_network_service_fallback_targets (GNetworkService *srv)
366 struct servent *entry;
369 entry = getservbyname (srv->priv->service, "tcp");
370 port = entry ? g_ntohs (entry->s_port) : 0;
371 #ifdef HAVE_ENDSERVENT
378 target = g_srv_target_new (srv->priv->domain, port, 0, 0);
379 return g_list_append (NULL, target);
382 #define G_TYPE_NETWORK_SERVICE_ADDRESS_ENUMERATOR (_g_network_service_address_enumerator_get_type ())
383 #define G_NETWORK_SERVICE_ADDRESS_ENUMERATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_NETWORK_SERVICE_ADDRESS_ENUMERATOR, GNetworkServiceAddressEnumerator))
386 GSocketAddressEnumerator parent_instance;
389 GNetworkService *srv;
390 GSocketAddressEnumerator *addr_enum;
396 } GNetworkServiceAddressEnumerator;
399 GSocketAddressEnumeratorClass parent_class;
401 } GNetworkServiceAddressEnumeratorClass;
403 static GType _g_network_service_address_enumerator_get_type (void);
404 G_DEFINE_TYPE (GNetworkServiceAddressEnumerator, _g_network_service_address_enumerator, G_TYPE_SOCKET_ADDRESS_ENUMERATOR)
406 static GSocketAddress *
407 g_network_service_address_enumerator_next (GSocketAddressEnumerator *enumerator,
408 GCancellable *cancellable,
411 GNetworkServiceAddressEnumerator *srv_enum =
412 G_NETWORK_SERVICE_ADDRESS_ENUMERATOR (enumerator);
413 GSocketAddress *ret = NULL;
415 /* If we haven't yet resolved srv, do that */
416 if (!srv_enum->srv->priv->targets)
419 GError *my_error = NULL;
421 targets = g_resolver_lookup_service (srv_enum->resolver,
422 srv_enum->srv->priv->service,
423 srv_enum->srv->priv->protocol,
424 srv_enum->srv->priv->domain,
425 cancellable, &my_error);
426 if (!targets && g_error_matches (my_error, G_RESOLVER_ERROR,
427 G_RESOLVER_ERROR_NOT_FOUND))
429 targets = g_network_service_fallback_targets (srv_enum->srv);
431 g_clear_error (&my_error);
436 g_propagate_error (error, my_error);
440 srv_enum->srv->priv->targets = targets;
441 srv_enum->t = srv_enum->srv->priv->targets;
444 /* Delegate to GNetworkAddress */
447 if (srv_enum->addr_enum == NULL && srv_enum->t)
449 GError *error = NULL;
452 GSocketConnectable *addr;
453 GSrvTarget *target = srv_enum->t->data;
455 srv_enum->t = g_list_next (srv_enum->t);
457 hostname = g_hostname_to_ascii (g_srv_target_get_hostname (target));
459 if (hostname == NULL)
461 if (srv_enum->error == NULL)
463 g_error_new (G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
464 "Received invalid hostname '%s' from GSrvTarget",
465 g_srv_target_get_hostname (target));
469 uri = _g_uri_from_authority (g_network_service_get_scheme (srv_enum->srv),
471 g_srv_target_get_port (target),
475 addr = g_network_address_parse_uri (uri,
476 g_srv_target_get_port (target),
482 if (srv_enum->error == NULL)
483 srv_enum->error = error;
487 if (srv_enum->use_proxy)
488 srv_enum->addr_enum = g_socket_connectable_proxy_enumerate (addr);
490 srv_enum->addr_enum = g_socket_connectable_enumerate (addr);
491 g_object_unref (addr);
494 if (srv_enum->addr_enum)
496 GError *error = NULL;
498 ret = g_socket_address_enumerator_next (srv_enum->addr_enum,
504 if (srv_enum->error == NULL)
505 srv_enum->error = error;
507 g_error_free (error);
512 g_object_unref (srv_enum->addr_enum);
513 srv_enum->addr_enum = NULL;
517 while (srv_enum->addr_enum == NULL && srv_enum->t);
519 if (ret == NULL && srv_enum->error)
521 g_propagate_error (error, srv_enum->error);
522 srv_enum->error = NULL;
528 static void next_async_resolved_targets (GObject *source_object,
529 GAsyncResult *result,
531 static void next_async_have_targets (GTask *srv_enum);
532 static void next_async_have_address (GObject *source_object,
533 GAsyncResult *result,
537 g_network_service_address_enumerator_next_async (GSocketAddressEnumerator *enumerator,
538 GCancellable *cancellable,
539 GAsyncReadyCallback callback,
542 GNetworkServiceAddressEnumerator *srv_enum =
543 G_NETWORK_SERVICE_ADDRESS_ENUMERATOR (enumerator);
546 task = g_task_new (enumerator, cancellable, callback, user_data);
548 /* If we haven't yet resolved srv, do that */
549 if (!srv_enum->srv->priv->targets)
551 g_resolver_lookup_service_async (srv_enum->resolver,
552 srv_enum->srv->priv->service,
553 srv_enum->srv->priv->protocol,
554 srv_enum->srv->priv->domain,
556 next_async_resolved_targets,
560 next_async_have_targets (task);
564 next_async_resolved_targets (GObject *source_object,
565 GAsyncResult *result,
568 GTask *task = user_data;
569 GNetworkServiceAddressEnumerator *srv_enum = g_task_get_source_object (task);
570 GError *error = NULL;
573 targets = g_resolver_lookup_service_finish (srv_enum->resolver,
576 if (!targets && g_error_matches (error, G_RESOLVER_ERROR,
577 G_RESOLVER_ERROR_NOT_FOUND))
579 targets = g_network_service_fallback_targets (srv_enum->srv);
581 g_clear_error (&error);
586 g_task_return_error (task, error);
587 g_object_unref (task);
591 srv_enum->t = srv_enum->srv->priv->targets = targets;
592 next_async_have_targets (task);
597 next_async_have_targets (GTask *task)
599 GNetworkServiceAddressEnumerator *srv_enum = g_task_get_source_object (task);
601 /* Delegate to GNetworkAddress */
602 if (srv_enum->addr_enum == NULL && srv_enum->t)
604 GSocketConnectable *addr;
605 GSrvTarget *target = srv_enum->t->data;
607 srv_enum->t = g_list_next (srv_enum->t);
608 addr = g_network_address_new (g_srv_target_get_hostname (target),
609 (guint16) g_srv_target_get_port (target));
611 if (srv_enum->use_proxy)
612 srv_enum->addr_enum = g_socket_connectable_proxy_enumerate (addr);
614 srv_enum->addr_enum = g_socket_connectable_enumerate (addr);
616 g_object_unref (addr);
619 if (srv_enum->addr_enum)
621 g_socket_address_enumerator_next_async (srv_enum->addr_enum,
622 g_task_get_cancellable (task),
623 next_async_have_address,
630 g_task_return_error (task, srv_enum->error);
631 srv_enum->error = NULL;
634 g_task_return_pointer (task, NULL, NULL);
636 g_object_unref (task);
641 next_async_have_address (GObject *source_object,
642 GAsyncResult *result,
645 GTask *task = user_data;
646 GNetworkServiceAddressEnumerator *srv_enum = g_task_get_source_object (task);
647 GSocketAddress *address;
648 GError *error = NULL;
650 address = g_socket_address_enumerator_next_finish (srv_enum->addr_enum,
656 if (srv_enum->error == NULL)
657 srv_enum->error = error;
659 g_error_free (error);
664 g_object_unref (srv_enum->addr_enum);
665 srv_enum->addr_enum = NULL;
667 next_async_have_targets (task);
671 g_task_return_pointer (task, address, g_object_unref);
672 g_object_unref (task);
676 static GSocketAddress *
677 g_network_service_address_enumerator_next_finish (GSocketAddressEnumerator *enumerator,
678 GAsyncResult *result,
681 return g_task_propagate_pointer (G_TASK (result), error);
685 _g_network_service_address_enumerator_init (GNetworkServiceAddressEnumerator *enumerator)
690 g_network_service_address_enumerator_finalize (GObject *object)
692 GNetworkServiceAddressEnumerator *srv_enum =
693 G_NETWORK_SERVICE_ADDRESS_ENUMERATOR (object);
696 g_object_unref (srv_enum->srv);
698 if (srv_enum->addr_enum)
699 g_object_unref (srv_enum->addr_enum);
701 if (srv_enum->resolver)
702 g_object_unref (srv_enum->resolver);
705 g_error_free (srv_enum->error);
707 G_OBJECT_CLASS (_g_network_service_address_enumerator_parent_class)->finalize (object);
711 _g_network_service_address_enumerator_class_init (GNetworkServiceAddressEnumeratorClass *srvenum_class)
713 GObjectClass *object_class = G_OBJECT_CLASS (srvenum_class);
714 GSocketAddressEnumeratorClass *enumerator_class =
715 G_SOCKET_ADDRESS_ENUMERATOR_CLASS (srvenum_class);
717 enumerator_class->next = g_network_service_address_enumerator_next;
718 enumerator_class->next_async = g_network_service_address_enumerator_next_async;
719 enumerator_class->next_finish = g_network_service_address_enumerator_next_finish;
721 object_class->finalize = g_network_service_address_enumerator_finalize;
724 static GSocketAddressEnumerator *
725 g_network_service_connectable_enumerate (GSocketConnectable *connectable)
727 GNetworkServiceAddressEnumerator *srv_enum;
729 srv_enum = g_object_new (G_TYPE_NETWORK_SERVICE_ADDRESS_ENUMERATOR, NULL);
730 srv_enum->srv = g_object_ref (connectable);
731 srv_enum->resolver = g_resolver_get_default ();
732 srv_enum->use_proxy = FALSE;
734 return G_SOCKET_ADDRESS_ENUMERATOR (srv_enum);
737 static GSocketAddressEnumerator *
738 g_network_service_connectable_proxy_enumerate (GSocketConnectable *connectable)
740 GSocketAddressEnumerator *addr_enum;
741 GNetworkServiceAddressEnumerator *srv_enum;
743 addr_enum = g_network_service_connectable_enumerate (connectable);
744 srv_enum = G_NETWORK_SERVICE_ADDRESS_ENUMERATOR (addr_enum);
745 srv_enum->use_proxy = TRUE;