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) ( (((uint32_t)_a)<<24) | (((uint32_t)_b)<<16) | \
319 (((uint32_t)_c)<<8) | ((uint32_t)_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 uint32_t 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-1);
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);
685 status = G_RESOLV_RESULT_STATUS_SUCCESS;
687 status = G_RESOLV_RESULT_STATUS_NO_ANSWER;
690 status = G_RESOLV_RESULT_STATUS_FORMAT_ERROR;
693 status = G_RESOLV_RESULT_STATUS_SERVER_FAILURE;
696 status = G_RESOLV_RESULT_STATUS_NAME_ERROR;
699 status = G_RESOLV_RESULT_STATUS_NOT_IMPLEMENTED;
702 status = G_RESOLV_RESULT_STATUS_REFUSED;
705 status = G_RESOLV_RESULT_STATUS_ERROR;
712 lookup = query->lookup;
714 if (query == lookup->ipv6_query)
715 lookup->ipv6_status = status;
716 else if (query == lookup->ipv4_query)
717 lookup->ipv4_status = status;
719 for (i = 0; i < count; i++) {
720 if (ns_parserr(&msg, ns_s_an, i, &rr) < 0)
723 if (ns_rr_class(rr) != ns_c_in)
726 g_assert(offsetof(struct sockaddr_in, sin_addr) ==
727 offsetof(struct sockaddr_in6, sin6_flowinfo));
729 if (ns_rr_type(rr) == ns_t_a &&
730 ns_rr_rdlen(rr) == NS_INADDRSZ) {
731 add_result(lookup, AF_INET, ns_rr_rdata(rr));
732 } else if (ns_rr_type(rr) == ns_t_aaaa &&
733 ns_rr_rdlen(rr) == NS_IN6ADDRSZ) {
734 add_result(lookup, AF_INET6, ns_rr_rdata(rr));
738 if (status != G_RESOLV_RESULT_STATUS_SUCCESS && query->nr_ns > 0)
741 if (query == lookup->ipv6_query)
742 lookup->ipv6_query = NULL;
744 lookup->ipv4_query = NULL;
746 g_queue_remove(resolv->query_queue, query);
747 destroy_query(query);
749 if (!lookup->ipv4_query && !lookup->ipv6_query)
750 sort_and_return_results(lookup);
753 static gboolean received_udp_data(GIOChannel *channel, GIOCondition cond,
756 struct resolv_nameserver *nameserver = user_data;
757 unsigned char buf[4096];
760 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
761 nameserver->udp_watch = 0;
765 sk = g_io_channel_unix_get_fd(nameserver->udp_channel);
767 len = recv(sk, buf, sizeof(buf), 0);
771 parse_response(nameserver, buf, len);
776 static int connect_udp_channel(struct resolv_nameserver *nameserver)
778 struct addrinfo hints, *rp;
782 memset(&hints, 0, sizeof(hints));
783 hints.ai_family = AF_UNSPEC;
784 hints.ai_socktype = SOCK_DGRAM;
785 hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV | AI_NUMERICHOST;
787 sprintf(portnr, "%d", nameserver->port);
788 err = getaddrinfo(nameserver->address, portnr, &hints, &rp);
793 * Do not blindly copy this code elsewhere; it doesn't loop over the
794 * results using ->ai_next as it should. That's OK in *this* case
795 * because it was a numeric lookup; we *know* there's only one.
800 sk = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
807 * If nameserver points to localhost ip, their is no need to
808 * bind the socket on any interface.
810 if (nameserver->resolv->index > 0 &&
811 strncmp(nameserver->address, "127.0.0.1", 9) != 0) {
812 char interface[IF_NAMESIZE];
814 memset(interface, 0, IF_NAMESIZE);
815 if (if_indextoname(nameserver->resolv->index, interface)) {
816 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
817 interface, IF_NAMESIZE) < 0) {
825 if (connect(sk, rp->ai_addr, rp->ai_addrlen) < 0) {
833 nameserver->udp_channel = g_io_channel_unix_new(sk);
834 if (!nameserver->udp_channel) {
839 g_io_channel_set_close_on_unref(nameserver->udp_channel, TRUE);
841 nameserver->udp_watch = g_io_add_watch(nameserver->udp_channel,
842 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
843 received_udp_data, nameserver);
848 GResolv *g_resolv_new(int index)
855 resolv = g_try_new0(GResolv, 1);
859 resolv->ref_count = 1;
861 resolv->result_family = AF_UNSPEC;
863 resolv->next_lookup_id = 1;
865 resolv->query_queue = g_queue_new();
866 if (!resolv->query_queue) {
871 resolv->lookup_queue = g_queue_new();
872 if (!resolv->lookup_queue) {
873 g_queue_free(resolv->query_queue);
878 resolv->index = index;
879 resolv->nameserver_list = NULL;
881 res_ninit(&resolv->res);
886 GResolv *g_resolv_ref(GResolv *resolv)
891 __sync_fetch_and_add(&resolv->ref_count, 1);
896 void g_resolv_unref(GResolv *resolv)
898 struct resolv_query *query;
899 struct resolv_lookup *lookup;
904 if (__sync_fetch_and_sub(&resolv->ref_count, 1) != 1)
907 while ((lookup = g_queue_pop_head(resolv->lookup_queue))) {
908 debug(resolv, "lookup %p id %d", lookup, lookup->id);
909 destroy_lookup(lookup);
912 while ((query = g_queue_pop_head(resolv->query_queue))) {
913 debug(resolv, "query %p", query);
914 destroy_query(query);
917 g_queue_free(resolv->query_queue);
918 g_queue_free(resolv->lookup_queue);
920 flush_nameservers(resolv);
922 res_nclose(&resolv->res);
927 void g_resolv_set_debug(GResolv *resolv, GResolvDebugFunc func,
933 resolv->debug_func = func;
934 resolv->debug_data = user_data;
937 bool g_resolv_add_nameserver(GResolv *resolv, const char *address,
938 uint16_t port, unsigned long flags)
940 struct resolv_nameserver *nameserver;
945 nameserver = g_try_new0(struct resolv_nameserver, 1);
949 nameserver->address = g_strdup(address);
950 nameserver->port = port;
951 nameserver->flags = flags;
952 nameserver->resolv = resolv;
954 if (connect_udp_channel(nameserver) < 0) {
955 free_nameserver(nameserver);
959 resolv->nameserver_list = g_list_append(resolv->nameserver_list,
962 debug(resolv, "setting nameserver %s", address);
967 void g_resolv_flush_nameservers(GResolv *resolv)
972 flush_nameservers(resolv);
975 static gint add_query(struct resolv_lookup *lookup, const char *hostname, int type)
977 struct resolv_query *query = g_try_new0(struct resolv_query, 1);
978 unsigned char buf[4096];
984 len = res_mkquery(ns_o_query, hostname, ns_c_in, type,
985 NULL, 0, NULL, buf, sizeof(buf));
987 query->msgid = buf[0] << 8 | buf[1];
989 debug(lookup->resolv, "sending %d bytes", len);
991 query->nr_ns = send_query(lookup->resolv, buf, len);
992 if (query->nr_ns <= 0) {
997 query->resolv = lookup->resolv;
998 query->lookup = lookup;
1000 g_queue_push_tail(lookup->resolv->query_queue, query);
1002 debug(lookup->resolv, "lookup %p id %d query %p", lookup, lookup->id,
1005 query->timeout = g_timeout_add_seconds(5, query_timeout, query);
1007 if (type == ns_t_aaaa)
1008 lookup->ipv6_query = query;
1010 lookup->ipv4_query = query;
1015 guint g_resolv_lookup_hostname(GResolv *resolv, const char *hostname,
1016 GResolvResultFunc func, gpointer user_data)
1018 struct resolv_lookup *lookup;
1023 debug(resolv, "hostname %s", hostname);
1025 if (!resolv->nameserver_list) {
1028 for (i = 0; i < resolv->res.nscount; i++) {
1030 int family = resolv->res.nsaddr_list[i].sin_family;
1031 void *sa_addr = &resolv->res.nsaddr_list[i].sin_addr;
1033 if (family != AF_INET &&
1034 resolv->res._u._ext.nsaddrs[i]) {
1036 sa_addr = &resolv->res._u._ext.nsaddrs[i]->sin6_addr;
1039 if (family != AF_INET && family != AF_INET6)
1042 if (inet_ntop(family, sa_addr, buf, sizeof(buf)))
1043 g_resolv_add_nameserver(resolv, buf, 53, 0);
1046 if (!resolv->nameserver_list)
1047 g_resolv_add_nameserver(resolv, "127.0.0.1", 53, 0);
1050 lookup = g_try_new0(struct resolv_lookup, 1);
1054 lookup->resolv = resolv;
1055 lookup->result_func = func;
1056 lookup->result_data = user_data;
1057 lookup->id = resolv->next_lookup_id++;
1059 if (resolv->result_family != AF_INET6) {
1060 if (add_query(lookup, hostname, ns_t_a)) {
1066 if (resolv->result_family != AF_INET) {
1067 if (add_query(lookup, hostname, ns_t_aaaa)) {
1068 if (resolv->result_family != AF_INET6) {
1069 g_queue_remove(resolv->query_queue,
1070 lookup->ipv4_query);
1071 destroy_query(lookup->ipv4_query);
1079 g_queue_push_tail(resolv->lookup_queue, lookup);
1081 debug(resolv, "lookup %p id %d", lookup, lookup->id);
1086 bool g_resolv_cancel_lookup(GResolv *resolv, guint id)
1088 struct resolv_lookup *lookup;
1091 debug(resolv, "lookup id %d", id);
1093 list = g_queue_find_custom(resolv->lookup_queue,
1094 GUINT_TO_POINTER(id), compare_lookup_id);
1099 lookup = list->data;
1101 debug(resolv, "lookup %p", lookup);
1103 g_queue_remove(resolv->lookup_queue, lookup);
1104 destroy_lookup(lookup);
1109 bool g_resolv_set_address_family(GResolv *resolv, int family)
1114 if (family != AF_UNSPEC && family != AF_INET && family != AF_INET6)
1117 resolv->result_family = family;