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 * SPDX-License-Identifier: LGPL-2.1-or-later
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General
21 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
28 #include "gresolver.h"
29 #include "gnetworkingprivate.h"
30 #include "gasyncresult.h"
31 #include "ginetaddress.h"
33 #include "gsrvtarget.h"
34 #include "gthreadedresolver.h"
36 #include "gcancellable.h"
47 * @short_description: Asynchronous and cancellable DNS resolver
50 * #GResolver provides cancellable synchronous and asynchronous DNS
51 * resolution, for hostnames (g_resolver_lookup_by_address(),
52 * g_resolver_lookup_by_name() and their async variants) and SRV
53 * (service) records (g_resolver_lookup_service()).
55 * #GNetworkAddress and #GNetworkService provide wrappers around
56 * #GResolver functionality that also implement #GSocketConnectable,
57 * making it easy to connect to a remote host/service.
59 * The default resolver (see g_resolver_get_default()) has a timeout of 30s set
60 * on it since GLib 2.78. Earlier versions of GLib did not support resolver
68 static GParamSpec *props[PROP_TIMEOUT + 1] = { NULL, };
75 static guint signals[LAST_SIGNAL] = { 0 };
77 struct _GResolverPrivate {
82 time_t resolv_conf_timestamp; /* protected by @mutex */
89 * The object that handles DNS resolution. Use g_resolver_get_default()
90 * to get the default resolver.
92 * This is an abstract type; subclasses of it implement different resolvers for
93 * different platforms and situations.
95 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GResolver, g_resolver, G_TYPE_OBJECT,
96 G_ADD_PRIVATE (GResolver)
97 g_networking_init ();)
100 srv_records_to_targets (GList *records)
102 const gchar *hostname;
103 guint16 port, priority, weight;
107 for (l = records; l != NULL; l = g_list_next (l))
109 g_variant_get (l->data, "(qqq&s)", &priority, &weight, &port, &hostname);
110 target = g_srv_target_new (hostname, port, priority, weight);
111 g_variant_unref (l->data);
115 return g_srv_target_list_sort (records);
119 g_resolver_real_lookup_service (GResolver *resolver,
121 GCancellable *cancellable,
126 records = G_RESOLVER_GET_CLASS (resolver)->lookup_records (resolver,
128 G_RESOLVER_RECORD_SRV,
132 return srv_records_to_targets (records);
136 g_resolver_real_lookup_service_async (GResolver *resolver,
138 GCancellable *cancellable,
139 GAsyncReadyCallback callback,
142 G_RESOLVER_GET_CLASS (resolver)->lookup_records_async (resolver,
144 G_RESOLVER_RECORD_SRV,
151 g_resolver_real_lookup_service_finish (GResolver *resolver,
152 GAsyncResult *result,
157 records = G_RESOLVER_GET_CLASS (resolver)->lookup_records_finish (resolver,
161 return srv_records_to_targets (records);
165 g_resolver_get_property (GObject *object,
170 GResolver *self = G_RESOLVER (object);
172 switch ((GResolverProperty) prop_id)
175 g_value_set_uint (value, g_resolver_get_timeout (self));
178 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
183 g_resolver_set_property (GObject *object,
188 GResolver *self = G_RESOLVER (object);
190 switch ((GResolverProperty) prop_id)
193 g_resolver_set_timeout (self, g_value_get_uint (value));
196 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
201 g_resolver_finalize (GObject *object)
204 GResolver *resolver = G_RESOLVER (object);
206 g_mutex_clear (&resolver->priv->mutex);
209 G_OBJECT_CLASS (g_resolver_parent_class)->finalize (object);
213 g_resolver_class_init (GResolverClass *resolver_class)
215 GObjectClass *object_class = G_OBJECT_CLASS (resolver_class);
217 object_class->get_property = g_resolver_get_property;
218 object_class->set_property = g_resolver_set_property;
219 object_class->finalize = g_resolver_finalize;
221 /* Automatically pass these over to the lookup_records methods */
222 resolver_class->lookup_service = g_resolver_real_lookup_service;
223 resolver_class->lookup_service_async = g_resolver_real_lookup_service_async;
224 resolver_class->lookup_service_finish = g_resolver_real_lookup_service_finish;
229 * The timeout applied to all resolver lookups, in milliseconds.
231 * This may be changed through the lifetime of the #GResolver. The new value
232 * will apply to any lookups started after the change, but not to any
233 * already-ongoing lookups.
235 * If this is `0`, no timeout is applied to lookups.
237 * No timeout was applied to lookups before this property was added in
242 props[PROP_TIMEOUT] =
243 g_param_spec_uint ("timeout",
245 P_("Timeout (ms) applied to all resolver lookups"),
247 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
249 g_object_class_install_properties (object_class, G_N_ELEMENTS (props), props);
253 * @resolver: a #GResolver
255 * Emitted when the resolver notices that the system resolver
256 * configuration has changed.
259 g_signal_new (I_("reload"),
262 G_STRUCT_OFFSET (GResolverClass, reload),
269 g_resolver_init (GResolver *resolver)
275 resolver->priv = g_resolver_get_instance_private (resolver);
278 if (stat (_PATH_RESCONF, &st) == 0)
279 resolver->priv->resolv_conf_timestamp = st.st_mtime;
281 g_mutex_init (&resolver->priv->mutex);
285 G_LOCK_DEFINE_STATIC (default_resolver);
286 static GResolver *default_resolver;
289 * g_resolver_get_default:
291 * Gets the default #GResolver. You should unref it when you are done
292 * with it. #GResolver may use its reference count as a hint about how
293 * many threads it should allocate for concurrent DNS resolutions.
295 * Returns: (transfer full): the default #GResolver.
300 g_resolver_get_default (void)
304 G_LOCK (default_resolver);
305 if (!default_resolver)
306 default_resolver = g_object_new (G_TYPE_THREADED_RESOLVER,
309 ret = g_object_ref (default_resolver);
310 G_UNLOCK (default_resolver);
316 * g_resolver_set_default:
317 * @resolver: the new default #GResolver
319 * Sets @resolver to be the application's default resolver (reffing
320 * @resolver, and unreffing the previous default resolver, if any).
321 * Future calls to g_resolver_get_default() will return this resolver.
323 * This can be used if an application wants to perform any sort of DNS
324 * caching or "pinning"; it can implement its own #GResolver that
325 * calls the original default resolver for DNS operations, and
326 * implements its own cache policies on top of that, and then set
327 * itself as the default resolver for all later code to use.
332 g_resolver_set_default (GResolver *resolver)
334 G_LOCK (default_resolver);
335 if (default_resolver)
336 g_object_unref (default_resolver);
337 default_resolver = g_object_ref (resolver);
338 G_UNLOCK (default_resolver);
342 maybe_emit_reload (GResolver *resolver)
347 if (stat (_PATH_RESCONF, &st) == 0)
349 g_mutex_lock (&resolver->priv->mutex);
350 if (st.st_mtime != resolver->priv->resolv_conf_timestamp)
352 resolver->priv->resolv_conf_timestamp = st.st_mtime;
353 g_mutex_unlock (&resolver->priv->mutex);
354 g_signal_emit (resolver, signals[RELOAD], 0);
357 g_mutex_unlock (&resolver->priv->mutex);
362 /* filter out duplicates, cf. https://bugzilla.gnome.org/show_bug.cgi?id=631379 */
364 remove_duplicates (GList *addrs)
370 /* TODO: if this is too slow (it's O(n^2) but n is typically really
371 * small), we can do something more clever but note that we must not
372 * change the order of elements...
374 for (l = addrs; l != NULL; l = l->next)
376 GInetAddress *address = G_INET_ADDRESS (l->data);
377 for (ll = l->next; ll != NULL; ll = lll)
379 GInetAddress *other_address = G_INET_ADDRESS (ll->data);
381 if (g_inet_address_equal (address, other_address))
383 g_object_unref (other_address);
384 /* we never return the first element */
385 g_warn_if_fail (g_list_delete_link (addrs, ll) == addrs);
392 hostname_is_localhost (const char *hostname)
394 size_t len = strlen (hostname);
397 /* Match "localhost", "localhost.", "*.localhost" and "*.localhost." */
398 if (len < strlen ("localhost"))
401 if (hostname[len - 1] == '.')
404 /* Scan backwards in @hostname to find the right-most dot (excluding the final dot, if it exists, as it was chopped off above).
405 * We can’t use strrchr() because because we need to operate with string lengths.
406 * End with @p pointing to the character after the right-most dot. */
407 p = hostname + len - 1;
408 while (p >= hostname)
415 else if (p == hostname)
422 return g_ascii_strncasecmp (p, "localhost", MAX (len, strlen ("localhost"))) == 0;
425 /* Note that this does not follow the "FALSE means @error is set"
426 * convention. The return value tells the caller whether it should
427 * return @addrs and @error to the caller right away, or if it should
428 * continue and trying to resolve the name as a hostname.
431 handle_ip_address_or_localhost (const char *hostname,
433 GResolverNameLookupFlags flags,
439 struct in_addr ip4addr;
442 addr = g_inet_address_new_from_string (hostname);
445 *addrs = g_list_append (NULL, addr);
453 /* Reject IPv6 addresses that have brackets ('[' or ']') and/or port numbers,
454 * as no valid addresses should contain these at this point.
455 * Non-standard IPv4 addresses would be rejected during the call to
456 * getaddrinfo() later.
458 if (strrchr (hostname, '[') != NULL ||
459 strrchr (hostname, ']') != NULL)
462 /* Reject non-standard IPv4 numbers-and-dots addresses.
463 * g_inet_address_new_from_string() will have accepted any "real" IP
464 * address, so if inet_aton() succeeds, then it's an address we want
467 if (inet_aton (hostname, &ip4addr))
471 gchar *error_message = g_win32_error_message (WSAHOST_NOT_FOUND);
473 gchar *error_message = g_locale_to_utf8 (gai_strerror (EAI_NONAME), -1, NULL, NULL, NULL);
474 if (error_message == NULL)
475 error_message = g_strdup ("[Invalid UTF-8]");
477 g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_NOT_FOUND,
478 _("Error resolving “%s”: %s"),
479 hostname, error_message);
480 g_free (error_message);
485 /* Always resolve localhost to a loopback address so it can be reliably considered secure.
486 This behavior is being adopted by browsers:
487 - https://w3c.github.io/webappsec-secure-contexts/
488 - https://groups.google.com/a/chromium.org/forum/#!msg/blink-dev/RC9dSw-O3fE/E3_0XaT0BAAJ
489 - https://chromium.googlesource.com/chromium/src.git/+/8da2a80724a9b896890602ff77ef2216cb951399
490 - https://bugs.webkit.org/show_bug.cgi?id=171934
491 - https://tools.ietf.org/html/draft-west-let-localhost-be-localhost-06
493 if (hostname_is_localhost (hostname))
495 if (flags & G_RESOLVER_NAME_LOOKUP_FLAGS_IPV6_ONLY)
496 *addrs = g_list_append (*addrs, g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV6));
497 if (flags & G_RESOLVER_NAME_LOOKUP_FLAGS_IPV4_ONLY)
498 *addrs = g_list_append (*addrs, g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4));
501 *addrs = g_list_append (*addrs, g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV6));
502 *addrs = g_list_append (*addrs, g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4));
511 lookup_by_name_real (GResolver *resolver,
512 const gchar *hostname,
513 GResolverNameLookupFlags flags,
514 GCancellable *cancellable,
518 gchar *ascii_hostname = NULL;
520 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
521 g_return_val_if_fail (hostname != NULL, NULL);
522 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
523 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
525 /* Check if @hostname is just an IP address */
526 if (handle_ip_address_or_localhost (hostname, &addrs, flags, error))
529 if (g_hostname_is_non_ascii (hostname))
530 hostname = ascii_hostname = g_hostname_to_ascii (hostname);
534 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
535 _("Invalid hostname"));
539 maybe_emit_reload (resolver);
541 if (flags != G_RESOLVER_NAME_LOOKUP_FLAGS_DEFAULT)
543 if (!G_RESOLVER_GET_CLASS (resolver)->lookup_by_name_with_flags)
545 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
546 /* Translators: The placeholder is for a function name. */
547 _("%s not implemented"), "lookup_by_name_with_flags");
548 g_free (ascii_hostname);
551 addrs = G_RESOLVER_GET_CLASS (resolver)->
552 lookup_by_name_with_flags (resolver, hostname, flags, cancellable, error);
555 addrs = G_RESOLVER_GET_CLASS (resolver)->
556 lookup_by_name (resolver, hostname, cancellable, error);
558 remove_duplicates (addrs);
560 g_free (ascii_hostname);
565 * g_resolver_lookup_by_name:
566 * @resolver: a #GResolver
567 * @hostname: the hostname to look up
568 * @cancellable: (nullable): a #GCancellable, or %NULL
569 * @error: return location for a #GError, or %NULL
571 * Synchronously resolves @hostname to determine its associated IP
572 * address(es). @hostname may be an ASCII-only or UTF-8 hostname, or
573 * the textual form of an IP address (in which case this just becomes
574 * a wrapper around g_inet_address_new_from_string()).
576 * On success, g_resolver_lookup_by_name() will return a non-empty #GList of
577 * #GInetAddress, sorted in order of preference and guaranteed to not
578 * contain duplicates. That is, if using the result to connect to
579 * @hostname, you should attempt to connect to the first address
580 * first, then the second if the first fails, etc. If you are using
581 * the result to listen on a socket, it is appropriate to add each
582 * result using e.g. g_socket_listener_add_address().
584 * If the DNS resolution fails, @error (if non-%NULL) will be set to a
585 * value from #GResolverError and %NULL will be returned.
587 * If @cancellable is non-%NULL, it can be used to cancel the
588 * operation, in which case @error (if non-%NULL) will be set to
589 * %G_IO_ERROR_CANCELLED.
591 * If you are planning to connect to a socket on the resolved IP
592 * address, it may be easier to create a #GNetworkAddress and use its
593 * #GSocketConnectable interface.
595 * Returns: (element-type GInetAddress) (transfer full): a non-empty #GList
596 * of #GInetAddress, or %NULL on error. You
597 * must unref each of the addresses and free the list when you are
598 * done with it. (You can use g_resolver_free_addresses() to do this.)
603 g_resolver_lookup_by_name (GResolver *resolver,
604 const gchar *hostname,
605 GCancellable *cancellable,
608 return lookup_by_name_real (resolver,
610 G_RESOLVER_NAME_LOOKUP_FLAGS_DEFAULT,
616 * g_resolver_lookup_by_name_with_flags:
617 * @resolver: a #GResolver
618 * @hostname: the hostname to look up
619 * @flags: extra #GResolverNameLookupFlags for the lookup
620 * @cancellable: (nullable): a #GCancellable, or %NULL
621 * @error: (nullable): return location for a #GError, or %NULL
623 * This differs from g_resolver_lookup_by_name() in that you can modify
624 * the lookup behavior with @flags. For example this can be used to limit
625 * results with %G_RESOLVER_NAME_LOOKUP_FLAGS_IPV4_ONLY.
627 * Returns: (element-type GInetAddress) (transfer full): a non-empty #GList
628 * of #GInetAddress, or %NULL on error. You
629 * must unref each of the addresses and free the list when you are
630 * done with it. (You can use g_resolver_free_addresses() to do this.)
635 g_resolver_lookup_by_name_with_flags (GResolver *resolver,
636 const gchar *hostname,
637 GResolverNameLookupFlags flags,
638 GCancellable *cancellable,
641 return lookup_by_name_real (resolver,
649 lookup_by_name_async_real (GResolver *resolver,
650 const gchar *hostname,
651 GResolverNameLookupFlags flags,
652 GCancellable *cancellable,
653 GAsyncReadyCallback callback,
656 gchar *ascii_hostname = NULL;
658 GError *error = NULL;
660 g_return_if_fail (G_IS_RESOLVER (resolver));
661 g_return_if_fail (hostname != NULL);
662 g_return_if_fail (!(flags & G_RESOLVER_NAME_LOOKUP_FLAGS_IPV4_ONLY && flags & G_RESOLVER_NAME_LOOKUP_FLAGS_IPV6_ONLY));
664 /* Check if @hostname is just an IP address */
665 if (handle_ip_address_or_localhost (hostname, &addrs, flags, &error))
669 task = g_task_new (resolver, cancellable, callback, user_data);
670 g_task_set_source_tag (task, lookup_by_name_async_real);
671 g_task_set_name (task, "[gio] resolver lookup");
673 g_task_return_pointer (task, addrs, (GDestroyNotify) g_resolver_free_addresses);
675 g_task_return_error (task, error);
676 g_object_unref (task);
680 if (g_hostname_is_non_ascii (hostname))
681 hostname = ascii_hostname = g_hostname_to_ascii (hostname);
687 g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_FAILED,
688 _("Invalid hostname"));
689 task = g_task_new (resolver, cancellable, callback, user_data);
690 g_task_set_source_tag (task, lookup_by_name_async_real);
691 g_task_set_name (task, "[gio] resolver lookup");
692 g_task_return_error (task, error);
693 g_object_unref (task);
697 maybe_emit_reload (resolver);
699 if (flags != G_RESOLVER_NAME_LOOKUP_FLAGS_DEFAULT)
701 if (G_RESOLVER_GET_CLASS (resolver)->lookup_by_name_with_flags_async == NULL)
705 g_set_error (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
706 /* Translators: The placeholder is for a function name. */
707 _("%s not implemented"), "lookup_by_name_with_flags_async");
708 task = g_task_new (resolver, cancellable, callback, user_data);
709 g_task_set_source_tag (task, lookup_by_name_async_real);
710 g_task_set_name (task, "[gio] resolver lookup");
711 g_task_return_error (task, error);
712 g_object_unref (task);
715 G_RESOLVER_GET_CLASS (resolver)->
716 lookup_by_name_with_flags_async (resolver, hostname, flags, cancellable, callback, user_data);
719 G_RESOLVER_GET_CLASS (resolver)->
720 lookup_by_name_async (resolver, hostname, cancellable, callback, user_data);
722 g_free (ascii_hostname);
726 lookup_by_name_finish_real (GResolver *resolver,
727 GAsyncResult *result,
733 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
734 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
735 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
737 if (g_async_result_legacy_propagate_error (result, error))
739 else if (g_async_result_is_tagged (result, lookup_by_name_async_real))
741 /* Handle the stringified-IP-addr case */
742 return g_task_propagate_pointer (G_TASK (result), error);
747 g_assert (G_RESOLVER_GET_CLASS (resolver)->lookup_by_name_with_flags_finish != NULL);
748 addrs = G_RESOLVER_GET_CLASS (resolver)->
749 lookup_by_name_with_flags_finish (resolver, result, error);
752 addrs = G_RESOLVER_GET_CLASS (resolver)->
753 lookup_by_name_finish (resolver, result, error);
755 remove_duplicates (addrs);
761 * g_resolver_lookup_by_name_with_flags_async:
762 * @resolver: a #GResolver
763 * @hostname: the hostname to look up the address of
764 * @flags: extra #GResolverNameLookupFlags for the lookup
765 * @cancellable: (nullable): a #GCancellable, or %NULL
766 * @callback: (scope async) (closure user_data): callback to call after resolution completes
767 * @user_data: data for @callback
769 * Begins asynchronously resolving @hostname to determine its
770 * associated IP address(es), and eventually calls @callback, which
771 * must call g_resolver_lookup_by_name_with_flags_finish() to get the result.
772 * See g_resolver_lookup_by_name() for more details.
777 g_resolver_lookup_by_name_with_flags_async (GResolver *resolver,
778 const gchar *hostname,
779 GResolverNameLookupFlags flags,
780 GCancellable *cancellable,
781 GAsyncReadyCallback callback,
784 lookup_by_name_async_real (resolver,
793 * g_resolver_lookup_by_name_async:
794 * @resolver: a #GResolver
795 * @hostname: the hostname to look up the address of
796 * @cancellable: (nullable): a #GCancellable, or %NULL
797 * @callback: (scope async) (closure user_data): callback to call after resolution completes
798 * @user_data: data for @callback
800 * Begins asynchronously resolving @hostname to determine its
801 * associated IP address(es), and eventually calls @callback, which
802 * must call g_resolver_lookup_by_name_finish() to get the result.
803 * See g_resolver_lookup_by_name() for more details.
808 g_resolver_lookup_by_name_async (GResolver *resolver,
809 const gchar *hostname,
810 GCancellable *cancellable,
811 GAsyncReadyCallback callback,
814 lookup_by_name_async_real (resolver,
823 * g_resolver_lookup_by_name_finish:
824 * @resolver: a #GResolver
825 * @result: the result passed to your #GAsyncReadyCallback
826 * @error: return location for a #GError, or %NULL
828 * Retrieves the result of a call to
829 * g_resolver_lookup_by_name_async().
831 * If the DNS resolution failed, @error (if non-%NULL) will be set to
832 * a value from #GResolverError. If the operation was cancelled,
833 * @error will be set to %G_IO_ERROR_CANCELLED.
835 * Returns: (element-type GInetAddress) (transfer full): a #GList
836 * of #GInetAddress, or %NULL on error. See g_resolver_lookup_by_name()
842 g_resolver_lookup_by_name_finish (GResolver *resolver,
843 GAsyncResult *result,
846 return lookup_by_name_finish_real (resolver,
853 * g_resolver_lookup_by_name_with_flags_finish:
854 * @resolver: a #GResolver
855 * @result: the result passed to your #GAsyncReadyCallback
856 * @error: return location for a #GError, or %NULL
858 * Retrieves the result of a call to
859 * g_resolver_lookup_by_name_with_flags_async().
861 * If the DNS resolution failed, @error (if non-%NULL) will be set to
862 * a value from #GResolverError. If the operation was cancelled,
863 * @error will be set to %G_IO_ERROR_CANCELLED.
865 * Returns: (element-type GInetAddress) (transfer full): a #GList
866 * of #GInetAddress, or %NULL on error. See g_resolver_lookup_by_name()
872 g_resolver_lookup_by_name_with_flags_finish (GResolver *resolver,
873 GAsyncResult *result,
876 return lookup_by_name_finish_real (resolver,
883 * g_resolver_free_addresses: (skip)
884 * @addresses: a #GList of #GInetAddress
886 * Frees @addresses (which should be the return value from
887 * g_resolver_lookup_by_name() or g_resolver_lookup_by_name_finish()).
888 * (This is a convenience method; you can also simply free the results
894 g_resolver_free_addresses (GList *addresses)
898 for (a = addresses; a; a = a->next)
899 g_object_unref (a->data);
900 g_list_free (addresses);
904 * g_resolver_lookup_by_address:
905 * @resolver: a #GResolver
906 * @address: the address to reverse-resolve
907 * @cancellable: (nullable): a #GCancellable, or %NULL
908 * @error: return location for a #GError, or %NULL
910 * Synchronously reverse-resolves @address to determine its
911 * associated hostname.
913 * If the DNS resolution fails, @error (if non-%NULL) will be set to
914 * a value from #GResolverError.
916 * If @cancellable is non-%NULL, it can be used to cancel the
917 * operation, in which case @error (if non-%NULL) will be set to
918 * %G_IO_ERROR_CANCELLED.
920 * Returns: a hostname (either ASCII-only, or in ASCII-encoded
921 * form), or %NULL on error.
926 g_resolver_lookup_by_address (GResolver *resolver,
927 GInetAddress *address,
928 GCancellable *cancellable,
931 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
932 g_return_val_if_fail (G_IS_INET_ADDRESS (address), NULL);
934 maybe_emit_reload (resolver);
935 return G_RESOLVER_GET_CLASS (resolver)->
936 lookup_by_address (resolver, address, cancellable, error);
940 * g_resolver_lookup_by_address_async:
941 * @resolver: a #GResolver
942 * @address: the address to reverse-resolve
943 * @cancellable: (nullable): a #GCancellable, or %NULL
944 * @callback: (scope async) (closure user_data): callback to call after resolution completes
945 * @user_data: data for @callback
947 * Begins asynchronously reverse-resolving @address to determine its
948 * associated hostname, and eventually calls @callback, which must
949 * call g_resolver_lookup_by_address_finish() to get the final result.
954 g_resolver_lookup_by_address_async (GResolver *resolver,
955 GInetAddress *address,
956 GCancellable *cancellable,
957 GAsyncReadyCallback callback,
960 g_return_if_fail (G_IS_RESOLVER (resolver));
961 g_return_if_fail (G_IS_INET_ADDRESS (address));
963 maybe_emit_reload (resolver);
964 G_RESOLVER_GET_CLASS (resolver)->
965 lookup_by_address_async (resolver, address, cancellable, callback, user_data);
969 * g_resolver_lookup_by_address_finish:
970 * @resolver: a #GResolver
971 * @result: the result passed to your #GAsyncReadyCallback
972 * @error: return location for a #GError, or %NULL
974 * Retrieves the result of a previous call to
975 * g_resolver_lookup_by_address_async().
977 * If the DNS resolution failed, @error (if non-%NULL) will be set to
978 * a value from #GResolverError. If the operation was cancelled,
979 * @error will be set to %G_IO_ERROR_CANCELLED.
981 * Returns: a hostname (either ASCII-only, or in ASCII-encoded
982 * form), or %NULL on error.
987 g_resolver_lookup_by_address_finish (GResolver *resolver,
988 GAsyncResult *result,
991 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
993 if (g_async_result_legacy_propagate_error (result, error))
996 return G_RESOLVER_GET_CLASS (resolver)->
997 lookup_by_address_finish (resolver, result, error);
1001 g_resolver_get_service_rrname (const char *service,
1002 const char *protocol,
1005 gchar *rrname, *ascii_domain = NULL;
1007 if (g_hostname_is_non_ascii (domain))
1008 domain = ascii_domain = g_hostname_to_ascii (domain);
1012 rrname = g_strdup_printf ("_%s._%s.%s", service, protocol, domain);
1014 g_free (ascii_domain);
1019 * g_resolver_lookup_service:
1020 * @resolver: a #GResolver
1021 * @service: the service type to look up (eg, "ldap")
1022 * @protocol: the networking protocol to use for @service (eg, "tcp")
1023 * @domain: the DNS domain to look up the service in
1024 * @cancellable: (nullable): a #GCancellable, or %NULL
1025 * @error: return location for a #GError, or %NULL
1027 * Synchronously performs a DNS SRV lookup for the given @service and
1028 * @protocol in the given @domain and returns an array of #GSrvTarget.
1029 * @domain may be an ASCII-only or UTF-8 hostname. Note also that the
1030 * @service and @protocol arguments do not include the leading underscore
1031 * that appears in the actual DNS entry.
1033 * On success, g_resolver_lookup_service() will return a non-empty #GList of
1034 * #GSrvTarget, sorted in order of preference. (That is, you should
1035 * attempt to connect to the first target first, then the second if
1036 * the first fails, etc.)
1038 * If the DNS resolution fails, @error (if non-%NULL) will be set to
1039 * a value from #GResolverError and %NULL will be returned.
1041 * If @cancellable is non-%NULL, it can be used to cancel the
1042 * operation, in which case @error (if non-%NULL) will be set to
1043 * %G_IO_ERROR_CANCELLED.
1045 * If you are planning to connect to the service, it is usually easier
1046 * to create a #GNetworkService and use its #GSocketConnectable
1049 * Returns: (element-type GSrvTarget) (transfer full): a non-empty #GList of
1050 * #GSrvTarget, or %NULL on error. You must free each of the targets and the
1051 * list when you are done with it. (You can use g_resolver_free_targets() to do
1057 g_resolver_lookup_service (GResolver *resolver,
1058 const gchar *service,
1059 const gchar *protocol,
1060 const gchar *domain,
1061 GCancellable *cancellable,
1067 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
1068 g_return_val_if_fail (service != NULL, NULL);
1069 g_return_val_if_fail (protocol != NULL, NULL);
1070 g_return_val_if_fail (domain != NULL, NULL);
1072 rrname = g_resolver_get_service_rrname (service, protocol, domain);
1075 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1076 _("Invalid domain"));
1080 maybe_emit_reload (resolver);
1081 targets = G_RESOLVER_GET_CLASS (resolver)->
1082 lookup_service (resolver, rrname, cancellable, error);
1089 * g_resolver_lookup_service_async:
1090 * @resolver: a #GResolver
1091 * @service: the service type to look up (eg, "ldap")
1092 * @protocol: the networking protocol to use for @service (eg, "tcp")
1093 * @domain: the DNS domain to look up the service in
1094 * @cancellable: (nullable): a #GCancellable, or %NULL
1095 * @callback: (scope async) (closure user_data): callback to call after resolution completes
1096 * @user_data: data for @callback
1098 * Begins asynchronously performing a DNS SRV lookup for the given
1099 * @service and @protocol in the given @domain, and eventually calls
1100 * @callback, which must call g_resolver_lookup_service_finish() to
1101 * get the final result. See g_resolver_lookup_service() for more
1107 g_resolver_lookup_service_async (GResolver *resolver,
1108 const gchar *service,
1109 const gchar *protocol,
1110 const gchar *domain,
1111 GCancellable *cancellable,
1112 GAsyncReadyCallback callback,
1117 g_return_if_fail (G_IS_RESOLVER (resolver));
1118 g_return_if_fail (service != NULL);
1119 g_return_if_fail (protocol != NULL);
1120 g_return_if_fail (domain != NULL);
1122 rrname = g_resolver_get_service_rrname (service, protocol, domain);
1125 g_task_report_new_error (resolver, callback, user_data,
1126 g_resolver_lookup_service_async,
1127 G_IO_ERROR, G_IO_ERROR_FAILED,
1128 _("Invalid domain"));
1132 maybe_emit_reload (resolver);
1133 G_RESOLVER_GET_CLASS (resolver)->
1134 lookup_service_async (resolver, rrname, cancellable, callback, user_data);
1140 * g_resolver_lookup_service_finish:
1141 * @resolver: a #GResolver
1142 * @result: the result passed to your #GAsyncReadyCallback
1143 * @error: return location for a #GError, or %NULL
1145 * Retrieves the result of a previous call to
1146 * g_resolver_lookup_service_async().
1148 * If the DNS resolution failed, @error (if non-%NULL) will be set to
1149 * a value from #GResolverError. If the operation was cancelled,
1150 * @error will be set to %G_IO_ERROR_CANCELLED.
1152 * Returns: (element-type GSrvTarget) (transfer full): a non-empty #GList of
1153 * #GSrvTarget, or %NULL on error. See g_resolver_lookup_service() for more
1159 g_resolver_lookup_service_finish (GResolver *resolver,
1160 GAsyncResult *result,
1163 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
1165 if (g_async_result_legacy_propagate_error (result, error))
1168 return G_RESOLVER_GET_CLASS (resolver)->
1169 lookup_service_finish (resolver, result, error);
1173 * g_resolver_free_targets: (skip)
1174 * @targets: a #GList of #GSrvTarget
1176 * Frees @targets (which should be the return value from
1177 * g_resolver_lookup_service() or g_resolver_lookup_service_finish()).
1178 * (This is a convenience method; you can also simply free the
1184 g_resolver_free_targets (GList *targets)
1188 for (t = targets; t; t = t->next)
1189 g_srv_target_free (t->data);
1190 g_list_free (targets);
1194 * g_resolver_lookup_records:
1195 * @resolver: a #GResolver
1196 * @rrname: the DNS name to look up the record for
1197 * @record_type: the type of DNS record to look up
1198 * @cancellable: (nullable): a #GCancellable, or %NULL
1199 * @error: return location for a #GError, or %NULL
1201 * Synchronously performs a DNS record lookup for the given @rrname and returns
1202 * a list of records as #GVariant tuples. See #GResolverRecordType for
1203 * information on what the records contain for each @record_type.
1205 * If the DNS resolution fails, @error (if non-%NULL) will be set to
1206 * a value from #GResolverError and %NULL will be returned.
1208 * If @cancellable is non-%NULL, it can be used to cancel the
1209 * operation, in which case @error (if non-%NULL) will be set to
1210 * %G_IO_ERROR_CANCELLED.
1212 * Returns: (element-type GVariant) (transfer full): a non-empty #GList of
1213 * #GVariant, or %NULL on error. You must free each of the records and the list
1214 * when you are done with it. (You can use g_list_free_full() with
1215 * g_variant_unref() to do this.)
1220 g_resolver_lookup_records (GResolver *resolver,
1221 const gchar *rrname,
1222 GResolverRecordType record_type,
1223 GCancellable *cancellable,
1228 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
1229 g_return_val_if_fail (rrname != NULL, NULL);
1231 maybe_emit_reload (resolver);
1232 records = G_RESOLVER_GET_CLASS (resolver)->
1233 lookup_records (resolver, rrname, record_type, cancellable, error);
1239 * g_resolver_lookup_records_async:
1240 * @resolver: a #GResolver
1241 * @rrname: the DNS name to look up the record for
1242 * @record_type: the type of DNS record to look up
1243 * @cancellable: (nullable): a #GCancellable, or %NULL
1244 * @callback: (scope async) (closure user_data): callback to call after resolution completes
1245 * @user_data: data for @callback
1247 * Begins asynchronously performing a DNS lookup for the given
1248 * @rrname, and eventually calls @callback, which must call
1249 * g_resolver_lookup_records_finish() to get the final result. See
1250 * g_resolver_lookup_records() for more details.
1255 g_resolver_lookup_records_async (GResolver *resolver,
1256 const gchar *rrname,
1257 GResolverRecordType record_type,
1258 GCancellable *cancellable,
1259 GAsyncReadyCallback callback,
1262 g_return_if_fail (G_IS_RESOLVER (resolver));
1263 g_return_if_fail (rrname != NULL);
1265 maybe_emit_reload (resolver);
1266 G_RESOLVER_GET_CLASS (resolver)->
1267 lookup_records_async (resolver, rrname, record_type, cancellable, callback, user_data);
1271 * g_resolver_lookup_records_finish:
1272 * @resolver: a #GResolver
1273 * @result: the result passed to your #GAsyncReadyCallback
1274 * @error: return location for a #GError, or %NULL
1276 * Retrieves the result of a previous call to
1277 * g_resolver_lookup_records_async(). Returns a non-empty list of records as
1278 * #GVariant tuples. See #GResolverRecordType for information on what the
1281 * If the DNS resolution failed, @error (if non-%NULL) will be set to
1282 * a value from #GResolverError. If the operation was cancelled,
1283 * @error will be set to %G_IO_ERROR_CANCELLED.
1285 * Returns: (element-type GVariant) (transfer full): a non-empty #GList of
1286 * #GVariant, or %NULL on error. You must free each of the records and the list
1287 * when you are done with it. (You can use g_list_free_full() with
1288 * g_variant_unref() to do this.)
1293 g_resolver_lookup_records_finish (GResolver *resolver,
1294 GAsyncResult *result,
1297 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
1298 return G_RESOLVER_GET_CLASS (resolver)->
1299 lookup_records_finish (resolver, result, error);
1303 g_resolver_get_serial (GResolver *resolver)
1307 g_return_val_if_fail (G_IS_RESOLVER (resolver), 0);
1309 maybe_emit_reload (resolver);
1312 g_mutex_lock (&resolver->priv->mutex);
1313 result = resolver->priv->resolv_conf_timestamp;
1314 g_mutex_unlock (&resolver->priv->mutex);
1323 * g_resolver_get_timeout:
1324 * @resolver: a #GResolver
1326 * Get the timeout applied to all resolver lookups. See #GResolver:timeout.
1328 * Returns: the resolver timeout, in milliseconds, or `0` for no timeout
1333 g_resolver_get_timeout (GResolver *resolver)
1335 GResolverPrivate *priv = g_resolver_get_instance_private (resolver);
1337 g_return_val_if_fail (G_IS_RESOLVER (resolver), 0);
1339 return priv->timeout_ms;
1343 * g_resolver_set_timeout:
1344 * @resolver: a #GResolver
1345 * @timeout_ms: timeout in milliseconds, or `0` for no timeouts
1347 * Set the timeout applied to all resolver lookups. See #GResolver:timeout.
1352 g_resolver_set_timeout (GResolver *resolver,
1353 unsigned timeout_ms)
1355 GResolverPrivate *priv = g_resolver_get_instance_private (resolver);
1357 g_return_if_fail (G_IS_RESOLVER (resolver));
1359 if (priv->timeout_ms == timeout_ms)
1362 priv->timeout_ms = timeout_ms;
1363 g_object_notify_by_pspec (G_OBJECT (resolver), props[PROP_TIMEOUT]);
1367 * g_resolver_error_quark:
1369 * Gets the #GResolver Error Quark.
1371 * Returns: a #GQuark.
1375 G_DEFINE_QUARK (g-resolver-error-quark, g_resolver_error)