docs: use "Returns:" consistently
[platform/upstream/glib.git] / gio / gresolver.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2
3 /* GIO - GLib Input, Output and Streaming Library
4  *
5  * Copyright (C) 2008 Red Hat, Inc.
6  *
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.
11  *
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.
16  *
17  * You should have received a copy of the GNU Lesser General
18  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "config.h"
22 #include <glib.h>
23 #include "glibintl.h"
24
25 #include "gresolver.h"
26 #include "gnetworkingprivate.h"
27 #include "gasyncresult.h"
28 #include "ginetaddress.h"
29 #include "gsimpleasyncresult.h"
30 #include "gtask.h"
31 #include "gsrvtarget.h"
32 #include "gthreadedresolver.h"
33
34 #ifdef G_OS_UNIX
35 #include <sys/stat.h>
36 #endif
37
38 #include <stdlib.h>
39
40
41 /**
42  * SECTION:gresolver
43  * @short_description: Asynchronous and cancellable DNS resolver
44  * @include: gio/gio.h
45  *
46  * #GResolver provides cancellable synchronous and asynchronous DNS
47  * resolution, for hostnames (g_resolver_lookup_by_address(),
48  * g_resolver_lookup_by_name() and their async variants) and SRV
49  * (service) records (g_resolver_lookup_service()).
50  *
51  * #GNetworkAddress and #GNetworkService provide wrappers around
52  * #GResolver functionality that also implement #GSocketConnectable,
53  * making it easy to connect to a remote host/service.
54  */
55
56 enum {
57   RELOAD,
58   LAST_SIGNAL
59 };
60
61 static guint signals[LAST_SIGNAL] = { 0 };
62
63 struct _GResolverPrivate {
64 #ifdef G_OS_UNIX
65   time_t resolv_conf_timestamp;
66 #else
67   int dummy;
68 #endif
69 };
70
71 /**
72  * GResolver:
73  *
74  * The object that handles DNS resolution. Use g_resolver_get_default()
75  * to get the default resolver.
76  */
77 G_DEFINE_TYPE_WITH_CODE (GResolver, g_resolver, G_TYPE_OBJECT,
78                          G_ADD_PRIVATE (GResolver)
79                          g_networking_init ();)
80
81 static GList *
82 srv_records_to_targets (GList *records)
83 {
84   const gchar *hostname;
85   guint16 port, priority, weight;
86   GSrvTarget *target;
87   GList *l;
88
89   for (l = records; l != NULL; l = g_list_next (l))
90     {
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);
94       l->data = target;
95     }
96
97   return g_srv_target_list_sort (records);
98 }
99
100 static GList *
101 g_resolver_real_lookup_service (GResolver            *resolver,
102                                 const gchar          *rrname,
103                                 GCancellable         *cancellable,
104                                 GError              **error)
105 {
106   GList *records;
107
108   records = G_RESOLVER_GET_CLASS (resolver)->lookup_records (resolver,
109                                                              rrname,
110                                                              G_RESOLVER_RECORD_SRV,
111                                                              cancellable,
112                                                              error);
113
114   return srv_records_to_targets (records);
115 }
116
117 static void
118 g_resolver_real_lookup_service_async (GResolver            *resolver,
119                                       const gchar          *rrname,
120                                       GCancellable         *cancellable,
121                                       GAsyncReadyCallback   callback,
122                                       gpointer              user_data)
123 {
124   G_RESOLVER_GET_CLASS (resolver)->lookup_records_async (resolver,
125                                                          rrname,
126                                                          G_RESOLVER_RECORD_SRV,
127                                                          cancellable,
128                                                          callback,
129                                                          user_data);
130 }
131
132 static GList *
133 g_resolver_real_lookup_service_finish (GResolver            *resolver,
134                                        GAsyncResult         *result,
135                                        GError              **error)
136 {
137   GList *records;
138
139   records = G_RESOLVER_GET_CLASS (resolver)->lookup_records_finish (resolver,
140                                                                     result,
141                                                                     error);
142
143   return srv_records_to_targets (records);
144 }
145
146 static void
147 g_resolver_class_init (GResolverClass *resolver_class)
148 {
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;
153
154   /**
155    * GResolver::reload:
156    * @resolver: a #GResolver
157    *
158    * Emitted when the resolver notices that the system resolver
159    * configuration has changed.
160    **/
161   signals[RELOAD] =
162     g_signal_new (I_("reload"),
163                   G_TYPE_RESOLVER,
164                   G_SIGNAL_RUN_LAST,
165                   G_STRUCT_OFFSET (GResolverClass, reload),
166                   NULL, NULL,
167                   g_cclosure_marshal_VOID__VOID,
168                   G_TYPE_NONE, 0);
169 }
170
171 static void
172 g_resolver_init (GResolver *resolver)
173 {
174 #ifdef G_OS_UNIX
175   struct stat st;
176 #endif
177
178   resolver->priv = g_resolver_get_instance_private (resolver);
179
180 #ifdef G_OS_UNIX
181   if (stat (_PATH_RESCONF, &st) == 0)
182     resolver->priv->resolv_conf_timestamp = st.st_mtime;
183 #endif
184 }
185
186 static GResolver *default_resolver;
187
188 /**
189  * g_resolver_get_default:
190  *
191  * Gets the default #GResolver. You should unref it when you are done
192  * with it. #GResolver may use its reference count as a hint about how
193  * many threads it should allocate for concurrent DNS resolutions.
194  *
195  * Returns: (transfer full): the default #GResolver.
196  *
197  * Since: 2.22
198  */
199 GResolver *
200 g_resolver_get_default (void)
201 {
202   if (!default_resolver)
203     default_resolver = g_object_new (G_TYPE_THREADED_RESOLVER, NULL);
204
205   return g_object_ref (default_resolver);
206 }
207
208 /**
209  * g_resolver_set_default:
210  * @resolver: the new default #GResolver
211  *
212  * Sets @resolver to be the application's default resolver (reffing
213  * @resolver, and unreffing the previous default resolver, if any).
214  * Future calls to g_resolver_get_default() will return this resolver.
215  *
216  * This can be used if an application wants to perform any sort of DNS
217  * caching or "pinning"; it can implement its own #GResolver that
218  * calls the original default resolver for DNS operations, and
219  * implements its own cache policies on top of that, and then set
220  * itself as the default resolver for all later code to use.
221  *
222  * Since: 2.22
223  */
224 void
225 g_resolver_set_default (GResolver *resolver)
226 {
227   if (default_resolver)
228     g_object_unref (default_resolver);
229   default_resolver = g_object_ref (resolver);
230 }
231
232 /* Bionic has res_init() but it's not in any header */
233 #ifdef __BIONIC__
234 int res_init (void);
235 #endif
236
237 static void
238 g_resolver_maybe_reload (GResolver *resolver)
239 {
240 #ifdef G_OS_UNIX
241   struct stat st;
242
243   if (stat (_PATH_RESCONF, &st) == 0)
244     {
245       if (st.st_mtime != resolver->priv->resolv_conf_timestamp)
246         {
247           resolver->priv->resolv_conf_timestamp = st.st_mtime;
248 #ifdef HAVE_RES_INIT
249           res_init ();
250 #endif
251           g_signal_emit (resolver, signals[RELOAD], 0);
252         }
253     }
254 #endif
255 }
256
257 /* filter out duplicates, cf. https://bugzilla.gnome.org/show_bug.cgi?id=631379 */
258 static void
259 remove_duplicates (GList *addrs)
260 {
261   GList *l;
262   GList *ll;
263   GList *lll;
264
265   /* TODO: if this is too slow (it's O(n^2) but n is typically really
266    * small), we can do something more clever but note that we must not
267    * change the order of elements...
268    */
269   for (l = addrs; l != NULL; l = l->next)
270     {
271       GInetAddress *address = G_INET_ADDRESS (l->data);
272       for (ll = l->next; ll != NULL; ll = lll)
273         {
274           GInetAddress *other_address = G_INET_ADDRESS (ll->data);
275           lll = ll->next;
276           if (g_inet_address_equal (address, other_address))
277             {
278               g_object_unref (other_address);
279               /* we never return the first element */
280               g_warn_if_fail (g_list_delete_link (addrs, ll) == addrs);
281             }
282         }
283     }
284 }
285
286 /* Note that this does not follow the "FALSE means @error is set"
287  * convention. The return value tells the caller whether it should
288  * return @addrs and @error to the caller right away, or if it should
289  * continue and trying to resolve the name as a hostname.
290  */
291 static gboolean
292 handle_ip_address (const char  *hostname,
293                    GList      **addrs,
294                    GError     **error)
295 {
296   GInetAddress *addr;
297   struct in_addr ip4addr;
298
299   addr = g_inet_address_new_from_string (hostname);
300   if (addr)
301     {
302       *addrs = g_list_append (NULL, addr);
303       return TRUE;
304     }
305
306   *addrs = NULL;
307
308   /* Reject non-standard IPv4 numbers-and-dots addresses.
309    * g_inet_address_new_from_string() will have accepted any "real" IP
310    * address, so if inet_aton() succeeds, then it's an address we want
311    * to reject.  This check is not necessary for Windows, as getaddrinfo()
312    * already rejects such IPv4 addresses on Windows.
313    */
314 #ifndef G_OS_WIN32
315   if (inet_aton (hostname, &ip4addr))
316     {
317       g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_NOT_FOUND,
318                    _("Error resolving '%s': %s"),
319                    hostname, gai_strerror (EAI_NONAME));
320       return TRUE;
321     }
322 #endif
323
324   return FALSE;
325 }
326
327 /**
328  * g_resolver_lookup_by_name:
329  * @resolver: a #GResolver
330  * @hostname: the hostname to look up
331  * @cancellable: (allow-none): a #GCancellable, or %NULL
332  * @error: return location for a #GError, or %NULL
333  *
334  * Synchronously resolves @hostname to determine its associated IP
335  * address(es). @hostname may be an ASCII-only or UTF-8 hostname, or
336  * the textual form of an IP address (in which case this just becomes
337  * a wrapper around g_inet_address_new_from_string()).
338  *
339  * On success, g_resolver_lookup_by_name() will return a #GList of
340  * #GInetAddress, sorted in order of preference and guaranteed to not
341  * contain duplicates. That is, if using the result to connect to
342  * @hostname, you should attempt to connect to the first address
343  * first, then the second if the first fails, etc. If you are using
344  * the result to listen on a socket, it is appropriate to add each
345  * result using e.g. g_socket_listener_add_address().
346  *
347  * If the DNS resolution fails, @error (if non-%NULL) will be set to a
348  * value from #GResolverError.
349  *
350  * If @cancellable is non-%NULL, it can be used to cancel the
351  * operation, in which case @error (if non-%NULL) will be set to
352  * %G_IO_ERROR_CANCELLED.
353  *
354  * If you are planning to connect to a socket on the resolved IP
355  * address, it may be easier to create a #GNetworkAddress and use its
356  * #GSocketConnectable interface.
357  *
358  * Returns: (element-type GInetAddress) (transfer full): a #GList
359  * of #GInetAddress, or %NULL on error. You
360  * must unref each of the addresses and free the list when you are
361  * done with it. (You can use g_resolver_free_addresses() to do this.)
362  *
363  * Since: 2.22
364  */
365 GList *
366 g_resolver_lookup_by_name (GResolver     *resolver,
367                            const gchar   *hostname,
368                            GCancellable  *cancellable,
369                            GError       **error)
370 {
371   GList *addrs;
372   gchar *ascii_hostname = NULL;
373
374   g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
375   g_return_val_if_fail (hostname != NULL, NULL);
376
377   /* Check if @hostname is just an IP address */
378   if (handle_ip_address (hostname, &addrs, error))
379     return addrs;
380
381   if (g_hostname_is_non_ascii (hostname))
382     hostname = ascii_hostname = g_hostname_to_ascii (hostname);
383
384   g_resolver_maybe_reload (resolver);
385   addrs = G_RESOLVER_GET_CLASS (resolver)->
386     lookup_by_name (resolver, hostname, cancellable, error);
387
388   remove_duplicates (addrs);
389
390   g_free (ascii_hostname);
391   return addrs;
392 }
393
394 /**
395  * g_resolver_lookup_by_name_async:
396  * @resolver: a #GResolver
397  * @hostname: the hostname to look up the address of
398  * @cancellable: (allow-none): a #GCancellable, or %NULL
399  * @callback: (scope async): callback to call after resolution completes
400  * @user_data: (closure): data for @callback
401  *
402  * Begins asynchronously resolving @hostname to determine its
403  * associated IP address(es), and eventually calls @callback, which
404  * must call g_resolver_lookup_by_name_finish() to get the result.
405  * See g_resolver_lookup_by_name() for more details.
406  *
407  * Since: 2.22
408  */
409 void
410 g_resolver_lookup_by_name_async (GResolver           *resolver,
411                                  const gchar         *hostname,
412                                  GCancellable        *cancellable,
413                                  GAsyncReadyCallback  callback,
414                                  gpointer             user_data)
415 {
416   gchar *ascii_hostname = NULL;
417   GList *addrs;
418   GError *error = NULL;
419
420   g_return_if_fail (G_IS_RESOLVER (resolver));
421   g_return_if_fail (hostname != NULL);
422
423   /* Check if @hostname is just an IP address */
424   if (handle_ip_address (hostname, &addrs, &error))
425     {
426       GTask *task;
427
428       task = g_task_new (resolver, cancellable, callback, user_data);
429       g_task_set_source_tag (task, g_resolver_lookup_by_name_async);
430       if (addrs)
431         g_task_return_pointer (task, addrs, (GDestroyNotify) g_resolver_free_addresses);
432       else
433         g_task_return_error (task, error);
434       g_object_unref (task);
435       return;
436     }
437
438   if (g_hostname_is_non_ascii (hostname))
439     hostname = ascii_hostname = g_hostname_to_ascii (hostname);
440
441   g_resolver_maybe_reload (resolver);
442   G_RESOLVER_GET_CLASS (resolver)->
443     lookup_by_name_async (resolver, hostname, cancellable, callback, user_data);
444
445   g_free (ascii_hostname);
446 }
447
448 /**
449  * g_resolver_lookup_by_name_finish:
450  * @resolver: a #GResolver
451  * @result: the result passed to your #GAsyncReadyCallback
452  * @error: return location for a #GError, or %NULL
453  *
454  * Retrieves the result of a call to
455  * g_resolver_lookup_by_name_async().
456  *
457  * If the DNS resolution failed, @error (if non-%NULL) will be set to
458  * a value from #GResolverError. If the operation was cancelled,
459  * @error will be set to %G_IO_ERROR_CANCELLED.
460  *
461  * Returns: (element-type GInetAddress) (transfer full): a #GList
462  * of #GInetAddress, or %NULL on error. See g_resolver_lookup_by_name()
463  * for more details.
464  *
465  * Since: 2.22
466  */
467 GList *
468 g_resolver_lookup_by_name_finish (GResolver     *resolver,
469                                   GAsyncResult  *result,
470                                   GError       **error)
471 {
472   GList *addrs;
473
474   g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
475
476   if (g_async_result_legacy_propagate_error (result, error))
477     return NULL;
478   else if (g_async_result_is_tagged (result, g_resolver_lookup_by_name_async))
479     {
480       /* Handle the stringified-IP-addr case */
481       return g_task_propagate_pointer (G_TASK (result), error);
482     }
483
484   addrs = G_RESOLVER_GET_CLASS (resolver)->
485     lookup_by_name_finish (resolver, result, error);
486
487   remove_duplicates (addrs);
488
489   return addrs;
490 }
491
492 /**
493  * g_resolver_free_addresses: (skip)
494  * @addresses: a #GList of #GInetAddress
495  *
496  * Frees @addresses (which should be the return value from
497  * g_resolver_lookup_by_name() or g_resolver_lookup_by_name_finish()).
498  * (This is a convenience method; you can also simply free the results
499  * by hand.)
500  *
501  * Since: 2.22
502  */
503 void
504 g_resolver_free_addresses (GList *addresses)
505 {
506   GList *a;
507
508   for (a = addresses; a; a = a->next)
509     g_object_unref (a->data);
510   g_list_free (addresses);
511 }
512
513 /**
514  * g_resolver_lookup_by_address:
515  * @resolver: a #GResolver
516  * @address: the address to reverse-resolve
517  * @cancellable: (allow-none): a #GCancellable, or %NULL
518  * @error: return location for a #GError, or %NULL
519  *
520  * Synchronously reverse-resolves @address to determine its
521  * associated hostname.
522  *
523  * If the DNS resolution fails, @error (if non-%NULL) will be set to
524  * a value from #GResolverError.
525  *
526  * If @cancellable is non-%NULL, it can be used to cancel the
527  * operation, in which case @error (if non-%NULL) will be set to
528  * %G_IO_ERROR_CANCELLED.
529  *
530  * Returns: a hostname (either ASCII-only, or in ASCII-encoded
531  *     form), or %NULL on error.
532  *
533  * Since: 2.22
534  */
535 gchar *
536 g_resolver_lookup_by_address (GResolver     *resolver,
537                               GInetAddress  *address,
538                               GCancellable  *cancellable,
539                               GError       **error)
540 {
541   g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
542   g_return_val_if_fail (G_IS_INET_ADDRESS (address), NULL);
543
544   g_resolver_maybe_reload (resolver);
545   return G_RESOLVER_GET_CLASS (resolver)->
546     lookup_by_address (resolver, address, cancellable, error);
547 }
548
549 /**
550  * g_resolver_lookup_by_address_async:
551  * @resolver: a #GResolver
552  * @address: the address to reverse-resolve
553  * @cancellable: (allow-none): a #GCancellable, or %NULL
554  * @callback: (scope async): callback to call after resolution completes
555  * @user_data: (closure): data for @callback
556  *
557  * Begins asynchronously reverse-resolving @address to determine its
558  * associated hostname, and eventually calls @callback, which must
559  * call g_resolver_lookup_by_address_finish() to get the final result.
560  *
561  * Since: 2.22
562  */
563 void
564 g_resolver_lookup_by_address_async (GResolver           *resolver,
565                                     GInetAddress        *address,
566                                     GCancellable        *cancellable,
567                                     GAsyncReadyCallback  callback,
568                                     gpointer             user_data)
569 {
570   g_return_if_fail (G_IS_RESOLVER (resolver));
571   g_return_if_fail (G_IS_INET_ADDRESS (address));
572
573   g_resolver_maybe_reload (resolver);
574   G_RESOLVER_GET_CLASS (resolver)->
575     lookup_by_address_async (resolver, address, cancellable, callback, user_data);
576 }
577
578 /**
579  * g_resolver_lookup_by_address_finish:
580  * @resolver: a #GResolver
581  * @result: the result passed to your #GAsyncReadyCallback
582  * @error: return location for a #GError, or %NULL
583  *
584  * Retrieves the result of a previous call to
585  * g_resolver_lookup_by_address_async().
586  *
587  * If the DNS resolution failed, @error (if non-%NULL) will be set to
588  * a value from #GResolverError. If the operation was cancelled,
589  * @error will be set to %G_IO_ERROR_CANCELLED.
590  *
591  * Returns: a hostname (either ASCII-only, or in ASCII-encoded
592  * form), or %NULL on error.
593  *
594  * Since: 2.22
595  */
596 gchar *
597 g_resolver_lookup_by_address_finish (GResolver     *resolver,
598                                      GAsyncResult  *result,
599                                      GError       **error)
600 {
601   g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
602
603   if (g_async_result_legacy_propagate_error (result, error))
604     return NULL;
605
606   return G_RESOLVER_GET_CLASS (resolver)->
607     lookup_by_address_finish (resolver, result, error);
608 }
609
610 static gchar *
611 g_resolver_get_service_rrname (const char *service,
612                                const char *protocol,
613                                const char *domain)
614 {
615   gchar *rrname, *ascii_domain = NULL;
616
617   if (g_hostname_is_non_ascii (domain))
618     domain = ascii_domain = g_hostname_to_ascii (domain);
619
620   rrname = g_strdup_printf ("_%s._%s.%s", service, protocol, domain);
621
622   g_free (ascii_domain);
623   return rrname;
624 }
625
626 /**
627  * g_resolver_lookup_service:
628  * @resolver: a #GResolver
629  * @service: the service type to look up (eg, "ldap")
630  * @protocol: the networking protocol to use for @service (eg, "tcp")
631  * @domain: the DNS domain to look up the service in
632  * @cancellable: (allow-none): a #GCancellable, or %NULL
633  * @error: return location for a #GError, or %NULL
634  *
635  * Synchronously performs a DNS SRV lookup for the given @service and
636  * @protocol in the given @domain and returns an array of #GSrvTarget.
637  * @domain may be an ASCII-only or UTF-8 hostname. Note also that the
638  * @service and @protocol arguments do not include the leading underscore
639  * that appears in the actual DNS entry.
640  *
641  * On success, g_resolver_lookup_service() will return a #GList of
642  * #GSrvTarget, sorted in order of preference. (That is, you should
643  * attempt to connect to the first target first, then the second if
644  * the first fails, etc.)
645  *
646  * If the DNS resolution fails, @error (if non-%NULL) will be set to
647  * a value from #GResolverError.
648  *
649  * If @cancellable is non-%NULL, it can be used to cancel the
650  * operation, in which case @error (if non-%NULL) will be set to
651  * %G_IO_ERROR_CANCELLED.
652  *
653  * If you are planning to connect to the service, it is usually easier
654  * to create a #GNetworkService and use its #GSocketConnectable
655  * interface.
656  *
657  * Returns: (element-type GSrvTarget) (transfer full): a #GList of #GSrvTarget,
658  * or %NULL on error. You must free each of the targets and the list when you are
659  * done with it. (You can use g_resolver_free_targets() to do this.)
660  *
661  * Since: 2.22
662  */
663 GList *
664 g_resolver_lookup_service (GResolver     *resolver,
665                            const gchar   *service,
666                            const gchar   *protocol,
667                            const gchar   *domain,
668                            GCancellable  *cancellable,
669                            GError       **error)
670 {
671   GList *targets;
672   gchar *rrname;
673
674   g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
675   g_return_val_if_fail (service != NULL, NULL);
676   g_return_val_if_fail (protocol != NULL, NULL);
677   g_return_val_if_fail (domain != NULL, NULL);
678
679   rrname = g_resolver_get_service_rrname (service, protocol, domain);
680
681   g_resolver_maybe_reload (resolver);
682   targets = G_RESOLVER_GET_CLASS (resolver)->
683     lookup_service (resolver, rrname, cancellable, error);
684
685   g_free (rrname);
686   return targets;
687 }
688
689 /**
690  * g_resolver_lookup_service_async:
691  * @resolver: a #GResolver
692  * @service: the service type to look up (eg, "ldap")
693  * @protocol: the networking protocol to use for @service (eg, "tcp")
694  * @domain: the DNS domain to look up the service in
695  * @cancellable: (allow-none): a #GCancellable, or %NULL
696  * @callback: (scope async): callback to call after resolution completes
697  * @user_data: (closure): data for @callback
698  *
699  * Begins asynchronously performing a DNS SRV lookup for the given
700  * @service and @protocol in the given @domain, and eventually calls
701  * @callback, which must call g_resolver_lookup_service_finish() to
702  * get the final result. See g_resolver_lookup_service() for more
703  * details.
704  *
705  * Since: 2.22
706  */
707 void
708 g_resolver_lookup_service_async (GResolver           *resolver,
709                                  const gchar         *service,
710                                  const gchar         *protocol,
711                                  const gchar         *domain,
712                                  GCancellable        *cancellable,
713                                  GAsyncReadyCallback  callback,
714                                  gpointer             user_data)
715 {
716   gchar *rrname;
717
718   g_return_if_fail (G_IS_RESOLVER (resolver));
719   g_return_if_fail (service != NULL);
720   g_return_if_fail (protocol != NULL);
721   g_return_if_fail (domain != NULL);
722
723   rrname = g_resolver_get_service_rrname (service, protocol, domain);
724
725   g_resolver_maybe_reload (resolver);
726   G_RESOLVER_GET_CLASS (resolver)->
727     lookup_service_async (resolver, rrname, cancellable, callback, user_data);
728
729   g_free (rrname);
730 }
731
732 /**
733  * g_resolver_lookup_service_finish:
734  * @resolver: a #GResolver
735  * @result: the result passed to your #GAsyncReadyCallback
736  * @error: return location for a #GError, or %NULL
737  *
738  * Retrieves the result of a previous call to
739  * g_resolver_lookup_service_async().
740  *
741  * If the DNS resolution failed, @error (if non-%NULL) will be set to
742  * a value from #GResolverError. If the operation was cancelled,
743  * @error will be set to %G_IO_ERROR_CANCELLED.
744  *
745  * Returns: (element-type GSrvTarget) (transfer full): a #GList of #GSrvTarget,
746  * or %NULL on error. See g_resolver_lookup_service() for more details.
747  *
748  * Since: 2.22
749  */
750 GList *
751 g_resolver_lookup_service_finish (GResolver     *resolver,
752                                   GAsyncResult  *result,
753                                   GError       **error)
754 {
755   g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
756
757   if (g_async_result_legacy_propagate_error (result, error))
758     return NULL;
759
760   return G_RESOLVER_GET_CLASS (resolver)->
761     lookup_service_finish (resolver, result, error);
762 }
763
764 /**
765  * g_resolver_free_targets: (skip)
766  * @targets: a #GList of #GSrvTarget
767  *
768  * Frees @targets (which should be the return value from
769  * g_resolver_lookup_service() or g_resolver_lookup_service_finish()).
770  * (This is a convenience method; you can also simply free the
771  * results by hand.)
772  *
773  * Since: 2.22
774  */
775 void
776 g_resolver_free_targets (GList *targets)
777 {
778   GList *t;
779
780   for (t = targets; t; t = t->next)
781     g_srv_target_free (t->data);
782   g_list_free (targets);
783 }
784
785 /**
786  * g_resolver_lookup_records:
787  * @resolver: a #GResolver
788  * @rrname: the DNS name to lookup the record for
789  * @record_type: the type of DNS record to lookup
790  * @cancellable: (allow-none): a #GCancellable, or %NULL
791  * @error: return location for a #GError, or %NULL
792  *
793  * Synchronously performs a DNS record lookup for the given @rrname and returns
794  * a list of records as #GVariant tuples. See #GResolverRecordType for
795  * information on what the records contain for each @record_type.
796  *
797  * If the DNS resolution fails, @error (if non-%NULL) will be set to
798  * a value from #GResolverError.
799  *
800  * If @cancellable is non-%NULL, it can be used to cancel the
801  * operation, in which case @error (if non-%NULL) will be set to
802  * %G_IO_ERROR_CANCELLED.
803  *
804  * Returns: (element-type GVariant) (transfer full): a #GList of #GVariant,
805  * or %NULL on error. You must free each of the records and the list when you are
806  * done with it. (You can use g_list_free_full() with g_variant_unref() to do this.)
807  *
808  * Since: 2.34
809  */
810 GList *
811 g_resolver_lookup_records (GResolver            *resolver,
812                            const gchar          *rrname,
813                            GResolverRecordType   record_type,
814                            GCancellable         *cancellable,
815                            GError              **error)
816 {
817   GList *records;
818
819   g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
820   g_return_val_if_fail (rrname != NULL, NULL);
821
822   g_resolver_maybe_reload (resolver);
823   records = G_RESOLVER_GET_CLASS (resolver)->
824     lookup_records (resolver, rrname, record_type, cancellable, error);
825
826   return records;
827 }
828
829 /**
830  * g_resolver_lookup_records_async:
831  * @resolver: a #GResolver
832  * @rrname: the DNS name to lookup the record for
833  * @record_type: the type of DNS record to lookup
834  * @cancellable: (allow-none): a #GCancellable, or %NULL
835  * @callback: (scope async): callback to call after resolution completes
836  * @user_data: (closure): data for @callback
837  *
838  * Begins asynchronously performing a DNS lookup for the given
839  * @rrname, and eventually calls @callback, which must call
840  * g_resolver_lookup_records_finish() to get the final result. See
841  * g_resolver_lookup_records() for more details.
842  *
843  * Since: 2.34
844  */
845 void
846 g_resolver_lookup_records_async (GResolver           *resolver,
847                                  const gchar         *rrname,
848                                  GResolverRecordType  record_type,
849                                  GCancellable        *cancellable,
850                                  GAsyncReadyCallback  callback,
851                                  gpointer             user_data)
852 {
853   g_return_if_fail (G_IS_RESOLVER (resolver));
854   g_return_if_fail (rrname != NULL);
855
856   g_resolver_maybe_reload (resolver);
857   G_RESOLVER_GET_CLASS (resolver)->
858     lookup_records_async (resolver, rrname, record_type, cancellable, callback, user_data);
859 }
860
861 /**
862  * g_resolver_lookup_records_finish:
863  * @resolver: a #GResolver
864  * @result: the result passed to your #GAsyncReadyCallback
865  * @error: return location for a #GError, or %NULL
866  *
867  * Retrieves the result of a previous call to
868  * g_resolver_lookup_records_async(). Returns a list of records as #GVariant
869  * tuples. See #GResolverRecordType for information on what the records contain.
870  *
871  * If the DNS resolution failed, @error (if non-%NULL) will be set to
872  * a value from #GResolverError. If the operation was cancelled,
873  * @error will be set to %G_IO_ERROR_CANCELLED.
874  *
875  * Returns: (element-type GVariant) (transfer full): a #GList of #GVariant,
876  * or %NULL on error. You must free each of the records and the list when you are
877  * done with it. (You can use g_list_free_full() with g_variant_unref() to do this.)
878  *
879  * Since: 2.34
880  */
881 GList *
882 g_resolver_lookup_records_finish (GResolver     *resolver,
883                                   GAsyncResult  *result,
884                                   GError       **error)
885 {
886   g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
887   return G_RESOLVER_GET_CLASS (resolver)->
888     lookup_records_finish (resolver, result, error);
889 }
890
891 guint64
892 g_resolver_get_serial (GResolver *resolver)
893 {
894   g_return_val_if_fail (G_IS_RESOLVER (resolver), 0);
895
896   g_resolver_maybe_reload (resolver);
897
898 #ifdef G_OS_UNIX
899   return (guint64) resolver->priv->resolv_conf_timestamp;
900 #else
901   return 1;
902 #endif
903 }
904
905 /**
906  * g_resolver_error_quark:
907  *
908  * Gets the #GResolver Error Quark.
909  *
910  * Returns: a #GQuark.
911  *
912  * Since: 2.22
913  */
914 G_DEFINE_QUARK (g-resolver-error-quark, g_resolver_error)