Coverage Report

Created: 2025-07-11 06:09

/src/unbound/iterator/iter_hints.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * iterator/iter_hints.c - iterative resolver module stub and root hints.
3
 *
4
 * Copyright (c) 2007, NLnet Labs. All rights reserved.
5
 *
6
 * This software is open source.
7
 * 
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 
12
 * Redistributions of source code must retain the above copyright notice,
13
 * this list of conditions and the following disclaimer.
14
 * 
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.
18
 * 
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.
22
 * 
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.
34
 */
35
36
/**
37
 * \file
38
 *
39
 * This file contains functions to assist the iterator module.
40
 * Keep track of stub and root hints, and read those from config.
41
 */
42
#include "config.h"
43
#include "iterator/iter_hints.h"
44
#include "iterator/iter_delegpt.h"
45
#include "util/log.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"
52
53
struct iter_hints* 
54
hints_create(void)
55
0
{
56
0
  struct iter_hints* hints = (struct iter_hints*)calloc(1,
57
0
    sizeof(struct iter_hints));
58
0
  if(!hints)
59
0
    return NULL;
60
0
  lock_rw_init(&hints->lock);
61
0
  lock_protect(&hints->lock, &hints->tree, sizeof(hints->tree));
62
0
  return hints;
63
0
}
64
65
static void hints_stub_free(struct iter_hints_stub* s)
66
0
{
67
0
  if(!s) return;
68
0
  delegpt_free_mlc(s->dp);
69
0
  free(s);
70
0
}
71
72
static void delhintnode(rbnode_type* n, void* ATTR_UNUSED(arg))
73
0
{
74
0
  struct iter_hints_stub* node = (struct iter_hints_stub*)n;
75
0
  hints_stub_free(node);
76
0
}
77
78
static void hints_del_tree(struct iter_hints* hints)
79
0
{
80
0
  traverse_postorder(&hints->tree, &delhintnode, NULL);
81
0
}
82
83
void 
84
hints_delete(struct iter_hints* hints)
85
0
{
86
0
  if(!hints) 
87
0
    return;
88
0
  lock_rw_destroy(&hints->lock);
89
0
  hints_del_tree(hints);
90
0
  free(hints);
91
0
}
92
93
/** add hint to delegation hints */
94
static int
95
ah(struct delegpt* dp, const char* sv, const char* ip)
96
0
{
97
0
  struct sockaddr_storage addr;
98
0
  socklen_t addrlen;
99
0
  size_t dname_len;
100
0
  uint8_t* dname = sldns_str2wire_dname(sv, &dname_len);
101
0
  if(!dname) {
102
0
    log_err("could not parse %s", sv);
103
0
    return 0;
104
0
  }
105
0
  if(!delegpt_add_ns_mlc(dp, dname, 0, NULL, UNBOUND_DNS_PORT) ||
106
0
     !extstrtoaddr(ip, &addr, &addrlen, UNBOUND_DNS_PORT) ||
107
0
     !delegpt_add_target_mlc(dp, dname, dname_len,
108
0
    &addr, addrlen, 0, 0)) {
109
0
    free(dname);
110
0
    return 0;
111
0
  }
112
0
  free(dname);
113
0
  return 1;
114
0
}
115
116
/** obtain compiletime provided root hints */
117
static struct delegpt* 
118
compile_time_root_prime(int do_ip4, int do_ip6)
119
0
{
120
  /* from:
121
   ;       This file is made available by InterNIC
122
   ;       under anonymous FTP as
123
   ;           file                /domain/named.cache
124
   ;           on server           FTP.INTERNIC.NET
125
   ;       -OR-                    RS.INTERNIC.NET
126
   ;
127
   ;       related version of root zone:   changes-on-20120103
128
   */
129
0
  struct delegpt* dp = delegpt_create_mlc((uint8_t*)"\000");
130
0
  if(!dp)
131
0
    return NULL;
132
0
  dp->has_parent_side_NS = 1;
133
0
      if(do_ip4) {
134
0
  if(!ah(dp, "A.ROOT-SERVERS.NET.", "198.41.0.4")) goto failed;
135
0
  if(!ah(dp, "B.ROOT-SERVERS.NET.", "170.247.170.2")) goto failed;
136
0
  if(!ah(dp, "C.ROOT-SERVERS.NET.", "192.33.4.12")) goto failed;
137
0
  if(!ah(dp, "D.ROOT-SERVERS.NET.", "199.7.91.13")) goto failed;
138
0
  if(!ah(dp, "E.ROOT-SERVERS.NET.", "192.203.230.10")) goto failed;
139
0
  if(!ah(dp, "F.ROOT-SERVERS.NET.", "192.5.5.241")) goto failed;
140
0
  if(!ah(dp, "G.ROOT-SERVERS.NET.", "192.112.36.4")) goto failed;
141
0
  if(!ah(dp, "H.ROOT-SERVERS.NET.", "198.97.190.53")) goto failed;
142
0
  if(!ah(dp, "I.ROOT-SERVERS.NET.", "192.36.148.17")) goto failed;
143
0
  if(!ah(dp, "J.ROOT-SERVERS.NET.", "192.58.128.30")) goto failed;
144
0
  if(!ah(dp, "K.ROOT-SERVERS.NET.", "193.0.14.129")) goto failed;
145
0
  if(!ah(dp, "L.ROOT-SERVERS.NET.", "199.7.83.42")) goto failed;
146
0
  if(!ah(dp, "M.ROOT-SERVERS.NET.", "202.12.27.33")) goto failed;
147
0
      }
148
0
      if(do_ip6) {
149
0
  if(!ah(dp, "A.ROOT-SERVERS.NET.", "2001:503:ba3e::2:30")) goto failed;
150
0
  if(!ah(dp, "B.ROOT-SERVERS.NET.", "2801:1b8:10::b")) goto failed;
151
0
  if(!ah(dp, "C.ROOT-SERVERS.NET.", "2001:500:2::c")) goto failed;
152
0
  if(!ah(dp, "D.ROOT-SERVERS.NET.", "2001:500:2d::d")) goto failed;
153
0
  if(!ah(dp, "E.ROOT-SERVERS.NET.", "2001:500:a8::e")) goto failed;
154
0
  if(!ah(dp, "F.ROOT-SERVERS.NET.", "2001:500:2f::f")) goto failed;
155
0
  if(!ah(dp, "G.ROOT-SERVERS.NET.", "2001:500:12::d0d")) goto failed;
156
0
  if(!ah(dp, "H.ROOT-SERVERS.NET.", "2001:500:1::53")) goto failed;
157
0
  if(!ah(dp, "I.ROOT-SERVERS.NET.", "2001:7fe::53")) goto failed;
158
0
  if(!ah(dp, "J.ROOT-SERVERS.NET.", "2001:503:c27::2:30")) goto failed;
159
0
  if(!ah(dp, "K.ROOT-SERVERS.NET.", "2001:7fd::1")) goto failed;
160
0
  if(!ah(dp, "L.ROOT-SERVERS.NET.", "2001:500:9f::42")) goto failed;
161
0
  if(!ah(dp, "M.ROOT-SERVERS.NET.", "2001:dc3::35")) goto failed;
162
0
      }
163
0
  return dp;
164
0
failed:
165
0
  delegpt_free_mlc(dp);
166
0
  return 0;
167
0
}
168
169
/** insert new hint info into hint structure */
170
static int
171
hints_insert(struct iter_hints* hints, uint16_t c, struct delegpt* dp,
172
  int noprime)
173
0
{
174
0
  struct iter_hints_stub* node = (struct iter_hints_stub*)malloc(
175
0
    sizeof(struct iter_hints_stub));
176
0
  if(!node) {
177
0
    delegpt_free_mlc(dp);
178
0
    return 0;
179
0
  }
180
0
  node->dp = dp;
181
0
  node->noprime = (uint8_t)noprime;
182
0
  if(!name_tree_insert(&hints->tree, &node->node, dp->name, dp->namelen,
183
0
    dp->namelabs, c)) {
184
0
    char buf[LDNS_MAX_DOMAINLEN];
185
0
    dname_str(dp->name, buf);
186
0
    log_err("second hints for zone %s ignored.", buf);
187
0
    delegpt_free_mlc(dp);
188
0
    free(node);
189
0
  }
190
0
  return 1;
191
0
}
192
193
/** set stub name */
194
static struct delegpt* 
195
read_stubs_name(struct config_stub* s)
196
0
{
197
0
  struct delegpt* dp;
198
0
  size_t dname_len;
199
0
  uint8_t* dname;
200
0
  if(!s->name) {
201
0
    log_err("stub zone without a name");
202
0
    return NULL;
203
0
  }
204
0
  dname = sldns_str2wire_dname(s->name, &dname_len);
205
0
  if(!dname) {
206
0
    log_err("cannot parse stub zone name %s", s->name);
207
0
    return NULL;
208
0
  }
209
0
  if(!(dp=delegpt_create_mlc(dname))) {
210
0
    free(dname);
211
0
    log_err("out of memory");
212
0
    return NULL;
213
0
  }
214
0
  free(dname);
215
0
  return dp;
216
0
}
217
218
/** set stub host names */
219
static int
220
read_stubs_host(struct config_stub* s, struct delegpt* dp)
221
0
{
222
0
  struct config_strlist* p;
223
0
  uint8_t* dname;
224
0
  char* tls_auth_name;
225
0
  int port;
226
0
  for(p = s->hosts; p; p = p->next) {
227
0
    log_assert(p->str);
228
0
    dname = authextstrtodname(p->str, &port, &tls_auth_name);
229
0
    if(!dname) {
230
0
      log_err("cannot parse stub %s nameserver name: '%s'", 
231
0
        s->name, p->str);
232
0
      return 0;
233
0
    }
234
#if ! defined(HAVE_SSL_SET1_HOST) && ! defined(HAVE_X509_VERIFY_PARAM_SET1_HOST)
235
    if(tls_auth_name)
236
      log_err("no name verification functionality in "
237
        "ssl library, ignored name for %s", p->str);
238
#endif
239
0
    if(!delegpt_add_ns_mlc(dp, dname, 0, tls_auth_name, port)) {
240
0
      free(dname);
241
0
      log_err("out of memory");
242
0
      return 0;
243
0
    }
244
0
    free(dname);
245
0
  }
246
0
  return 1;
247
0
}
248
249
/** set stub server addresses */
250
static int 
251
read_stubs_addr(struct config_stub* s, struct delegpt* dp)
252
0
{
253
0
  struct config_strlist* p;
254
0
  struct sockaddr_storage addr;
255
0
  socklen_t addrlen;
256
0
  char* auth_name;
257
0
  for(p = s->addrs; p; p = p->next) {
258
0
    log_assert(p->str);
259
0
    if(!authextstrtoaddr(p->str, &addr, &addrlen, &auth_name)) {
260
0
      log_err("cannot parse stub %s ip address: '%s'", 
261
0
        s->name, p->str);
262
0
      return 0;
263
0
    }
264
#if ! defined(HAVE_SSL_SET1_HOST) && ! defined(HAVE_X509_VERIFY_PARAM_SET1_HOST)
265
    if(auth_name)
266
      log_err("no name verification functionality in "
267
        "ssl library, ignored name for %s", p->str);
268
#endif
269
0
    if(!delegpt_add_addr_mlc(dp, &addr, addrlen, 0, 0,
270
0
      auth_name, -1)) {
271
0
      log_err("out of memory");
272
0
      return 0;
273
0
    }
274
0
  }
275
0
  return 1;
276
0
}
277
278
/** read stubs config */
279
static int 
280
read_stubs(struct iter_hints* hints, struct config_file* cfg)
281
0
{
282
0
  struct config_stub* s;
283
0
  struct delegpt* dp;
284
0
  for(s = cfg->stubs; s; s = s->next) {
285
0
    if(!(dp=read_stubs_name(s)))
286
0
      return 0;
287
0
    if(!read_stubs_host(s, dp) || !read_stubs_addr(s, dp)) {
288
0
      delegpt_free_mlc(dp);
289
0
      return 0;
290
0
    }
291
    /* the flag is turned off for 'stub-first' so that the
292
     * last resort will ask for parent-side NS record and thus
293
     * fallback to the internet name servers on a failure */
294
0
    dp->has_parent_side_NS = (uint8_t)!s->isfirst;
295
    /* Do not cache if set. */
296
0
    dp->no_cache = s->no_cache;
297
    /* ssl_upstream */
298
0
    dp->ssl_upstream = (uint8_t)s->ssl_upstream;
299
    /* tcp_upstream */
300
0
    dp->tcp_upstream = (uint8_t)s->tcp_upstream;
301
0
    delegpt_log(VERB_QUERY, dp);
302
0
    if(!hints_insert(hints, LDNS_RR_CLASS_IN, dp, !s->isprime))
303
0
      return 0;
304
0
  }
305
0
  return 1;
306
0
}
307
308
/** read root hints from file */
309
static int 
310
read_root_hints(struct iter_hints* hints, char* fname)
311
0
{
312
0
  struct sldns_file_parse_state pstate;
313
0
  struct delegpt* dp;
314
0
  uint8_t rr[LDNS_RR_BUF_SIZE];
315
0
  size_t rr_len, dname_len;
316
0
  int status;
317
0
  uint16_t c = LDNS_RR_CLASS_IN;
318
0
  FILE* f = fopen(fname, "r");
319
0
  if(!f) {
320
0
    log_err("could not read root hints %s: %s",
321
0
      fname, strerror(errno));
322
0
    return 0;
323
0
  }
324
0
  dp = delegpt_create_mlc(NULL);
325
0
  if(!dp) {
326
0
    log_err("out of memory reading root hints");
327
0
    fclose(f);
328
0
    return 0;
329
0
  }
330
0
  verbose(VERB_QUERY, "Reading root hints from %s", fname);
331
0
  memset(&pstate, 0, sizeof(pstate));
332
0
  pstate.lineno = 1;
333
0
  dp->has_parent_side_NS = 1;
334
0
  while(!feof(f)) {
335
0
    rr_len = sizeof(rr);
336
0
    dname_len = 0;
337
0
    status = sldns_fp2wire_rr_buf(f, rr, &rr_len, &dname_len,
338
0
      &pstate);
339
0
    if(status != 0) {
340
0
      log_err("reading root hints %s %d:%d: %s", fname,
341
0
        pstate.lineno, LDNS_WIREPARSE_OFFSET(status),
342
0
        sldns_get_errorstr_parse(status));
343
0
      goto stop_read;
344
0
    }
345
0
    if(rr_len == 0)
346
0
      continue; /* EMPTY line, TTL or ORIGIN */
347
0
    if(sldns_wirerr_get_type(rr, rr_len, dname_len)
348
0
      == LDNS_RR_TYPE_NS) {
349
0
      if(!delegpt_add_ns_mlc(dp, sldns_wirerr_get_rdata(rr,
350
0
        rr_len, dname_len), 0, NULL, UNBOUND_DNS_PORT)) {
351
0
        log_err("out of memory reading root hints");
352
0
        goto stop_read;
353
0
      }
354
0
      c = sldns_wirerr_get_class(rr, rr_len, dname_len);
355
0
      if(!dp->name) {
356
0
        if(!delegpt_set_name_mlc(dp, rr)) {
357
0
          log_err("out of memory.");
358
0
          goto stop_read;
359
0
        }
360
0
      }
361
0
    } else if(sldns_wirerr_get_type(rr, rr_len, dname_len)
362
0
      == LDNS_RR_TYPE_A && sldns_wirerr_get_rdatalen(rr,
363
0
      rr_len, dname_len) == INET_SIZE) {
364
0
      struct sockaddr_in sa;
365
0
      socklen_t len = (socklen_t)sizeof(sa);
366
0
      memset(&sa, 0, len);
367
0
      sa.sin_family = AF_INET;
368
0
      sa.sin_port = (in_port_t)htons(UNBOUND_DNS_PORT);
369
0
      memmove(&sa.sin_addr, 
370
0
        sldns_wirerr_get_rdata(rr, rr_len, dname_len),
371
0
        INET_SIZE);
372
0
      if(!delegpt_add_target_mlc(dp, rr, dname_len,
373
0
          (struct sockaddr_storage*)&sa, len, 
374
0
          0, 0)) {
375
0
        log_err("out of memory reading root hints");
376
0
        goto stop_read;
377
0
      }
378
0
    } else if(sldns_wirerr_get_type(rr, rr_len, dname_len)
379
0
      == LDNS_RR_TYPE_AAAA && sldns_wirerr_get_rdatalen(rr,
380
0
      rr_len, dname_len) == INET6_SIZE) {
381
0
      struct sockaddr_in6 sa;
382
0
      socklen_t len = (socklen_t)sizeof(sa);
383
0
      memset(&sa, 0, len);
384
0
      sa.sin6_family = AF_INET6;
385
0
      sa.sin6_port = (in_port_t)htons(UNBOUND_DNS_PORT);
386
0
      memmove(&sa.sin6_addr, 
387
0
        sldns_wirerr_get_rdata(rr, rr_len, dname_len),
388
0
        INET6_SIZE);
389
0
      if(!delegpt_add_target_mlc(dp, rr, dname_len,
390
0
          (struct sockaddr_storage*)&sa, len,
391
0
          0, 0)) {
392
0
        log_err("out of memory reading root hints");
393
0
        goto stop_read;
394
0
      }
395
0
    } else {
396
0
      char buf[17];
397
0
      sldns_wire2str_type_buf(sldns_wirerr_get_type(rr,
398
0
        rr_len, dname_len), buf, sizeof(buf));
399
0
      log_warn("root hints %s:%d skipping type %s",
400
0
        fname, pstate.lineno, buf);
401
0
    }
402
0
  }
403
0
  fclose(f);
404
0
  if(!dp->name) {
405
0
    log_warn("root hints %s: no NS content", fname);
406
0
    delegpt_free_mlc(dp);
407
0
    return 1;
408
0
  }
409
0
  delegpt_log(VERB_QUERY, dp);
410
0
  if(!hints_insert(hints, c, dp, 0)) {
411
0
    return 0;
412
0
  }
413
0
  return 1;
414
415
0
stop_read:
416
0
  delegpt_free_mlc(dp);
417
0
  fclose(f);
418
0
  return 0;
419
0
}
420
421
/** read root hints list */
422
static int 
423
read_root_hints_list(struct iter_hints* hints, struct config_file* cfg)
424
0
{
425
0
  struct config_strlist* p;
426
0
  for(p = cfg->root_hints; p; p = p->next) {
427
0
    log_assert(p->str);
428
0
    if(p->str && p->str[0]) {
429
0
      char* f = p->str;
430
0
      if(cfg->chrootdir && cfg->chrootdir[0] &&
431
0
        strncmp(p->str, cfg->chrootdir, 
432
0
        strlen(cfg->chrootdir)) == 0)
433
0
        f += strlen(cfg->chrootdir);
434
0
      if(!read_root_hints(hints, f))
435
0
        return 0;
436
0
    }
437
0
  }
438
0
  return 1;
439
0
}
440
441
int 
442
hints_apply_cfg(struct iter_hints* hints, struct config_file* cfg)
443
0
{
444
0
  int nolock = 1;
445
0
  lock_rw_wrlock(&hints->lock);
446
0
  hints_del_tree(hints);
447
0
  name_tree_init(&hints->tree);
448
449
  /* read root hints */
450
0
  if(!read_root_hints_list(hints, cfg)) {
451
0
    lock_rw_unlock(&hints->lock);
452
0
    return 0;
453
0
  }
454
455
  /* read stub hints */
456
0
  if(!read_stubs(hints, cfg)) {
457
0
    lock_rw_unlock(&hints->lock);
458
0
    return 0;
459
0
  }
460
461
  /* use fallback compiletime root hints */
462
0
  if(!hints_find_root(hints, LDNS_RR_CLASS_IN, nolock)) {
463
0
    struct delegpt* dp = compile_time_root_prime(cfg->do_ip4,
464
0
      cfg->do_ip6);
465
0
    verbose(VERB_ALGO, "no config, using builtin root hints.");
466
0
    if(!dp) {
467
0
      lock_rw_unlock(&hints->lock);
468
0
      return 0;
469
0
    }
470
0
    if(!hints_insert(hints, LDNS_RR_CLASS_IN, dp, 0)) {
471
0
      lock_rw_unlock(&hints->lock);
472
0
      return 0;
473
0
    }
474
0
  }
475
476
0
  name_tree_init_parents(&hints->tree);
477
0
  lock_rw_unlock(&hints->lock);
478
0
  return 1;
479
0
}
480
481
struct delegpt*
482
hints_find(struct iter_hints* hints, uint8_t* qname, uint16_t qclass,
483
  int nolock)
484
0
{
485
0
  struct iter_hints_stub *stub;
486
0
  size_t len;
487
0
  int has_dp;
488
0
  int labs = dname_count_size_labels(qname, &len);
489
  /* lock_() calls are macros that could be nothing, surround in {} */
490
0
  if(!nolock) { lock_rw_rdlock(&hints->lock); }
491
0
  stub = (struct iter_hints_stub*)name_tree_find(&hints->tree,
492
0
    qname, len, labs, qclass);
493
0
  has_dp = stub && stub->dp;
494
0
  if(!has_dp && !nolock) { lock_rw_unlock(&hints->lock); }
495
0
  return has_dp?stub->dp:NULL;
496
0
}
497
498
struct delegpt*
499
hints_find_root(struct iter_hints* hints, uint16_t qclass, int nolock)
500
0
{
501
0
  uint8_t rootlab = 0;
502
0
  return hints_find(hints, &rootlab, qclass, nolock);
503
0
}
504
505
struct iter_hints_stub* 
506
hints_lookup_stub(struct iter_hints* hints, uint8_t* qname,
507
  uint16_t qclass, struct delegpt* cache_dp, int nolock)
508
0
{
509
0
  size_t len;
510
0
  int labs;
511
0
  struct iter_hints_stub *r;
512
513
  /* first lookup the stub */
514
0
  labs = dname_count_size_labels(qname, &len);
515
  /* lock_() calls are macros that could be nothing, surround in {} */
516
0
  if(!nolock) { lock_rw_rdlock(&hints->lock); }
517
0
  r = (struct iter_hints_stub*)name_tree_lookup(&hints->tree, qname,
518
0
    len, labs, qclass);
519
0
  if(!r) {
520
0
    if(!nolock) { lock_rw_unlock(&hints->lock); }
521
0
    return NULL;
522
0
  }
523
524
  /* If there is no cache (root prime situation) */
525
0
  if(cache_dp == NULL) {
526
0
    if(r->dp->namelabs != 1)
527
0
      return r; /* no cache dp, use any non-root stub */
528
0
    if(!nolock) { lock_rw_unlock(&hints->lock); }
529
0
    return NULL;
530
0
  }
531
532
  /*
533
   * If the stub is same as the delegation we got
534
   * And has noprime set, we need to 'prime' to use this stub instead.
535
   */
536
0
  if(r->noprime && query_dname_compare(cache_dp->name, r->dp->name)==0)
537
0
    return r; /* use this stub instead of cached dp */
538
  
539
  /* 
540
   * If our cached delegation point is above the hint, we need to prime.
541
   */
542
0
  if(dname_strict_subdomain(r->dp->name, r->dp->namelabs,
543
0
    cache_dp->name, cache_dp->namelabs))
544
0
    return r; /* need to prime this stub */
545
0
  if(!nolock) { lock_rw_unlock(&hints->lock); }
546
0
  return NULL;
547
0
}
548
549
int hints_next_root(struct iter_hints* hints, uint16_t* qclass, int nolock)
550
0
{
551
0
  int ret;
552
  /* lock_() calls are macros that could be nothing, surround in {} */
553
0
  if(!nolock) { lock_rw_rdlock(&hints->lock); }
554
0
  ret = name_tree_next_root(&hints->tree, qclass);
555
0
  if(!nolock) { lock_rw_unlock(&hints->lock); }
556
0
  return ret;
557
0
}
558
559
size_t 
560
hints_get_mem(struct iter_hints* hints)
561
0
{
562
0
  size_t s;
563
0
  struct iter_hints_stub* p;
564
0
  if(!hints) return 0;
565
0
  lock_rw_rdlock(&hints->lock);
566
0
  s = sizeof(*hints);
567
0
  RBTREE_FOR(p, struct iter_hints_stub*, &hints->tree) {
568
0
    s += sizeof(*p) + delegpt_get_mem(p->dp);
569
0
  }
570
0
  lock_rw_unlock(&hints->lock);
571
0
  return s;
572
0
}
573
574
int 
575
hints_add_stub(struct iter_hints* hints, uint16_t c, struct delegpt* dp,
576
  int noprime, int nolock)
577
0
{
578
0
  struct iter_hints_stub *z;
579
  /* lock_() calls are macros that could be nothing, surround in {} */
580
0
  if(!nolock) { lock_rw_wrlock(&hints->lock); }
581
0
  if((z=(struct iter_hints_stub*)name_tree_find(&hints->tree,
582
0
    dp->name, dp->namelen, dp->namelabs, c)) != NULL) {
583
0
    (void)rbtree_delete(&hints->tree, &z->node);
584
0
    hints_stub_free(z);
585
0
  }
586
0
  if(!hints_insert(hints, c, dp, noprime)) {
587
0
    if(!nolock) { lock_rw_unlock(&hints->lock); }
588
0
    return 0;
589
0
  }
590
0
  name_tree_init_parents(&hints->tree);
591
0
  if(!nolock) { lock_rw_unlock(&hints->lock); }
592
0
  return 1;
593
0
}
594
595
void 
596
hints_delete_stub(struct iter_hints* hints, uint16_t c, uint8_t* nm,
597
  int nolock)
598
0
{
599
0
  struct iter_hints_stub *z;
600
0
  size_t len;
601
0
  int labs = dname_count_size_labels(nm, &len);
602
  /* lock_() calls are macros that could be nothing, surround in {} */
603
0
  if(!nolock) { lock_rw_wrlock(&hints->lock); }
604
0
  if(!(z=(struct iter_hints_stub*)name_tree_find(&hints->tree,
605
0
    nm, len, labs, c))) {
606
0
    if(!nolock) { lock_rw_unlock(&hints->lock); }
607
0
    return; /* nothing to do */
608
0
  }
609
0
  (void)rbtree_delete(&hints->tree, &z->node);
610
0
  hints_stub_free(z);
611
0
  name_tree_init_parents(&hints->tree);
612
0
  if(!nolock) { lock_rw_unlock(&hints->lock); }
613
0
}
614
615
void
616
hints_swap_tree(struct iter_hints* hints, struct iter_hints* data)
617
0
{
618
0
  rbnode_type* oldroot = hints->tree.root;
619
0
  size_t oldcount = hints->tree.count;
620
0
  hints->tree.root = data->tree.root;
621
0
  hints->tree.count = data->tree.count;
622
0
  data->tree.root = oldroot;
623
0
  data->tree.count = oldcount;
624
0
}