3 * Resolver library with GLib integration
5 * Copyright (C) 2009-2010 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>
49 struct sockaddr_in sin;
50 struct sockaddr_in6 sin6;
54 struct sockaddr_in sin;
55 struct sockaddr_in6 sin6;
61 struct resolv_lookup {
66 struct sort_result *results;
68 struct resolv_query *ipv4_query;
69 struct resolv_query *ipv6_query;
74 GResolvResultFunc result_func;
85 struct resolv_lookup *lookup;
88 struct resolv_nameserver {
95 GIOChannel *udp_channel;
102 guint next_lookup_id;
103 GQueue *lookup_queue;
107 GList *nameserver_list;
109 struct __res_state res;
111 GResolvDebugFunc debug_func;
115 static inline void debug(GResolv *resolv, const char *format, ...)
120 if (resolv->debug_func == NULL)
123 va_start(ap, format);
125 if (vsnprintf(str, sizeof(str), format, ap) > 0)
126 resolv->debug_func(str, resolv->debug_data);
131 static void destroy_query(struct resolv_query *query)
133 if (query->timeout > 0)
134 g_source_remove(query->timeout);
139 static void destroy_lookup(struct resolv_lookup *lookup)
141 if (lookup->ipv4_query != NULL) {
142 g_queue_remove(lookup->resolv->query_queue,
144 destroy_query(lookup->ipv4_query);
147 if (lookup->ipv6_query != NULL) {
148 g_queue_remove(lookup->resolv->query_queue,
150 destroy_query(lookup->ipv6_query);
153 g_free(lookup->results);
157 static void find_srcaddr(struct sort_result *res)
159 socklen_t sl = sizeof(res->src);
162 fd = socket(res->dst.sa.sa_family, SOCK_DGRAM, IPPROTO_IP);
166 if (connect(fd, &res->dst.sa, sizeof(res->dst)) < 0) {
171 if (getsockname(fd, &res->src.sa, &sl) < 0) {
176 res->reachable = TRUE;
182 unsigned char addr[NS_IN6ADDRSZ];
187 static const struct gai_table gai_labels[] = {
189 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
190 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
194 .addr = { 0x20, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
195 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
199 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
200 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
204 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
205 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 },
209 /* Variations from RFC 3484, matching glibc behaviour */
210 .addr = { 0xfe, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
211 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
215 .addr = { 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
216 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
220 .addr = { 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
221 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
226 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
227 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
233 static const struct gai_table gai_precedences[] = {
235 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
236 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
240 .addr = { 0x20, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
241 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
245 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
246 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
250 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
251 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 },
255 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
256 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
262 static unsigned char v4mapped[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
263 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 };
265 static gboolean mask_compare(const unsigned char *one,
266 const unsigned char *two, int mask)
269 if (memcmp(one, two, mask / 8))
276 if (mask && ((*one ^ *two) >> (8 - mask)))
282 static int match_gai_table(struct sockaddr *sa, const struct gai_table *tbl)
284 struct sockaddr_in *sin = (void *)sa;
285 struct sockaddr_in6 *sin6 = (void *)sa;
288 if (sa->sa_family == AF_INET) {
290 memcpy(v4mapped+12, &sin->sin_addr, NS_INADDRSZ);
292 addr = &sin6->sin6_addr;
295 if (mask_compare(addr, tbl->addr, tbl->mask))
301 #define DQUAD(_a,_b,_c,_d) ( ((_a)<<24) | ((_b)<<16) | ((_c)<<8) | (_d) )
302 #define V4MATCH(addr, a,b,c,d, m) ( ((addr) ^ DQUAD(a,b,c,d)) >> (32 - (m)) )
304 #define RFC3484_SCOPE_LINK 2
305 #define RFC3484_SCOPE_SITE 5
306 #define RFC3484_SCOPE_GLOBAL 14
308 static int addr_scope(struct sockaddr *sa)
310 if (sa->sa_family == AF_INET) {
311 struct sockaddr_in *sin = (void *)sa;
312 guint32 addr = ntohl(sin->sin_addr.s_addr);
314 if (V4MATCH(addr, 169,254,0,0, 16) ||
315 V4MATCH(addr, 127,0,0,0, 8))
316 return RFC3484_SCOPE_LINK;
319 if (V4MATCH(addr, 10,0,0,0, 8) ||
320 V4MATCH(addr, 172,16,0,0, 12) ||
321 V4MATCH(addr, 192,168,0,0, 16))
322 return RFC3484_SCOPE_SITE;
325 return RFC3484_SCOPE_GLOBAL;
327 struct sockaddr_in6 *sin6 = (void *)sa;
329 /* Multicast addresses have a 4-bit scope field */
330 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
331 return sin6->sin6_addr.s6_addr[1] & 0xf;
333 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
334 IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
335 return RFC3484_SCOPE_LINK;
337 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
338 return RFC3484_SCOPE_SITE;
340 return RFC3484_SCOPE_GLOBAL;
344 static int rfc3484_compare(const void *__one, const void *__two)
346 const struct sort_result *one = __one;
347 const struct sort_result *two = __two;
349 /* Rule 1: Avoid unusable destinations */
350 if (one->reachable && !two->reachable)
352 else if (two->reachable && !one->reachable)
355 /* Rule 2: Prefer matching scope */
356 if (one->dst_scope == one->src_scope &&
357 two->dst_scope != two->src_scope)
359 else if (two->dst_scope == two->src_scope &&
360 one->dst_scope != one->src_scope)
363 /* Rule 3: Avoid deprecated addresses */
365 /* Rule 4: Prefer home addresses */
367 /* Rule 5: Prefer matching label */
368 if (one->dst_label == one->src_label &&
369 two->dst_label != two->src_label)
371 else if (two->dst_label == two->src_label &&
372 one->dst_label != one->src_label)
375 /* Rule 6: Prefer higher precedence */
376 if (one->precedence > two->precedence)
378 else if (two->precedence > one->precedence)
381 /* Rule 7: Prefer native transport */
383 /* Rule 8: Prefer smaller scope */
384 if (one->dst_scope != two->dst_scope)
385 return one->dst_scope - two->dst_scope;
387 /* Rule 9: Use longest matching prefix */
388 if (one->dst.sa.sa_family == AF_INET) {
390 * Rule 9 is meaningless and counterproductive for Legacy IP
391 * unless perhaps we can tell that it's actually on the local
392 * subnet. But we don't (yet) have local interface config
393 * information, so do nothing here for Legacy IP for now.
398 for (i = 0; i < 4; i++) {
399 guint32 cmp_one, cmp_two;
401 cmp_one = one->src.sin6.sin6_addr.s6_addr32[i] ^
402 one->dst.sin6.sin6_addr.s6_addr32[i];
403 cmp_two = two->src.sin6.sin6_addr.s6_addr32[i] ^
404 two->dst.sin6.sin6_addr.s6_addr32[i];
406 if (!cmp_two && !cmp_one)
409 if (cmp_one && !cmp_two)
411 if (cmp_two && !cmp_one)
414 /* g_bit_storage() is effectively fls() */
415 cmp_one = g_bit_storage(ntohl(cmp_one));
416 cmp_two = g_bit_storage(ntohl(cmp_two));
418 if (cmp_one == cmp_two)
421 return cmp_one - cmp_two;
426 /* Rule 10: Otherwise, leave the order unchanged */
433 static void rfc3484_sort_results(struct resolv_lookup *lookup)
437 for (i = 0; i < lookup->nr_results; i++) {
438 struct sort_result *res = &lookup->results[i];
440 res->precedence = match_gai_table(&res->dst.sa,
442 res->dst_label = match_gai_table(&res->dst.sa, gai_labels);
443 res->src_label = match_gai_table(&res->src.sa, gai_labels);
444 res->dst_scope = addr_scope(&res->dst.sa);
445 res->src_scope = addr_scope(&res->src.sa);
448 qsort(lookup->results, lookup->nr_results,
449 sizeof(struct sort_result), rfc3484_compare);
452 static void sort_and_return_results(struct resolv_lookup *lookup)
455 GResolvResultStatus status;
456 char **results = g_try_new0(char *, lookup->nr_results + 1);
462 rfc3484_sort_results(lookup);
464 for (i = 0; i < lookup->nr_results; i++) {
465 if (lookup->results[i].dst.sa.sa_family == AF_INET) {
466 if (inet_ntop(AF_INET,
467 &lookup->results[i].dst.sin.sin_addr,
468 buf, sizeof(buf)) == NULL)
470 } else if (lookup->results[i].dst.sa.sa_family == AF_INET6) {
471 if (inet_ntop(AF_INET6,
472 &lookup->results[i].dst.sin6.sin6_addr,
473 buf, sizeof(buf)) == NULL)
478 results[n++] = strdup(buf);
483 status = lookup->ipv4_status;
485 if (status == G_RESOLV_RESULT_STATUS_SUCCESS)
486 status = lookup->ipv6_status;
488 lookup->result_func(status, results, lookup->result_data);
491 g_queue_remove(lookup->resolv->lookup_queue, lookup);
492 destroy_lookup(lookup);
495 static gboolean query_timeout(gpointer user_data)
497 struct resolv_query *query = user_data;
498 struct resolv_lookup *lookup = query->lookup;
499 GResolv *resolv = query->resolv;
503 if (query == lookup->ipv4_query) {
504 lookup->ipv4_status = G_RESOLV_RESULT_STATUS_NO_RESPONSE;
505 lookup->ipv4_query = NULL;
506 } else if (query == lookup->ipv6_query) {
507 lookup->ipv6_status = G_RESOLV_RESULT_STATUS_NO_RESPONSE;
508 lookup->ipv6_query = NULL;
511 if (lookup->ipv4_query == NULL && lookup->ipv4_query == NULL)
512 sort_and_return_results(lookup);
514 destroy_query(query);
515 g_queue_remove(resolv->query_queue, query);
520 static void free_nameserver(struct resolv_nameserver *nameserver)
522 if (nameserver == NULL)
525 if (nameserver->udp_watch > 0)
526 g_source_remove(nameserver->udp_watch);
528 if (nameserver->udp_channel != NULL)
529 g_io_channel_unref(nameserver->udp_channel);
531 g_free(nameserver->address);
535 static void flush_nameservers(GResolv *resolv)
539 for (list = g_list_first(resolv->nameserver_list);
540 list; list = g_list_next(list))
541 free_nameserver(list->data);
543 g_list_free(resolv->nameserver_list);
544 resolv->nameserver_list = NULL;
547 static int send_query(GResolv *resolv, const unsigned char *buf, int len)
551 if (resolv->nameserver_list == NULL)
554 for (list = g_list_first(resolv->nameserver_list);
555 list; list = g_list_next(list)) {
556 struct resolv_nameserver *nameserver = list->data;
559 if (nameserver->udp_channel == NULL)
562 sk = g_io_channel_unix_get_fd(nameserver->udp_channel);
564 sent = send(sk, buf, len, 0);
570 static gint compare_lookup_id(gconstpointer a, gconstpointer b)
572 const struct resolv_lookup *lookup = a;
573 guint id = GPOINTER_TO_UINT(b);
584 static gint compare_query_msgid(gconstpointer a, gconstpointer b)
586 const struct resolv_query *query = a;
587 uint16_t msgid = GPOINTER_TO_UINT(b);
589 if (query->msgid < msgid)
592 if (query->msgid > msgid)
598 static void add_result(struct resolv_lookup *lookup, int family,
601 int n = lookup->nr_results++;
602 lookup->results = g_realloc(lookup->results,
603 sizeof(struct sort_result) * (n + 1));
605 memset(&lookup->results[n], 0, sizeof(struct sort_result));
607 lookup->results[n].dst.sa.sa_family = family;
608 if (family == AF_INET)
609 memcpy(&lookup->results[n].dst.sin.sin_addr,
612 memcpy(&lookup->results[n].dst.sin6.sin6_addr,
616 static void parse_response(struct resolv_nameserver *nameserver,
617 const unsigned char *buf, int len)
619 GResolv *resolv = nameserver->resolv;
620 GResolvResultStatus status;
621 struct resolv_query *query;
622 struct resolv_lookup *lookup;
626 int i, n, rcode, count;
628 debug(resolv, "response from %s", nameserver->address);
630 ns_initparse(buf, len, &msg);
632 rcode = ns_msg_getflag(msg, ns_f_rcode);
633 count = ns_msg_count(msg, ns_s_an);
635 debug(resolv, "msg id: 0x%04x rcode: %d count: %d",
636 ns_msg_id(msg), rcode, count);
640 status = G_RESOLV_RESULT_STATUS_SUCCESS;
643 status = G_RESOLV_RESULT_STATUS_FORMAT_ERROR;
646 status = G_RESOLV_RESULT_STATUS_SERVER_FAILURE;
649 status = G_RESOLV_RESULT_STATUS_NAME_ERROR;
652 status = G_RESOLV_RESULT_STATUS_NOT_IMPLEMENTED;
655 status = G_RESOLV_RESULT_STATUS_REFUSED;
658 status = G_RESOLV_RESULT_STATUS_ERROR;
662 list = g_queue_find_custom(resolv->query_queue,
663 GUINT_TO_POINTER(ns_msg_id(msg)), compare_query_msgid);
668 lookup = query->lookup;
670 if (query == lookup->ipv6_query) {
671 lookup->ipv6_status = status;
672 lookup->ipv6_query = NULL;
673 } else if (query == lookup->ipv4_query) {
674 lookup->ipv4_status = status;
675 lookup->ipv4_query = NULL;
678 for (i = 0, n = 0; i < count; i++) {
679 ns_parserr(&msg, ns_s_an, i, &rr);
681 if (ns_rr_class(rr) != ns_c_in)
684 g_assert(offsetof(struct sockaddr_in, sin_addr) ==
685 offsetof(struct sockaddr_in6, sin6_flowinfo));
687 if (ns_rr_type(rr) == ns_t_a &&
688 ns_rr_rdlen(rr) == NS_INADDRSZ) {
689 add_result(lookup, AF_INET, ns_rr_rdata(rr));
690 } else if (ns_rr_type(rr) == ns_t_aaaa &&
691 ns_rr_rdlen(rr) == NS_IN6ADDRSZ) {
692 add_result(lookup, AF_INET6, ns_rr_rdata(rr));
696 if (lookup->ipv4_query == NULL && lookup->ipv6_query == NULL)
697 sort_and_return_results(lookup);
699 destroy_query(query);
700 g_queue_remove(resolv->query_queue, query);
703 static gboolean received_udp_data(GIOChannel *channel, GIOCondition cond,
706 struct resolv_nameserver *nameserver = user_data;
707 unsigned char buf[4096];
710 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
711 nameserver->udp_watch = 0;
715 sk = g_io_channel_unix_get_fd(nameserver->udp_channel);
717 len = recv(sk, buf, sizeof(buf), 0);
721 parse_response(nameserver, buf, len);
726 static int connect_udp_channel(struct resolv_nameserver *nameserver)
728 struct addrinfo hints, *rp;
732 memset(&hints, 0, sizeof(hints));
733 hints.ai_family = AF_UNSPEC;
734 hints.ai_socktype = SOCK_DGRAM;
735 hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV | AI_NUMERICHOST;
737 sprintf(portnr, "%d", nameserver->port);
738 err = getaddrinfo(nameserver->address, portnr, &hints, &rp);
743 * Do not blindly copy this code elsewhere; it doesn't loop over the
744 * results using ->ai_next as it should. That's OK in *this* case
745 * because it was a numeric lookup; we *know* there's only one.
750 sk = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
756 if (connect(sk, rp->ai_addr, rp->ai_addrlen) < 0) {
764 nameserver->udp_channel = g_io_channel_unix_new(sk);
765 if (nameserver->udp_channel == NULL) {
770 g_io_channel_set_close_on_unref(nameserver->udp_channel, TRUE);
772 nameserver->udp_watch = g_io_add_watch(nameserver->udp_channel,
773 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
774 received_udp_data, nameserver);
779 GResolv *g_resolv_new(int index)
786 resolv = g_try_new0(GResolv, 1);
790 resolv->ref_count = 1;
792 resolv->next_lookup_id = 1;
794 resolv->query_queue = g_queue_new();
795 if (resolv->query_queue == NULL) {
800 resolv->lookup_queue = g_queue_new();
801 if (resolv->lookup_queue == NULL) {
802 g_queue_free(resolv->query_queue);
807 resolv->index = index;
808 resolv->nameserver_list = NULL;
810 res_ninit(&resolv->res);
815 GResolv *g_resolv_ref(GResolv *resolv)
820 g_atomic_int_inc(&resolv->ref_count);
825 void g_resolv_unref(GResolv *resolv)
827 struct resolv_query *query;
832 if (g_atomic_int_dec_and_test(&resolv->ref_count) == FALSE)
835 while ((query = g_queue_pop_head(resolv->query_queue)))
836 destroy_query(query);
838 g_queue_free(resolv->query_queue);
839 g_queue_free(resolv->lookup_queue);
841 flush_nameservers(resolv);
843 res_nclose(&resolv->res);
848 void g_resolv_set_debug(GResolv *resolv, GResolvDebugFunc func,
854 resolv->debug_func = func;
855 resolv->debug_data = user_data;
858 gboolean g_resolv_add_nameserver(GResolv *resolv, const char *address,
859 uint16_t port, unsigned long flags)
861 struct resolv_nameserver *nameserver;
866 nameserver = g_try_new0(struct resolv_nameserver, 1);
867 if (nameserver == NULL)
870 nameserver->address = g_strdup(address);
871 nameserver->port = port;
872 nameserver->flags = flags;
874 if (connect_udp_channel(nameserver) < 0) {
875 free_nameserver(nameserver);
879 nameserver->resolv = resolv;
881 resolv->nameserver_list = g_list_append(resolv->nameserver_list,
884 debug(resolv, "setting nameserver %s", address);
889 void g_resolv_flush_nameservers(GResolv *resolv)
894 flush_nameservers(resolv);
897 static gint add_query(struct resolv_lookup *lookup, const char *hostname, int type)
899 struct resolv_query *query = g_try_new0(struct resolv_query, 1);
900 unsigned char buf[4096];
906 len = res_mkquery(ns_o_query, hostname, ns_c_in, type,
907 NULL, 0, NULL, buf, sizeof(buf));
909 query->msgid = buf[0] << 8 | buf[1];
911 if (send_query(lookup->resolv, buf, len) < 0)
914 query->resolv = lookup->resolv;
915 query->lookup = lookup;
917 g_queue_push_tail(lookup->resolv->query_queue, query);
919 query->timeout = g_timeout_add_seconds(5, query_timeout, query);
921 if (type == ns_t_aaaa)
922 lookup->ipv6_query = query;
924 lookup->ipv4_query = query;
929 guint g_resolv_lookup_hostname(GResolv *resolv, const char *hostname,
930 GResolvResultFunc func, gpointer user_data)
932 struct resolv_lookup *lookup;
934 debug(resolv, "lookup hostname %s", hostname);
939 if (resolv->nameserver_list == NULL) {
942 for (i = 0; i < resolv->res.nscount; i++) {
944 int family = resolv->res.nsaddr_list[i].sin_family;
945 void *sa_addr = &resolv->res.nsaddr_list[i].sin_addr;
947 if (family != AF_INET &&
948 resolv->res._u._ext.nsaddrs[i]) {
950 sa_addr = &resolv->res._u._ext.nsaddrs[i]->sin6_addr;
953 if (family != AF_INET && family != AF_INET6)
956 if (inet_ntop(family, sa_addr, buf, sizeof(buf)))
957 g_resolv_add_nameserver(resolv, buf, 53, 0);
960 if (resolv->nameserver_list == NULL)
961 g_resolv_add_nameserver(resolv, "127.0.0.1", 53, 0);
964 lookup = g_try_new0(struct resolv_lookup, 1);
968 lookup->resolv = resolv;
969 lookup->result_func = func;
970 lookup->result_data = user_data;
971 lookup->id = resolv->next_lookup_id++;
973 if (add_query(lookup, hostname, ns_t_a)) {
978 if (add_query(lookup, hostname, ns_t_aaaa)) {
979 destroy_query(lookup->ipv4_query);
980 g_queue_remove(resolv->query_queue, lookup->ipv4_query);
985 g_queue_push_tail(resolv->lookup_queue, lookup);
989 gboolean g_resolv_cancel_lookup(GResolv *resolv, guint id)
993 list = g_queue_find_custom(resolv->lookup_queue,
994 GUINT_TO_POINTER(id), compare_lookup_id);
999 destroy_lookup(list->data);
1000 g_queue_remove(resolv->query_queue, list->data);