]>
git.saurik.com Git - apple/xnu.git/blob - bsd/net/radix.c
cfe85497436e19784fe6fbe26b3f20d6d5faf152
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
55 * $FreeBSD: src/sys/net/radix.c,v 1.20.2.2 2001/03/06 00:56:50 obrien Exp $
59 * Routines to build and maintain radix trees for routing lookups.
62 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/malloc.h>
66 #define M_DONTWAIT M_NOWAIT
67 #include <sys/domain.h>
71 #include <sys/syslog.h>
72 #include <net/radix.h>
75 static int rn_walktree_from
__P((struct radix_node_head
*h
, void *a
,
76 void *m
, walktree_f_t
*f
, void *w
));
77 static int rn_walktree
__P((struct radix_node_head
*, walktree_f_t
*, void *));
78 static struct radix_node
79 *rn_insert
__P((void *, struct radix_node_head
*, int *,
80 struct radix_node
[2])),
81 *rn_newpair
__P((void *, int, struct radix_node
[2])),
82 *rn_search
__P((void *, struct radix_node
*)),
83 *rn_search_m
__P((void *, struct radix_node
*, void *));
85 static int max_keylen
;
86 static struct radix_mask
*rn_mkfreelist
;
87 static struct radix_node_head
*mask_rnhead
;
88 static char *addmask_key
;
89 static char normal_chars
[] = {0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, -1};
90 static char *rn_zeros
, *rn_ones
;
92 #define rn_masktop (mask_rnhead->rnh_treetop)
94 #define Bcmp(a, b, l) \
95 (l == 0 ? 0 : bcmp((caddr_t)(a), (caddr_t)(b), (u_long)l))
97 static int rn_lexobetter
__P((void *m_arg
, void *n_arg
));
98 static struct radix_mask
*
99 rn_new_radix_mask
__P((struct radix_node
*tt
,
100 struct radix_mask
*next
));
101 static int rn_satsifies_leaf
__P((char *trial
, struct radix_node
*leaf
,
105 * The data structure for the keys is a radix tree with one way
106 * branching removed. The index rn_bit at an internal node n represents a bit
107 * position to be tested. The tree is arranged so that all descendants
108 * of a node n have keys whose bits all agree up to position rn_bit - 1.
109 * (We say the index of n is rn_bit.)
111 * There is at least one descendant which has a one bit at position rn_bit,
112 * and at least one with a zero there.
114 * A route is determined by a pair of key and mask. We require that the
115 * bit-wise logical and of the key and mask to be the key.
116 * We define the index of a route to associated with the mask to be
117 * the first bit number in the mask where 0 occurs (with bit number 0
118 * representing the highest order bit).
120 * We say a mask is normal if every bit is 0, past the index of the mask.
121 * If a node n has a descendant (k, m) with index(m) == index(n) == rn_bit,
122 * and m is a normal mask, then the route applies to every descendant of n.
123 * If the index(m) < rn_bit, this implies the trailing last few bits of k
124 * before bit b are all 0, (and hence consequently true of every descendant
125 * of n), so the route applies to all descendants of the node as well.
127 * Similar logic shows that a non-normal mask m such that
128 * index(m) <= index(n) could potentially apply to many children of n.
129 * Thus, for each non-host route, we attach its mask to a list at an internal
130 * node as high in the tree as we can go.
132 * The present version of the code makes use of normal routes in short-
133 * circuiting an explict mask and compare operation when testing whether
134 * a key satisfies a normal route, and also in remembering the unique leaf
135 * that governs a subtree.
138 static struct radix_node
*
139 rn_search(v_arg
, head
)
141 struct radix_node
*head
;
143 register struct radix_node
*x
;
146 for (x
= head
, v
= v_arg
; x
->rn_bit
>= 0;) {
147 if (x
->rn_bmask
& v
[x
->rn_offset
])
155 static struct radix_node
*
156 rn_search_m(v_arg
, head
, m_arg
)
157 struct radix_node
*head
;
160 register struct radix_node
*x
;
161 register caddr_t v
= v_arg
, m
= m_arg
;
163 for (x
= head
; x
->rn_bit
>= 0;) {
164 if ((x
->rn_bmask
& m
[x
->rn_offset
]) &&
165 (x
->rn_bmask
& v
[x
->rn_offset
]))
174 rn_refines(m_arg
, n_arg
)
177 register caddr_t m
= m_arg
, n
= n_arg
;
178 register caddr_t lim
, lim2
= lim
= n
+ *(u_char
*)n
;
179 int longer
= (*(u_char
*)n
++) - (int)(*(u_char
*)m
++);
180 int masks_are_equal
= 1;
193 if (masks_are_equal
&& (longer
< 0))
194 for (lim2
= m
- longer
; m
< lim2
; )
197 return (!masks_are_equal
);
201 rn_lookup(v_arg
, m_arg
, head
)
203 struct radix_node_head
*head
;
205 register struct radix_node
*x
;
209 x
= rn_addmask(m_arg
, 1, head
->rnh_treetop
->rn_offset
);
214 x
= rn_match(v_arg
, head
);
216 while (x
&& x
->rn_mask
!= netmask
)
223 rn_satsifies_leaf(trial
, leaf
, skip
)
225 register struct radix_node
*leaf
;
228 register char *cp
= trial
, *cp2
= leaf
->rn_key
, *cp3
= leaf
->rn_mask
;
230 int length
= min(*(u_char
*)cp
, *(u_char
*)cp2
);
235 length
= min(length
, *(u_char
*)cp3
);
236 cplim
= cp
+ length
; cp3
+= skip
; cp2
+= skip
;
237 for (cp
+= skip
; cp
< cplim
; cp
++, cp2
++, cp3
++)
238 if ((*cp
^ *cp2
) & *cp3
)
244 rn_match(v_arg
, head
)
246 struct radix_node_head
*head
;
249 register struct radix_node
*t
= head
->rnh_treetop
, *x
;
250 register caddr_t cp
= v
, cp2
;
252 struct radix_node
*saved_t
, *top
= t
;
253 int off
= t
->rn_offset
, vlen
= *(u_char
*)cp
, matched_off
;
254 register int test
, b
, rn_bit
;
257 * Open code rn_search(v, top) to avoid overhead of extra
260 for (; t
->rn_bit
>= 0; ) {
261 if (t
->rn_bmask
& cp
[t
->rn_offset
])
267 * See if we match exactly as a host destination
268 * or at least learn how many bits match, for normal mask finesse.
270 * It doesn't hurt us to limit how many bytes to check
271 * to the length of the mask, since if it matches we had a genuine
272 * match and the leaf we have is the most specific one anyway;
273 * if it didn't match with a shorter length it would fail
274 * with a long one. This wins big for class B&C netmasks which
275 * are probably the most common case...
278 vlen
= *(u_char
*)t
->rn_mask
;
279 cp
+= off
; cp2
= t
->rn_key
+ off
; cplim
= v
+ vlen
;
280 for (; cp
< cplim
; cp
++, cp2
++)
284 * This extra grot is in case we are explicitly asked
285 * to look up the default. Ugh!
287 * Never return the root node itself, it seems to cause a
290 if (t
->rn_flags
& RNF_ROOT
)
294 test
= (*cp
^ *cp2
) & 0xff; /* find first bit that differs */
295 for (b
= 7; (test
>>= 1) > 0;)
297 matched_off
= cp
- v
;
298 b
+= matched_off
<< 3;
301 * If there is a host route in a duped-key chain, it will be first.
303 if ((saved_t
= t
)->rn_mask
== 0)
305 for (; t
; t
= t
->rn_dupedkey
)
307 * Even if we don't match exactly as a host,
308 * we may match if the leaf we wound up at is
311 if (t
->rn_flags
& RNF_NORMAL
) {
312 if (rn_bit
<= t
->rn_bit
)
314 } else if (rn_satsifies_leaf(v
, t
, matched_off
))
317 /* start searching up the tree */
319 register struct radix_mask
*m
;
323 * If non-contiguous masks ever become important
324 * we can restore the masking and open coding of
325 * the search and satisfaction test and put the
326 * calculation of "off" back before the "do".
329 if (m
->rm_flags
& RNF_NORMAL
) {
330 if (rn_bit
<= m
->rm_bit
)
333 off
= min(t
->rn_offset
, matched_off
);
334 x
= rn_search_m(v
, t
, m
->rm_mask
);
335 while (x
&& x
->rn_mask
!= m
->rm_mask
)
337 if (x
&& rn_satsifies_leaf(v
, x
, off
))
348 struct radix_node
*rn_clist
;
353 static struct radix_node
*
354 rn_newpair(v
, b
, nodes
)
357 struct radix_node nodes
[2];
359 register struct radix_node
*tt
= nodes
, *t
= tt
+ 1;
361 t
->rn_bmask
= 0x80 >> (b
& 7);
363 t
->rn_offset
= b
>> 3;
365 tt
->rn_key
= (caddr_t
)v
;
367 tt
->rn_flags
= t
->rn_flags
= RNF_ACTIVE
;
368 tt
->rn_mklist
= t
->rn_mklist
= 0;
370 tt
->rn_info
= rn_nodenum
++; t
->rn_info
= rn_nodenum
++;
372 tt
->rn_ybro
= rn_clist
;
378 static struct radix_node
*
379 rn_insert(v_arg
, head
, dupentry
, nodes
)
381 struct radix_node_head
*head
;
383 struct radix_node nodes
[2];
386 struct radix_node
*top
= head
->rnh_treetop
;
387 int head_off
= top
->rn_offset
, vlen
= (int)*((u_char
*)v
);
388 register struct radix_node
*t
= rn_search(v_arg
, top
);
389 register caddr_t cp
= v
+ head_off
;
391 struct radix_node
*tt
;
393 * Find first bit at which v and t->rn_key differ
396 register caddr_t cp2
= t
->rn_key
+ head_off
;
397 register int cmp_res
;
398 caddr_t cplim
= v
+ vlen
;
407 cmp_res
= (cp
[-1] ^ cp2
[-1]) & 0xff;
408 for (b
= (cp
- v
) << 3; cmp_res
; b
--)
412 register struct radix_node
*p
, *x
= top
;
416 if (cp
[x
->rn_offset
] & x
->rn_bmask
)
420 } while (b
> (unsigned) x
->rn_bit
);
421 /* x->rn_bit < b && x->rn_bit >= 0 */
424 log(LOG_DEBUG
, "rn_insert: Going In:\n"), traverse(p
);
426 t
= rn_newpair(v_arg
, b
, nodes
);
428 if ((cp
[p
->rn_offset
] & p
->rn_bmask
) == 0)
433 t
->rn_parent
= p
; /* frees x, p as temp vars below */
434 if ((cp
[t
->rn_offset
] & t
->rn_bmask
) == 0) {
442 log(LOG_DEBUG
, "rn_insert: Coming Out:\n"), traverse(p
);
449 rn_addmask(n_arg
, search
, skip
)
453 caddr_t netmask
= (caddr_t
)n_arg
;
454 register struct radix_node
*x
;
455 register caddr_t cp
, cplim
;
456 register int b
= 0, mlen
, j
;
457 int maskduplicated
, m0
, isnormal
;
458 struct radix_node
*saved_x
;
459 static int last_zeroed
= 0;
461 if ((mlen
= *(u_char
*)netmask
) > max_keylen
)
466 return (mask_rnhead
->rnh_nodes
);
468 Bcopy(rn_ones
+ 1, addmask_key
+ 1, skip
- 1);
469 if ((m0
= mlen
) > skip
)
470 Bcopy(netmask
+ skip
, addmask_key
+ skip
, mlen
- skip
);
472 * Trim trailing zeroes.
474 for (cp
= addmask_key
+ mlen
; (cp
> addmask_key
) && cp
[-1] == 0;)
476 mlen
= cp
- addmask_key
;
478 if (m0
>= last_zeroed
)
480 return (mask_rnhead
->rnh_nodes
);
482 if (m0
< last_zeroed
)
483 Bzero(addmask_key
+ m0
, last_zeroed
- m0
);
484 *addmask_key
= last_zeroed
= mlen
;
485 x
= rn_search(addmask_key
, rn_masktop
);
486 if (Bcmp(addmask_key
, x
->rn_key
, mlen
) != 0)
490 R_Malloc(x
, struct radix_node
*, max_keylen
+ 2 * sizeof (*x
));
491 if ((saved_x
= x
) == 0)
493 Bzero(x
, max_keylen
+ 2 * sizeof (*x
));
494 netmask
= cp
= (caddr_t
)(x
+ 2);
495 Bcopy(addmask_key
, cp
, mlen
);
496 x
= rn_insert(cp
, mask_rnhead
, &maskduplicated
, x
);
497 if (maskduplicated
) {
498 log(LOG_ERR
, "rn_addmask: mask impossibly already in tree");
503 * Calculate index of mask, and check for normalcy.
505 cplim
= netmask
+ mlen
; isnormal
= 1;
506 for (cp
= netmask
+ skip
; (cp
< cplim
) && *(u_char
*)cp
== 0xff;)
509 for (j
= 0x80; (j
& *cp
) != 0; j
>>= 1)
511 if (*cp
!= normal_chars
[b
] || cp
!= (cplim
- 1))
514 b
+= (cp
- netmask
) << 3;
517 x
->rn_flags
|= RNF_NORMAL
;
521 static int /* XXX: arbitrary ordering for non-contiguous masks */
522 rn_lexobetter(m_arg
, n_arg
)
525 register u_char
*mp
= m_arg
, *np
= n_arg
, *lim
;
528 return 1; /* not really, but need to check longer one first */
530 for (lim
= mp
+ *mp
; mp
< lim
;)
536 static struct radix_mask
*
537 rn_new_radix_mask(tt
, next
)
538 register struct radix_node
*tt
;
539 register struct radix_mask
*next
;
541 register struct radix_mask
*m
;
545 log(LOG_ERR
, "Mask for route not entered\n");
549 m
->rm_bit
= tt
->rn_bit
;
550 m
->rm_flags
= tt
->rn_flags
;
551 if (tt
->rn_flags
& RNF_NORMAL
)
554 m
->rm_mask
= tt
->rn_mask
;
561 rn_addroute(v_arg
, n_arg
, head
, treenodes
)
563 struct radix_node_head
*head
;
564 struct radix_node treenodes
[2];
566 caddr_t v
= (caddr_t
)v_arg
, netmask
= (caddr_t
)n_arg
;
567 register struct radix_node
*t
, *x
= 0, *tt
;
568 struct radix_node
*saved_tt
, *top
= head
->rnh_treetop
;
569 short b
= 0, b_leaf
= 0;
572 struct radix_mask
*m
, **mp
;
575 * In dealing with non-contiguous masks, there may be
576 * many different routes which have the same mask.
577 * We will find it useful to have a unique pointer to
578 * the mask to speed avoiding duplicate references at
579 * nodes and possibly save time in calculating indices.
582 if ((x
= rn_addmask(netmask
, 0, top
->rn_offset
)) == 0)
589 * Deal with duplicated keys: attach node to previous instance
591 saved_tt
= tt
= rn_insert(v
, head
, &keyduplicated
, treenodes
);
593 for (t
= tt
; tt
; t
= tt
, tt
= tt
->rn_dupedkey
) {
594 if (tt
->rn_mask
== netmask
)
598 ((b_leaf
< tt
->rn_bit
) /* index(netmask) > node */
599 || rn_refines(netmask
, tt
->rn_mask
)
600 || rn_lexobetter(netmask
, tt
->rn_mask
))))
604 * If the mask is not duplicated, we wouldn't
605 * find it among possible duplicate key entries
606 * anyway, so the above test doesn't hurt.
608 * We sort the masks for a duplicated key the same way as
609 * in a masklist -- most specific to least specific.
610 * This may require the unfortunate nuisance of relocating
611 * the head of the list.
613 if (tt
== saved_tt
) {
614 struct radix_node
*xx
= x
;
615 /* link in at head of list */
616 (tt
= treenodes
)->rn_dupedkey
= t
;
617 tt
->rn_flags
= t
->rn_flags
;
618 tt
->rn_parent
= x
= t
->rn_parent
;
619 t
->rn_parent
= tt
; /* parent */
624 saved_tt
= tt
; x
= xx
;
626 (tt
= treenodes
)->rn_dupedkey
= t
->rn_dupedkey
;
628 tt
->rn_parent
= t
; /* parent */
629 if (tt
->rn_dupedkey
) /* parent */
630 tt
->rn_dupedkey
->rn_parent
= tt
; /* parent */
633 t
=tt
+1; tt
->rn_info
= rn_nodenum
++; t
->rn_info
= rn_nodenum
++;
634 tt
->rn_twin
= t
; tt
->rn_ybro
= rn_clist
; rn_clist
= tt
;
636 tt
->rn_key
= (caddr_t
) v
;
638 tt
->rn_flags
= RNF_ACTIVE
;
644 tt
->rn_mask
= netmask
;
645 tt
->rn_bit
= x
->rn_bit
;
646 tt
->rn_flags
|= x
->rn_flags
& RNF_NORMAL
;
648 t
= saved_tt
->rn_parent
;
651 b_leaf
= -1 - t
->rn_bit
;
652 if (t
->rn_right
== saved_tt
)
656 /* Promote general routes from below */
658 for (mp
= &t
->rn_mklist
; x
; x
= x
->rn_dupedkey
)
659 if (x
->rn_mask
&& (x
->rn_bit
>= b_leaf
) && x
->rn_mklist
== 0) {
660 *mp
= m
= rn_new_radix_mask(x
, 0);
664 } else if (x
->rn_mklist
) {
666 * Skip over masks whose index is > that of new node
668 for (mp
= &x
->rn_mklist
; (m
= *mp
); mp
= &m
->rm_mklist
)
669 if (m
->rm_bit
>= b_leaf
)
671 t
->rn_mklist
= m
; *mp
= 0;
674 /* Add new route to highest possible ancestor's list */
675 if ((netmask
== 0) || (b
> t
->rn_bit
))
676 return tt
; /* can't lift at all */
681 } while (b
<= t
->rn_bit
&& x
!= top
);
683 * Search through routes associated with node to
684 * insert new route according to index.
685 * Need same criteria as when sorting dupedkeys to avoid
686 * double loop on deletion.
688 for (mp
= &x
->rn_mklist
; (m
= *mp
); mp
= &m
->rm_mklist
) {
689 if (m
->rm_bit
< b_leaf
)
691 if (m
->rm_bit
> b_leaf
)
693 if (m
->rm_flags
& RNF_NORMAL
) {
694 mmask
= m
->rm_leaf
->rn_mask
;
695 if (tt
->rn_flags
& RNF_NORMAL
) {
697 "Non-unique normal route, mask not entered");
702 if (mmask
== netmask
) {
707 if (rn_refines(netmask
, mmask
)
708 || rn_lexobetter(netmask
, mmask
))
711 *mp
= rn_new_radix_mask(tt
, *mp
);
716 rn_delete(v_arg
, netmask_arg
, head
)
717 void *v_arg
, *netmask_arg
;
718 struct radix_node_head
*head
;
720 register struct radix_node
*t
, *p
, *x
, *tt
;
721 struct radix_mask
*m
, *saved_m
, **mp
;
722 struct radix_node
*dupedkey
, *saved_tt
, *top
;
724 int b
, head_off
, vlen
;
727 netmask
= netmask_arg
;
728 x
= head
->rnh_treetop
;
729 tt
= rn_search(v
, x
);
730 head_off
= x
->rn_offset
;
735 Bcmp(v
+ head_off
, tt
->rn_key
+ head_off
, vlen
- head_off
))
738 * Delete our route from mask lists.
741 if ((x
= rn_addmask(netmask
, 1, head_off
)) == 0)
744 while (tt
->rn_mask
!= netmask
)
745 if ((tt
= tt
->rn_dupedkey
) == 0)
748 if (tt
->rn_mask
== 0 || (saved_m
= m
= tt
->rn_mklist
) == 0)
750 if (tt
->rn_flags
& RNF_NORMAL
) {
751 if (m
->rm_leaf
!= tt
|| m
->rm_refs
> 0) {
752 log(LOG_ERR
, "rn_delete: inconsistent annotation\n");
753 return 0; /* dangling ref could cause disaster */
756 if (m
->rm_mask
!= tt
->rn_mask
) {
757 log(LOG_ERR
, "rn_delete: inconsistent annotation\n");
760 if (--m
->rm_refs
>= 0)
764 t
= saved_tt
->rn_parent
;
766 goto on1
; /* Wasn't lifted at all */
770 } while (b
<= t
->rn_bit
&& x
!= top
);
771 for (mp
= &x
->rn_mklist
; (m
= *mp
); mp
= &m
->rm_mklist
)
778 log(LOG_ERR
, "rn_delete: couldn't find our annotation\n");
779 if (tt
->rn_flags
& RNF_NORMAL
)
780 return (0); /* Dangling ref to us */
784 * Eliminate us from tree
786 if (tt
->rn_flags
& RNF_ROOT
)
789 /* Get us out of the creation list */
790 for (t
= rn_clist
; t
&& t
->rn_ybro
!= tt
; t
= t
->rn_ybro
) {}
791 if (t
) t
->rn_ybro
= tt
->rn_ybro
;
794 dupedkey
= saved_tt
->rn_dupedkey
;
797 * at this point, tt is the deletion target and saved_tt
798 * is the head of the dupekey chain
800 if (tt
== saved_tt
) {
801 /* remove from head of chain */
802 x
= dupedkey
; x
->rn_parent
= t
;
803 if (t
->rn_left
== tt
)
808 /* find node in front of tt on the chain */
809 for (x
= p
= saved_tt
; p
&& p
->rn_dupedkey
!= tt
;)
812 p
->rn_dupedkey
= tt
->rn_dupedkey
;
813 if (tt
->rn_dupedkey
) /* parent */
814 tt
->rn_dupedkey
->rn_parent
= p
;
816 } else log(LOG_ERR
, "rn_delete: couldn't find us\n");
819 if (t
->rn_flags
& RNF_ACTIVE
) {
833 x
->rn_left
->rn_parent
= x
;
834 x
->rn_right
->rn_parent
= x
;
838 if (t
->rn_left
== tt
)
843 if (p
->rn_right
== t
)
849 * Demote routes attached to us.
852 if (x
->rn_bit
>= 0) {
853 for (mp
= &x
->rn_mklist
; (m
= *mp
);)
857 /* If there are any key,mask pairs in a sibling
858 duped-key chain, some subset will appear sorted
859 in the same order attached to our mklist */
860 for (m
= t
->rn_mklist
; m
&& x
; x
= x
->rn_dupedkey
)
861 if (m
== x
->rn_mklist
) {
862 struct radix_mask
*mm
= m
->rm_mklist
;
864 if (--(m
->rm_refs
) < 0)
870 "rn_delete: Orphaned Mask %p at %p\n",
871 (void *)m
, (void *)x
);
875 * We may be holding an active internal node in the tree.
886 t
->rn_left
->rn_parent
= t
;
887 t
->rn_right
->rn_parent
= t
;
895 tt
->rn_flags
&= ~RNF_ACTIVE
;
896 tt
[1].rn_flags
&= ~RNF_ACTIVE
;
901 * This is the same as rn_walktree() except for the parameters and the
905 rn_walktree_from(h
, a
, m
, f
, w
)
906 struct radix_node_head
*h
;
912 struct radix_node
*base
, *next
;
913 u_char
*xa
= (u_char
*)a
;
914 u_char
*xm
= (u_char
*)m
;
915 register struct radix_node
*rn
, *last
= 0 /* shut up gcc */;
920 * rn_search_m is sort-of-open-coded here.
922 /* printf("about to search\n"); */
923 for (rn
= h
->rnh_treetop
; rn
->rn_bit
>= 0; ) {
925 /* printf("rn_bit %d, rn_bmask %x, xm[rn_offset] %x\n",
926 rn->rn_bit, rn->rn_bmask, xm[rn->rn_offset]); */
927 if (!(rn
->rn_bmask
& xm
[rn
->rn_offset
])) {
930 if (rn
->rn_bmask
& xa
[rn
->rn_offset
]) {
936 /* printf("done searching\n"); */
939 * Two cases: either we stepped off the end of our mask,
940 * in which case last == rn, or we reached a leaf, in which
941 * case we want to start from the last node we looked at.
942 * Either way, last is the node we want to start from.
947 /* printf("rn %p, lastb %d\n", rn, lastb);*/
950 * This gets complicated because we may delete the node
951 * while applying the function f to it, so we need to calculate
952 * the successor node in advance.
954 while (rn
->rn_bit
>= 0)
958 /* printf("node %p (%d)\n", rn, rn->rn_bit); */
960 /* If at right child go back up, otherwise, go right */
961 while (rn
->rn_parent
->rn_right
== rn
962 && !(rn
->rn_flags
& RNF_ROOT
)) {
965 /* if went up beyond last, stop */
966 if (rn
->rn_bit
< lastb
) {
968 /* printf("up too far\n"); */
972 /* Find the next *leaf* since next node might vanish, too */
973 for (rn
= rn
->rn_parent
->rn_right
; rn
->rn_bit
>= 0;)
977 while ((rn
= base
) != 0) {
978 base
= rn
->rn_dupedkey
;
979 /* printf("leaf %p\n", rn); */
980 if (!(rn
->rn_flags
& RNF_ROOT
)
981 && (error
= (*f
)(rn
, w
)))
986 if (rn
->rn_flags
& RNF_ROOT
) {
987 /* printf("root, stopping"); */
997 struct radix_node_head
*h
;
1002 struct radix_node
*base
, *next
;
1003 register struct radix_node
*rn
= h
->rnh_treetop
;
1005 * This gets complicated because we may delete the node
1006 * while applying the function f to it, so we need to calculate
1007 * the successor node in advance.
1009 /* First time through node, go left */
1010 while (rn
->rn_bit
>= 0)
1016 /* If at right child go back up, otherwise, go right */
1017 while (rn
!= NULL
&& rn
->rn_parent
!= NULL
&& rn
->rn_parent
->rn_right
== rn
1018 && (rn
->rn_flags
& RNF_ROOT
) == 0)
1020 /* Find the next *leaf* since next node might vanish, too */
1021 if (rn
== NULL
|| rn
->rn_parent
== NULL
|| rn
->rn_parent
->rn_right
== NULL
)
1023 for (rn
= rn
->rn_parent
->rn_right
; rn
->rn_bit
>= 0;) {
1024 if (rn
== NULL
|| rn
->rn_parent
== NULL
|| rn
->rn_parent
->rn_right
== NULL
|| rn
->rn_left
== NULL
)
1029 /* Process leaves */
1030 while ((rn
= base
)) {
1033 base
= rn
->rn_dupedkey
;
1034 if (!(rn
->rn_flags
& RNF_ROOT
)
1035 && (error
= (*f
)(rn
, w
)))
1041 if (rn
->rn_flags
& RNF_ROOT
)
1048 rn_inithead(head
, off
)
1052 register struct radix_node_head
*rnh
;
1053 register struct radix_node
*t
, *tt
, *ttt
;
1056 R_Malloc(rnh
, struct radix_node_head
*, sizeof (*rnh
));
1059 Bzero(rnh
, sizeof (*rnh
));
1061 t
= rn_newpair(rn_zeros
, off
, rnh
->rnh_nodes
);
1062 ttt
= rnh
->rnh_nodes
+ 2;
1066 tt
->rn_flags
= t
->rn_flags
= RNF_ROOT
| RNF_ACTIVE
;
1067 tt
->rn_bit
= -1 - off
;
1069 ttt
->rn_key
= rn_ones
;
1070 rnh
->rnh_addaddr
= rn_addroute
;
1071 rnh
->rnh_deladdr
= rn_delete
;
1072 rnh
->rnh_matchaddr
= rn_match
;
1073 rnh
->rnh_lookup
= rn_lookup
;
1074 rnh
->rnh_walktree
= rn_walktree
;
1075 rnh
->rnh_walktree_from
= rn_walktree_from
;
1076 rnh
->rnh_treetop
= t
;
1087 for (dom
= domains
; dom
; dom
= dom
->dom_next
)
1088 if (dom
->dom_maxrtkey
> max_keylen
)
1089 max_keylen
= dom
->dom_maxrtkey
;
1091 if (max_keylen
== 0) {
1093 "rn_init: radix functions require max_keylen be set\n");
1096 R_Malloc(rn_zeros
, char *, 3 * max_keylen
);
1097 if (rn_zeros
== NULL
)
1099 Bzero(rn_zeros
, 3 * max_keylen
);
1100 rn_ones
= cp
= rn_zeros
+ max_keylen
;
1101 addmask_key
= cplim
= rn_ones
+ max_keylen
;
1104 if (rn_inithead((void **)&mask_rnhead
, 0) == 0)