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_async_result_legacy_propagate_error (result, error))
450 else if (g_async_result_is_tagged (result, g_resolver_lookup_by_name_async))
452 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
455 /* Handle the stringified-IP-addr case */
456 addr = g_simple_async_result_get_op_res_gpointer (simple);
457 return g_list_append (NULL, g_object_ref (addr));
460 addrs = G_RESOLVER_GET_CLASS (resolver)->
461 lookup_by_name_finish (resolver, result, error);
463 remove_duplicates (addrs);
469 * g_resolver_free_addresses: (skip)
470 * @addresses: a #GList of #GInetAddress
472 * Frees @addresses (which should be the return value from
473 * g_resolver_lookup_by_name() or g_resolver_lookup_by_name_finish()).
474 * (This is a convenience method; you can also simply free the results
480 g_resolver_free_addresses (GList *addresses)
484 for (a = addresses; a; a = a->next)
485 g_object_unref (a->data);
486 g_list_free (addresses);
490 * g_resolver_lookup_by_address:
491 * @resolver: a #GResolver
492 * @address: the address to reverse-resolve
493 * @cancellable: (allow-none): a #GCancellable, or %NULL
494 * @error: return location for a #GError, or %NULL
496 * Synchronously reverse-resolves @address to determine its
497 * associated hostname.
499 * If the DNS resolution fails, @error (if non-%NULL) will be set to
500 * a value from #GResolverError.
502 * If @cancellable is non-%NULL, it can be used to cancel the
503 * operation, in which case @error (if non-%NULL) will be set to
504 * %G_IO_ERROR_CANCELLED.
506 * Return value: a hostname (either ASCII-only, or in ASCII-encoded
507 * form), or %NULL on error.
512 g_resolver_lookup_by_address (GResolver *resolver,
513 GInetAddress *address,
514 GCancellable *cancellable,
517 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
518 g_return_val_if_fail (G_IS_INET_ADDRESS (address), NULL);
520 g_resolver_maybe_reload (resolver);
521 return G_RESOLVER_GET_CLASS (resolver)->
522 lookup_by_address (resolver, address, cancellable, error);
526 * g_resolver_lookup_by_address_async:
527 * @resolver: a #GResolver
528 * @address: the address to reverse-resolve
529 * @cancellable: (allow-none): a #GCancellable, or %NULL
530 * @callback: (scope async): callback to call after resolution completes
531 * @user_data: (closure): data for @callback
533 * Begins asynchronously reverse-resolving @address to determine its
534 * associated hostname, and eventually calls @callback, which must
535 * call g_resolver_lookup_by_address_finish() to get the final result.
540 g_resolver_lookup_by_address_async (GResolver *resolver,
541 GInetAddress *address,
542 GCancellable *cancellable,
543 GAsyncReadyCallback callback,
546 g_return_if_fail (G_IS_RESOLVER (resolver));
547 g_return_if_fail (G_IS_INET_ADDRESS (address));
549 g_resolver_maybe_reload (resolver);
550 G_RESOLVER_GET_CLASS (resolver)->
551 lookup_by_address_async (resolver, address, cancellable, callback, user_data);
555 * g_resolver_lookup_by_address_finish:
556 * @resolver: a #GResolver
557 * @result: the result passed to your #GAsyncReadyCallback
558 * @error: return location for a #GError, or %NULL
560 * Retrieves the result of a previous call to
561 * g_resolver_lookup_by_address_async().
563 * If the DNS resolution failed, @error (if non-%NULL) will be set to
564 * a value from #GResolverError. If the operation was cancelled,
565 * @error will be set to %G_IO_ERROR_CANCELLED.
567 * Return value: a hostname (either ASCII-only, or in ASCII-encoded
568 * form), or %NULL on error.
573 g_resolver_lookup_by_address_finish (GResolver *resolver,
574 GAsyncResult *result,
577 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
579 if (g_async_result_legacy_propagate_error (result, error))
582 return G_RESOLVER_GET_CLASS (resolver)->
583 lookup_by_address_finish (resolver, result, error);
587 g_resolver_get_service_rrname (const char *service,
588 const char *protocol,
591 gchar *rrname, *ascii_domain = NULL;
593 if (g_hostname_is_non_ascii (domain))
594 domain = ascii_domain = g_hostname_to_ascii (domain);
596 rrname = g_strdup_printf ("_%s._%s.%s", service, protocol, domain);
598 g_free (ascii_domain);
603 * g_resolver_lookup_service:
604 * @resolver: a #GResolver
605 * @service: the service type to look up (eg, "ldap")
606 * @protocol: the networking protocol to use for @service (eg, "tcp")
607 * @domain: the DNS domain to look up the service in
608 * @cancellable: (allow-none): a #GCancellable, or %NULL
609 * @error: return location for a #GError, or %NULL
611 * Synchronously performs a DNS SRV lookup for the given @service and
612 * @protocol in the given @domain and returns an array of #GSrvTarget.
613 * @domain may be an ASCII-only or UTF-8 hostname. Note also that the
614 * @service and @protocol arguments <emphasis>do not</emphasis>
615 * include the leading underscore that appears in the actual DNS
618 * On success, g_resolver_lookup_service() will return a #GList of
619 * #GSrvTarget, sorted in order of preference. (That is, you should
620 * attempt to connect to the first target first, then the second if
621 * the first fails, etc.)
623 * If the DNS resolution fails, @error (if non-%NULL) will be set to
624 * a value from #GResolverError.
626 * If @cancellable is non-%NULL, it can be used to cancel the
627 * operation, in which case @error (if non-%NULL) will be set to
628 * %G_IO_ERROR_CANCELLED.
630 * If you are planning to connect to the service, it is usually easier
631 * to create a #GNetworkService and use its #GSocketConnectable
634 * Return value: (element-type GSrvTarget) (transfer full): a #GList of #GSrvTarget,
635 * or %NULL on error. You must free each of the targets and the list when you are
636 * done with it. (You can use g_resolver_free_targets() to do this.)
641 g_resolver_lookup_service (GResolver *resolver,
642 const gchar *service,
643 const gchar *protocol,
645 GCancellable *cancellable,
651 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
652 g_return_val_if_fail (service != NULL, NULL);
653 g_return_val_if_fail (protocol != NULL, NULL);
654 g_return_val_if_fail (domain != NULL, NULL);
656 rrname = g_resolver_get_service_rrname (service, protocol, domain);
658 g_resolver_maybe_reload (resolver);
659 targets = G_RESOLVER_GET_CLASS (resolver)->
660 lookup_service (resolver, rrname, cancellable, error);
667 * g_resolver_lookup_service_async:
668 * @resolver: a #GResolver
669 * @service: the service type to look up (eg, "ldap")
670 * @protocol: the networking protocol to use for @service (eg, "tcp")
671 * @domain: the DNS domain to look up the service in
672 * @cancellable: (allow-none): a #GCancellable, or %NULL
673 * @callback: (scope async): callback to call after resolution completes
674 * @user_data: (closure): data for @callback
676 * Begins asynchronously performing a DNS SRV lookup for the given
677 * @service and @protocol in the given @domain, and eventually calls
678 * @callback, which must call g_resolver_lookup_service_finish() to
679 * get the final result. See g_resolver_lookup_service() for more
685 g_resolver_lookup_service_async (GResolver *resolver,
686 const gchar *service,
687 const gchar *protocol,
689 GCancellable *cancellable,
690 GAsyncReadyCallback callback,
695 g_return_if_fail (G_IS_RESOLVER (resolver));
696 g_return_if_fail (service != NULL);
697 g_return_if_fail (protocol != NULL);
698 g_return_if_fail (domain != NULL);
700 rrname = g_resolver_get_service_rrname (service, protocol, domain);
702 g_resolver_maybe_reload (resolver);
703 G_RESOLVER_GET_CLASS (resolver)->
704 lookup_service_async (resolver, rrname, cancellable, callback, user_data);
710 * g_resolver_lookup_service_finish:
711 * @resolver: a #GResolver
712 * @result: the result passed to your #GAsyncReadyCallback
713 * @error: return location for a #GError, or %NULL
715 * Retrieves the result of a previous call to
716 * g_resolver_lookup_service_async().
718 * If the DNS resolution failed, @error (if non-%NULL) will be set to
719 * a value from #GResolverError. If the operation was cancelled,
720 * @error will be set to %G_IO_ERROR_CANCELLED.
722 * Return value: (element-type GSrvTarget) (transfer full): a #GList of #GSrvTarget,
723 * or %NULL on error. See g_resolver_lookup_service() for more details.
728 g_resolver_lookup_service_finish (GResolver *resolver,
729 GAsyncResult *result,
732 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
734 if (g_async_result_legacy_propagate_error (result, error))
737 return G_RESOLVER_GET_CLASS (resolver)->
738 lookup_service_finish (resolver, result, error);
742 * g_resolver_free_targets: (skip)
743 * @targets: a #GList of #GSrvTarget
745 * Frees @targets (which should be the return value from
746 * g_resolver_lookup_service() or g_resolver_lookup_service_finish()).
747 * (This is a convenience method; you can also simply free the
753 g_resolver_free_targets (GList *targets)
757 for (t = targets; t; t = t->next)
758 g_srv_target_free (t->data);
759 g_list_free (targets);
763 * g_resolver_lookup_records:
764 * @resolver: a #GResolver
765 * @rrname: the DNS name to lookup the record for
766 * @record_type: the type of DNS record to lookup
767 * @cancellable: (allow-none): a #GCancellable, or %NULL
768 * @error: return location for a #GError, or %NULL
770 * Synchronously performs a DNS record lookup for the given @rrname and returns
771 * a list of records as #GVariant tuples. See #GResolverRecordType for
772 * information on what the records contain for each @record_type.
774 * If the DNS resolution fails, @error (if non-%NULL) will be set to
775 * a value from #GResolverError.
777 * If @cancellable is non-%NULL, it can be used to cancel the
778 * operation, in which case @error (if non-%NULL) will be set to
779 * %G_IO_ERROR_CANCELLED.
781 * Return value: (element-type GVariant) (transfer full): a #GList of #GVariant,
782 * or %NULL on error. You must free each of the records and the list when you are
783 * done with it. (You can use g_list_free_full() with g_variant_unref() to do this.)
788 g_resolver_lookup_records (GResolver *resolver,
790 GResolverRecordType record_type,
791 GCancellable *cancellable,
796 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
797 g_return_val_if_fail (rrname != NULL, NULL);
799 g_resolver_maybe_reload (resolver);
800 records = G_RESOLVER_GET_CLASS (resolver)->
801 lookup_records (resolver, rrname, record_type, cancellable, error);
807 * g_resolver_lookup_records_async:
808 * @resolver: a #GResolver
809 * @rrname: the DNS name to lookup the record for
810 * @record_type: the type of DNS record to lookup
811 * @cancellable: (allow-none): a #GCancellable, or %NULL
812 * @callback: (scope async): callback to call after resolution completes
813 * @user_data: (closure): data for @callback
815 * Begins asynchronously performing a DNS lookup for the given
816 * @rrname, and eventually calls @callback, which must call
817 * g_resolver_lookup_records_finish() to get the final result. See
818 * g_resolver_lookup_records() for more details.
823 g_resolver_lookup_records_async (GResolver *resolver,
825 GResolverRecordType record_type,
826 GCancellable *cancellable,
827 GAsyncReadyCallback callback,
830 g_return_if_fail (G_IS_RESOLVER (resolver));
831 g_return_if_fail (rrname != NULL);
833 g_resolver_maybe_reload (resolver);
834 G_RESOLVER_GET_CLASS (resolver)->
835 lookup_records_async (resolver, rrname, record_type, cancellable, callback, user_data);
839 * g_resolver_lookup_records_finish:
840 * @resolver: a #GResolver
841 * @result: the result passed to your #GAsyncReadyCallback
842 * @error: return location for a #GError, or %NULL
844 * Retrieves the result of a previous call to
845 * g_resolver_lookup_records_async(). Returns a list of records as #GVariant
846 * tuples. See #GResolverRecordType for information on what the records contain.
848 * If the DNS resolution failed, @error (if non-%NULL) will be set to
849 * a value from #GResolverError. If the operation was cancelled,
850 * @error will be set to %G_IO_ERROR_CANCELLED.
852 * Return value: (element-type GVariant) (transfer full): a #GList of #GVariant,
853 * or %NULL on error. You must free each of the records and the list when you are
854 * done with it. (You can use g_list_free_full() with g_variant_unref() to do this.)
859 g_resolver_lookup_records_finish (GResolver *resolver,
860 GAsyncResult *result,
863 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
864 return G_RESOLVER_GET_CLASS (resolver)->
865 lookup_records_finish (resolver, result, error);
869 * g_resolver_error_quark:
871 * Gets the #GResolver Error Quark.
873 * Return value: a #GQuark.
878 g_resolver_error_quark (void)
880 return g_quark_from_static_string ("g-resolver-error-quark");
884 static GResolverError
885 g_resolver_error_from_addrinfo_error (gint err)
890 #if defined(EAI_NODATA) && (EAI_NODATA != EAI_NONAME)
894 return G_RESOLVER_ERROR_NOT_FOUND;
897 return G_RESOLVER_ERROR_TEMPORARY_FAILURE;
900 return G_RESOLVER_ERROR_INTERNAL;
904 struct addrinfo _g_resolver_addrinfo_hints;
906 /* Private method to process a getaddrinfo() response. */
908 _g_resolver_addresses_from_addrinfo (const char *hostname,
909 struct addrinfo *res,
914 GSocketAddress *sockaddr;
920 g_set_error (error, G_RESOLVER_ERROR,
921 g_resolver_error_from_addrinfo_error (gai_retval),
922 _("Error resolving '%s': %s"),
923 hostname, gai_strerror (gai_retval));
927 g_return_val_if_fail (res != NULL, NULL);
930 for (ai = res; ai; ai = ai->ai_next)
932 sockaddr = g_socket_address_new_from_native (ai->ai_addr, ai->ai_addrlen);
933 if (!sockaddr || !G_IS_INET_SOCKET_ADDRESS (sockaddr))
936 addr = g_object_ref (g_inet_socket_address_get_address ((GInetSocketAddress *)sockaddr));
937 addrs = g_list_prepend (addrs, addr);
938 g_object_unref (sockaddr);
941 return g_list_reverse (addrs);
944 /* Private method to set up a getnameinfo() request */
946 _g_resolver_address_to_sockaddr (GInetAddress *address,
947 struct sockaddr_storage *sa,
950 GSocketAddress *sockaddr;
952 sockaddr = g_inet_socket_address_new (address, 0);
953 g_socket_address_to_native (sockaddr, (struct sockaddr *)sa, sizeof (*sa), NULL);
954 *len = g_socket_address_get_native_size (sockaddr);
955 g_object_unref (sockaddr);
958 /* Private method to process a getnameinfo() response. */
960 _g_resolver_name_from_nameinfo (GInetAddress *address,
969 phys = g_inet_address_to_string (address);
970 g_set_error (error, G_RESOLVER_ERROR,
971 g_resolver_error_from_addrinfo_error (gni_retval),
972 _("Error reverse-resolving '%s': %s"),
973 phys ? phys : "(unknown)", gai_strerror (gni_retval));
978 return g_strdup (name);
981 #if defined(G_OS_UNIX)
984 parse_short (guchar **p,
990 GETSHORT (*value, *p);
995 parse_long (guchar **p,
1001 GETLONG (*value, *p);
1006 parse_res_srv (guchar *answer,
1010 gchar namebuf[1024];
1011 guint16 priority, weight, port;
1014 if (!parse_short (&p, end, &priority) ||
1015 !parse_short (&p, end, &weight) ||
1016 !parse_short (&p, end, &port))
1019 n = dn_expand (answer, end, p, namebuf, sizeof (namebuf));
1024 return g_variant_new ("(qqqs)",
1032 parse_res_soa (guchar *answer,
1036 gchar mnamebuf[1024];
1037 gchar rnamebuf[1024];
1038 guint32 serial, refresh, retry, expire, ttl;
1041 n = dn_expand (answer, end, p, mnamebuf, sizeof (mnamebuf));
1046 n = dn_expand (answer, end, p, rnamebuf, sizeof (rnamebuf));
1051 if (!parse_long (&p, end, &serial) ||
1052 !parse_long (&p, end, &refresh) ||
1053 !parse_long (&p, end, &retry) ||
1054 !parse_long (&p, end, &expire) ||
1055 !parse_long (&p, end, &ttl))
1058 return g_variant_new ("(ssuuuuu)",
1069 parse_res_ns (guchar *answer,
1073 gchar namebuf[1024];
1076 n = dn_expand (answer, end, p, namebuf, sizeof (namebuf));
1080 return g_variant_new ("(s)", namebuf);
1084 parse_res_mx (guchar *answer,
1088 gchar namebuf[1024];
1092 if (!parse_short (&p, end, &preference))
1095 n = dn_expand (answer, end, p, namebuf, sizeof (namebuf));
1100 return g_variant_new ("(qs)",
1106 parse_res_txt (guchar *answer,
1114 array = g_ptr_array_new_with_free_func (g_free);
1120 g_ptr_array_add (array, g_strndup ((gchar *)p, len));
1124 record = g_variant_new ("(@as)",
1125 g_variant_new_strv ((const gchar **)array->pdata, array->len));
1126 g_ptr_array_free (array, TRUE);
1131 _g_resolver_record_type_to_rrtype (GResolverRecordType type)
1135 case G_RESOLVER_RECORD_SRV:
1137 case G_RESOLVER_RECORD_TXT:
1139 case G_RESOLVER_RECORD_SOA:
1141 case G_RESOLVER_RECORD_NS:
1143 case G_RESOLVER_RECORD_MX:
1146 g_return_val_if_reached (-1);
1149 /* Private method to process a res_query response into GSrvTargets */
1151 _g_resolver_records_from_res_query (const gchar *rrname,
1160 guint16 type, qclass, rdlength;
1169 GResolverError errnum;
1170 const gchar *format;
1172 if (len == 0 || herr == HOST_NOT_FOUND || herr == NO_DATA)
1174 errnum = G_RESOLVER_ERROR_NOT_FOUND;
1175 format = _("No DNS record of the requested type for '%s'");
1177 else if (herr == TRY_AGAIN)
1179 errnum = G_RESOLVER_ERROR_TEMPORARY_FAILURE;
1180 format = _("Temporarily unable to resolve '%s'");
1184 errnum = G_RESOLVER_ERROR_INTERNAL;
1185 format = _("Error resolving '%s'");
1188 g_set_error (error, G_RESOLVER_ERROR, errnum, format, rrname);
1194 header = (HEADER *)answer;
1195 p = answer + sizeof (HEADER);
1199 count = ntohs (header->qdcount);
1200 for (i = 0; i < count && p < end; i++)
1202 n = dn_skipname (p, end);
1209 /* Incomplete response */
1212 g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_TEMPORARY_FAILURE,
1213 _("Incomplete data received for '%s'"), rrname);
1218 count = ntohs (header->ancount);
1219 for (i = 0; i < count && p < end; i++)
1221 n = dn_skipname (p, end);
1226 if (!parse_short (&p, end, &type) ||
1227 !parse_short (&p, end, &qclass) ||
1228 !parse_long (&p, end, &ttl) ||
1229 !parse_short (&p, end, &rdlength))
1232 ttl = ttl; /* To avoid -Wunused-but-set-variable */
1234 if (p + rdlength > end)
1237 if (type == rrtype && qclass == C_IN)
1242 record = parse_res_srv (answer, end, p);
1245 record = parse_res_mx (answer, end, p);
1248 record = parse_res_soa (answer, end, p);
1251 record = parse_res_ns (answer, end, p);
1254 record = parse_res_txt (answer, p + rdlength, p);
1257 g_warn_if_reached ();
1263 records = g_list_prepend (records, record);
1269 /* Somehow got a truncated response */
1272 g_list_free_full (records, (GDestroyNotify)g_variant_unref);
1273 g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_TEMPORARY_FAILURE,
1274 _("Incomplete data received for '%s'"), rrname);
1281 #elif defined(G_OS_WIN32)
1283 parse_dns_srv (DNS_RECORD *rec)
1285 return g_variant_new ("(qqqs)",
1286 (guint16)rec->Data.SRV.wPriority,
1287 (guint16)rec->Data.SRV.wWeight,
1288 (guint16)rec->Data.SRV.wPort,
1289 rec->Data.SRV.pNameTarget);
1293 parse_dns_soa (DNS_RECORD *rec)
1295 return g_variant_new ("(ssuuuuu)",
1296 rec->Data.SOA.pNamePrimaryServer,
1297 rec->Data.SOA.pNameAdministrator,
1298 (guint32)rec->Data.SOA.dwSerialNo,
1299 (guint32)rec->Data.SOA.dwRefresh,
1300 (guint32)rec->Data.SOA.dwRetry,
1301 (guint32)rec->Data.SOA.dwExpire,
1302 (guint32)rec->Data.SOA.dwDefaultTtl);
1306 parse_dns_ns (DNS_RECORD *rec)
1308 return g_variant_new ("(s)", rec->Data.NS.pNameHost);
1312 parse_dns_mx (DNS_RECORD *rec)
1314 return g_variant_new ("(qs)",
1315 (guint16)rec->Data.MX.wPreference,
1316 rec->Data.MX.pNameExchange);
1320 parse_dns_txt (DNS_RECORD *rec)
1326 array = g_ptr_array_new ();
1327 for (i = 0; i < rec->Data.TXT.dwStringCount; i++)
1328 g_ptr_array_add (array, rec->Data.TXT.pStringArray[i]);
1329 record = g_variant_new ("(@as)",
1330 g_variant_new_strv ((const gchar **)array->pdata, array->len));
1331 g_ptr_array_free (array, TRUE);
1336 _g_resolver_record_type_to_dnstype (GResolverRecordType type)
1340 case G_RESOLVER_RECORD_SRV:
1341 return DNS_TYPE_SRV;
1342 case G_RESOLVER_RECORD_TXT:
1343 return DNS_TYPE_TEXT;
1344 case G_RESOLVER_RECORD_SOA:
1345 return DNS_TYPE_SOA;
1346 case G_RESOLVER_RECORD_NS:
1348 case G_RESOLVER_RECORD_MX:
1351 g_return_val_if_reached (-1);
1354 /* Private method to process a DnsQuery response into GVariants */
1356 _g_resolver_records_from_DnsQuery (const gchar *rrname,
1359 DNS_RECORD *results,
1366 if (status != ERROR_SUCCESS)
1368 GResolverError errnum;
1369 const gchar *format;
1371 if (status == DNS_ERROR_RCODE_NAME_ERROR)
1373 errnum = G_RESOLVER_ERROR_NOT_FOUND;
1374 format = _("No DNS record of the requested type for '%s'");
1376 else if (status == DNS_ERROR_RCODE_SERVER_FAILURE)
1378 errnum = G_RESOLVER_ERROR_TEMPORARY_FAILURE;
1379 format = _("Temporarily unable to resolve '%s'");
1383 errnum = G_RESOLVER_ERROR_INTERNAL;
1384 format = _("Error resolving '%s'");
1387 g_set_error (error, G_RESOLVER_ERROR, errnum, format, rrname);
1392 for (rec = results; rec; rec = rec->pNext)
1394 if (rec->wType != dnstype)
1399 record = parse_dns_srv (rec);
1402 record = parse_dns_soa (rec);
1405 record = parse_dns_ns (rec);
1408 record = parse_dns_mx (rec);
1411 record = parse_dns_txt (rec);
1414 g_warn_if_reached ();
1419 records = g_list_prepend (records, g_variant_ref_sink (record));