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);
579 g_task_set_name (task, "[gio] resolver lookup");
581 g_task_return_pointer (task, addrs, (GDestroyNotify) g_resolver_free_addresses);
583 g_task_return_error (task, error);
584 g_object_unref (task);
588 if (g_hostname_is_non_ascii (hostname))
589 hostname = ascii_hostname = g_hostname_to_ascii (hostname);
595 g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_FAILED,
596 _("Invalid hostname"));
597 task = g_task_new (resolver, cancellable, callback, user_data);
598 g_task_set_source_tag (task, lookup_by_name_async_real);
599 g_task_set_name (task, "[gio] resolver lookup");
600 g_task_return_error (task, error);
601 g_object_unref (task);
605 g_resolver_maybe_reload (resolver);
607 if (flags != G_RESOLVER_NAME_LOOKUP_FLAGS_DEFAULT)
609 if (G_RESOLVER_GET_CLASS (resolver)->lookup_by_name_with_flags_async == NULL)
613 g_set_error (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
614 /* Translators: The placeholder is for a function name. */
615 _("%s not implemented"), "lookup_by_name_with_flags_async");
616 task = g_task_new (resolver, cancellable, callback, user_data);
617 g_task_set_source_tag (task, lookup_by_name_async_real);
618 g_task_set_name (task, "[gio] resolver lookup");
619 g_task_return_error (task, error);
620 g_object_unref (task);
623 G_RESOLVER_GET_CLASS (resolver)->
624 lookup_by_name_with_flags_async (resolver, hostname, flags, cancellable, callback, user_data);
627 G_RESOLVER_GET_CLASS (resolver)->
628 lookup_by_name_async (resolver, hostname, cancellable, callback, user_data);
630 g_free (ascii_hostname);
634 lookup_by_name_finish_real (GResolver *resolver,
635 GAsyncResult *result,
641 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
642 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
643 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
645 if (g_async_result_legacy_propagate_error (result, error))
647 else if (g_async_result_is_tagged (result, lookup_by_name_async_real))
649 /* Handle the stringified-IP-addr case */
650 return g_task_propagate_pointer (G_TASK (result), error);
655 g_assert (G_RESOLVER_GET_CLASS (resolver)->lookup_by_name_with_flags_finish != NULL);
656 addrs = G_RESOLVER_GET_CLASS (resolver)->
657 lookup_by_name_with_flags_finish (resolver, result, error);
660 addrs = G_RESOLVER_GET_CLASS (resolver)->
661 lookup_by_name_finish (resolver, result, error);
663 remove_duplicates (addrs);
669 * g_resolver_lookup_by_name_with_flags_async:
670 * @resolver: a #GResolver
671 * @hostname: the hostname to look up the address of
672 * @flags: extra #GResolverNameLookupFlags for the lookup
673 * @cancellable: (nullable): a #GCancellable, or %NULL
674 * @callback: (scope async): callback to call after resolution completes
675 * @user_data: (closure): data for @callback
677 * Begins asynchronously resolving @hostname to determine its
678 * associated IP address(es), and eventually calls @callback, which
679 * must call g_resolver_lookup_by_name_with_flags_finish() to get the result.
680 * See g_resolver_lookup_by_name() for more details.
685 g_resolver_lookup_by_name_with_flags_async (GResolver *resolver,
686 const gchar *hostname,
687 GResolverNameLookupFlags flags,
688 GCancellable *cancellable,
689 GAsyncReadyCallback callback,
692 lookup_by_name_async_real (resolver,
701 * g_resolver_lookup_by_name_async:
702 * @resolver: a #GResolver
703 * @hostname: the hostname to look up the address of
704 * @cancellable: (nullable): a #GCancellable, or %NULL
705 * @callback: (scope async): callback to call after resolution completes
706 * @user_data: (closure): data for @callback
708 * Begins asynchronously resolving @hostname to determine its
709 * associated IP address(es), and eventually calls @callback, which
710 * must call g_resolver_lookup_by_name_finish() to get the result.
711 * See g_resolver_lookup_by_name() for more details.
716 g_resolver_lookup_by_name_async (GResolver *resolver,
717 const gchar *hostname,
718 GCancellable *cancellable,
719 GAsyncReadyCallback callback,
722 lookup_by_name_async_real (resolver,
731 * g_resolver_lookup_by_name_finish:
732 * @resolver: a #GResolver
733 * @result: the result passed to your #GAsyncReadyCallback
734 * @error: return location for a #GError, or %NULL
736 * Retrieves the result of a call to
737 * g_resolver_lookup_by_name_async().
739 * If the DNS resolution failed, @error (if non-%NULL) will be set to
740 * a value from #GResolverError. If the operation was cancelled,
741 * @error will be set to %G_IO_ERROR_CANCELLED.
743 * Returns: (element-type GInetAddress) (transfer full): a #GList
744 * of #GInetAddress, or %NULL on error. See g_resolver_lookup_by_name()
750 g_resolver_lookup_by_name_finish (GResolver *resolver,
751 GAsyncResult *result,
754 return lookup_by_name_finish_real (resolver,
761 * g_resolver_lookup_by_name_with_flags_finish:
762 * @resolver: a #GResolver
763 * @result: the result passed to your #GAsyncReadyCallback
764 * @error: return location for a #GError, or %NULL
766 * Retrieves the result of a call to
767 * g_resolver_lookup_by_name_with_flags_async().
769 * If the DNS resolution failed, @error (if non-%NULL) will be set to
770 * a value from #GResolverError. If the operation was cancelled,
771 * @error will be set to %G_IO_ERROR_CANCELLED.
773 * Returns: (element-type GInetAddress) (transfer full): a #GList
774 * of #GInetAddress, or %NULL on error. See g_resolver_lookup_by_name()
780 g_resolver_lookup_by_name_with_flags_finish (GResolver *resolver,
781 GAsyncResult *result,
784 return lookup_by_name_finish_real (resolver,
791 * g_resolver_free_addresses: (skip)
792 * @addresses: a #GList of #GInetAddress
794 * Frees @addresses (which should be the return value from
795 * g_resolver_lookup_by_name() or g_resolver_lookup_by_name_finish()).
796 * (This is a convenience method; you can also simply free the results
802 g_resolver_free_addresses (GList *addresses)
806 for (a = addresses; a; a = a->next)
807 g_object_unref (a->data);
808 g_list_free (addresses);
812 * g_resolver_lookup_by_address:
813 * @resolver: a #GResolver
814 * @address: the address to reverse-resolve
815 * @cancellable: (nullable): a #GCancellable, or %NULL
816 * @error: return location for a #GError, or %NULL
818 * Synchronously reverse-resolves @address to determine its
819 * associated hostname.
821 * If the DNS resolution fails, @error (if non-%NULL) will be set to
822 * a value from #GResolverError.
824 * If @cancellable is non-%NULL, it can be used to cancel the
825 * operation, in which case @error (if non-%NULL) will be set to
826 * %G_IO_ERROR_CANCELLED.
828 * Returns: a hostname (either ASCII-only, or in ASCII-encoded
829 * form), or %NULL on error.
834 g_resolver_lookup_by_address (GResolver *resolver,
835 GInetAddress *address,
836 GCancellable *cancellable,
839 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
840 g_return_val_if_fail (G_IS_INET_ADDRESS (address), NULL);
842 g_resolver_maybe_reload (resolver);
843 return G_RESOLVER_GET_CLASS (resolver)->
844 lookup_by_address (resolver, address, cancellable, error);
848 * g_resolver_lookup_by_address_async:
849 * @resolver: a #GResolver
850 * @address: the address to reverse-resolve
851 * @cancellable: (nullable): a #GCancellable, or %NULL
852 * @callback: (scope async): callback to call after resolution completes
853 * @user_data: (closure): data for @callback
855 * Begins asynchronously reverse-resolving @address to determine its
856 * associated hostname, and eventually calls @callback, which must
857 * call g_resolver_lookup_by_address_finish() to get the final result.
862 g_resolver_lookup_by_address_async (GResolver *resolver,
863 GInetAddress *address,
864 GCancellable *cancellable,
865 GAsyncReadyCallback callback,
868 g_return_if_fail (G_IS_RESOLVER (resolver));
869 g_return_if_fail (G_IS_INET_ADDRESS (address));
871 g_resolver_maybe_reload (resolver);
872 G_RESOLVER_GET_CLASS (resolver)->
873 lookup_by_address_async (resolver, address, cancellable, callback, user_data);
877 * g_resolver_lookup_by_address_finish:
878 * @resolver: a #GResolver
879 * @result: the result passed to your #GAsyncReadyCallback
880 * @error: return location for a #GError, or %NULL
882 * Retrieves the result of a previous call to
883 * g_resolver_lookup_by_address_async().
885 * If the DNS resolution failed, @error (if non-%NULL) will be set to
886 * a value from #GResolverError. If the operation was cancelled,
887 * @error will be set to %G_IO_ERROR_CANCELLED.
889 * Returns: a hostname (either ASCII-only, or in ASCII-encoded
890 * form), or %NULL on error.
895 g_resolver_lookup_by_address_finish (GResolver *resolver,
896 GAsyncResult *result,
899 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
901 if (g_async_result_legacy_propagate_error (result, error))
904 return G_RESOLVER_GET_CLASS (resolver)->
905 lookup_by_address_finish (resolver, result, error);
909 g_resolver_get_service_rrname (const char *service,
910 const char *protocol,
913 gchar *rrname, *ascii_domain = NULL;
915 if (g_hostname_is_non_ascii (domain))
916 domain = ascii_domain = g_hostname_to_ascii (domain);
920 rrname = g_strdup_printf ("_%s._%s.%s", service, protocol, domain);
922 g_free (ascii_domain);
927 * g_resolver_lookup_service:
928 * @resolver: a #GResolver
929 * @service: the service type to look up (eg, "ldap")
930 * @protocol: the networking protocol to use for @service (eg, "tcp")
931 * @domain: the DNS domain to look up the service in
932 * @cancellable: (nullable): a #GCancellable, or %NULL
933 * @error: return location for a #GError, or %NULL
935 * Synchronously performs a DNS SRV lookup for the given @service and
936 * @protocol in the given @domain and returns an array of #GSrvTarget.
937 * @domain may be an ASCII-only or UTF-8 hostname. Note also that the
938 * @service and @protocol arguments do not include the leading underscore
939 * that appears in the actual DNS entry.
941 * On success, g_resolver_lookup_service() will return a non-empty #GList of
942 * #GSrvTarget, sorted in order of preference. (That is, you should
943 * attempt to connect to the first target first, then the second if
944 * the first fails, etc.)
946 * If the DNS resolution fails, @error (if non-%NULL) will be set to
947 * a value from #GResolverError and %NULL will be returned.
949 * If @cancellable is non-%NULL, it can be used to cancel the
950 * operation, in which case @error (if non-%NULL) will be set to
951 * %G_IO_ERROR_CANCELLED.
953 * If you are planning to connect to the service, it is usually easier
954 * to create a #GNetworkService and use its #GSocketConnectable
957 * Returns: (element-type GSrvTarget) (transfer full): a non-empty #GList of
958 * #GSrvTarget, or %NULL on error. You must free each of the targets and the
959 * list when you are done with it. (You can use g_resolver_free_targets() to do
965 g_resolver_lookup_service (GResolver *resolver,
966 const gchar *service,
967 const gchar *protocol,
969 GCancellable *cancellable,
975 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
976 g_return_val_if_fail (service != NULL, NULL);
977 g_return_val_if_fail (protocol != NULL, NULL);
978 g_return_val_if_fail (domain != NULL, NULL);
980 rrname = g_resolver_get_service_rrname (service, protocol, domain);
983 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
984 _("Invalid domain"));
988 g_resolver_maybe_reload (resolver);
989 targets = G_RESOLVER_GET_CLASS (resolver)->
990 lookup_service (resolver, rrname, cancellable, error);
997 * g_resolver_lookup_service_async:
998 * @resolver: a #GResolver
999 * @service: the service type to look up (eg, "ldap")
1000 * @protocol: the networking protocol to use for @service (eg, "tcp")
1001 * @domain: the DNS domain to look up the service in
1002 * @cancellable: (nullable): a #GCancellable, or %NULL
1003 * @callback: (scope async): callback to call after resolution completes
1004 * @user_data: (closure): data for @callback
1006 * Begins asynchronously performing a DNS SRV lookup for the given
1007 * @service and @protocol in the given @domain, and eventually calls
1008 * @callback, which must call g_resolver_lookup_service_finish() to
1009 * get the final result. See g_resolver_lookup_service() for more
1015 g_resolver_lookup_service_async (GResolver *resolver,
1016 const gchar *service,
1017 const gchar *protocol,
1018 const gchar *domain,
1019 GCancellable *cancellable,
1020 GAsyncReadyCallback callback,
1025 g_return_if_fail (G_IS_RESOLVER (resolver));
1026 g_return_if_fail (service != NULL);
1027 g_return_if_fail (protocol != NULL);
1028 g_return_if_fail (domain != NULL);
1030 rrname = g_resolver_get_service_rrname (service, protocol, domain);
1033 g_task_report_new_error (resolver, callback, user_data,
1034 g_resolver_lookup_service_async,
1035 G_IO_ERROR, G_IO_ERROR_FAILED,
1036 _("Invalid domain"));
1040 g_resolver_maybe_reload (resolver);
1041 G_RESOLVER_GET_CLASS (resolver)->
1042 lookup_service_async (resolver, rrname, cancellable, callback, user_data);
1048 * g_resolver_lookup_service_finish:
1049 * @resolver: a #GResolver
1050 * @result: the result passed to your #GAsyncReadyCallback
1051 * @error: return location for a #GError, or %NULL
1053 * Retrieves the result of a previous call to
1054 * g_resolver_lookup_service_async().
1056 * If the DNS resolution failed, @error (if non-%NULL) will be set to
1057 * a value from #GResolverError. If the operation was cancelled,
1058 * @error will be set to %G_IO_ERROR_CANCELLED.
1060 * Returns: (element-type GSrvTarget) (transfer full): a non-empty #GList of
1061 * #GSrvTarget, or %NULL on error. See g_resolver_lookup_service() for more
1067 g_resolver_lookup_service_finish (GResolver *resolver,
1068 GAsyncResult *result,
1071 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
1073 if (g_async_result_legacy_propagate_error (result, error))
1076 return G_RESOLVER_GET_CLASS (resolver)->
1077 lookup_service_finish (resolver, result, error);
1081 * g_resolver_free_targets: (skip)
1082 * @targets: a #GList of #GSrvTarget
1084 * Frees @targets (which should be the return value from
1085 * g_resolver_lookup_service() or g_resolver_lookup_service_finish()).
1086 * (This is a convenience method; you can also simply free the
1092 g_resolver_free_targets (GList *targets)
1096 for (t = targets; t; t = t->next)
1097 g_srv_target_free (t->data);
1098 g_list_free (targets);
1102 * g_resolver_lookup_records:
1103 * @resolver: a #GResolver
1104 * @rrname: the DNS name to look up the record for
1105 * @record_type: the type of DNS record to look up
1106 * @cancellable: (nullable): a #GCancellable, or %NULL
1107 * @error: return location for a #GError, or %NULL
1109 * Synchronously performs a DNS record lookup for the given @rrname and returns
1110 * a list of records as #GVariant tuples. See #GResolverRecordType for
1111 * information on what the records contain for each @record_type.
1113 * If the DNS resolution fails, @error (if non-%NULL) will be set to
1114 * a value from #GResolverError and %NULL will be returned.
1116 * If @cancellable is non-%NULL, it can be used to cancel the
1117 * operation, in which case @error (if non-%NULL) will be set to
1118 * %G_IO_ERROR_CANCELLED.
1120 * Returns: (element-type GVariant) (transfer full): a non-empty #GList of
1121 * #GVariant, or %NULL on error. You must free each of the records and the list
1122 * when you are done with it. (You can use g_list_free_full() with
1123 * g_variant_unref() to do this.)
1128 g_resolver_lookup_records (GResolver *resolver,
1129 const gchar *rrname,
1130 GResolverRecordType record_type,
1131 GCancellable *cancellable,
1136 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
1137 g_return_val_if_fail (rrname != NULL, NULL);
1139 g_resolver_maybe_reload (resolver);
1140 records = G_RESOLVER_GET_CLASS (resolver)->
1141 lookup_records (resolver, rrname, record_type, cancellable, error);
1147 * g_resolver_lookup_records_async:
1148 * @resolver: a #GResolver
1149 * @rrname: the DNS name to look up the record for
1150 * @record_type: the type of DNS record to look up
1151 * @cancellable: (nullable): a #GCancellable, or %NULL
1152 * @callback: (scope async): callback to call after resolution completes
1153 * @user_data: (closure): data for @callback
1155 * Begins asynchronously performing a DNS lookup for the given
1156 * @rrname, and eventually calls @callback, which must call
1157 * g_resolver_lookup_records_finish() to get the final result. See
1158 * g_resolver_lookup_records() for more details.
1163 g_resolver_lookup_records_async (GResolver *resolver,
1164 const gchar *rrname,
1165 GResolverRecordType record_type,
1166 GCancellable *cancellable,
1167 GAsyncReadyCallback callback,
1170 g_return_if_fail (G_IS_RESOLVER (resolver));
1171 g_return_if_fail (rrname != NULL);
1173 g_resolver_maybe_reload (resolver);
1174 G_RESOLVER_GET_CLASS (resolver)->
1175 lookup_records_async (resolver, rrname, record_type, cancellable, callback, user_data);
1179 * g_resolver_lookup_records_finish:
1180 * @resolver: a #GResolver
1181 * @result: the result passed to your #GAsyncReadyCallback
1182 * @error: return location for a #GError, or %NULL
1184 * Retrieves the result of a previous call to
1185 * g_resolver_lookup_records_async(). Returns a non-empty list of records as
1186 * #GVariant tuples. See #GResolverRecordType for information on what the
1189 * If the DNS resolution failed, @error (if non-%NULL) will be set to
1190 * a value from #GResolverError. If the operation was cancelled,
1191 * @error will be set to %G_IO_ERROR_CANCELLED.
1193 * Returns: (element-type GVariant) (transfer full): a non-empty #GList of
1194 * #GVariant, or %NULL on error. You must free each of the records and the list
1195 * when you are done with it. (You can use g_list_free_full() with
1196 * g_variant_unref() to do this.)
1201 g_resolver_lookup_records_finish (GResolver *resolver,
1202 GAsyncResult *result,
1205 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
1206 return G_RESOLVER_GET_CLASS (resolver)->
1207 lookup_records_finish (resolver, result, error);
1211 g_resolver_get_serial (GResolver *resolver)
1213 g_return_val_if_fail (G_IS_RESOLVER (resolver), 0);
1215 g_resolver_maybe_reload (resolver);
1218 return (guint64) resolver->priv->resolv_conf_timestamp;
1225 * g_resolver_error_quark:
1227 * Gets the #GResolver Error Quark.
1229 * Returns: a #GQuark.
1233 G_DEFINE_QUARK (g-resolver-error-quark, g_resolver_error)