@@ -359,39 +359,50 @@ forwards_apply_cfg(struct iter_forwards* fwd, struct config_file* cfg)
359
359
}
360
360
361
361
struct delegpt *
362
- forwards_find (struct iter_forwards * fwd , uint8_t * qname , uint16_t qclass )
362
+ forwards_find (struct iter_forwards * fwd , uint8_t * qname , uint16_t qclass ,
363
+ int nolock )
363
364
{
364
- rbnode_type * res = NULL ;
365
+ struct iter_forward_zone * res ;
365
366
struct iter_forward_zone key ;
367
+ int has_dp ;
366
368
key .node .key = & key ;
367
369
key .dclass = qclass ;
368
370
key .name = qname ;
369
371
key .namelabs = dname_count_size_labels (qname , & key .namelen );
370
- res = rbtree_search (fwd -> tree , & key );
371
- if (res ) return ((struct iter_forward_zone * )res )-> dp ;
372
- return NULL ;
372
+ /* lock_() calls are macros that could be nothing, surround in {} */
373
+ if (!nolock ) { lock_rw_rdlock (& fwd -> lock ); }
374
+ res = (struct iter_forward_zone * )rbtree_search (fwd -> tree , & key );
375
+ has_dp = res && res -> dp ;
376
+ if (!has_dp && !nolock ) { lock_rw_unlock (& fwd -> lock ); }
377
+ return has_dp ?res -> dp :NULL ;
373
378
}
374
379
375
380
struct delegpt *
376
- forwards_lookup (struct iter_forwards * fwd , uint8_t * qname , uint16_t qclass )
381
+ forwards_lookup (struct iter_forwards * fwd , uint8_t * qname , uint16_t qclass ,
382
+ int nolock )
377
383
{
378
384
/* lookup the forward zone in the tree */
379
385
rbnode_type * res = NULL ;
380
386
struct iter_forward_zone * result ;
381
387
struct iter_forward_zone key ;
388
+ int has_dp ;
382
389
key .node .key = & key ;
383
390
key .dclass = qclass ;
384
391
key .name = qname ;
385
392
key .namelabs = dname_count_size_labels (qname , & key .namelen );
393
+ /* lock_() calls are macros that could be nothing, surround in {} */
394
+ if (!nolock ) { lock_rw_rdlock (& fwd -> lock ); }
386
395
if (rbtree_find_less_equal (fwd -> tree , & key , & res )) {
387
396
/* exact */
388
397
result = (struct iter_forward_zone * )res ;
389
398
} else {
390
399
/* smaller element (or no element) */
391
400
int m ;
392
401
result = (struct iter_forward_zone * )res ;
393
- if (!result || result -> dclass != qclass )
402
+ if (!result || result -> dclass != qclass ) {
403
+ if (!nolock ) { lock_rw_unlock (& fwd -> lock ); }
394
404
return NULL ;
405
+ }
395
406
/* count number of labels matched */
396
407
(void )dname_lab_cmp (result -> name , result -> namelabs , key .name ,
397
408
key .namelabs , & m );
@@ -401,20 +412,22 @@ forwards_lookup(struct iter_forwards* fwd, uint8_t* qname, uint16_t qclass)
401
412
result = result -> parent ;
402
413
}
403
414
}
404
- if ( result )
405
- return result -> dp ;
406
- return NULL ;
415
+ has_dp = result && result -> dp ;
416
+ if (! has_dp && ! nolock ) { lock_rw_unlock ( & fwd -> lock ); }
417
+ return has_dp ? result -> dp : NULL ;
407
418
}
408
419
409
420
struct delegpt *
410
- forwards_lookup_root (struct iter_forwards * fwd , uint16_t qclass )
421
+ forwards_lookup_root (struct iter_forwards * fwd , uint16_t qclass , int nolock )
411
422
{
412
423
uint8_t root = 0 ;
413
- return forwards_lookup (fwd , & root , qclass );
424
+ return forwards_lookup (fwd , & root , qclass , nolock );
414
425
}
415
426
416
- int
417
- forwards_next_root (struct iter_forwards * fwd , uint16_t * dclass )
427
+ /* Finds next root item in forwards lookup tree.
428
+ * Caller needs to handle locking of the forwards structure. */
429
+ static int
430
+ next_root_locked (struct iter_forwards * fwd , uint16_t * dclass )
418
431
{
419
432
struct iter_forward_zone key ;
420
433
rbnode_type * n ;
@@ -431,7 +444,7 @@ forwards_next_root(struct iter_forwards* fwd, uint16_t* dclass)
431
444
}
432
445
/* root not first item? search for higher items */
433
446
* dclass = p -> dclass + 1 ;
434
- return forwards_next_root (fwd , dclass );
447
+ return next_root_locked (fwd , dclass );
435
448
}
436
449
/* find class n in tree, we may get a direct hit, or if we don't
437
450
* this is the last item of the previous class so rbtree_next() takes
@@ -459,10 +472,21 @@ forwards_next_root(struct iter_forwards* fwd, uint16_t* dclass)
459
472
}
460
473
/* not a root node, return next higher item */
461
474
* dclass = p -> dclass + 1 ;
462
- return forwards_next_root (fwd , dclass );
475
+ return next_root_locked (fwd , dclass );
463
476
}
464
477
}
465
478
479
+ int
480
+ forwards_next_root (struct iter_forwards * fwd , uint16_t * dclass , int nolock )
481
+ {
482
+ int ret ;
483
+ /* lock_() calls are macros that could be nothing, surround in {} */
484
+ if (!nolock ) { lock_rw_rdlock (& fwd -> lock ); }
485
+ ret = next_root_locked (fwd , dclass );
486
+ if (!nolock ) { lock_rw_unlock (& fwd -> lock ); }
487
+ return ret ;
488
+ }
489
+
466
490
size_t
467
491
forwards_get_mem (struct iter_forwards * fwd )
468
492
{
@@ -491,53 +515,80 @@ fwd_zone_find(struct iter_forwards* fwd, uint16_t c, uint8_t* nm)
491
515
}
492
516
493
517
int
494
- forwards_add_zone (struct iter_forwards * fwd , uint16_t c , struct delegpt * dp )
518
+ forwards_add_zone (struct iter_forwards * fwd , uint16_t c , struct delegpt * dp ,
519
+ int nolock )
495
520
{
496
521
struct iter_forward_zone * z ;
522
+ /* lock_() calls are macros that could be nothing, surround in {} */
523
+ if (!nolock ) { lock_rw_wrlock (& fwd -> lock ); }
497
524
if ((z = fwd_zone_find (fwd , c , dp -> name )) != NULL ) {
498
525
(void )rbtree_delete (fwd -> tree , & z -> node );
499
526
fwd_zone_free (z );
500
527
}
501
- if (!forwards_insert (fwd , c , dp ))
528
+ if (!forwards_insert (fwd , c , dp )) {
529
+ if (!nolock ) { lock_rw_unlock (& fwd -> lock ); }
502
530
return 0 ;
531
+ }
503
532
fwd_init_parents (fwd );
533
+ if (!nolock ) { lock_rw_unlock (& fwd -> lock ); }
504
534
return 1 ;
505
535
}
506
536
507
537
void
508
- forwards_delete_zone (struct iter_forwards * fwd , uint16_t c , uint8_t * nm )
538
+ forwards_delete_zone (struct iter_forwards * fwd , uint16_t c , uint8_t * nm ,
539
+ int nolock )
509
540
{
510
541
struct iter_forward_zone * z ;
511
- if (!(z = fwd_zone_find (fwd , c , nm )))
542
+ /* lock_() calls are macros that could be nothing, surround in {} */
543
+ if (!nolock ) { lock_rw_wrlock (& fwd -> lock ); }
544
+ if (!(z = fwd_zone_find (fwd , c , nm ))) {
545
+ if (!nolock ) { lock_rw_unlock (& fwd -> lock ); }
512
546
return ; /* nothing to do */
547
+ }
513
548
(void )rbtree_delete (fwd -> tree , & z -> node );
514
549
fwd_zone_free (z );
515
550
fwd_init_parents (fwd );
551
+ if (!nolock ) { lock_rw_unlock (& fwd -> lock ); }
516
552
}
517
553
518
554
int
519
- forwards_add_stub_hole (struct iter_forwards * fwd , uint16_t c , uint8_t * nm )
555
+ forwards_add_stub_hole (struct iter_forwards * fwd , uint16_t c , uint8_t * nm ,
556
+ int nolock )
520
557
{
521
- if (fwd_zone_find (fwd , c , nm ) != NULL )
558
+ /* lock_() calls are macros that could be nothing, surround in {} */
559
+ if (!nolock ) { lock_rw_wrlock (& fwd -> lock ); }
560
+ if (fwd_zone_find (fwd , c , nm ) != NULL ) {
561
+ if (!nolock ) { lock_rw_unlock (& fwd -> lock ); }
522
562
return 1 ; /* already a stub zone there */
563
+ }
523
564
if (!fwd_add_stub_hole (fwd , c , nm )) {
565
+ if (!nolock ) { lock_rw_unlock (& fwd -> lock ); }
524
566
return 0 ;
525
567
}
526
568
fwd_init_parents (fwd );
569
+ if (!nolock ) { lock_rw_unlock (& fwd -> lock ); }
527
570
return 1 ;
528
571
}
529
572
530
573
void
531
- forwards_delete_stub_hole (struct iter_forwards * fwd , uint16_t c , uint8_t * nm )
574
+ forwards_delete_stub_hole (struct iter_forwards * fwd , uint16_t c ,
575
+ uint8_t * nm , int nolock )
532
576
{
533
577
struct iter_forward_zone * z ;
534
- if (!(z = fwd_zone_find (fwd , c , nm )))
578
+ /* lock_() calls are macros that could be nothing, surround in {} */
579
+ if (!nolock ) { lock_rw_wrlock (& fwd -> lock ); }
580
+ if (!(z = fwd_zone_find (fwd , c , nm ))) {
581
+ if (!nolock ) { lock_rw_unlock (& fwd -> lock ); }
535
582
return ; /* nothing to do */
536
- if (z -> dp != NULL )
583
+ }
584
+ if (z -> dp != NULL ) {
585
+ if (!nolock ) { lock_rw_unlock (& fwd -> lock ); }
537
586
return ; /* not a stub hole */
587
+ }
538
588
(void )rbtree_delete (fwd -> tree , & z -> node );
539
589
fwd_zone_free (z );
540
590
fwd_init_parents (fwd );
591
+ if (!nolock ) { lock_rw_unlock (& fwd -> lock ); }
541
592
}
542
593
543
594
void
0 commit comments