3 * Resolver library with GLib integration
5 * Copyright (C) 2009-2013 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
33 #include <sys/types.h>
34 #include <sys/socket.h>
36 #include <arpa/inet.h>
37 #include <arpa/nameser.h>
51 struct sockaddr_in sin;
52 struct sockaddr_in6 sin6;
56 struct sockaddr_in sin;
57 struct sockaddr_in6 sin6;
63 struct resolv_lookup {
68 struct sort_result *results;
70 struct resolv_query *ipv4_query;
71 struct resolv_query *ipv6_query;
76 GResolvResultFunc result_func;
88 struct resolv_lookup *lookup;
91 struct resolv_nameserver {
98 GIOChannel *udp_channel;
107 guint next_lookup_id;
108 GQueue *lookup_queue;
112 GList *nameserver_list;
114 struct __res_state res;
116 GResolvDebugFunc debug_func;
120 #define debug(resolv, format, arg...) \
121 _debug(resolv, __FILE__, __func__, format, ## arg)
123 static void _debug(GResolv *resolv, const char *file, const char *caller,
124 const char *format, ...)
130 if (!resolv->debug_func)
133 va_start(ap, format);
135 if ((len = snprintf(str, sizeof(str), "%s:%s() resolv %p ",
136 file, caller, resolv)) > 0) {
137 if (vsnprintf(str + len, sizeof(str) - len, format, ap) > 0)
138 resolv->debug_func(str, resolv->debug_data);
144 static void destroy_query(struct resolv_query *query)
146 debug(query->resolv, "query %p timeout %d", query, query->timeout);
148 if (query->timeout > 0)
149 g_source_remove(query->timeout);
154 static void destroy_lookup(struct resolv_lookup *lookup)
156 debug(lookup->resolv, "lookup %p id %d ipv4 %p ipv6 %p",
157 lookup, lookup->id, lookup->ipv4_query, lookup->ipv6_query);
159 if (lookup->ipv4_query) {
160 g_queue_remove(lookup->resolv->query_queue,
162 destroy_query(lookup->ipv4_query);
165 if (lookup->ipv6_query) {
166 g_queue_remove(lookup->resolv->query_queue,
168 destroy_query(lookup->ipv6_query);
171 g_free(lookup->results);
175 static void find_srcaddr(struct sort_result *res)
177 socklen_t sl = sizeof(res->src);
180 fd = socket(res->dst.sa.sa_family, SOCK_DGRAM | SOCK_CLOEXEC,
185 if (connect(fd, &res->dst.sa, sizeof(res->dst)) < 0)
188 if (getsockname(fd, &res->src.sa, &sl) < 0)
191 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 bool 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);
474 GResolvResultFunc result_func = lookup->result_func;
475 void *result_data = lookup->result_data;
481 memset(buf, 0, INET6_ADDRSTRLEN + 1);
483 rfc3484_sort_results(lookup);
485 for (i = 0; i < lookup->nr_results; i++) {
486 if (lookup->results[i].dst.sa.sa_family == AF_INET) {
487 if (!inet_ntop(AF_INET,
488 &lookup->results[i].dst.sin.sin_addr,
489 buf, sizeof(buf) - 1))
491 } else if (lookup->results[i].dst.sa.sa_family == AF_INET6) {
492 if (!inet_ntop(AF_INET6,
493 &lookup->results[i].dst.sin6.sin6_addr,
494 buf, sizeof(buf) - 1))
499 results[n++] = g_strdup(buf);
504 if (lookup->resolv->result_family == AF_INET)
505 status = lookup->ipv4_status;
506 else if (lookup->resolv->result_family == AF_INET6)
507 status = lookup->ipv6_status;
509 if (lookup->ipv6_status == G_RESOLV_RESULT_STATUS_SUCCESS)
510 status = lookup->ipv6_status;
512 status = lookup->ipv4_status;
515 debug(lookup->resolv, "lookup %p received %d results", lookup, n-1);
517 g_queue_remove(lookup->resolv->lookup_queue, lookup);
518 destroy_lookup(lookup);
520 result_func(status, results, result_data);
525 static gboolean query_timeout(gpointer user_data)
527 struct resolv_query *query = user_data;
528 struct resolv_lookup *lookup = query->lookup;
529 GResolv *resolv = query->resolv;
533 if (query == lookup->ipv4_query) {
534 lookup->ipv4_status = G_RESOLV_RESULT_STATUS_NO_RESPONSE;
535 lookup->ipv4_query = NULL;
536 } else if (query == lookup->ipv6_query) {
537 lookup->ipv6_status = G_RESOLV_RESULT_STATUS_NO_RESPONSE;
538 lookup->ipv6_query = NULL;
541 g_queue_remove(resolv->query_queue, query);
542 destroy_query(query);
544 if (!lookup->ipv4_query && !lookup->ipv6_query)
545 sort_and_return_results(lookup);
550 static void free_nameserver(struct resolv_nameserver *nameserver)
555 if (nameserver->udp_watch > 0)
556 g_source_remove(nameserver->udp_watch);
558 if (nameserver->udp_channel) {
559 g_io_channel_shutdown(nameserver->udp_channel, TRUE, NULL);
560 g_io_channel_unref(nameserver->udp_channel);
563 g_free(nameserver->address);
567 static void flush_nameservers(GResolv *resolv)
571 for (list = g_list_first(resolv->nameserver_list);
572 list; list = g_list_next(list))
573 free_nameserver(list->data);
575 g_list_free(resolv->nameserver_list);
576 resolv->nameserver_list = NULL;
579 static int send_query(GResolv *resolv, const unsigned char *buf, int len)
584 if (!resolv->nameserver_list)
587 for (list = g_list_first(resolv->nameserver_list), nr_ns = 0;
588 list; list = g_list_next(list), nr_ns++) {
589 struct resolv_nameserver *nameserver = list->data;
592 if (!nameserver->udp_channel)
595 sk = g_io_channel_unix_get_fd(nameserver->udp_channel);
597 sent = send(sk, buf, len, 0);
605 static gint compare_lookup_id(gconstpointer a, gconstpointer b)
607 const struct resolv_lookup *lookup = a;
608 guint id = GPOINTER_TO_UINT(b);
619 static gint compare_query_msgid(gconstpointer a, gconstpointer b)
621 const struct resolv_query *query = a;
622 uint16_t msgid = GPOINTER_TO_UINT(b);
624 if (query->msgid < msgid)
627 if (query->msgid > msgid)
633 static void add_result(struct resolv_lookup *lookup, int family,
636 int n = lookup->nr_results++;
637 lookup->results = g_try_realloc(lookup->results,
638 sizeof(struct sort_result) * (n + 1));
639 if (!lookup->results)
642 memset(&lookup->results[n], 0, sizeof(struct sort_result));
644 lookup->results[n].dst.sa.sa_family = family;
645 if (family == AF_INET)
646 memcpy(&lookup->results[n].dst.sin.sin_addr,
649 memcpy(&lookup->results[n].dst.sin6.sin6_addr,
653 static void parse_response(struct resolv_nameserver *nameserver,
654 const unsigned char *buf, int len)
656 GResolv *resolv = nameserver->resolv;
657 GResolvResultStatus status;
658 struct resolv_query *query;
659 struct resolv_lookup *lookup;
665 debug(resolv, "response from %s", nameserver->address);
667 if (ns_initparse(buf, len, &msg) < 0)
670 list = g_queue_find_custom(resolv->query_queue,
671 GUINT_TO_POINTER(ns_msg_id(msg)), compare_query_msgid);
675 rcode = ns_msg_getflag(msg, ns_f_rcode);
676 count = ns_msg_count(msg, ns_s_an);
678 debug(resolv, "msg id: 0x%04x rcode: %d count: %d",
679 ns_msg_id(msg), rcode, count);
684 status = G_RESOLV_RESULT_STATUS_SUCCESS;
686 status = G_RESOLV_RESULT_STATUS_NO_ANSWER;
689 status = G_RESOLV_RESULT_STATUS_FORMAT_ERROR;
692 status = G_RESOLV_RESULT_STATUS_SERVER_FAILURE;
695 status = G_RESOLV_RESULT_STATUS_NAME_ERROR;
698 status = G_RESOLV_RESULT_STATUS_NOT_IMPLEMENTED;
701 status = G_RESOLV_RESULT_STATUS_REFUSED;
704 status = G_RESOLV_RESULT_STATUS_ERROR;
711 lookup = query->lookup;
713 if (query == lookup->ipv6_query)
714 lookup->ipv6_status = status;
715 else if (query == lookup->ipv4_query)
716 lookup->ipv4_status = status;
718 for (i = 0; i < count; i++) {
719 if (ns_parserr(&msg, ns_s_an, i, &rr) < 0)
722 if (ns_rr_class(rr) != ns_c_in)
725 g_assert(offsetof(struct sockaddr_in, sin_addr) ==
726 offsetof(struct sockaddr_in6, sin6_flowinfo));
728 if (ns_rr_type(rr) == ns_t_a &&
729 ns_rr_rdlen(rr) == NS_INADDRSZ) {
730 add_result(lookup, AF_INET, ns_rr_rdata(rr));
731 } else if (ns_rr_type(rr) == ns_t_aaaa &&
732 ns_rr_rdlen(rr) == NS_IN6ADDRSZ) {
733 add_result(lookup, AF_INET6, ns_rr_rdata(rr));
737 if (status != G_RESOLV_RESULT_STATUS_SUCCESS && query->nr_ns > 0)
740 if (query == lookup->ipv6_query)
741 lookup->ipv6_query = NULL;
743 lookup->ipv4_query = NULL;
745 g_queue_remove(resolv->query_queue, query);
746 destroy_query(query);
748 if (!lookup->ipv4_query && !lookup->ipv6_query)
749 sort_and_return_results(lookup);
752 static gboolean received_udp_data(GIOChannel *channel, GIOCondition cond,
755 struct resolv_nameserver *nameserver = user_data;
756 unsigned char buf[4096];
759 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
760 nameserver->udp_watch = 0;
764 sk = g_io_channel_unix_get_fd(nameserver->udp_channel);
766 len = recv(sk, buf, sizeof(buf), 0);
770 parse_response(nameserver, buf, len);
775 static int connect_udp_channel(struct resolv_nameserver *nameserver)
777 struct addrinfo hints, *rp;
781 memset(&hints, 0, sizeof(hints));
782 hints.ai_family = AF_UNSPEC;
783 hints.ai_socktype = SOCK_DGRAM;
784 hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV | AI_NUMERICHOST;
786 sprintf(portnr, "%d", nameserver->port);
787 err = getaddrinfo(nameserver->address, portnr, &hints, &rp);
792 * Do not blindly copy this code elsewhere; it doesn't loop over the
793 * results using ->ai_next as it should. That's OK in *this* case
794 * because it was a numeric lookup; we *know* there's only one.
799 sk = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
806 * If nameserver points to localhost ip, their is no need to
807 * bind the socket on any interface.
809 if (nameserver->resolv->index > 0 &&
810 strncmp(nameserver->address, "127.0.0.1", 9) != 0) {
811 char interface[IF_NAMESIZE];
813 memset(interface, 0, IF_NAMESIZE);
814 if (if_indextoname(nameserver->resolv->index, interface)) {
815 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
816 interface, IF_NAMESIZE) < 0) {
824 if (connect(sk, rp->ai_addr, rp->ai_addrlen) < 0) {
832 nameserver->udp_channel = g_io_channel_unix_new(sk);
833 if (!nameserver->udp_channel) {
838 g_io_channel_set_close_on_unref(nameserver->udp_channel, TRUE);
840 nameserver->udp_watch = g_io_add_watch(nameserver->udp_channel,
841 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
842 received_udp_data, nameserver);
847 GResolv *g_resolv_new(int index)
854 resolv = g_try_new0(GResolv, 1);
858 resolv->ref_count = 1;
860 resolv->result_family = AF_UNSPEC;
862 resolv->next_lookup_id = 1;
864 resolv->query_queue = g_queue_new();
865 if (!resolv->query_queue) {
870 resolv->lookup_queue = g_queue_new();
871 if (!resolv->lookup_queue) {
872 g_queue_free(resolv->query_queue);
877 resolv->index = index;
878 resolv->nameserver_list = NULL;
880 res_ninit(&resolv->res);
885 GResolv *g_resolv_ref(GResolv *resolv)
890 __sync_fetch_and_add(&resolv->ref_count, 1);
895 void g_resolv_unref(GResolv *resolv)
897 struct resolv_query *query;
898 struct resolv_lookup *lookup;
903 if (__sync_fetch_and_sub(&resolv->ref_count, 1) != 1)
906 while ((lookup = g_queue_pop_head(resolv->lookup_queue))) {
907 debug(resolv, "lookup %p id %d", lookup, lookup->id);
908 destroy_lookup(lookup);
911 while ((query = g_queue_pop_head(resolv->query_queue))) {
912 debug(resolv, "query %p", query);
913 destroy_query(query);
916 g_queue_free(resolv->query_queue);
917 g_queue_free(resolv->lookup_queue);
919 flush_nameservers(resolv);
921 res_nclose(&resolv->res);
926 void g_resolv_set_debug(GResolv *resolv, GResolvDebugFunc func,
932 resolv->debug_func = func;
933 resolv->debug_data = user_data;
936 bool g_resolv_add_nameserver(GResolv *resolv, const char *address,
937 uint16_t port, unsigned long flags)
939 struct resolv_nameserver *nameserver;
944 nameserver = g_try_new0(struct resolv_nameserver, 1);
948 nameserver->address = g_strdup(address);
949 nameserver->port = port;
950 nameserver->flags = flags;
951 nameserver->resolv = resolv;
953 if (connect_udp_channel(nameserver) < 0) {
954 free_nameserver(nameserver);
958 resolv->nameserver_list = g_list_append(resolv->nameserver_list,
961 debug(resolv, "setting nameserver %s", address);
966 void g_resolv_flush_nameservers(GResolv *resolv)
971 flush_nameservers(resolv);
974 static gint add_query(struct resolv_lookup *lookup, const char *hostname, int type)
976 struct resolv_query *query = g_try_new0(struct resolv_query, 1);
977 unsigned char buf[4096];
983 len = res_mkquery(ns_o_query, hostname, ns_c_in, type,
984 NULL, 0, NULL, buf, sizeof(buf));
986 query->msgid = buf[0] << 8 | buf[1];
988 debug(lookup->resolv, "sending %d bytes", len);
990 query->nr_ns = send_query(lookup->resolv, buf, len);
991 if (query->nr_ns <= 0) {
996 query->resolv = lookup->resolv;
997 query->lookup = lookup;
999 g_queue_push_tail(lookup->resolv->query_queue, query);
1001 debug(lookup->resolv, "lookup %p id %d query %p", lookup, lookup->id,
1004 query->timeout = g_timeout_add_seconds(5, query_timeout, query);
1006 if (type == ns_t_aaaa)
1007 lookup->ipv6_query = query;
1009 lookup->ipv4_query = query;
1014 guint g_resolv_lookup_hostname(GResolv *resolv, const char *hostname,
1015 GResolvResultFunc func, gpointer user_data)
1017 struct resolv_lookup *lookup;
1022 debug(resolv, "hostname %s", hostname);
1024 if (!resolv->nameserver_list) {
1027 for (i = 0; i < resolv->res.nscount; i++) {
1029 int family = resolv->res.nsaddr_list[i].sin_family;
1030 void *sa_addr = &resolv->res.nsaddr_list[i].sin_addr;
1032 if (family != AF_INET &&
1033 resolv->res._u._ext.nsaddrs[i]) {
1035 sa_addr = &resolv->res._u._ext.nsaddrs[i]->sin6_addr;
1038 if (family != AF_INET && family != AF_INET6)
1041 if (inet_ntop(family, sa_addr, buf, sizeof(buf)))
1042 g_resolv_add_nameserver(resolv, buf, 53, 0);
1045 if (!resolv->nameserver_list)
1046 g_resolv_add_nameserver(resolv, "127.0.0.1", 53, 0);
1049 lookup = g_try_new0(struct resolv_lookup, 1);
1053 lookup->resolv = resolv;
1054 lookup->result_func = func;
1055 lookup->result_data = user_data;
1056 lookup->id = resolv->next_lookup_id++;
1058 if (resolv->result_family != AF_INET6) {
1059 if (add_query(lookup, hostname, ns_t_a)) {
1065 if (resolv->result_family != AF_INET) {
1066 if (add_query(lookup, hostname, ns_t_aaaa)) {
1067 if (resolv->result_family != AF_INET6) {
1068 g_queue_remove(resolv->query_queue,
1069 lookup->ipv4_query);
1070 destroy_query(lookup->ipv4_query);
1078 g_queue_push_tail(resolv->lookup_queue, lookup);
1080 debug(resolv, "lookup %p id %d", lookup, lookup->id);
1085 bool g_resolv_cancel_lookup(GResolv *resolv, guint id)
1087 struct resolv_lookup *lookup;
1090 debug(resolv, "lookup id %d", id);
1092 list = g_queue_find_custom(resolv->lookup_queue,
1093 GUINT_TO_POINTER(id), compare_lookup_id);
1098 lookup = list->data;
1100 debug(resolv, "lookup %p", lookup);
1102 g_queue_remove(resolv->lookup_queue, lookup);
1103 destroy_lookup(lookup);
1108 bool g_resolv_set_address_family(GResolv *resolv, int family)
1113 if (family != AF_UNSPEC && family != AF_INET && family != AF_INET6)
1116 resolv->result_family = family;