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
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;
87 struct resolv_lookup *lookup;
90 struct resolv_nameserver {
97 GIOChannel *udp_channel;
106 guint next_lookup_id;
107 GQueue *lookup_queue;
111 GList *nameserver_list;
113 struct __res_state res;
115 GResolvDebugFunc debug_func;
119 #define debug(resolv, format, arg...) \
120 _debug(resolv, __FILE__, __func__, format, ## arg)
122 static void _debug(GResolv *resolv, const char *file, const char *caller,
123 const char *format, ...)
129 if (!resolv->debug_func)
132 va_start(ap, format);
134 if ((len = snprintf(str, sizeof(str), "%s:%s() resolv %p ",
135 file, caller, resolv)) > 0) {
136 if (vsnprintf(str + len, sizeof(str) - len, format, ap) > 0)
137 resolv->debug_func(str, resolv->debug_data);
143 static void destroy_query(struct resolv_query *query)
145 debug(query->resolv, "query %p timeout %d", query, query->timeout);
147 if (query->timeout > 0)
148 g_source_remove(query->timeout);
153 static void destroy_lookup(struct resolv_lookup *lookup)
155 debug(lookup->resolv, "lookup %p id %d ipv4 %p ipv6 %p",
156 lookup, lookup->id, lookup->ipv4_query, lookup->ipv6_query);
158 if (lookup->ipv4_query) {
159 g_queue_remove(lookup->resolv->query_queue,
161 destroy_query(lookup->ipv4_query);
164 if (lookup->ipv6_query) {
165 g_queue_remove(lookup->resolv->query_queue,
167 destroy_query(lookup->ipv6_query);
170 g_free(lookup->results);
174 static void find_srcaddr(struct sort_result *res)
176 socklen_t sl = sizeof(res->src);
179 fd = socket(res->dst.sa.sa_family, SOCK_DGRAM | SOCK_CLOEXEC,
184 if (connect(fd, &res->dst.sa, sizeof(res->dst)) < 0)
187 if (getsockname(fd, &res->src.sa, &sl) < 0)
190 res->reachable = true;
198 unsigned char addr[NS_IN6ADDRSZ];
203 static const struct gai_table gai_labels[] = {
205 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
206 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
210 .addr = { 0x20, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
211 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
215 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
216 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
220 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
221 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 },
225 /* Variations from RFC 3484, matching glibc behaviour */
226 .addr = { 0xfe, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
227 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
231 .addr = { 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
232 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
236 .addr = { 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
237 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
242 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
243 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
249 static const struct gai_table gai_precedences[] = {
251 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
252 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
256 .addr = { 0x20, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
257 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
261 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
262 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
266 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
267 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 },
271 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
272 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
278 static unsigned char v4mapped[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
279 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 };
281 static bool mask_compare(const unsigned char *one,
282 const unsigned char *two, int mask)
285 if (memcmp(one, two, mask / 8))
292 if (mask && ((*one ^ *two) >> (8 - mask)))
298 static int match_gai_table(struct sockaddr *sa, const struct gai_table *tbl)
300 struct sockaddr_in *sin = (void *)sa;
301 struct sockaddr_in6 *sin6 = (void *)sa;
304 if (sa->sa_family == AF_INET) {
306 memcpy(v4mapped+12, &sin->sin_addr, NS_INADDRSZ);
308 addr = &sin6->sin6_addr;
311 if (mask_compare(addr, tbl->addr, tbl->mask))
317 #define DQUAD(_a,_b,_c,_d) ( ((_a)<<24) | ((_b)<<16) | ((_c)<<8) | (_d) )
318 #define V4MATCH(addr, a,b,c,d, m) ( ((addr) ^ DQUAD(a,b,c,d)) >> (32 - (m)) )
320 #define RFC3484_SCOPE_LINK 2
321 #define RFC3484_SCOPE_SITE 5
322 #define RFC3484_SCOPE_GLOBAL 14
324 static int addr_scope(struct sockaddr *sa)
326 if (sa->sa_family == AF_INET) {
327 struct sockaddr_in *sin = (void *)sa;
328 guint32 addr = ntohl(sin->sin_addr.s_addr);
330 if (V4MATCH(addr, 169,254,0,0, 16) ||
331 V4MATCH(addr, 127,0,0,0, 8))
332 return RFC3484_SCOPE_LINK;
335 if (V4MATCH(addr, 10,0,0,0, 8) ||
336 V4MATCH(addr, 172,16,0,0, 12) ||
337 V4MATCH(addr, 192,168,0,0, 16))
338 return RFC3484_SCOPE_SITE;
341 return RFC3484_SCOPE_GLOBAL;
343 struct sockaddr_in6 *sin6 = (void *)sa;
345 /* Multicast addresses have a 4-bit scope field */
346 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
347 return sin6->sin6_addr.s6_addr[1] & 0xf;
349 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
350 IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
351 return RFC3484_SCOPE_LINK;
353 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
354 return RFC3484_SCOPE_SITE;
356 return RFC3484_SCOPE_GLOBAL;
360 static int rfc3484_compare(const void *__one, const void *__two)
362 const struct sort_result *one = __one;
363 const struct sort_result *two = __two;
365 /* Rule 1: Avoid unusable destinations */
366 if (one->reachable && !two->reachable)
368 else if (two->reachable && !one->reachable)
371 /* Rule 2: Prefer matching scope */
372 if (one->dst_scope == one->src_scope &&
373 two->dst_scope != two->src_scope)
375 else if (two->dst_scope == two->src_scope &&
376 one->dst_scope != one->src_scope)
379 /* Rule 3: Avoid deprecated addresses */
381 /* Rule 4: Prefer home addresses */
383 /* Rule 5: Prefer matching label */
384 if (one->dst_label == one->src_label &&
385 two->dst_label != two->src_label)
387 else if (two->dst_label == two->src_label &&
388 one->dst_label != one->src_label)
391 /* Rule 6: Prefer higher precedence */
392 if (one->precedence > two->precedence)
394 else if (two->precedence > one->precedence)
397 /* Rule 7: Prefer native transport */
399 /* Rule 8: Prefer smaller scope */
400 if (one->dst_scope != two->dst_scope)
401 return one->dst_scope - two->dst_scope;
403 /* Rule 9: Use longest matching prefix */
404 if (one->dst.sa.sa_family == AF_INET) {
406 * Rule 9 is meaningless and counterproductive for Legacy IP
407 * unless perhaps we can tell that it's actually on the local
408 * subnet. But we don't (yet) have local interface config
409 * information, so do nothing here for Legacy IP for now.
414 for (i = 0; i < 4; i++) {
415 guint32 cmp_one, cmp_two;
417 cmp_one = one->src.sin6.sin6_addr.s6_addr32[i] ^
418 one->dst.sin6.sin6_addr.s6_addr32[i];
419 cmp_two = two->src.sin6.sin6_addr.s6_addr32[i] ^
420 two->dst.sin6.sin6_addr.s6_addr32[i];
422 if (!cmp_two && !cmp_one)
425 if (cmp_one && !cmp_two)
427 if (cmp_two && !cmp_one)
430 /* g_bit_storage() is effectively fls() */
431 cmp_one = g_bit_storage(ntohl(cmp_one));
432 cmp_two = g_bit_storage(ntohl(cmp_two));
434 if (cmp_one == cmp_two)
437 return cmp_one - cmp_two;
442 /* Rule 10: Otherwise, leave the order unchanged */
449 static void rfc3484_sort_results(struct resolv_lookup *lookup)
453 for (i = 0; i < lookup->nr_results; i++) {
454 struct sort_result *res = &lookup->results[i];
456 res->precedence = match_gai_table(&res->dst.sa,
458 res->dst_label = match_gai_table(&res->dst.sa, gai_labels);
459 res->src_label = match_gai_table(&res->src.sa, gai_labels);
460 res->dst_scope = addr_scope(&res->dst.sa);
461 res->src_scope = addr_scope(&res->src.sa);
464 qsort(lookup->results, lookup->nr_results,
465 sizeof(struct sort_result), rfc3484_compare);
468 static void sort_and_return_results(struct resolv_lookup *lookup)
470 char buf[INET6_ADDRSTRLEN + 1];
471 GResolvResultStatus status;
472 char **results = g_try_new0(char *, lookup->nr_results + 1);
473 GResolvResultFunc result_func = lookup->result_func;
474 void *result_data = lookup->result_data;
480 memset(buf, 0, INET6_ADDRSTRLEN + 1);
482 rfc3484_sort_results(lookup);
484 for (i = 0; i < lookup->nr_results; i++) {
485 if (lookup->results[i].dst.sa.sa_family == AF_INET) {
486 if (!inet_ntop(AF_INET,
487 &lookup->results[i].dst.sin.sin_addr,
488 buf, sizeof(buf) - 1))
490 } else if (lookup->results[i].dst.sa.sa_family == AF_INET6) {
491 if (!inet_ntop(AF_INET6,
492 &lookup->results[i].dst.sin6.sin6_addr,
493 buf, sizeof(buf) - 1))
498 results[n++] = g_strdup(buf);
503 if (lookup->resolv->result_family == AF_INET)
504 status = lookup->ipv4_status;
505 else if (lookup->resolv->result_family == AF_INET6)
506 status = lookup->ipv6_status;
508 if (lookup->ipv6_status == G_RESOLV_RESULT_STATUS_SUCCESS)
509 status = lookup->ipv6_status;
511 status = lookup->ipv4_status;
514 debug(lookup->resolv, "lookup %p received %d results", lookup, n);
516 g_queue_remove(lookup->resolv->lookup_queue, lookup);
517 destroy_lookup(lookup);
519 result_func(status, results, result_data);
524 static gboolean query_timeout(gpointer user_data)
526 struct resolv_query *query = user_data;
527 struct resolv_lookup *lookup = query->lookup;
528 GResolv *resolv = query->resolv;
532 if (query == lookup->ipv4_query) {
533 lookup->ipv4_status = G_RESOLV_RESULT_STATUS_NO_RESPONSE;
534 lookup->ipv4_query = NULL;
535 } else if (query == lookup->ipv6_query) {
536 lookup->ipv6_status = G_RESOLV_RESULT_STATUS_NO_RESPONSE;
537 lookup->ipv6_query = NULL;
540 g_queue_remove(resolv->query_queue, query);
541 destroy_query(query);
543 if (!lookup->ipv4_query && !lookup->ipv6_query)
544 sort_and_return_results(lookup);
549 static void free_nameserver(struct resolv_nameserver *nameserver)
554 if (nameserver->udp_watch > 0)
555 g_source_remove(nameserver->udp_watch);
557 if (nameserver->udp_channel) {
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)
583 if (!resolv->nameserver_list)
586 for (list = g_list_first(resolv->nameserver_list), nr_ns = 0;
587 list; list = g_list_next(list), nr_ns++) {
588 struct resolv_nameserver *nameserver = list->data;
591 if (!nameserver->udp_channel)
594 sk = g_io_channel_unix_get_fd(nameserver->udp_channel);
596 sent = send(sk, buf, len, 0);
604 static gint compare_lookup_id(gconstpointer a, gconstpointer b)
606 const struct resolv_lookup *lookup = a;
607 guint id = GPOINTER_TO_UINT(b);
618 static gint compare_query_msgid(gconstpointer a, gconstpointer b)
620 const struct resolv_query *query = a;
621 uint16_t msgid = GPOINTER_TO_UINT(b);
623 if (query->msgid < msgid)
626 if (query->msgid > msgid)
632 static void add_result(struct resolv_lookup *lookup, int family,
635 int n = lookup->nr_results++;
636 lookup->results = g_try_realloc(lookup->results,
637 sizeof(struct sort_result) * (n + 1));
638 if (!lookup->results)
641 memset(&lookup->results[n], 0, sizeof(struct sort_result));
643 lookup->results[n].dst.sa.sa_family = family;
644 if (family == AF_INET)
645 memcpy(&lookup->results[n].dst.sin.sin_addr,
648 memcpy(&lookup->results[n].dst.sin6.sin6_addr,
652 static void parse_response(struct resolv_nameserver *nameserver,
653 const unsigned char *buf, int len)
655 GResolv *resolv = nameserver->resolv;
656 GResolvResultStatus status;
657 struct resolv_query *query;
658 struct resolv_lookup *lookup;
664 debug(resolv, "response from %s", nameserver->address);
666 if (ns_initparse(buf, len, &msg) < 0)
669 list = g_queue_find_custom(resolv->query_queue,
670 GUINT_TO_POINTER(ns_msg_id(msg)), compare_query_msgid);
674 rcode = ns_msg_getflag(msg, ns_f_rcode);
675 count = ns_msg_count(msg, ns_s_an);
677 debug(resolv, "msg id: 0x%04x rcode: %d count: %d",
678 ns_msg_id(msg), rcode, count);
682 status = G_RESOLV_RESULT_STATUS_SUCCESS;
685 status = G_RESOLV_RESULT_STATUS_FORMAT_ERROR;
688 status = G_RESOLV_RESULT_STATUS_SERVER_FAILURE;
691 status = G_RESOLV_RESULT_STATUS_NAME_ERROR;
694 status = G_RESOLV_RESULT_STATUS_NOT_IMPLEMENTED;
697 status = G_RESOLV_RESULT_STATUS_REFUSED;
700 status = G_RESOLV_RESULT_STATUS_ERROR;
707 lookup = query->lookup;
709 if (query == lookup->ipv6_query)
710 lookup->ipv6_status = status;
711 else if (query == lookup->ipv4_query)
712 lookup->ipv4_status = status;
714 for (i = 0; i < count; i++) {
715 if (ns_parserr(&msg, ns_s_an, i, &rr) < 0)
718 if (ns_rr_class(rr) != ns_c_in)
721 g_assert(offsetof(struct sockaddr_in, sin_addr) ==
722 offsetof(struct sockaddr_in6, sin6_flowinfo));
724 if (ns_rr_type(rr) == ns_t_a &&
725 ns_rr_rdlen(rr) == NS_INADDRSZ) {
726 add_result(lookup, AF_INET, ns_rr_rdata(rr));
727 } else if (ns_rr_type(rr) == ns_t_aaaa &&
728 ns_rr_rdlen(rr) == NS_IN6ADDRSZ) {
729 add_result(lookup, AF_INET6, ns_rr_rdata(rr));
733 if (status != G_RESOLV_RESULT_STATUS_SUCCESS && query->nr_ns > 0)
736 if (query == lookup->ipv6_query)
737 lookup->ipv6_query = NULL;
739 lookup->ipv4_query = NULL;
741 g_queue_remove(resolv->query_queue, query);
742 destroy_query(query);
744 if (!lookup->ipv4_query && !lookup->ipv6_query)
745 sort_and_return_results(lookup);
748 static gboolean received_udp_data(GIOChannel *channel, GIOCondition cond,
751 struct resolv_nameserver *nameserver = user_data;
752 unsigned char buf[4096];
755 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
756 nameserver->udp_watch = 0;
760 sk = g_io_channel_unix_get_fd(nameserver->udp_channel);
762 len = recv(sk, buf, sizeof(buf), 0);
766 parse_response(nameserver, buf, len);
771 static int connect_udp_channel(struct resolv_nameserver *nameserver)
773 struct addrinfo hints, *rp;
777 memset(&hints, 0, sizeof(hints));
778 hints.ai_family = AF_UNSPEC;
779 hints.ai_socktype = SOCK_DGRAM;
780 hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV | AI_NUMERICHOST;
782 sprintf(portnr, "%d", nameserver->port);
783 err = getaddrinfo(nameserver->address, portnr, &hints, &rp);
788 * Do not blindly copy this code elsewhere; it doesn't loop over the
789 * results using ->ai_next as it should. That's OK in *this* case
790 * because it was a numeric lookup; we *know* there's only one.
795 sk = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
802 * If nameserver points to localhost ip, their is no need to
803 * bind the socket on any interface.
805 if (nameserver->resolv->index > 0 &&
806 strncmp(nameserver->address, "127.0.0.1", 9) != 0) {
807 char interface[IF_NAMESIZE];
809 memset(interface, 0, IF_NAMESIZE);
810 if (if_indextoname(nameserver->resolv->index, interface)) {
811 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
812 interface, IF_NAMESIZE) < 0) {
820 if (connect(sk, rp->ai_addr, rp->ai_addrlen) < 0) {
828 nameserver->udp_channel = g_io_channel_unix_new(sk);
829 if (!nameserver->udp_channel) {
834 g_io_channel_set_close_on_unref(nameserver->udp_channel, TRUE);
836 nameserver->udp_watch = g_io_add_watch(nameserver->udp_channel,
837 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
838 received_udp_data, nameserver);
843 GResolv *g_resolv_new(int index)
850 resolv = g_try_new0(GResolv, 1);
854 resolv->ref_count = 1;
856 resolv->result_family = AF_UNSPEC;
858 resolv->next_lookup_id = 1;
860 resolv->query_queue = g_queue_new();
861 if (!resolv->query_queue) {
866 resolv->lookup_queue = g_queue_new();
867 if (!resolv->lookup_queue) {
868 g_queue_free(resolv->query_queue);
873 resolv->index = index;
874 resolv->nameserver_list = NULL;
876 res_ninit(&resolv->res);
881 GResolv *g_resolv_ref(GResolv *resolv)
886 __sync_fetch_and_add(&resolv->ref_count, 1);
891 void g_resolv_unref(GResolv *resolv)
893 struct resolv_query *query;
894 struct resolv_lookup *lookup;
899 if (__sync_fetch_and_sub(&resolv->ref_count, 1) != 1)
902 while ((lookup = g_queue_pop_head(resolv->lookup_queue))) {
903 debug(resolv, "lookup %p id %d", lookup, lookup->id);
904 destroy_lookup(lookup);
907 while ((query = g_queue_pop_head(resolv->query_queue))) {
908 debug(resolv, "query %p", query);
909 destroy_query(query);
912 g_queue_free(resolv->query_queue);
913 g_queue_free(resolv->lookup_queue);
915 flush_nameservers(resolv);
917 res_nclose(&resolv->res);
922 void g_resolv_set_debug(GResolv *resolv, GResolvDebugFunc func,
928 resolv->debug_func = func;
929 resolv->debug_data = user_data;
932 bool g_resolv_add_nameserver(GResolv *resolv, const char *address,
933 uint16_t port, unsigned long flags)
935 struct resolv_nameserver *nameserver;
940 nameserver = g_try_new0(struct resolv_nameserver, 1);
944 nameserver->address = g_strdup(address);
945 nameserver->port = port;
946 nameserver->flags = flags;
947 nameserver->resolv = resolv;
950 if (connect_udp_channel(nameserver) < 0) {
951 free_nameserver(nameserver);
956 resolv->nameserver_list = g_list_append(resolv->nameserver_list,
959 debug(resolv, "setting nameserver %s", address);
964 void g_resolv_flush_nameservers(GResolv *resolv)
969 flush_nameservers(resolv);
972 static gint add_query(struct resolv_lookup *lookup, const char *hostname, int type)
974 struct resolv_query *query = g_try_new0(struct resolv_query, 1);
975 unsigned char buf[4096];
981 len = res_mkquery(ns_o_query, hostname, ns_c_in, type,
982 NULL, 0, NULL, buf, sizeof(buf));
984 query->msgid = buf[0] << 8 | buf[1];
986 debug(lookup->resolv, "sending %d bytes", len);
988 query->nr_ns = send_query(lookup->resolv, buf, len);
989 if (query->nr_ns <= 0) {
994 query->resolv = lookup->resolv;
995 query->lookup = lookup;
997 g_queue_push_tail(lookup->resolv->query_queue, query);
999 debug(lookup->resolv, "lookup %p id %d query %p", lookup, lookup->id,
1002 query->timeout = g_timeout_add_seconds(5, query_timeout, query);
1004 if (type == ns_t_aaaa)
1005 lookup->ipv6_query = query;
1007 lookup->ipv4_query = query;
1012 guint g_resolv_lookup_hostname(GResolv *resolv, const char *hostname,
1013 GResolvResultFunc func, gpointer user_data)
1015 struct resolv_lookup *lookup;
1020 debug(resolv, "hostname %s", hostname);
1022 if (!resolv->nameserver_list) {
1025 for (i = 0; i < resolv->res.nscount; i++) {
1027 int family = resolv->res.nsaddr_list[i].sin_family;
1028 void *sa_addr = &resolv->res.nsaddr_list[i].sin_addr;
1030 if (family != AF_INET &&
1031 resolv->res._u._ext.nsaddrs[i]) {
1033 sa_addr = &resolv->res._u._ext.nsaddrs[i]->sin6_addr;
1036 if (family != AF_INET && family != AF_INET6)
1039 if (inet_ntop(family, sa_addr, buf, sizeof(buf)))
1040 g_resolv_add_nameserver(resolv, buf, 53, 0);
1043 if (!resolv->nameserver_list)
1044 g_resolv_add_nameserver(resolv, "127.0.0.1", 53, 0);
1047 lookup = g_try_new0(struct resolv_lookup, 1);
1051 lookup->resolv = resolv;
1052 lookup->result_func = func;
1053 lookup->result_data = user_data;
1054 lookup->id = resolv->next_lookup_id++;
1058 if (resolv->result_family != AF_INET6) {
1059 if (add_query(lookup, hostname, ns_t_a)) {
1067 if (resolv->result_family != AF_INET) {
1068 if (add_query(lookup, hostname, ns_t_aaaa)) {
1069 if (resolv->result_family != AF_INET6) {
1070 g_queue_remove(resolv->query_queue,
1071 lookup->ipv4_query);
1072 destroy_query(lookup->ipv4_query);
1082 g_queue_push_tail(resolv->lookup_queue, lookup);
1084 debug(resolv, "lookup %p id %d", lookup, lookup->id);
1089 bool g_resolv_cancel_lookup(GResolv *resolv, guint id)
1091 struct resolv_lookup *lookup;
1094 debug(resolv, "lookup id %d", id);
1096 list = g_queue_find_custom(resolv->lookup_queue,
1097 GUINT_TO_POINTER(id), compare_lookup_id);
1102 lookup = list->data;
1104 debug(resolv, "lookup %p", lookup);
1106 g_queue_remove(resolv->lookup_queue, lookup);
1107 destroy_lookup(lookup);
1112 bool g_resolv_set_address_family(GResolv *resolv, int family)
1117 if (family != AF_UNSPEC && family != AF_INET && family != AF_INET6)
1120 resolv->result_family = family;