Coverage Report

Created: 2026-01-17 06:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/unbound/iterator/iter_hints.c
Line
Count
Source
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
0
    if(dname_subdomain_c(dname, dp->name)) {
235
0
      log_warn("stub-host '%s' may have a circular "
236
0
        "dependency on stub-zone '%s'",
237
0
        p->str, s->name);
238
0
    }
239
#if ! defined(HAVE_SSL_SET1_HOST) && ! defined(HAVE_X509_VERIFY_PARAM_SET1_HOST)
240
    if(tls_auth_name)
241
      log_err("no name verification functionality in "
242
        "ssl library, ignored name for %s", p->str);
243
#endif
244
0
    if(!delegpt_add_ns_mlc(dp, dname, 0, tls_auth_name, port)) {
245
0
      free(dname);
246
0
      log_err("out of memory");
247
0
      return 0;
248
0
    }
249
0
    free(dname);
250
0
  }
251
0
  return 1;
252
0
}
253
254
/** set stub server addresses */
255
static int 
256
read_stubs_addr(struct config_stub* s, struct delegpt* dp)
257
0
{
258
0
  struct config_strlist* p;
259
0
  struct sockaddr_storage addr;
260
0
  socklen_t addrlen;
261
0
  char* auth_name;
262
0
  for(p = s->addrs; p; p = p->next) {
263
0
    log_assert(p->str);
264
0
    if(!authextstrtoaddr(p->str, &addr, &addrlen, &auth_name)) {
265
0
      log_err("cannot parse stub %s ip address: '%s'", 
266
0
        s->name, p->str);
267
0
      return 0;
268
0
    }
269
#if ! defined(HAVE_SSL_SET1_HOST) && ! defined(HAVE_X509_VERIFY_PARAM_SET1_HOST)
270
    if(auth_name)
271
      log_err("no name verification functionality in "
272
        "ssl library, ignored name for %s", p->str);
273
#endif
274
0
    if(!delegpt_add_addr_mlc(dp, &addr, addrlen, 0, 0,
275
0
      auth_name, -1)) {
276
0
      log_err("out of memory");
277
0
      return 0;
278
0
    }
279
0
  }
280
0
  return 1;
281
0
}
282
283
/** read stubs config */
284
static int 
285
read_stubs(struct iter_hints* hints, struct config_file* cfg)
286
0
{
287
0
  struct config_stub* s;
288
0
  struct delegpt* dp;
289
0
  for(s = cfg->stubs; s; s = s->next) {
290
0
    if(!(dp=read_stubs_name(s)))
291
0
      return 0;
292
0
    if(!read_stubs_host(s, dp) || !read_stubs_addr(s, dp)) {
293
0
      delegpt_free_mlc(dp);
294
0
      return 0;
295
0
    }
296
    /* the flag is turned off for 'stub-first' so that the
297
     * last resort will ask for parent-side NS record and thus
298
     * fallback to the internet name servers on a failure */
299
0
    dp->has_parent_side_NS = (uint8_t)!s->isfirst;
300
    /* Do not cache if set. */
301
0
    dp->no_cache = s->no_cache;
302
    /* ssl_upstream */
303
0
    dp->ssl_upstream = (uint8_t)s->ssl_upstream;
304
    /* tcp_upstream */
305
0
    dp->tcp_upstream = (uint8_t)s->tcp_upstream;
306
0
    delegpt_log(VERB_QUERY, dp);
307
0
    if(!hints_insert(hints, LDNS_RR_CLASS_IN, dp, !s->isprime))
308
0
      return 0;
309
0
  }
310
0
  return 1;
311
0
}
312
313
/** read root hints from file */
314
static int 
315
read_root_hints(struct iter_hints* hints, char* fname)
316
0
{
317
0
  struct sldns_file_parse_state pstate;
318
0
  struct delegpt* dp;
319
0
  uint8_t rr[LDNS_RR_BUF_SIZE];
320
0
  size_t rr_len, dname_len;
321
0
  int status;
322
0
  uint16_t c = LDNS_RR_CLASS_IN;
323
0
  FILE* f = fopen(fname, "r");
324
0
  if(!f) {
325
0
    log_err("could not read root hints %s: %s",
326
0
      fname, strerror(errno));
327
0
    return 0;
328
0
  }
329
0
  dp = delegpt_create_mlc(NULL);
330
0
  if(!dp) {
331
0
    log_err("out of memory reading root hints");
332
0
    fclose(f);
333
0
    return 0;
334
0
  }
335
0
  verbose(VERB_QUERY, "Reading root hints from %s", fname);
336
0
  memset(&pstate, 0, sizeof(pstate));
337
0
  pstate.lineno = 1;
338
0
  dp->has_parent_side_NS = 1;
339
0
  while(!feof(f)) {
340
0
    rr_len = sizeof(rr);
341
0
    dname_len = 0;
342
0
    status = sldns_fp2wire_rr_buf(f, rr, &rr_len, &dname_len,
343
0
      &pstate);
344
0
    if(status != 0) {
345
0
      log_err("reading root hints %s %d:%d: %s", fname,
346
0
        pstate.lineno, LDNS_WIREPARSE_OFFSET(status),
347
0
        sldns_get_errorstr_parse(status));
348
0
      goto stop_read;
349
0
    }
350
0
    if(rr_len == 0)
351
0
      continue; /* EMPTY line, TTL or ORIGIN */
352
0
    if(sldns_wirerr_get_type(rr, rr_len, dname_len)
353
0
      == LDNS_RR_TYPE_NS) {
354
0
      if(!delegpt_add_ns_mlc(dp, sldns_wirerr_get_rdata(rr,
355
0
        rr_len, dname_len), 0, NULL, UNBOUND_DNS_PORT)) {
356
0
        log_err("out of memory reading root hints");
357
0
        goto stop_read;
358
0
      }
359
0
      c = sldns_wirerr_get_class(rr, rr_len, dname_len);
360
0
      if(!dp->name) {
361
0
        if(!delegpt_set_name_mlc(dp, rr)) {
362
0
          log_err("out of memory.");
363
0
          goto stop_read;
364
0
        }
365
0
      }
366
0
    } else if(sldns_wirerr_get_type(rr, rr_len, dname_len)
367
0
      == LDNS_RR_TYPE_A && sldns_wirerr_get_rdatalen(rr,
368
0
      rr_len, dname_len) == INET_SIZE) {
369
0
      struct sockaddr_in sa;
370
0
      socklen_t len = (socklen_t)sizeof(sa);
371
0
      memset(&sa, 0, len);
372
0
      sa.sin_family = AF_INET;
373
0
      sa.sin_port = (in_port_t)htons(UNBOUND_DNS_PORT);
374
0
      memmove(&sa.sin_addr, 
375
0
        sldns_wirerr_get_rdata(rr, rr_len, dname_len),
376
0
        INET_SIZE);
377
0
      if(!delegpt_add_target_mlc(dp, rr, dname_len,
378
0
          (struct sockaddr_storage*)&sa, len, 
379
0
          0, 0)) {
380
0
        log_err("out of memory reading root hints");
381
0
        goto stop_read;
382
0
      }
383
0
    } else if(sldns_wirerr_get_type(rr, rr_len, dname_len)
384
0
      == LDNS_RR_TYPE_AAAA && sldns_wirerr_get_rdatalen(rr,
385
0
      rr_len, dname_len) == INET6_SIZE) {
386
0
      struct sockaddr_in6 sa;
387
0
      socklen_t len = (socklen_t)sizeof(sa);
388
0
      memset(&sa, 0, len);
389
0
      sa.sin6_family = AF_INET6;
390
0
      sa.sin6_port = (in_port_t)htons(UNBOUND_DNS_PORT);
391
0
      memmove(&sa.sin6_addr, 
392
0
        sldns_wirerr_get_rdata(rr, rr_len, dname_len),
393
0
        INET6_SIZE);
394
0
      if(!delegpt_add_target_mlc(dp, rr, dname_len,
395
0
          (struct sockaddr_storage*)&sa, len,
396
0
          0, 0)) {
397
0
        log_err("out of memory reading root hints");
398
0
        goto stop_read;
399
0
      }
400
0
    } else {
401
0
      char buf[17];
402
0
      sldns_wire2str_type_buf(sldns_wirerr_get_type(rr,
403
0
        rr_len, dname_len), buf, sizeof(buf));
404
0
      log_warn("root hints %s:%d skipping type %s",
405
0
        fname, pstate.lineno, buf);
406
0
    }
407
0
  }
408
0
  fclose(f);
409
0
  if(!dp->name) {
410
0
    log_warn("root hints %s: no NS content", fname);
411
0
    delegpt_free_mlc(dp);
412
0
    return 1;
413
0
  }
414
0
  delegpt_log(VERB_QUERY, dp);
415
0
  if(!hints_insert(hints, c, dp, 0)) {
416
0
    return 0;
417
0
  }
418
0
  return 1;
419
420
0
stop_read:
421
0
  delegpt_free_mlc(dp);
422
0
  fclose(f);
423
0
  return 0;
424
0
}
425
426
/** read root hints list */
427
static int 
428
read_root_hints_list(struct iter_hints* hints, struct config_file* cfg)
429
0
{
430
0
  struct config_strlist* p;
431
0
  for(p = cfg->root_hints; p; p = p->next) {
432
0
    log_assert(p->str);
433
0
    if(p->str && p->str[0]) {
434
0
      char* f = p->str;
435
0
      if(cfg->chrootdir && cfg->chrootdir[0] &&
436
0
        strncmp(p->str, cfg->chrootdir, 
437
0
        strlen(cfg->chrootdir)) == 0)
438
0
        f += strlen(cfg->chrootdir);
439
0
      if(!read_root_hints(hints, f))
440
0
        return 0;
441
0
    }
442
0
  }
443
0
  return 1;
444
0
}
445
446
int 
447
hints_apply_cfg(struct iter_hints* hints, struct config_file* cfg)
448
0
{
449
0
  int nolock = 1;
450
0
  lock_rw_wrlock(&hints->lock);
451
0
  hints_del_tree(hints);
452
0
  name_tree_init(&hints->tree);
453
454
  /* read root hints */
455
0
  if(!read_root_hints_list(hints, cfg)) {
456
0
    lock_rw_unlock(&hints->lock);
457
0
    return 0;
458
0
  }
459
460
  /* read stub hints */
461
0
  if(!read_stubs(hints, cfg)) {
462
0
    lock_rw_unlock(&hints->lock);
463
0
    return 0;
464
0
  }
465
466
  /* use fallback compiletime root hints */
467
0
  if(!hints_find_root(hints, LDNS_RR_CLASS_IN, nolock)) {
468
0
    struct delegpt* dp = compile_time_root_prime(cfg->do_ip4,
469
0
      cfg->do_ip6);
470
0
    verbose(VERB_ALGO, "no config, using builtin root hints.");
471
0
    if(!dp) {
472
0
      lock_rw_unlock(&hints->lock);
473
0
      return 0;
474
0
    }
475
0
    if(!hints_insert(hints, LDNS_RR_CLASS_IN, dp, 0)) {
476
0
      lock_rw_unlock(&hints->lock);
477
0
      return 0;
478
0
    }
479
0
  }
480
481
0
  name_tree_init_parents(&hints->tree);
482
0
  lock_rw_unlock(&hints->lock);
483
0
  return 1;
484
0
}
485
486
struct delegpt*
487
hints_find(struct iter_hints* hints, uint8_t* qname, uint16_t qclass,
488
  int nolock)
489
0
{
490
0
  struct iter_hints_stub *stub;
491
0
  size_t len;
492
0
  int has_dp;
493
0
  int labs = dname_count_size_labels(qname, &len);
494
  /* lock_() calls are macros that could be nothing, surround in {} */
495
0
  if(!nolock) { lock_rw_rdlock(&hints->lock); }
496
0
  stub = (struct iter_hints_stub*)name_tree_find(&hints->tree,
497
0
    qname, len, labs, qclass);
498
0
  has_dp = stub && stub->dp;
499
0
  if(!has_dp && !nolock) { lock_rw_unlock(&hints->lock); }
500
0
  return has_dp?stub->dp:NULL;
501
0
}
502
503
struct delegpt*
504
hints_find_root(struct iter_hints* hints, uint16_t qclass, int nolock)
505
0
{
506
0
  uint8_t rootlab = 0;
507
0
  return hints_find(hints, &rootlab, qclass, nolock);
508
0
}
509
510
struct iter_hints_stub* 
511
hints_lookup_stub(struct iter_hints* hints, uint8_t* qname,
512
  uint16_t qclass, struct delegpt* cache_dp, int nolock)
513
0
{
514
0
  size_t len;
515
0
  int labs;
516
0
  struct iter_hints_stub *r;
517
518
  /* first lookup the stub */
519
0
  labs = dname_count_size_labels(qname, &len);
520
  /* lock_() calls are macros that could be nothing, surround in {} */
521
0
  if(!nolock) { lock_rw_rdlock(&hints->lock); }
522
0
  r = (struct iter_hints_stub*)name_tree_lookup(&hints->tree, qname,
523
0
    len, labs, qclass);
524
0
  if(!r) {
525
0
    if(!nolock) { lock_rw_unlock(&hints->lock); }
526
0
    return NULL;
527
0
  }
528
529
  /* If there is no cache (root prime situation) */
530
0
  if(cache_dp == NULL) {
531
0
    if(r->dp->namelabs != 1)
532
0
      return r; /* no cache dp, use any non-root stub */
533
0
    if(!nolock) { lock_rw_unlock(&hints->lock); }
534
0
    return NULL;
535
0
  }
536
537
  /*
538
   * If the stub is same as the delegation we got
539
   * And has noprime set, we need to 'prime' to use this stub instead.
540
   */
541
0
  if(r->noprime && query_dname_compare(cache_dp->name, r->dp->name)==0)
542
0
    return r; /* use this stub instead of cached dp */
543
  
544
  /* 
545
   * If our cached delegation point is above the hint, we need to prime.
546
   */
547
0
  if(dname_strict_subdomain(r->dp->name, r->dp->namelabs,
548
0
    cache_dp->name, cache_dp->namelabs))
549
0
    return r; /* need to prime this stub */
550
0
  if(!nolock) { lock_rw_unlock(&hints->lock); }
551
0
  return NULL;
552
0
}
553
554
int hints_next_root(struct iter_hints* hints, uint16_t* qclass, int nolock)
555
0
{
556
0
  int ret;
557
  /* lock_() calls are macros that could be nothing, surround in {} */
558
0
  if(!nolock) { lock_rw_rdlock(&hints->lock); }
559
0
  ret = name_tree_next_root(&hints->tree, qclass);
560
0
  if(!nolock) { lock_rw_unlock(&hints->lock); }
561
0
  return ret;
562
0
}
563
564
size_t 
565
hints_get_mem(struct iter_hints* hints)
566
0
{
567
0
  size_t s;
568
0
  struct iter_hints_stub* p;
569
0
  if(!hints) return 0;
570
0
  lock_rw_rdlock(&hints->lock);
571
0
  s = sizeof(*hints);
572
0
  RBTREE_FOR(p, struct iter_hints_stub*, &hints->tree) {
573
0
    s += sizeof(*p) + delegpt_get_mem(p->dp);
574
0
  }
575
0
  lock_rw_unlock(&hints->lock);
576
0
  return s;
577
0
}
578
579
int 
580
hints_add_stub(struct iter_hints* hints, uint16_t c, struct delegpt* dp,
581
  int noprime, int nolock)
582
0
{
583
0
  struct iter_hints_stub *z;
584
  /* lock_() calls are macros that could be nothing, surround in {} */
585
0
  if(!nolock) { lock_rw_wrlock(&hints->lock); }
586
0
  if((z=(struct iter_hints_stub*)name_tree_find(&hints->tree,
587
0
    dp->name, dp->namelen, dp->namelabs, c)) != NULL) {
588
0
    (void)rbtree_delete(&hints->tree, &z->node);
589
0
    hints_stub_free(z);
590
0
  }
591
0
  if(!hints_insert(hints, c, dp, noprime)) {
592
0
    if(!nolock) { lock_rw_unlock(&hints->lock); }
593
0
    return 0;
594
0
  }
595
0
  name_tree_init_parents(&hints->tree);
596
0
  if(!nolock) { lock_rw_unlock(&hints->lock); }
597
0
  return 1;
598
0
}
599
600
void 
601
hints_delete_stub(struct iter_hints* hints, uint16_t c, uint8_t* nm,
602
  int nolock)
603
0
{
604
0
  struct iter_hints_stub *z;
605
0
  size_t len;
606
0
  int labs = dname_count_size_labels(nm, &len);
607
  /* lock_() calls are macros that could be nothing, surround in {} */
608
0
  if(!nolock) { lock_rw_wrlock(&hints->lock); }
609
0
  if(!(z=(struct iter_hints_stub*)name_tree_find(&hints->tree,
610
0
    nm, len, labs, c))) {
611
0
    if(!nolock) { lock_rw_unlock(&hints->lock); }
612
0
    return; /* nothing to do */
613
0
  }
614
0
  (void)rbtree_delete(&hints->tree, &z->node);
615
0
  hints_stub_free(z);
616
0
  name_tree_init_parents(&hints->tree);
617
0
  if(!nolock) { lock_rw_unlock(&hints->lock); }
618
0
}
619
620
void
621
hints_swap_tree(struct iter_hints* hints, struct iter_hints* data)
622
0
{
623
0
  rbnode_type* oldroot = hints->tree.root;
624
0
  size_t oldcount = hints->tree.count;
625
0
  hints->tree.root = data->tree.root;
626
0
  hints->tree.count = data->tree.count;
627
0
  data->tree.root = oldroot;
628
0
  data->tree.count = oldcount;
629
0
}