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),
172 g_cclosure_marshal_VOID__VOID,
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);
299 /* Note that this does not follow the "FALSE means @error is set"
300 * convention. The return value tells the caller whether it should
301 * return @addrs and @error to the caller right away, or if it should
302 * continue and trying to resolve the name as a hostname.
305 handle_ip_address (const char *hostname,
312 struct in_addr ip4addr;
315 addr = g_inet_address_new_from_string (hostname);
318 *addrs = g_list_append (NULL, addr);
326 /* Reject IPv6 addresses that have brackets ('[' or ']') and/or port numbers,
327 * as no valid addresses should contain these at this point.
328 * Non-standard IPv4 addresses would be rejected during the call to
329 * getaddrinfo() later.
331 if (strrchr (hostname, '[') != NULL ||
332 strrchr (hostname, ']') != NULL)
335 /* Reject non-standard IPv4 numbers-and-dots addresses.
336 * g_inet_address_new_from_string() will have accepted any "real" IP
337 * address, so if inet_aton() succeeds, then it's an address we want
340 if (inet_aton (hostname, &ip4addr))
343 g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_NOT_FOUND,
344 _("Error resolving “%s”: %s"),
345 hostname, gai_strerror (EAI_NONAME));
353 lookup_by_name_real (GResolver *resolver,
354 const gchar *hostname,
355 GResolverNameLookupFlags flags,
356 GCancellable *cancellable,
360 gchar *ascii_hostname = NULL;
362 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
363 g_return_val_if_fail (hostname != NULL, NULL);
364 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
365 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
367 /* Check if @hostname is just an IP address */
368 if (handle_ip_address (hostname, &addrs, error))
371 if (g_hostname_is_non_ascii (hostname))
372 hostname = ascii_hostname = g_hostname_to_ascii (hostname);
376 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
377 _("Invalid hostname"));
381 g_resolver_maybe_reload (resolver);
383 if (flags != G_RESOLVER_NAME_LOOKUP_FLAGS_DEFAULT)
385 if (!G_RESOLVER_GET_CLASS (resolver)->lookup_by_name_with_flags)
387 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
388 /* Translators: The placeholder is for a function name. */
389 _("%s not implemented"), "lookup_by_name_with_flags");
390 g_free (ascii_hostname);
393 addrs = G_RESOLVER_GET_CLASS (resolver)->
394 lookup_by_name_with_flags (resolver, hostname, flags, cancellable, error);
397 addrs = G_RESOLVER_GET_CLASS (resolver)->
398 lookup_by_name (resolver, hostname, cancellable, error);
400 remove_duplicates (addrs);
402 g_free (ascii_hostname);
407 * g_resolver_lookup_by_name:
408 * @resolver: a #GResolver
409 * @hostname: the hostname to look up
410 * @cancellable: (nullable): a #GCancellable, or %NULL
411 * @error: return location for a #GError, or %NULL
413 * Synchronously resolves @hostname to determine its associated IP
414 * address(es). @hostname may be an ASCII-only or UTF-8 hostname, or
415 * the textual form of an IP address (in which case this just becomes
416 * a wrapper around g_inet_address_new_from_string()).
418 * On success, g_resolver_lookup_by_name() will return a non-empty #GList of
419 * #GInetAddress, sorted in order of preference and guaranteed to not
420 * contain duplicates. That is, if using the result to connect to
421 * @hostname, you should attempt to connect to the first address
422 * first, then the second if the first fails, etc. If you are using
423 * the result to listen on a socket, it is appropriate to add each
424 * result using e.g. g_socket_listener_add_address().
426 * If the DNS resolution fails, @error (if non-%NULL) will be set to a
427 * value from #GResolverError and %NULL will be returned.
429 * If @cancellable is non-%NULL, it can be used to cancel the
430 * operation, in which case @error (if non-%NULL) will be set to
431 * %G_IO_ERROR_CANCELLED.
433 * If you are planning to connect to a socket on the resolved IP
434 * address, it may be easier to create a #GNetworkAddress and use its
435 * #GSocketConnectable interface.
437 * Returns: (element-type GInetAddress) (transfer full): a non-empty #GList
438 * of #GInetAddress, or %NULL on error. You
439 * must unref each of the addresses and free the list when you are
440 * done with it. (You can use g_resolver_free_addresses() to do this.)
445 g_resolver_lookup_by_name (GResolver *resolver,
446 const gchar *hostname,
447 GCancellable *cancellable,
450 return lookup_by_name_real (resolver,
452 G_RESOLVER_NAME_LOOKUP_FLAGS_DEFAULT,
458 * g_resolver_lookup_by_name_with_flags:
459 * @resolver: a #GResolver
460 * @hostname: the hostname to look up
461 * @flags: extra #GResolverNameLookupFlags for the lookup
462 * @cancellable: (nullable): a #GCancellable, or %NULL
463 * @error: (nullable): return location for a #GError, or %NULL
465 * This differs from g_resolver_lookup_by_name() in that you can modify
466 * the lookup behavior with @flags. For example this can be used to limit
467 * results with #G_RESOLVER_NAME_LOOKUP_FLAGS_IPV4_ONLY.
469 * Returns: (element-type GInetAddress) (transfer full): a non-empty #GList
470 * of #GInetAddress, or %NULL on error. You
471 * must unref each of the addresses and free the list when you are
472 * done with it. (You can use g_resolver_free_addresses() to do this.)
477 g_resolver_lookup_by_name_with_flags (GResolver *resolver,
478 const gchar *hostname,
479 GResolverNameLookupFlags flags,
480 GCancellable *cancellable,
483 return lookup_by_name_real (resolver,
491 lookup_by_name_async_real (GResolver *resolver,
492 const gchar *hostname,
493 GResolverNameLookupFlags flags,
494 GCancellable *cancellable,
495 GAsyncReadyCallback callback,
498 gchar *ascii_hostname = NULL;
500 GError *error = NULL;
502 g_return_if_fail (G_IS_RESOLVER (resolver));
503 g_return_if_fail (hostname != NULL);
504 g_return_if_fail (!(flags & G_RESOLVER_NAME_LOOKUP_FLAGS_IPV4_ONLY && flags & G_RESOLVER_NAME_LOOKUP_FLAGS_IPV6_ONLY));
506 /* Check if @hostname is just an IP address */
507 if (handle_ip_address (hostname, &addrs, &error))
511 task = g_task_new (resolver, cancellable, callback, user_data);
512 g_task_set_source_tag (task, lookup_by_name_async_real);
514 g_task_return_pointer (task, addrs, (GDestroyNotify) g_resolver_free_addresses);
516 g_task_return_error (task, error);
517 g_object_unref (task);
521 if (g_hostname_is_non_ascii (hostname))
522 hostname = ascii_hostname = g_hostname_to_ascii (hostname);
528 g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_FAILED,
529 _("Invalid hostname"));
530 task = g_task_new (resolver, cancellable, callback, user_data);
531 g_task_set_source_tag (task, lookup_by_name_async_real);
532 g_task_return_error (task, error);
533 g_object_unref (task);
537 g_resolver_maybe_reload (resolver);
539 if (flags != G_RESOLVER_NAME_LOOKUP_FLAGS_DEFAULT)
541 if (G_RESOLVER_GET_CLASS (resolver)->lookup_by_name_with_flags_async == NULL)
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_async");
548 task = g_task_new (resolver, cancellable, callback, user_data);
549 g_task_set_source_tag (task, lookup_by_name_async_real);
550 g_task_return_error (task, error);
551 g_object_unref (task);
554 G_RESOLVER_GET_CLASS (resolver)->
555 lookup_by_name_with_flags_async (resolver, hostname, flags, cancellable, callback, user_data);
558 G_RESOLVER_GET_CLASS (resolver)->
559 lookup_by_name_async (resolver, hostname, cancellable, callback, user_data);
561 g_free (ascii_hostname);
565 lookup_by_name_finish_real (GResolver *resolver,
566 GAsyncResult *result,
572 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
573 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
574 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
576 if (g_async_result_legacy_propagate_error (result, error))
578 else if (g_async_result_is_tagged (result, lookup_by_name_async_real))
580 /* Handle the stringified-IP-addr case */
581 return g_task_propagate_pointer (G_TASK (result), error);
586 g_assert (G_RESOLVER_GET_CLASS (resolver)->lookup_by_name_with_flags_finish != NULL);
587 addrs = G_RESOLVER_GET_CLASS (resolver)->
588 lookup_by_name_with_flags_finish (resolver, result, error);
591 addrs = G_RESOLVER_GET_CLASS (resolver)->
592 lookup_by_name_finish (resolver, result, error);
594 remove_duplicates (addrs);
600 * g_resolver_lookup_by_name_with_flags_async:
601 * @resolver: a #GResolver
602 * @hostname: the hostname to look up the address of
603 * @flags: extra #GResolverNameLookupFlags for the lookup
604 * @cancellable: (nullable): a #GCancellable, or %NULL
605 * @callback: (scope async): callback to call after resolution completes
606 * @user_data: (closure): data for @callback
608 * Begins asynchronously resolving @hostname to determine its
609 * associated IP address(es), and eventually calls @callback, which
610 * must call g_resolver_lookup_by_name_with_flags_finish() to get the result.
611 * See g_resolver_lookup_by_name() for more details.
616 g_resolver_lookup_by_name_with_flags_async (GResolver *resolver,
617 const gchar *hostname,
618 GResolverNameLookupFlags flags,
619 GCancellable *cancellable,
620 GAsyncReadyCallback callback,
623 lookup_by_name_async_real (resolver,
632 * g_resolver_lookup_by_name_async:
633 * @resolver: a #GResolver
634 * @hostname: the hostname to look up the address of
635 * @cancellable: (nullable): a #GCancellable, or %NULL
636 * @callback: (scope async): callback to call after resolution completes
637 * @user_data: (closure): data for @callback
639 * Begins asynchronously resolving @hostname to determine its
640 * associated IP address(es), and eventually calls @callback, which
641 * must call g_resolver_lookup_by_name_finish() to get the result.
642 * See g_resolver_lookup_by_name() for more details.
647 g_resolver_lookup_by_name_async (GResolver *resolver,
648 const gchar *hostname,
649 GCancellable *cancellable,
650 GAsyncReadyCallback callback,
653 lookup_by_name_async_real (resolver,
662 * g_resolver_lookup_by_name_finish:
663 * @resolver: a #GResolver
664 * @result: the result passed to your #GAsyncReadyCallback
665 * @error: return location for a #GError, or %NULL
667 * Retrieves the result of a call to
668 * g_resolver_lookup_by_name_async().
670 * If the DNS resolution failed, @error (if non-%NULL) will be set to
671 * a value from #GResolverError. If the operation was cancelled,
672 * @error will be set to %G_IO_ERROR_CANCELLED.
674 * Returns: (element-type GInetAddress) (transfer full): a #GList
675 * of #GInetAddress, or %NULL on error. See g_resolver_lookup_by_name()
681 g_resolver_lookup_by_name_finish (GResolver *resolver,
682 GAsyncResult *result,
685 return lookup_by_name_finish_real (resolver,
692 * g_resolver_lookup_by_name_with_flags_finish:
693 * @resolver: a #GResolver
694 * @result: the result passed to your #GAsyncReadyCallback
695 * @error: return location for a #GError, or %NULL
697 * Retrieves the result of a call to
698 * g_resolver_lookup_by_name_with_flags_async().
700 * If the DNS resolution failed, @error (if non-%NULL) will be set to
701 * a value from #GResolverError. If the operation was cancelled,
702 * @error will be set to %G_IO_ERROR_CANCELLED.
704 * Returns: (element-type GInetAddress) (transfer full): a #GList
705 * of #GInetAddress, or %NULL on error. See g_resolver_lookup_by_name()
711 g_resolver_lookup_by_name_with_flags_finish (GResolver *resolver,
712 GAsyncResult *result,
715 return lookup_by_name_finish_real (resolver,
722 * g_resolver_free_addresses: (skip)
723 * @addresses: a #GList of #GInetAddress
725 * Frees @addresses (which should be the return value from
726 * g_resolver_lookup_by_name() or g_resolver_lookup_by_name_finish()).
727 * (This is a convenience method; you can also simply free the results
733 g_resolver_free_addresses (GList *addresses)
737 for (a = addresses; a; a = a->next)
738 g_object_unref (a->data);
739 g_list_free (addresses);
743 * g_resolver_lookup_by_address:
744 * @resolver: a #GResolver
745 * @address: the address to reverse-resolve
746 * @cancellable: (nullable): a #GCancellable, or %NULL
747 * @error: return location for a #GError, or %NULL
749 * Synchronously reverse-resolves @address to determine its
750 * associated hostname.
752 * If the DNS resolution fails, @error (if non-%NULL) will be set to
753 * a value from #GResolverError.
755 * If @cancellable is non-%NULL, it can be used to cancel the
756 * operation, in which case @error (if non-%NULL) will be set to
757 * %G_IO_ERROR_CANCELLED.
759 * Returns: a hostname (either ASCII-only, or in ASCII-encoded
760 * form), or %NULL on error.
765 g_resolver_lookup_by_address (GResolver *resolver,
766 GInetAddress *address,
767 GCancellable *cancellable,
770 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
771 g_return_val_if_fail (G_IS_INET_ADDRESS (address), NULL);
773 g_resolver_maybe_reload (resolver);
774 return G_RESOLVER_GET_CLASS (resolver)->
775 lookup_by_address (resolver, address, cancellable, error);
779 * g_resolver_lookup_by_address_async:
780 * @resolver: a #GResolver
781 * @address: the address to reverse-resolve
782 * @cancellable: (nullable): a #GCancellable, or %NULL
783 * @callback: (scope async): callback to call after resolution completes
784 * @user_data: (closure): data for @callback
786 * Begins asynchronously reverse-resolving @address to determine its
787 * associated hostname, and eventually calls @callback, which must
788 * call g_resolver_lookup_by_address_finish() to get the final result.
793 g_resolver_lookup_by_address_async (GResolver *resolver,
794 GInetAddress *address,
795 GCancellable *cancellable,
796 GAsyncReadyCallback callback,
799 g_return_if_fail (G_IS_RESOLVER (resolver));
800 g_return_if_fail (G_IS_INET_ADDRESS (address));
802 g_resolver_maybe_reload (resolver);
803 G_RESOLVER_GET_CLASS (resolver)->
804 lookup_by_address_async (resolver, address, cancellable, callback, user_data);
808 * g_resolver_lookup_by_address_finish:
809 * @resolver: a #GResolver
810 * @result: the result passed to your #GAsyncReadyCallback
811 * @error: return location for a #GError, or %NULL
813 * Retrieves the result of a previous call to
814 * g_resolver_lookup_by_address_async().
816 * If the DNS resolution failed, @error (if non-%NULL) will be set to
817 * a value from #GResolverError. If the operation was cancelled,
818 * @error will be set to %G_IO_ERROR_CANCELLED.
820 * Returns: a hostname (either ASCII-only, or in ASCII-encoded
821 * form), or %NULL on error.
826 g_resolver_lookup_by_address_finish (GResolver *resolver,
827 GAsyncResult *result,
830 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
832 if (g_async_result_legacy_propagate_error (result, error))
835 return G_RESOLVER_GET_CLASS (resolver)->
836 lookup_by_address_finish (resolver, result, error);
840 g_resolver_get_service_rrname (const char *service,
841 const char *protocol,
844 gchar *rrname, *ascii_domain = NULL;
846 if (g_hostname_is_non_ascii (domain))
847 domain = ascii_domain = g_hostname_to_ascii (domain);
851 rrname = g_strdup_printf ("_%s._%s.%s", service, protocol, domain);
853 g_free (ascii_domain);
858 * g_resolver_lookup_service:
859 * @resolver: a #GResolver
860 * @service: the service type to look up (eg, "ldap")
861 * @protocol: the networking protocol to use for @service (eg, "tcp")
862 * @domain: the DNS domain to look up the service in
863 * @cancellable: (nullable): a #GCancellable, or %NULL
864 * @error: return location for a #GError, or %NULL
866 * Synchronously performs a DNS SRV lookup for the given @service and
867 * @protocol in the given @domain and returns an array of #GSrvTarget.
868 * @domain may be an ASCII-only or UTF-8 hostname. Note also that the
869 * @service and @protocol arguments do not include the leading underscore
870 * that appears in the actual DNS entry.
872 * On success, g_resolver_lookup_service() will return a non-empty #GList of
873 * #GSrvTarget, sorted in order of preference. (That is, you should
874 * attempt to connect to the first target first, then the second if
875 * the first fails, etc.)
877 * If the DNS resolution fails, @error (if non-%NULL) will be set to
878 * a value from #GResolverError and %NULL will be returned.
880 * If @cancellable is non-%NULL, it can be used to cancel the
881 * operation, in which case @error (if non-%NULL) will be set to
882 * %G_IO_ERROR_CANCELLED.
884 * If you are planning to connect to the service, it is usually easier
885 * to create a #GNetworkService and use its #GSocketConnectable
888 * Returns: (element-type GSrvTarget) (transfer full): a non-empty #GList of
889 * #GSrvTarget, or %NULL on error. You must free each of the targets and the
890 * list when you are done with it. (You can use g_resolver_free_targets() to do
896 g_resolver_lookup_service (GResolver *resolver,
897 const gchar *service,
898 const gchar *protocol,
900 GCancellable *cancellable,
906 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
907 g_return_val_if_fail (service != NULL, NULL);
908 g_return_val_if_fail (protocol != NULL, NULL);
909 g_return_val_if_fail (domain != NULL, NULL);
911 rrname = g_resolver_get_service_rrname (service, protocol, domain);
914 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
915 _("Invalid domain"));
919 g_resolver_maybe_reload (resolver);
920 targets = G_RESOLVER_GET_CLASS (resolver)->
921 lookup_service (resolver, rrname, cancellable, error);
928 * g_resolver_lookup_service_async:
929 * @resolver: a #GResolver
930 * @service: the service type to look up (eg, "ldap")
931 * @protocol: the networking protocol to use for @service (eg, "tcp")
932 * @domain: the DNS domain to look up the service in
933 * @cancellable: (nullable): a #GCancellable, or %NULL
934 * @callback: (scope async): callback to call after resolution completes
935 * @user_data: (closure): data for @callback
937 * Begins asynchronously performing a DNS SRV lookup for the given
938 * @service and @protocol in the given @domain, and eventually calls
939 * @callback, which must call g_resolver_lookup_service_finish() to
940 * get the final result. See g_resolver_lookup_service() for more
946 g_resolver_lookup_service_async (GResolver *resolver,
947 const gchar *service,
948 const gchar *protocol,
950 GCancellable *cancellable,
951 GAsyncReadyCallback callback,
956 g_return_if_fail (G_IS_RESOLVER (resolver));
957 g_return_if_fail (service != NULL);
958 g_return_if_fail (protocol != NULL);
959 g_return_if_fail (domain != NULL);
961 rrname = g_resolver_get_service_rrname (service, protocol, domain);
964 g_task_report_new_error (resolver, callback, user_data,
965 g_resolver_lookup_service_async,
966 G_IO_ERROR, G_IO_ERROR_FAILED,
967 _("Invalid domain"));
971 g_resolver_maybe_reload (resolver);
972 G_RESOLVER_GET_CLASS (resolver)->
973 lookup_service_async (resolver, rrname, cancellable, callback, user_data);
979 * g_resolver_lookup_service_finish:
980 * @resolver: a #GResolver
981 * @result: the result passed to your #GAsyncReadyCallback
982 * @error: return location for a #GError, or %NULL
984 * Retrieves the result of a previous call to
985 * g_resolver_lookup_service_async().
987 * If the DNS resolution failed, @error (if non-%NULL) will be set to
988 * a value from #GResolverError. If the operation was cancelled,
989 * @error will be set to %G_IO_ERROR_CANCELLED.
991 * Returns: (element-type GSrvTarget) (transfer full): a non-empty #GList of
992 * #GSrvTarget, or %NULL on error. See g_resolver_lookup_service() for more
998 g_resolver_lookup_service_finish (GResolver *resolver,
999 GAsyncResult *result,
1002 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
1004 if (g_async_result_legacy_propagate_error (result, error))
1007 return G_RESOLVER_GET_CLASS (resolver)->
1008 lookup_service_finish (resolver, result, error);
1012 * g_resolver_free_targets: (skip)
1013 * @targets: a #GList of #GSrvTarget
1015 * Frees @targets (which should be the return value from
1016 * g_resolver_lookup_service() or g_resolver_lookup_service_finish()).
1017 * (This is a convenience method; you can also simply free the
1023 g_resolver_free_targets (GList *targets)
1027 for (t = targets; t; t = t->next)
1028 g_srv_target_free (t->data);
1029 g_list_free (targets);
1033 * g_resolver_lookup_records:
1034 * @resolver: a #GResolver
1035 * @rrname: the DNS name to lookup the record for
1036 * @record_type: the type of DNS record to lookup
1037 * @cancellable: (nullable): a #GCancellable, or %NULL
1038 * @error: return location for a #GError, or %NULL
1040 * Synchronously performs a DNS record lookup for the given @rrname and returns
1041 * a list of records as #GVariant tuples. See #GResolverRecordType for
1042 * information on what the records contain for each @record_type.
1044 * If the DNS resolution fails, @error (if non-%NULL) will be set to
1045 * a value from #GResolverError and %NULL will be returned.
1047 * If @cancellable is non-%NULL, it can be used to cancel the
1048 * operation, in which case @error (if non-%NULL) will be set to
1049 * %G_IO_ERROR_CANCELLED.
1051 * Returns: (element-type GVariant) (transfer full): a non-empty #GList of
1052 * #GVariant, or %NULL on error. You must free each of the records and the list
1053 * when you are done with it. (You can use g_list_free_full() with
1054 * g_variant_unref() to do this.)
1059 g_resolver_lookup_records (GResolver *resolver,
1060 const gchar *rrname,
1061 GResolverRecordType record_type,
1062 GCancellable *cancellable,
1067 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
1068 g_return_val_if_fail (rrname != NULL, NULL);
1070 g_resolver_maybe_reload (resolver);
1071 records = G_RESOLVER_GET_CLASS (resolver)->
1072 lookup_records (resolver, rrname, record_type, cancellable, error);
1078 * g_resolver_lookup_records_async:
1079 * @resolver: a #GResolver
1080 * @rrname: the DNS name to lookup the record for
1081 * @record_type: the type of DNS record to lookup
1082 * @cancellable: (nullable): a #GCancellable, or %NULL
1083 * @callback: (scope async): callback to call after resolution completes
1084 * @user_data: (closure): data for @callback
1086 * Begins asynchronously performing a DNS lookup for the given
1087 * @rrname, and eventually calls @callback, which must call
1088 * g_resolver_lookup_records_finish() to get the final result. See
1089 * g_resolver_lookup_records() for more details.
1094 g_resolver_lookup_records_async (GResolver *resolver,
1095 const gchar *rrname,
1096 GResolverRecordType record_type,
1097 GCancellable *cancellable,
1098 GAsyncReadyCallback callback,
1101 g_return_if_fail (G_IS_RESOLVER (resolver));
1102 g_return_if_fail (rrname != NULL);
1104 g_resolver_maybe_reload (resolver);
1105 G_RESOLVER_GET_CLASS (resolver)->
1106 lookup_records_async (resolver, rrname, record_type, cancellable, callback, user_data);
1110 * g_resolver_lookup_records_finish:
1111 * @resolver: a #GResolver
1112 * @result: the result passed to your #GAsyncReadyCallback
1113 * @error: return location for a #GError, or %NULL
1115 * Retrieves the result of a previous call to
1116 * g_resolver_lookup_records_async(). Returns a non-empty list of records as
1117 * #GVariant tuples. See #GResolverRecordType for information on what the
1120 * If the DNS resolution failed, @error (if non-%NULL) will be set to
1121 * a value from #GResolverError. If the operation was cancelled,
1122 * @error will be set to %G_IO_ERROR_CANCELLED.
1124 * Returns: (element-type GVariant) (transfer full): a non-empty #GList of
1125 * #GVariant, or %NULL on error. You must free each of the records and the list
1126 * when you are done with it. (You can use g_list_free_full() with
1127 * g_variant_unref() to do this.)
1132 g_resolver_lookup_records_finish (GResolver *resolver,
1133 GAsyncResult *result,
1136 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
1137 return G_RESOLVER_GET_CLASS (resolver)->
1138 lookup_records_finish (resolver, result, error);
1142 g_resolver_get_serial (GResolver *resolver)
1144 g_return_val_if_fail (G_IS_RESOLVER (resolver), 0);
1146 g_resolver_maybe_reload (resolver);
1149 return (guint64) resolver->priv->resolv_conf_timestamp;
1156 * g_resolver_error_quark:
1158 * Gets the #GResolver Error Quark.
1160 * Returns: a #GQuark.
1164 G_DEFINE_QUARK (g-resolver-error-quark, g_resolver_error)