1 //==========================================================================
5 //==========================================================================
6 //####BSDCOPYRIGHTBEGIN####
8 // -------------------------------------------
10 // Portions of this software may have been derived from OpenBSD,
11 // FreeBSD or other sources, and are covered by the appropriate
12 // copyright disclaimers included herein.
14 // Portions created by Red Hat are
15 // Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
17 // -------------------------------------------
19 //####BSDCOPYRIGHTEND####
20 //==========================================================================
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/malloc.h>
65 #define M_DONTWAIT M_NOWAIT
66 #include <sys/domain.h>
70 #include <net/radix.h>
73 static int rn_walktree_from __P((struct radix_node_head *h, void *a,
74 void *m, walktree_f_t *f, void *w));
75 static int rn_walktree __P((struct radix_node_head *, walktree_f_t *, void *));
76 static struct radix_node
77 *rn_insert __P((void *, struct radix_node_head *, int *,
78 struct radix_node [2])),
79 *rn_newpair __P((void *, int, struct radix_node[2])),
80 *rn_search __P((void *, struct radix_node *)),
81 *rn_search_m __P((void *, struct radix_node *, void *));
83 static int max_keylen;
84 static struct radix_mask *rn_mkfreelist;
85 static struct radix_node_head *mask_rnhead;
86 static char *addmask_key;
87 static char normal_chars[] = {0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, -1};
88 static char *rn_zeros, *rn_ones;
90 #define rn_masktop (mask_rnhead->rnh_treetop)
92 #define Bcmp(a, b, l) \
93 (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_bit 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_bit - 1.
107 * (We say the index of n is rn_bit.)
109 * There is at least one descendant which has a one bit at position rn_bit,
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_bit,
120 * and m is a normal mask, then the route applies to every descendant of n.
121 * If the index(m) < rn_bit, 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_bit >= 0;) {
145 if (x->rn_bmask & v[x->rn_offset])
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_bit >= 0;) {
162 if ((x->rn_bmask & m[x->rn_offset]) &&
163 (x->rn_bmask & v[x->rn_offset]))
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 x = rn_addmask(m_arg, 1, head->rnh_treetop->rn_offset);
212 x = rn_match(v_arg, head);
214 while (x && x->rn_mask != netmask)
221 rn_satsifies_leaf(trial, leaf, skip)
223 register struct radix_node *leaf;
226 register char *cp = trial, *cp2 = leaf->rn_key, *cp3 = leaf->rn_mask;
228 int length = min(*(u_char *)cp, *(u_char *)cp2);
233 length = min(length, *(u_char *)cp3);
234 cplim = cp + length; cp3 += skip; cp2 += skip;
235 for (cp += skip; cp < cplim; cp++, cp2++, cp3++)
236 if ((*cp ^ *cp2) & *cp3)
242 rn_match(v_arg, head)
244 struct radix_node_head *head;
247 register struct radix_node *t = head->rnh_treetop, *x;
248 register caddr_t cp = v, cp2;
250 struct radix_node *saved_t, *top = t;
251 int off = t->rn_offset, vlen = *(u_char *)cp, matched_off;
252 register int test, b, rn_bit;
255 * Open code rn_search(v, top) to avoid overhead of extra
258 for (; t->rn_bit >= 0; ) {
259 if (t->rn_bmask & cp[t->rn_offset])
265 * See if we match exactly as a host destination
266 * or at least learn how many bits match, for normal mask finesse.
268 * It doesn't hurt us to limit how many bytes to check
269 * to the length of the mask, since if it matches we had a genuine
270 * match and the leaf we have is the most specific one anyway;
271 * if it didn't match with a shorter length it would fail
272 * with a long one. This wins big for class B&C netmasks which
273 * are probably the most common case...
276 vlen = *(u_char *)t->rn_mask;
277 cp += off; cp2 = t->rn_key + off; cplim = v + vlen;
278 for (; cp < cplim; cp++, cp2++)
282 * This extra grot is in case we are explicitly asked
283 * to look up the default. Ugh!
285 * Never return the root node itself, it seems to cause a
288 if (t->rn_flags & RNF_ROOT)
292 test = (*cp ^ *cp2) & 0xff; /* find first bit that differs */
293 for (b = 7; (test >>= 1) > 0;)
295 matched_off = cp - v;
296 b += matched_off << 3;
299 * If there is a host route in a duped-key chain, it will be first.
301 if ((saved_t = t)->rn_mask == 0)
303 for (; t; t = t->rn_dupedkey)
305 * Even if we don't match exactly as a host,
306 * we may match if the leaf we wound up at is
309 if (t->rn_flags & RNF_NORMAL) {
310 if (rn_bit <= t->rn_bit)
312 } else if (rn_satsifies_leaf(v, t, matched_off))
315 /* start searching up the tree */
317 register struct radix_mask *m;
321 * If non-contiguous masks ever become important
322 * we can restore the masking and open coding of
323 * the search and satisfaction test and put the
324 * calculation of "off" back before the "do".
327 if (m->rm_flags & RNF_NORMAL) {
328 if (rn_bit <= m->rm_bit)
331 off = min(t->rn_offset, matched_off);
332 x = rn_search_m(v, t, m->rm_mask);
333 while (x && x->rn_mask != m->rm_mask)
335 if (x && rn_satsifies_leaf(v, x, off))
346 struct radix_node *rn_clist;
351 static struct radix_node *
352 rn_newpair(v, b, nodes)
355 struct radix_node nodes[2];
357 register struct radix_node *tt = nodes, *t = tt + 1;
359 t->rn_bmask = 0x80 >> (b & 7);
361 t->rn_offset = b >> 3;
363 tt->rn_key = (caddr_t)v;
365 tt->rn_flags = t->rn_flags = RNF_ACTIVE;
366 tt->rn_mklist = t->rn_mklist = 0;
368 tt->rn_info = rn_nodenum++; t->rn_info = rn_nodenum++;
370 tt->rn_ybro = rn_clist;
376 static struct radix_node *
377 rn_insert(v_arg, head, dupentry, nodes)
379 struct radix_node_head *head;
381 struct radix_node nodes[2];
384 struct radix_node *top = head->rnh_treetop;
385 int head_off = top->rn_offset, vlen = (int)*((u_char *)v);
386 register struct radix_node *t = rn_search(v_arg, top);
387 register caddr_t cp = v + head_off;
389 struct radix_node *tt;
391 * Find first bit at which v and t->rn_key differ
394 register caddr_t cp2 = t->rn_key + head_off;
395 register int cmp_res;
396 caddr_t cplim = v + vlen;
405 cmp_res = (cp[-1] ^ cp2[-1]) & 0xff;
406 for (b = (cp - v) << 3; cmp_res; b--)
410 register struct radix_node *p, *x = top;
414 if (cp[x->rn_offset] & x->rn_bmask)
418 } while (b > (unsigned) x->rn_bit);
419 /* x->rn_bit < b && x->rn_bit >= 0 */
422 log(LOG_DEBUG, "rn_insert: Going In:\n"), traverse(p);
424 t = rn_newpair(v_arg, b, nodes);
426 if ((cp[p->rn_offset] & p->rn_bmask) == 0)
431 t->rn_parent = p; /* frees x, p as temp vars below */
432 if ((cp[t->rn_offset] & t->rn_bmask) == 0) {
440 log(LOG_DEBUG, "rn_insert: Coming Out:\n"), traverse(p);
447 rn_addmask(n_arg, search, skip)
451 caddr_t netmask = (caddr_t)n_arg;
452 register struct radix_node *x;
453 register caddr_t cp, cplim;
454 register int b = 0, mlen, j;
455 int maskduplicated, m0, isnormal;
456 struct radix_node *saved_x;
457 static int last_zeroed = 0;
459 if ((mlen = *(u_char *)netmask) > max_keylen)
464 return (mask_rnhead->rnh_nodes);
466 Bcopy(rn_ones + 1, addmask_key + 1, skip - 1);
467 if ((m0 = mlen) > skip)
468 Bcopy(netmask + skip, addmask_key + skip, mlen - skip);
470 * Trim trailing zeroes.
472 for (cp = addmask_key + mlen; (cp > addmask_key) && cp[-1] == 0;)
474 mlen = cp - addmask_key;
476 if (m0 >= last_zeroed)
478 return (mask_rnhead->rnh_nodes);
480 if (m0 < last_zeroed)
481 Bzero(addmask_key + m0, last_zeroed - m0);
482 *addmask_key = last_zeroed = mlen;
483 x = rn_search(addmask_key, rn_masktop);
484 if (Bcmp(addmask_key, x->rn_key, mlen) != 0)
488 R_Malloc(x, struct radix_node *, max_keylen + 2 * sizeof (*x));
489 if ((saved_x = x) == 0)
491 Bzero(x, max_keylen + 2 * sizeof (*x));
492 netmask = cp = (caddr_t)(x + 2);
493 Bcopy(addmask_key, cp, mlen);
494 x = rn_insert(cp, mask_rnhead, &maskduplicated, x);
495 if (maskduplicated) {
496 log(LOG_ERR, "rn_addmask: mask impossibly already in tree");
501 * Calculate index of mask, and check for normalcy.
503 cplim = netmask + mlen; isnormal = 1;
504 for (cp = netmask + skip; (cp < cplim) && *(u_char *)cp == 0xff;)
507 for (j = 0x80; (j & *cp) != 0; j >>= 1)
509 if (*cp != normal_chars[b] || cp != (cplim - 1))
512 b += (cp - netmask) << 3;
515 x->rn_flags |= RNF_NORMAL;
519 static int /* XXX: arbitrary ordering for non-contiguous masks */
520 rn_lexobetter(m_arg, n_arg)
523 register u_char *mp = m_arg, *np = n_arg, *lim;
526 return 1; /* not really, but need to check longer one first */
528 for (lim = mp + *mp; mp < lim;)
534 static struct radix_mask *
535 rn_new_radix_mask(tt, next)
536 register struct radix_node *tt;
537 register struct radix_mask *next;
539 register struct radix_mask *m;
543 log(LOG_ERR, "Mask for route not entered\n");
547 m->rm_bit = tt->rn_bit;
548 m->rm_flags = tt->rn_flags;
549 if (tt->rn_flags & RNF_NORMAL)
552 m->rm_mask = tt->rn_mask;
559 rn_addroute(v_arg, n_arg, head, treenodes)
561 struct radix_node_head *head;
562 struct radix_node treenodes[2];
564 caddr_t v = (caddr_t)v_arg, netmask = (caddr_t)n_arg;
565 register struct radix_node *t, *x = 0, *tt;
566 struct radix_node *saved_tt, *top = head->rnh_treetop;
567 short b = 0, b_leaf = 0;
570 struct radix_mask *m, **mp;
573 * In dealing with non-contiguous masks, there may be
574 * many different routes which have the same mask.
575 * We will find it useful to have a unique pointer to
576 * the mask to speed avoiding duplicate references at
577 * nodes and possibly save time in calculating indices.
580 if ((x = rn_addmask(netmask, 0, top->rn_offset)) == 0)
587 * Deal with duplicated keys: attach node to previous instance
589 saved_tt = tt = rn_insert(v, head, &keyduplicated, treenodes);
591 for (t = tt; tt; t = tt, tt = tt->rn_dupedkey) {
592 if (tt->rn_mask == netmask)
596 ((b_leaf < tt->rn_bit) /* index(netmask) > node */
597 || rn_refines(netmask, tt->rn_mask)
598 || rn_lexobetter(netmask, tt->rn_mask))))
602 * If the mask is not duplicated, we wouldn't
603 * find it among possible duplicate key entries
604 * anyway, so the above test doesn't hurt.
606 * We sort the masks for a duplicated key the same way as
607 * in a masklist -- most specific to least specific.
608 * This may require the unfortunate nuisance of relocating
609 * the head of the list.
611 if (tt == saved_tt) {
612 struct radix_node *xx = x;
613 /* link in at head of list */
614 (tt = treenodes)->rn_dupedkey = t;
615 tt->rn_flags = t->rn_flags;
616 tt->rn_parent = x = t->rn_parent;
617 t->rn_parent = tt; /* parent */
622 saved_tt = tt; x = xx;
624 (tt = treenodes)->rn_dupedkey = t->rn_dupedkey;
626 tt->rn_parent = t; /* parent */
627 if (tt->rn_dupedkey) /* parent */
628 tt->rn_dupedkey->rn_parent = tt; /* parent */
631 t=tt+1; tt->rn_info = rn_nodenum++; t->rn_info = rn_nodenum++;
632 tt->rn_twin = t; tt->rn_ybro = rn_clist; rn_clist = tt;
634 tt->rn_key = (caddr_t) v;
636 tt->rn_flags = RNF_ACTIVE;
642 tt->rn_mask = netmask;
643 tt->rn_bit = x->rn_bit;
644 tt->rn_flags |= x->rn_flags & RNF_NORMAL;
646 t = saved_tt->rn_parent;
649 b_leaf = -1 - t->rn_bit;
650 if (t->rn_right == saved_tt)
654 /* Promote general routes from below */
656 for (mp = &t->rn_mklist; x; x = x->rn_dupedkey)
657 if (x->rn_mask && (x->rn_bit >= b_leaf) && x->rn_mklist == 0) {
658 *mp = m = rn_new_radix_mask(x, 0);
662 } else if (x->rn_mklist) {
664 * Skip over masks whose index is > that of new node
666 for (mp = &x->rn_mklist; (m = *mp); mp = &m->rm_mklist)
667 if (m->rm_bit >= b_leaf)
669 t->rn_mklist = m; *mp = 0;
672 /* Add new route to highest possible ancestor's list */
673 if ((netmask == 0) || (b > t->rn_bit ))
674 return tt; /* can't lift at all */
679 } while (b <= t->rn_bit && x != top);
681 * Search through routes associated with node to
682 * insert new route according to index.
683 * Need same criteria as when sorting dupedkeys to avoid
684 * double loop on deletion.
686 for (mp = &x->rn_mklist; (m = *mp); mp = &m->rm_mklist) {
687 if (m->rm_bit < b_leaf)
689 if (m->rm_bit > b_leaf)
691 if (m->rm_flags & RNF_NORMAL) {
692 mmask = m->rm_leaf->rn_mask;
693 if (tt->rn_flags & RNF_NORMAL) {
695 "Non-unique normal route, mask not entered\n");
700 if (mmask == netmask) {
705 if (rn_refines(netmask, mmask)
706 || rn_lexobetter(netmask, mmask))
709 *mp = rn_new_radix_mask(tt, *mp);
714 rn_delete(v_arg, netmask_arg, head)
715 void *v_arg, *netmask_arg;
716 struct radix_node_head *head;
718 register struct radix_node *t, *p, *x, *tt;
719 struct radix_mask *m, *saved_m, **mp;
720 struct radix_node *dupedkey, *saved_tt, *top;
722 int b, head_off, vlen;
725 netmask = netmask_arg;
726 x = head->rnh_treetop;
727 tt = rn_search(v, x);
728 head_off = x->rn_offset;
733 Bcmp(v + head_off, tt->rn_key + head_off, vlen - head_off))
736 * Delete our route from mask lists.
739 if ((x = rn_addmask(netmask, 1, head_off)) == 0)
742 while (tt->rn_mask != netmask)
743 if ((tt = tt->rn_dupedkey) == 0)
746 if (tt->rn_mask == 0 || (saved_m = m = tt->rn_mklist) == 0)
748 if (tt->rn_flags & RNF_NORMAL) {
749 if (m->rm_leaf != tt || m->rm_refs > 0) {
750 log(LOG_ERR, "rn_delete: inconsistent annotation\n");
751 return 0; /* dangling ref could cause disaster */
754 if (m->rm_mask != tt->rn_mask) {
755 log(LOG_ERR, "rn_delete: inconsistent annotation\n");
758 if (--m->rm_refs >= 0)
762 t = saved_tt->rn_parent;
764 goto on1; /* Wasn't lifted at all */
768 } while (b <= t->rn_bit && x != top);
769 for (mp = &x->rn_mklist; (m = *mp); mp = &m->rm_mklist)
776 log(LOG_ERR, "rn_delete: couldn't find our annotation\n");
777 if (tt->rn_flags & RNF_NORMAL)
778 return (0); /* Dangling ref to us */
782 * Eliminate us from tree
784 if (tt->rn_flags & RNF_ROOT)
787 /* Get us out of the creation list */
788 for (t = rn_clist; t && t->rn_ybro != tt; t = t->rn_ybro) {}
789 if (t) t->rn_ybro = tt->rn_ybro;
792 dupedkey = saved_tt->rn_dupedkey;
795 * at this point, tt is the deletion target and saved_tt
796 * is the head of the dupekey chain
798 if (tt == saved_tt) {
799 /* remove from head of chain */
800 x = dupedkey; x->rn_parent = t;
801 if (t->rn_left == tt)
806 /* find node in front of tt on the chain */
807 for (x = p = saved_tt; p && p->rn_dupedkey != tt;)
810 p->rn_dupedkey = tt->rn_dupedkey;
811 if (tt->rn_dupedkey) /* parent */
812 tt->rn_dupedkey->rn_parent = p;
815 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)
1014 /* If at right child go back up, otherwise, go right */
1015 while (rn->rn_parent->rn_right == rn
1016 && (rn->rn_flags & RNF_ROOT) == 0)
1018 /* Find the next *leaf* since next node might vanish, too */
1019 for (rn = rn->rn_parent->rn_right; rn->rn_bit >= 0;)
1022 /* Process leaves */
1023 while ((rn = base)) {
1024 base = rn->rn_dupedkey;
1025 if (!(rn->rn_flags & RNF_ROOT)
1026 && (error = (*f)(rn, w)))
1030 if (rn->rn_flags & RNF_ROOT)
1037 rn_inithead(head, off)
1041 register struct radix_node_head *rnh;
1042 register struct radix_node *t, *tt, *ttt;
1045 R_Malloc(rnh, struct radix_node_head *, sizeof (*rnh));
1048 Bzero(rnh, sizeof (*rnh));
1050 t = rn_newpair(rn_zeros, off, rnh->rnh_nodes);
1051 ttt = rnh->rnh_nodes + 2;
1055 tt->rn_flags = t->rn_flags = RNF_ROOT | RNF_ACTIVE;
1056 tt->rn_bit = -1 - off;
1058 ttt->rn_key = rn_ones;
1059 rnh->rnh_addaddr = rn_addroute;
1060 rnh->rnh_deladdr = rn_delete;
1061 rnh->rnh_matchaddr = rn_match;
1062 rnh->rnh_lookup = rn_lookup;
1063 rnh->rnh_walktree = rn_walktree;
1064 rnh->rnh_walktree_from = rn_walktree_from;
1065 rnh->rnh_treetop = t;
1076 for (dom = domains; dom; dom = dom->dom_next)
1077 if (dom->dom_maxrtkey > max_keylen)
1078 max_keylen = dom->dom_maxrtkey;
1080 if (max_keylen == 0) {
1082 "rn_init: radix functions require max_keylen be set\n");
1085 R_Malloc(rn_zeros, char *, 3 * max_keylen);
1086 if (rn_zeros == NULL)
1088 Bzero(rn_zeros, 3 * max_keylen);
1089 rn_ones = cp = rn_zeros + max_keylen;
1090 addmask_key = cplim = rn_ones + max_keylen;
1093 if (rn_inithead((void **)(void*)&mask_rnhead, 0) == 0)