2 * iterator/iter_hints.c - iterative resolver module stub and root hints.
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * This file contains functions to assist the iterator module.
40 * Keep track of stub and root hints, and read those from config.
43 #include "iterator/iter_hints.h"
44 #include "iterator/iter_delegpt.h"
46 #include "util/config_file.h"
47 #include "util/net_help.h"
48 #include "util/data/dname.h"
49 #include "sldns/rrdef.h"
50 #include "sldns/str2wire.h"
51 #include "sldns/wire2str.h"
56 struct iter_hints* hints = (struct iter_hints*)calloc(1,
57 sizeof(struct iter_hints));
63 static void hints_stub_free(struct iter_hints_stub* s)
66 delegpt_free_mlc(s->dp);
70 static void delhintnode(rbnode_type* n, void* ATTR_UNUSED(arg))
72 struct iter_hints_stub* node = (struct iter_hints_stub*)n;
73 hints_stub_free(node);
76 static void hints_del_tree(struct iter_hints* hints)
78 traverse_postorder(&hints->tree, &delhintnode, NULL);
82 hints_delete(struct iter_hints* hints)
86 hints_del_tree(hints);
90 /** add hint to delegation hints */
92 ah(struct delegpt* dp, const char* sv, const char* ip)
94 struct sockaddr_storage addr;
97 uint8_t* dname = sldns_str2wire_dname(sv, &dname_len);
99 log_err("could not parse %s", sv);
102 if(!delegpt_add_ns_mlc(dp, dname, 0) ||
103 !extstrtoaddr(ip, &addr, &addrlen) ||
104 !delegpt_add_target_mlc(dp, dname, dname_len,
105 &addr, addrlen, 0, 0)) {
113 /** obtain compiletime provided root hints */
114 static struct delegpt*
115 compile_time_root_prime(int do_ip4, int do_ip6)
118 ; This file is made available by InterNIC
119 ; under anonymous FTP as
120 ; file /domain/named.cache
121 ; on server FTP.INTERNIC.NET
122 ; -OR- RS.INTERNIC.NET
124 ; related version of root zone: changes-on-20120103
126 struct delegpt* dp = delegpt_create_mlc((uint8_t*)"\000");
129 dp->has_parent_side_NS = 1;
131 if(!ah(dp, "A.ROOT-SERVERS.NET.", "198.41.0.4")) goto failed;
132 if(!ah(dp, "B.ROOT-SERVERS.NET.", "192.228.79.201")) goto failed;
133 if(!ah(dp, "C.ROOT-SERVERS.NET.", "192.33.4.12")) goto failed;
134 if(!ah(dp, "D.ROOT-SERVERS.NET.", "199.7.91.13")) goto failed;
135 if(!ah(dp, "E.ROOT-SERVERS.NET.", "192.203.230.10")) goto failed;
136 if(!ah(dp, "F.ROOT-SERVERS.NET.", "192.5.5.241")) goto failed;
137 if(!ah(dp, "G.ROOT-SERVERS.NET.", "192.112.36.4")) goto failed;
138 if(!ah(dp, "H.ROOT-SERVERS.NET.", "198.97.190.53")) goto failed;
139 if(!ah(dp, "I.ROOT-SERVERS.NET.", "192.36.148.17")) goto failed;
140 if(!ah(dp, "J.ROOT-SERVERS.NET.", "192.58.128.30")) goto failed;
141 if(!ah(dp, "K.ROOT-SERVERS.NET.", "193.0.14.129")) goto failed;
142 if(!ah(dp, "L.ROOT-SERVERS.NET.", "199.7.83.42")) goto failed;
143 if(!ah(dp, "M.ROOT-SERVERS.NET.", "202.12.27.33")) goto failed;
146 if(!ah(dp, "A.ROOT-SERVERS.NET.", "2001:503:ba3e::2:30")) goto failed;
147 if(!ah(dp, "B.ROOT-SERVERS.NET.", "2001:500:84::b")) goto failed;
148 if(!ah(dp, "C.ROOT-SERVERS.NET.", "2001:500:2::c")) goto failed;
149 if(!ah(dp, "D.ROOT-SERVERS.NET.", "2001:500:2d::d")) goto failed;
150 if(!ah(dp, "E.ROOT-SERVERS.NET.", "2001:500:a8::e")) goto failed;
151 if(!ah(dp, "F.ROOT-SERVERS.NET.", "2001:500:2f::f")) goto failed;
152 if(!ah(dp, "G.ROOT-SERVERS.NET.", "2001:500:12::d0d")) goto failed;
153 if(!ah(dp, "H.ROOT-SERVERS.NET.", "2001:500:1::53")) goto failed;
154 if(!ah(dp, "I.ROOT-SERVERS.NET.", "2001:7fe::53")) goto failed;
155 if(!ah(dp, "J.ROOT-SERVERS.NET.", "2001:503:c27::2:30")) goto failed;
156 if(!ah(dp, "K.ROOT-SERVERS.NET.", "2001:7fd::1")) goto failed;
157 if(!ah(dp, "L.ROOT-SERVERS.NET.", "2001:500:9f::42")) goto failed;
158 if(!ah(dp, "M.ROOT-SERVERS.NET.", "2001:dc3::35")) goto failed;
162 delegpt_free_mlc(dp);
166 /** insert new hint info into hint structure */
168 hints_insert(struct iter_hints* hints, uint16_t c, struct delegpt* dp,
171 struct iter_hints_stub* node = (struct iter_hints_stub*)malloc(
172 sizeof(struct iter_hints_stub));
174 delegpt_free_mlc(dp);
178 node->noprime = (uint8_t)noprime;
179 if(!name_tree_insert(&hints->tree, &node->node, dp->name, dp->namelen,
182 dname_str(dp->name, buf);
183 log_err("second hints for zone %s ignored.", buf);
184 delegpt_free_mlc(dp);
191 static struct delegpt*
192 read_stubs_name(struct config_stub* s)
198 log_err("stub zone without a name");
201 dname = sldns_str2wire_dname(s->name, &dname_len);
203 log_err("cannot parse stub zone name %s", s->name);
206 if(!(dp=delegpt_create_mlc(dname))) {
208 log_err("out of memory");
215 /** set stub host names */
217 read_stubs_host(struct config_stub* s, struct delegpt* dp)
219 struct config_strlist* p;
222 for(p = s->hosts; p; p = p->next) {
224 dname = sldns_str2wire_dname(p->str, &dname_len);
226 log_err("cannot parse stub %s nameserver name: '%s'",
230 if(!delegpt_add_ns_mlc(dp, dname, 0)) {
232 log_err("out of memory");
240 /** set stub server addresses */
242 read_stubs_addr(struct config_stub* s, struct delegpt* dp)
244 struct config_strlist* p;
245 struct sockaddr_storage addr;
247 for(p = s->addrs; p; p = p->next) {
249 if(!extstrtoaddr(p->str, &addr, &addrlen)) {
250 log_err("cannot parse stub %s ip address: '%s'",
254 if(!delegpt_add_addr_mlc(dp, &addr, addrlen, 0, 0)) {
255 log_err("out of memory");
262 /** read stubs config */
264 read_stubs(struct iter_hints* hints, struct config_file* cfg)
266 struct config_stub* s;
268 for(s = cfg->stubs; s; s = s->next) {
269 if(!(dp=read_stubs_name(s)))
271 if(!read_stubs_host(s, dp) || !read_stubs_addr(s, dp)) {
272 delegpt_free_mlc(dp);
275 /* the flag is turned off for 'stub-first' so that the
276 * last resort will ask for parent-side NS record and thus
277 * fallback to the internet name servers on a failure */
278 dp->has_parent_side_NS = (uint8_t)!s->isfirst;
280 dp->ssl_upstream = (uint8_t)s->ssl_upstream;
281 delegpt_log(VERB_QUERY, dp);
282 if(!hints_insert(hints, LDNS_RR_CLASS_IN, dp, !s->isprime))
288 /** read root hints from file */
290 read_root_hints(struct iter_hints* hints, char* fname)
292 struct sldns_file_parse_state pstate;
294 uint8_t rr[LDNS_RR_BUF_SIZE];
295 size_t rr_len, dname_len;
297 uint16_t c = LDNS_RR_CLASS_IN;
298 FILE* f = fopen(fname, "r");
300 log_err("could not read root hints %s: %s",
301 fname, strerror(errno));
304 dp = delegpt_create_mlc(NULL);
306 log_err("out of memory reading root hints");
310 verbose(VERB_QUERY, "Reading root hints from %s", fname);
311 memset(&pstate, 0, sizeof(pstate));
313 dp->has_parent_side_NS = 1;
317 status = sldns_fp2wire_rr_buf(f, rr, &rr_len, &dname_len,
320 log_err("reading root hints %s %d:%d: %s", fname,
321 pstate.lineno, LDNS_WIREPARSE_OFFSET(status),
322 sldns_get_errorstr_parse(status));
326 continue; /* EMPTY line, TTL or ORIGIN */
327 if(sldns_wirerr_get_type(rr, rr_len, dname_len)
328 == LDNS_RR_TYPE_NS) {
329 if(!delegpt_add_ns_mlc(dp, sldns_wirerr_get_rdata(rr,
330 rr_len, dname_len), 0)) {
331 log_err("out of memory reading root hints");
334 c = sldns_wirerr_get_class(rr, rr_len, dname_len);
336 if(!delegpt_set_name_mlc(dp, rr)) {
337 log_err("out of memory.");
341 } else if(sldns_wirerr_get_type(rr, rr_len, dname_len)
342 == LDNS_RR_TYPE_A && sldns_wirerr_get_rdatalen(rr,
343 rr_len, dname_len) == INET_SIZE) {
344 struct sockaddr_in sa;
345 socklen_t len = (socklen_t)sizeof(sa);
347 sa.sin_family = AF_INET;
348 sa.sin_port = (in_port_t)htons(UNBOUND_DNS_PORT);
349 memmove(&sa.sin_addr,
350 sldns_wirerr_get_rdata(rr, rr_len, dname_len),
352 if(!delegpt_add_target_mlc(dp, rr, dname_len,
353 (struct sockaddr_storage*)&sa, len,
355 log_err("out of memory reading root hints");
358 } else if(sldns_wirerr_get_type(rr, rr_len, dname_len)
359 == LDNS_RR_TYPE_AAAA && sldns_wirerr_get_rdatalen(rr,
360 rr_len, dname_len) == INET6_SIZE) {
361 struct sockaddr_in6 sa;
362 socklen_t len = (socklen_t)sizeof(sa);
364 sa.sin6_family = AF_INET6;
365 sa.sin6_port = (in_port_t)htons(UNBOUND_DNS_PORT);
366 memmove(&sa.sin6_addr,
367 sldns_wirerr_get_rdata(rr, rr_len, dname_len),
369 if(!delegpt_add_target_mlc(dp, rr, dname_len,
370 (struct sockaddr_storage*)&sa, len,
372 log_err("out of memory reading root hints");
377 sldns_wire2str_type_buf(sldns_wirerr_get_type(rr,
378 rr_len, dname_len), buf, sizeof(buf));
379 log_warn("root hints %s:%d skipping type %s",
380 fname, pstate.lineno, buf);
385 log_warn("root hints %s: no NS content", fname);
386 delegpt_free_mlc(dp);
389 if(!hints_insert(hints, c, dp, 0)) {
392 delegpt_log(VERB_QUERY, dp);
396 delegpt_free_mlc(dp);
401 /** read root hints list */
403 read_root_hints_list(struct iter_hints* hints, struct config_file* cfg)
405 struct config_strlist* p;
406 for(p = cfg->root_hints; p; p = p->next) {
408 if(p->str && p->str[0]) {
410 if(cfg->chrootdir && cfg->chrootdir[0] &&
411 strncmp(p->str, cfg->chrootdir,
412 strlen(cfg->chrootdir)) == 0)
413 f += strlen(cfg->chrootdir);
414 if(!read_root_hints(hints, f))
422 hints_apply_cfg(struct iter_hints* hints, struct config_file* cfg)
424 hints_del_tree(hints);
425 name_tree_init(&hints->tree);
427 /* read root hints */
428 if(!read_root_hints_list(hints, cfg))
431 /* read stub hints */
432 if(!read_stubs(hints, cfg))
435 /* use fallback compiletime root hints */
436 if(!hints_lookup_root(hints, LDNS_RR_CLASS_IN)) {
437 struct delegpt* dp = compile_time_root_prime(cfg->do_ip4,
439 verbose(VERB_ALGO, "no config, using builtin root hints.");
442 if(!hints_insert(hints, LDNS_RR_CLASS_IN, dp, 0))
446 name_tree_init_parents(&hints->tree);
451 hints_lookup_root(struct iter_hints* hints, uint16_t qclass)
454 struct iter_hints_stub *stub;
455 stub = (struct iter_hints_stub*)name_tree_find(&hints->tree,
456 &rootlab, 1, 1, qclass);
462 struct iter_hints_stub*
463 hints_lookup_stub(struct iter_hints* hints, uint8_t* qname,
464 uint16_t qclass, struct delegpt* cache_dp)
468 struct iter_hints_stub *r;
470 /* first lookup the stub */
471 labs = dname_count_size_labels(qname, &len);
472 r = (struct iter_hints_stub*)name_tree_lookup(&hints->tree, qname,
476 /* If there is no cache (root prime situation) */
477 if(cache_dp == NULL) {
478 if(r->dp->namelabs != 1)
479 return r; /* no cache dp, use any non-root stub */
484 * If the stub is same as the delegation we got
485 * And has noprime set, we need to 'prime' to use this stub instead.
487 if(r->noprime && query_dname_compare(cache_dp->name, r->dp->name)==0)
488 return r; /* use this stub instead of cached dp */
491 * If our cached delegation point is above the hint, we need to prime.
493 if(dname_strict_subdomain(r->dp->name, r->dp->namelabs,
494 cache_dp->name, cache_dp->namelabs))
495 return r; /* need to prime this stub */
499 int hints_next_root(struct iter_hints* hints, uint16_t* qclass)
501 return name_tree_next_root(&hints->tree, qclass);
505 hints_get_mem(struct iter_hints* hints)
508 struct iter_hints_stub* p;
511 RBTREE_FOR(p, struct iter_hints_stub*, &hints->tree) {
512 s += sizeof(*p) + delegpt_get_mem(p->dp);
518 hints_add_stub(struct iter_hints* hints, uint16_t c, struct delegpt* dp,
521 struct iter_hints_stub *z;
522 if((z=(struct iter_hints_stub*)name_tree_find(&hints->tree,
523 dp->name, dp->namelen, dp->namelabs, c)) != NULL) {
524 (void)rbtree_delete(&hints->tree, &z->node);
527 if(!hints_insert(hints, c, dp, noprime))
529 name_tree_init_parents(&hints->tree);
534 hints_delete_stub(struct iter_hints* hints, uint16_t c, uint8_t* nm)
536 struct iter_hints_stub *z;
538 int labs = dname_count_size_labels(nm, &len);
539 if(!(z=(struct iter_hints_stub*)name_tree_find(&hints->tree,
541 return; /* nothing to do */
542 (void)rbtree_delete(&hints->tree, &z->node);
544 name_tree_init_parents(&hints->tree);