1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2008 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
27 #include "gresolver.h"
28 #include "gnetworkingprivate.h"
29 #include "gasyncresult.h"
30 #include "ginetaddress.h"
31 #include "ginetsocketaddress.h"
32 #include "gsimpleasyncresult.h"
33 #include "gsrvtarget.h"
36 #include "gunixresolver.h"
40 #include "gwin32resolver.h"
48 * @short_description: Asynchronous and cancellable DNS resolver
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()).
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.
66 static guint signals[LAST_SIGNAL] = { 0 };
68 struct _GResolverPrivate {
70 time_t resolv_conf_timestamp;
79 * The object that handles DNS resolution. Use g_resolver_get_default()
80 * to get the default resolver.
82 G_DEFINE_TYPE (GResolver, g_resolver, G_TYPE_OBJECT)
85 g_resolver_class_init (GResolverClass *resolver_class)
89 g_type_class_add_private (resolver_class, sizeof (GResolverPrivate));
91 /* Make sure _g_networking_init() has been called */
92 type = g_inet_address_get_type ();
93 (type); /* To avoid -Wunused-but-set-variable */
95 /* Initialize _g_resolver_addrinfo_hints */
97 _g_resolver_addrinfo_hints.ai_flags |= AI_ADDRCONFIG;
99 /* These two don't actually matter, they just get copied into the
100 * returned addrinfo structures (and then we ignore them). But if
101 * we leave them unset, we'll get back duplicate answers.
103 _g_resolver_addrinfo_hints.ai_socktype = SOCK_STREAM;
104 _g_resolver_addrinfo_hints.ai_protocol = IPPROTO_TCP;
108 * @resolver: a #GResolver
110 * Emitted when the resolver notices that the system resolver
111 * configuration has changed.
114 g_signal_new (I_("reload"),
117 G_STRUCT_OFFSET (GResolverClass, reload),
119 g_cclosure_marshal_VOID__VOID,
124 g_resolver_init (GResolver *resolver)
130 resolver->priv = G_TYPE_INSTANCE_GET_PRIVATE (resolver, G_TYPE_RESOLVER, GResolverPrivate);
133 if (stat (_PATH_RESCONF, &st) == 0)
134 resolver->priv->resolv_conf_timestamp = st.st_mtime;
138 static GResolver *default_resolver;
141 * g_resolver_get_default:
143 * Gets the default #GResolver. You should unref it when you are done
144 * with it. #GResolver may use its reference count as a hint about how
145 * many threads/processes, etc it should allocate for concurrent DNS
148 * Return value: (transfer full): the default #GResolver.
153 g_resolver_get_default (void)
155 if (!default_resolver)
157 if (g_thread_supported ())
158 default_resolver = g_object_new (G_TYPE_THREADED_RESOLVER, NULL);
161 #if defined(G_OS_UNIX)
162 default_resolver = g_object_new (G_TYPE_UNIX_RESOLVER, NULL);
163 #elif defined(G_OS_WIN32)
164 default_resolver = g_object_new (G_TYPE_WIN32_RESOLVER, NULL);
169 return g_object_ref (default_resolver);
173 * g_resolver_set_default:
174 * @resolver: the new default #GResolver
176 * Sets @resolver to be the application's default resolver (reffing
177 * @resolver, and unreffing the previous default resolver, if any).
178 * Future calls to g_resolver_get_default() will return this resolver.
180 * This can be used if an application wants to perform any sort of DNS
181 * caching or "pinning"; it can implement its own #GResolver that
182 * calls the original default resolver for DNS operations, and
183 * implements its own cache policies on top of that, and then set
184 * itself as the default resolver for all later code to use.
189 g_resolver_set_default (GResolver *resolver)
191 if (default_resolver)
192 g_object_unref (default_resolver);
193 default_resolver = g_object_ref (resolver);
198 g_resolver_maybe_reload (GResolver *resolver)
203 if (stat (_PATH_RESCONF, &st) == 0)
205 if (st.st_mtime != resolver->priv->resolv_conf_timestamp)
207 resolver->priv->resolv_conf_timestamp = st.st_mtime;
209 g_signal_emit (resolver, signals[RELOAD], 0);
215 /* filter out duplicates, cf. https://bugzilla.gnome.org/show_bug.cgi?id=631379 */
217 remove_duplicates (GList *addrs)
223 /* TODO: if this is too slow (it's O(n^2) but n is typically really
224 * small), we can do something more clever but note that we must not
225 * change the order of elements...
227 for (l = addrs; l != NULL; l = l->next)
229 GInetAddress *address = G_INET_ADDRESS (l->data);
230 for (ll = l->next; ll != NULL; ll = lll)
232 GInetAddress *other_address = G_INET_ADDRESS (ll->data);
234 if (g_inet_address_equal (address, other_address))
236 g_object_unref (other_address);
237 /* we never return the first element */
238 g_warn_if_fail (g_list_delete_link (addrs, ll) == addrs);
246 * g_resolver_lookup_by_name:
247 * @resolver: a #GResolver
248 * @hostname: the hostname to look up
249 * @cancellable: (allow-none): a #GCancellable, or %NULL
250 * @error: return location for a #GError, or %NULL
252 * Synchronously resolves @hostname to determine its associated IP
253 * address(es). @hostname may be an ASCII-only or UTF-8 hostname, or
254 * the textual form of an IP address (in which case this just becomes
255 * a wrapper around g_inet_address_new_from_string()).
257 * On success, g_resolver_lookup_by_name() will return a #GList of
258 * #GInetAddress, sorted in order of preference and guaranteed to not
259 * contain duplicates. That is, if using the result to connect to
260 * @hostname, you should attempt to connect to the first address
261 * first, then the second if the first fails, etc. If you are using
262 * the result to listen on a socket, it is appropriate to add each
263 * result using e.g. g_socket_listener_add_address().
265 * If the DNS resolution fails, @error (if non-%NULL) will be set to a
266 * value from #GResolverError.
268 * If @cancellable is non-%NULL, it can be used to cancel the
269 * operation, in which case @error (if non-%NULL) will be set to
270 * %G_IO_ERROR_CANCELLED.
272 * If you are planning to connect to a socket on the resolved IP
273 * address, it may be easier to create a #GNetworkAddress and use its
274 * #GSocketConnectable interface.
276 * Return value: (element-type GInetAddress) (transfer full): a #GList
277 * of #GInetAddress, or %NULL on error. You
278 * must unref each of the addresses and free the list when you are
279 * done with it. (You can use g_resolver_free_addresses() to do this.)
284 g_resolver_lookup_by_name (GResolver *resolver,
285 const gchar *hostname,
286 GCancellable *cancellable,
291 gchar *ascii_hostname = NULL;
293 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
294 g_return_val_if_fail (hostname != NULL, NULL);
296 /* Check if @hostname is just an IP address */
297 addr = g_inet_address_new_from_string (hostname);
299 return g_list_append (NULL, addr);
301 if (g_hostname_is_non_ascii (hostname))
302 hostname = ascii_hostname = g_hostname_to_ascii (hostname);
304 g_resolver_maybe_reload (resolver);
305 addrs = G_RESOLVER_GET_CLASS (resolver)->
306 lookup_by_name (resolver, hostname, cancellable, error);
308 remove_duplicates (addrs);
310 g_free (ascii_hostname);
315 * g_resolver_lookup_by_name_async:
316 * @resolver: a #GResolver
317 * @hostname: the hostname to look up the address of
318 * @cancellable: (allow-none): a #GCancellable, or %NULL
319 * @callback: (scope async): callback to call after resolution completes
320 * @user_data: (closure): data for @callback
322 * Begins asynchronously resolving @hostname to determine its
323 * associated IP address(es), and eventually calls @callback, which
324 * must call g_resolver_lookup_by_name_finish() to get the result.
325 * See g_resolver_lookup_by_name() for more details.
330 g_resolver_lookup_by_name_async (GResolver *resolver,
331 const gchar *hostname,
332 GCancellable *cancellable,
333 GAsyncReadyCallback callback,
337 gchar *ascii_hostname = NULL;
339 g_return_if_fail (G_IS_RESOLVER (resolver));
340 g_return_if_fail (hostname != NULL);
342 /* Check if @hostname is just an IP address */
343 addr = g_inet_address_new_from_string (hostname);
346 GSimpleAsyncResult *simple;
348 simple = g_simple_async_result_new (G_OBJECT (resolver),
350 g_resolver_lookup_by_name_async);
352 g_simple_async_result_set_op_res_gpointer (simple, addr, g_object_unref);
353 g_simple_async_result_complete_in_idle (simple);
354 g_object_unref (simple);
358 if (g_hostname_is_non_ascii (hostname))
359 hostname = ascii_hostname = g_hostname_to_ascii (hostname);
361 g_resolver_maybe_reload (resolver);
362 G_RESOLVER_GET_CLASS (resolver)->
363 lookup_by_name_async (resolver, hostname, cancellable, callback, user_data);
365 g_free (ascii_hostname);
369 * g_resolver_lookup_by_name_finish:
370 * @resolver: a #GResolver
371 * @result: the result passed to your #GAsyncReadyCallback
372 * @error: return location for a #GError, or %NULL
374 * Retrieves the result of a call to
375 * g_resolver_lookup_by_name_async().
377 * If the DNS resolution failed, @error (if non-%NULL) will be set to
378 * a value from #GResolverError. If the operation was cancelled,
379 * @error will be set to %G_IO_ERROR_CANCELLED.
381 * Return value: (element-type GInetAddress) (transfer full): a #GList
382 * of #GInetAddress, or %NULL on error. See g_resolver_lookup_by_name()
388 g_resolver_lookup_by_name_finish (GResolver *resolver,
389 GAsyncResult *result,
394 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
396 if (G_IS_SIMPLE_ASYNC_RESULT (result))
398 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
400 if (g_simple_async_result_propagate_error (simple, error))
403 /* Handle the stringified-IP-addr case */
404 if (g_simple_async_result_get_source_tag (simple) == g_resolver_lookup_by_name_async)
408 addr = g_simple_async_result_get_op_res_gpointer (simple);
409 return g_list_append (NULL, g_object_ref (addr));
413 addrs = G_RESOLVER_GET_CLASS (resolver)->
414 lookup_by_name_finish (resolver, result, error);
416 remove_duplicates (addrs);
422 * g_resolver_free_addresses: (skip)
423 * @addresses: a #GList of #GInetAddress
425 * Frees @addresses (which should be the return value from
426 * g_resolver_lookup_by_name() or g_resolver_lookup_by_name_finish()).
427 * (This is a convenience method; you can also simply free the results
433 g_resolver_free_addresses (GList *addresses)
437 for (a = addresses; a; a = a->next)
438 g_object_unref (a->data);
439 g_list_free (addresses);
443 * g_resolver_lookup_by_address:
444 * @resolver: a #GResolver
445 * @address: the address to reverse-resolve
446 * @cancellable: (allow-none): a #GCancellable, or %NULL
447 * @error: return location for a #GError, or %NULL
449 * Synchronously reverse-resolves @address to determine its
450 * associated hostname.
452 * If the DNS resolution fails, @error (if non-%NULL) will be set to
453 * a value from #GResolverError.
455 * If @cancellable is non-%NULL, it can be used to cancel the
456 * operation, in which case @error (if non-%NULL) will be set to
457 * %G_IO_ERROR_CANCELLED.
459 * Return value: a hostname (either ASCII-only, or in ASCII-encoded
460 * form), or %NULL on error.
465 g_resolver_lookup_by_address (GResolver *resolver,
466 GInetAddress *address,
467 GCancellable *cancellable,
470 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
471 g_return_val_if_fail (G_IS_INET_ADDRESS (address), NULL);
473 g_resolver_maybe_reload (resolver);
474 return G_RESOLVER_GET_CLASS (resolver)->
475 lookup_by_address (resolver, address, cancellable, error);
479 * g_resolver_lookup_by_address_async:
480 * @resolver: a #GResolver
481 * @address: the address to reverse-resolve
482 * @cancellable: (allow-none): a #GCancellable, or %NULL
483 * @callback: (scope async): callback to call after resolution completes
484 * @user_data: (closure): data for @callback
486 * Begins asynchronously reverse-resolving @address to determine its
487 * associated hostname, and eventually calls @callback, which must
488 * call g_resolver_lookup_by_address_finish() to get the final result.
493 g_resolver_lookup_by_address_async (GResolver *resolver,
494 GInetAddress *address,
495 GCancellable *cancellable,
496 GAsyncReadyCallback callback,
499 g_return_if_fail (G_IS_RESOLVER (resolver));
500 g_return_if_fail (G_IS_INET_ADDRESS (address));
502 g_resolver_maybe_reload (resolver);
503 G_RESOLVER_GET_CLASS (resolver)->
504 lookup_by_address_async (resolver, address, cancellable, callback, user_data);
508 * g_resolver_lookup_by_address_finish:
509 * @resolver: a #GResolver
510 * @result: the result passed to your #GAsyncReadyCallback
511 * @error: return location for a #GError, or %NULL
513 * Retrieves the result of a previous call to
514 * g_resolver_lookup_by_address_async().
516 * If the DNS resolution failed, @error (if non-%NULL) will be set to
517 * a value from #GResolverError. If the operation was cancelled,
518 * @error will be set to %G_IO_ERROR_CANCELLED.
520 * Return value: a hostname (either ASCII-only, or in ASCII-encoded
521 * form), or %NULL on error.
526 g_resolver_lookup_by_address_finish (GResolver *resolver,
527 GAsyncResult *result,
530 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
532 if (G_IS_SIMPLE_ASYNC_RESULT (result))
534 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
536 if (g_simple_async_result_propagate_error (simple, error))
540 return G_RESOLVER_GET_CLASS (resolver)->
541 lookup_by_address_finish (resolver, result, error);
545 g_resolver_get_service_rrname (const char *service,
546 const char *protocol,
549 gchar *rrname, *ascii_domain = NULL;
551 if (g_hostname_is_non_ascii (domain))
552 domain = ascii_domain = g_hostname_to_ascii (domain);
554 rrname = g_strdup_printf ("_%s._%s.%s", service, protocol, domain);
556 g_free (ascii_domain);
561 * g_resolver_lookup_service:
562 * @resolver: a #GResolver
563 * @service: the service type to look up (eg, "ldap")
564 * @protocol: the networking protocol to use for @service (eg, "tcp")
565 * @domain: the DNS domain to look up the service in
566 * @cancellable: (allow-none): a #GCancellable, or %NULL
567 * @error: return location for a #GError, or %NULL
569 * Synchronously performs a DNS SRV lookup for the given @service and
570 * @protocol in the given @domain and returns an array of #GSrvTarget.
571 * @domain may be an ASCII-only or UTF-8 hostname. Note also that the
572 * @service and @protocol arguments <emphasis>do not</emphasis>
573 * include the leading underscore that appears in the actual DNS
576 * On success, g_resolver_lookup_service() will return a #GList of
577 * #GSrvTarget, sorted in order of preference. (That is, you should
578 * attempt to connect to the first target first, then the second if
579 * the first fails, etc.)
581 * If the DNS resolution fails, @error (if non-%NULL) will be set to
582 * a value from #GResolverError.
584 * If @cancellable is non-%NULL, it can be used to cancel the
585 * operation, in which case @error (if non-%NULL) will be set to
586 * %G_IO_ERROR_CANCELLED.
588 * If you are planning to connect to the service, it is usually easier
589 * to create a #GNetworkService and use its #GSocketConnectable
592 * Return value: (element-type GSrvTarget) (transfer full): a #GList of #GSrvTarget,
593 * or %NULL on error. You must free each of the targets and the list when you are
594 * done with it. (You can use g_resolver_free_targets() to do this.)
599 g_resolver_lookup_service (GResolver *resolver,
600 const gchar *service,
601 const gchar *protocol,
603 GCancellable *cancellable,
609 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
610 g_return_val_if_fail (service != NULL, NULL);
611 g_return_val_if_fail (protocol != NULL, NULL);
612 g_return_val_if_fail (domain != NULL, NULL);
614 rrname = g_resolver_get_service_rrname (service, protocol, domain);
616 g_resolver_maybe_reload (resolver);
617 targets = G_RESOLVER_GET_CLASS (resolver)->
618 lookup_service (resolver, rrname, cancellable, error);
625 * g_resolver_lookup_service_async:
626 * @resolver: a #GResolver
627 * @service: the service type to look up (eg, "ldap")
628 * @protocol: the networking protocol to use for @service (eg, "tcp")
629 * @domain: the DNS domain to look up the service in
630 * @cancellable: (allow-none): a #GCancellable, or %NULL
631 * @callback: (scope async): callback to call after resolution completes
632 * @user_data: (closure): data for @callback
634 * Begins asynchronously performing a DNS SRV lookup for the given
635 * @service and @protocol in the given @domain, and eventually calls
636 * @callback, which must call g_resolver_lookup_service_finish() to
637 * get the final result. See g_resolver_lookup_service() for more
643 g_resolver_lookup_service_async (GResolver *resolver,
644 const gchar *service,
645 const gchar *protocol,
647 GCancellable *cancellable,
648 GAsyncReadyCallback callback,
653 g_return_if_fail (G_IS_RESOLVER (resolver));
654 g_return_if_fail (service != NULL);
655 g_return_if_fail (protocol != NULL);
656 g_return_if_fail (domain != NULL);
658 rrname = g_resolver_get_service_rrname (service, protocol, domain);
660 g_resolver_maybe_reload (resolver);
661 G_RESOLVER_GET_CLASS (resolver)->
662 lookup_service_async (resolver, rrname, cancellable, callback, user_data);
668 * g_resolver_lookup_service_finish:
669 * @resolver: a #GResolver
670 * @result: the result passed to your #GAsyncReadyCallback
671 * @error: return location for a #GError, or %NULL
673 * Retrieves the result of a previous call to
674 * g_resolver_lookup_service_async().
676 * If the DNS resolution failed, @error (if non-%NULL) will be set to
677 * a value from #GResolverError. If the operation was cancelled,
678 * @error will be set to %G_IO_ERROR_CANCELLED.
680 * Return value: (element-type GSrvTarget) (transfer full): a #GList of #GSrvTarget,
681 * or %NULL on error. See g_resolver_lookup_service() for more details.
686 g_resolver_lookup_service_finish (GResolver *resolver,
687 GAsyncResult *result,
690 g_return_val_if_fail (G_IS_RESOLVER (resolver), NULL);
692 if (G_IS_SIMPLE_ASYNC_RESULT (result))
694 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
696 if (g_simple_async_result_propagate_error (simple, error))
700 return G_RESOLVER_GET_CLASS (resolver)->
701 lookup_service_finish (resolver, result, error);
705 * g_resolver_free_targets: (skip)
706 * @targets: a #GList of #GSrvTarget
708 * Frees @targets (which should be the return value from
709 * g_resolver_lookup_service() or g_resolver_lookup_service_finish()).
710 * (This is a convenience method; you can also simply free the
716 g_resolver_free_targets (GList *targets)
720 for (t = targets; t; t = t->next)
721 g_srv_target_free (t->data);
722 g_list_free (targets);
726 * g_resolver_error_quark:
728 * Gets the #GResolver Error Quark.
730 * Return value: a #GQuark.
735 g_resolver_error_quark (void)
737 return g_quark_from_static_string ("g-resolver-error-quark");
741 static GResolverError
742 g_resolver_error_from_addrinfo_error (gint err)
747 #if defined(EAI_NODATA) && (EAI_NODATA != EAI_NONAME)
751 return G_RESOLVER_ERROR_NOT_FOUND;
754 return G_RESOLVER_ERROR_TEMPORARY_FAILURE;
757 return G_RESOLVER_ERROR_INTERNAL;
761 struct addrinfo _g_resolver_addrinfo_hints;
763 /* Private method to process a getaddrinfo() response. */
765 _g_resolver_addresses_from_addrinfo (const char *hostname,
766 struct addrinfo *res,
771 GSocketAddress *sockaddr;
777 g_set_error (error, G_RESOLVER_ERROR,
778 g_resolver_error_from_addrinfo_error (gai_retval),
779 _("Error resolving '%s': %s"),
780 hostname, gai_strerror (gai_retval));
784 g_return_val_if_fail (res != NULL, NULL);
787 for (ai = res; ai; ai = ai->ai_next)
789 sockaddr = g_socket_address_new_from_native (ai->ai_addr, ai->ai_addrlen);
790 if (!sockaddr || !G_IS_INET_SOCKET_ADDRESS (sockaddr))
793 addr = g_object_ref (g_inet_socket_address_get_address ((GInetSocketAddress *)sockaddr));
794 addrs = g_list_prepend (addrs, addr);
795 g_object_unref (sockaddr);
798 return g_list_reverse (addrs);
801 /* Private method to set up a getnameinfo() request */
803 _g_resolver_address_to_sockaddr (GInetAddress *address,
804 struct sockaddr_storage *sa,
807 GSocketAddress *sockaddr;
809 sockaddr = g_inet_socket_address_new (address, 0);
810 g_socket_address_to_native (sockaddr, (struct sockaddr *)sa, sizeof (*sa), NULL);
811 *len = g_socket_address_get_native_size (sockaddr);
812 g_object_unref (sockaddr);
815 /* Private method to process a getnameinfo() response. */
817 _g_resolver_name_from_nameinfo (GInetAddress *address,
826 phys = g_inet_address_to_string (address);
827 g_set_error (error, G_RESOLVER_ERROR,
828 g_resolver_error_from_addrinfo_error (gni_retval),
829 _("Error reverse-resolving '%s': %s"),
830 phys ? phys : "(unknown)", gai_strerror (gni_retval));
835 return g_strdup (name);
838 #if defined(G_OS_UNIX)
839 /* Private method to process a res_query response into GSrvTargets */
841 _g_resolver_targets_from_res_query (const gchar *rrname,
850 guint16 type, qclass, rdlength, priority, weight, port;
858 GResolverError errnum;
861 if (len == 0 || herr == HOST_NOT_FOUND || herr == NO_DATA)
863 errnum = G_RESOLVER_ERROR_NOT_FOUND;
864 format = _("No service record for '%s'");
866 else if (herr == TRY_AGAIN)
868 errnum = G_RESOLVER_ERROR_TEMPORARY_FAILURE;
869 format = _("Temporarily unable to resolve '%s'");
873 errnum = G_RESOLVER_ERROR_INTERNAL;
874 format = _("Error resolving '%s'");
877 g_set_error (error, G_RESOLVER_ERROR, errnum, format, rrname);
883 header = (HEADER *)answer;
884 p = answer + sizeof (HEADER);
888 count = ntohs (header->qdcount);
889 while (count-- && p < end)
891 p += dn_expand (answer, end, p, namebuf, sizeof (namebuf));
896 count = ntohs (header->ancount);
897 while (count-- && p < end)
899 p += dn_expand (answer, end, p, namebuf, sizeof (namebuf));
901 GETSHORT (qclass, p);
903 ttl = ttl; /* To avoid -Wunused-but-set-variable */
904 GETSHORT (rdlength, p);
906 if (type != T_SRV || qclass != C_IN)
912 GETSHORT (priority, p);
913 GETSHORT (weight, p);
915 p += dn_expand (answer, end, p, namebuf, sizeof (namebuf));
917 target = g_srv_target_new (namebuf, port, priority, weight);
918 targets = g_list_prepend (targets, target);
921 return g_srv_target_list_sort (targets);
923 #elif defined(G_OS_WIN32)
924 /* Private method to process a DnsQuery response into GSrvTargets */
926 _g_resolver_targets_from_DnsQuery (const gchar *rrname,
935 if (status != ERROR_SUCCESS)
937 GResolverError errnum;
940 if (status == DNS_ERROR_RCODE_NAME_ERROR)
942 errnum = G_RESOLVER_ERROR_NOT_FOUND;
943 format = _("No service record for '%s'");
945 else if (status == DNS_ERROR_RCODE_SERVER_FAILURE)
947 errnum = G_RESOLVER_ERROR_TEMPORARY_FAILURE;
948 format = _("Temporarily unable to resolve '%s'");
952 errnum = G_RESOLVER_ERROR_INTERNAL;
953 format = _("Error resolving '%s'");
956 g_set_error (error, G_RESOLVER_ERROR, errnum, format, rrname);
961 for (rec = results; rec; rec = rec->pNext)
963 if (rec->wType != DNS_TYPE_SRV)
966 target = g_srv_target_new (rec->Data.SRV.pNameTarget,
968 rec->Data.SRV.wPriority,
969 rec->Data.SRV.wWeight);
970 targets = g_list_prepend (targets, target);
973 return g_srv_target_list_sort (targets);