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