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 void sort_and_return_results(struct resolv_lookup *lookup);
116 static void rfc3484_sort_results(struct resolv_lookup *lookup);
118 static inline void debug(GResolv *resolv, const char *format, ...)
123 if (resolv->debug_func == NULL)
126 va_start(ap, format);
128 if (vsnprintf(str, sizeof(str), format, ap) > 0)
129 resolv->debug_func(str, resolv->debug_data);
134 static void destroy_query(struct resolv_query *query)
136 if (query->timeout > 0)
137 g_source_remove(query->timeout);
142 static void destroy_lookup(struct resolv_lookup *lookup)
144 if (lookup->ipv4_query != NULL) {
145 g_queue_remove(lookup->resolv->query_queue,
147 destroy_query(lookup->ipv4_query);
150 if (lookup->ipv6_query != NULL) {
151 g_queue_remove(lookup->resolv->query_queue,
153 destroy_query(lookup->ipv6_query);
156 g_free(lookup->results);
160 static gboolean query_timeout(gpointer user_data)
162 struct resolv_query *query = user_data;
163 struct resolv_lookup *lookup = query->lookup;
164 GResolv *resolv = query->resolv;
168 if (query == lookup->ipv4_query) {
169 lookup->ipv4_status = G_RESOLV_RESULT_STATUS_NO_RESPONSE;
170 lookup->ipv4_query = NULL;
171 } else if (query == lookup->ipv6_query) {
172 lookup->ipv6_status = G_RESOLV_RESULT_STATUS_NO_RESPONSE;
173 lookup->ipv6_query = NULL;
176 if (lookup->ipv4_query == NULL && lookup->ipv4_query == NULL)
177 sort_and_return_results(lookup);
179 destroy_query(query);
180 g_queue_remove(resolv->query_queue, query);
185 static void free_nameserver(struct resolv_nameserver *nameserver)
187 if (nameserver == NULL)
190 if (nameserver->udp_watch > 0)
191 g_source_remove(nameserver->udp_watch);
193 if (nameserver->udp_channel != NULL)
194 g_io_channel_unref(nameserver->udp_channel);
196 g_free(nameserver->address);
200 static void flush_nameservers(GResolv *resolv)
204 for (list = g_list_first(resolv->nameserver_list);
205 list; list = g_list_next(list))
206 free_nameserver(list->data);
208 g_list_free(resolv->nameserver_list);
209 resolv->nameserver_list = NULL;
212 static int send_query(GResolv *resolv, const unsigned char *buf, int len)
216 if (resolv->nameserver_list == NULL)
219 for (list = g_list_first(resolv->nameserver_list);
220 list; list = g_list_next(list)) {
221 struct resolv_nameserver *nameserver = list->data;
224 if (nameserver->udp_channel == NULL)
227 sk = g_io_channel_unix_get_fd(nameserver->udp_channel);
229 sent = send(sk, buf, len, 0);
235 static gint compare_lookup_id(gconstpointer a, gconstpointer b)
237 const struct resolv_lookup *lookup = a;
238 guint id = GPOINTER_TO_UINT(b);
249 static gint compare_query_msgid(gconstpointer a, gconstpointer b)
251 const struct resolv_query *query = a;
252 uint16_t msgid = GPOINTER_TO_UINT(b);
254 if (query->msgid < msgid)
257 if (query->msgid > msgid)
263 static void add_result(struct resolv_lookup *lookup, int family,
266 int n = lookup->nr_results++;
267 lookup->results = g_realloc(lookup->results,
268 sizeof(struct sort_result) * (n + 1));
270 memset(&lookup->results[n], 0, sizeof(struct sort_result));
272 lookup->results[n].dst.sa.sa_family = family;
273 if (family == AF_INET)
274 memcpy(&lookup->results[n].dst.sin.sin_addr,
277 memcpy(&lookup->results[n].dst.sin6.sin6_addr,
281 static void parse_response(struct resolv_nameserver *nameserver,
282 const unsigned char *buf, int len)
284 GResolv *resolv = nameserver->resolv;
285 GResolvResultStatus status;
286 struct resolv_query *query;
287 struct resolv_lookup *lookup;
291 int i, n, rcode, count;
293 debug(resolv, "response from %s", nameserver->address);
295 ns_initparse(buf, len, &msg);
297 rcode = ns_msg_getflag(msg, ns_f_rcode);
298 count = ns_msg_count(msg, ns_s_an);
300 debug(resolv, "msg id: 0x%04x rcode: %d count: %d",
301 ns_msg_id(msg), rcode, count);
305 status = G_RESOLV_RESULT_STATUS_SUCCESS;
308 status = G_RESOLV_RESULT_STATUS_FORMAT_ERROR;
311 status = G_RESOLV_RESULT_STATUS_SERVER_FAILURE;
314 status = G_RESOLV_RESULT_STATUS_NAME_ERROR;
317 status = G_RESOLV_RESULT_STATUS_NOT_IMPLEMENTED;
320 status = G_RESOLV_RESULT_STATUS_REFUSED;
323 status = G_RESOLV_RESULT_STATUS_ERROR;
327 list = g_queue_find_custom(resolv->query_queue,
328 GUINT_TO_POINTER(ns_msg_id(msg)), compare_query_msgid);
333 lookup = query->lookup;
335 if (query == lookup->ipv6_query) {
336 lookup->ipv6_status = status;
337 lookup->ipv6_query = NULL;
338 } else if (query == lookup->ipv4_query) {
339 lookup->ipv4_status = status;
340 lookup->ipv4_query = NULL;
343 for (i = 0, n = 0; i < count; i++) {
344 ns_parserr(&msg, ns_s_an, i, &rr);
346 if (ns_rr_class(rr) != ns_c_in)
349 g_assert(offsetof(struct sockaddr_in, sin_addr) ==
350 offsetof(struct sockaddr_in6, sin6_flowinfo));
352 if (ns_rr_type(rr) == ns_t_a &&
353 ns_rr_rdlen(rr) == NS_INADDRSZ) {
354 add_result(lookup, AF_INET, ns_rr_rdata(rr));
355 } else if (ns_rr_type(rr) == ns_t_aaaa &&
356 ns_rr_rdlen(rr) == NS_IN6ADDRSZ) {
357 add_result(lookup, AF_INET6, ns_rr_rdata(rr));
361 if (lookup->ipv4_query == NULL && lookup->ipv6_query == NULL)
362 sort_and_return_results(lookup);
364 destroy_query(query);
365 g_queue_remove(resolv->query_queue, query);
368 static gboolean received_udp_data(GIOChannel *channel, GIOCondition cond,
371 struct resolv_nameserver *nameserver = user_data;
372 unsigned char buf[4096];
375 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
376 nameserver->udp_watch = 0;
380 sk = g_io_channel_unix_get_fd(nameserver->udp_channel);
382 len = recv(sk, buf, sizeof(buf), 0);
386 parse_response(nameserver, buf, len);
391 static int connect_udp_channel(struct resolv_nameserver *nameserver)
393 struct addrinfo hints, *rp;
397 memset(&hints, 0, sizeof(hints));
398 hints.ai_family = AF_UNSPEC;
399 hints.ai_socktype = SOCK_DGRAM;
400 hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV | AI_NUMERICHOST;
402 sprintf(portnr, "%d", nameserver->port);
403 err = getaddrinfo(nameserver->address, portnr, &hints, &rp);
408 * Do not blindly copy this code elsewhere; it doesn't loop over the
409 * results using ->ai_next as it should. That's OK in *this* case
410 * because it was a numeric lookup; we *know* there's only one.
415 sk = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
421 if (connect(sk, rp->ai_addr, rp->ai_addrlen) < 0) {
429 nameserver->udp_channel = g_io_channel_unix_new(sk);
430 if (nameserver->udp_channel == NULL) {
435 g_io_channel_set_close_on_unref(nameserver->udp_channel, TRUE);
437 nameserver->udp_watch = g_io_add_watch(nameserver->udp_channel,
438 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
439 received_udp_data, nameserver);
444 GResolv *g_resolv_new(int index)
451 resolv = g_try_new0(GResolv, 1);
455 resolv->ref_count = 1;
457 resolv->next_lookup_id = 1;
459 resolv->query_queue = g_queue_new();
460 if (resolv->query_queue == NULL) {
465 resolv->lookup_queue = g_queue_new();
466 if (resolv->lookup_queue == NULL) {
467 g_queue_free(resolv->query_queue);
472 resolv->index = index;
473 resolv->nameserver_list = NULL;
475 res_ninit(&resolv->res);
480 GResolv *g_resolv_ref(GResolv *resolv)
485 g_atomic_int_inc(&resolv->ref_count);
490 void g_resolv_unref(GResolv *resolv)
492 struct resolv_query *query;
497 if (g_atomic_int_dec_and_test(&resolv->ref_count) == FALSE)
500 while ((query = g_queue_pop_head(resolv->query_queue)))
501 destroy_query(query);
503 g_queue_free(resolv->query_queue);
504 g_queue_free(resolv->lookup_queue);
506 flush_nameservers(resolv);
508 res_nclose(&resolv->res);
513 void g_resolv_set_debug(GResolv *resolv, GResolvDebugFunc func,
519 resolv->debug_func = func;
520 resolv->debug_data = user_data;
523 gboolean g_resolv_add_nameserver(GResolv *resolv, const char *address,
524 uint16_t port, unsigned long flags)
526 struct resolv_nameserver *nameserver;
531 nameserver = g_try_new0(struct resolv_nameserver, 1);
532 if (nameserver == NULL)
535 nameserver->address = g_strdup(address);
536 nameserver->port = port;
537 nameserver->flags = flags;
539 if (connect_udp_channel(nameserver) < 0) {
540 free_nameserver(nameserver);
544 nameserver->resolv = resolv;
546 resolv->nameserver_list = g_list_append(resolv->nameserver_list,
549 debug(resolv, "setting nameserver %s", address);
554 void g_resolv_flush_nameservers(GResolv *resolv)
559 flush_nameservers(resolv);
562 static void sort_and_return_results(struct resolv_lookup *lookup)
565 GResolvResultStatus status;
566 char **results = g_try_new0(char *, lookup->nr_results + 1);
572 rfc3484_sort_results(lookup);
574 for (i = 0; i < lookup->nr_results; i++) {
575 if (lookup->results[i].dst.sa.sa_family == AF_INET) {
576 if (inet_ntop(AF_INET,
577 &lookup->results[i].dst.sin.sin_addr,
578 buf, sizeof(buf)) == NULL)
580 } else if (lookup->results[i].dst.sa.sa_family == AF_INET6) {
581 if (inet_ntop(AF_INET6,
582 &lookup->results[i].dst.sin6.sin6_addr,
583 buf, sizeof(buf)) == NULL)
588 results[n++] = strdup(buf);
593 status = lookup->ipv4_status;
595 if (status == G_RESOLV_RESULT_STATUS_SUCCESS)
596 status = lookup->ipv6_status;
598 lookup->result_func(status, results, lookup->result_data);
601 g_queue_remove(lookup->resolv->lookup_queue, lookup);
602 destroy_lookup(lookup);
605 static gint add_query(struct resolv_lookup *lookup, const char *hostname, int type)
607 struct resolv_query *query = g_try_new0(struct resolv_query, 1);
608 unsigned char buf[4096];
614 len = res_mkquery(ns_o_query, hostname, ns_c_in, type,
615 NULL, 0, NULL, buf, sizeof(buf));
617 query->msgid = buf[0] << 8 | buf[1];
619 if (send_query(lookup->resolv, buf, len) < 0)
622 query->resolv = lookup->resolv;
623 query->lookup = lookup;
625 g_queue_push_tail(lookup->resolv->query_queue, query);
627 query->timeout = g_timeout_add_seconds(5, query_timeout, query);
629 if (type == ns_t_aaaa)
630 lookup->ipv6_query = query;
632 lookup->ipv4_query = query;
637 guint g_resolv_lookup_hostname(GResolv *resolv, const char *hostname,
638 GResolvResultFunc func, gpointer user_data)
640 struct resolv_lookup *lookup;
642 debug(resolv, "lookup hostname %s", hostname);
647 if (resolv->nameserver_list == NULL) {
650 for (i = 0; i < resolv->res.nscount; i++) {
652 int family = resolv->res.nsaddr_list[i].sin_family;
653 void *sa_addr = &resolv->res.nsaddr_list[i].sin_addr;
655 if (family != AF_INET &&
656 resolv->res._u._ext.nsaddrs[i]) {
658 sa_addr = &resolv->res._u._ext.nsaddrs[i]->sin6_addr;
661 if (family != AF_INET && family != AF_INET6)
664 if (inet_ntop(family, sa_addr, buf, sizeof(buf)))
665 g_resolv_add_nameserver(resolv, buf, 53, 0);
668 if (resolv->nameserver_list == NULL)
669 g_resolv_add_nameserver(resolv, "127.0.0.1", 53, 0);
672 lookup = g_try_new0(struct resolv_lookup, 1);
676 lookup->resolv = resolv;
677 lookup->result_func = func;
678 lookup->result_data = user_data;
679 lookup->id = resolv->next_lookup_id++;
681 if (add_query(lookup, hostname, ns_t_a)) {
686 if (add_query(lookup, hostname, ns_t_aaaa)) {
687 destroy_query(lookup->ipv4_query);
688 g_queue_remove(resolv->query_queue, lookup->ipv4_query);
693 g_queue_push_tail(resolv->lookup_queue, lookup);
697 gboolean g_resolv_cancel_lookup(GResolv *resolv, guint id)
701 list = g_queue_find_custom(resolv->lookup_queue,
702 GUINT_TO_POINTER(id), compare_lookup_id);
707 destroy_lookup(list->data);
708 g_queue_remove(resolv->query_queue, list->data);
713 static void find_srcaddr(struct sort_result *res)
715 socklen_t sl = sizeof(res->src);
718 fd = socket(res->dst.sa.sa_family, SOCK_DGRAM, IPPROTO_IP);
722 if (connect(fd, &res->dst.sa, sizeof(res->dst)) < 0) {
727 if (getsockname(fd, &res->src.sa, &sl) < 0) {
732 res->reachable = TRUE;
738 unsigned char addr[NS_IN6ADDRSZ];
743 static const struct gai_table gai_labels[] = {
745 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
746 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
750 .addr = { 0x20, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
751 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
755 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
756 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
760 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
761 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 },
765 /* Variations from RFC 3484, matching glibc behaviour */
766 .addr = { 0xfe, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
767 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
771 .addr = { 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
772 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
776 .addr = { 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
777 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
782 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
783 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
789 static const struct gai_table gai_precedences[] = {
791 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
792 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
796 .addr = { 0x20, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
797 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
801 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
802 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
806 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
807 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 },
811 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
812 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
818 static unsigned char v4mapped[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
819 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 };
821 static gboolean mask_compare(const unsigned char *one,
822 const unsigned char *two, int mask)
825 if (memcmp(one, two, mask / 8))
832 if (mask && ((*one ^ *two) >> (8 - mask)))
838 static int match_gai_table(struct sockaddr *sa, const struct gai_table *tbl)
840 struct sockaddr_in *sin = (void *)sa;
841 struct sockaddr_in6 *sin6 = (void *)sa;
844 if (sa->sa_family == AF_INET) {
846 memcpy(v4mapped+12, &sin->sin_addr, NS_INADDRSZ);
848 addr = &sin6->sin6_addr;
851 if (mask_compare(addr, tbl->addr, tbl->mask))
857 #define DQUAD(_a,_b,_c,_d) ( ((_a)<<24) | ((_b)<<16) | ((_c)<<8) | (_d) )
858 #define V4MATCH(addr, a,b,c,d, m) ( ((addr) ^ DQUAD(a,b,c,d)) >> (32 - (m)) )
860 #define RFC3484_SCOPE_LINK 2
861 #define RFC3484_SCOPE_SITE 5
862 #define RFC3484_SCOPE_GLOBAL 14
864 static int addr_scope(struct sockaddr *sa)
866 if (sa->sa_family == AF_INET) {
867 struct sockaddr_in *sin = (void *)sa;
868 guint32 addr = ntohl(sin->sin_addr.s_addr);
870 if (V4MATCH(addr, 169,254,0,0, 16) ||
871 V4MATCH(addr, 127,0,0,0, 8))
872 return RFC3484_SCOPE_LINK;
875 if (V4MATCH(addr, 10,0,0,0, 8) ||
876 V4MATCH(addr, 172,16,0,0, 12) ||
877 V4MATCH(addr, 192,168,0,0, 16))
878 return RFC3484_SCOPE_SITE;
881 return RFC3484_SCOPE_GLOBAL;
883 struct sockaddr_in6 *sin6 = (void *)sa;
885 /* Multicast addresses have a 4-bit scope field */
886 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
887 return sin6->sin6_addr.s6_addr[1] & 0xf;
889 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
890 IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
891 return RFC3484_SCOPE_LINK;
893 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
894 return RFC3484_SCOPE_SITE;
896 return RFC3484_SCOPE_GLOBAL;
900 static int rfc3484_compare(const void *__one, const void *__two)
902 const struct sort_result *one = __one;
903 const struct sort_result *two = __two;
905 /* Rule 1: Avoid unusable destinations */
906 if (one->reachable && !two->reachable)
908 else if (two->reachable && !one->reachable)
911 /* Rule 2: Prefer matching scope */
912 if (one->dst_scope == one->src_scope &&
913 two->dst_scope != two->src_scope)
915 else if (two->dst_scope == two->src_scope &&
916 one->dst_scope != one->src_scope)
919 /* Rule 3: Avoid deprecated addresses */
921 /* Rule 4: Prefer home addresses */
923 /* Rule 5: Prefer matching label */
924 if (one->dst_label == one->src_label &&
925 two->dst_label != two->src_label)
927 else if (two->dst_label == two->src_label &&
928 one->dst_label != one->src_label)
931 /* Rule 6: Prefer higher precedence */
932 if (one->precedence > two->precedence)
934 else if (two->precedence > one->precedence)
937 /* Rule 7: Prefer native transport */
939 /* Rule 8: Prefer smaller scope */
940 if (one->dst_scope != two->dst_scope)
941 return one->dst_scope - two->dst_scope;
943 /* Rule 9: Use longest matching prefix */
944 if (one->dst.sa.sa_family == AF_INET) {
946 * Rule 9 is meaningless and counterproductive for Legacy IP
947 * unless perhaps we can tell that it's actually on the local
948 * subnet. But we don't (yet) have local interface config
949 * information, so do nothing here for Legacy IP for now.
954 for (i = 0; i < 4; i++) {
955 guint32 cmp_one, cmp_two;
957 cmp_one = one->src.sin6.sin6_addr.s6_addr32[i] ^
958 one->dst.sin6.sin6_addr.s6_addr32[i];
959 cmp_two = two->src.sin6.sin6_addr.s6_addr32[i] ^
960 two->dst.sin6.sin6_addr.s6_addr32[i];
962 if (!cmp_two && !cmp_one)
965 if (cmp_one && !cmp_two)
967 if (cmp_two && !cmp_one)
970 /* g_bit_storage() is effectively fls() */
971 cmp_one = g_bit_storage(ntohl(cmp_one));
972 cmp_two = g_bit_storage(ntohl(cmp_two));
974 if (cmp_one == cmp_two)
977 return cmp_one - cmp_two;
982 /* Rule 10: Otherwise, leave the order unchanged */
989 static void rfc3484_sort_results(struct resolv_lookup *lookup)
993 for (i = 0; i < lookup->nr_results; i++) {
994 struct sort_result *res = &lookup->results[i];
996 res->precedence = match_gai_table(&res->dst.sa,
998 res->dst_label = match_gai_table(&res->dst.sa, gai_labels);
999 res->src_label = match_gai_table(&res->src.sa, gai_labels);
1000 res->dst_scope = addr_scope(&res->dst.sa);
1001 res->src_scope = addr_scope(&res->src.sa);
1004 qsort(lookup->results, lookup->nr_results,
1005 sizeof(struct sort_result), rfc3484_compare);