GResolver wrappers: GNetworkAddress, GNetworkService, GSocketConnectable
[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, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include "config.h"
24 #include <glib.h>
25 #include "glibintl.h"
26
27 #include "gresolver.h"
28 #include "gnetworkingprivate.h"
29 #include "gasyncresult.h"
30 #include "ginetaddress.h"
31 #include "ginetsocketaddress.h"
32 #include "gsimpleasyncresult.h"
33 #include "gsrvtarget.h"
34
35 #ifdef G_OS_UNIX
36 #include "gunixresolver.h"
37 #endif
38 #ifdef G_OS_WIN32
39 #include "gwin32resolver.h"
40 #endif
41
42 #include <stdlib.h>
43
44 #include "gioalias.h"
45
46 /**
47  * SECTION:gresolver
48  * @short_description: Asynchronous and cancellable DNS resolver
49  * @include: gio/gio.h
50  *
51  * #GResolver provides cancellable synchronous and asynchronous DNS
52  * resolution, for hostnames (g_resolver_lookup_by_address(),
53  * g_resolver_lookup_by_name() and their async variants) and SRV
54  * (service) records (g_resolver_lookup_service()).
55  *
56  * #GNetworkAddress and #GNetworkService provide wrappers around
57  * #GResolver functionality that also implement #GSocketConnectable,
58  * making it easy to connect to a remote host/service.
59  **/
60
61 /**
62  * GResolver:
63  *
64  * The object that handles DNS resolution. Use g_resolver_get_default()
65  * to get the default resolver.
66  */
67 G_DEFINE_TYPE (GResolver, g_resolver, G_TYPE_OBJECT)
68
69 static void
70 g_resolver_class_init (GResolverClass *resolver_class)
71 {
72   /* Make sure _g_networking_init() has been called */
73   (void) g_inet_address_get_type ();
74
75   /* Initialize _g_resolver_addrinfo_hints */
76 #ifdef AI_ADDRCONFIG
77   _g_resolver_addrinfo_hints.ai_flags |= AI_ADDRCONFIG;
78 #endif
79   /* These two don't actually matter, they just get copied into the
80    * returned addrinfo structures (and then we ignore them). But if
81    * we leave them unset, we'll get back duplicate answers.
82    */
83   _g_resolver_addrinfo_hints.ai_socktype = SOCK_STREAM;
84   _g_resolver_addrinfo_hints.ai_protocol = IPPROTO_TCP;
85 }
86
87 static void
88 g_resolver_init (GResolver *resolver)
89 {
90   ;
91 }
92
93 static GResolver *default_resolver;
94
95 /**
96  * g_resolver_get_default:
97  *
98  * Gets the default #GResolver. You should unref it when you are done
99  * with it. #GResolver may use its reference count as a hint about how
100  * many threads/processes, etc it should allocate for concurrent DNS
101  * resolutions.
102  *
103  * Return value: the #GResolver.
104  *
105  * Since: 2.22
106  **/
107 GResolver *
108 g_resolver_get_default (void)
109 {
110   if (!default_resolver)
111     {
112       if (g_thread_supported ())
113         default_resolver = g_object_new (G_TYPE_THREADED_RESOLVER, NULL);
114       else
115         {
116 #if defined(G_OS_UNIX)
117           default_resolver = g_object_new (G_TYPE_UNIX_RESOLVER, NULL);
118 #elif defined(G_OS_WIN32)
119           default_resolver = g_object_new (G_TYPE_WIN32_RESOLVER, NULL);
120 #endif
121         }
122     }
123
124   return g_object_ref (default_resolver);
125 }
126
127 /**
128  * g_resolver_set_default:
129  * @resolver: the new default #GResolver
130  *
131  * Sets @resolver to be the application's default resolver (reffing
132  * @resolver, and unreffing the previous default resolver, if any).
133  * Future calls to g_resolver_get_default() will return this resolver.
134  *
135  * This can be used if an application wants to perform any sort of DNS
136  * caching or "pinning"; it can implement its own #GResolver that
137  * calls the original default resolver for DNS operations, and
138  * implements its own cache policies on top of that, and then set
139  * itself as the default resolver for all later code to use.
140  *
141  * Since: 2.22
142  **/
143 void
144 g_resolver_set_default (GResolver *resolver)
145 {
146   if (default_resolver)
147     g_object_unref (default_resolver);
148   default_resolver = g_object_ref (resolver);
149 }
150
151
152 /**
153  * g_resolver_lookup_by_name:
154  * @resolver: a #GResolver
155  * @hostname: the hostname to look up
156  * @cancellable: a #GCancellable, or %NULL
157  * @error: return location for a #GError, or %NULL
158  *
159  * Synchronously resolves @hostname to determine its associated IP
160  * address(es). @hostname may be an ASCII-only or UTF-8 hostname, or
161  * the textual form of an IP address (in which case this just becomes
162  * a wrapper around g_inet_address_new_from_string()).
163  *
164  * On success, g_resolver_lookup_by_name() will return a #GList of
165  * #GInetAddress, sorted in order of preference. (That is, you should
166  * attempt to connect to the first address first, then the second if
167  * the first fails, etc.)
168  *
169  * If the DNS resolution fails, @error (if non-%NULL) will be set to a
170  * value from #GResolverError.
171  *
172  * If @cancellable is non-%NULL, it can be used to cancel the
173  * operation, in which case @error (if non-%NULL) will be set to
174  * %G_IO_ERROR_CANCELLED.
175  *
176  * If you are planning to connect to a socket on the resolved IP
177  * address, it may be easier to create a #GNetworkAddress and use its
178  * #GSocketConnectable interface.
179  *
180  * Return value: a #GList of #GInetAddress, or %NULL on error. You
181  * must unref each of the addresses and free the list when you are
182  * done with it. (You can use g_resolver_free_addresses() to do this.)
183  *
184  * Since: 2.22
185  **/
186 GList *
187 g_resolver_lookup_by_name (GResolver     *resolver,
188                            const gchar   *hostname,
189                            GCancellable  *cancellable,
190                            GError       **error)
191 {
192   GInetAddress *addr;
193   GList *addrs;
194   gchar *ascii_hostname = NULL;
195
196   g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
197   g_return_val_if_fail (hostname != NULL, NULL);
198
199   /* Check if @hostname is just an IP address */
200   addr = g_inet_address_new_from_string (hostname);
201   if (addr)
202     return g_list_append (NULL, addr);
203
204   if (g_hostname_is_non_ascii (hostname))
205     hostname = ascii_hostname = g_hostname_to_ascii (hostname);
206
207   addrs = G_RESOLVER_GET_CLASS (resolver)->
208     lookup_by_name (resolver, hostname, cancellable, error);
209
210   g_free (ascii_hostname);
211   return addrs;
212 }
213
214 /**
215  * g_resolver_lookup_by_name_async:
216  * @resolver: a #GResolver
217  * @hostname: the hostname to look up the address of
218  * @cancellable: a #GCancellable, or %NULL
219  * @callback: callback to call after resolution completes
220  * @user_data: data for @callback
221  *
222  * Begins asynchronously resolving @hostname to determine its
223  * associated IP address(es), and eventually calls @callback, which
224  * must call g_resolver_lookup_by_name_finish() to get the result. See
225  * g_resolver_lookup_by_name() for more details.
226  *
227  * Since: 2.22
228  **/
229 void
230 g_resolver_lookup_by_name_async (GResolver           *resolver,
231                                  const gchar         *hostname,
232                                  GCancellable        *cancellable,
233                                  GAsyncReadyCallback  callback,
234                                  gpointer             user_data)
235 {
236   GInetAddress *addr;
237   gchar *ascii_hostname = NULL;
238
239   g_return_if_fail (G_IS_RESOLVER (resolver));
240   g_return_if_fail (hostname != NULL);
241
242   /* Check if @hostname is just an IP address */
243   addr = g_inet_address_new_from_string (hostname);
244   if (addr)
245     {
246       GSimpleAsyncResult *simple;
247       GList *addrs;
248
249       simple = g_simple_async_result_new (G_OBJECT (resolver),
250                                           callback, user_data,
251                                           g_resolver_lookup_by_name_async);
252
253       addrs = g_list_append (NULL, addr);
254       g_simple_async_result_set_op_res_gpointer (simple, addrs, (GDestroyNotify)g_resolver_free_addresses);
255       g_simple_async_result_complete_in_idle (simple);
256       g_object_unref (simple);
257       return;
258     }
259
260   if (g_hostname_is_non_ascii (hostname))
261     hostname = ascii_hostname = g_hostname_to_ascii (hostname);
262
263   G_RESOLVER_GET_CLASS (resolver)->
264     lookup_by_name_async (resolver, hostname, cancellable, callback, user_data);
265
266   g_free (ascii_hostname);
267 }
268
269 /**
270  * g_resolver_lookup_by_name_finish:
271  * @resolver: a #GResolver
272  * @result: the result passed to your #GAsyncReadyCallback
273  * @error: return location for a #GError, or %NULL
274  *
275  * Retrieves the result of a call to
276  * g_resolver_lookup_by_name_async().
277  *
278  * If the DNS resolution failed, @error (if non-%NULL) will be set to
279  * a value from #GResolverError. If the operation was cancelled,
280  * @error will be set to %G_IO_ERROR_CANCELLED.
281  *
282  * Return value: a #GList of #GInetAddress, or %NULL on error. See
283  * g_resolver_lookup_by_name() for more details.
284  *
285  * Since: 2.22
286  **/
287 GList *
288 g_resolver_lookup_by_name_finish (GResolver     *resolver,
289                                   GAsyncResult  *result,
290                                   GError       **error)
291 {
292   g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
293
294   if (G_IS_SIMPLE_ASYNC_RESULT (result))
295     {
296       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
297
298       if (g_simple_async_result_propagate_error (simple, error))
299         return NULL;
300
301       /* Handle the stringified-IP-addr case */
302       if (g_simple_async_result_get_source_tag (simple) == g_resolver_lookup_by_name_async)
303         {
304           GList *addrs;
305
306           addrs = g_simple_async_result_get_op_res_gpointer (simple);
307           g_simple_async_result_set_op_res_gpointer (simple, NULL, NULL);
308           return addrs;
309         }
310     }
311
312   return G_RESOLVER_GET_CLASS (resolver)->
313     lookup_by_name_finish (resolver, result, error);
314 }
315
316 /**
317  * g_resolver_free_addresses:
318  * @addresses: a #GList of #GInetAddress
319  *
320  * Frees @addresses (which should be the return value from
321  * g_resolver_lookup_by_name() or g_resolver_lookup_by_name_finish()).
322  * (This is a convenience method; you can also simply free the results
323  * by hand.)
324  *
325  * Since: 2.22
326  **/
327 void
328 g_resolver_free_addresses (GList *addresses)
329 {
330   GList *a;
331
332   for (a = addresses; a; a = a->next)
333     g_object_unref (a->data);
334   g_list_free (addresses);
335 }
336
337 /**
338  * g_resolver_lookup_by_address:
339  * @resolver: a #GResolver
340  * @address: the address to reverse-resolve
341  * @cancellable: a #GCancellable, or %NULL
342  * @error: return location for a #GError, or %NULL
343  *
344  * Synchronously reverse-resolves @address to determine its
345  * associated hostname.
346  *
347  * If the DNS resolution fails, @error (if non-%NULL) will be set to
348  * a 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  * Return value: a hostname (either ASCII-only, or in ASCII-encoded
355  * form), or %NULL on error.
356  *
357  * Since: 2.22
358  **/
359 gchar *
360 g_resolver_lookup_by_address (GResolver     *resolver,
361                               GInetAddress  *address,
362                               GCancellable  *cancellable,
363                               GError       **error)
364 {
365   g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
366   g_return_val_if_fail (G_IS_INET_ADDRESS (address), NULL);
367
368   return G_RESOLVER_GET_CLASS (resolver)->
369     lookup_by_address (resolver, address, cancellable, error);
370 }
371
372 /**
373  * g_resolver_lookup_by_address_async:
374  * @resolver: a #GResolver
375  * @address: the address to reverse-resolve
376  * @cancellable: a #GCancellable, or %NULL
377  * @callback: callback to call after resolution completes
378  * @user_data: data for @callback
379  *
380  * Begins asynchronously reverse-resolving @address to determine its
381  * associated hostname, and eventually calls @callback, which must
382  * call g_resolver_lookup_by_address_finish() to get the final result.
383  *
384  * Since: 2.22
385  **/
386 void
387 g_resolver_lookup_by_address_async (GResolver           *resolver,
388                                     GInetAddress        *address,
389                                     GCancellable        *cancellable,
390                                     GAsyncReadyCallback  callback,
391                                     gpointer             user_data)
392 {
393   g_return_if_fail (G_IS_RESOLVER (resolver));
394   g_return_if_fail (G_IS_INET_ADDRESS (address));
395
396   G_RESOLVER_GET_CLASS (resolver)->
397     lookup_by_address_async (resolver, address, cancellable, callback, user_data);
398 }
399
400 /**
401  * g_resolver_lookup_by_address_finish:
402  * @resolver: a #GResolver
403  * @result: the result passed to your #GAsyncReadyCallback
404  * @error: return location for a #GError, or %NULL
405  *
406  * Retrieves the result of a previous call to
407  * g_resolver_lookup_by_address_async().
408  *
409  * If the DNS resolution failed, @error (if non-%NULL) will be set to
410  * a value from #GResolverError. If the operation was cancelled,
411  * @error will be set to %G_IO_ERROR_CANCELLED.
412  *
413  * Return value: a hostname (either ASCII-only, or in ASCII-encoded
414  * form), or %NULL on error.
415  *
416  * Since: 2.22
417  **/
418 gchar *
419 g_resolver_lookup_by_address_finish (GResolver     *resolver,
420                                      GAsyncResult  *result,
421                                      GError       **error)
422 {
423   g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
424
425   if (G_IS_SIMPLE_ASYNC_RESULT (result))
426     {
427       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
428
429       if (g_simple_async_result_propagate_error (simple, error))
430         return NULL;
431     }
432
433   return G_RESOLVER_GET_CLASS (resolver)->
434     lookup_by_address_finish (resolver, result, error);
435 }
436
437 static gchar *
438 g_resolver_get_service_rrname (const char *service,
439                                const char *protocol,
440                                const char *domain)
441 {
442   gchar *rrname, *ascii_domain = NULL;
443
444   if (g_hostname_is_non_ascii (domain))
445     domain = ascii_domain = g_hostname_to_ascii (domain);
446
447   rrname = g_strdup_printf ("_%s._%s.%s", service, protocol, domain);
448
449   g_free (ascii_domain);
450   return rrname;
451 }
452
453 /**
454  * g_resolver_lookup_service:
455  * @resolver: a #GResolver
456  * @service: the service type to look up (eg, "ldap")
457  * @protocol: the networking protocol to use for @service (eg, "tcp")
458  * @domain: the DNS domain to look up the service in
459  * @cancellable: a #GCancellable, or %NULL
460  * @error: return location for a #GError, or %NULL
461  *
462  * Synchronously performs a DNS SRV lookup for the given @service and
463  * @protocol in the given @domain and returns an array of #GSrvTarget.
464  * @domain may be an ASCII-only or UTF-8 hostname. Note also that the
465  * @service and @protocol arguments <emphasis>do not</emphasis>
466  * include the leading underscore that appears in the actual DNS
467  * entry.
468  *
469  * On success, g_resolver_lookup_service() will return a #GList of
470  * #GSrvTarget, sorted in order of preference. (That is, you should
471  * attempt to connect to the first target first, then the second if
472  * the first fails, etc.)
473  *
474  * If the DNS resolution fails, @error (if non-%NULL) will be set to
475  * a value from #GResolverError.
476  *
477  * If @cancellable is non-%NULL, it can be used to cancel the
478  * operation, in which case @error (if non-%NULL) will be set to
479  * %G_IO_ERROR_CANCELLED.
480  *
481  * If you are planning to connect to the service, it is usually easier
482  * to create a #GNetworkService and use its #GSocketConnectable
483  * interface.
484  *
485  * Return value: a #GList of #GSrvTarget, or %NULL on error. You must
486  * free each of the targets and the list when you are done with it.
487  * (You can use g_resolver_free_targets() to do this.)
488  *
489  * Since: 2.22
490  **/
491 GList *
492 g_resolver_lookup_service (GResolver     *resolver,
493                            const gchar   *service,
494                            const gchar   *protocol,
495                            const gchar   *domain,
496                            GCancellable  *cancellable,
497                            GError       **error)
498 {
499   GList *targets;
500   gchar *rrname;
501
502   g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
503   g_return_val_if_fail (service != NULL, NULL);
504   g_return_val_if_fail (protocol != NULL, NULL);
505   g_return_val_if_fail (domain != NULL, NULL);
506
507   rrname = g_resolver_get_service_rrname (service, protocol, domain);
508
509   targets = G_RESOLVER_GET_CLASS (resolver)->
510     lookup_service (resolver, rrname, cancellable, error);
511
512   g_free (rrname);
513   return targets;
514 }
515
516 /**
517  * g_resolver_lookup_service_async:
518  * @resolver: a #GResolver
519  * @service: the service type to look up (eg, "ldap")
520  * @protocol: the networking protocol to use for @service (eg, "tcp")
521  * @domain: the DNS domain to look up the service in
522  * @cancellable: a #GCancellable, or %NULL
523  * @callback: callback to call after resolution completes
524  * @user_data: data for @callback
525  *
526  * Begins asynchronously performing a DNS SRV lookup for the given
527  * @service and @protocol in the given @domain, and eventually calls
528  * @callback, which must call g_resolver_lookup_service_finish() to
529  * get the final result. See g_resolver_lookup_service() for more
530  * details.
531  *
532  * Since: 2.22
533  **/
534 void
535 g_resolver_lookup_service_async (GResolver           *resolver,
536                                  const gchar         *service,
537                                  const gchar         *protocol,
538                                  const gchar         *domain,
539                                  GCancellable        *cancellable,
540                                  GAsyncReadyCallback  callback,
541                                  gpointer             user_data)
542 {
543   gchar *rrname;
544
545   g_return_if_fail (G_IS_RESOLVER (resolver));
546   g_return_if_fail (service != NULL);
547   g_return_if_fail (protocol != NULL);
548   g_return_if_fail (domain != NULL);
549
550   rrname = g_resolver_get_service_rrname (service, protocol, domain);
551
552   G_RESOLVER_GET_CLASS (resolver)->
553     lookup_service_async (resolver, rrname, cancellable, callback, user_data);
554
555   g_free (rrname);
556 }
557
558 /**
559  * g_resolver_lookup_service_finish:
560  * @resolver: a #GResolver
561  * @result: the result passed to your #GAsyncReadyCallback
562  * @error: return location for a #GError, or %NULL
563  *
564  * Retrieves the result of a previous call to
565  * g_resolver_lookup_service_async().
566  *
567  * If the DNS resolution failed, @error (if non-%NULL) will be set to
568  * a value from #GResolverError. If the operation was cancelled,
569  * @error will be set to %G_IO_ERROR_CANCELLED.
570  *
571  * Return value: a #GList of #GSrvTarget, or %NULL on error. See
572  * g_resolver_lookup_service() for more details.
573  *
574  * Since: 2.22
575  **/
576 GList *
577 g_resolver_lookup_service_finish (GResolver     *resolver,
578                                   GAsyncResult  *result,
579                                   GError       **error)
580 {
581   g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
582
583   if (G_IS_SIMPLE_ASYNC_RESULT (result))
584     {
585       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
586
587       if (g_simple_async_result_propagate_error (simple, error))
588         return NULL;
589     }
590
591   return G_RESOLVER_GET_CLASS (resolver)->
592     lookup_service_finish (resolver, result, error);
593 }
594
595 /**
596  * g_resolver_free_targets:
597  * @targets: a #GList of #GSrvTarget
598  *
599  * Frees @targets (which should be the return value from
600  * g_resolver_lookup_service() or g_resolver_lookup_service_finish()).
601  * (This is a convenience method; you can also simply free the
602  * results by hand.)
603  *
604  * Since: 2.22
605  **/
606 void
607 g_resolver_free_targets (GList *targets)
608 {
609   GList *t;
610
611   for (t = targets; t; t = t->next)
612     g_srv_target_free (t->data);
613   g_list_free (targets);
614 }
615
616 /**
617  * g_resolver_error_quark:
618  * 
619  * Gets the #GResolver Error Quark.
620  *
621  * Return value: a #GQuark.
622  *
623  * Since: 2.22
624  **/
625 GQuark
626 g_resolver_error_quark (void)
627 {
628   return g_quark_from_static_string ("g-resolver-error-quark");
629 }
630
631
632 static GResolverError
633 g_resolver_error_from_addrinfo_error (gint err)
634 {
635   switch (err)
636     {
637     case EAI_FAIL:
638     case EAI_NODATA:
639     case EAI_NONAME:
640       return G_RESOLVER_ERROR_NOT_FOUND;
641
642     case EAI_AGAIN:
643       return G_RESOLVER_ERROR_TEMPORARY_FAILURE;
644
645     default:
646       return G_RESOLVER_ERROR_INTERNAL;
647     }
648 }
649
650 struct addrinfo _g_resolver_addrinfo_hints;
651
652 /* Private method to process a getaddrinfo() response. */
653 GList *
654 _g_resolver_addresses_from_addrinfo (const char       *hostname,
655                                      struct addrinfo  *res,
656                                      gint              gai_retval,
657                                      GError          **error)
658 {
659   struct addrinfo *ai;
660   GSocketAddress *sockaddr;
661   GInetAddress *addr;
662   GList *addrs;
663
664   if (gai_retval != 0)
665     {
666       g_set_error (error, G_RESOLVER_ERROR,
667                    g_resolver_error_from_addrinfo_error (gai_retval),
668                    _("Error resolving '%s': %s"),
669                    hostname, gai_strerror (gai_retval));
670       return NULL;
671     }
672
673   g_return_val_if_fail (res != NULL, NULL);
674
675   addrs = NULL;
676   for (ai = res; ai; ai = ai->ai_next)
677     {
678       sockaddr = g_socket_address_new_from_native (ai->ai_addr, ai->ai_addrlen);
679       if (!sockaddr || !G_IS_INET_SOCKET_ADDRESS (sockaddr))
680         continue;
681
682       addr = g_object_ref (g_inet_socket_address_get_address ((GInetSocketAddress *)sockaddr));
683       addrs = g_list_prepend (addrs, addr);
684       g_object_unref (sockaddr);
685     }
686
687   return g_list_reverse (addrs);
688 }
689
690 /* Private method to set up a getnameinfo() request */
691 void
692 _g_resolver_address_to_sockaddr (GInetAddress            *address,
693                                  struct sockaddr_storage *sa,
694                                  gsize                   *sa_len)
695 {
696   GSocketAddress *sockaddr;
697
698   sockaddr = g_inet_socket_address_new (address, 0);
699   g_socket_address_to_native (sockaddr, (struct sockaddr *)sa, sizeof (*sa));
700   *sa_len = g_socket_address_get_native_size (sockaddr);
701   g_object_unref (sockaddr);
702 }
703
704 /* Private method to process a getnameinfo() response. */
705 char *
706 _g_resolver_name_from_nameinfo (GInetAddress  *address,
707                                 const gchar   *name,
708                                 gint           gni_retval,
709                                 GError       **error)
710 {
711   if (gni_retval != 0)
712     {
713       gchar *phys;
714
715       phys = g_inet_address_to_string (address);
716       g_set_error (error, G_RESOLVER_ERROR,
717                    g_resolver_error_from_addrinfo_error (gni_retval),
718                    _("Error reverse-resolving '%s': %s"),
719                    phys ? phys : "(unknown)", gai_strerror (gni_retval));
720       g_free (phys);
721       return NULL;
722     }
723
724   return g_strdup (name);
725 }
726
727 #if defined(G_OS_UNIX)
728 /* Private method to process a res_query response into GSrvTargets */
729 GList *
730 _g_resolver_targets_from_res_query (const gchar      *rrname,
731                                     guchar           *answer,
732                                     gint              len,
733                                     gint              herr,
734                                     GError          **error)
735 {
736   gint count;
737   gchar namebuf[1024];
738   guchar *end, *p;
739   guint16 type, qclass, rdlength, priority, weight, port;
740   guint32 ttl;
741   HEADER *header;
742   GSrvTarget *target;
743   GList *targets;
744
745   if (len <= 0)
746     {
747       GResolverError errnum;
748       const gchar *format;
749
750       if (len == 0 || herr == HOST_NOT_FOUND || herr == NO_DATA)
751         {
752           errnum = G_RESOLVER_ERROR_NOT_FOUND;
753           format = _("No service record for '%s'");
754         }
755       else if (herr == TRY_AGAIN)
756         {
757           errnum = G_RESOLVER_ERROR_TEMPORARY_FAILURE;
758           format = _("Temporarily unable to resolve '%s'");
759         }
760       else
761         {
762           errnum = G_RESOLVER_ERROR_INTERNAL;
763           format = _("Error resolving '%s'");
764         }
765
766       g_set_error (error, G_RESOLVER_ERROR, errnum, format, rrname);
767       return NULL;
768     }
769
770   targets = NULL;
771
772   header = (HEADER *)answer;
773   p = answer + sizeof (HEADER);
774   end = answer + len;
775
776   /* Skip query */
777   count = ntohs (header->qdcount);
778   while (count-- && p < end)
779     {
780       p += dn_expand (answer, end, p, namebuf, sizeof (namebuf));
781       p += 4;
782     }
783
784   /* Read answers */
785   count = ntohs (header->ancount);
786   while (count-- && p < end)
787     {
788       p += dn_expand (answer, end, p, namebuf, sizeof (namebuf));
789       GETSHORT (type, p);
790       GETSHORT (qclass, p);
791       GETLONG  (ttl, p);
792       GETSHORT (rdlength, p);
793
794       if (type != T_SRV || qclass != C_IN)
795         {
796           p += rdlength;
797           continue;
798         }
799
800       GETSHORT (priority, p);
801       GETSHORT (weight, p);
802       GETSHORT (port, p);
803       p += dn_expand (answer, end, p, namebuf, sizeof (namebuf));
804
805       target = g_srv_target_new (namebuf, port, priority, weight);
806       targets = g_list_prepend (targets, target);
807     }
808
809   return g_srv_target_list_sort (targets);
810 }
811 #elif defined(G_OS_WIN32)
812 /* Private method to process a DnsQuery response into GSrvTargets */
813 GList *
814 _g_resolver_targets_from_DnsQuery (const gchar  *rrname,
815                                    DNS_STATUS    status,
816                                    DNS_RECORD   *results,
817                                    GError      **error)
818 {
819   DNS_RECORD *rec;
820   GSrvTarget *target;
821   GList *targets;
822
823   if (status != ERROR_SUCCESS)
824     {
825       GResolverError errnum;
826       const gchar *format;
827
828       if (status == DNS_ERROR_RCODE_NAME_ERROR)
829         {
830           errnum = G_RESOLVER_ERROR_NOT_FOUND;
831           format = _("No service record for '%s'");
832         }
833       else if (status == DNS_ERROR_RCODE_SERVER_FAILURE)
834         {
835           errnum = G_RESOLVER_ERROR_TEMPORARY_FAILURE;
836           format = _("Temporarily unable to resolve '%s'");
837         }
838       else
839         {
840           errnum = G_RESOLVER_ERROR_INTERNAL;
841           format = _("Error resolving '%s'");
842         }
843
844       g_set_error (error, G_RESOLVER_ERROR, errnum, format, rrname);
845       return NULL;
846     }
847
848   targets = NULL;
849   for (rec = results; rec; rec = rec->pNext)
850     {
851       if (rec->wType != DNS_TYPE_SRV)
852         continue;
853
854       target = g_srv_target_new (rec->Data.SRV.pNameTarget,
855                                  rec->Data.SRV.wPort,
856                                  rec->Data.SRV.wPriority,
857                                  rec->Data.SRV.wWeight);
858       targets = g_list_prepend (targets, target);
859     }
860
861   return g_srv_target_list_sort (targets);
862 }
863
864 #endif
865
866 #define __G_RESOLVER_C__
867 #include "gioaliasdef.c"