1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2008 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
27 #include "gresolver.h"
28 #include "gnetworkingprivate.h"
29 #include "gasyncresult.h"
30 #include "ginetaddress.h"
31 #include "ginetsocketaddress.h"
32 #include "gsimpleasyncresult.h"
34 #include "gsrvtarget.h"
35 #include "gthreadedresolver.h"
46 * @short_description: Asynchronous and cancellable DNS resolver
49 * #GResolver provides cancellable synchronous and asynchronous DNS
50 * resolution, for hostnames (g_resolver_lookup_by_address(),
51 * g_resolver_lookup_by_name() and their async variants) and SRV
52 * (service) records (g_resolver_lookup_service()).
54 * #GNetworkAddress and #GNetworkService provide wrappers around
55 * #GResolver functionality that also implement #GSocketConnectable,
56 * making it easy to connect to a remote host/service.
64 static guint signals[LAST_SIGNAL] = { 0 };
66 struct _GResolverPrivate {
68 time_t resolv_conf_timestamp;
77 * The object that handles DNS resolution. Use g_resolver_get_default()
78 * to get the default resolver.
80 G_DEFINE_TYPE (GResolver, g_resolver, G_TYPE_OBJECT)
83 srv_records_to_targets (GList *records)
85 const gchar *hostname;
86 guint16 port, priority, weight;
90 for (l = records; l != NULL; l = g_list_next (l))
92 g_variant_get (l->data, "(qqq&s)", &priority, &weight, &port, &hostname);
93 target = g_srv_target_new (hostname, port, priority, weight);
94 g_variant_unref (l->data);
98 return g_srv_target_list_sort (records);
102 g_resolver_real_lookup_service (GResolver *resolver,
104 GCancellable *cancellable,
109 records = G_RESOLVER_GET_CLASS (resolver)->lookup_records (resolver,
111 G_RESOLVER_RECORD_SRV,
115 return srv_records_to_targets (records);
119 g_resolver_real_lookup_service_async (GResolver *resolver,
121 GCancellable *cancellable,
122 GAsyncReadyCallback callback,
125 G_RESOLVER_GET_CLASS (resolver)->lookup_records_async (resolver,
127 G_RESOLVER_RECORD_SRV,
134 g_resolver_real_lookup_service_finish (GResolver *resolver,
135 GAsyncResult *result,
140 records = G_RESOLVER_GET_CLASS (resolver)->lookup_records_finish (resolver,
144 return srv_records_to_targets (records);
148 g_resolver_class_init (GResolverClass *resolver_class)
150 /* Automatically pass these over to the lookup_records methods */
151 resolver_class->lookup_service = g_resolver_real_lookup_service;
152 resolver_class->lookup_service_async = g_resolver_real_lookup_service_async;
153 resolver_class->lookup_service_finish = g_resolver_real_lookup_service_finish;
155 g_type_class_add_private (resolver_class, sizeof (GResolverPrivate));
157 /* Make sure _g_networking_init() has been called */
158 g_type_ensure (G_TYPE_INET_ADDRESS);
160 /* Initialize _g_resolver_addrinfo_hints */
162 _g_resolver_addrinfo_hints.ai_flags |= AI_ADDRCONFIG;
164 /* These two don't actually matter, they just get copied into the
165 * returned addrinfo structures (and then we ignore them). But if
166 * we leave them unset, we'll get back duplicate answers.
168 _g_resolver_addrinfo_hints.ai_socktype = SOCK_STREAM;
169 _g_resolver_addrinfo_hints.ai_protocol = IPPROTO_TCP;
173 * @resolver: a #GResolver
175 * Emitted when the resolver notices that the system resolver
176 * configuration has changed.
179 g_signal_new (I_("reload"),
182 G_STRUCT_OFFSET (GResolverClass, reload),
184 g_cclosure_marshal_VOID__VOID,
189 g_resolver_init (GResolver *resolver)
195 resolver->priv = G_TYPE_INSTANCE_GET_PRIVATE (resolver, G_TYPE_RESOLVER, GResolverPrivate);
198 if (stat (_PATH_RESCONF, &st) == 0)
199 resolver->priv->resolv_conf_timestamp = st.st_mtime;
203 static GResolver *default_resolver;
206 * g_resolver_get_default:
208 * Gets the default #GResolver. You should unref it when you are done
209 * with it. #GResolver may use its reference count as a hint about how
210 * many threads it should allocate for concurrent DNS resolutions.
212 * Return value: (transfer full): the default #GResolver.
217 g_resolver_get_default (void)
219 if (!default_resolver)
220 default_resolver = g_object_new (G_TYPE_THREADED_RESOLVER, NULL);
222 return g_object_ref (default_resolver);
226 * g_resolver_set_default:
227 * @resolver: the new default #GResolver
229 * Sets @resolver to be the application's default resolver (reffing
230 * @resolver, and unreffing the previous default resolver, if any).
231 * Future calls to g_resolver_get_default() will return this resolver.
233 * This can be used if an application wants to perform any sort of DNS
234 * caching or "pinning"; it can implement its own #GResolver that
235 * calls the original default resolver for DNS operations, and
236 * implements its own cache policies on top of that, and then set
237 * itself as the default resolver for all later code to use.
242 g_resolver_set_default (GResolver *resolver)
244 if (default_resolver)
245 g_object_unref (default_resolver);
246 default_resolver = g_object_ref (resolver);
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;
262 g_signal_emit (resolver, signals[RELOAD], 0);
268 /* filter out duplicates, cf. https://bugzilla.gnome.org/show_bug.cgi?id=631379 */
270 remove_duplicates (GList *addrs)
276 /* TODO: if this is too slow (it's O(n^2) but n is typically really
277 * small), we can do something more clever but note that we must not
278 * change the order of elements...
280 for (l = addrs; l != NULL; l = l->next)
282 GInetAddress *address = G_INET_ADDRESS (l->data);
283 for (ll = l->next; ll != NULL; ll = lll)
285 GInetAddress *other_address = G_INET_ADDRESS (ll->data);
287 if (g_inet_address_equal (address, other_address))
289 g_object_unref (other_address);
290 /* we never return the first element */
291 g_warn_if_fail (g_list_delete_link (addrs, ll) == addrs);
299 * g_resolver_lookup_by_name:
300 * @resolver: a #GResolver
301 * @hostname: the hostname to look up
302 * @cancellable: (allow-none): a #GCancellable, or %NULL
303 * @error: return location for a #GError, or %NULL
305 * Synchronously resolves @hostname to determine its associated IP
306 * address(es). @hostname may be an ASCII-only or UTF-8 hostname, or
307 * the textual form of an IP address (in which case this just becomes
308 * a wrapper around g_inet_address_new_from_string()).
310 * On success, g_resolver_lookup_by_name() will return a #GList of
311 * #GInetAddress, sorted in order of preference and guaranteed to not
312 * contain duplicates. That is, if using the result to connect to
313 * @hostname, you should attempt to connect to the first address
314 * first, then the second if the first fails, etc. If you are using
315 * the result to listen on a socket, it is appropriate to add each
316 * result using e.g. g_socket_listener_add_address().
318 * If the DNS resolution fails, @error (if non-%NULL) will be set to a
319 * value from #GResolverError.
321 * If @cancellable is non-%NULL, it can be used to cancel the
322 * operation, in which case @error (if non-%NULL) will be set to
323 * %G_IO_ERROR_CANCELLED.
325 * If you are planning to connect to a socket on the resolved IP
326 * address, it may be easier to create a #GNetworkAddress and use its
327 * #GSocketConnectable interface.
329 * Return value: (element-type GInetAddress) (transfer full): a #GList
330 * of #GInetAddress, or %NULL on error. You
331 * must unref each of the addresses and free the list when you are
332 * done with it. (You can use g_resolver_free_addresses() to do this.)
337 g_resolver_lookup_by_name (GResolver *resolver,
338 const gchar *hostname,
339 GCancellable *cancellable,
344 gchar *ascii_hostname = NULL;
346 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
347 g_return_val_if_fail (hostname != NULL, NULL);
349 /* Check if @hostname is just an IP address */
350 addr = g_inet_address_new_from_string (hostname);
352 return g_list_append (NULL, addr);
354 if (g_hostname_is_non_ascii (hostname))
355 hostname = ascii_hostname = g_hostname_to_ascii (hostname);
357 g_resolver_maybe_reload (resolver);
358 addrs = G_RESOLVER_GET_CLASS (resolver)->
359 lookup_by_name (resolver, hostname, cancellable, error);
361 remove_duplicates (addrs);
363 g_free (ascii_hostname);
368 * g_resolver_lookup_by_name_async:
369 * @resolver: a #GResolver
370 * @hostname: the hostname to look up the address of
371 * @cancellable: (allow-none): a #GCancellable, or %NULL
372 * @callback: (scope async): callback to call after resolution completes
373 * @user_data: (closure): data for @callback
375 * Begins asynchronously resolving @hostname to determine its
376 * associated IP address(es), and eventually calls @callback, which
377 * must call g_resolver_lookup_by_name_finish() to get the result.
378 * See g_resolver_lookup_by_name() for more details.
383 g_resolver_lookup_by_name_async (GResolver *resolver,
384 const gchar *hostname,
385 GCancellable *cancellable,
386 GAsyncReadyCallback callback,
390 gchar *ascii_hostname = NULL;
392 g_return_if_fail (G_IS_RESOLVER (resolver));
393 g_return_if_fail (hostname != NULL);
395 /* Check if @hostname is just an IP address */
396 addr = g_inet_address_new_from_string (hostname);
401 task = g_task_new (resolver, cancellable, callback, user_data);
402 g_task_set_source_tag (task, g_resolver_lookup_by_name_async);
403 g_task_return_pointer (task, g_list_append (NULL, addr),
404 (GDestroyNotify) g_resolver_free_addresses);
405 g_object_unref (task);
409 if (g_hostname_is_non_ascii (hostname))
410 hostname = ascii_hostname = g_hostname_to_ascii (hostname);
412 g_resolver_maybe_reload (resolver);
413 G_RESOLVER_GET_CLASS (resolver)->
414 lookup_by_name_async (resolver, hostname, cancellable, callback, user_data);
416 g_free (ascii_hostname);
420 * g_resolver_lookup_by_name_finish:
421 * @resolver: a #GResolver
422 * @result: the result passed to your #GAsyncReadyCallback
423 * @error: return location for a #GError, or %NULL
425 * Retrieves the result of a call to
426 * g_resolver_lookup_by_name_async().
428 * If the DNS resolution failed, @error (if non-%NULL) will be set to
429 * a value from #GResolverError. If the operation was cancelled,
430 * @error will be set to %G_IO_ERROR_CANCELLED.
432 * Return value: (element-type GInetAddress) (transfer full): a #GList
433 * of #GInetAddress, or %NULL on error. See g_resolver_lookup_by_name()
439 g_resolver_lookup_by_name_finish (GResolver *resolver,
440 GAsyncResult *result,
445 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
447 if (g_async_result_legacy_propagate_error (result, error))
449 else if (g_async_result_is_tagged (result, g_resolver_lookup_by_name_async))
451 /* Handle the stringified-IP-addr case */
452 return g_task_propagate_pointer (G_TASK (result), error);
455 addrs = G_RESOLVER_GET_CLASS (resolver)->
456 lookup_by_name_finish (resolver, result, error);
458 remove_duplicates (addrs);
464 * g_resolver_free_addresses: (skip)
465 * @addresses: a #GList of #GInetAddress
467 * Frees @addresses (which should be the return value from
468 * g_resolver_lookup_by_name() or g_resolver_lookup_by_name_finish()).
469 * (This is a convenience method; you can also simply free the results
475 g_resolver_free_addresses (GList *addresses)
479 for (a = addresses; a; a = a->next)
480 g_object_unref (a->data);
481 g_list_free (addresses);
485 * g_resolver_lookup_by_address:
486 * @resolver: a #GResolver
487 * @address: the address to reverse-resolve
488 * @cancellable: (allow-none): a #GCancellable, or %NULL
489 * @error: return location for a #GError, or %NULL
491 * Synchronously reverse-resolves @address to determine its
492 * associated hostname.
494 * If the DNS resolution fails, @error (if non-%NULL) will be set to
495 * a value from #GResolverError.
497 * If @cancellable is non-%NULL, it can be used to cancel the
498 * operation, in which case @error (if non-%NULL) will be set to
499 * %G_IO_ERROR_CANCELLED.
501 * Return value: a hostname (either ASCII-only, or in ASCII-encoded
502 * form), or %NULL on error.
507 g_resolver_lookup_by_address (GResolver *resolver,
508 GInetAddress *address,
509 GCancellable *cancellable,
512 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
513 g_return_val_if_fail (G_IS_INET_ADDRESS (address), NULL);
515 g_resolver_maybe_reload (resolver);
516 return G_RESOLVER_GET_CLASS (resolver)->
517 lookup_by_address (resolver, address, cancellable, error);
521 * g_resolver_lookup_by_address_async:
522 * @resolver: a #GResolver
523 * @address: the address to reverse-resolve
524 * @cancellable: (allow-none): a #GCancellable, or %NULL
525 * @callback: (scope async): callback to call after resolution completes
526 * @user_data: (closure): data for @callback
528 * Begins asynchronously reverse-resolving @address to determine its
529 * associated hostname, and eventually calls @callback, which must
530 * call g_resolver_lookup_by_address_finish() to get the final result.
535 g_resolver_lookup_by_address_async (GResolver *resolver,
536 GInetAddress *address,
537 GCancellable *cancellable,
538 GAsyncReadyCallback callback,
541 g_return_if_fail (G_IS_RESOLVER (resolver));
542 g_return_if_fail (G_IS_INET_ADDRESS (address));
544 g_resolver_maybe_reload (resolver);
545 G_RESOLVER_GET_CLASS (resolver)->
546 lookup_by_address_async (resolver, address, cancellable, callback, user_data);
550 * g_resolver_lookup_by_address_finish:
551 * @resolver: a #GResolver
552 * @result: the result passed to your #GAsyncReadyCallback
553 * @error: return location for a #GError, or %NULL
555 * Retrieves the result of a previous call to
556 * g_resolver_lookup_by_address_async().
558 * If the DNS resolution failed, @error (if non-%NULL) will be set to
559 * a value from #GResolverError. If the operation was cancelled,
560 * @error will be set to %G_IO_ERROR_CANCELLED.
562 * Return value: a hostname (either ASCII-only, or in ASCII-encoded
563 * form), or %NULL on error.
568 g_resolver_lookup_by_address_finish (GResolver *resolver,
569 GAsyncResult *result,
572 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
574 if (g_async_result_legacy_propagate_error (result, error))
577 return G_RESOLVER_GET_CLASS (resolver)->
578 lookup_by_address_finish (resolver, result, error);
582 g_resolver_get_service_rrname (const char *service,
583 const char *protocol,
586 gchar *rrname, *ascii_domain = NULL;
588 if (g_hostname_is_non_ascii (domain))
589 domain = ascii_domain = g_hostname_to_ascii (domain);
591 rrname = g_strdup_printf ("_%s._%s.%s", service, protocol, domain);
593 g_free (ascii_domain);
598 * g_resolver_lookup_service:
599 * @resolver: a #GResolver
600 * @service: the service type to look up (eg, "ldap")
601 * @protocol: the networking protocol to use for @service (eg, "tcp")
602 * @domain: the DNS domain to look up the service in
603 * @cancellable: (allow-none): a #GCancellable, or %NULL
604 * @error: return location for a #GError, or %NULL
606 * Synchronously performs a DNS SRV lookup for the given @service and
607 * @protocol in the given @domain and returns an array of #GSrvTarget.
608 * @domain may be an ASCII-only or UTF-8 hostname. Note also that the
609 * @service and @protocol arguments <emphasis>do not</emphasis>
610 * include the leading underscore that appears in the actual DNS
613 * On success, g_resolver_lookup_service() will return a #GList of
614 * #GSrvTarget, sorted in order of preference. (That is, you should
615 * attempt to connect to the first target first, then the second if
616 * the first fails, etc.)
618 * If the DNS resolution fails, @error (if non-%NULL) will be set to
619 * a value from #GResolverError.
621 * If @cancellable is non-%NULL, it can be used to cancel the
622 * operation, in which case @error (if non-%NULL) will be set to
623 * %G_IO_ERROR_CANCELLED.
625 * If you are planning to connect to the service, it is usually easier
626 * to create a #GNetworkService and use its #GSocketConnectable
629 * Return value: (element-type GSrvTarget) (transfer full): a #GList of #GSrvTarget,
630 * or %NULL on error. You must free each of the targets and the list when you are
631 * done with it. (You can use g_resolver_free_targets() to do this.)
636 g_resolver_lookup_service (GResolver *resolver,
637 const gchar *service,
638 const gchar *protocol,
640 GCancellable *cancellable,
646 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
647 g_return_val_if_fail (service != NULL, NULL);
648 g_return_val_if_fail (protocol != NULL, NULL);
649 g_return_val_if_fail (domain != NULL, NULL);
651 rrname = g_resolver_get_service_rrname (service, protocol, domain);
653 g_resolver_maybe_reload (resolver);
654 targets = G_RESOLVER_GET_CLASS (resolver)->
655 lookup_service (resolver, rrname, cancellable, error);
662 * g_resolver_lookup_service_async:
663 * @resolver: a #GResolver
664 * @service: the service type to look up (eg, "ldap")
665 * @protocol: the networking protocol to use for @service (eg, "tcp")
666 * @domain: the DNS domain to look up the service in
667 * @cancellable: (allow-none): a #GCancellable, or %NULL
668 * @callback: (scope async): callback to call after resolution completes
669 * @user_data: (closure): data for @callback
671 * Begins asynchronously performing a DNS SRV lookup for the given
672 * @service and @protocol in the given @domain, and eventually calls
673 * @callback, which must call g_resolver_lookup_service_finish() to
674 * get the final result. See g_resolver_lookup_service() for more
680 g_resolver_lookup_service_async (GResolver *resolver,
681 const gchar *service,
682 const gchar *protocol,
684 GCancellable *cancellable,
685 GAsyncReadyCallback callback,
690 g_return_if_fail (G_IS_RESOLVER (resolver));
691 g_return_if_fail (service != NULL);
692 g_return_if_fail (protocol != NULL);
693 g_return_if_fail (domain != NULL);
695 rrname = g_resolver_get_service_rrname (service, protocol, domain);
697 g_resolver_maybe_reload (resolver);
698 G_RESOLVER_GET_CLASS (resolver)->
699 lookup_service_async (resolver, rrname, cancellable, callback, user_data);
705 * g_resolver_lookup_service_finish:
706 * @resolver: a #GResolver
707 * @result: the result passed to your #GAsyncReadyCallback
708 * @error: return location for a #GError, or %NULL
710 * Retrieves the result of a previous call to
711 * g_resolver_lookup_service_async().
713 * If the DNS resolution failed, @error (if non-%NULL) will be set to
714 * a value from #GResolverError. If the operation was cancelled,
715 * @error will be set to %G_IO_ERROR_CANCELLED.
717 * Return value: (element-type GSrvTarget) (transfer full): a #GList of #GSrvTarget,
718 * or %NULL on error. See g_resolver_lookup_service() for more details.
723 g_resolver_lookup_service_finish (GResolver *resolver,
724 GAsyncResult *result,
727 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
729 if (g_async_result_legacy_propagate_error (result, error))
732 return G_RESOLVER_GET_CLASS (resolver)->
733 lookup_service_finish (resolver, result, error);
737 * g_resolver_free_targets: (skip)
738 * @targets: a #GList of #GSrvTarget
740 * Frees @targets (which should be the return value from
741 * g_resolver_lookup_service() or g_resolver_lookup_service_finish()).
742 * (This is a convenience method; you can also simply free the
748 g_resolver_free_targets (GList *targets)
752 for (t = targets; t; t = t->next)
753 g_srv_target_free (t->data);
754 g_list_free (targets);
758 * g_resolver_lookup_records:
759 * @resolver: a #GResolver
760 * @rrname: the DNS name to lookup the record for
761 * @record_type: the type of DNS record to lookup
762 * @cancellable: (allow-none): a #GCancellable, or %NULL
763 * @error: return location for a #GError, or %NULL
765 * Synchronously performs a DNS record lookup for the given @rrname and returns
766 * a list of records as #GVariant tuples. See #GResolverRecordType for
767 * information on what the records contain for each @record_type.
769 * If the DNS resolution fails, @error (if non-%NULL) will be set to
770 * a value from #GResolverError.
772 * If @cancellable is non-%NULL, it can be used to cancel the
773 * operation, in which case @error (if non-%NULL) will be set to
774 * %G_IO_ERROR_CANCELLED.
776 * Return value: (element-type GVariant) (transfer full): a #GList of #GVariant,
777 * or %NULL on error. You must free each of the records and the list when you are
778 * done with it. (You can use g_list_free_full() with g_variant_unref() to do this.)
783 g_resolver_lookup_records (GResolver *resolver,
785 GResolverRecordType record_type,
786 GCancellable *cancellable,
791 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
792 g_return_val_if_fail (rrname != NULL, NULL);
794 g_resolver_maybe_reload (resolver);
795 records = G_RESOLVER_GET_CLASS (resolver)->
796 lookup_records (resolver, rrname, record_type, cancellable, error);
802 * g_resolver_lookup_records_async:
803 * @resolver: a #GResolver
804 * @rrname: the DNS name to lookup the record for
805 * @record_type: the type of DNS record to lookup
806 * @cancellable: (allow-none): a #GCancellable, or %NULL
807 * @callback: (scope async): callback to call after resolution completes
808 * @user_data: (closure): data for @callback
810 * Begins asynchronously performing a DNS lookup for the given
811 * @rrname, and eventually calls @callback, which must call
812 * g_resolver_lookup_records_finish() to get the final result. See
813 * g_resolver_lookup_records() for more details.
818 g_resolver_lookup_records_async (GResolver *resolver,
820 GResolverRecordType record_type,
821 GCancellable *cancellable,
822 GAsyncReadyCallback callback,
825 g_return_if_fail (G_IS_RESOLVER (resolver));
826 g_return_if_fail (rrname != NULL);
828 g_resolver_maybe_reload (resolver);
829 G_RESOLVER_GET_CLASS (resolver)->
830 lookup_records_async (resolver, rrname, record_type, cancellable, callback, user_data);
834 * g_resolver_lookup_records_finish:
835 * @resolver: a #GResolver
836 * @result: the result passed to your #GAsyncReadyCallback
837 * @error: return location for a #GError, or %NULL
839 * Retrieves the result of a previous call to
840 * g_resolver_lookup_records_async(). Returns a list of records as #GVariant
841 * tuples. See #GResolverRecordType for information on what the records contain.
843 * If the DNS resolution failed, @error (if non-%NULL) will be set to
844 * a value from #GResolverError. If the operation was cancelled,
845 * @error will be set to %G_IO_ERROR_CANCELLED.
847 * Return value: (element-type GVariant) (transfer full): a #GList of #GVariant,
848 * or %NULL on error. You must free each of the records and the list when you are
849 * done with it. (You can use g_list_free_full() with g_variant_unref() to do this.)
854 g_resolver_lookup_records_finish (GResolver *resolver,
855 GAsyncResult *result,
858 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
859 return G_RESOLVER_GET_CLASS (resolver)->
860 lookup_records_finish (resolver, result, error);
864 * g_resolver_error_quark:
866 * Gets the #GResolver Error Quark.
868 * Return value: a #GQuark.
872 G_DEFINE_QUARK (g-resolver-error-quark, g_resolver_error)
874 static GResolverError
875 g_resolver_error_from_addrinfo_error (gint err)
880 #if defined(EAI_NODATA) && (EAI_NODATA != EAI_NONAME)
884 return G_RESOLVER_ERROR_NOT_FOUND;
887 return G_RESOLVER_ERROR_TEMPORARY_FAILURE;
890 return G_RESOLVER_ERROR_INTERNAL;
894 struct addrinfo _g_resolver_addrinfo_hints;
896 /* Private method to process a getaddrinfo() response. */
898 _g_resolver_addresses_from_addrinfo (const char *hostname,
899 struct addrinfo *res,
904 GSocketAddress *sockaddr;
910 g_set_error (error, G_RESOLVER_ERROR,
911 g_resolver_error_from_addrinfo_error (gai_retval),
912 _("Error resolving '%s': %s"),
913 hostname, gai_strerror (gai_retval));
917 g_return_val_if_fail (res != NULL, NULL);
920 for (ai = res; ai; ai = ai->ai_next)
922 sockaddr = g_socket_address_new_from_native (ai->ai_addr, ai->ai_addrlen);
923 if (!sockaddr || !G_IS_INET_SOCKET_ADDRESS (sockaddr))
926 addr = g_object_ref (g_inet_socket_address_get_address ((GInetSocketAddress *)sockaddr));
927 addrs = g_list_prepend (addrs, addr);
928 g_object_unref (sockaddr);
931 return g_list_reverse (addrs);
934 /* Private method to set up a getnameinfo() request */
936 _g_resolver_address_to_sockaddr (GInetAddress *address,
937 struct sockaddr_storage *sa,
940 GSocketAddress *sockaddr;
942 sockaddr = g_inet_socket_address_new (address, 0);
943 g_socket_address_to_native (sockaddr, (struct sockaddr *)sa, sizeof (*sa), NULL);
944 *len = g_socket_address_get_native_size (sockaddr);
945 g_object_unref (sockaddr);
948 /* Private method to process a getnameinfo() response. */
950 _g_resolver_name_from_nameinfo (GInetAddress *address,
959 phys = g_inet_address_to_string (address);
960 g_set_error (error, G_RESOLVER_ERROR,
961 g_resolver_error_from_addrinfo_error (gni_retval),
962 _("Error reverse-resolving '%s': %s"),
963 phys ? phys : "(unknown)", gai_strerror (gni_retval));
968 return g_strdup (name);
971 #if defined(G_OS_UNIX)
974 parse_short (guchar **p,
980 GETSHORT (*value, *p);
985 parse_long (guchar **p,
991 GETLONG (*value, *p);
996 parse_res_srv (guchar *answer,
1000 gchar namebuf[1024];
1001 guint16 priority, weight, port;
1004 if (!parse_short (&p, end, &priority) ||
1005 !parse_short (&p, end, &weight) ||
1006 !parse_short (&p, end, &port))
1009 n = dn_expand (answer, end, p, namebuf, sizeof (namebuf));
1014 return g_variant_new ("(qqqs)",
1022 parse_res_soa (guchar *answer,
1026 gchar mnamebuf[1024];
1027 gchar rnamebuf[1024];
1028 guint32 serial, refresh, retry, expire, ttl;
1031 n = dn_expand (answer, end, p, mnamebuf, sizeof (mnamebuf));
1036 n = dn_expand (answer, end, p, rnamebuf, sizeof (rnamebuf));
1041 if (!parse_long (&p, end, &serial) ||
1042 !parse_long (&p, end, &refresh) ||
1043 !parse_long (&p, end, &retry) ||
1044 !parse_long (&p, end, &expire) ||
1045 !parse_long (&p, end, &ttl))
1048 return g_variant_new ("(ssuuuuu)",
1059 parse_res_ns (guchar *answer,
1063 gchar namebuf[1024];
1066 n = dn_expand (answer, end, p, namebuf, sizeof (namebuf));
1070 return g_variant_new ("(s)", namebuf);
1074 parse_res_mx (guchar *answer,
1078 gchar namebuf[1024];
1082 if (!parse_short (&p, end, &preference))
1085 n = dn_expand (answer, end, p, namebuf, sizeof (namebuf));
1090 return g_variant_new ("(qs)",
1096 parse_res_txt (guchar *answer,
1104 array = g_ptr_array_new_with_free_func (g_free);
1110 g_ptr_array_add (array, g_strndup ((gchar *)p, len));
1114 record = g_variant_new ("(@as)",
1115 g_variant_new_strv ((const gchar **)array->pdata, array->len));
1116 g_ptr_array_free (array, TRUE);
1121 _g_resolver_record_type_to_rrtype (GResolverRecordType type)
1125 case G_RESOLVER_RECORD_SRV:
1127 case G_RESOLVER_RECORD_TXT:
1129 case G_RESOLVER_RECORD_SOA:
1131 case G_RESOLVER_RECORD_NS:
1133 case G_RESOLVER_RECORD_MX:
1136 g_return_val_if_reached (-1);
1139 /* Private method to process a res_query response into GSrvTargets */
1141 _g_resolver_records_from_res_query (const gchar *rrname,
1150 guint16 type, qclass, rdlength;
1159 GResolverError errnum;
1160 const gchar *format;
1162 if (len == 0 || herr == HOST_NOT_FOUND || herr == NO_DATA)
1164 errnum = G_RESOLVER_ERROR_NOT_FOUND;
1165 format = _("No DNS record of the requested type for '%s'");
1167 else if (herr == TRY_AGAIN)
1169 errnum = G_RESOLVER_ERROR_TEMPORARY_FAILURE;
1170 format = _("Temporarily unable to resolve '%s'");
1174 errnum = G_RESOLVER_ERROR_INTERNAL;
1175 format = _("Error resolving '%s'");
1178 g_set_error (error, G_RESOLVER_ERROR, errnum, format, rrname);
1184 header = (HEADER *)answer;
1185 p = answer + sizeof (HEADER);
1189 count = ntohs (header->qdcount);
1190 for (i = 0; i < count && p < end; i++)
1192 n = dn_skipname (p, end);
1199 /* Incomplete response */
1202 g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_TEMPORARY_FAILURE,
1203 _("Incomplete data received for '%s'"), rrname);
1208 count = ntohs (header->ancount);
1209 for (i = 0; i < count && p < end; i++)
1211 n = dn_skipname (p, end);
1216 if (!parse_short (&p, end, &type) ||
1217 !parse_short (&p, end, &qclass) ||
1218 !parse_long (&p, end, &ttl) ||
1219 !parse_short (&p, end, &rdlength))
1222 ttl = ttl; /* To avoid -Wunused-but-set-variable */
1224 if (p + rdlength > end)
1227 if (type == rrtype && qclass == C_IN)
1232 record = parse_res_srv (answer, end, p);
1235 record = parse_res_mx (answer, end, p);
1238 record = parse_res_soa (answer, end, p);
1241 record = parse_res_ns (answer, end, p);
1244 record = parse_res_txt (answer, p + rdlength, p);
1247 g_warn_if_reached ();
1253 records = g_list_prepend (records, record);
1259 /* Somehow got a truncated response */
1262 g_list_free_full (records, (GDestroyNotify)g_variant_unref);
1263 g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_TEMPORARY_FAILURE,
1264 _("Incomplete data received for '%s'"), rrname);
1271 #elif defined(G_OS_WIN32)
1273 parse_dns_srv (DNS_RECORD *rec)
1275 return g_variant_new ("(qqqs)",
1276 (guint16)rec->Data.SRV.wPriority,
1277 (guint16)rec->Data.SRV.wWeight,
1278 (guint16)rec->Data.SRV.wPort,
1279 rec->Data.SRV.pNameTarget);
1283 parse_dns_soa (DNS_RECORD *rec)
1285 return g_variant_new ("(ssuuuuu)",
1286 rec->Data.SOA.pNamePrimaryServer,
1287 rec->Data.SOA.pNameAdministrator,
1288 (guint32)rec->Data.SOA.dwSerialNo,
1289 (guint32)rec->Data.SOA.dwRefresh,
1290 (guint32)rec->Data.SOA.dwRetry,
1291 (guint32)rec->Data.SOA.dwExpire,
1292 (guint32)rec->Data.SOA.dwDefaultTtl);
1296 parse_dns_ns (DNS_RECORD *rec)
1298 return g_variant_new ("(s)", rec->Data.NS.pNameHost);
1302 parse_dns_mx (DNS_RECORD *rec)
1304 return g_variant_new ("(qs)",
1305 (guint16)rec->Data.MX.wPreference,
1306 rec->Data.MX.pNameExchange);
1310 parse_dns_txt (DNS_RECORD *rec)
1316 array = g_ptr_array_new ();
1317 for (i = 0; i < rec->Data.TXT.dwStringCount; i++)
1318 g_ptr_array_add (array, rec->Data.TXT.pStringArray[i]);
1319 record = g_variant_new ("(@as)",
1320 g_variant_new_strv ((const gchar **)array->pdata, array->len));
1321 g_ptr_array_free (array, TRUE);
1326 _g_resolver_record_type_to_dnstype (GResolverRecordType type)
1330 case G_RESOLVER_RECORD_SRV:
1331 return DNS_TYPE_SRV;
1332 case G_RESOLVER_RECORD_TXT:
1333 return DNS_TYPE_TEXT;
1334 case G_RESOLVER_RECORD_SOA:
1335 return DNS_TYPE_SOA;
1336 case G_RESOLVER_RECORD_NS:
1338 case G_RESOLVER_RECORD_MX:
1341 g_return_val_if_reached (-1);
1344 /* Private method to process a DnsQuery response into GVariants */
1346 _g_resolver_records_from_DnsQuery (const gchar *rrname,
1349 DNS_RECORD *results,
1356 if (status != ERROR_SUCCESS)
1358 GResolverError errnum;
1359 const gchar *format;
1361 if (status == DNS_ERROR_RCODE_NAME_ERROR)
1363 errnum = G_RESOLVER_ERROR_NOT_FOUND;
1364 format = _("No DNS record of the requested type for '%s'");
1366 else if (status == DNS_ERROR_RCODE_SERVER_FAILURE)
1368 errnum = G_RESOLVER_ERROR_TEMPORARY_FAILURE;
1369 format = _("Temporarily unable to resolve '%s'");
1373 errnum = G_RESOLVER_ERROR_INTERNAL;
1374 format = _("Error resolving '%s'");
1377 g_set_error (error, G_RESOLVER_ERROR, errnum, format, rrname);
1382 for (rec = results; rec; rec = rec->pNext)
1384 if (rec->wType != dnstype)
1389 record = parse_dns_srv (rec);
1392 record = parse_dns_soa (rec);
1395 record = parse_dns_ns (rec);
1398 record = parse_dns_mx (rec);
1401 record = parse_dns_txt (rec);
1404 g_warn_if_reached ();
1409 records = g_list_prepend (records, g_variant_ref_sink (record));