cleanup
[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 #ifndef G_OS_WIN32
298   struct in_addr ip4addr;
299 #endif
300
301   addr = g_inet_address_new_from_string (hostname);
302   if (addr)
303     {
304       *addrs = g_list_append (NULL, addr);
305       return TRUE;
306     }
307
308   *addrs = NULL;
309
310   /* Reject non-standard IPv4 numbers-and-dots addresses.
311    * g_inet_address_new_from_string() will have accepted any "real" IP
312    * address, so if inet_aton() succeeds, then it's an address we want
313    * to reject.  This check is not necessary for Windows, as getaddrinfo()
314    * already rejects such IPv4 addresses on Windows.
315    */
316 #ifndef G_OS_WIN32
317   if (inet_aton (hostname, &ip4addr))
318     {
319       g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_NOT_FOUND,
320                    _("Error resolving '%s': %s"),
321                    hostname, gai_strerror (EAI_NONAME));
322       return TRUE;
323     }
324 #endif
325
326   return FALSE;
327 }
328
329 /**
330  * g_resolver_lookup_by_name:
331  * @resolver: a #GResolver
332  * @hostname: the hostname to look up
333  * @cancellable: (allow-none): a #GCancellable, or %NULL
334  * @error: return location for a #GError, or %NULL
335  *
336  * Synchronously resolves @hostname to determine its associated IP
337  * address(es). @hostname may be an ASCII-only or UTF-8 hostname, or
338  * the textual form of an IP address (in which case this just becomes
339  * a wrapper around g_inet_address_new_from_string()).
340  *
341  * On success, g_resolver_lookup_by_name() will return a non-empty #GList of
342  * #GInetAddress, sorted in order of preference and guaranteed to not
343  * contain duplicates. That is, if using the result to connect to
344  * @hostname, you should attempt to connect to the first address
345  * first, then the second if the first fails, etc. If you are using
346  * the result to listen on a socket, it is appropriate to add each
347  * result using e.g. g_socket_listener_add_address().
348  *
349  * If the DNS resolution fails, @error (if non-%NULL) will be set to a
350  * value from #GResolverError and %NULL will be returned.
351  *
352  * If @cancellable is non-%NULL, it can be used to cancel the
353  * operation, in which case @error (if non-%NULL) will be set to
354  * %G_IO_ERROR_CANCELLED.
355  *
356  * If you are planning to connect to a socket on the resolved IP
357  * address, it may be easier to create a #GNetworkAddress and use its
358  * #GSocketConnectable interface.
359  *
360  * Returns: (element-type GInetAddress) (transfer full): a non-empty #GList
361  * of #GInetAddress, or %NULL on error. You
362  * must unref each of the addresses and free the list when you are
363  * done with it. (You can use g_resolver_free_addresses() to do this.)
364  *
365  * Since: 2.22
366  */
367 GList *
368 g_resolver_lookup_by_name (GResolver     *resolver,
369                            const gchar   *hostname,
370                            GCancellable  *cancellable,
371                            GError       **error)
372 {
373   GList *addrs;
374   gchar *ascii_hostname = NULL;
375
376   g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
377   g_return_val_if_fail (hostname != NULL, NULL);
378
379   /* Check if @hostname is just an IP address */
380   if (handle_ip_address (hostname, &addrs, error))
381     return addrs;
382
383   if (g_hostname_is_non_ascii (hostname))
384     hostname = ascii_hostname = g_hostname_to_ascii (hostname);
385
386   g_resolver_maybe_reload (resolver);
387   addrs = G_RESOLVER_GET_CLASS (resolver)->
388     lookup_by_name (resolver, hostname, cancellable, error);
389
390   remove_duplicates (addrs);
391
392   g_free (ascii_hostname);
393   return addrs;
394 }
395
396 /**
397  * g_resolver_lookup_by_name_async:
398  * @resolver: a #GResolver
399  * @hostname: the hostname to look up the address of
400  * @cancellable: (allow-none): a #GCancellable, or %NULL
401  * @callback: (scope async): callback to call after resolution completes
402  * @user_data: (closure): data for @callback
403  *
404  * Begins asynchronously resolving @hostname to determine its
405  * associated IP address(es), and eventually calls @callback, which
406  * must call g_resolver_lookup_by_name_finish() to get the result.
407  * See g_resolver_lookup_by_name() for more details.
408  *
409  * Since: 2.22
410  */
411 void
412 g_resolver_lookup_by_name_async (GResolver           *resolver,
413                                  const gchar         *hostname,
414                                  GCancellable        *cancellable,
415                                  GAsyncReadyCallback  callback,
416                                  gpointer             user_data)
417 {
418   gchar *ascii_hostname = NULL;
419   GList *addrs;
420   GError *error = NULL;
421
422   g_return_if_fail (G_IS_RESOLVER (resolver));
423   g_return_if_fail (hostname != NULL);
424
425   /* Check if @hostname is just an IP address */
426   if (handle_ip_address (hostname, &addrs, &error))
427     {
428       GTask *task;
429
430       task = g_task_new (resolver, cancellable, callback, user_data);
431       g_task_set_source_tag (task, g_resolver_lookup_by_name_async);
432       if (addrs)
433         g_task_return_pointer (task, addrs, (GDestroyNotify) g_resolver_free_addresses);
434       else
435         g_task_return_error (task, error);
436       g_object_unref (task);
437       return;
438     }
439
440   if (g_hostname_is_non_ascii (hostname))
441     hostname = ascii_hostname = g_hostname_to_ascii (hostname);
442
443   g_resolver_maybe_reload (resolver);
444   G_RESOLVER_GET_CLASS (resolver)->
445     lookup_by_name_async (resolver, hostname, cancellable, callback, user_data);
446
447   g_free (ascii_hostname);
448 }
449
450 /**
451  * g_resolver_lookup_by_name_finish:
452  * @resolver: a #GResolver
453  * @result: the result passed to your #GAsyncReadyCallback
454  * @error: return location for a #GError, or %NULL
455  *
456  * Retrieves the result of a call to
457  * g_resolver_lookup_by_name_async().
458  *
459  * If the DNS resolution failed, @error (if non-%NULL) will be set to
460  * a value from #GResolverError. If the operation was cancelled,
461  * @error will be set to %G_IO_ERROR_CANCELLED.
462  *
463  * Returns: (element-type GInetAddress) (transfer full): a #GList
464  * of #GInetAddress, or %NULL on error. See g_resolver_lookup_by_name()
465  * for more details.
466  *
467  * Since: 2.22
468  */
469 GList *
470 g_resolver_lookup_by_name_finish (GResolver     *resolver,
471                                   GAsyncResult  *result,
472                                   GError       **error)
473 {
474   GList *addrs;
475
476   g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
477
478   if (g_async_result_legacy_propagate_error (result, error))
479     return NULL;
480   else if (g_async_result_is_tagged (result, g_resolver_lookup_by_name_async))
481     {
482       /* Handle the stringified-IP-addr case */
483       return g_task_propagate_pointer (G_TASK (result), error);
484     }
485
486   addrs = G_RESOLVER_GET_CLASS (resolver)->
487     lookup_by_name_finish (resolver, result, error);
488
489   remove_duplicates (addrs);
490
491   return addrs;
492 }
493
494 /**
495  * g_resolver_free_addresses: (skip)
496  * @addresses: a #GList of #GInetAddress
497  *
498  * Frees @addresses (which should be the return value from
499  * g_resolver_lookup_by_name() or g_resolver_lookup_by_name_finish()).
500  * (This is a convenience method; you can also simply free the results
501  * by hand.)
502  *
503  * Since: 2.22
504  */
505 void
506 g_resolver_free_addresses (GList *addresses)
507 {
508   GList *a;
509
510   for (a = addresses; a; a = a->next)
511     g_object_unref (a->data);
512   g_list_free (addresses);
513 }
514
515 /**
516  * g_resolver_lookup_by_address:
517  * @resolver: a #GResolver
518  * @address: the address to reverse-resolve
519  * @cancellable: (allow-none): a #GCancellable, or %NULL
520  * @error: return location for a #GError, or %NULL
521  *
522  * Synchronously reverse-resolves @address to determine its
523  * associated hostname.
524  *
525  * If the DNS resolution fails, @error (if non-%NULL) will be set to
526  * a value from #GResolverError.
527  *
528  * If @cancellable is non-%NULL, it can be used to cancel the
529  * operation, in which case @error (if non-%NULL) will be set to
530  * %G_IO_ERROR_CANCELLED.
531  *
532  * Returns: a hostname (either ASCII-only, or in ASCII-encoded
533  *     form), or %NULL on error.
534  *
535  * Since: 2.22
536  */
537 gchar *
538 g_resolver_lookup_by_address (GResolver     *resolver,
539                               GInetAddress  *address,
540                               GCancellable  *cancellable,
541                               GError       **error)
542 {
543   g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
544   g_return_val_if_fail (G_IS_INET_ADDRESS (address), NULL);
545
546   g_resolver_maybe_reload (resolver);
547   return G_RESOLVER_GET_CLASS (resolver)->
548     lookup_by_address (resolver, address, cancellable, error);
549 }
550
551 /**
552  * g_resolver_lookup_by_address_async:
553  * @resolver: a #GResolver
554  * @address: the address to reverse-resolve
555  * @cancellable: (allow-none): a #GCancellable, or %NULL
556  * @callback: (scope async): callback to call after resolution completes
557  * @user_data: (closure): data for @callback
558  *
559  * Begins asynchronously reverse-resolving @address to determine its
560  * associated hostname, and eventually calls @callback, which must
561  * call g_resolver_lookup_by_address_finish() to get the final result.
562  *
563  * Since: 2.22
564  */
565 void
566 g_resolver_lookup_by_address_async (GResolver           *resolver,
567                                     GInetAddress        *address,
568                                     GCancellable        *cancellable,
569                                     GAsyncReadyCallback  callback,
570                                     gpointer             user_data)
571 {
572   g_return_if_fail (G_IS_RESOLVER (resolver));
573   g_return_if_fail (G_IS_INET_ADDRESS (address));
574
575   g_resolver_maybe_reload (resolver);
576   G_RESOLVER_GET_CLASS (resolver)->
577     lookup_by_address_async (resolver, address, cancellable, callback, user_data);
578 }
579
580 /**
581  * g_resolver_lookup_by_address_finish:
582  * @resolver: a #GResolver
583  * @result: the result passed to your #GAsyncReadyCallback
584  * @error: return location for a #GError, or %NULL
585  *
586  * Retrieves the result of a previous call to
587  * g_resolver_lookup_by_address_async().
588  *
589  * If the DNS resolution failed, @error (if non-%NULL) will be set to
590  * a value from #GResolverError. If the operation was cancelled,
591  * @error will be set to %G_IO_ERROR_CANCELLED.
592  *
593  * Returns: a hostname (either ASCII-only, or in ASCII-encoded
594  * form), or %NULL on error.
595  *
596  * Since: 2.22
597  */
598 gchar *
599 g_resolver_lookup_by_address_finish (GResolver     *resolver,
600                                      GAsyncResult  *result,
601                                      GError       **error)
602 {
603   g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
604
605   if (g_async_result_legacy_propagate_error (result, error))
606     return NULL;
607
608   return G_RESOLVER_GET_CLASS (resolver)->
609     lookup_by_address_finish (resolver, result, error);
610 }
611
612 static gchar *
613 g_resolver_get_service_rrname (const char *service,
614                                const char *protocol,
615                                const char *domain)
616 {
617   gchar *rrname, *ascii_domain = NULL;
618
619   if (g_hostname_is_non_ascii (domain))
620     domain = ascii_domain = g_hostname_to_ascii (domain);
621
622   rrname = g_strdup_printf ("_%s._%s.%s", service, protocol, domain);
623
624   g_free (ascii_domain);
625   return rrname;
626 }
627
628 /**
629  * g_resolver_lookup_service:
630  * @resolver: a #GResolver
631  * @service: the service type to look up (eg, "ldap")
632  * @protocol: the networking protocol to use for @service (eg, "tcp")
633  * @domain: the DNS domain to look up the service in
634  * @cancellable: (allow-none): a #GCancellable, or %NULL
635  * @error: return location for a #GError, or %NULL
636  *
637  * Synchronously performs a DNS SRV lookup for the given @service and
638  * @protocol in the given @domain and returns an array of #GSrvTarget.
639  * @domain may be an ASCII-only or UTF-8 hostname. Note also that the
640  * @service and @protocol arguments do not include the leading underscore
641  * that appears in the actual DNS entry.
642  *
643  * On success, g_resolver_lookup_service() will return a non-empty #GList of
644  * #GSrvTarget, sorted in order of preference. (That is, you should
645  * attempt to connect to the first target first, then the second if
646  * the first fails, etc.)
647  *
648  * If the DNS resolution fails, @error (if non-%NULL) will be set to
649  * a value from #GResolverError and %NULL will be returned.
650  *
651  * If @cancellable is non-%NULL, it can be used to cancel the
652  * operation, in which case @error (if non-%NULL) will be set to
653  * %G_IO_ERROR_CANCELLED.
654  *
655  * If you are planning to connect to the service, it is usually easier
656  * to create a #GNetworkService and use its #GSocketConnectable
657  * interface.
658  *
659  * Returns: (element-type GSrvTarget) (transfer full): a non-empty #GList of
660  * #GSrvTarget, or %NULL on error. You must free each of the targets and the
661  * list when you are done with it. (You can use g_resolver_free_targets() to do
662  * this.)
663  *
664  * Since: 2.22
665  */
666 GList *
667 g_resolver_lookup_service (GResolver     *resolver,
668                            const gchar   *service,
669                            const gchar   *protocol,
670                            const gchar   *domain,
671                            GCancellable  *cancellable,
672                            GError       **error)
673 {
674   GList *targets;
675   gchar *rrname;
676
677   g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
678   g_return_val_if_fail (service != NULL, NULL);
679   g_return_val_if_fail (protocol != NULL, NULL);
680   g_return_val_if_fail (domain != NULL, NULL);
681
682   rrname = g_resolver_get_service_rrname (service, protocol, domain);
683
684   g_resolver_maybe_reload (resolver);
685   targets = G_RESOLVER_GET_CLASS (resolver)->
686     lookup_service (resolver, rrname, cancellable, error);
687
688   g_free (rrname);
689   return targets;
690 }
691
692 /**
693  * g_resolver_lookup_service_async:
694  * @resolver: a #GResolver
695  * @service: the service type to look up (eg, "ldap")
696  * @protocol: the networking protocol to use for @service (eg, "tcp")
697  * @domain: the DNS domain to look up the service in
698  * @cancellable: (allow-none): a #GCancellable, or %NULL
699  * @callback: (scope async): callback to call after resolution completes
700  * @user_data: (closure): data for @callback
701  *
702  * Begins asynchronously performing a DNS SRV lookup for the given
703  * @service and @protocol in the given @domain, and eventually calls
704  * @callback, which must call g_resolver_lookup_service_finish() to
705  * get the final result. See g_resolver_lookup_service() for more
706  * details.
707  *
708  * Since: 2.22
709  */
710 void
711 g_resolver_lookup_service_async (GResolver           *resolver,
712                                  const gchar         *service,
713                                  const gchar         *protocol,
714                                  const gchar         *domain,
715                                  GCancellable        *cancellable,
716                                  GAsyncReadyCallback  callback,
717                                  gpointer             user_data)
718 {
719   gchar *rrname;
720
721   g_return_if_fail (G_IS_RESOLVER (resolver));
722   g_return_if_fail (service != NULL);
723   g_return_if_fail (protocol != NULL);
724   g_return_if_fail (domain != NULL);
725
726   rrname = g_resolver_get_service_rrname (service, protocol, domain);
727
728   g_resolver_maybe_reload (resolver);
729   G_RESOLVER_GET_CLASS (resolver)->
730     lookup_service_async (resolver, rrname, cancellable, callback, user_data);
731
732   g_free (rrname);
733 }
734
735 /**
736  * g_resolver_lookup_service_finish:
737  * @resolver: a #GResolver
738  * @result: the result passed to your #GAsyncReadyCallback
739  * @error: return location for a #GError, or %NULL
740  *
741  * Retrieves the result of a previous call to
742  * g_resolver_lookup_service_async().
743  *
744  * If the DNS resolution failed, @error (if non-%NULL) will be set to
745  * a value from #GResolverError. If the operation was cancelled,
746  * @error will be set to %G_IO_ERROR_CANCELLED.
747  *
748  * Returns: (element-type GSrvTarget) (transfer full): a non-empty #GList of
749  * #GSrvTarget, or %NULL on error. See g_resolver_lookup_service() for more
750  * details.
751  *
752  * Since: 2.22
753  */
754 GList *
755 g_resolver_lookup_service_finish (GResolver     *resolver,
756                                   GAsyncResult  *result,
757                                   GError       **error)
758 {
759   g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
760
761   if (g_async_result_legacy_propagate_error (result, error))
762     return NULL;
763
764   return G_RESOLVER_GET_CLASS (resolver)->
765     lookup_service_finish (resolver, result, error);
766 }
767
768 /**
769  * g_resolver_free_targets: (skip)
770  * @targets: a #GList of #GSrvTarget
771  *
772  * Frees @targets (which should be the return value from
773  * g_resolver_lookup_service() or g_resolver_lookup_service_finish()).
774  * (This is a convenience method; you can also simply free the
775  * results by hand.)
776  *
777  * Since: 2.22
778  */
779 void
780 g_resolver_free_targets (GList *targets)
781 {
782   GList *t;
783
784   for (t = targets; t; t = t->next)
785     g_srv_target_free (t->data);
786   g_list_free (targets);
787 }
788
789 /**
790  * g_resolver_lookup_records:
791  * @resolver: a #GResolver
792  * @rrname: the DNS name to lookup the record for
793  * @record_type: the type of DNS record to lookup
794  * @cancellable: (allow-none): a #GCancellable, or %NULL
795  * @error: return location for a #GError, or %NULL
796  *
797  * Synchronously performs a DNS record lookup for the given @rrname and returns
798  * a list of records as #GVariant tuples. See #GResolverRecordType for
799  * information on what the records contain for each @record_type.
800  *
801  * If the DNS resolution fails, @error (if non-%NULL) will be set to
802  * a value from #GResolverError and %NULL will be returned.
803  *
804  * If @cancellable is non-%NULL, it can be used to cancel the
805  * operation, in which case @error (if non-%NULL) will be set to
806  * %G_IO_ERROR_CANCELLED.
807  *
808  * Returns: (element-type GVariant) (transfer full): a non-empty #GList of
809  * #GVariant, or %NULL on error. You must free each of the records and the list
810  * when you are done with it. (You can use g_list_free_full() with
811  * g_variant_unref() to do this.)
812  *
813  * Since: 2.34
814  */
815 GList *
816 g_resolver_lookup_records (GResolver            *resolver,
817                            const gchar          *rrname,
818                            GResolverRecordType   record_type,
819                            GCancellable         *cancellable,
820                            GError              **error)
821 {
822   GList *records;
823
824   g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
825   g_return_val_if_fail (rrname != NULL, NULL);
826
827   g_resolver_maybe_reload (resolver);
828   records = G_RESOLVER_GET_CLASS (resolver)->
829     lookup_records (resolver, rrname, record_type, cancellable, error);
830
831   return records;
832 }
833
834 /**
835  * g_resolver_lookup_records_async:
836  * @resolver: a #GResolver
837  * @rrname: the DNS name to lookup the record for
838  * @record_type: the type of DNS record to lookup
839  * @cancellable: (allow-none): a #GCancellable, or %NULL
840  * @callback: (scope async): callback to call after resolution completes
841  * @user_data: (closure): data for @callback
842  *
843  * Begins asynchronously performing a DNS lookup for the given
844  * @rrname, and eventually calls @callback, which must call
845  * g_resolver_lookup_records_finish() to get the final result. See
846  * g_resolver_lookup_records() for more details.
847  *
848  * Since: 2.34
849  */
850 void
851 g_resolver_lookup_records_async (GResolver           *resolver,
852                                  const gchar         *rrname,
853                                  GResolverRecordType  record_type,
854                                  GCancellable        *cancellable,
855                                  GAsyncReadyCallback  callback,
856                                  gpointer             user_data)
857 {
858   g_return_if_fail (G_IS_RESOLVER (resolver));
859   g_return_if_fail (rrname != NULL);
860
861   g_resolver_maybe_reload (resolver);
862   G_RESOLVER_GET_CLASS (resolver)->
863     lookup_records_async (resolver, rrname, record_type, cancellable, callback, user_data);
864 }
865
866 /**
867  * g_resolver_lookup_records_finish:
868  * @resolver: a #GResolver
869  * @result: the result passed to your #GAsyncReadyCallback
870  * @error: return location for a #GError, or %NULL
871  *
872  * Retrieves the result of a previous call to
873  * g_resolver_lookup_records_async(). Returns a non-empty list of records as
874  * #GVariant tuples. See #GResolverRecordType for information on what the
875  * records contain.
876  *
877  * If the DNS resolution failed, @error (if non-%NULL) will be set to
878  * a value from #GResolverError. If the operation was cancelled,
879  * @error will be set to %G_IO_ERROR_CANCELLED.
880  *
881  * Returns: (element-type GVariant) (transfer full): a non-empty #GList of
882  * #GVariant, or %NULL on error. You must free each of the records and the list
883  * when you are done with it. (You can use g_list_free_full() with
884  * g_variant_unref() to do this.)
885  *
886  * Since: 2.34
887  */
888 GList *
889 g_resolver_lookup_records_finish (GResolver     *resolver,
890                                   GAsyncResult  *result,
891                                   GError       **error)
892 {
893   g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
894   return G_RESOLVER_GET_CLASS (resolver)->
895     lookup_records_finish (resolver, result, error);
896 }
897
898 guint64
899 g_resolver_get_serial (GResolver *resolver)
900 {
901   g_return_val_if_fail (G_IS_RESOLVER (resolver), 0);
902
903   g_resolver_maybe_reload (resolver);
904
905 #ifdef G_OS_UNIX
906   return (guint64) resolver->priv->resolv_conf_timestamp;
907 #else
908   return 1;
909 #endif
910 }
911
912 /**
913  * g_resolver_error_quark:
914  *
915  * Gets the #GResolver Error Quark.
916  *
917  * Returns: a #GQuark.
918  *
919  * Since: 2.22
920  */
921 G_DEFINE_QUARK (g-resolver-error-quark, g_resolver_error)