]>
git.saurik.com Git - apple/xnu.git/blob - bsd/net/radix.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1988, 1989, 1993
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * @(#)radix.c 8.4 (Berkeley) 11/2/94
58 * Routines to build and maintain radix trees for routing lookups.
61 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/malloc.h>
65 #define M_DONTWAIT M_NOWAIT
66 #include <sys/domain.h>
70 #include <sys/syslog.h>
71 #include <net/radix.h>
74 static int rn_walktree_from
__P((struct radix_node_head
*h
, void *a
,
75 void *m
, walktree_f_t
*f
, void *w
));
76 static int rn_walktree
__P((struct radix_node_head
*, walktree_f_t
*, void *));
77 static struct radix_node
78 *rn_insert
__P((void *, struct radix_node_head
*, int *,
79 struct radix_node
[2])),
80 *rn_newpair
__P((void *, int, struct radix_node
[2])),
81 *rn_search
__P((void *, struct radix_node
*)),
82 *rn_search_m
__P((void *, struct radix_node
*, void *));
84 static int max_keylen
;
85 static struct radix_mask
*rn_mkfreelist
;
86 static struct radix_node_head
*mask_rnhead
;
87 static char *addmask_key
;
88 static char normal_chars
[] = {0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, -1};
89 static char *rn_zeros
, *rn_ones
;
91 #define rn_masktop (mask_rnhead->rnh_treetop)
93 #define Bcmp(a, b, l) (l == 0 ? 0 : bcmp((caddr_t)(a), (caddr_t)(b), (u_long)l))
95 static int rn_lexobetter
__P((void *m_arg
, void *n_arg
));
96 static struct radix_mask
*
97 rn_new_radix_mask
__P((struct radix_node
*tt
,
98 struct radix_mask
*next
));
99 static int rn_satsifies_leaf
__P((char *trial
, struct radix_node
*leaf
,
103 * The data structure for the keys is a radix tree with one way
104 * branching removed. The index rn_b at an internal node n represents a bit
105 * position to be tested. The tree is arranged so that all descendants
106 * of a node n have keys whose bits all agree up to position rn_b - 1.
107 * (We say the index of n is rn_b.)
109 * There is at least one descendant which has a one bit at position rn_b,
110 * and at least one with a zero there.
112 * A route is determined by a pair of key and mask. We require that the
113 * bit-wise logical and of the key and mask to be the key.
114 * We define the index of a route to associated with the mask to be
115 * the first bit number in the mask where 0 occurs (with bit number 0
116 * representing the highest order bit).
118 * We say a mask is normal if every bit is 0, past the index of the mask.
119 * If a node n has a descendant (k, m) with index(m) == index(n) == rn_b,
120 * and m is a normal mask, then the route applies to every descendant of n.
121 * If the index(m) < rn_b, this implies the trailing last few bits of k
122 * before bit b are all 0, (and hence consequently true of every descendant
123 * of n), so the route applies to all descendants of the node as well.
125 * Similar logic shows that a non-normal mask m such that
126 * index(m) <= index(n) could potentially apply to many children of n.
127 * Thus, for each non-host route, we attach its mask to a list at an internal
128 * node as high in the tree as we can go.
130 * The present version of the code makes use of normal routes in short-
131 * circuiting an explict mask and compare operation when testing whether
132 * a key satisfies a normal route, and also in remembering the unique leaf
133 * that governs a subtree.
136 static struct radix_node
*
137 rn_search(v_arg
, head
)
139 struct radix_node
*head
;
141 register struct radix_node
*x
;
144 for (x
= head
, v
= v_arg
; x
->rn_b
>= 0;) {
145 if (x
->rn_bmask
& v
[x
->rn_off
])
153 static struct radix_node
*
154 rn_search_m(v_arg
, head
, m_arg
)
155 struct radix_node
*head
;
158 register struct radix_node
*x
;
159 register caddr_t v
= v_arg
, m
= m_arg
;
161 for (x
= head
; x
->rn_b
>= 0;) {
162 if ((x
->rn_bmask
& m
[x
->rn_off
]) &&
163 (x
->rn_bmask
& v
[x
->rn_off
]))
172 rn_refines(m_arg
, n_arg
)
175 register caddr_t m
= m_arg
, n
= n_arg
;
176 register caddr_t lim
, lim2
= lim
= n
+ *(u_char
*)n
;
177 int longer
= (*(u_char
*)n
++) - (int)(*(u_char
*)m
++);
178 int masks_are_equal
= 1;
191 if (masks_are_equal
&& (longer
< 0))
192 for (lim2
= m
- longer
; m
< lim2
; )
195 return (!masks_are_equal
);
199 rn_lookup(v_arg
, m_arg
, head
)
201 struct radix_node_head
*head
;
203 register struct radix_node
*x
;
207 if ((x
= rn_addmask(m_arg
, 1, head
->rnh_treetop
->rn_off
)) == 0)
211 x
= rn_match(v_arg
, head
);
213 while (x
&& x
->rn_mask
!= netmask
)
220 rn_satsifies_leaf(trial
, leaf
, skip
)
222 register struct radix_node
*leaf
;
225 register char *cp
= trial
, *cp2
= leaf
->rn_key
, *cp3
= leaf
->rn_mask
;
227 int length
= min(*(u_char
*)cp
, *(u_char
*)cp2
);
232 length
= min(length
, *(u_char
*)cp3
);
233 cplim
= cp
+ length
; cp3
+= skip
; cp2
+= skip
;
234 for (cp
+= skip
; cp
< cplim
; cp
++, cp2
++, cp3
++)
235 if ((*cp
^ *cp2
) & *cp3
)
241 rn_match(v_arg
, head
)
243 struct radix_node_head
*head
;
246 register struct radix_node
*t
= head
->rnh_treetop
, *x
;
247 register caddr_t cp
= v
, cp2
;
249 struct radix_node
*saved_t
, *top
= t
;
250 int off
= t
->rn_off
, vlen
= *(u_char
*)cp
, matched_off
;
251 register int test
, b
, rn_b
;
254 * Open code rn_search(v, top) to avoid overhead of extra
257 for (; t
->rn_b
>= 0; ) {
258 if (t
->rn_bmask
& cp
[t
->rn_off
])
264 * See if we match exactly as a host destination
265 * or at least learn how many bits match, for normal mask finesse.
267 * It doesn't hurt us to limit how many bytes to check
268 * to the length of the mask, since if it matches we had a genuine
269 * match and the leaf we have is the most specific one anyway;
270 * if it didn't match with a shorter length it would fail
271 * with a long one. This wins big for class B&C netmasks which
272 * are probably the most common case...
275 vlen
= *(u_char
*)t
->rn_mask
;
276 cp
+= off
; cp2
= t
->rn_key
+ off
; cplim
= v
+ vlen
;
277 for (; cp
< cplim
; cp
++, cp2
++)
281 * This extra grot is in case we are explicitly asked
282 * to look up the default. Ugh!
284 if ((t
->rn_flags
& RNF_ROOT
) && t
->rn_dupedkey
)
288 test
= (*cp
^ *cp2
) & 0xff; /* find first bit that differs */
289 for (b
= 7; (test
>>= 1) > 0;)
291 matched_off
= cp
- v
;
292 b
+= matched_off
<< 3;
295 * If there is a host route in a duped-key chain, it will be first.
297 if ((saved_t
= t
)->rn_mask
== 0)
299 for (; t
; t
= t
->rn_dupedkey
)
301 * Even if we don't match exactly as a host,
302 * we may match if the leaf we wound up at is
305 if (t
->rn_flags
& RNF_NORMAL
) {
308 } else if (rn_satsifies_leaf(v
, t
, matched_off
))
311 /* start searching up the tree */
313 register struct radix_mask
*m
;
318 * If non-contiguous masks ever become important
319 * we can restore the masking and open coding of
320 * the search and satisfaction test and put the
321 * calculation of "off" back before the "do".
324 if (m
->rm_flags
& RNF_NORMAL
) {
328 off
= min(t
->rn_off
, matched_off
);
329 x
= rn_search_m(v
, t
, m
->rm_mask
);
330 while (x
&& x
->rn_mask
!= m
->rm_mask
)
332 if (x
&& rn_satsifies_leaf(v
, x
, off
))
344 struct radix_node
*rn_clist
;
349 static struct radix_node
*
350 rn_newpair(v
, b
, nodes
)
353 struct radix_node nodes
[2];
355 register struct radix_node
*tt
= nodes
, *t
= tt
+ 1;
356 t
->rn_b
= b
; t
->rn_bmask
= 0x80 >> (b
& 7);
357 t
->rn_l
= tt
; t
->rn_off
= b
>> 3;
358 tt
->rn_b
= -1; tt
->rn_key
= (caddr_t
)v
; tt
->rn_p
= t
;
359 tt
->rn_flags
= t
->rn_flags
= RNF_ACTIVE
;
361 tt
->rn_info
= rn_nodenum
++; t
->rn_info
= rn_nodenum
++;
362 tt
->rn_twin
= t
; tt
->rn_ybro
= rn_clist
; rn_clist
= tt
;
367 static struct radix_node
*
368 rn_insert(v_arg
, head
, dupentry
, nodes
)
370 struct radix_node_head
*head
;
372 struct radix_node nodes
[2];
375 struct radix_node
*top
= head
->rnh_treetop
;
376 int head_off
= top
->rn_off
, vlen
= (int)*((u_char
*)v
);
377 register struct radix_node
*t
= rn_search(v_arg
, top
);
378 register caddr_t cp
= v
+ head_off
;
380 struct radix_node
*tt
;
382 * Find first bit at which v and t->rn_key differ
385 register caddr_t cp2
= t
->rn_key
+ head_off
;
386 register int cmp_res
;
387 caddr_t cplim
= v
+ vlen
;
396 cmp_res
= (cp
[-1] ^ cp2
[-1]) & 0xff;
397 for (b
= (cp
- v
) << 3; cmp_res
; b
--)
401 register struct radix_node
*p
, *x
= top
;
405 if (cp
[x
->rn_off
] & x
->rn_bmask
)
408 } while (b
> (unsigned) x
->rn_b
); /* x->rn_b < b && x->rn_b >= 0 */
411 log(LOG_DEBUG
, "rn_insert: Going In:\n"), traverse(p
);
413 t
= rn_newpair(v_arg
, b
, nodes
); tt
= t
->rn_l
;
414 if ((cp
[p
->rn_off
] & p
->rn_bmask
) == 0)
418 x
->rn_p
= t
; t
->rn_p
= p
; /* frees x, p as temp vars below */
419 if ((cp
[t
->rn_off
] & t
->rn_bmask
) == 0) {
422 t
->rn_r
= tt
; t
->rn_l
= x
;
426 log(LOG_DEBUG
, "rn_insert: Coming Out:\n"), traverse(p
);
433 rn_addmask(n_arg
, search
, skip
)
437 caddr_t netmask
= (caddr_t
)n_arg
;
438 register struct radix_node
*x
;
439 register caddr_t cp
, cplim
;
440 register int b
= 0, mlen
, j
;
441 int maskduplicated
, m0
, isnormal
;
442 struct radix_node
*saved_x
;
443 static int last_zeroed
= 0;
445 if ((mlen
= *(u_char
*)netmask
) > max_keylen
)
450 return (mask_rnhead
->rnh_nodes
);
452 Bcopy(rn_ones
+ 1, addmask_key
+ 1, skip
- 1);
453 if ((m0
= mlen
) > skip
)
454 Bcopy(netmask
+ skip
, addmask_key
+ skip
, mlen
- skip
);
456 * Trim trailing zeroes.
458 for (cp
= addmask_key
+ mlen
; (cp
> addmask_key
) && cp
[-1] == 0;)
460 mlen
= cp
- addmask_key
;
462 if (m0
>= last_zeroed
)
464 return (mask_rnhead
->rnh_nodes
);
466 if (m0
< last_zeroed
)
467 Bzero(addmask_key
+ m0
, last_zeroed
- m0
);
468 *addmask_key
= last_zeroed
= mlen
;
469 x
= rn_search(addmask_key
, rn_masktop
);
470 if (Bcmp(addmask_key
, x
->rn_key
, mlen
) != 0)
474 R_Malloc(x
, struct radix_node
*, max_keylen
+ 2 * sizeof (*x
));
475 if ((saved_x
= x
) == 0)
477 Bzero(x
, max_keylen
+ 2 * sizeof (*x
));
478 netmask
= cp
= (caddr_t
)(x
+ 2);
479 Bcopy(addmask_key
, cp
, mlen
);
480 x
= rn_insert(cp
, mask_rnhead
, &maskduplicated
, x
);
481 if (maskduplicated
) {
482 log(LOG_ERR
, "rn_addmask: mask impossibly already in tree");
487 * Calculate index of mask, and check for normalcy.
489 cplim
= netmask
+ mlen
; isnormal
= 1;
490 for (cp
= netmask
+ skip
; (cp
< cplim
) && *(u_char
*)cp
== 0xff;)
493 for (j
= 0x80; (j
& *cp
) != 0; j
>>= 1)
495 if (*cp
!= normal_chars
[b
] || cp
!= (cplim
- 1))
498 b
+= (cp
- netmask
) << 3;
501 x
->rn_flags
|= RNF_NORMAL
;
505 static int /* XXX: arbitrary ordering for non-contiguous masks */
506 rn_lexobetter(m_arg
, n_arg
)
509 register u_char
*mp
= m_arg
, *np
= n_arg
, *lim
;
512 return 1; /* not really, but need to check longer one first */
514 for (lim
= mp
+ *mp
; mp
< lim
;)
520 static struct radix_mask
*
521 rn_new_radix_mask(tt
, next
)
522 register struct radix_node
*tt
;
523 register struct radix_mask
*next
;
525 register struct radix_mask
*m
;
529 log(LOG_ERR
, "Mask for route not entered\n");
534 m
->rm_flags
= tt
->rn_flags
;
535 if (tt
->rn_flags
& RNF_NORMAL
)
538 m
->rm_mask
= tt
->rn_mask
;
545 rn_addroute(v_arg
, n_arg
, head
, treenodes
)
547 struct radix_node_head
*head
;
548 struct radix_node treenodes
[2];
550 caddr_t v
= (caddr_t
)v_arg
, netmask
= (caddr_t
)n_arg
;
551 register struct radix_node
*t
, *x
= 0, *tt
;
552 struct radix_node
*saved_tt
, *top
= head
->rnh_treetop
;
553 short b
= 0, b_leaf
= 0;
556 struct radix_mask
*m
, **mp
;
559 * In dealing with non-contiguous masks, there may be
560 * many different routes which have the same mask.
561 * We will find it useful to have a unique pointer to
562 * the mask to speed avoiding duplicate references at
563 * nodes and possibly save time in calculating indices.
566 if ((x
= rn_addmask(netmask
, 0, top
->rn_off
)) == 0)
573 * Deal with duplicated keys: attach node to previous instance
575 saved_tt
= tt
= rn_insert(v
, head
, &keyduplicated
, treenodes
);
577 for (t
= tt
; tt
; t
= tt
, tt
= tt
->rn_dupedkey
) {
578 if (tt
->rn_mask
== netmask
)
582 ((b_leaf
< tt
->rn_b
) || /* index(netmask) > node */
583 rn_refines(netmask
, tt
->rn_mask
) ||
584 rn_lexobetter(netmask
, tt
->rn_mask
))))
588 * If the mask is not duplicated, we wouldn't
589 * find it among possible duplicate key entries
590 * anyway, so the above test doesn't hurt.
592 * We sort the masks for a duplicated key the same way as
593 * in a masklist -- most specific to least specific.
594 * This may require the unfortunate nuisance of relocating
595 * the head of the list.
597 if (tt
== saved_tt
) {
598 struct radix_node
*xx
= x
;
599 /* link in at head of list */
600 (tt
= treenodes
)->rn_dupedkey
= t
;
601 tt
->rn_flags
= t
->rn_flags
;
602 tt
->rn_p
= x
= t
->rn_p
;
603 t
->rn_p
= tt
; /* parent */
604 if (x
->rn_l
== t
) x
->rn_l
= tt
; else x
->rn_r
= tt
;
605 saved_tt
= tt
; x
= xx
;
607 (tt
= treenodes
)->rn_dupedkey
= t
->rn_dupedkey
;
609 tt
->rn_p
= t
; /* parent */
610 if (tt
->rn_dupedkey
) /* parent */
611 tt
->rn_dupedkey
->rn_p
= tt
; /* parent */
614 t
=tt
+1; tt
->rn_info
= rn_nodenum
++; t
->rn_info
= rn_nodenum
++;
615 tt
->rn_twin
= t
; tt
->rn_ybro
= rn_clist
; rn_clist
= tt
;
617 tt
->rn_key
= (caddr_t
) v
;
619 tt
->rn_flags
= RNF_ACTIVE
;
625 tt
->rn_mask
= netmask
;
627 tt
->rn_flags
|= x
->rn_flags
& RNF_NORMAL
;
632 b_leaf
= -1 - t
->rn_b
;
633 if (t
->rn_r
== saved_tt
) x
= t
->rn_l
; else x
= t
->rn_r
;
634 /* Promote general routes from below */
636 for (mp
= &t
->rn_mklist
; x
; x
= x
->rn_dupedkey
)
637 if (x
->rn_mask
&& (x
->rn_b
>= b_leaf
) && x
->rn_mklist
== 0) {
638 *mp
= m
= rn_new_radix_mask(x
, 0);
642 } else if (x
->rn_mklist
) {
644 * Skip over masks whose index is > that of new node
646 for (mp
= &x
->rn_mklist
; (m
= *mp
); mp
= &m
->rm_mklist
)
647 if (m
->rm_b
>= b_leaf
)
649 t
->rn_mklist
= m
; *mp
= 0;
652 /* Add new route to highest possible ancestor's list */
653 if ((netmask
== 0) || (b
> t
->rn_b
))
654 return tt
; /* can't lift at all */
659 } while (b
<= t
->rn_b
&& x
!= top
);
661 * Search through routes associated with node to
662 * insert new route according to index.
663 * Need same criteria as when sorting dupedkeys to avoid
664 * double loop on deletion.
666 for (mp
= &x
->rn_mklist
; (m
= *mp
); mp
= &m
->rm_mklist
) {
667 if (m
->rm_b
< b_leaf
)
669 if (m
->rm_b
> b_leaf
)
671 if (m
->rm_flags
& RNF_NORMAL
) {
672 mmask
= m
->rm_leaf
->rn_mask
;
673 if (tt
->rn_flags
& RNF_NORMAL
) {
675 "Non-unique normal route, mask not entered");
680 if (mmask
== netmask
) {
685 if (rn_refines(netmask
, mmask
) || rn_lexobetter(netmask
, mmask
))
688 *mp
= rn_new_radix_mask(tt
, *mp
);
693 rn_delete(v_arg
, netmask_arg
, head
)
694 void *v_arg
, *netmask_arg
;
695 struct radix_node_head
*head
;
697 register struct radix_node
*t
, *p
, *x
, *tt
;
698 struct radix_mask
*m
, *saved_m
, **mp
;
699 struct radix_node
*dupedkey
, *saved_tt
, *top
;
701 int b
, head_off
, vlen
;
704 netmask
= netmask_arg
;
705 x
= head
->rnh_treetop
;
706 tt
= rn_search(v
, x
);
707 head_off
= x
->rn_off
;
712 Bcmp(v
+ head_off
, tt
->rn_key
+ head_off
, vlen
- head_off
))
715 * Delete our route from mask lists.
718 if ((x
= rn_addmask(netmask
, 1, head_off
)) == 0)
721 while (tt
->rn_mask
!= netmask
)
722 if ((tt
= tt
->rn_dupedkey
) == 0)
725 if (tt
->rn_mask
== 0 || (saved_m
= m
= tt
->rn_mklist
) == 0)
727 if (tt
->rn_flags
& RNF_NORMAL
) {
728 if (m
->rm_leaf
!= tt
|| m
->rm_refs
> 0) {
729 log(LOG_ERR
, "rn_delete: inconsistent annotation\n");
730 return 0; /* dangling ref could cause disaster */
733 if (m
->rm_mask
!= tt
->rn_mask
) {
734 log(LOG_ERR
, "rn_delete: inconsistent annotation\n");
737 if (--m
->rm_refs
>= 0)
743 goto on1
; /* Wasn't lifted at all */
747 } while (b
<= t
->rn_b
&& x
!= top
);
748 for (mp
= &x
->rn_mklist
; (m
= *mp
); mp
= &m
->rm_mklist
)
755 log(LOG_ERR
, "rn_delete: couldn't find our annotation\n");
756 if (tt
->rn_flags
& RNF_NORMAL
)
757 return (0); /* Dangling ref to us */
761 * Eliminate us from tree
763 if (tt
->rn_flags
& RNF_ROOT
)
766 /* Get us out of the creation list */
767 for (t
= rn_clist
; t
&& t
->rn_ybro
!= tt
; t
= t
->rn_ybro
) {}
768 if (t
) t
->rn_ybro
= tt
->rn_ybro
;
771 dupedkey
= saved_tt
->rn_dupedkey
;
774 * at this point, tt is the deletion target and saved_tt
775 * is the head of the dupekey chain
777 if (tt
== saved_tt
) {
778 /* remove from head of chain */
779 x
= dupedkey
; x
->rn_p
= t
;
780 if (t
->rn_l
== tt
) t
->rn_l
= x
; else t
->rn_r
= x
;
782 /* find node in front of tt on the chain */
783 for (x
= p
= saved_tt
; p
&& p
->rn_dupedkey
!= tt
;)
786 p
->rn_dupedkey
= tt
->rn_dupedkey
;
787 if (tt
->rn_dupedkey
) /* parent */
788 tt
->rn_dupedkey
->rn_p
= p
; /* parent */
789 } else log(LOG_ERR
, "rn_delete: couldn't find us\n");
792 if (t
->rn_flags
& RNF_ACTIVE
) {
794 *++x
= *t
; p
= t
->rn_p
;
796 b
= t
->rn_info
; *++x
= *t
; t
->rn_info
= b
; p
= t
->rn_p
;
798 if (p
->rn_l
== t
) p
->rn_l
= x
; else p
->rn_r
= x
;
799 x
->rn_l
->rn_p
= x
; x
->rn_r
->rn_p
= x
;
803 if (t
->rn_l
== tt
) x
= t
->rn_r
; else x
= t
->rn_l
;
805 if (p
->rn_r
== t
) p
->rn_r
= x
; else p
->rn_l
= x
;
808 * Demote routes attached to us.
812 for (mp
= &x
->rn_mklist
; (m
= *mp
);)
816 /* If there are any key,mask pairs in a sibling
817 duped-key chain, some subset will appear sorted
818 in the same order attached to our mklist */
819 for (m
= t
->rn_mklist
; m
&& x
; x
= x
->rn_dupedkey
)
820 if (m
== x
->rn_mklist
) {
821 struct radix_mask
*mm
= m
->rm_mklist
;
823 if (--(m
->rm_refs
) < 0)
829 "rn_delete: Orphaned Mask %p at %p\n",
830 (void *)m
, (void *)x
);
834 * We may be holding an active internal node in the tree.
841 b
= t
->rn_info
; *t
= *x
; t
->rn_info
= b
;
843 t
->rn_l
->rn_p
= t
; t
->rn_r
->rn_p
= t
;
845 if (p
->rn_l
== x
) p
->rn_l
= t
; else p
->rn_r
= t
;
848 tt
->rn_flags
&= ~RNF_ACTIVE
;
849 tt
[1].rn_flags
&= ~RNF_ACTIVE
;
854 * This is the same as rn_walktree() except for the parameters and the
858 rn_walktree_from(h
, a
, m
, f
, w
)
859 struct radix_node_head
*h
;
865 struct radix_node
*base
, *next
;
866 u_char
*xa
= (u_char
*)a
;
867 u_char
*xm
= (u_char
*)m
;
868 register struct radix_node
*rn
, *last
= 0 /* shut up gcc */;
873 * rn_search_m is sort-of-open-coded here.
875 /* printf("about to search\n"); */
876 for (rn
= h
->rnh_treetop
; rn
->rn_b
>= 0; ) {
878 /* printf("rn_b %d, rn_bmask %x, xm[rn_off] %x\n",
879 rn->rn_b, rn->rn_bmask, xm[rn->rn_off]); */
880 if (!(rn
->rn_bmask
& xm
[rn
->rn_off
])) {
883 if (rn
->rn_bmask
& xa
[rn
->rn_off
]) {
889 /* printf("done searching\n"); */
892 * Two cases: either we stepped off the end of our mask,
893 * in which case last == rn, or we reached a leaf, in which
894 * case we want to start from the last node we looked at.
895 * Either way, last is the node we want to start from.
900 /* printf("rn %p, lastb %d\n", rn, lastb);*/
903 * This gets complicated because we may delete the node
904 * while applying the function f to it, so we need to calculate
905 * the successor node in advance.
907 while (rn
->rn_b
>= 0)
911 /* printf("node %p (%d)\n", rn, rn->rn_b); */
913 /* If at right child go back up, otherwise, go right */
914 while (rn
->rn_p
->rn_r
== rn
&& !(rn
->rn_flags
& RNF_ROOT
)) {
917 /* if went up beyond last, stop */
918 if (rn
->rn_b
< lastb
) {
920 /* printf("up too far\n"); */
924 /* Find the next *leaf* since next node might vanish, too */
925 for (rn
= rn
->rn_p
->rn_r
; rn
->rn_b
>= 0;)
929 while ((rn
= base
) != 0) {
930 base
= rn
->rn_dupedkey
;
931 /* printf("leaf %p\n", rn); */
932 if (!(rn
->rn_flags
& RNF_ROOT
)
933 && (error
= (*f
)(rn
, w
)))
938 if (rn
->rn_flags
& RNF_ROOT
) {
939 /* printf("root, stopping"); */
949 struct radix_node_head
*h
;
954 struct radix_node
*base
, *next
;
955 register struct radix_node
*rn
= h
->rnh_treetop
;
957 * This gets complicated because we may delete the node
958 * while applying the function f to it, so we need to calculate
959 * the successor node in advance.
961 /* First time through node, go left */
962 while (rn
->rn_b
>= 0)
966 /* If at right child go back up, otherwise, go right */
967 while (rn
->rn_p
->rn_r
== rn
&& (rn
->rn_flags
& RNF_ROOT
) == 0)
969 /* Find the next *leaf* since next node might vanish, too */
970 for (rn
= rn
->rn_p
->rn_r
; rn
->rn_b
>= 0;)
974 while ((rn
= base
)) {
975 base
= rn
->rn_dupedkey
;
976 if (!(rn
->rn_flags
& RNF_ROOT
) && (error
= (*f
)(rn
, w
)))
980 if (rn
->rn_flags
& RNF_ROOT
)
987 rn_inithead(head
, off
)
991 register struct radix_node_head
*rnh
;
992 register struct radix_node
*t
, *tt
, *ttt
;
995 R_Malloc(rnh
, struct radix_node_head
*, sizeof (*rnh
));
998 Bzero(rnh
, sizeof (*rnh
));
1000 t
= rn_newpair(rn_zeros
, off
, rnh
->rnh_nodes
);
1001 ttt
= rnh
->rnh_nodes
+ 2;
1005 tt
->rn_flags
= t
->rn_flags
= RNF_ROOT
| RNF_ACTIVE
;
1006 tt
->rn_b
= -1 - off
;
1008 ttt
->rn_key
= rn_ones
;
1009 rnh
->rnh_addaddr
= rn_addroute
;
1010 rnh
->rnh_deladdr
= rn_delete
;
1011 rnh
->rnh_matchaddr
= rn_match
;
1012 rnh
->rnh_lookup
= rn_lookup
;
1013 rnh
->rnh_walktree
= rn_walktree
;
1014 rnh
->rnh_walktree_from
= rn_walktree_from
;
1015 rnh
->rnh_treetop
= t
;
1026 for (dom
= domains
; dom
; dom
= dom
->dom_next
)
1027 if (dom
->dom_maxrtkey
> max_keylen
)
1028 max_keylen
= dom
->dom_maxrtkey
;
1030 if (max_keylen
== 0) {
1032 "rn_init: radix functions require max_keylen be set\n");
1035 R_Malloc(rn_zeros
, char *, 3 * max_keylen
);
1036 if (rn_zeros
== NULL
)
1038 Bzero(rn_zeros
, 3 * max_keylen
);
1039 rn_ones
= cp
= rn_zeros
+ max_keylen
;
1040 addmask_key
= cplim
= rn_ones
+ max_keylen
;
1043 if (rn_inithead((void **)&mask_rnhead
, 0) == 0)