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) {
189 if (getsockname(fd, &res->src.sa, &sl) < 0) {
194 res->reachable = true;
200 unsigned char addr[NS_IN6ADDRSZ];
205 static const struct gai_table gai_labels[] = {
207 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
208 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
212 .addr = { 0x20, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
213 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
217 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
218 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
222 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
223 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 },
227 /* Variations from RFC 3484, matching glibc behaviour */
228 .addr = { 0xfe, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
229 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
233 .addr = { 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
234 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
238 .addr = { 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
239 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
244 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
245 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
251 static const struct gai_table gai_precedences[] = {
253 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
254 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
258 .addr = { 0x20, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
259 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
263 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
264 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
268 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
269 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 },
273 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
274 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
280 static unsigned char v4mapped[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
281 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 };
283 static bool mask_compare(const unsigned char *one,
284 const unsigned char *two, int mask)
287 if (memcmp(one, two, mask / 8))
294 if (mask && ((*one ^ *two) >> (8 - mask)))
300 static int match_gai_table(struct sockaddr *sa, const struct gai_table *tbl)
302 struct sockaddr_in *sin = (void *)sa;
303 struct sockaddr_in6 *sin6 = (void *)sa;
306 if (sa->sa_family == AF_INET) {
308 memcpy(v4mapped+12, &sin->sin_addr, NS_INADDRSZ);
310 addr = &sin6->sin6_addr;
313 if (mask_compare(addr, tbl->addr, tbl->mask))
319 #define DQUAD(_a,_b,_c,_d) ( ((_a)<<24) | ((_b)<<16) | ((_c)<<8) | (_d) )
320 #define V4MATCH(addr, a,b,c,d, m) ( ((addr) ^ DQUAD(a,b,c,d)) >> (32 - (m)) )
322 #define RFC3484_SCOPE_LINK 2
323 #define RFC3484_SCOPE_SITE 5
324 #define RFC3484_SCOPE_GLOBAL 14
326 static int addr_scope(struct sockaddr *sa)
328 if (sa->sa_family == AF_INET) {
329 struct sockaddr_in *sin = (void *)sa;
330 guint32 addr = ntohl(sin->sin_addr.s_addr);
332 if (V4MATCH(addr, 169,254,0,0, 16) ||
333 V4MATCH(addr, 127,0,0,0, 8))
334 return RFC3484_SCOPE_LINK;
337 if (V4MATCH(addr, 10,0,0,0, 8) ||
338 V4MATCH(addr, 172,16,0,0, 12) ||
339 V4MATCH(addr, 192,168,0,0, 16))
340 return RFC3484_SCOPE_SITE;
343 return RFC3484_SCOPE_GLOBAL;
345 struct sockaddr_in6 *sin6 = (void *)sa;
347 /* Multicast addresses have a 4-bit scope field */
348 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
349 return sin6->sin6_addr.s6_addr[1] & 0xf;
351 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
352 IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
353 return RFC3484_SCOPE_LINK;
355 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
356 return RFC3484_SCOPE_SITE;
358 return RFC3484_SCOPE_GLOBAL;
362 static int rfc3484_compare(const void *__one, const void *__two)
364 const struct sort_result *one = __one;
365 const struct sort_result *two = __two;
367 /* Rule 1: Avoid unusable destinations */
368 if (one->reachable && !two->reachable)
370 else if (two->reachable && !one->reachable)
373 /* Rule 2: Prefer matching scope */
374 if (one->dst_scope == one->src_scope &&
375 two->dst_scope != two->src_scope)
377 else if (two->dst_scope == two->src_scope &&
378 one->dst_scope != one->src_scope)
381 /* Rule 3: Avoid deprecated addresses */
383 /* Rule 4: Prefer home addresses */
385 /* Rule 5: Prefer matching label */
386 if (one->dst_label == one->src_label &&
387 two->dst_label != two->src_label)
389 else if (two->dst_label == two->src_label &&
390 one->dst_label != one->src_label)
393 /* Rule 6: Prefer higher precedence */
394 if (one->precedence > two->precedence)
396 else if (two->precedence > one->precedence)
399 /* Rule 7: Prefer native transport */
401 /* Rule 8: Prefer smaller scope */
402 if (one->dst_scope != two->dst_scope)
403 return one->dst_scope - two->dst_scope;
405 /* Rule 9: Use longest matching prefix */
406 if (one->dst.sa.sa_family == AF_INET) {
408 * Rule 9 is meaningless and counterproductive for Legacy IP
409 * unless perhaps we can tell that it's actually on the local
410 * subnet. But we don't (yet) have local interface config
411 * information, so do nothing here for Legacy IP for now.
416 for (i = 0; i < 4; i++) {
417 guint32 cmp_one, cmp_two;
419 cmp_one = one->src.sin6.sin6_addr.s6_addr32[i] ^
420 one->dst.sin6.sin6_addr.s6_addr32[i];
421 cmp_two = two->src.sin6.sin6_addr.s6_addr32[i] ^
422 two->dst.sin6.sin6_addr.s6_addr32[i];
424 if (!cmp_two && !cmp_one)
427 if (cmp_one && !cmp_two)
429 if (cmp_two && !cmp_one)
432 /* g_bit_storage() is effectively fls() */
433 cmp_one = g_bit_storage(ntohl(cmp_one));
434 cmp_two = g_bit_storage(ntohl(cmp_two));
436 if (cmp_one == cmp_two)
439 return cmp_one - cmp_two;
444 /* Rule 10: Otherwise, leave the order unchanged */
451 static void rfc3484_sort_results(struct resolv_lookup *lookup)
455 for (i = 0; i < lookup->nr_results; i++) {
456 struct sort_result *res = &lookup->results[i];
458 res->precedence = match_gai_table(&res->dst.sa,
460 res->dst_label = match_gai_table(&res->dst.sa, gai_labels);
461 res->src_label = match_gai_table(&res->src.sa, gai_labels);
462 res->dst_scope = addr_scope(&res->dst.sa);
463 res->src_scope = addr_scope(&res->src.sa);
466 qsort(lookup->results, lookup->nr_results,
467 sizeof(struct sort_result), rfc3484_compare);
470 static void sort_and_return_results(struct resolv_lookup *lookup)
472 char buf[INET6_ADDRSTRLEN + 1];
473 GResolvResultStatus status;
474 char **results = g_try_new0(char *, lookup->nr_results + 1);
475 GResolvResultFunc result_func = lookup->result_func;
476 void *result_data = lookup->result_data;
482 memset(buf, 0, INET6_ADDRSTRLEN + 1);
484 rfc3484_sort_results(lookup);
486 for (i = 0; i < lookup->nr_results; i++) {
487 if (lookup->results[i].dst.sa.sa_family == AF_INET) {
488 if (!inet_ntop(AF_INET,
489 &lookup->results[i].dst.sin.sin_addr,
490 buf, sizeof(buf) - 1))
492 } else if (lookup->results[i].dst.sa.sa_family == AF_INET6) {
493 if (!inet_ntop(AF_INET6,
494 &lookup->results[i].dst.sin6.sin6_addr,
495 buf, sizeof(buf) - 1))
500 results[n++] = g_strdup(buf);
505 if (lookup->resolv->result_family == AF_INET)
506 status = lookup->ipv4_status;
507 else if (lookup->resolv->result_family == AF_INET6)
508 status = lookup->ipv6_status;
510 if (lookup->ipv6_status == G_RESOLV_RESULT_STATUS_SUCCESS)
511 status = lookup->ipv6_status;
513 status = lookup->ipv4_status;
516 debug(lookup->resolv, "lookup %p received %d results", lookup, n);
518 g_queue_remove(lookup->resolv->lookup_queue, lookup);
519 destroy_lookup(lookup);
521 result_func(status, results, result_data);
526 static gboolean query_timeout(gpointer user_data)
528 struct resolv_query *query = user_data;
529 struct resolv_lookup *lookup = query->lookup;
530 GResolv *resolv = query->resolv;
534 if (query == lookup->ipv4_query) {
535 lookup->ipv4_status = G_RESOLV_RESULT_STATUS_NO_RESPONSE;
536 lookup->ipv4_query = NULL;
537 } else if (query == lookup->ipv6_query) {
538 lookup->ipv6_status = G_RESOLV_RESULT_STATUS_NO_RESPONSE;
539 lookup->ipv6_query = NULL;
542 g_queue_remove(resolv->query_queue, query);
543 destroy_query(query);
545 if (!lookup->ipv4_query && !lookup->ipv6_query)
546 sort_and_return_results(lookup);
551 static void free_nameserver(struct resolv_nameserver *nameserver)
556 if (nameserver->udp_watch > 0)
557 g_source_remove(nameserver->udp_watch);
559 if (nameserver->udp_channel) {
560 g_io_channel_shutdown(nameserver->udp_channel, TRUE, NULL);
561 g_io_channel_unref(nameserver->udp_channel);
564 g_free(nameserver->address);
568 static void flush_nameservers(GResolv *resolv)
572 for (list = g_list_first(resolv->nameserver_list);
573 list; list = g_list_next(list))
574 free_nameserver(list->data);
576 g_list_free(resolv->nameserver_list);
577 resolv->nameserver_list = NULL;
580 static int send_query(GResolv *resolv, const unsigned char *buf, int len)
585 if (!resolv->nameserver_list)
588 for (list = g_list_first(resolv->nameserver_list), nr_ns = 0;
589 list; list = g_list_next(list), nr_ns++) {
590 struct resolv_nameserver *nameserver = list->data;
593 if (!nameserver->udp_channel)
596 sk = g_io_channel_unix_get_fd(nameserver->udp_channel);
598 sent = send(sk, buf, len, 0);
606 static gint compare_lookup_id(gconstpointer a, gconstpointer b)
608 const struct resolv_lookup *lookup = a;
609 guint id = GPOINTER_TO_UINT(b);
620 static gint compare_query_msgid(gconstpointer a, gconstpointer b)
622 const struct resolv_query *query = a;
623 uint16_t msgid = GPOINTER_TO_UINT(b);
625 if (query->msgid < msgid)
628 if (query->msgid > msgid)
634 static void add_result(struct resolv_lookup *lookup, int family,
637 int n = lookup->nr_results++;
638 lookup->results = g_try_realloc(lookup->results,
639 sizeof(struct sort_result) * (n + 1));
640 if (!lookup->results)
643 memset(&lookup->results[n], 0, sizeof(struct sort_result));
645 lookup->results[n].dst.sa.sa_family = family;
646 if (family == AF_INET)
647 memcpy(&lookup->results[n].dst.sin.sin_addr,
650 memcpy(&lookup->results[n].dst.sin6.sin6_addr,
654 static void parse_response(struct resolv_nameserver *nameserver,
655 const unsigned char *buf, int len)
657 GResolv *resolv = nameserver->resolv;
658 GResolvResultStatus status;
659 struct resolv_query *query;
660 struct resolv_lookup *lookup;
666 debug(resolv, "response from %s", nameserver->address);
668 if (ns_initparse(buf, len, &msg) < 0)
671 list = g_queue_find_custom(resolv->query_queue,
672 GUINT_TO_POINTER(ns_msg_id(msg)), compare_query_msgid);
676 rcode = ns_msg_getflag(msg, ns_f_rcode);
677 count = ns_msg_count(msg, ns_s_an);
679 debug(resolv, "msg id: 0x%04x rcode: %d count: %d",
680 ns_msg_id(msg), rcode, count);
684 status = G_RESOLV_RESULT_STATUS_SUCCESS;
687 status = G_RESOLV_RESULT_STATUS_FORMAT_ERROR;
690 status = G_RESOLV_RESULT_STATUS_SERVER_FAILURE;
693 status = G_RESOLV_RESULT_STATUS_NAME_ERROR;
696 status = G_RESOLV_RESULT_STATUS_NOT_IMPLEMENTED;
699 status = G_RESOLV_RESULT_STATUS_REFUSED;
702 status = G_RESOLV_RESULT_STATUS_ERROR;
709 lookup = query->lookup;
711 if (query == lookup->ipv6_query)
712 lookup->ipv6_status = status;
713 else if (query == lookup->ipv4_query)
714 lookup->ipv4_status = status;
716 for (i = 0; i < count; i++) {
717 if (ns_parserr(&msg, ns_s_an, i, &rr) < 0)
720 if (ns_rr_class(rr) != ns_c_in)
723 g_assert(offsetof(struct sockaddr_in, sin_addr) ==
724 offsetof(struct sockaddr_in6, sin6_flowinfo));
726 if (ns_rr_type(rr) == ns_t_a &&
727 ns_rr_rdlen(rr) == NS_INADDRSZ) {
728 add_result(lookup, AF_INET, ns_rr_rdata(rr));
729 } else if (ns_rr_type(rr) == ns_t_aaaa &&
730 ns_rr_rdlen(rr) == NS_IN6ADDRSZ) {
731 add_result(lookup, AF_INET6, ns_rr_rdata(rr));
735 if (status != G_RESOLV_RESULT_STATUS_SUCCESS && query->nr_ns > 0)
738 if (query == lookup->ipv6_query)
739 lookup->ipv6_query = NULL;
741 lookup->ipv4_query = NULL;
743 g_queue_remove(resolv->query_queue, query);
744 destroy_query(query);
746 if (!lookup->ipv4_query && !lookup->ipv6_query)
747 sort_and_return_results(lookup);
750 static gboolean received_udp_data(GIOChannel *channel, GIOCondition cond,
753 struct resolv_nameserver *nameserver = user_data;
754 unsigned char buf[4096];
757 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
758 nameserver->udp_watch = 0;
762 sk = g_io_channel_unix_get_fd(nameserver->udp_channel);
764 len = recv(sk, buf, sizeof(buf), 0);
768 parse_response(nameserver, buf, len);
773 static int connect_udp_channel(struct resolv_nameserver *nameserver)
775 struct addrinfo hints, *rp;
779 memset(&hints, 0, sizeof(hints));
780 hints.ai_family = AF_UNSPEC;
781 hints.ai_socktype = SOCK_DGRAM;
782 hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV | AI_NUMERICHOST;
784 sprintf(portnr, "%d", nameserver->port);
785 err = getaddrinfo(nameserver->address, portnr, &hints, &rp);
790 * Do not blindly copy this code elsewhere; it doesn't loop over the
791 * results using ->ai_next as it should. That's OK in *this* case
792 * because it was a numeric lookup; we *know* there's only one.
797 sk = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
804 * If nameserver points to localhost ip, their is no need to
805 * bind the socket on any interface.
807 if (nameserver->resolv->index > 0 &&
808 strncmp(nameserver->address, "127.0.0.1", 9) != 0) {
809 char interface[IF_NAMESIZE];
811 memset(interface, 0, IF_NAMESIZE);
812 if (if_indextoname(nameserver->resolv->index, interface)) {
813 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
814 interface, IF_NAMESIZE) < 0) {
822 if (connect(sk, rp->ai_addr, rp->ai_addrlen) < 0) {
830 nameserver->udp_channel = g_io_channel_unix_new(sk);
831 if (!nameserver->udp_channel) {
836 g_io_channel_set_close_on_unref(nameserver->udp_channel, TRUE);
838 nameserver->udp_watch = g_io_add_watch(nameserver->udp_channel,
839 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
840 received_udp_data, nameserver);
845 GResolv *g_resolv_new(int index)
852 resolv = g_try_new0(GResolv, 1);
856 resolv->ref_count = 1;
858 resolv->result_family = AF_UNSPEC;
860 resolv->next_lookup_id = 1;
862 resolv->query_queue = g_queue_new();
863 if (!resolv->query_queue) {
868 resolv->lookup_queue = g_queue_new();
869 if (!resolv->lookup_queue) {
870 g_queue_free(resolv->query_queue);
875 resolv->index = index;
876 resolv->nameserver_list = NULL;
878 res_ninit(&resolv->res);
883 GResolv *g_resolv_ref(GResolv *resolv)
888 __sync_fetch_and_add(&resolv->ref_count, 1);
893 void g_resolv_unref(GResolv *resolv)
895 struct resolv_query *query;
896 struct resolv_lookup *lookup;
901 if (__sync_fetch_and_sub(&resolv->ref_count, 1) != 1)
904 while ((lookup = g_queue_pop_head(resolv->lookup_queue))) {
905 debug(resolv, "lookup %p id %d", lookup, lookup->id);
906 destroy_lookup(lookup);
909 while ((query = g_queue_pop_head(resolv->query_queue))) {
910 debug(resolv, "query %p", query);
911 destroy_query(query);
914 g_queue_free(resolv->query_queue);
915 g_queue_free(resolv->lookup_queue);
917 flush_nameservers(resolv);
919 res_nclose(&resolv->res);
924 void g_resolv_set_debug(GResolv *resolv, GResolvDebugFunc func,
930 resolv->debug_func = func;
931 resolv->debug_data = user_data;
934 bool g_resolv_add_nameserver(GResolv *resolv, const char *address,
935 uint16_t port, unsigned long flags)
937 struct resolv_nameserver *nameserver;
942 nameserver = g_try_new0(struct resolv_nameserver, 1);
946 nameserver->address = g_strdup(address);
947 nameserver->port = port;
948 nameserver->flags = flags;
949 nameserver->resolv = resolv;
952 if (connect_udp_channel(nameserver) < 0) {
953 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++;
1060 if (resolv->result_family != AF_INET6) {
1061 if (add_query(lookup, hostname, ns_t_a)) {
1069 if (resolv->result_family != AF_INET) {
1070 if (add_query(lookup, hostname, ns_t_aaaa)) {
1071 if (resolv->result_family != AF_INET6) {
1072 g_queue_remove(resolv->query_queue,
1073 lookup->ipv4_query);
1074 destroy_query(lookup->ipv4_query);
1084 g_queue_push_tail(resolv->lookup_queue, lookup);
1086 debug(resolv, "lookup %p id %d", lookup, lookup->id);
1091 bool g_resolv_cancel_lookup(GResolv *resolv, guint id)
1093 struct resolv_lookup *lookup;
1096 debug(resolv, "lookup id %d", id);
1098 list = g_queue_find_custom(resolv->lookup_queue,
1099 GUINT_TO_POINTER(id), compare_lookup_id);
1104 lookup = list->data;
1106 debug(resolv, "lookup %p", lookup);
1108 g_queue_remove(resolv->lookup_queue, lookup);
1109 destroy_lookup(lookup);
1114 bool g_resolv_set_address_family(GResolv *resolv, int family)
1119 if (family != AF_UNSPEC && family != AF_INET && family != AF_INET6)
1122 resolv->result_family = family;