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.
6 * Copyright (C) 2018 Igalia S.L.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General
19 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
26 #include "gresolver.h"
27 #include "gnetworkingprivate.h"
28 #include "gasyncresult.h"
29 #include "ginetaddress.h"
31 #include "gsrvtarget.h"
32 #include "gthreadedresolver.h"
34 #include "gcancellable.h"
45 * @short_description: Asynchronous and cancellable DNS resolver
48 * #GResolver provides cancellable synchronous and asynchronous DNS
49 * resolution, for hostnames (g_resolver_lookup_by_address(),
50 * g_resolver_lookup_by_name() and their async variants) and SRV
51 * (service) records (g_resolver_lookup_service()).
53 * #GNetworkAddress and #GNetworkService provide wrappers around
54 * #GResolver functionality that also implement #GSocketConnectable,
55 * making it easy to connect to a remote host/service.
63 static guint signals[LAST_SIGNAL] = { 0 };
65 struct _GResolverPrivate {
67 time_t resolv_conf_timestamp;
76 * The object that handles DNS resolution. Use g_resolver_get_default()
77 * to get the default resolver.
79 * This is an abstract type; subclasses of it implement different resolvers for
80 * different platforms and situations.
82 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GResolver, g_resolver, G_TYPE_OBJECT,
83 G_ADD_PRIVATE (GResolver)
84 g_networking_init ();)
87 srv_records_to_targets (GList *records)
89 const gchar *hostname;
90 guint16 port, priority, weight;
94 for (l = records; l != NULL; l = g_list_next (l))
96 g_variant_get (l->data, "(qqq&s)", &priority, &weight, &port, &hostname);
97 target = g_srv_target_new (hostname, port, priority, weight);
98 g_variant_unref (l->data);
102 return g_srv_target_list_sort (records);
106 g_resolver_real_lookup_service (GResolver *resolver,
108 GCancellable *cancellable,
113 records = G_RESOLVER_GET_CLASS (resolver)->lookup_records (resolver,
115 G_RESOLVER_RECORD_SRV,
119 return srv_records_to_targets (records);
123 g_resolver_real_lookup_service_async (GResolver *resolver,
125 GCancellable *cancellable,
126 GAsyncReadyCallback callback,
129 G_RESOLVER_GET_CLASS (resolver)->lookup_records_async (resolver,
131 G_RESOLVER_RECORD_SRV,
138 g_resolver_real_lookup_service_finish (GResolver *resolver,
139 GAsyncResult *result,
144 records = G_RESOLVER_GET_CLASS (resolver)->lookup_records_finish (resolver,
148 return srv_records_to_targets (records);
152 g_resolver_class_init (GResolverClass *resolver_class)
154 /* Automatically pass these over to the lookup_records methods */
155 resolver_class->lookup_service = g_resolver_real_lookup_service;
156 resolver_class->lookup_service_async = g_resolver_real_lookup_service_async;
157 resolver_class->lookup_service_finish = g_resolver_real_lookup_service_finish;
161 * @resolver: a #GResolver
163 * Emitted when the resolver notices that the system resolver
164 * configuration has changed.
167 g_signal_new (I_("reload"),
170 G_STRUCT_OFFSET (GResolverClass, reload),
177 g_resolver_init (GResolver *resolver)
183 resolver->priv = g_resolver_get_instance_private (resolver);
186 if (stat (_PATH_RESCONF, &st) == 0)
187 resolver->priv->resolv_conf_timestamp = st.st_mtime;
191 G_LOCK_DEFINE_STATIC (default_resolver);
192 static GResolver *default_resolver;
195 * g_resolver_get_default:
197 * Gets the default #GResolver. You should unref it when you are done
198 * with it. #GResolver may use its reference count as a hint about how
199 * many threads it should allocate for concurrent DNS resolutions.
201 * Returns: (transfer full): the default #GResolver.
206 g_resolver_get_default (void)
210 G_LOCK (default_resolver);
211 if (!default_resolver)
212 default_resolver = g_object_new (G_TYPE_THREADED_RESOLVER, NULL);
213 ret = g_object_ref (default_resolver);
214 G_UNLOCK (default_resolver);
220 * g_resolver_set_default:
221 * @resolver: the new default #GResolver
223 * Sets @resolver to be the application's default resolver (reffing
224 * @resolver, and unreffing the previous default resolver, if any).
225 * Future calls to g_resolver_get_default() will return this resolver.
227 * This can be used if an application wants to perform any sort of DNS
228 * caching or "pinning"; it can implement its own #GResolver that
229 * calls the original default resolver for DNS operations, and
230 * implements its own cache policies on top of that, and then set
231 * itself as the default resolver for all later code to use.
236 g_resolver_set_default (GResolver *resolver)
238 G_LOCK (default_resolver);
239 if (default_resolver)
240 g_object_unref (default_resolver);
241 default_resolver = g_object_ref (resolver);
242 G_UNLOCK (default_resolver);
245 /* Bionic has res_init() but it's not in any header */
251 g_resolver_maybe_reload (GResolver *resolver)
256 if (stat (_PATH_RESCONF, &st) == 0)
258 if (st.st_mtime != resolver->priv->resolv_conf_timestamp)
260 resolver->priv->resolv_conf_timestamp = st.st_mtime;
264 g_signal_emit (resolver, signals[RELOAD], 0);
270 /* filter out duplicates, cf. https://bugzilla.gnome.org/show_bug.cgi?id=631379 */
272 remove_duplicates (GList *addrs)
278 /* TODO: if this is too slow (it's O(n^2) but n is typically really
279 * small), we can do something more clever but note that we must not
280 * change the order of elements...
282 for (l = addrs; l != NULL; l = l->next)
284 GInetAddress *address = G_INET_ADDRESS (l->data);
285 for (ll = l->next; ll != NULL; ll = lll)
287 GInetAddress *other_address = G_INET_ADDRESS (ll->data);
289 if (g_inet_address_equal (address, other_address))
291 g_object_unref (other_address);
292 /* we never return the first element */
293 g_warn_if_fail (g_list_delete_link (addrs, ll) == addrs);
300 hostname_is_localhost (const char *hostname)
302 size_t len = strlen (hostname);
305 /* Match "localhost", "localhost.", "*.localhost" and "*.localhost." */
306 if (len < strlen ("localhost"))
309 if (hostname[len - 1] == '.')
312 /* Scan backwards in @hostname to find the right-most dot (excluding the final dot, if it exists, as it was chopped off above).
313 * We can’t use strrchr() because because we need to operate with string lengths.
314 * End with @p pointing to the character after the right-most dot. */
315 p = hostname + len - 1;
316 while (p >= hostname)
323 else if (p == hostname)
330 return g_ascii_strncasecmp (p, "localhost", MAX (len, strlen ("localhost"))) == 0;
333 /* Note that this does not follow the "FALSE means @error is set"
334 * convention. The return value tells the caller whether it should
335 * return @addrs and @error to the caller right away, or if it should
336 * continue and trying to resolve the name as a hostname.
339 handle_ip_address_or_localhost (const char *hostname,
341 GResolverNameLookupFlags flags,
347 struct in_addr ip4addr;
350 addr = g_inet_address_new_from_string (hostname);
353 *addrs = g_list_append (NULL, addr);
361 /* Reject IPv6 addresses that have brackets ('[' or ']') and/or port numbers,
362 * as no valid addresses should contain these at this point.
363 * Non-standard IPv4 addresses would be rejected during the call to
364 * getaddrinfo() later.
366 if (strrchr (hostname, '[') != NULL ||
367 strrchr (hostname, ']') != NULL)
370 /* Reject non-standard IPv4 numbers-and-dots addresses.
371 * g_inet_address_new_from_string() will have accepted any "real" IP
372 * address, so if inet_aton() succeeds, then it's an address we want
375 if (inet_aton (hostname, &ip4addr))
379 gchar *error_message = g_win32_error_message (WSAHOST_NOT_FOUND);
381 gchar *error_message = g_locale_to_utf8 (gai_strerror (EAI_NONAME), -1, NULL, NULL, NULL);
382 if (error_message == NULL)
383 error_message = g_strdup ("[Invalid UTF-8]");
385 g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_NOT_FOUND,
386 _("Error resolving “%s”: %s"),
387 hostname, error_message);
388 g_free (error_message);
393 /* Always resolve localhost to a loopback address so it can be reliably considered secure.
394 This behavior is being adopted by browsers:
395 - https://w3c.github.io/webappsec-secure-contexts/
396 - https://groups.google.com/a/chromium.org/forum/#!msg/blink-dev/RC9dSw-O3fE/E3_0XaT0BAAJ
397 - https://chromium.googlesource.com/chromium/src.git/+/8da2a80724a9b896890602ff77ef2216cb951399
398 - https://bugs.webkit.org/show_bug.cgi?id=171934
399 - https://tools.ietf.org/html/draft-west-let-localhost-be-localhost-06
401 if (hostname_is_localhost (hostname))
403 if (flags & G_RESOLVER_NAME_LOOKUP_FLAGS_IPV6_ONLY)
404 *addrs = g_list_append (*addrs, g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV6));
405 if (flags & G_RESOLVER_NAME_LOOKUP_FLAGS_IPV4_ONLY)
406 *addrs = g_list_append (*addrs, g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4));
409 *addrs = g_list_append (*addrs, g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV6));
410 *addrs = g_list_append (*addrs, g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4));
419 lookup_by_name_real (GResolver *resolver,
420 const gchar *hostname,
421 GResolverNameLookupFlags flags,
422 GCancellable *cancellable,
426 gchar *ascii_hostname = NULL;
428 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
429 g_return_val_if_fail (hostname != NULL, NULL);
430 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
431 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
433 /* Check if @hostname is just an IP address */
434 if (handle_ip_address_or_localhost (hostname, &addrs, flags, error))
437 if (g_hostname_is_non_ascii (hostname))
438 hostname = ascii_hostname = g_hostname_to_ascii (hostname);
442 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
443 _("Invalid hostname"));
447 g_resolver_maybe_reload (resolver);
449 if (flags != G_RESOLVER_NAME_LOOKUP_FLAGS_DEFAULT)
451 if (!G_RESOLVER_GET_CLASS (resolver)->lookup_by_name_with_flags)
453 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
454 /* Translators: The placeholder is for a function name. */
455 _("%s not implemented"), "lookup_by_name_with_flags");
456 g_free (ascii_hostname);
459 addrs = G_RESOLVER_GET_CLASS (resolver)->
460 lookup_by_name_with_flags (resolver, hostname, flags, cancellable, error);
463 addrs = G_RESOLVER_GET_CLASS (resolver)->
464 lookup_by_name (resolver, hostname, cancellable, error);
466 remove_duplicates (addrs);
468 g_free (ascii_hostname);
473 * g_resolver_lookup_by_name:
474 * @resolver: a #GResolver
475 * @hostname: the hostname to look up
476 * @cancellable: (nullable): a #GCancellable, or %NULL
477 * @error: return location for a #GError, or %NULL
479 * Synchronously resolves @hostname to determine its associated IP
480 * address(es). @hostname may be an ASCII-only or UTF-8 hostname, or
481 * the textual form of an IP address (in which case this just becomes
482 * a wrapper around g_inet_address_new_from_string()).
484 * On success, g_resolver_lookup_by_name() will return a non-empty #GList of
485 * #GInetAddress, sorted in order of preference and guaranteed to not
486 * contain duplicates. That is, if using the result to connect to
487 * @hostname, you should attempt to connect to the first address
488 * first, then the second if the first fails, etc. If you are using
489 * the result to listen on a socket, it is appropriate to add each
490 * result using e.g. g_socket_listener_add_address().
492 * If the DNS resolution fails, @error (if non-%NULL) will be set to a
493 * value from #GResolverError and %NULL will be returned.
495 * If @cancellable is non-%NULL, it can be used to cancel the
496 * operation, in which case @error (if non-%NULL) will be set to
497 * %G_IO_ERROR_CANCELLED.
499 * If you are planning to connect to a socket on the resolved IP
500 * address, it may be easier to create a #GNetworkAddress and use its
501 * #GSocketConnectable interface.
503 * Returns: (element-type GInetAddress) (transfer full): a non-empty #GList
504 * of #GInetAddress, or %NULL on error. You
505 * must unref each of the addresses and free the list when you are
506 * done with it. (You can use g_resolver_free_addresses() to do this.)
511 g_resolver_lookup_by_name (GResolver *resolver,
512 const gchar *hostname,
513 GCancellable *cancellable,
516 return lookup_by_name_real (resolver,
518 G_RESOLVER_NAME_LOOKUP_FLAGS_DEFAULT,
524 * g_resolver_lookup_by_name_with_flags:
525 * @resolver: a #GResolver
526 * @hostname: the hostname to look up
527 * @flags: extra #GResolverNameLookupFlags for the lookup
528 * @cancellable: (nullable): a #GCancellable, or %NULL
529 * @error: (nullable): return location for a #GError, or %NULL
531 * This differs from g_resolver_lookup_by_name() in that you can modify
532 * the lookup behavior with @flags. For example this can be used to limit
533 * results with #G_RESOLVER_NAME_LOOKUP_FLAGS_IPV4_ONLY.
535 * Returns: (element-type GInetAddress) (transfer full): a non-empty #GList
536 * of #GInetAddress, or %NULL on error. You
537 * must unref each of the addresses and free the list when you are
538 * done with it. (You can use g_resolver_free_addresses() to do this.)
543 g_resolver_lookup_by_name_with_flags (GResolver *resolver,
544 const gchar *hostname,
545 GResolverNameLookupFlags flags,
546 GCancellable *cancellable,
549 return lookup_by_name_real (resolver,
557 lookup_by_name_async_real (GResolver *resolver,
558 const gchar *hostname,
559 GResolverNameLookupFlags flags,
560 GCancellable *cancellable,
561 GAsyncReadyCallback callback,
564 gchar *ascii_hostname = NULL;
566 GError *error = NULL;
568 g_return_if_fail (G_IS_RESOLVER (resolver));
569 g_return_if_fail (hostname != NULL);
570 g_return_if_fail (!(flags & G_RESOLVER_NAME_LOOKUP_FLAGS_IPV4_ONLY && flags & G_RESOLVER_NAME_LOOKUP_FLAGS_IPV6_ONLY));
572 /* Check if @hostname is just an IP address */
573 if (handle_ip_address_or_localhost (hostname, &addrs, flags, &error))
577 task = g_task_new (resolver, cancellable, callback, user_data);
578 g_task_set_source_tag (task, lookup_by_name_async_real);
580 g_task_return_pointer (task, addrs, (GDestroyNotify) g_resolver_free_addresses);
582 g_task_return_error (task, error);
583 g_object_unref (task);
587 if (g_hostname_is_non_ascii (hostname))
588 hostname = ascii_hostname = g_hostname_to_ascii (hostname);
594 g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_FAILED,
595 _("Invalid hostname"));
596 task = g_task_new (resolver, cancellable, callback, user_data);
597 g_task_set_source_tag (task, lookup_by_name_async_real);
598 g_task_return_error (task, error);
599 g_object_unref (task);
603 g_resolver_maybe_reload (resolver);
605 if (flags != G_RESOLVER_NAME_LOOKUP_FLAGS_DEFAULT)
607 if (G_RESOLVER_GET_CLASS (resolver)->lookup_by_name_with_flags_async == NULL)
611 g_set_error (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
612 /* Translators: The placeholder is for a function name. */
613 _("%s not implemented"), "lookup_by_name_with_flags_async");
614 task = g_task_new (resolver, cancellable, callback, user_data);
615 g_task_set_source_tag (task, lookup_by_name_async_real);
616 g_task_return_error (task, error);
617 g_object_unref (task);
620 G_RESOLVER_GET_CLASS (resolver)->
621 lookup_by_name_with_flags_async (resolver, hostname, flags, cancellable, callback, user_data);
624 G_RESOLVER_GET_CLASS (resolver)->
625 lookup_by_name_async (resolver, hostname, cancellable, callback, user_data);
627 g_free (ascii_hostname);
631 lookup_by_name_finish_real (GResolver *resolver,
632 GAsyncResult *result,
638 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
639 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
640 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
642 if (g_async_result_legacy_propagate_error (result, error))
644 else if (g_async_result_is_tagged (result, lookup_by_name_async_real))
646 /* Handle the stringified-IP-addr case */
647 return g_task_propagate_pointer (G_TASK (result), error);
652 g_assert (G_RESOLVER_GET_CLASS (resolver)->lookup_by_name_with_flags_finish != NULL);
653 addrs = G_RESOLVER_GET_CLASS (resolver)->
654 lookup_by_name_with_flags_finish (resolver, result, error);
657 addrs = G_RESOLVER_GET_CLASS (resolver)->
658 lookup_by_name_finish (resolver, result, error);
660 remove_duplicates (addrs);
666 * g_resolver_lookup_by_name_with_flags_async:
667 * @resolver: a #GResolver
668 * @hostname: the hostname to look up the address of
669 * @flags: extra #GResolverNameLookupFlags for the lookup
670 * @cancellable: (nullable): a #GCancellable, or %NULL
671 * @callback: (scope async): callback to call after resolution completes
672 * @user_data: (closure): data for @callback
674 * Begins asynchronously resolving @hostname to determine its
675 * associated IP address(es), and eventually calls @callback, which
676 * must call g_resolver_lookup_by_name_with_flags_finish() to get the result.
677 * See g_resolver_lookup_by_name() for more details.
682 g_resolver_lookup_by_name_with_flags_async (GResolver *resolver,
683 const gchar *hostname,
684 GResolverNameLookupFlags flags,
685 GCancellable *cancellable,
686 GAsyncReadyCallback callback,
689 lookup_by_name_async_real (resolver,
698 * g_resolver_lookup_by_name_async:
699 * @resolver: a #GResolver
700 * @hostname: the hostname to look up the address of
701 * @cancellable: (nullable): a #GCancellable, or %NULL
702 * @callback: (scope async): callback to call after resolution completes
703 * @user_data: (closure): data for @callback
705 * Begins asynchronously resolving @hostname to determine its
706 * associated IP address(es), and eventually calls @callback, which
707 * must call g_resolver_lookup_by_name_finish() to get the result.
708 * See g_resolver_lookup_by_name() for more details.
713 g_resolver_lookup_by_name_async (GResolver *resolver,
714 const gchar *hostname,
715 GCancellable *cancellable,
716 GAsyncReadyCallback callback,
719 lookup_by_name_async_real (resolver,
728 * g_resolver_lookup_by_name_finish:
729 * @resolver: a #GResolver
730 * @result: the result passed to your #GAsyncReadyCallback
731 * @error: return location for a #GError, or %NULL
733 * Retrieves the result of a call to
734 * g_resolver_lookup_by_name_async().
736 * If the DNS resolution failed, @error (if non-%NULL) will be set to
737 * a value from #GResolverError. If the operation was cancelled,
738 * @error will be set to %G_IO_ERROR_CANCELLED.
740 * Returns: (element-type GInetAddress) (transfer full): a #GList
741 * of #GInetAddress, or %NULL on error. See g_resolver_lookup_by_name()
747 g_resolver_lookup_by_name_finish (GResolver *resolver,
748 GAsyncResult *result,
751 return lookup_by_name_finish_real (resolver,
758 * g_resolver_lookup_by_name_with_flags_finish:
759 * @resolver: a #GResolver
760 * @result: the result passed to your #GAsyncReadyCallback
761 * @error: return location for a #GError, or %NULL
763 * Retrieves the result of a call to
764 * g_resolver_lookup_by_name_with_flags_async().
766 * If the DNS resolution failed, @error (if non-%NULL) will be set to
767 * a value from #GResolverError. If the operation was cancelled,
768 * @error will be set to %G_IO_ERROR_CANCELLED.
770 * Returns: (element-type GInetAddress) (transfer full): a #GList
771 * of #GInetAddress, or %NULL on error. See g_resolver_lookup_by_name()
777 g_resolver_lookup_by_name_with_flags_finish (GResolver *resolver,
778 GAsyncResult *result,
781 return lookup_by_name_finish_real (resolver,
788 * g_resolver_free_addresses: (skip)
789 * @addresses: a #GList of #GInetAddress
791 * Frees @addresses (which should be the return value from
792 * g_resolver_lookup_by_name() or g_resolver_lookup_by_name_finish()).
793 * (This is a convenience method; you can also simply free the results
799 g_resolver_free_addresses (GList *addresses)
803 for (a = addresses; a; a = a->next)
804 g_object_unref (a->data);
805 g_list_free (addresses);
809 * g_resolver_lookup_by_address:
810 * @resolver: a #GResolver
811 * @address: the address to reverse-resolve
812 * @cancellable: (nullable): a #GCancellable, or %NULL
813 * @error: return location for a #GError, or %NULL
815 * Synchronously reverse-resolves @address to determine its
816 * associated hostname.
818 * If the DNS resolution fails, @error (if non-%NULL) will be set to
819 * a value from #GResolverError.
821 * If @cancellable is non-%NULL, it can be used to cancel the
822 * operation, in which case @error (if non-%NULL) will be set to
823 * %G_IO_ERROR_CANCELLED.
825 * Returns: a hostname (either ASCII-only, or in ASCII-encoded
826 * form), or %NULL on error.
831 g_resolver_lookup_by_address (GResolver *resolver,
832 GInetAddress *address,
833 GCancellable *cancellable,
836 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
837 g_return_val_if_fail (G_IS_INET_ADDRESS (address), NULL);
839 g_resolver_maybe_reload (resolver);
840 return G_RESOLVER_GET_CLASS (resolver)->
841 lookup_by_address (resolver, address, cancellable, error);
845 * g_resolver_lookup_by_address_async:
846 * @resolver: a #GResolver
847 * @address: the address to reverse-resolve
848 * @cancellable: (nullable): a #GCancellable, or %NULL
849 * @callback: (scope async): callback to call after resolution completes
850 * @user_data: (closure): data for @callback
852 * Begins asynchronously reverse-resolving @address to determine its
853 * associated hostname, and eventually calls @callback, which must
854 * call g_resolver_lookup_by_address_finish() to get the final result.
859 g_resolver_lookup_by_address_async (GResolver *resolver,
860 GInetAddress *address,
861 GCancellable *cancellable,
862 GAsyncReadyCallback callback,
865 g_return_if_fail (G_IS_RESOLVER (resolver));
866 g_return_if_fail (G_IS_INET_ADDRESS (address));
868 g_resolver_maybe_reload (resolver);
869 G_RESOLVER_GET_CLASS (resolver)->
870 lookup_by_address_async (resolver, address, cancellable, callback, user_data);
874 * g_resolver_lookup_by_address_finish:
875 * @resolver: a #GResolver
876 * @result: the result passed to your #GAsyncReadyCallback
877 * @error: return location for a #GError, or %NULL
879 * Retrieves the result of a previous call to
880 * g_resolver_lookup_by_address_async().
882 * If the DNS resolution failed, @error (if non-%NULL) will be set to
883 * a value from #GResolverError. If the operation was cancelled,
884 * @error will be set to %G_IO_ERROR_CANCELLED.
886 * Returns: a hostname (either ASCII-only, or in ASCII-encoded
887 * form), or %NULL on error.
892 g_resolver_lookup_by_address_finish (GResolver *resolver,
893 GAsyncResult *result,
896 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
898 if (g_async_result_legacy_propagate_error (result, error))
901 return G_RESOLVER_GET_CLASS (resolver)->
902 lookup_by_address_finish (resolver, result, error);
906 g_resolver_get_service_rrname (const char *service,
907 const char *protocol,
910 gchar *rrname, *ascii_domain = NULL;
912 if (g_hostname_is_non_ascii (domain))
913 domain = ascii_domain = g_hostname_to_ascii (domain);
917 rrname = g_strdup_printf ("_%s._%s.%s", service, protocol, domain);
919 g_free (ascii_domain);
924 * g_resolver_lookup_service:
925 * @resolver: a #GResolver
926 * @service: the service type to look up (eg, "ldap")
927 * @protocol: the networking protocol to use for @service (eg, "tcp")
928 * @domain: the DNS domain to look up the service in
929 * @cancellable: (nullable): a #GCancellable, or %NULL
930 * @error: return location for a #GError, or %NULL
932 * Synchronously performs a DNS SRV lookup for the given @service and
933 * @protocol in the given @domain and returns an array of #GSrvTarget.
934 * @domain may be an ASCII-only or UTF-8 hostname. Note also that the
935 * @service and @protocol arguments do not include the leading underscore
936 * that appears in the actual DNS entry.
938 * On success, g_resolver_lookup_service() will return a non-empty #GList of
939 * #GSrvTarget, sorted in order of preference. (That is, you should
940 * attempt to connect to the first target first, then the second if
941 * the first fails, etc.)
943 * If the DNS resolution fails, @error (if non-%NULL) will be set to
944 * a value from #GResolverError and %NULL will be returned.
946 * If @cancellable is non-%NULL, it can be used to cancel the
947 * operation, in which case @error (if non-%NULL) will be set to
948 * %G_IO_ERROR_CANCELLED.
950 * If you are planning to connect to the service, it is usually easier
951 * to create a #GNetworkService and use its #GSocketConnectable
954 * Returns: (element-type GSrvTarget) (transfer full): a non-empty #GList of
955 * #GSrvTarget, or %NULL on error. You must free each of the targets and the
956 * list when you are done with it. (You can use g_resolver_free_targets() to do
962 g_resolver_lookup_service (GResolver *resolver,
963 const gchar *service,
964 const gchar *protocol,
966 GCancellable *cancellable,
972 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
973 g_return_val_if_fail (service != NULL, NULL);
974 g_return_val_if_fail (protocol != NULL, NULL);
975 g_return_val_if_fail (domain != NULL, NULL);
977 rrname = g_resolver_get_service_rrname (service, protocol, domain);
980 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
981 _("Invalid domain"));
985 g_resolver_maybe_reload (resolver);
986 targets = G_RESOLVER_GET_CLASS (resolver)->
987 lookup_service (resolver, rrname, cancellable, error);
994 * g_resolver_lookup_service_async:
995 * @resolver: a #GResolver
996 * @service: the service type to look up (eg, "ldap")
997 * @protocol: the networking protocol to use for @service (eg, "tcp")
998 * @domain: the DNS domain to look up the service in
999 * @cancellable: (nullable): a #GCancellable, or %NULL
1000 * @callback: (scope async): callback to call after resolution completes
1001 * @user_data: (closure): data for @callback
1003 * Begins asynchronously performing a DNS SRV lookup for the given
1004 * @service and @protocol in the given @domain, and eventually calls
1005 * @callback, which must call g_resolver_lookup_service_finish() to
1006 * get the final result. See g_resolver_lookup_service() for more
1012 g_resolver_lookup_service_async (GResolver *resolver,
1013 const gchar *service,
1014 const gchar *protocol,
1015 const gchar *domain,
1016 GCancellable *cancellable,
1017 GAsyncReadyCallback callback,
1022 g_return_if_fail (G_IS_RESOLVER (resolver));
1023 g_return_if_fail (service != NULL);
1024 g_return_if_fail (protocol != NULL);
1025 g_return_if_fail (domain != NULL);
1027 rrname = g_resolver_get_service_rrname (service, protocol, domain);
1030 g_task_report_new_error (resolver, callback, user_data,
1031 g_resolver_lookup_service_async,
1032 G_IO_ERROR, G_IO_ERROR_FAILED,
1033 _("Invalid domain"));
1037 g_resolver_maybe_reload (resolver);
1038 G_RESOLVER_GET_CLASS (resolver)->
1039 lookup_service_async (resolver, rrname, cancellable, callback, user_data);
1045 * g_resolver_lookup_service_finish:
1046 * @resolver: a #GResolver
1047 * @result: the result passed to your #GAsyncReadyCallback
1048 * @error: return location for a #GError, or %NULL
1050 * Retrieves the result of a previous call to
1051 * g_resolver_lookup_service_async().
1053 * If the DNS resolution failed, @error (if non-%NULL) will be set to
1054 * a value from #GResolverError. If the operation was cancelled,
1055 * @error will be set to %G_IO_ERROR_CANCELLED.
1057 * Returns: (element-type GSrvTarget) (transfer full): a non-empty #GList of
1058 * #GSrvTarget, or %NULL on error. See g_resolver_lookup_service() for more
1064 g_resolver_lookup_service_finish (GResolver *resolver,
1065 GAsyncResult *result,
1068 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
1070 if (g_async_result_legacy_propagate_error (result, error))
1073 return G_RESOLVER_GET_CLASS (resolver)->
1074 lookup_service_finish (resolver, result, error);
1078 * g_resolver_free_targets: (skip)
1079 * @targets: a #GList of #GSrvTarget
1081 * Frees @targets (which should be the return value from
1082 * g_resolver_lookup_service() or g_resolver_lookup_service_finish()).
1083 * (This is a convenience method; you can also simply free the
1089 g_resolver_free_targets (GList *targets)
1093 for (t = targets; t; t = t->next)
1094 g_srv_target_free (t->data);
1095 g_list_free (targets);
1099 * g_resolver_lookup_records:
1100 * @resolver: a #GResolver
1101 * @rrname: the DNS name to look up the record for
1102 * @record_type: the type of DNS record to look up
1103 * @cancellable: (nullable): a #GCancellable, or %NULL
1104 * @error: return location for a #GError, or %NULL
1106 * Synchronously performs a DNS record lookup for the given @rrname and returns
1107 * a list of records as #GVariant tuples. See #GResolverRecordType for
1108 * information on what the records contain for each @record_type.
1110 * If the DNS resolution fails, @error (if non-%NULL) will be set to
1111 * a value from #GResolverError and %NULL will be returned.
1113 * If @cancellable is non-%NULL, it can be used to cancel the
1114 * operation, in which case @error (if non-%NULL) will be set to
1115 * %G_IO_ERROR_CANCELLED.
1117 * Returns: (element-type GVariant) (transfer full): a non-empty #GList of
1118 * #GVariant, or %NULL on error. You must free each of the records and the list
1119 * when you are done with it. (You can use g_list_free_full() with
1120 * g_variant_unref() to do this.)
1125 g_resolver_lookup_records (GResolver *resolver,
1126 const gchar *rrname,
1127 GResolverRecordType record_type,
1128 GCancellable *cancellable,
1133 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
1134 g_return_val_if_fail (rrname != NULL, NULL);
1136 g_resolver_maybe_reload (resolver);
1137 records = G_RESOLVER_GET_CLASS (resolver)->
1138 lookup_records (resolver, rrname, record_type, cancellable, error);
1144 * g_resolver_lookup_records_async:
1145 * @resolver: a #GResolver
1146 * @rrname: the DNS name to look up the record for
1147 * @record_type: the type of DNS record to look up
1148 * @cancellable: (nullable): a #GCancellable, or %NULL
1149 * @callback: (scope async): callback to call after resolution completes
1150 * @user_data: (closure): data for @callback
1152 * Begins asynchronously performing a DNS lookup for the given
1153 * @rrname, and eventually calls @callback, which must call
1154 * g_resolver_lookup_records_finish() to get the final result. See
1155 * g_resolver_lookup_records() for more details.
1160 g_resolver_lookup_records_async (GResolver *resolver,
1161 const gchar *rrname,
1162 GResolverRecordType record_type,
1163 GCancellable *cancellable,
1164 GAsyncReadyCallback callback,
1167 g_return_if_fail (G_IS_RESOLVER (resolver));
1168 g_return_if_fail (rrname != NULL);
1170 g_resolver_maybe_reload (resolver);
1171 G_RESOLVER_GET_CLASS (resolver)->
1172 lookup_records_async (resolver, rrname, record_type, cancellable, callback, user_data);
1176 * g_resolver_lookup_records_finish:
1177 * @resolver: a #GResolver
1178 * @result: the result passed to your #GAsyncReadyCallback
1179 * @error: return location for a #GError, or %NULL
1181 * Retrieves the result of a previous call to
1182 * g_resolver_lookup_records_async(). Returns a non-empty list of records as
1183 * #GVariant tuples. See #GResolverRecordType for information on what the
1186 * If the DNS resolution failed, @error (if non-%NULL) will be set to
1187 * a value from #GResolverError. If the operation was cancelled,
1188 * @error will be set to %G_IO_ERROR_CANCELLED.
1190 * Returns: (element-type GVariant) (transfer full): a non-empty #GList of
1191 * #GVariant, or %NULL on error. You must free each of the records and the list
1192 * when you are done with it. (You can use g_list_free_full() with
1193 * g_variant_unref() to do this.)
1198 g_resolver_lookup_records_finish (GResolver *resolver,
1199 GAsyncResult *result,
1202 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
1203 return G_RESOLVER_GET_CLASS (resolver)->
1204 lookup_records_finish (resolver, result, error);
1208 g_resolver_get_serial (GResolver *resolver)
1210 g_return_val_if_fail (G_IS_RESOLVER (resolver), 0);
1212 g_resolver_maybe_reload (resolver);
1215 return (guint64) resolver->priv->resolv_conf_timestamp;
1222 * g_resolver_error_quark:
1224 * Gets the #GResolver Error Quark.
1226 * Returns: a #GQuark.
1230 G_DEFINE_QUARK (g-resolver-error-quark, g_resolver_error)