]>
git.ipfire.org Git - thirdparty/squid.git/blob - lib/radix.c
2 * Copyright (c) 1988, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)radix.c 8.4 (Berkeley) 11/2/94
37 * Routines to build and maintain radix trees for routing lookups.
43 #include <sys/param.h>
44 #define M_DONTWAIT M_NOWAIT
53 struct radix_mask
*rn_mkfreelist
;
54 struct radix_node_head
*mask_rnhead
;
55 static char *addmask_key
;
56 static char normal_chars
[] = {0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, -1};
57 static char *rn_zeros
, *rn_ones
;
59 #define rn_masktop (mask_rnhead->rnh_treetop)
61 #define Bcmp(a, b, l) (l == 0 ? 0 : bcmp((caddr_t)(a), (caddr_t)(b), (u_long)l))
63 * The data structure for the keys is a radix tree with one way
64 * branching removed. The index rn_b at an internal node n represents a bit
65 * position to be tested. The tree is arranged so that all descendants
66 * of a node n have keys whose bits all agree up to position rn_b - 1.
67 * (We say the index of n is rn_b.)
69 * There is at least one descendant which has a one bit at position rn_b,
70 * and at least one with a zero there.
72 * A route is determined by a pair of key and mask. We require that the
73 * bit-wise logical and of the key and mask to be the key.
74 * We define the index of a route to associated with the mask to be
75 * the first bit number in the mask where 0 occurs (with bit number 0
76 * representing the highest order bit).
78 * We say a mask is normal if every bit is 0, past the index of the mask.
79 * If a node n has a descendant (k, m) with index(m) == index(n) == rn_b,
80 * and m is a normal mask, then the route applies to every descendant of n.
81 * If the index(m) < rn_b, this implies the trailing last few bits of k
82 * before bit b are all 0, (and hence consequently true of every descendant
83 * of n), so the route applies to all descendants of the node as well.
85 * Similar logic shows that a non-normal mask m such that
86 * index(m) <= index(n) could potentially apply to many children of n.
87 * Thus, for each non-host route, we attach its mask to a list at an internal
88 * node as high in the tree as we can go.
90 * The present version of the code makes use of normal routes in short-
91 * circuiting an explict mask and compare operation when testing whether
92 * a key satisfies a normal route, and also in remembering the unique leaf
93 * that governs a subtree.
97 rn_search(v_arg
, head
)
99 struct radix_node
*head
;
101 register struct radix_node
*x
;
104 for (x
= head
, v
= v_arg
; x
->rn_b
>= 0;) {
105 if (x
->rn_bmask
& v
[x
->rn_off
])
114 rn_search_m(v_arg
, head
, m_arg
)
115 struct radix_node
*head
;
118 register struct radix_node
*x
;
119 register caddr_t v
= v_arg
, m
= m_arg
;
121 for (x
= head
; x
->rn_b
>= 0;) {
122 if ((x
->rn_bmask
& m
[x
->rn_off
]) &&
123 (x
->rn_bmask
& v
[x
->rn_off
]))
132 rn_refines(m_arg
, n_arg
)
135 register caddr_t m
= m_arg
, n
= n_arg
;
136 register caddr_t lim
, lim2
= lim
= n
+ *(u_char
*)n
;
137 int longer
= (*(u_char
*)n
++) - (int)(*(u_char
*)m
++);
138 int masks_are_equal
= 1;
151 if (masks_are_equal
&& (longer
< 0))
152 for (lim2
= m
- longer
; m
< lim2
; )
155 return (!masks_are_equal
);
159 rn_lookup(v_arg
, m_arg
, head
)
161 struct radix_node_head
*head
;
163 register struct radix_node
*x
;
167 if ((x
= rn_addmask(m_arg
, 1, head
->rnh_treetop
->rn_off
)) == 0)
171 x
= rn_match(v_arg
, head
);
173 while (x
&& x
->rn_mask
!= netmask
)
180 int rn_satsifies_leaf(trial
, leaf
, skip
)
182 register struct radix_node
*leaf
;
185 register char *cp
= trial
, *cp2
= leaf
->rn_key
, *cp3
= leaf
->rn_mask
;
187 int length
= min(*(u_char
*)cp
, *(u_char
*)cp2
);
192 length
= min(length
, *(u_char
*)cp3
);
193 cplim
= cp
+ length
; cp3
+= skip
; cp2
+= skip
;
194 for (cp
+= skip
; cp
< cplim
; cp
++, cp2
++, cp3
++)
195 if ((*cp
^ *cp2
) & *cp3
)
201 rn_match(v_arg
, head
)
203 struct radix_node_head
*head
;
206 register struct radix_node
*t
= head
->rnh_treetop
, *x
;
207 register caddr_t cp
= v
, cp2
;
209 struct radix_node
*saved_t
, *top
= t
;
210 int off
= t
->rn_off
, vlen
= *(u_char
*)cp
, matched_off
;
211 register int test
, b
, rn_b
;
214 * Open code rn_search(v, top) to avoid overhead of extra
217 for (; t
->rn_b
>= 0; ) {
218 if (t
->rn_bmask
& cp
[t
->rn_off
])
224 * See if we match exactly as a host destination
225 * or at least learn how many bits match, for normal mask finesse.
227 * It doesn't hurt us to limit how many bytes to check
228 * to the length of the mask, since if it matches we had a genuine
229 * match and the leaf we have is the most specific one anyway;
230 * if it didn't match with a shorter length it would fail
231 * with a long one. This wins big for class B&C netmasks which
232 * are probably the most common case...
235 vlen
= *(u_char
*)t
->rn_mask
;
236 cp
+= off
; cp2
= t
->rn_key
+ off
; cplim
= v
+ vlen
;
237 for (; cp
< cplim
; cp
++, cp2
++)
241 * This extra grot is in case we are explicitly asked
242 * to look up the default. Ugh!
244 if ((t
->rn_flags
& RNF_ROOT
) && t
->rn_dupedkey
)
248 test
= (*cp
^ *cp2
) & 0xff; /* find first bit that differs */
249 for (b
= 7; (test
>>= 1) > 0;)
251 matched_off
= cp
- v
;
252 b
+= matched_off
<< 3;
255 * If there is a host route in a duped-key chain, it will be first.
257 if ((saved_t
= t
)->rn_mask
== 0)
259 for (; t
; t
= t
->rn_dupedkey
)
261 * Even if we don't match exactly as a host,
262 * we may match if the leaf we wound up at is
265 if (t
->rn_flags
& RNF_NORMAL
) {
268 } else if (rn_satsifies_leaf(v
, t
, matched_off
))
271 /* start searching up the tree */
273 register struct radix_mask
*m
;
275 if ((m
= t
->rn_mklist
)) {
277 * If non-contiguous masks ever become important
278 * we can restore the masking and open coding of
279 * the search and satisfaction test and put the
280 * calculation of "off" back before the "do".
283 if (m
->rm_flags
& RNF_NORMAL
) {
287 off
= min(t
->rn_off
, matched_off
);
288 x
= rn_search_m(v
, t
, m
->rm_mask
);
289 while (x
&& x
->rn_mask
!= m
->rm_mask
)
291 if (x
&& rn_satsifies_leaf(v
, x
, off
))
294 } while ((m
= m
->rm_mklist
));
302 struct radix_node
*rn_clist
;
308 rn_newpair(v
, b
, nodes
)
311 struct radix_node nodes
[2];
313 register struct radix_node
*tt
= nodes
, *t
= tt
+ 1;
314 t
->rn_b
= b
; t
->rn_bmask
= 0x80 >> (b
& 7);
315 t
->rn_l
= tt
; t
->rn_off
= b
>> 3;
316 tt
->rn_b
= -1; tt
->rn_key
= (caddr_t
)v
; tt
->rn_p
= t
;
317 tt
->rn_flags
= t
->rn_flags
= RNF_ACTIVE
;
319 tt
->rn_info
= rn_nodenum
++; t
->rn_info
= rn_nodenum
++;
320 tt
->rn_twin
= t
; tt
->rn_ybro
= rn_clist
; rn_clist
= tt
;
326 rn_insert(v_arg
, head
, dupentry
, nodes
)
328 struct radix_node_head
*head
;
330 struct radix_node nodes
[2];
333 struct radix_node
*top
= head
->rnh_treetop
;
334 int head_off
= top
->rn_off
, vlen
= (int)*((u_char
*)v
);
335 register struct radix_node
*t
= rn_search(v_arg
, top
);
336 register caddr_t cp
= v
+ head_off
;
338 struct radix_node
*tt
;
340 * Find first bit at which v and t->rn_key differ
343 register caddr_t cp2
= t
->rn_key
+ head_off
;
344 register int cmp_res
;
345 caddr_t cplim
= v
+ vlen
;
354 cmp_res
= (cp
[-1] ^ cp2
[-1]) & 0xff;
355 for (b
= (cp
- v
) << 3; cmp_res
; b
--)
359 register struct radix_node
*p
, *x
= top
;
363 if (cp
[x
->rn_off
] & x
->rn_bmask
)
366 } while (b
> (unsigned) x
->rn_b
); /* x->rn_b < b && x->rn_b >= 0 */
369 fprintf(stderr
, "rn_insert: Going In:\n"); traverse(p
);
371 t
= rn_newpair(v_arg
, b
, nodes
); tt
= t
->rn_l
;
372 if ((cp
[p
->rn_off
] & p
->rn_bmask
) == 0)
376 x
->rn_p
= t
; t
->rn_p
= p
; /* frees x, p as temp vars below */
377 if ((cp
[t
->rn_off
] & t
->rn_bmask
) == 0) {
380 t
->rn_r
= tt
; t
->rn_l
= x
;
384 log(LOG_DEBUG
, "rn_insert: Coming Out:\n"), traverse(p
);
391 rn_addmask(n_arg
, search
, skip
)
395 caddr_t netmask
= (caddr_t
)n_arg
;
396 register struct radix_node
*x
;
397 register caddr_t cp
, cplim
;
398 register int b
= 0, mlen
, j
;
399 int maskduplicated
, m0
, isnormal
;
400 struct radix_node
*saved_x
;
401 static int last_zeroed
= 0;
403 if ((mlen
= *(u_char
*)netmask
) > max_keylen
)
408 return (mask_rnhead
->rnh_nodes
);
410 memcpy(addmask_key
+1,rn_ones
+ 1, skip
- 1);
411 if ((m0
= mlen
) > skip
)
412 memcpy(addmask_key
+ skip
, netmask
+ skip
, mlen
- skip
);
414 * Trim trailing zeroes.
416 for (cp
= addmask_key
+ mlen
; (cp
> addmask_key
) && cp
[-1] == 0;)
418 mlen
= cp
- addmask_key
;
420 if (m0
>= last_zeroed
)
422 return (mask_rnhead
->rnh_nodes
);
424 if (m0
< last_zeroed
)
425 memset( addmask_key
+ m0
,'\0', last_zeroed
- m0
);
426 *addmask_key
= last_zeroed
= mlen
;
427 x
= rn_search(addmask_key
, rn_masktop
);
428 if (memcmp(addmask_key
, x
->rn_key
, mlen
) != 0)
432 R_Malloc(x
, struct radix_node
*, max_keylen
+ 2 * sizeof (*x
));
433 if ((saved_x
= x
) == 0)
435 memset(x
,'\0', max_keylen
+ 2 * sizeof (*x
));
436 netmask
= cp
= (caddr_t
)(x
+ 2);
437 memcpy(cp
,addmask_key
, mlen
);
438 x
= rn_insert(cp
, mask_rnhead
, &maskduplicated
, x
);
439 if (maskduplicated
) {
440 fprintf(stderr
, "rn_addmask: mask impossibly already in tree");
445 * Calculate index of mask, and check for normalcy.
447 cplim
= netmask
+ mlen
; isnormal
= 1;
448 for (cp
= netmask
+ skip
; (cp
< cplim
) && *(u_char
*)cp
== 0xff;)
451 for (j
= 0x80; (j
& *cp
) != 0; j
>>= 1)
453 if (*cp
!= normal_chars
[b
] || cp
!= (cplim
- 1))
456 b
+= (cp
- netmask
) << 3;
459 x
->rn_flags
|= RNF_NORMAL
;
463 static int /* XXX: arbitrary ordering for non-contiguous masks */
464 rn_lexobetter(m_arg
, n_arg
)
467 register u_char
*mp
= m_arg
, *np
= n_arg
, *lim
;
470 return 1; /* not really, but need to check longer one first */
472 for (lim
= mp
+ *mp
; mp
< lim
;)
478 static struct radix_mask
*
479 rn_new_radix_mask(tt
, next
)
480 register struct radix_node
*tt
;
481 register struct radix_mask
*next
;
483 register struct radix_mask
*m
;
487 fprintf(stderr
, "Mask for route not entered\n");
490 memset(m
,'\0', sizeof *m
);
492 m
->rm_flags
= tt
->rn_flags
;
493 if (tt
->rn_flags
& RNF_NORMAL
)
496 m
->rm_mask
= tt
->rn_mask
;
503 rn_addroute(v_arg
, n_arg
, head
, treenodes
)
505 struct radix_node_head
*head
;
506 struct radix_node treenodes
[2];
508 caddr_t v
= (caddr_t
)v_arg
, netmask
= (caddr_t
)n_arg
;
509 register struct radix_node
*t
, *x
=NULL
, *tt
;
510 struct radix_node
*saved_tt
, *top
= head
->rnh_treetop
;
511 short b
= 0, b_leaf
=0;
514 struct radix_mask
*m
, **mp
;
517 * In dealing with non-contiguous masks, there may be
518 * many different routes which have the same mask.
519 * We will find it useful to have a unique pointer to
520 * the mask to speed avoiding duplicate references at
521 * nodes and possibly save time in calculating indices.
524 if ((x
= rn_addmask(netmask
, 0, top
->rn_off
)) == 0)
531 * Deal with duplicated keys: attach node to previous instance
533 saved_tt
= tt
= rn_insert(v
, head
, &keyduplicated
, treenodes
);
535 for (t
= tt
; tt
; t
= tt
, tt
= tt
->rn_dupedkey
) {
536 if (tt
->rn_mask
== netmask
)
540 ((b_leaf
< tt
->rn_b
) || /* index(netmask) > node */
541 rn_refines(netmask
, tt
->rn_mask
) ||
542 rn_lexobetter(netmask
, tt
->rn_mask
))))
546 * If the mask is not duplicated, we wouldn't
547 * find it among possible duplicate key entries
548 * anyway, so the above test doesn't hurt.
550 * We sort the masks for a duplicated key the same way as
551 * in a masklist -- most specific to least specific.
552 * This may require the unfortunate nuisance of relocating
553 * the head of the list.
555 if (tt
== saved_tt
) {
556 struct radix_node
*xx
= x
;
557 /* link in at head of list */
558 (tt
= treenodes
)->rn_dupedkey
= t
;
559 tt
->rn_flags
= t
->rn_flags
;
560 tt
->rn_p
= x
= t
->rn_p
;
561 if (x
->rn_l
== t
) x
->rn_l
= tt
; else x
->rn_r
= tt
;
562 saved_tt
= tt
; x
= xx
;
564 (tt
= treenodes
)->rn_dupedkey
= t
->rn_dupedkey
;
568 t
=tt
+1; tt
->rn_info
= rn_nodenum
++; t
->rn_info
= rn_nodenum
++;
569 tt
->rn_twin
= t
; tt
->rn_ybro
= rn_clist
; rn_clist
= tt
;
571 tt
->rn_key
= (caddr_t
) v
;
573 tt
->rn_flags
= RNF_ACTIVE
;
579 tt
->rn_mask
= netmask
;
581 tt
->rn_flags
|= x
->rn_flags
& RNF_NORMAL
;
586 b_leaf
= -1 - t
->rn_b
;
587 if (t
->rn_r
== saved_tt
) x
= t
->rn_l
; else x
= t
->rn_r
;
588 /* Promote general routes from below */
590 for (mp
= &t
->rn_mklist
; x
; x
= x
->rn_dupedkey
)
591 if (x
->rn_mask
&& (x
->rn_b
>= b_leaf
) && x
->rn_mklist
== 0) {
592 if ((*mp
= m
= rn_new_radix_mask(x
, 0)))
595 } else if (x
->rn_mklist
) {
597 * Skip over masks whose index is > that of new node
599 for (mp
= &x
->rn_mklist
; (m
= *mp
); mp
= &m
->rm_mklist
)
600 if (m
->rm_b
>= b_leaf
)
602 t
->rn_mklist
= m
; *mp
= 0;
605 /* Add new route to highest possible ancestor's list */
606 if ((netmask
== 0) || (b
> t
->rn_b
))
607 return tt
; /* can't lift at all */
612 } while (b
<= t
->rn_b
&& x
!= top
);
614 * Search through routes associated with node to
615 * insert new route according to index.
616 * Need same criteria as when sorting dupedkeys to avoid
617 * double loop on deletion.
619 for (mp
= &x
->rn_mklist
; (m
= *mp
); mp
= &m
->rm_mklist
) {
620 if (m
->rm_b
< b_leaf
)
622 if (m
->rm_b
> b_leaf
)
624 if (m
->rm_flags
& RNF_NORMAL
) {
625 mmask
= m
->rm_leaf
->rn_mask
;
626 if (tt
->rn_flags
& RNF_NORMAL
) {
628 "Non-unique normal route, mask not entered");
633 if (mmask
== netmask
) {
638 if (rn_refines(netmask
, mmask
) || rn_lexobetter(netmask
, mmask
))
641 *mp
= rn_new_radix_mask(tt
, *mp
);
646 rn_delete(v_arg
, netmask_arg
, head
)
647 void *v_arg
, *netmask_arg
;
648 struct radix_node_head
*head
;
650 register struct radix_node
*t
, *p
, *x
, *tt
;
651 struct radix_mask
*m
, *saved_m
, **mp
;
652 struct radix_node
*dupedkey
, *saved_tt
, *top
;
654 int b
, head_off
, vlen
;
657 netmask
= netmask_arg
;
658 x
= head
->rnh_treetop
;
659 tt
= rn_search(v
, x
);
660 head_off
= x
->rn_off
;
665 memcmp(v
+ head_off
, tt
->rn_key
+ head_off
, vlen
- head_off
))
668 * Delete our route from mask lists.
671 if ((x
= rn_addmask(netmask
, 1, head_off
)) == 0)
674 while (tt
->rn_mask
!= netmask
)
675 if ((tt
= tt
->rn_dupedkey
) == 0)
678 if (tt
->rn_mask
== 0 || (saved_m
= m
= tt
->rn_mklist
) == 0)
680 if (tt
->rn_flags
& RNF_NORMAL
) {
681 if (m
->rm_leaf
!= tt
|| m
->rm_refs
> 0) {
682 fprintf(stderr
, "rn_delete: inconsistent annotation\n");
683 return 0; /* dangling ref could cause disaster */
686 if (m
->rm_mask
!= tt
->rn_mask
) {
687 fprintf(stderr
, "rn_delete: inconsistent annotation\n");
690 if (--m
->rm_refs
>= 0)
696 goto on1
; /* Wasn't lifted at all */
700 } while (b
<= t
->rn_b
&& x
!= top
);
701 for (mp
= &x
->rn_mklist
; (m
= *mp
); mp
= &m
->rm_mklist
)
708 fprintf(stderr
, "rn_delete: couldn't find our annotation\n");
709 if (tt
->rn_flags
& RNF_NORMAL
)
710 return (0); /* Dangling ref to us */
714 * Eliminate us from tree
716 if (tt
->rn_flags
& RNF_ROOT
)
719 /* Get us out of the creation list */
720 for (t
= rn_clist
; t
&& t
->rn_ybro
!= tt
; t
= t
->rn_ybro
) {}
721 if (t
) t
->rn_ybro
= tt
->rn_ybro
;
724 if ((dupedkey
= saved_tt
->rn_dupedkey
)) {
725 if (tt
== saved_tt
) {
726 x
= dupedkey
; x
->rn_p
= t
;
727 if (t
->rn_l
== tt
) t
->rn_l
= x
; else t
->rn_r
= x
;
729 for (x
= p
= saved_tt
; p
&& p
->rn_dupedkey
!= tt
;)
731 if (p
) p
->rn_dupedkey
= tt
->rn_dupedkey
;
732 else fprintf(stderr
, "rn_delete: couldn't find us\n");
735 if (t
->rn_flags
& RNF_ACTIVE
) {
737 *++x
= *t
; p
= t
->rn_p
;
739 b
= t
->rn_info
; *++x
= *t
; t
->rn_info
= b
; p
= t
->rn_p
;
741 if (p
->rn_l
== t
) p
->rn_l
= x
; else p
->rn_r
= x
;
742 x
->rn_l
->rn_p
= x
; x
->rn_r
->rn_p
= x
;
746 if (t
->rn_l
== tt
) x
= t
->rn_r
; else x
= t
->rn_l
;
748 if (p
->rn_r
== t
) p
->rn_r
= x
; else p
->rn_l
= x
;
751 * Demote routes attached to us.
755 for (mp
= &x
->rn_mklist
; (m
= *mp
);)
759 /* If there are any key,mask pairs in a sibling
760 duped-key chain, some subset will appear sorted
761 in the same order attached to our mklist */
762 for (m
= t
->rn_mklist
; m
&& x
; x
= x
->rn_dupedkey
)
763 if (m
== x
->rn_mklist
) {
764 struct radix_mask
*mm
= m
->rm_mklist
;
766 if (--(m
->rm_refs
) < 0)
771 fprintf(stderr
, "%s %x at %x\n",
772 "rn_delete: Orphaned Mask", (int)m
, (int)x
);
776 * We may be holding an active internal node in the tree.
783 b
= t
->rn_info
; *t
= *x
; t
->rn_info
= b
;
785 t
->rn_l
->rn_p
= t
; t
->rn_r
->rn_p
= t
;
787 if (p
->rn_l
== x
) p
->rn_l
= t
; else p
->rn_r
= t
;
790 tt
->rn_flags
&= ~RNF_ACTIVE
;
791 tt
[1].rn_flags
&= ~RNF_ACTIVE
;
797 struct radix_node_head
*h
;
802 struct radix_node
*base
, *next
;
803 register struct radix_node
*rn
= h
->rnh_treetop
;
805 * This gets complicated because we may delete the node
806 * while applying the function f to it, so we need to calculate
807 * the successor node in advance.
809 /* First time through node, go left */
810 while (rn
->rn_b
>= 0)
814 /* If at right child go back up, otherwise, go right */
815 while (rn
->rn_p
->rn_r
== rn
&& (rn
->rn_flags
& RNF_ROOT
) == 0)
817 /* Find the next *leaf* since next node might vanish, too */
818 for (rn
= rn
->rn_p
->rn_r
; rn
->rn_b
>= 0;)
822 while ((rn
= base
)) {
823 base
= rn
->rn_dupedkey
;
824 if (!(rn
->rn_flags
& RNF_ROOT
) && (error
= (*f
)(rn
, w
)))
828 if (rn
->rn_flags
& RNF_ROOT
)
835 rn_inithead(head
, off
)
839 register struct radix_node_head
*rnh
;
840 register struct radix_node
*t
, *tt
, *ttt
;
843 R_Malloc(rnh
, struct radix_node_head
*, sizeof (*rnh
));
846 memset(rnh
, '\0', sizeof (*rnh
));
848 t
= rn_newpair(rn_zeros
, off
, rnh
->rnh_nodes
);
849 ttt
= rnh
->rnh_nodes
+ 2;
853 tt
->rn_flags
= t
->rn_flags
= RNF_ROOT
| RNF_ACTIVE
;
856 ttt
->rn_key
= rn_ones
;
857 rnh
->rnh_addaddr
= rn_addroute
;
858 rnh
->rnh_deladdr
= rn_delete
;
859 rnh
->rnh_matchaddr
= rn_match
;
860 rnh
->rnh_lookup
= rn_lookup
;
861 rnh
->rnh_walktree
= rn_walktree
;
862 rnh
->rnh_treetop
= t
;
873 for (dom
= domains
; dom
; dom
= dom
->dom_next
)
874 if (dom
->dom_maxrtkey
> max_keylen
)
875 max_keylen
= dom
->dom_maxrtkey
;
877 if (max_keylen
== 0) {
879 "rn_init: radix functions require max_keylen be set\n");
882 R_Malloc(rn_zeros
, char *, 3 * max_keylen
);
883 if (rn_zeros
== NULL
)
885 fprintf(stderr
,"rn_init failed.\n");
888 memset(rn_zeros
, '\0', 3 * max_keylen
);
889 rn_ones
= cp
= rn_zeros
+ max_keylen
;
890 addmask_key
= cplim
= rn_ones
+ max_keylen
;
893 if (rn_inithead((void **)&mask_rnhead
, 0) == 0)
895 fprintf(stderr
,"rn_init2 failed.\n");