1 /*
2 * iterator/iter_fwd.c - iterative resolver module forward zones.
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 forward zones and config settings.
41 */
42 #include "config.h"
43 #include "iterator/iter_fwd.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
52 int
fwd_cmp(const void * k1,const void * k2)53 fwd_cmp(const void* k1, const void* k2)
54 {
55 int m;
56 struct iter_forward_zone* n1 = (struct iter_forward_zone*)k1;
57 struct iter_forward_zone* n2 = (struct iter_forward_zone*)k2;
58 if(n1->dclass != n2->dclass) {
59 if(n1->dclass < n2->dclass)
60 return -1;
61 return 1;
62 }
63 return dname_lab_cmp(n1->name, n1->namelabs, n2->name, n2->namelabs,
64 &m);
65 }
66
67 struct iter_forwards*
forwards_create(void)68 forwards_create(void)
69 {
70 struct iter_forwards* fwd = (struct iter_forwards*)calloc(1,
71 sizeof(struct iter_forwards));
72 if(!fwd)
73 return NULL;
74 lock_rw_init(&fwd->lock);
75 return fwd;
76 }
77
fwd_zone_free(struct iter_forward_zone * n)78 static void fwd_zone_free(struct iter_forward_zone* n)
79 {
80 if(!n) return;
81 delegpt_free_mlc(n->dp);
82 free(n->name);
83 free(n);
84 }
85
delfwdnode(rbnode_type * n,void * ATTR_UNUSED (arg))86 static void delfwdnode(rbnode_type* n, void* ATTR_UNUSED(arg))
87 {
88 struct iter_forward_zone* node = (struct iter_forward_zone*)n;
89 fwd_zone_free(node);
90 }
91
fwd_del_tree(struct iter_forwards * fwd)92 static void fwd_del_tree(struct iter_forwards* fwd)
93 {
94 if(fwd->tree)
95 traverse_postorder(fwd->tree, &delfwdnode, NULL);
96 free(fwd->tree);
97 }
98
99 void
forwards_delete(struct iter_forwards * fwd)100 forwards_delete(struct iter_forwards* fwd)
101 {
102 if(!fwd)
103 return;
104 lock_rw_destroy(&fwd->lock);
105 fwd_del_tree(fwd);
106 free(fwd);
107 }
108
109 /** insert info into forward structure */
110 static int
forwards_insert_data(struct iter_forwards * fwd,uint16_t c,uint8_t * nm,size_t nmlen,int nmlabs,struct delegpt * dp)111 forwards_insert_data(struct iter_forwards* fwd, uint16_t c, uint8_t* nm,
112 size_t nmlen, int nmlabs, struct delegpt* dp)
113 {
114 struct iter_forward_zone* node = (struct iter_forward_zone*)malloc(
115 sizeof(struct iter_forward_zone));
116 if(!node) {
117 delegpt_free_mlc(dp);
118 return 0;
119 }
120 node->node.key = node;
121 node->dclass = c;
122 node->name = memdup(nm, nmlen);
123 if(!node->name) {
124 delegpt_free_mlc(dp);
125 free(node);
126 return 0;
127 }
128 node->namelen = nmlen;
129 node->namelabs = nmlabs;
130 node->dp = dp;
131 if(!rbtree_insert(fwd->tree, &node->node)) {
132 char buf[LDNS_MAX_DOMAINLEN];
133 dname_str(nm, buf);
134 log_err("duplicate forward zone %s ignored.", buf);
135 delegpt_free_mlc(dp);
136 free(node->name);
137 free(node);
138 }
139 return 1;
140 }
141
142 static struct iter_forward_zone*
fwd_zone_find(struct iter_forwards * fwd,uint16_t c,uint8_t * nm)143 fwd_zone_find(struct iter_forwards* fwd, uint16_t c, uint8_t* nm)
144 {
145 struct iter_forward_zone key;
146 key.node.key = &key;
147 key.dclass = c;
148 key.name = nm;
149 key.namelabs = dname_count_size_labels(nm, &key.namelen);
150 return (struct iter_forward_zone*)rbtree_search(fwd->tree, &key);
151 }
152
153 /** insert new info into forward structure given dp */
154 static int
forwards_insert(struct iter_forwards * fwd,uint16_t c,struct delegpt * dp)155 forwards_insert(struct iter_forwards* fwd, uint16_t c, struct delegpt* dp)
156 {
157 return forwards_insert_data(fwd, c, dp->name, dp->namelen,
158 dp->namelabs, dp);
159 }
160
161 /** initialise parent pointers in the tree */
162 static void
fwd_init_parents(struct iter_forwards * fwd)163 fwd_init_parents(struct iter_forwards* fwd)
164 {
165 struct iter_forward_zone* node, *prev = NULL, *p;
166 int m;
167 RBTREE_FOR(node, struct iter_forward_zone*, fwd->tree) {
168 node->parent = NULL;
169 if(!prev || prev->dclass != node->dclass) {
170 prev = node;
171 continue;
172 }
173 (void)dname_lab_cmp(prev->name, prev->namelabs, node->name,
174 node->namelabs, &m); /* we know prev is smaller */
175 /* sort order like: . com. bla.com. zwb.com. net. */
176 /* find the previous, or parent-parent-parent */
177 for(p = prev; p; p = p->parent)
178 /* looking for name with few labels, a parent */
179 if(p->namelabs <= m) {
180 /* ==: since prev matched m, this is closest*/
181 /* <: prev matches more, but is not a parent,
182 * this one is a (grand)parent */
183 node->parent = p;
184 break;
185 }
186 prev = node;
187 }
188 }
189
190 /** set zone name */
191 static struct delegpt*
read_fwds_name(struct config_stub * s)192 read_fwds_name(struct config_stub* s)
193 {
194 struct delegpt* dp;
195 uint8_t* dname;
196 size_t dname_len;
197 if(!s->name) {
198 log_err("forward zone without a name (use name \".\" to forward everything)");
199 return NULL;
200 }
201 dname = sldns_str2wire_dname(s->name, &dname_len);
202 if(!dname) {
203 log_err("cannot parse forward zone name %s", s->name);
204 return NULL;
205 }
206 if(!(dp=delegpt_create_mlc(dname))) {
207 free(dname);
208 log_err("out of memory");
209 return NULL;
210 }
211 free(dname);
212 return dp;
213 }
214
215 /** set fwd host names */
216 static int
read_fwds_host(struct config_stub * s,struct delegpt * dp)217 read_fwds_host(struct config_stub* s, struct delegpt* dp)
218 {
219 struct config_strlist* p;
220 uint8_t* dname;
221 char* tls_auth_name;
222 int port;
223 for(p = s->hosts; p; p = p->next) {
224 log_assert(p->str);
225 dname = authextstrtodname(p->str, &port, &tls_auth_name);
226 if(!dname) {
227 log_err("cannot parse forward %s server name: '%s'",
228 s->name, p->str);
229 return 0;
230 }
231 #if ! defined(HAVE_SSL_SET1_HOST) && ! defined(HAVE_X509_VERIFY_PARAM_SET1_HOST)
232 if(tls_auth_name)
233 log_err("no name verification functionality in "
234 "ssl library, ignored name for %s", p->str);
235 #endif
236 if(!delegpt_add_ns_mlc(dp, dname, 0, tls_auth_name, port)) {
237 free(dname);
238 log_err("out of memory");
239 return 0;
240 }
241 free(dname);
242 }
243 return 1;
244 }
245
246 /** set fwd server addresses */
247 static int
read_fwds_addr(struct config_stub * s,struct delegpt * dp)248 read_fwds_addr(struct config_stub* s, struct delegpt* dp)
249 {
250 struct config_strlist* p;
251 struct sockaddr_storage addr;
252 socklen_t addrlen;
253 char* tls_auth_name;
254 for(p = s->addrs; p; p = p->next) {
255 log_assert(p->str);
256 if(!authextstrtoaddr(p->str, &addr, &addrlen, &tls_auth_name)) {
257 log_err("cannot parse forward %s ip address: '%s'",
258 s->name, p->str);
259 return 0;
260 }
261 #if ! defined(HAVE_SSL_SET1_HOST) && ! defined(HAVE_X509_VERIFY_PARAM_SET1_HOST)
262 if(tls_auth_name)
263 log_err("no name verification functionality in "
264 "ssl library, ignored name for %s", p->str);
265 #endif
266 if(!delegpt_add_addr_mlc(dp, &addr, addrlen, 0, 0,
267 tls_auth_name, -1)) {
268 log_err("out of memory");
269 return 0;
270 }
271 }
272 return 1;
273 }
274
275 /** read forwards config */
276 static int
read_forwards(struct iter_forwards * fwd,struct config_file * cfg)277 read_forwards(struct iter_forwards* fwd, struct config_file* cfg)
278 {
279 struct config_stub* s;
280 for(s = cfg->forwards; s; s = s->next) {
281 struct delegpt* dp;
282 if(!(dp=read_fwds_name(s)))
283 return 0;
284 if(!read_fwds_host(s, dp) || !read_fwds_addr(s, dp)) {
285 delegpt_free_mlc(dp);
286 return 0;
287 }
288 /* set flag that parent side NS information is included.
289 * Asking a (higher up) server on the internet is not useful */
290 /* the flag is turned off for 'forward-first' so that the
291 * last resort will ask for parent-side NS record and thus
292 * fallback to the internet name servers on a failure */
293 dp->has_parent_side_NS = (uint8_t)!s->isfirst;
294 /* Do not cache if set. */
295 dp->no_cache = s->no_cache;
296 /* use SSL for queries to this forwarder */
297 dp->ssl_upstream = (uint8_t)s->ssl_upstream;
298 /* use TCP for queries to this forwarder */
299 dp->tcp_upstream = (uint8_t)s->tcp_upstream;
300 verbose(VERB_QUERY, "Forward zone server list:");
301 delegpt_log(VERB_QUERY, dp);
302 if(!forwards_insert(fwd, LDNS_RR_CLASS_IN, dp))
303 return 0;
304 }
305 return 1;
306 }
307
308 /** insert a stub hole (if necessary) for stub name */
309 static int
fwd_add_stub_hole(struct iter_forwards * fwd,uint16_t c,uint8_t * nm)310 fwd_add_stub_hole(struct iter_forwards* fwd, uint16_t c, uint8_t* nm)
311 {
312 struct iter_forward_zone key;
313 key.node.key = &key;
314 key.dclass = c;
315 key.name = nm;
316 key.namelabs = dname_count_size_labels(key.name, &key.namelen);
317 return forwards_insert_data(fwd, key.dclass, key.name,
318 key.namelen, key.namelabs, NULL);
319 }
320
321 /** make NULL entries for stubs */
322 static int
make_stub_holes(struct iter_forwards * fwd,struct config_file * cfg)323 make_stub_holes(struct iter_forwards* fwd, struct config_file* cfg)
324 {
325 struct config_stub* s;
326 uint8_t* dname;
327 size_t dname_len;
328 for(s = cfg->stubs; s; s = s->next) {
329 if(!s->name) continue;
330 dname = sldns_str2wire_dname(s->name, &dname_len);
331 if(!dname) {
332 log_err("cannot parse stub name '%s'", s->name);
333 return 0;
334 }
335 if(fwd_zone_find(fwd, LDNS_RR_CLASS_IN, dname) != NULL) {
336 /* Already a forward zone there. */
337 free(dname);
338 continue;
339 }
340 if(!fwd_add_stub_hole(fwd, LDNS_RR_CLASS_IN, dname)) {
341 free(dname);
342 log_err("out of memory");
343 return 0;
344 }
345 free(dname);
346 }
347 return 1;
348 }
349
350 /** make NULL entries for auths */
351 static int
make_auth_holes(struct iter_forwards * fwd,struct config_file * cfg)352 make_auth_holes(struct iter_forwards* fwd, struct config_file* cfg)
353 {
354 struct config_auth* a;
355 uint8_t* dname;
356 size_t dname_len;
357 for(a = cfg->auths; a; a = a->next) {
358 if(!a->name) continue;
359 dname = sldns_str2wire_dname(a->name, &dname_len);
360 if(!dname) {
361 log_err("cannot parse auth name '%s'", a->name);
362 return 0;
363 }
364 if(fwd_zone_find(fwd, LDNS_RR_CLASS_IN, dname) != NULL) {
365 /* Already a forward zone there. */
366 free(dname);
367 continue;
368 }
369 if(!fwd_add_stub_hole(fwd, LDNS_RR_CLASS_IN, dname)) {
370 free(dname);
371 log_err("out of memory");
372 return 0;
373 }
374 free(dname);
375 }
376 return 1;
377 }
378
379 int
forwards_apply_cfg(struct iter_forwards * fwd,struct config_file * cfg)380 forwards_apply_cfg(struct iter_forwards* fwd, struct config_file* cfg)
381 {
382 if(fwd->tree) {
383 lock_unprotect(&fwd->lock, fwd->tree);
384 }
385 fwd_del_tree(fwd);
386 fwd->tree = rbtree_create(fwd_cmp);
387 if(!fwd->tree)
388 return 0;
389 lock_protect(&fwd->lock, fwd->tree, sizeof(*fwd->tree));
390
391 lock_rw_wrlock(&fwd->lock);
392 /* read forward zones */
393 if(!read_forwards(fwd, cfg)) {
394 lock_rw_unlock(&fwd->lock);
395 return 0;
396 }
397 if(!make_stub_holes(fwd, cfg)) {
398 lock_rw_unlock(&fwd->lock);
399 return 0;
400 }
401 /* TODO: Now we punch holes for auth zones as well so that in
402 * iterator:forward_request() we see the configured
403 * delegation point, but code flow/naming is hard to follow.
404 * Consider having a single tree with configured
405 * delegation points for all categories
406 * (stubs, forwards, auths). */
407 if(!make_auth_holes(fwd, cfg)) {
408 lock_rw_unlock(&fwd->lock);
409 return 0;
410 }
411 fwd_init_parents(fwd);
412 lock_rw_unlock(&fwd->lock);
413 return 1;
414 }
415
416 struct delegpt*
forwards_find(struct iter_forwards * fwd,uint8_t * qname,uint16_t qclass,int nolock)417 forwards_find(struct iter_forwards* fwd, uint8_t* qname, uint16_t qclass,
418 int nolock)
419 {
420 struct iter_forward_zone* res;
421 struct iter_forward_zone key;
422 int has_dp;
423 key.node.key = &key;
424 key.dclass = qclass;
425 key.name = qname;
426 key.namelabs = dname_count_size_labels(qname, &key.namelen);
427 /* lock_() calls are macros that could be nothing, surround in {} */
428 if(!nolock) { lock_rw_rdlock(&fwd->lock); }
429 res = (struct iter_forward_zone*)rbtree_search(fwd->tree, &key);
430 has_dp = res && res->dp;
431 if(!has_dp && !nolock) { lock_rw_unlock(&fwd->lock); }
432 return has_dp?res->dp:NULL;
433 }
434
435 struct delegpt*
forwards_lookup(struct iter_forwards * fwd,uint8_t * qname,uint16_t qclass,int nolock)436 forwards_lookup(struct iter_forwards* fwd, uint8_t* qname, uint16_t qclass,
437 int nolock)
438 {
439 /* lookup the forward zone in the tree */
440 rbnode_type* res = NULL;
441 struct iter_forward_zone *result;
442 struct iter_forward_zone key;
443 int has_dp;
444 key.node.key = &key;
445 key.dclass = qclass;
446 key.name = qname;
447 key.namelabs = dname_count_size_labels(qname, &key.namelen);
448 /* lock_() calls are macros that could be nothing, surround in {} */
449 if(!nolock) { lock_rw_rdlock(&fwd->lock); }
450 if(rbtree_find_less_equal(fwd->tree, &key, &res)) {
451 /* exact */
452 result = (struct iter_forward_zone*)res;
453 } else {
454 /* smaller element (or no element) */
455 int m;
456 result = (struct iter_forward_zone*)res;
457 if(!result || result->dclass != qclass) {
458 if(!nolock) { lock_rw_unlock(&fwd->lock); }
459 return NULL;
460 }
461 /* count number of labels matched */
462 (void)dname_lab_cmp(result->name, result->namelabs, key.name,
463 key.namelabs, &m);
464 while(result) { /* go up until qname is subdomain of stub */
465 if(result->namelabs <= m)
466 break;
467 result = result->parent;
468 }
469 }
470 has_dp = result && result->dp;
471 if(!has_dp && !nolock) { lock_rw_unlock(&fwd->lock); }
472 return has_dp?result->dp:NULL;
473 }
474
475 struct delegpt*
forwards_lookup_root(struct iter_forwards * fwd,uint16_t qclass,int nolock)476 forwards_lookup_root(struct iter_forwards* fwd, uint16_t qclass, int nolock)
477 {
478 uint8_t root = 0;
479 return forwards_lookup(fwd, &root, qclass, nolock);
480 }
481
482 /* Finds next root item in forwards lookup tree.
483 * Caller needs to handle locking of the forwards structure. */
484 static int
next_root_locked(struct iter_forwards * fwd,uint16_t * dclass)485 next_root_locked(struct iter_forwards* fwd, uint16_t* dclass)
486 {
487 struct iter_forward_zone key;
488 rbnode_type* n;
489 struct iter_forward_zone* p;
490 if(*dclass == 0) {
491 /* first root item is first item in tree */
492 n = rbtree_first(fwd->tree);
493 if(n == RBTREE_NULL)
494 return 0;
495 p = (struct iter_forward_zone*)n;
496 if(dname_is_root(p->name)) {
497 *dclass = p->dclass;
498 return 1;
499 }
500 /* root not first item? search for higher items */
501 *dclass = p->dclass + 1;
502 return next_root_locked(fwd, dclass);
503 }
504 /* find class n in tree, we may get a direct hit, or if we don't
505 * this is the last item of the previous class so rbtree_next() takes
506 * us to the next root (if any) */
507 key.node.key = &key;
508 key.name = (uint8_t*)"\000";
509 key.namelen = 1;
510 key.namelabs = 0;
511 key.dclass = *dclass;
512 n = NULL;
513 if(rbtree_find_less_equal(fwd->tree, &key, &n)) {
514 /* exact */
515 return 1;
516 } else {
517 /* smaller element */
518 if(!n || n == RBTREE_NULL)
519 return 0; /* nothing found */
520 n = rbtree_next(n);
521 if(n == RBTREE_NULL)
522 return 0; /* no higher */
523 p = (struct iter_forward_zone*)n;
524 if(dname_is_root(p->name)) {
525 *dclass = p->dclass;
526 return 1;
527 }
528 /* not a root node, return next higher item */
529 *dclass = p->dclass+1;
530 return next_root_locked(fwd, dclass);
531 }
532 }
533
534 int
forwards_next_root(struct iter_forwards * fwd,uint16_t * dclass,int nolock)535 forwards_next_root(struct iter_forwards* fwd, uint16_t* dclass, int nolock)
536 {
537 int ret;
538 /* lock_() calls are macros that could be nothing, surround in {} */
539 if(!nolock) { lock_rw_rdlock(&fwd->lock); }
540 ret = next_root_locked(fwd, dclass);
541 if(!nolock) { lock_rw_unlock(&fwd->lock); }
542 return ret;
543 }
544
545 size_t
forwards_get_mem(struct iter_forwards * fwd)546 forwards_get_mem(struct iter_forwards* fwd)
547 {
548 struct iter_forward_zone* p;
549 size_t s;
550 if(!fwd)
551 return 0;
552 lock_rw_rdlock(&fwd->lock);
553 s = sizeof(*fwd) + sizeof(*fwd->tree);
554 RBTREE_FOR(p, struct iter_forward_zone*, fwd->tree) {
555 s += sizeof(*p) + p->namelen + delegpt_get_mem(p->dp);
556 }
557 lock_rw_unlock(&fwd->lock);
558 return s;
559 }
560
561 int
forwards_add_zone(struct iter_forwards * fwd,uint16_t c,struct delegpt * dp,int nolock)562 forwards_add_zone(struct iter_forwards* fwd, uint16_t c, struct delegpt* dp,
563 int nolock)
564 {
565 struct iter_forward_zone *z;
566 /* lock_() calls are macros that could be nothing, surround in {} */
567 if(!nolock) { lock_rw_wrlock(&fwd->lock); }
568 if((z=fwd_zone_find(fwd, c, dp->name)) != NULL) {
569 (void)rbtree_delete(fwd->tree, &z->node);
570 fwd_zone_free(z);
571 }
572 if(!forwards_insert(fwd, c, dp)) {
573 if(!nolock) { lock_rw_unlock(&fwd->lock); }
574 return 0;
575 }
576 fwd_init_parents(fwd);
577 if(!nolock) { lock_rw_unlock(&fwd->lock); }
578 return 1;
579 }
580
581 void
forwards_delete_zone(struct iter_forwards * fwd,uint16_t c,uint8_t * nm,int nolock)582 forwards_delete_zone(struct iter_forwards* fwd, uint16_t c, uint8_t* nm,
583 int nolock)
584 {
585 struct iter_forward_zone *z;
586 /* lock_() calls are macros that could be nothing, surround in {} */
587 if(!nolock) { lock_rw_wrlock(&fwd->lock); }
588 if(!(z=fwd_zone_find(fwd, c, nm))) {
589 if(!nolock) { lock_rw_unlock(&fwd->lock); }
590 return; /* nothing to do */
591 }
592 (void)rbtree_delete(fwd->tree, &z->node);
593 fwd_zone_free(z);
594 fwd_init_parents(fwd);
595 if(!nolock) { lock_rw_unlock(&fwd->lock); }
596 }
597
598 int
forwards_add_stub_hole(struct iter_forwards * fwd,uint16_t c,uint8_t * nm,int nolock)599 forwards_add_stub_hole(struct iter_forwards* fwd, uint16_t c, uint8_t* nm,
600 int nolock)
601 {
602 /* lock_() calls are macros that could be nothing, surround in {} */
603 if(!nolock) { lock_rw_wrlock(&fwd->lock); }
604 if(fwd_zone_find(fwd, c, nm) != NULL) {
605 if(!nolock) { lock_rw_unlock(&fwd->lock); }
606 return 1; /* already a stub zone there */
607 }
608 if(!fwd_add_stub_hole(fwd, c, nm)) {
609 if(!nolock) { lock_rw_unlock(&fwd->lock); }
610 return 0;
611 }
612 fwd_init_parents(fwd);
613 if(!nolock) { lock_rw_unlock(&fwd->lock); }
614 return 1;
615 }
616
617 void
forwards_delete_stub_hole(struct iter_forwards * fwd,uint16_t c,uint8_t * nm,int nolock)618 forwards_delete_stub_hole(struct iter_forwards* fwd, uint16_t c,
619 uint8_t* nm, int nolock)
620 {
621 struct iter_forward_zone *z;
622 /* lock_() calls are macros that could be nothing, surround in {} */
623 if(!nolock) { lock_rw_wrlock(&fwd->lock); }
624 if(!(z=fwd_zone_find(fwd, c, nm))) {
625 if(!nolock) { lock_rw_unlock(&fwd->lock); }
626 return; /* nothing to do */
627 }
628 if(z->dp != NULL) {
629 if(!nolock) { lock_rw_unlock(&fwd->lock); }
630 return; /* not a stub hole */
631 }
632 (void)rbtree_delete(fwd->tree, &z->node);
633 fwd_zone_free(z);
634 fwd_init_parents(fwd);
635 if(!nolock) { lock_rw_unlock(&fwd->lock); }
636 }
637
638 void
forwards_swap_tree(struct iter_forwards * fwd,struct iter_forwards * data)639 forwards_swap_tree(struct iter_forwards* fwd, struct iter_forwards* data)
640 {
641 rbtree_type* oldtree = fwd->tree;
642 if(oldtree) {
643 lock_unprotect(&fwd->lock, oldtree);
644 }
645 if(data->tree) {
646 lock_unprotect(&data->lock, data->tree);
647 }
648 fwd->tree = data->tree;
649 data->tree = oldtree;
650 lock_protect(&fwd->lock, fwd->tree, sizeof(*fwd->tree));
651 lock_protect(&data->lock, data->tree, sizeof(*data->tree));
652 }
653