3 * Resolver library with GLib integration
5 * Copyright (C) 2009-2012 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <sys/types.h>
33 #include <sys/socket.h>
35 #include <arpa/inet.h>
36 #include <arpa/nameser.h>
50 struct sockaddr_in sin;
51 struct sockaddr_in6 sin6;
55 struct sockaddr_in sin;
56 struct sockaddr_in6 sin6;
62 struct resolv_lookup {
67 struct sort_result *results;
69 struct resolv_query *ipv4_query;
70 struct resolv_query *ipv6_query;
75 GResolvResultFunc result_func;
86 struct resolv_lookup *lookup;
89 struct resolv_nameserver {
96 GIOChannel *udp_channel;
105 guint next_lookup_id;
106 GQueue *lookup_queue;
110 GList *nameserver_list;
112 struct __res_state res;
114 GResolvDebugFunc debug_func;
118 #define debug(resolv, format, arg...) \
119 _debug(resolv, __FILE__, __func__, format, ## arg)
121 static void _debug(GResolv *resolv, const char *file, const char *caller,
122 const char *format, ...)
128 if (resolv->debug_func == NULL)
131 va_start(ap, format);
133 if ((len = snprintf(str, sizeof(str), "%s:%s() resolv %p ",
134 file, caller, resolv)) > 0) {
135 if (vsnprintf(str + len, sizeof(str) - len, format, ap) > 0)
136 resolv->debug_func(str, resolv->debug_data);
142 static void destroy_query(struct resolv_query *query)
144 debug(query->resolv, "query %p timeout %d", query, query->timeout);
146 if (query->timeout > 0)
147 g_source_remove(query->timeout);
152 static void destroy_lookup(struct resolv_lookup *lookup)
154 debug(lookup->resolv, "lookup %p id %d ipv4 %p ipv6 %p",
155 lookup, lookup->id, lookup->ipv4_query, lookup->ipv6_query);
157 if (lookup->ipv4_query != NULL) {
158 g_queue_remove(lookup->resolv->query_queue,
160 destroy_query(lookup->ipv4_query);
163 if (lookup->ipv6_query != NULL) {
164 g_queue_remove(lookup->resolv->query_queue,
166 destroy_query(lookup->ipv6_query);
169 g_free(lookup->results);
173 static void find_srcaddr(struct sort_result *res)
175 socklen_t sl = sizeof(res->src);
178 fd = socket(res->dst.sa.sa_family, SOCK_DGRAM | SOCK_CLOEXEC,
183 if (connect(fd, &res->dst.sa, sizeof(res->dst)) < 0) {
188 if (getsockname(fd, &res->src.sa, &sl) < 0) {
193 res->reachable = TRUE;
199 unsigned char addr[NS_IN6ADDRSZ];
204 static const struct gai_table gai_labels[] = {
206 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
207 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
211 .addr = { 0x20, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
212 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
216 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
217 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
221 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
222 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 },
226 /* Variations from RFC 3484, matching glibc behaviour */
227 .addr = { 0xfe, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
228 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
232 .addr = { 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
233 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
237 .addr = { 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
238 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
243 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
244 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
250 static const struct gai_table gai_precedences[] = {
252 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
253 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
257 .addr = { 0x20, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
258 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
262 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
263 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
267 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
268 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 },
272 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
273 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
279 static unsigned char v4mapped[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
280 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 };
282 static gboolean mask_compare(const unsigned char *one,
283 const unsigned char *two, int mask)
286 if (memcmp(one, two, mask / 8))
293 if (mask && ((*one ^ *two) >> (8 - mask)))
299 static int match_gai_table(struct sockaddr *sa, const struct gai_table *tbl)
301 struct sockaddr_in *sin = (void *)sa;
302 struct sockaddr_in6 *sin6 = (void *)sa;
305 if (sa->sa_family == AF_INET) {
307 memcpy(v4mapped+12, &sin->sin_addr, NS_INADDRSZ);
309 addr = &sin6->sin6_addr;
312 if (mask_compare(addr, tbl->addr, tbl->mask))
318 #define DQUAD(_a,_b,_c,_d) ( ((_a)<<24) | ((_b)<<16) | ((_c)<<8) | (_d) )
319 #define V4MATCH(addr, a,b,c,d, m) ( ((addr) ^ DQUAD(a,b,c,d)) >> (32 - (m)) )
321 #define RFC3484_SCOPE_LINK 2
322 #define RFC3484_SCOPE_SITE 5
323 #define RFC3484_SCOPE_GLOBAL 14
325 static int addr_scope(struct sockaddr *sa)
327 if (sa->sa_family == AF_INET) {
328 struct sockaddr_in *sin = (void *)sa;
329 guint32 addr = ntohl(sin->sin_addr.s_addr);
331 if (V4MATCH(addr, 169,254,0,0, 16) ||
332 V4MATCH(addr, 127,0,0,0, 8))
333 return RFC3484_SCOPE_LINK;
336 if (V4MATCH(addr, 10,0,0,0, 8) ||
337 V4MATCH(addr, 172,16,0,0, 12) ||
338 V4MATCH(addr, 192,168,0,0, 16))
339 return RFC3484_SCOPE_SITE;
342 return RFC3484_SCOPE_GLOBAL;
344 struct sockaddr_in6 *sin6 = (void *)sa;
346 /* Multicast addresses have a 4-bit scope field */
347 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
348 return sin6->sin6_addr.s6_addr[1] & 0xf;
350 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
351 IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
352 return RFC3484_SCOPE_LINK;
354 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
355 return RFC3484_SCOPE_SITE;
357 return RFC3484_SCOPE_GLOBAL;
361 static int rfc3484_compare(const void *__one, const void *__two)
363 const struct sort_result *one = __one;
364 const struct sort_result *two = __two;
366 /* Rule 1: Avoid unusable destinations */
367 if (one->reachable && !two->reachable)
369 else if (two->reachable && !one->reachable)
372 /* Rule 2: Prefer matching scope */
373 if (one->dst_scope == one->src_scope &&
374 two->dst_scope != two->src_scope)
376 else if (two->dst_scope == two->src_scope &&
377 one->dst_scope != one->src_scope)
380 /* Rule 3: Avoid deprecated addresses */
382 /* Rule 4: Prefer home addresses */
384 /* Rule 5: Prefer matching label */
385 if (one->dst_label == one->src_label &&
386 two->dst_label != two->src_label)
388 else if (two->dst_label == two->src_label &&
389 one->dst_label != one->src_label)
392 /* Rule 6: Prefer higher precedence */
393 if (one->precedence > two->precedence)
395 else if (two->precedence > one->precedence)
398 /* Rule 7: Prefer native transport */
400 /* Rule 8: Prefer smaller scope */
401 if (one->dst_scope != two->dst_scope)
402 return one->dst_scope - two->dst_scope;
404 /* Rule 9: Use longest matching prefix */
405 if (one->dst.sa.sa_family == AF_INET) {
407 * Rule 9 is meaningless and counterproductive for Legacy IP
408 * unless perhaps we can tell that it's actually on the local
409 * subnet. But we don't (yet) have local interface config
410 * information, so do nothing here for Legacy IP for now.
415 for (i = 0; i < 4; i++) {
416 guint32 cmp_one, cmp_two;
418 cmp_one = one->src.sin6.sin6_addr.s6_addr32[i] ^
419 one->dst.sin6.sin6_addr.s6_addr32[i];
420 cmp_two = two->src.sin6.sin6_addr.s6_addr32[i] ^
421 two->dst.sin6.sin6_addr.s6_addr32[i];
423 if (!cmp_two && !cmp_one)
426 if (cmp_one && !cmp_two)
428 if (cmp_two && !cmp_one)
431 /* g_bit_storage() is effectively fls() */
432 cmp_one = g_bit_storage(ntohl(cmp_one));
433 cmp_two = g_bit_storage(ntohl(cmp_two));
435 if (cmp_one == cmp_two)
438 return cmp_one - cmp_two;
443 /* Rule 10: Otherwise, leave the order unchanged */
450 static void rfc3484_sort_results(struct resolv_lookup *lookup)
454 for (i = 0; i < lookup->nr_results; i++) {
455 struct sort_result *res = &lookup->results[i];
457 res->precedence = match_gai_table(&res->dst.sa,
459 res->dst_label = match_gai_table(&res->dst.sa, gai_labels);
460 res->src_label = match_gai_table(&res->src.sa, gai_labels);
461 res->dst_scope = addr_scope(&res->dst.sa);
462 res->src_scope = addr_scope(&res->src.sa);
465 qsort(lookup->results, lookup->nr_results,
466 sizeof(struct sort_result), rfc3484_compare);
469 static void sort_and_return_results(struct resolv_lookup *lookup)
471 char buf[INET6_ADDRSTRLEN + 1];
472 GResolvResultStatus status;
473 char **results = g_try_new0(char *, lookup->nr_results + 1);
479 memset(buf, 0, INET6_ADDRSTRLEN + 1);
481 rfc3484_sort_results(lookup);
483 for (i = 0; i < lookup->nr_results; i++) {
484 if (lookup->results[i].dst.sa.sa_family == AF_INET) {
485 if (inet_ntop(AF_INET,
486 &lookup->results[i].dst.sin.sin_addr,
487 buf, sizeof(buf) - 1) == NULL)
489 } else if (lookup->results[i].dst.sa.sa_family == AF_INET6) {
490 if (inet_ntop(AF_INET6,
491 &lookup->results[i].dst.sin6.sin6_addr,
492 buf, sizeof(buf) - 1) == NULL)
497 results[n++] = strdup(buf);
502 if (lookup->resolv->result_family == AF_INET)
503 status = lookup->ipv4_status;
504 else if (lookup->resolv->result_family == AF_INET6)
505 status = lookup->ipv6_status;
507 if (lookup->ipv6_status == G_RESOLV_RESULT_STATUS_SUCCESS)
508 status = lookup->ipv6_status;
510 status = lookup->ipv4_status;
513 debug(lookup->resolv, "lookup %p received %d results", lookup, n);
515 g_queue_remove(lookup->resolv->lookup_queue, lookup);
517 lookup->result_func(status, results, lookup->result_data);
520 destroy_lookup(lookup);
523 static gboolean query_timeout(gpointer user_data)
525 struct resolv_query *query = user_data;
526 struct resolv_lookup *lookup = query->lookup;
527 GResolv *resolv = query->resolv;
531 if (query == lookup->ipv4_query) {
532 lookup->ipv4_status = G_RESOLV_RESULT_STATUS_NO_RESPONSE;
533 lookup->ipv4_query = NULL;
534 } else if (query == lookup->ipv6_query) {
535 lookup->ipv6_status = G_RESOLV_RESULT_STATUS_NO_RESPONSE;
536 lookup->ipv6_query = NULL;
539 g_queue_remove(resolv->query_queue, query);
541 if (lookup->ipv4_query == NULL && lookup->ipv6_query == NULL)
542 sort_and_return_results(lookup);
544 destroy_query(query);
549 static void free_nameserver(struct resolv_nameserver *nameserver)
551 if (nameserver == NULL)
554 if (nameserver->udp_watch > 0)
555 g_source_remove(nameserver->udp_watch);
557 if (nameserver->udp_channel != NULL) {
558 g_io_channel_shutdown(nameserver->udp_channel, TRUE, NULL);
559 g_io_channel_unref(nameserver->udp_channel);
562 g_free(nameserver->address);
566 static void flush_nameservers(GResolv *resolv)
570 for (list = g_list_first(resolv->nameserver_list);
571 list; list = g_list_next(list))
572 free_nameserver(list->data);
574 g_list_free(resolv->nameserver_list);
575 resolv->nameserver_list = NULL;
578 static int send_query(GResolv *resolv, const unsigned char *buf, int len)
582 if (resolv->nameserver_list == NULL)
585 for (list = g_list_first(resolv->nameserver_list);
586 list; list = g_list_next(list)) {
587 struct resolv_nameserver *nameserver = list->data;
590 if (nameserver->udp_channel == NULL)
593 sk = g_io_channel_unix_get_fd(nameserver->udp_channel);
595 sent = send(sk, buf, len, 0);
603 static gint compare_lookup_id(gconstpointer a, gconstpointer b)
605 const struct resolv_lookup *lookup = a;
606 guint id = GPOINTER_TO_UINT(b);
617 static gint compare_query_msgid(gconstpointer a, gconstpointer b)
619 const struct resolv_query *query = a;
620 uint16_t msgid = GPOINTER_TO_UINT(b);
622 if (query->msgid < msgid)
625 if (query->msgid > msgid)
631 static void add_result(struct resolv_lookup *lookup, int family,
634 int n = lookup->nr_results++;
635 lookup->results = g_try_realloc(lookup->results,
636 sizeof(struct sort_result) * (n + 1));
637 if (lookup->results == NULL)
640 memset(&lookup->results[n], 0, sizeof(struct sort_result));
642 lookup->results[n].dst.sa.sa_family = family;
643 if (family == AF_INET)
644 memcpy(&lookup->results[n].dst.sin.sin_addr,
647 memcpy(&lookup->results[n].dst.sin6.sin6_addr,
651 static void parse_response(struct resolv_nameserver *nameserver,
652 const unsigned char *buf, int len)
654 GResolv *resolv = nameserver->resolv;
655 GResolvResultStatus status;
656 struct resolv_query *query;
657 struct resolv_lookup *lookup;
663 debug(resolv, "response from %s", nameserver->address);
665 ns_initparse(buf, len, &msg);
667 rcode = ns_msg_getflag(msg, ns_f_rcode);
668 count = ns_msg_count(msg, ns_s_an);
670 debug(resolv, "msg id: 0x%04x rcode: %d count: %d",
671 ns_msg_id(msg), rcode, count);
675 status = G_RESOLV_RESULT_STATUS_SUCCESS;
678 status = G_RESOLV_RESULT_STATUS_FORMAT_ERROR;
681 status = G_RESOLV_RESULT_STATUS_SERVER_FAILURE;
684 status = G_RESOLV_RESULT_STATUS_NAME_ERROR;
687 status = G_RESOLV_RESULT_STATUS_NOT_IMPLEMENTED;
690 status = G_RESOLV_RESULT_STATUS_REFUSED;
693 status = G_RESOLV_RESULT_STATUS_ERROR;
697 list = g_queue_find_custom(resolv->query_queue,
698 GUINT_TO_POINTER(ns_msg_id(msg)), compare_query_msgid);
703 lookup = query->lookup;
705 if (query == lookup->ipv6_query) {
706 lookup->ipv6_status = status;
707 lookup->ipv6_query = NULL;
708 } else if (query == lookup->ipv4_query) {
709 lookup->ipv4_status = status;
710 lookup->ipv4_query = NULL;
713 for (i = 0; i < count; i++) {
714 ns_parserr(&msg, ns_s_an, i, &rr);
716 if (ns_rr_class(rr) != ns_c_in)
719 g_assert(offsetof(struct sockaddr_in, sin_addr) ==
720 offsetof(struct sockaddr_in6, sin6_flowinfo));
722 if (ns_rr_type(rr) == ns_t_a &&
723 ns_rr_rdlen(rr) == NS_INADDRSZ) {
724 add_result(lookup, AF_INET, ns_rr_rdata(rr));
725 } else if (ns_rr_type(rr) == ns_t_aaaa &&
726 ns_rr_rdlen(rr) == NS_IN6ADDRSZ) {
727 add_result(lookup, AF_INET6, ns_rr_rdata(rr));
731 g_queue_remove(resolv->query_queue, query);
733 if (lookup->ipv4_query == NULL && lookup->ipv6_query == NULL)
734 sort_and_return_results(lookup);
736 destroy_query(query);
739 static gboolean received_udp_data(GIOChannel *channel, GIOCondition cond,
742 struct resolv_nameserver *nameserver = user_data;
743 unsigned char buf[4096];
746 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
747 nameserver->udp_watch = 0;
751 sk = g_io_channel_unix_get_fd(nameserver->udp_channel);
753 len = recv(sk, buf, sizeof(buf), 0);
757 parse_response(nameserver, buf, len);
762 static int connect_udp_channel(struct resolv_nameserver *nameserver)
764 struct addrinfo hints, *rp;
768 memset(&hints, 0, sizeof(hints));
769 hints.ai_family = AF_UNSPEC;
770 hints.ai_socktype = SOCK_DGRAM;
771 hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV | AI_NUMERICHOST;
773 sprintf(portnr, "%d", nameserver->port);
774 err = getaddrinfo(nameserver->address, portnr, &hints, &rp);
779 * Do not blindly copy this code elsewhere; it doesn't loop over the
780 * results using ->ai_next as it should. That's OK in *this* case
781 * because it was a numeric lookup; we *know* there's only one.
786 sk = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
793 * If nameserver points to localhost ip, their is no need to
794 * bind the socket on any interface.
796 if (nameserver->resolv->index > 0 &&
797 strncmp(nameserver->address, "127.0.0.1", 9) != 0) {
798 char interface[IF_NAMESIZE];
800 memset(interface, 0, IF_NAMESIZE);
801 if (if_indextoname(nameserver->resolv->index,
802 interface) != NULL) {
803 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
804 interface, IF_NAMESIZE) < 0) {
812 if (connect(sk, rp->ai_addr, rp->ai_addrlen) < 0) {
820 nameserver->udp_channel = g_io_channel_unix_new(sk);
821 if (nameserver->udp_channel == NULL) {
826 g_io_channel_set_close_on_unref(nameserver->udp_channel, TRUE);
828 nameserver->udp_watch = g_io_add_watch(nameserver->udp_channel,
829 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
830 received_udp_data, nameserver);
835 GResolv *g_resolv_new(int index)
842 resolv = g_try_new0(GResolv, 1);
846 resolv->ref_count = 1;
848 resolv->result_family = AF_UNSPEC;
850 resolv->next_lookup_id = 1;
852 resolv->query_queue = g_queue_new();
853 if (resolv->query_queue == NULL) {
858 resolv->lookup_queue = g_queue_new();
859 if (resolv->lookup_queue == NULL) {
860 g_queue_free(resolv->query_queue);
865 resolv->index = index;
866 resolv->nameserver_list = NULL;
868 res_ninit(&resolv->res);
873 GResolv *g_resolv_ref(GResolv *resolv)
878 __sync_fetch_and_add(&resolv->ref_count, 1);
883 void g_resolv_unref(GResolv *resolv)
885 struct resolv_query *query;
886 struct resolv_lookup *lookup;
891 if (__sync_fetch_and_sub(&resolv->ref_count, 1) != 1)
894 while ((lookup = g_queue_pop_head(resolv->lookup_queue))) {
895 debug(resolv, "lookup %p id %d", lookup, lookup->id);
896 destroy_lookup(lookup);
899 while ((query = g_queue_pop_head(resolv->query_queue))) {
900 debug(resolv, "query %p", query);
901 destroy_query(query);
904 g_queue_free(resolv->query_queue);
905 g_queue_free(resolv->lookup_queue);
907 flush_nameservers(resolv);
909 res_nclose(&resolv->res);
914 void g_resolv_set_debug(GResolv *resolv, GResolvDebugFunc func,
920 resolv->debug_func = func;
921 resolv->debug_data = user_data;
924 gboolean g_resolv_add_nameserver(GResolv *resolv, const char *address,
925 uint16_t port, unsigned long flags)
927 struct resolv_nameserver *nameserver;
932 nameserver = g_try_new0(struct resolv_nameserver, 1);
933 if (nameserver == NULL)
936 nameserver->address = g_strdup(address);
937 nameserver->port = port;
938 nameserver->flags = flags;
939 nameserver->resolv = resolv;
941 if (connect_udp_channel(nameserver) < 0) {
942 free_nameserver(nameserver);
946 resolv->nameserver_list = g_list_append(resolv->nameserver_list,
949 debug(resolv, "setting nameserver %s", address);
954 void g_resolv_flush_nameservers(GResolv *resolv)
959 flush_nameservers(resolv);
962 static gint add_query(struct resolv_lookup *lookup, const char *hostname, int type)
964 struct resolv_query *query = g_try_new0(struct resolv_query, 1);
965 unsigned char buf[4096];
971 len = res_mkquery(ns_o_query, hostname, ns_c_in, type,
972 NULL, 0, NULL, buf, sizeof(buf));
974 query->msgid = buf[0] << 8 | buf[1];
976 debug(lookup->resolv, "sending %d bytes", len);
978 if (send_query(lookup->resolv, buf, len) < 0) {
983 query->resolv = lookup->resolv;
984 query->lookup = lookup;
986 g_queue_push_tail(lookup->resolv->query_queue, query);
988 debug(lookup->resolv, "lookup %p id %d query %p", lookup, lookup->id,
991 query->timeout = g_timeout_add_seconds(5, query_timeout, query);
993 if (type == ns_t_aaaa)
994 lookup->ipv6_query = query;
996 lookup->ipv4_query = query;
1001 guint g_resolv_lookup_hostname(GResolv *resolv, const char *hostname,
1002 GResolvResultFunc func, gpointer user_data)
1004 struct resolv_lookup *lookup;
1006 debug(resolv, "hostname %s", hostname);
1011 if (resolv->nameserver_list == NULL) {
1014 for (i = 0; i < resolv->res.nscount; i++) {
1016 int family = resolv->res.nsaddr_list[i].sin_family;
1017 void *sa_addr = &resolv->res.nsaddr_list[i].sin_addr;
1019 if (family != AF_INET &&
1020 resolv->res._u._ext.nsaddrs[i]) {
1022 sa_addr = &resolv->res._u._ext.nsaddrs[i]->sin6_addr;
1025 if (family != AF_INET && family != AF_INET6)
1028 if (inet_ntop(family, sa_addr, buf, sizeof(buf)))
1029 g_resolv_add_nameserver(resolv, buf, 53, 0);
1032 if (resolv->nameserver_list == NULL)
1033 g_resolv_add_nameserver(resolv, "127.0.0.1", 53, 0);
1036 lookup = g_try_new0(struct resolv_lookup, 1);
1040 lookup->resolv = resolv;
1041 lookup->result_func = func;
1042 lookup->result_data = user_data;
1043 lookup->id = resolv->next_lookup_id++;
1045 if (resolv->result_family != AF_INET6) {
1046 if (add_query(lookup, hostname, ns_t_a)) {
1052 if (resolv->result_family != AF_INET) {
1053 if (add_query(lookup, hostname, ns_t_aaaa)) {
1054 if (resolv->result_family != AF_INET6) {
1055 g_queue_remove(resolv->query_queue,
1056 lookup->ipv4_query);
1057 destroy_query(lookup->ipv4_query);
1065 g_queue_push_tail(resolv->lookup_queue, lookup);
1067 debug(resolv, "lookup %p id %d", lookup, lookup->id);
1072 gboolean g_resolv_cancel_lookup(GResolv *resolv, guint id)
1074 struct resolv_lookup *lookup;
1077 debug(resolv, "lookup id %d", id);
1079 list = g_queue_find_custom(resolv->lookup_queue,
1080 GUINT_TO_POINTER(id), compare_lookup_id);
1085 lookup = list->data;
1087 debug(resolv, "lookup %p", lookup);
1089 g_queue_remove(resolv->lookup_queue, lookup);
1090 destroy_lookup(lookup);
1095 gboolean g_resolv_set_address_family(GResolv *resolv, int family)
1100 if (family != AF_UNSPEC && family != AF_INET && family != AF_INET6)
1103 resolv->result_family = family;