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"
33 #include "gsrvtarget.h"
34 #include "gthreadedresolver.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 G_DEFINE_TYPE (GResolver, g_resolver, G_TYPE_OBJECT)
82 srv_records_to_targets (GList *records)
84 const gchar *hostname;
85 guint16 port, priority, weight;
89 for (l = records; l != NULL; l = g_list_next (l))
91 g_variant_get (l->data, "(qqq&s)", &priority, &weight, &port, &hostname);
92 target = g_srv_target_new (hostname, port, priority, weight);
93 g_variant_unref (l->data);
97 return g_srv_target_list_sort (records);
101 g_resolver_real_lookup_service (GResolver *resolver,
103 GCancellable *cancellable,
108 records = G_RESOLVER_GET_CLASS (resolver)->lookup_records (resolver,
110 G_RESOLVER_RECORD_SRV,
114 return srv_records_to_targets (records);
118 g_resolver_real_lookup_service_async (GResolver *resolver,
120 GCancellable *cancellable,
121 GAsyncReadyCallback callback,
124 G_RESOLVER_GET_CLASS (resolver)->lookup_records_async (resolver,
126 G_RESOLVER_RECORD_SRV,
133 g_resolver_real_lookup_service_finish (GResolver *resolver,
134 GAsyncResult *result,
139 records = G_RESOLVER_GET_CLASS (resolver)->lookup_records_finish (resolver,
143 return srv_records_to_targets (records);
147 g_resolver_class_init (GResolverClass *resolver_class)
149 /* Automatically pass these over to the lookup_records methods */
150 resolver_class->lookup_service = g_resolver_real_lookup_service;
151 resolver_class->lookup_service_async = g_resolver_real_lookup_service_async;
152 resolver_class->lookup_service_finish = g_resolver_real_lookup_service_finish;
154 g_type_class_add_private (resolver_class, sizeof (GResolverPrivate));
156 /* Make sure _g_networking_init() has been called */
157 g_type_ensure (G_TYPE_INET_ADDRESS);
159 /* Initialize _g_resolver_addrinfo_hints */
161 _g_resolver_addrinfo_hints.ai_flags |= AI_ADDRCONFIG;
163 /* These two don't actually matter, they just get copied into the
164 * returned addrinfo structures (and then we ignore them). But if
165 * we leave them unset, we'll get back duplicate answers.
167 _g_resolver_addrinfo_hints.ai_socktype = SOCK_STREAM;
168 _g_resolver_addrinfo_hints.ai_protocol = IPPROTO_TCP;
172 * @resolver: a #GResolver
174 * Emitted when the resolver notices that the system resolver
175 * configuration has changed.
178 g_signal_new (I_("reload"),
181 G_STRUCT_OFFSET (GResolverClass, reload),
183 g_cclosure_marshal_VOID__VOID,
188 g_resolver_init (GResolver *resolver)
194 resolver->priv = G_TYPE_INSTANCE_GET_PRIVATE (resolver, G_TYPE_RESOLVER, GResolverPrivate);
197 if (stat (_PATH_RESCONF, &st) == 0)
198 resolver->priv->resolv_conf_timestamp = st.st_mtime;
202 static GResolver *default_resolver;
205 * g_resolver_get_default:
207 * Gets the default #GResolver. You should unref it when you are done
208 * with it. #GResolver may use its reference count as a hint about how
209 * many threads it should allocate for concurrent DNS resolutions.
211 * Return value: (transfer full): the default #GResolver.
216 g_resolver_get_default (void)
218 if (!default_resolver)
219 default_resolver = g_object_new (G_TYPE_THREADED_RESOLVER, NULL);
221 return g_object_ref (default_resolver);
225 * g_resolver_set_default:
226 * @resolver: the new default #GResolver
228 * Sets @resolver to be the application's default resolver (reffing
229 * @resolver, and unreffing the previous default resolver, if any).
230 * Future calls to g_resolver_get_default() will return this resolver.
232 * This can be used if an application wants to perform any sort of DNS
233 * caching or "pinning"; it can implement its own #GResolver that
234 * calls the original default resolver for DNS operations, and
235 * implements its own cache policies on top of that, and then set
236 * itself as the default resolver for all later code to use.
241 g_resolver_set_default (GResolver *resolver)
243 if (default_resolver)
244 g_object_unref (default_resolver);
245 default_resolver = g_object_ref (resolver);
250 g_resolver_maybe_reload (GResolver *resolver)
255 if (stat (_PATH_RESCONF, &st) == 0)
257 if (st.st_mtime != resolver->priv->resolv_conf_timestamp)
259 resolver->priv->resolv_conf_timestamp = st.st_mtime;
261 g_signal_emit (resolver, signals[RELOAD], 0);
267 /* filter out duplicates, cf. https://bugzilla.gnome.org/show_bug.cgi?id=631379 */
269 remove_duplicates (GList *addrs)
275 /* TODO: if this is too slow (it's O(n^2) but n is typically really
276 * small), we can do something more clever but note that we must not
277 * change the order of elements...
279 for (l = addrs; l != NULL; l = l->next)
281 GInetAddress *address = G_INET_ADDRESS (l->data);
282 for (ll = l->next; ll != NULL; ll = lll)
284 GInetAddress *other_address = G_INET_ADDRESS (ll->data);
286 if (g_inet_address_equal (address, other_address))
288 g_object_unref (other_address);
289 /* we never return the first element */
290 g_warn_if_fail (g_list_delete_link (addrs, ll) == addrs);
298 * g_resolver_lookup_by_name:
299 * @resolver: a #GResolver
300 * @hostname: the hostname to look up
301 * @cancellable: (allow-none): a #GCancellable, or %NULL
302 * @error: return location for a #GError, or %NULL
304 * Synchronously resolves @hostname to determine its associated IP
305 * address(es). @hostname may be an ASCII-only or UTF-8 hostname, or
306 * the textual form of an IP address (in which case this just becomes
307 * a wrapper around g_inet_address_new_from_string()).
309 * On success, g_resolver_lookup_by_name() will return a #GList of
310 * #GInetAddress, sorted in order of preference and guaranteed to not
311 * contain duplicates. That is, if using the result to connect to
312 * @hostname, you should attempt to connect to the first address
313 * first, then the second if the first fails, etc. If you are using
314 * the result to listen on a socket, it is appropriate to add each
315 * result using e.g. g_socket_listener_add_address().
317 * If the DNS resolution fails, @error (if non-%NULL) will be set to a
318 * value from #GResolverError.
320 * If @cancellable is non-%NULL, it can be used to cancel the
321 * operation, in which case @error (if non-%NULL) will be set to
322 * %G_IO_ERROR_CANCELLED.
324 * If you are planning to connect to a socket on the resolved IP
325 * address, it may be easier to create a #GNetworkAddress and use its
326 * #GSocketConnectable interface.
328 * Return value: (element-type GInetAddress) (transfer full): a #GList
329 * of #GInetAddress, or %NULL on error. You
330 * must unref each of the addresses and free the list when you are
331 * done with it. (You can use g_resolver_free_addresses() to do this.)
336 g_resolver_lookup_by_name (GResolver *resolver,
337 const gchar *hostname,
338 GCancellable *cancellable,
343 gchar *ascii_hostname = NULL;
345 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
346 g_return_val_if_fail (hostname != NULL, NULL);
348 /* Check if @hostname is just an IP address */
349 addr = g_inet_address_new_from_string (hostname);
351 return g_list_append (NULL, addr);
353 if (g_hostname_is_non_ascii (hostname))
354 hostname = ascii_hostname = g_hostname_to_ascii (hostname);
356 g_resolver_maybe_reload (resolver);
357 addrs = G_RESOLVER_GET_CLASS (resolver)->
358 lookup_by_name (resolver, hostname, cancellable, error);
360 remove_duplicates (addrs);
362 g_free (ascii_hostname);
367 * g_resolver_lookup_by_name_async:
368 * @resolver: a #GResolver
369 * @hostname: the hostname to look up the address of
370 * @cancellable: (allow-none): a #GCancellable, or %NULL
371 * @callback: (scope async): callback to call after resolution completes
372 * @user_data: (closure): data for @callback
374 * Begins asynchronously resolving @hostname to determine its
375 * associated IP address(es), and eventually calls @callback, which
376 * must call g_resolver_lookup_by_name_finish() to get the result.
377 * See g_resolver_lookup_by_name() for more details.
382 g_resolver_lookup_by_name_async (GResolver *resolver,
383 const gchar *hostname,
384 GCancellable *cancellable,
385 GAsyncReadyCallback callback,
389 gchar *ascii_hostname = NULL;
391 g_return_if_fail (G_IS_RESOLVER (resolver));
392 g_return_if_fail (hostname != NULL);
394 /* Check if @hostname is just an IP address */
395 addr = g_inet_address_new_from_string (hostname);
398 GSimpleAsyncResult *simple;
400 simple = g_simple_async_result_new (G_OBJECT (resolver),
402 g_resolver_lookup_by_name_async);
404 g_simple_async_result_set_op_res_gpointer (simple, addr, g_object_unref);
405 g_simple_async_result_complete_in_idle (simple);
406 g_object_unref (simple);
410 if (g_hostname_is_non_ascii (hostname))
411 hostname = ascii_hostname = g_hostname_to_ascii (hostname);
413 g_resolver_maybe_reload (resolver);
414 G_RESOLVER_GET_CLASS (resolver)->
415 lookup_by_name_async (resolver, hostname, cancellable, callback, user_data);
417 g_free (ascii_hostname);
421 * g_resolver_lookup_by_name_finish:
422 * @resolver: a #GResolver
423 * @result: the result passed to your #GAsyncReadyCallback
424 * @error: return location for a #GError, or %NULL
426 * Retrieves the result of a call to
427 * g_resolver_lookup_by_name_async().
429 * If the DNS resolution failed, @error (if non-%NULL) will be set to
430 * a value from #GResolverError. If the operation was cancelled,
431 * @error will be set to %G_IO_ERROR_CANCELLED.
433 * Return value: (element-type GInetAddress) (transfer full): a #GList
434 * of #GInetAddress, or %NULL on error. See g_resolver_lookup_by_name()
440 g_resolver_lookup_by_name_finish (GResolver *resolver,
441 GAsyncResult *result,
446 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
448 if (G_IS_SIMPLE_ASYNC_RESULT (result))
450 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
452 if (g_simple_async_result_propagate_error (simple, error))
455 /* Handle the stringified-IP-addr case */
456 if (g_simple_async_result_get_source_tag (simple) == g_resolver_lookup_by_name_async)
460 addr = g_simple_async_result_get_op_res_gpointer (simple);
461 return g_list_append (NULL, g_object_ref (addr));
465 addrs = G_RESOLVER_GET_CLASS (resolver)->
466 lookup_by_name_finish (resolver, result, error);
468 remove_duplicates (addrs);
474 * g_resolver_free_addresses: (skip)
475 * @addresses: a #GList of #GInetAddress
477 * Frees @addresses (which should be the return value from
478 * g_resolver_lookup_by_name() or g_resolver_lookup_by_name_finish()).
479 * (This is a convenience method; you can also simply free the results
485 g_resolver_free_addresses (GList *addresses)
489 for (a = addresses; a; a = a->next)
490 g_object_unref (a->data);
491 g_list_free (addresses);
495 * g_resolver_lookup_by_address:
496 * @resolver: a #GResolver
497 * @address: the address to reverse-resolve
498 * @cancellable: (allow-none): a #GCancellable, or %NULL
499 * @error: return location for a #GError, or %NULL
501 * Synchronously reverse-resolves @address to determine its
502 * associated hostname.
504 * If the DNS resolution fails, @error (if non-%NULL) will be set to
505 * a value from #GResolverError.
507 * If @cancellable is non-%NULL, it can be used to cancel the
508 * operation, in which case @error (if non-%NULL) will be set to
509 * %G_IO_ERROR_CANCELLED.
511 * Return value: a hostname (either ASCII-only, or in ASCII-encoded
512 * form), or %NULL on error.
517 g_resolver_lookup_by_address (GResolver *resolver,
518 GInetAddress *address,
519 GCancellable *cancellable,
522 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
523 g_return_val_if_fail (G_IS_INET_ADDRESS (address), NULL);
525 g_resolver_maybe_reload (resolver);
526 return G_RESOLVER_GET_CLASS (resolver)->
527 lookup_by_address (resolver, address, cancellable, error);
531 * g_resolver_lookup_by_address_async:
532 * @resolver: a #GResolver
533 * @address: the address to reverse-resolve
534 * @cancellable: (allow-none): a #GCancellable, or %NULL
535 * @callback: (scope async): callback to call after resolution completes
536 * @user_data: (closure): data for @callback
538 * Begins asynchronously reverse-resolving @address to determine its
539 * associated hostname, and eventually calls @callback, which must
540 * call g_resolver_lookup_by_address_finish() to get the final result.
545 g_resolver_lookup_by_address_async (GResolver *resolver,
546 GInetAddress *address,
547 GCancellable *cancellable,
548 GAsyncReadyCallback callback,
551 g_return_if_fail (G_IS_RESOLVER (resolver));
552 g_return_if_fail (G_IS_INET_ADDRESS (address));
554 g_resolver_maybe_reload (resolver);
555 G_RESOLVER_GET_CLASS (resolver)->
556 lookup_by_address_async (resolver, address, cancellable, callback, user_data);
560 * g_resolver_lookup_by_address_finish:
561 * @resolver: a #GResolver
562 * @result: the result passed to your #GAsyncReadyCallback
563 * @error: return location for a #GError, or %NULL
565 * Retrieves the result of a previous call to
566 * g_resolver_lookup_by_address_async().
568 * If the DNS resolution failed, @error (if non-%NULL) will be set to
569 * a value from #GResolverError. If the operation was cancelled,
570 * @error will be set to %G_IO_ERROR_CANCELLED.
572 * Return value: a hostname (either ASCII-only, or in ASCII-encoded
573 * form), or %NULL on error.
578 g_resolver_lookup_by_address_finish (GResolver *resolver,
579 GAsyncResult *result,
582 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
584 if (G_IS_SIMPLE_ASYNC_RESULT (result))
586 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
588 if (g_simple_async_result_propagate_error (simple, error))
592 return G_RESOLVER_GET_CLASS (resolver)->
593 lookup_by_address_finish (resolver, result, error);
597 g_resolver_get_service_rrname (const char *service,
598 const char *protocol,
601 gchar *rrname, *ascii_domain = NULL;
603 if (g_hostname_is_non_ascii (domain))
604 domain = ascii_domain = g_hostname_to_ascii (domain);
606 rrname = g_strdup_printf ("_%s._%s.%s", service, protocol, domain);
608 g_free (ascii_domain);
613 * g_resolver_lookup_service:
614 * @resolver: a #GResolver
615 * @service: the service type to look up (eg, "ldap")
616 * @protocol: the networking protocol to use for @service (eg, "tcp")
617 * @domain: the DNS domain to look up the service in
618 * @cancellable: (allow-none): a #GCancellable, or %NULL
619 * @error: return location for a #GError, or %NULL
621 * Synchronously performs a DNS SRV lookup for the given @service and
622 * @protocol in the given @domain and returns an array of #GSrvTarget.
623 * @domain may be an ASCII-only or UTF-8 hostname. Note also that the
624 * @service and @protocol arguments <emphasis>do not</emphasis>
625 * include the leading underscore that appears in the actual DNS
628 * On success, g_resolver_lookup_service() will return a #GList of
629 * #GSrvTarget, sorted in order of preference. (That is, you should
630 * attempt to connect to the first target first, then the second if
631 * the first fails, etc.)
633 * If the DNS resolution fails, @error (if non-%NULL) will be set to
634 * a value from #GResolverError.
636 * If @cancellable is non-%NULL, it can be used to cancel the
637 * operation, in which case @error (if non-%NULL) will be set to
638 * %G_IO_ERROR_CANCELLED.
640 * If you are planning to connect to the service, it is usually easier
641 * to create a #GNetworkService and use its #GSocketConnectable
644 * Return value: (element-type GSrvTarget) (transfer full): a #GList of #GSrvTarget,
645 * or %NULL on error. You must free each of the targets and the list when you are
646 * done with it. (You can use g_resolver_free_targets() to do this.)
651 g_resolver_lookup_service (GResolver *resolver,
652 const gchar *service,
653 const gchar *protocol,
655 GCancellable *cancellable,
661 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
662 g_return_val_if_fail (service != NULL, NULL);
663 g_return_val_if_fail (protocol != NULL, NULL);
664 g_return_val_if_fail (domain != NULL, NULL);
666 rrname = g_resolver_get_service_rrname (service, protocol, domain);
668 g_resolver_maybe_reload (resolver);
669 targets = G_RESOLVER_GET_CLASS (resolver)->
670 lookup_service (resolver, rrname, cancellable, error);
677 * g_resolver_lookup_service_async:
678 * @resolver: a #GResolver
679 * @service: the service type to look up (eg, "ldap")
680 * @protocol: the networking protocol to use for @service (eg, "tcp")
681 * @domain: the DNS domain to look up the service in
682 * @cancellable: (allow-none): a #GCancellable, or %NULL
683 * @callback: (scope async): callback to call after resolution completes
684 * @user_data: (closure): data for @callback
686 * Begins asynchronously performing a DNS SRV lookup for the given
687 * @service and @protocol in the given @domain, and eventually calls
688 * @callback, which must call g_resolver_lookup_service_finish() to
689 * get the final result. See g_resolver_lookup_service() for more
695 g_resolver_lookup_service_async (GResolver *resolver,
696 const gchar *service,
697 const gchar *protocol,
699 GCancellable *cancellable,
700 GAsyncReadyCallback callback,
705 g_return_if_fail (G_IS_RESOLVER (resolver));
706 g_return_if_fail (service != NULL);
707 g_return_if_fail (protocol != NULL);
708 g_return_if_fail (domain != NULL);
710 rrname = g_resolver_get_service_rrname (service, protocol, domain);
712 g_resolver_maybe_reload (resolver);
713 G_RESOLVER_GET_CLASS (resolver)->
714 lookup_service_async (resolver, rrname, cancellable, callback, user_data);
720 * g_resolver_lookup_service_finish:
721 * @resolver: a #GResolver
722 * @result: the result passed to your #GAsyncReadyCallback
723 * @error: return location for a #GError, or %NULL
725 * Retrieves the result of a previous call to
726 * g_resolver_lookup_service_async().
728 * If the DNS resolution failed, @error (if non-%NULL) will be set to
729 * a value from #GResolverError. If the operation was cancelled,
730 * @error will be set to %G_IO_ERROR_CANCELLED.
732 * Return value: (element-type GSrvTarget) (transfer full): a #GList of #GSrvTarget,
733 * or %NULL on error. See g_resolver_lookup_service() for more details.
738 g_resolver_lookup_service_finish (GResolver *resolver,
739 GAsyncResult *result,
742 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
744 if (G_IS_SIMPLE_ASYNC_RESULT (result))
746 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
748 if (g_simple_async_result_propagate_error (simple, error))
752 return G_RESOLVER_GET_CLASS (resolver)->
753 lookup_service_finish (resolver, result, error);
757 * g_resolver_free_targets: (skip)
758 * @targets: a #GList of #GSrvTarget
760 * Frees @targets (which should be the return value from
761 * g_resolver_lookup_service() or g_resolver_lookup_service_finish()).
762 * (This is a convenience method; you can also simply free the
768 g_resolver_free_targets (GList *targets)
772 for (t = targets; t; t = t->next)
773 g_srv_target_free (t->data);
774 g_list_free (targets);
778 * g_resolver_lookup_records:
779 * @resolver: a #GResolver
780 * @rrname: the DNS name to lookup the record for
781 * @record_type: the type of DNS record to lookup
782 * @cancellable: (allow-none): a #GCancellable, or %NULL
783 * @error: return location for a #GError, or %NULL
785 * Synchronously performs a DNS record lookup for the given @rrname and returns
786 * a list of records as #GVariant tuples. See #GResolverRecordType for
787 * information on what the records contain for each @record_type.
789 * If the DNS resolution fails, @error (if non-%NULL) will be set to
790 * a value from #GResolverError.
792 * If @cancellable is non-%NULL, it can be used to cancel the
793 * operation, in which case @error (if non-%NULL) will be set to
794 * %G_IO_ERROR_CANCELLED.
796 * Return value: (element-type GVariant) (transfer full): a #GList of #GVariant,
797 * or %NULL on error. You must free each of the records and the list when you are
798 * done with it. (You can use g_list_free_full() with g_variant_unref() to do this.)
803 g_resolver_lookup_records (GResolver *resolver,
805 GResolverRecordType record_type,
806 GCancellable *cancellable,
811 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
812 g_return_val_if_fail (rrname != NULL, NULL);
814 g_resolver_maybe_reload (resolver);
815 records = G_RESOLVER_GET_CLASS (resolver)->
816 lookup_records (resolver, rrname, record_type, cancellable, error);
822 * g_resolver_lookup_records_async:
823 * @resolver: a #GResolver
824 * @rrname: the DNS name to lookup the record for
825 * @record_type: the type of DNS record to lookup
826 * @cancellable: (allow-none): a #GCancellable, or %NULL
827 * @callback: (scope async): callback to call after resolution completes
828 * @user_data: (closure): data for @callback
830 * Begins asynchronously performing a DNS lookup for the given
831 * @rrname, and eventually calls @callback, which must call
832 * g_resolver_lookup_records_finish() to get the final result. See
833 * g_resolver_lookup_records() for more details.
838 g_resolver_lookup_records_async (GResolver *resolver,
840 GResolverRecordType record_type,
841 GCancellable *cancellable,
842 GAsyncReadyCallback callback,
845 g_return_if_fail (G_IS_RESOLVER (resolver));
846 g_return_if_fail (rrname != NULL);
848 g_resolver_maybe_reload (resolver);
849 G_RESOLVER_GET_CLASS (resolver)->
850 lookup_records_async (resolver, rrname, record_type, cancellable, callback, user_data);
854 * g_resolver_lookup_records_finish:
855 * @resolver: a #GResolver
856 * @result: the result passed to your #GAsyncReadyCallback
857 * @error: return location for a #GError, or %NULL
859 * Retrieves the result of a previous call to
860 * g_resolver_lookup_records_async(). Returns a list of records as #GVariant
861 * tuples. See #GResolverRecordType for information on what the records contain.
863 * If the DNS resolution failed, @error (if non-%NULL) will be set to
864 * a value from #GResolverError. If the operation was cancelled,
865 * @error will be set to %G_IO_ERROR_CANCELLED.
867 * Return value: (element-type GVariant) (transfer full): a #GList of #GVariant,
868 * or %NULL on error. You must free each of the records and the list when you are
869 * done with it. (You can use g_list_free_full() with g_variant_unref() to do this.)
874 g_resolver_lookup_records_finish (GResolver *resolver,
875 GAsyncResult *result,
878 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
879 return G_RESOLVER_GET_CLASS (resolver)->
880 lookup_records_finish (resolver, result, error);
884 * g_resolver_error_quark:
886 * Gets the #GResolver Error Quark.
888 * Return value: a #GQuark.
893 g_resolver_error_quark (void)
895 return g_quark_from_static_string ("g-resolver-error-quark");
899 static GResolverError
900 g_resolver_error_from_addrinfo_error (gint err)
905 #if defined(EAI_NODATA) && (EAI_NODATA != EAI_NONAME)
909 return G_RESOLVER_ERROR_NOT_FOUND;
912 return G_RESOLVER_ERROR_TEMPORARY_FAILURE;
915 return G_RESOLVER_ERROR_INTERNAL;
919 struct addrinfo _g_resolver_addrinfo_hints;
921 /* Private method to process a getaddrinfo() response. */
923 _g_resolver_addresses_from_addrinfo (const char *hostname,
924 struct addrinfo *res,
929 GSocketAddress *sockaddr;
935 g_set_error (error, G_RESOLVER_ERROR,
936 g_resolver_error_from_addrinfo_error (gai_retval),
937 _("Error resolving '%s': %s"),
938 hostname, gai_strerror (gai_retval));
942 g_return_val_if_fail (res != NULL, NULL);
945 for (ai = res; ai; ai = ai->ai_next)
947 sockaddr = g_socket_address_new_from_native (ai->ai_addr, ai->ai_addrlen);
948 if (!sockaddr || !G_IS_INET_SOCKET_ADDRESS (sockaddr))
951 addr = g_object_ref (g_inet_socket_address_get_address ((GInetSocketAddress *)sockaddr));
952 addrs = g_list_prepend (addrs, addr);
953 g_object_unref (sockaddr);
956 return g_list_reverse (addrs);
959 /* Private method to set up a getnameinfo() request */
961 _g_resolver_address_to_sockaddr (GInetAddress *address,
962 struct sockaddr_storage *sa,
965 GSocketAddress *sockaddr;
967 sockaddr = g_inet_socket_address_new (address, 0);
968 g_socket_address_to_native (sockaddr, (struct sockaddr *)sa, sizeof (*sa), NULL);
969 *len = g_socket_address_get_native_size (sockaddr);
970 g_object_unref (sockaddr);
973 /* Private method to process a getnameinfo() response. */
975 _g_resolver_name_from_nameinfo (GInetAddress *address,
984 phys = g_inet_address_to_string (address);
985 g_set_error (error, G_RESOLVER_ERROR,
986 g_resolver_error_from_addrinfo_error (gni_retval),
987 _("Error reverse-resolving '%s': %s"),
988 phys ? phys : "(unknown)", gai_strerror (gni_retval));
993 return g_strdup (name);
996 #if defined(G_OS_UNIX)
998 parse_res_srv (guchar *answer,
1002 gchar namebuf[1024];
1003 guint16 priority, weight, port;
1005 GETSHORT (priority, *p);
1006 GETSHORT (weight, *p);
1007 GETSHORT (port, *p);
1008 *p += dn_expand (answer, end, *p, namebuf, sizeof (namebuf));
1010 return g_variant_new ("(qqqs)",
1018 parse_res_soa (guchar *answer,
1022 gchar mnamebuf[1024];
1023 gchar rnamebuf[1024];
1024 guint32 serial, refresh, retry, expire, ttl;
1026 *p += dn_expand (answer, end, *p, mnamebuf, sizeof (mnamebuf));
1027 *p += dn_expand (answer, end, *p, rnamebuf, sizeof (rnamebuf));
1029 GETLONG (serial, *p);
1030 GETLONG (refresh, *p);
1031 GETLONG (retry, *p);
1032 GETLONG (expire, *p);
1035 return g_variant_new ("(ssuuuuu)",
1046 parse_res_ns (guchar *answer,
1050 gchar namebuf[1024];
1052 *p += dn_expand (answer, end, *p, namebuf, sizeof (namebuf));
1054 return g_variant_new ("(s)", namebuf);
1058 parse_res_mx (guchar *answer,
1062 gchar namebuf[1024];
1065 GETSHORT (preference, *p);
1067 *p += dn_expand (answer, end, *p, namebuf, sizeof (namebuf));
1069 return g_variant_new ("(qs)",
1075 parse_res_txt (guchar *answer,
1084 array = g_ptr_array_new_with_free_func (g_free);
1090 g_ptr_array_add (array, g_strndup ((gchar *)at, len));
1095 record = g_variant_new ("(@as)",
1096 g_variant_new_strv ((const gchar **)array->pdata, array->len));
1097 g_ptr_array_free (array, TRUE);
1102 _g_resolver_record_type_to_rrtype (GResolverRecordType type)
1106 case G_RESOLVER_RECORD_SRV:
1108 case G_RESOLVER_RECORD_TXT:
1110 case G_RESOLVER_RECORD_SOA:
1112 case G_RESOLVER_RECORD_NS:
1114 case G_RESOLVER_RECORD_MX:
1117 g_return_val_if_reached (-1);
1120 /* Private method to process a res_query response into GSrvTargets */
1122 _g_resolver_records_from_res_query (const gchar *rrname,
1130 gchar namebuf[1024];
1132 guint16 type, qclass, rdlength;
1140 GResolverError errnum;
1141 const gchar *format;
1143 if (len == 0 || herr == HOST_NOT_FOUND || herr == NO_DATA)
1145 errnum = G_RESOLVER_ERROR_NOT_FOUND;
1146 format = _("No DNS record of the requested type for '%s'");
1148 else if (herr == TRY_AGAIN)
1150 errnum = G_RESOLVER_ERROR_TEMPORARY_FAILURE;
1151 format = _("Temporarily unable to resolve '%s'");
1155 errnum = G_RESOLVER_ERROR_INTERNAL;
1156 format = _("Error resolving '%s'");
1159 g_set_error (error, G_RESOLVER_ERROR, errnum, format, rrname);
1165 header = (HEADER *)answer;
1166 p = answer + sizeof (HEADER);
1170 count = ntohs (header->qdcount);
1171 while (count-- && p < end)
1173 p += dn_expand (answer, end, p, namebuf, sizeof (namebuf));
1176 /* To silence gcc warnings */
1177 namebuf[0] = namebuf[1];
1181 count = ntohs (header->ancount);
1182 while (count-- && p < end)
1184 p += dn_expand (answer, end, p, namebuf, sizeof (namebuf));
1186 GETSHORT (qclass, p);
1188 ttl = ttl; /* To avoid -Wunused-but-set-variable */
1189 GETSHORT (rdlength, p);
1191 if (type != rrtype || qclass != C_IN)
1200 record = parse_res_srv (answer, end, &p);
1203 record = parse_res_mx (answer, end, &p);
1206 record = parse_res_soa (answer, end, &p);
1209 record = parse_res_ns (answer, end, &p);
1212 record = parse_res_txt (answer, p + rdlength, &p);
1215 g_warn_if_reached ();
1221 records = g_list_prepend (records, record);
1227 #elif defined(G_OS_WIN32)
1229 parse_dns_srv (DNS_RECORD *rec)
1231 return g_variant_new ("(qqqs)",
1232 (guint16)rec->Data.SRV.wPriority,
1233 (guint16)rec->Data.SRV.wWeight,
1234 (guint16)rec->Data.SRV.wPort,
1235 rec->Data.SRV.pNameTarget);
1239 parse_dns_soa (DNS_RECORD *rec)
1241 return g_variant_new ("(ssuuuuu)",
1242 rec->Data.SOA.pNamePrimaryServer,
1243 rec->Data.SOA.pNameAdministrator,
1244 (guint32)rec->Data.SOA.dwSerialNo,
1245 (guint32)rec->Data.SOA.dwRefresh,
1246 (guint32)rec->Data.SOA.dwRetry,
1247 (guint32)rec->Data.SOA.dwExpire,
1248 (guint32)rec->Data.SOA.dwDefaultTtl);
1252 parse_dns_ns (DNS_RECORD *rec)
1254 return g_variant_new ("(s)", rec->Data.NS.pNameHost);
1258 parse_dns_mx (DNS_RECORD *rec)
1260 return g_variant_new ("(qs)",
1261 (guint16)rec->Data.MX.wPreference,
1262 rec->Data.MX.pNameExchange);
1266 parse_dns_txt (DNS_RECORD *rec)
1272 array = g_ptr_array_new ();
1273 for (i = 0; i < rec->Data.TXT.dwStringCount; i++)
1274 g_ptr_array_add (array, rec->Data.TXT.pStringArray[i]);
1275 record = g_variant_new ("(@as)",
1276 g_variant_new_strv ((const gchar **)array->pdata, array->len));
1277 g_ptr_array_free (array, TRUE);
1282 _g_resolver_record_type_to_dnstype (GResolverRecordType type)
1286 case G_RESOLVER_RECORD_SRV:
1287 return DNS_TYPE_SRV;
1288 case G_RESOLVER_RECORD_TXT:
1289 return DNS_TYPE_TEXT;
1290 case G_RESOLVER_RECORD_SOA:
1291 return DNS_TYPE_SOA;
1292 case G_RESOLVER_RECORD_NS:
1294 case G_RESOLVER_RECORD_MX:
1297 g_return_val_if_reached (-1);
1300 /* Private method to process a DnsQuery response into GVariants */
1302 _g_resolver_records_from_DnsQuery (const gchar *rrname,
1305 DNS_RECORD *results,
1312 if (status != ERROR_SUCCESS)
1314 GResolverError errnum;
1315 const gchar *format;
1317 if (status == DNS_ERROR_RCODE_NAME_ERROR)
1319 errnum = G_RESOLVER_ERROR_NOT_FOUND;
1320 format = _("No DNS record of the requested type for '%s'");
1322 else if (status == DNS_ERROR_RCODE_SERVER_FAILURE)
1324 errnum = G_RESOLVER_ERROR_TEMPORARY_FAILURE;
1325 format = _("Temporarily unable to resolve '%s'");
1329 errnum = G_RESOLVER_ERROR_INTERNAL;
1330 format = _("Error resolving '%s'");
1333 g_set_error (error, G_RESOLVER_ERROR, errnum, format, rrname);
1338 for (rec = results; rec; rec = rec->pNext)
1340 if (rec->wType != dnstype)
1345 record = parse_dns_srv (rec);
1348 record = parse_dns_soa (rec);
1351 record = parse_dns_ns (rec);
1354 record = parse_dns_mx (rec);
1357 record = parse_dns_txt (rec);
1360 g_warn_if_reached ();
1365 records = g_list_prepend (records, g_variant_ref_sink (record));