Home | History | Annotate | Line # | Download | only in routed
radix.c revision 1.3
      1  1.3  christos /*	$NetBSD: radix.c,v 1.3 1996/09/24 16:24:18 christos Exp $	*/
      2  1.2   thorpej 
      3  1.1   thorpej /*
      4  1.1   thorpej  * Copyright (c) 1988, 1989, 1993
      5  1.1   thorpej  *	The Regents of the University of California.  All rights reserved.
      6  1.1   thorpej  *
      7  1.1   thorpej  * Redistribution and use in source and binary forms, with or without
      8  1.1   thorpej  * modification, are permitted provided that the following conditions
      9  1.1   thorpej  * are met:
     10  1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     11  1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     12  1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     14  1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     15  1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     16  1.1   thorpej  *    must display the following acknowledgement:
     17  1.1   thorpej  *	This product includes software developed by the University of
     18  1.1   thorpej  *	California, Berkeley and its contributors.
     19  1.1   thorpej  * 4. Neither the name of the University nor the names of its contributors
     20  1.1   thorpej  *    may be used to endorse or promote products derived from this software
     21  1.1   thorpej  *    without specific prior written permission.
     22  1.1   thorpej  *
     23  1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1   thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1   thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1   thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1   thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1   thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1   thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1   thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1   thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1   thorpej  * SUCH DAMAGE.
     34  1.1   thorpej  *
     35  1.1   thorpej  *	@(#)radix.c	8.4 (Berkeley) 11/2/94
     36  1.1   thorpej  */
     37  1.1   thorpej 
     38  1.1   thorpej /*
     39  1.1   thorpej  * Routines to build and maintain radix trees for routing lookups.
     40  1.1   thorpej  */
     41  1.3  christos #if !defined(lint) && !defined(sgi) && !defined(__NetBSD__)
     42  1.3  christos static char sccsid[] = "@(#)rdisc.c	8.1 (Berkeley) x/y/95";
     43  1.3  christos #elif defined(__NetBSD__)
     44  1.3  christos static char rcsid[] = "$NetBSD: radix.c,v 1.3 1996/09/24 16:24:18 christos Exp $";
     45  1.3  christos #endif
     46  1.2   thorpej 
     47  1.2   thorpej #include "defs.h"
     48  1.2   thorpej 
     49  1.1   thorpej #define log(x, msg) syslog(x, msg)
     50  1.1   thorpej #define panic(s) {log(LOG_ERR,s); exit(1);}
     51  1.3  christos #define min(a,b) (((a)<(b))?(a):(b))
     52  1.1   thorpej 
     53  1.1   thorpej int	max_keylen;
     54  1.1   thorpej struct radix_mask *rn_mkfreelist;
     55  1.1   thorpej struct radix_node_head *mask_rnhead;
     56  1.1   thorpej static char *addmask_key;
     57  1.1   thorpej static char normal_chars[] = {0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, -1};
     58  1.1   thorpej static char *rn_zeros, *rn_ones;
     59  1.1   thorpej 
     60  1.1   thorpej #define rn_masktop (mask_rnhead->rnh_treetop)
     61  1.1   thorpej #undef Bcmp
     62  1.1   thorpej #define Bcmp(a, b, l) (l == 0 ? 0 : bcmp((caddr_t)(a), (caddr_t)(b), (u_long)l))
     63  1.1   thorpej 
     64  1.1   thorpej static int rn_satsifies_leaf(char *, struct radix_node *, int);
     65  1.1   thorpej 
     66  1.1   thorpej /*
     67  1.1   thorpej  * The data structure for the keys is a radix tree with one way
     68  1.1   thorpej  * branching removed.  The index rn_b at an internal node n represents a bit
     69  1.1   thorpej  * position to be tested.  The tree is arranged so that all descendants
     70  1.1   thorpej  * of a node n have keys whose bits all agree up to position rn_b - 1.
     71  1.1   thorpej  * (We say the index of n is rn_b.)
     72  1.1   thorpej  *
     73  1.1   thorpej  * There is at least one descendant which has a one bit at position rn_b,
     74  1.1   thorpej  * and at least one with a zero there.
     75  1.1   thorpej  *
     76  1.1   thorpej  * A route is determined by a pair of key and mask.  We require that the
     77  1.1   thorpej  * bit-wise logical and of the key and mask to be the key.
     78  1.1   thorpej  * We define the index of a route to associated with the mask to be
     79  1.1   thorpej  * the first bit number in the mask where 0 occurs (with bit number 0
     80  1.1   thorpej  * representing the highest order bit).
     81  1.3  christos  *
     82  1.1   thorpej  * We say a mask is normal if every bit is 0, past the index of the mask.
     83  1.1   thorpej  * If a node n has a descendant (k, m) with index(m) == index(n) == rn_b,
     84  1.1   thorpej  * and m is a normal mask, then the route applies to every descendant of n.
     85  1.1   thorpej  * If the index(m) < rn_b, this implies the trailing last few bits of k
     86  1.1   thorpej  * before bit b are all 0, (and hence consequently true of every descendant
     87  1.1   thorpej  * of n), so the route applies to all descendants of the node as well.
     88  1.3  christos  *
     89  1.1   thorpej  * Similar logic shows that a non-normal mask m such that
     90  1.1   thorpej  * index(m) <= index(n) could potentially apply to many children of n.
     91  1.1   thorpej  * Thus, for each non-host route, we attach its mask to a list at an internal
     92  1.3  christos  * node as high in the tree as we can go.
     93  1.1   thorpej  *
     94  1.1   thorpej  * The present version of the code makes use of normal routes in short-
     95  1.1   thorpej  * circuiting an explict mask and compare operation when testing whether
     96  1.1   thorpej  * a key satisfies a normal route, and also in remembering the unique leaf
     97  1.1   thorpej  * that governs a subtree.
     98  1.1   thorpej  */
     99  1.1   thorpej 
    100  1.1   thorpej struct radix_node *
    101  1.3  christos rn_search(void *v_arg,
    102  1.3  christos 	  struct radix_node *head)
    103  1.1   thorpej {
    104  1.1   thorpej 	register struct radix_node *x;
    105  1.1   thorpej 	register caddr_t v;
    106  1.1   thorpej 
    107  1.1   thorpej 	for (x = head, v = v_arg; x->rn_b >= 0;) {
    108  1.1   thorpej 		if (x->rn_bmask & v[x->rn_off])
    109  1.1   thorpej 			x = x->rn_r;
    110  1.1   thorpej 		else
    111  1.1   thorpej 			x = x->rn_l;
    112  1.1   thorpej 	}
    113  1.1   thorpej 	return (x);
    114  1.1   thorpej }
    115  1.1   thorpej 
    116  1.1   thorpej struct radix_node *
    117  1.3  christos rn_search_m(void *v_arg,
    118  1.3  christos 	    struct radix_node *head,
    119  1.3  christos 	    void *m_arg)
    120  1.1   thorpej {
    121  1.1   thorpej 	register struct radix_node *x;
    122  1.1   thorpej 	register caddr_t v = v_arg, m = m_arg;
    123  1.1   thorpej 
    124  1.1   thorpej 	for (x = head; x->rn_b >= 0;) {
    125  1.1   thorpej 		if ((x->rn_bmask & m[x->rn_off]) &&
    126  1.1   thorpej 		    (x->rn_bmask & v[x->rn_off]))
    127  1.1   thorpej 			x = x->rn_r;
    128  1.1   thorpej 		else
    129  1.1   thorpej 			x = x->rn_l;
    130  1.1   thorpej 	}
    131  1.1   thorpej 	return x;
    132  1.1   thorpej }
    133  1.1   thorpej 
    134  1.1   thorpej int
    135  1.3  christos rn_refines(void* m_arg, void *n_arg)
    136  1.1   thorpej {
    137  1.1   thorpej 	register caddr_t m = m_arg, n = n_arg;
    138  1.1   thorpej 	register caddr_t lim, lim2 = lim = n + *(u_char *)n;
    139  1.1   thorpej 	int longer = (*(u_char *)n++) - (int)(*(u_char *)m++);
    140  1.1   thorpej 	int masks_are_equal = 1;
    141  1.1   thorpej 
    142  1.1   thorpej 	if (longer > 0)
    143  1.1   thorpej 		lim -= longer;
    144  1.1   thorpej 	while (n < lim) {
    145  1.1   thorpej 		if (*n & ~(*m))
    146  1.1   thorpej 			return 0;
    147  1.1   thorpej 		if (*n++ != *m++)
    148  1.1   thorpej 			masks_are_equal = 0;
    149  1.1   thorpej 	}
    150  1.1   thorpej 	while (n < lim2)
    151  1.1   thorpej 		if (*n++)
    152  1.1   thorpej 			return 0;
    153  1.1   thorpej 	if (masks_are_equal && (longer < 0))
    154  1.1   thorpej 		for (lim2 = m - longer; m < lim2; )
    155  1.1   thorpej 			if (*m++)
    156  1.1   thorpej 				return 1;
    157  1.1   thorpej 	return (!masks_are_equal);
    158  1.1   thorpej }
    159  1.1   thorpej 
    160  1.1   thorpej struct radix_node *
    161  1.1   thorpej rn_lookup(v_arg, m_arg, head)
    162  1.1   thorpej 	void *v_arg, *m_arg;
    163  1.1   thorpej 	struct radix_node_head *head;
    164  1.1   thorpej {
    165  1.1   thorpej 	register struct radix_node *x;
    166  1.1   thorpej 	caddr_t netmask = 0;
    167  1.1   thorpej 
    168  1.1   thorpej 	if (m_arg) {
    169  1.1   thorpej 		if ((x = rn_addmask(m_arg, 1, head->rnh_treetop->rn_off)) == 0)
    170  1.1   thorpej 			return (0);
    171  1.1   thorpej 		netmask = x->rn_key;
    172  1.1   thorpej 	}
    173  1.1   thorpej 	x = rn_match(v_arg, head);
    174  1.1   thorpej 	if (x && netmask) {
    175  1.1   thorpej 		while (x && x->rn_mask != netmask)
    176  1.1   thorpej 			x = x->rn_dupedkey;
    177  1.1   thorpej 	}
    178  1.1   thorpej 	return x;
    179  1.1   thorpej }
    180  1.1   thorpej 
    181  1.1   thorpej static int
    182  1.1   thorpej rn_satsifies_leaf(char *trial,
    183  1.1   thorpej 		  register struct radix_node *leaf,
    184  1.1   thorpej 		  int skip)
    185  1.1   thorpej {
    186  1.1   thorpej 	register char *cp = trial, *cp2 = leaf->rn_key, *cp3 = leaf->rn_mask;
    187  1.1   thorpej 	char *cplim;
    188  1.1   thorpej 	int length = min(*(u_char *)cp, *(u_char *)cp2);
    189  1.1   thorpej 
    190  1.1   thorpej 	if (cp3 == 0)
    191  1.1   thorpej 		cp3 = rn_ones;
    192  1.1   thorpej 	else
    193  1.1   thorpej 		length = min(length, *(u_char *)cp3);
    194  1.1   thorpej 	cplim = cp + length; cp3 += skip; cp2 += skip;
    195  1.1   thorpej 	for (cp += skip; cp < cplim; cp++, cp2++, cp3++)
    196  1.1   thorpej 		if ((*cp ^ *cp2) & *cp3)
    197  1.1   thorpej 			return 0;
    198  1.1   thorpej 	return 1;
    199  1.1   thorpej }
    200  1.1   thorpej 
    201  1.1   thorpej struct radix_node *
    202  1.3  christos rn_match(void *v_arg,
    203  1.3  christos 	 struct radix_node_head *head)
    204  1.1   thorpej {
    205  1.1   thorpej 	caddr_t v = v_arg;
    206  1.1   thorpej 	register struct radix_node *t = head->rnh_treetop, *x;
    207  1.1   thorpej 	register caddr_t cp = v, cp2;
    208  1.1   thorpej 	caddr_t cplim;
    209  1.1   thorpej 	struct radix_node *saved_t, *top = t;
    210  1.1   thorpej 	int off = t->rn_off, vlen = *(u_char *)cp, matched_off;
    211  1.1   thorpej 	register int test, b, rn_b;
    212  1.1   thorpej 
    213  1.1   thorpej 	/*
    214  1.1   thorpej 	 * Open code rn_search(v, top) to avoid overhead of extra
    215  1.1   thorpej 	 * subroutine call.
    216  1.1   thorpej 	 */
    217  1.1   thorpej 	for (; t->rn_b >= 0; ) {
    218  1.1   thorpej 		if (t->rn_bmask & cp[t->rn_off])
    219  1.1   thorpej 			t = t->rn_r;
    220  1.1   thorpej 		else
    221  1.1   thorpej 			t = t->rn_l;
    222  1.1   thorpej 	}
    223  1.1   thorpej 	/*
    224  1.1   thorpej 	 * See if we match exactly as a host destination
    225  1.1   thorpej 	 * or at least learn how many bits match, for normal mask finesse.
    226  1.1   thorpej 	 *
    227  1.1   thorpej 	 * It doesn't hurt us to limit how many bytes to check
    228  1.1   thorpej 	 * to the length of the mask, since if it matches we had a genuine
    229  1.1   thorpej 	 * match and the leaf we have is the most specific one anyway;
    230  1.1   thorpej 	 * if it didn't match with a shorter length it would fail
    231  1.1   thorpej 	 * with a long one.  This wins big for class B&C netmasks which
    232  1.1   thorpej 	 * are probably the most common case...
    233  1.1   thorpej 	 */
    234  1.1   thorpej 	if (t->rn_mask)
    235  1.1   thorpej 		vlen = *(u_char *)t->rn_mask;
    236  1.1   thorpej 	cp += off; cp2 = t->rn_key + off; cplim = v + vlen;
    237  1.1   thorpej 	for (; cp < cplim; cp++, cp2++)
    238  1.1   thorpej 		if (*cp != *cp2)
    239  1.1   thorpej 			goto on1;
    240  1.1   thorpej 	/*
    241  1.1   thorpej 	 * This extra grot is in case we are explicitly asked
    242  1.1   thorpej 	 * to look up the default.  Ugh!
    243  1.3  christos 	 * Or 255.255.255.255
    244  1.3  christos 	 *
    245  1.3  christos 	 * In this case, we have a complete match of the key.  Unless
    246  1.3  christos 	 * the node is one of the roots, we are finished.
    247  1.3  christos 	 * If it is the zeros root, then take what we have, prefering
    248  1.3  christos 	 * any real data.
    249  1.3  christos 	 * If it is the ones root, then pretend the target key was followed
    250  1.3  christos 	 * by a byte of zeros.
    251  1.3  christos 	 */
    252  1.3  christos 	if (!(t->rn_flags & RNF_ROOT))
    253  1.3  christos 		return t;		/* not a root */
    254  1.3  christos 	if (t->rn_dupedkey) {
    255  1.1   thorpej 		t = t->rn_dupedkey;
    256  1.3  christos 		return t;		/* have some real data */
    257  1.3  christos 	}
    258  1.3  christos 	if (*(cp-1) == 0)
    259  1.3  christos 		return t;		/* not the ones root */
    260  1.3  christos 	b = 0;				/* fake a zero after 255.255.255.255 */
    261  1.3  christos 	goto on2;
    262  1.1   thorpej on1:
    263  1.1   thorpej 	test = (*cp ^ *cp2) & 0xff; /* find first bit that differs */
    264  1.1   thorpej 	for (b = 7; (test >>= 1) > 0;)
    265  1.1   thorpej 		b--;
    266  1.3  christos on2:
    267  1.1   thorpej 	matched_off = cp - v;
    268  1.1   thorpej 	b += matched_off << 3;
    269  1.1   thorpej 	rn_b = -1 - b;
    270  1.1   thorpej 	/*
    271  1.1   thorpej 	 * If there is a host route in a duped-key chain, it will be first.
    272  1.1   thorpej 	 */
    273  1.1   thorpej 	if ((saved_t = t)->rn_mask == 0)
    274  1.1   thorpej 		t = t->rn_dupedkey;
    275  1.1   thorpej 	for (; t; t = t->rn_dupedkey)
    276  1.1   thorpej 		/*
    277  1.1   thorpej 		 * Even if we don't match exactly as a host,
    278  1.1   thorpej 		 * we may match if the leaf we wound up at is
    279  1.1   thorpej 		 * a route to a net.
    280  1.1   thorpej 		 */
    281  1.1   thorpej 		if (t->rn_flags & RNF_NORMAL) {
    282  1.1   thorpej 			if (rn_b <= t->rn_b)
    283  1.1   thorpej 				return t;
    284  1.1   thorpej 		} else if (rn_satsifies_leaf(v, t, matched_off))
    285  1.1   thorpej 				return t;
    286  1.1   thorpej 	t = saved_t;
    287  1.1   thorpej 	/* start searching up the tree */
    288  1.1   thorpej 	do {
    289  1.1   thorpej 		register struct radix_mask *m;
    290  1.1   thorpej 		t = t->rn_p;
    291  1.2   thorpej 		if ((m = t->rn_mklist)) {
    292  1.1   thorpej 			/*
    293  1.1   thorpej 			 * If non-contiguous masks ever become important
    294  1.1   thorpej 			 * we can restore the masking and open coding of
    295  1.1   thorpej 			 * the search and satisfaction test and put the
    296  1.1   thorpej 			 * calculation of "off" back before the "do".
    297  1.1   thorpej 			 */
    298  1.1   thorpej 			do {
    299  1.1   thorpej 				if (m->rm_flags & RNF_NORMAL) {
    300  1.1   thorpej 					if (rn_b <= m->rm_b)
    301  1.1   thorpej 						return (m->rm_leaf);
    302  1.1   thorpej 				} else {
    303  1.1   thorpej 					off = min(t->rn_off, matched_off);
    304  1.1   thorpej 					x = rn_search_m(v, t, m->rm_mask);
    305  1.1   thorpej 					while (x && x->rn_mask != m->rm_mask)
    306  1.1   thorpej 						x = x->rn_dupedkey;
    307  1.1   thorpej 					if (x && rn_satsifies_leaf(v, x, off))
    308  1.1   thorpej 						    return x;
    309  1.1   thorpej 				}
    310  1.2   thorpej 			} while ((m = m->rm_mklist));
    311  1.1   thorpej 		}
    312  1.1   thorpej 	} while (t != top);
    313  1.1   thorpej 	return 0;
    314  1.1   thorpej }
    315  1.3  christos 
    316  1.1   thorpej #ifdef RN_DEBUG
    317  1.1   thorpej int	rn_nodenum;
    318  1.1   thorpej struct	radix_node *rn_clist;
    319  1.1   thorpej int	rn_saveinfo;
    320  1.1   thorpej int	rn_debug =  1;
    321  1.1   thorpej #endif
    322  1.1   thorpej 
    323  1.1   thorpej struct radix_node *
    324  1.3  christos rn_newpair(void *v, int b, struct radix_node nodes[2])
    325  1.1   thorpej {
    326  1.1   thorpej 	register struct radix_node *tt = nodes, *t = tt + 1;
    327  1.1   thorpej 	t->rn_b = b; t->rn_bmask = 0x80 >> (b & 7);
    328  1.1   thorpej 	t->rn_l = tt; t->rn_off = b >> 3;
    329  1.1   thorpej 	tt->rn_b = -1; tt->rn_key = (caddr_t)v; tt->rn_p = t;
    330  1.1   thorpej 	tt->rn_flags = t->rn_flags = RNF_ACTIVE;
    331  1.1   thorpej #ifdef RN_DEBUG
    332  1.1   thorpej 	tt->rn_info = rn_nodenum++; t->rn_info = rn_nodenum++;
    333  1.1   thorpej 	tt->rn_twin = t; tt->rn_ybro = rn_clist; rn_clist = tt;
    334  1.1   thorpej #endif
    335  1.1   thorpej 	return t;
    336  1.1   thorpej }
    337  1.1   thorpej 
    338  1.1   thorpej struct radix_node *
    339  1.3  christos rn_insert(void* v_arg,
    340  1.3  christos 	  struct radix_node_head *head,
    341  1.3  christos 	  int *dupentry,
    342  1.3  christos 	  struct radix_node nodes[2])
    343  1.1   thorpej {
    344  1.1   thorpej 	caddr_t v = v_arg;
    345  1.1   thorpej 	struct radix_node *top = head->rnh_treetop;
    346  1.1   thorpej 	int head_off = top->rn_off, vlen = (int)*((u_char *)v);
    347  1.1   thorpej 	register struct radix_node *t = rn_search(v_arg, top);
    348  1.1   thorpej 	register caddr_t cp = v + head_off;
    349  1.1   thorpej 	register int b;
    350  1.1   thorpej 	struct radix_node *tt;
    351  1.3  christos 
    352  1.3  christos 	/*
    353  1.1   thorpej 	 * Find first bit at which v and t->rn_key differ
    354  1.1   thorpej 	 */
    355  1.1   thorpej     {
    356  1.1   thorpej 	register caddr_t cp2 = t->rn_key + head_off;
    357  1.1   thorpej 	register int cmp_res;
    358  1.1   thorpej 	caddr_t cplim = v + vlen;
    359  1.1   thorpej 
    360  1.1   thorpej 	while (cp < cplim)
    361  1.1   thorpej 		if (*cp2++ != *cp++)
    362  1.1   thorpej 			goto on1;
    363  1.3  christos 	/* handle adding 255.255.255.255 */
    364  1.3  christos 	if (!(t->rn_flags & RNF_ROOT) || *(cp2-1) == 0) {
    365  1.3  christos 		*dupentry = 1;
    366  1.3  christos 		return t;
    367  1.3  christos 	}
    368  1.1   thorpej on1:
    369  1.1   thorpej 	*dupentry = 0;
    370  1.1   thorpej 	cmp_res = (cp[-1] ^ cp2[-1]) & 0xff;
    371  1.1   thorpej 	for (b = (cp - v) << 3; cmp_res; b--)
    372  1.1   thorpej 		cmp_res >>= 1;
    373  1.1   thorpej     }
    374  1.1   thorpej     {
    375  1.1   thorpej 	register struct radix_node *p, *x = top;
    376  1.1   thorpej 	cp = v;
    377  1.1   thorpej 	do {
    378  1.1   thorpej 		p = x;
    379  1.3  christos 		if (cp[x->rn_off] & x->rn_bmask)
    380  1.1   thorpej 			x = x->rn_r;
    381  1.1   thorpej 		else x = x->rn_l;
    382  1.1   thorpej 	} while (b > (unsigned) x->rn_b); /* x->rn_b < b && x->rn_b >= 0 */
    383  1.1   thorpej #ifdef RN_DEBUG
    384  1.1   thorpej 	if (rn_debug)
    385  1.1   thorpej 		log(LOG_DEBUG, "rn_insert: Going In:\n"), traverse(p);
    386  1.1   thorpej #endif
    387  1.1   thorpej 	t = rn_newpair(v_arg, b, nodes); tt = t->rn_l;
    388  1.1   thorpej 	if ((cp[p->rn_off] & p->rn_bmask) == 0)
    389  1.1   thorpej 		p->rn_l = t;
    390  1.1   thorpej 	else
    391  1.1   thorpej 		p->rn_r = t;
    392  1.1   thorpej 	x->rn_p = t; t->rn_p = p; /* frees x, p as temp vars below */
    393  1.1   thorpej 	if ((cp[t->rn_off] & t->rn_bmask) == 0) {
    394  1.1   thorpej 		t->rn_r = x;
    395  1.1   thorpej 	} else {
    396  1.1   thorpej 		t->rn_r = tt; t->rn_l = x;
    397  1.1   thorpej 	}
    398  1.1   thorpej #ifdef RN_DEBUG
    399  1.1   thorpej 	if (rn_debug)
    400  1.1   thorpej 		log(LOG_DEBUG, "rn_insert: Coming Out:\n"), traverse(p);
    401  1.1   thorpej #endif
    402  1.1   thorpej     }
    403  1.1   thorpej 	return (tt);
    404  1.1   thorpej }
    405  1.1   thorpej 
    406  1.1   thorpej struct radix_node *
    407  1.3  christos rn_addmask(void *n_arg, int search, int skip)
    408  1.1   thorpej {
    409  1.1   thorpej 	caddr_t netmask = (caddr_t)n_arg;
    410  1.1   thorpej 	register struct radix_node *x;
    411  1.1   thorpej 	register caddr_t cp, cplim;
    412  1.1   thorpej 	register int b = 0, mlen, j;
    413  1.1   thorpej 	int maskduplicated, m0, isnormal;
    414  1.1   thorpej 	struct radix_node *saved_x;
    415  1.1   thorpej 	static int last_zeroed = 0;
    416  1.1   thorpej 
    417  1.1   thorpej 	if ((mlen = *(u_char *)netmask) > max_keylen)
    418  1.1   thorpej 		mlen = max_keylen;
    419  1.1   thorpej 	if (skip == 0)
    420  1.1   thorpej 		skip = 1;
    421  1.1   thorpej 	if (mlen <= skip)
    422  1.1   thorpej 		return (mask_rnhead->rnh_nodes);
    423  1.1   thorpej 	if (skip > 1)
    424  1.1   thorpej 		Bcopy(rn_ones + 1, addmask_key + 1, skip - 1);
    425  1.1   thorpej 	if ((m0 = mlen) > skip)
    426  1.1   thorpej 		Bcopy(netmask + skip, addmask_key + skip, mlen - skip);
    427  1.1   thorpej 	/*
    428  1.1   thorpej 	 * Trim trailing zeroes.
    429  1.1   thorpej 	 */
    430  1.1   thorpej 	for (cp = addmask_key + mlen; (cp > addmask_key) && cp[-1] == 0;)
    431  1.1   thorpej 		cp--;
    432  1.1   thorpej 	mlen = cp - addmask_key;
    433  1.1   thorpej 	if (mlen <= skip) {
    434  1.1   thorpej 		if (m0 >= last_zeroed)
    435  1.1   thorpej 			last_zeroed = mlen;
    436  1.1   thorpej 		return (mask_rnhead->rnh_nodes);
    437  1.1   thorpej 	}
    438  1.1   thorpej 	if (m0 < last_zeroed)
    439  1.1   thorpej 		Bzero(addmask_key + m0, last_zeroed - m0);
    440  1.1   thorpej 	*addmask_key = last_zeroed = mlen;
    441  1.1   thorpej 	x = rn_search(addmask_key, rn_masktop);
    442  1.1   thorpej 	if (Bcmp(addmask_key, x->rn_key, mlen) != 0)
    443  1.1   thorpej 		x = 0;
    444  1.1   thorpej 	if (x || search)
    445  1.1   thorpej 		return (x);
    446  1.1   thorpej 	R_Malloc(x, struct radix_node *, max_keylen + 2 * sizeof (*x));
    447  1.1   thorpej 	if ((saved_x = x) == 0)
    448  1.1   thorpej 		return (0);
    449  1.1   thorpej 	Bzero(x, max_keylen + 2 * sizeof (*x));
    450  1.1   thorpej 	netmask = cp = (caddr_t)(x + 2);
    451  1.1   thorpej 	Bcopy(addmask_key, cp, mlen);
    452  1.1   thorpej 	x = rn_insert(cp, mask_rnhead, &maskduplicated, x);
    453  1.1   thorpej 	if (maskduplicated) {
    454  1.1   thorpej 		log(LOG_ERR, "rn_addmask: mask impossibly already in tree");
    455  1.1   thorpej 		Free(saved_x);
    456  1.1   thorpej 		return (x);
    457  1.1   thorpej 	}
    458  1.1   thorpej 	/*
    459  1.1   thorpej 	 * Calculate index of mask, and check for normalcy.
    460  1.1   thorpej 	 */
    461  1.1   thorpej 	cplim = netmask + mlen; isnormal = 1;
    462  1.1   thorpej 	for (cp = netmask + skip; (cp < cplim) && *(u_char *)cp == 0xff;)
    463  1.1   thorpej 		cp++;
    464  1.1   thorpej 	if (cp != cplim) {
    465  1.3  christos 		for (j = 0x80; (j & *cp) != 0; j >>= 1)
    466  1.1   thorpej 			b++;
    467  1.1   thorpej 		if (*cp != normal_chars[b] || cp != (cplim - 1))
    468  1.1   thorpej 			isnormal = 0;
    469  1.1   thorpej 	}
    470  1.1   thorpej 	b += (cp - netmask) << 3;
    471  1.1   thorpej 	x->rn_b = -1 - b;
    472  1.1   thorpej 	if (isnormal)
    473  1.1   thorpej 		x->rn_flags |= RNF_NORMAL;
    474  1.1   thorpej 	return (x);
    475  1.1   thorpej }
    476  1.1   thorpej 
    477  1.1   thorpej static int	/* XXX: arbitrary ordering for non-contiguous masks */
    478  1.1   thorpej rn_lexobetter(void *m_arg, void *n_arg)
    479  1.1   thorpej {
    480  1.1   thorpej 	register u_char *mp = m_arg, *np = n_arg, *lim;
    481  1.1   thorpej 
    482  1.1   thorpej 	if (*mp > *np)
    483  1.1   thorpej 		return 1;  /* not really, but need to check longer one first */
    484  1.3  christos 	if (*mp == *np)
    485  1.1   thorpej 		for (lim = mp + *mp; mp < lim;)
    486  1.1   thorpej 			if (*mp++ > *np++)
    487  1.1   thorpej 				return 1;
    488  1.1   thorpej 	return 0;
    489  1.1   thorpej }
    490  1.1   thorpej 
    491  1.1   thorpej static struct radix_mask *
    492  1.1   thorpej rn_new_radix_mask(register struct radix_node *tt,
    493  1.1   thorpej 		  register struct radix_mask *next)
    494  1.1   thorpej {
    495  1.1   thorpej 	register struct radix_mask *m;
    496  1.1   thorpej 
    497  1.1   thorpej 	MKGet(m);
    498  1.1   thorpej 	if (m == 0) {
    499  1.1   thorpej 		log(LOG_ERR, "Mask for route not entered\n");
    500  1.1   thorpej 		return (0);
    501  1.1   thorpej 	}
    502  1.1   thorpej 	Bzero(m, sizeof *m);
    503  1.1   thorpej 	m->rm_b = tt->rn_b;
    504  1.1   thorpej 	m->rm_flags = tt->rn_flags;
    505  1.1   thorpej 	if (tt->rn_flags & RNF_NORMAL)
    506  1.1   thorpej 		m->rm_leaf = tt;
    507  1.1   thorpej 	else
    508  1.1   thorpej 		m->rm_mask = tt->rn_mask;
    509  1.1   thorpej 	m->rm_mklist = next;
    510  1.1   thorpej 	tt->rn_mklist = m;
    511  1.1   thorpej 	return m;
    512  1.1   thorpej }
    513  1.1   thorpej 
    514  1.1   thorpej struct radix_node *
    515  1.3  christos rn_addroute(void *v_arg,
    516  1.3  christos 	    void *n_arg,
    517  1.3  christos 	    struct radix_node_head *head,
    518  1.3  christos 	    struct radix_node treenodes[2])
    519  1.1   thorpej {
    520  1.1   thorpej 	caddr_t v = (caddr_t)v_arg, netmask = (caddr_t)n_arg;
    521  1.3  christos 	register struct radix_node *t, *x = 0, *tt;
    522  1.1   thorpej 	struct radix_node *saved_tt, *top = head->rnh_treetop;
    523  1.3  christos 	short b = 0, b_leaf = 0;
    524  1.1   thorpej 	int keyduplicated;
    525  1.1   thorpej 	caddr_t mmask;
    526  1.1   thorpej 	struct radix_mask *m, **mp;
    527  1.1   thorpej 
    528  1.1   thorpej 	/*
    529  1.1   thorpej 	 * In dealing with non-contiguous masks, there may be
    530  1.1   thorpej 	 * many different routes which have the same mask.
    531  1.1   thorpej 	 * We will find it useful to have a unique pointer to
    532  1.1   thorpej 	 * the mask to speed avoiding duplicate references at
    533  1.1   thorpej 	 * nodes and possibly save time in calculating indices.
    534  1.1   thorpej 	 */
    535  1.1   thorpej 	if (netmask)  {
    536  1.1   thorpej 		if ((x = rn_addmask(netmask, 0, top->rn_off)) == 0)
    537  1.1   thorpej 			return (0);
    538  1.1   thorpej 		b_leaf = x->rn_b;
    539  1.1   thorpej 		b = -1 - x->rn_b;
    540  1.1   thorpej 		netmask = x->rn_key;
    541  1.1   thorpej 	}
    542  1.1   thorpej 	/*
    543  1.1   thorpej 	 * Deal with duplicated keys: attach node to previous instance
    544  1.1   thorpej 	 */
    545  1.1   thorpej 	saved_tt = tt = rn_insert(v, head, &keyduplicated, treenodes);
    546  1.1   thorpej 	if (keyduplicated) {
    547  1.1   thorpej 		for (t = tt; tt; t = tt, tt = tt->rn_dupedkey) {
    548  1.1   thorpej 			if (tt->rn_mask == netmask)
    549  1.1   thorpej 				return (0);
    550  1.1   thorpej 			if (netmask == 0 ||
    551  1.1   thorpej 			    (tt->rn_mask &&
    552  1.1   thorpej 			     ((b_leaf < tt->rn_b) || /* index(netmask) > node */
    553  1.1   thorpej 			       rn_refines(netmask, tt->rn_mask) ||
    554  1.1   thorpej 			       rn_lexobetter(netmask, tt->rn_mask))))
    555  1.1   thorpej 				break;
    556  1.1   thorpej 		}
    557  1.1   thorpej 		/*
    558  1.1   thorpej 		 * If the mask is not duplicated, we wouldn't
    559  1.1   thorpej 		 * find it among possible duplicate key entries
    560  1.1   thorpej 		 * anyway, so the above test doesn't hurt.
    561  1.1   thorpej 		 *
    562  1.1   thorpej 		 * We sort the masks for a duplicated key the same way as
    563  1.1   thorpej 		 * in a masklist -- most specific to least specific.
    564  1.1   thorpej 		 * This may require the unfortunate nuisance of relocating
    565  1.1   thorpej 		 * the head of the list.
    566  1.1   thorpej 		 */
    567  1.1   thorpej 		if (tt == saved_tt) {
    568  1.1   thorpej 			struct	radix_node *xx = x;
    569  1.1   thorpej 			/* link in at head of list */
    570  1.1   thorpej 			(tt = treenodes)->rn_dupedkey = t;
    571  1.1   thorpej 			tt->rn_flags = t->rn_flags;
    572  1.1   thorpej 			tt->rn_p = x = t->rn_p;
    573  1.1   thorpej 			if (x->rn_l == t) x->rn_l = tt; else x->rn_r = tt;
    574  1.1   thorpej 			saved_tt = tt; x = xx;
    575  1.1   thorpej 		} else {
    576  1.1   thorpej 			(tt = treenodes)->rn_dupedkey = t->rn_dupedkey;
    577  1.1   thorpej 			t->rn_dupedkey = tt;
    578  1.1   thorpej 		}
    579  1.1   thorpej #ifdef RN_DEBUG
    580  1.1   thorpej 		t=tt+1; tt->rn_info = rn_nodenum++; t->rn_info = rn_nodenum++;
    581  1.1   thorpej 		tt->rn_twin = t; tt->rn_ybro = rn_clist; rn_clist = tt;
    582  1.1   thorpej #endif
    583  1.1   thorpej 		tt->rn_key = (caddr_t) v;
    584  1.1   thorpej 		tt->rn_b = -1;
    585  1.1   thorpej 		tt->rn_flags = RNF_ACTIVE;
    586  1.1   thorpej 	}
    587  1.1   thorpej 	/*
    588  1.1   thorpej 	 * Put mask in tree.
    589  1.1   thorpej 	 */
    590  1.1   thorpej 	if (netmask) {
    591  1.1   thorpej 		tt->rn_mask = netmask;
    592  1.1   thorpej 		tt->rn_b = x->rn_b;
    593  1.1   thorpej 		tt->rn_flags |= x->rn_flags & RNF_NORMAL;
    594  1.1   thorpej 	}
    595  1.1   thorpej 	t = saved_tt->rn_p;
    596  1.1   thorpej 	if (keyduplicated)
    597  1.1   thorpej 		goto on2;
    598  1.1   thorpej 	b_leaf = -1 - t->rn_b;
    599  1.1   thorpej 	if (t->rn_r == saved_tt) x = t->rn_l; else x = t->rn_r;
    600  1.1   thorpej 	/* Promote general routes from below */
    601  1.3  christos 	if (x->rn_b < 0) {
    602  1.1   thorpej 	    for (mp = &t->rn_mklist; x; x = x->rn_dupedkey)
    603  1.1   thorpej 		if (x->rn_mask && (x->rn_b >= b_leaf) && x->rn_mklist == 0) {
    604  1.2   thorpej 			if ((*mp = m = rn_new_radix_mask(x, 0)))
    605  1.1   thorpej 				mp = &m->rm_mklist;
    606  1.1   thorpej 		}
    607  1.1   thorpej 	} else if (x->rn_mklist) {
    608  1.1   thorpej 		/*
    609  1.1   thorpej 		 * Skip over masks whose index is > that of new node
    610  1.1   thorpej 		 */
    611  1.2   thorpej 		for (mp = &x->rn_mklist; (m = *mp); mp = &m->rm_mklist)
    612  1.1   thorpej 			if (m->rm_b >= b_leaf)
    613  1.1   thorpej 				break;
    614  1.1   thorpej 		t->rn_mklist = m; *mp = 0;
    615  1.1   thorpej 	}
    616  1.1   thorpej on2:
    617  1.1   thorpej 	/* Add new route to highest possible ancestor's list */
    618  1.1   thorpej 	if ((netmask == 0) || (b > t->rn_b ))
    619  1.1   thorpej 		return tt; /* can't lift at all */
    620  1.1   thorpej 	b_leaf = tt->rn_b;
    621  1.1   thorpej 	do {
    622  1.1   thorpej 		x = t;
    623  1.1   thorpej 		t = t->rn_p;
    624  1.1   thorpej 	} while (b <= t->rn_b && x != top);
    625  1.1   thorpej 	/*
    626  1.1   thorpej 	 * Search through routes associated with node to
    627  1.1   thorpej 	 * insert new route according to index.
    628  1.1   thorpej 	 * Need same criteria as when sorting dupedkeys to avoid
    629  1.1   thorpej 	 * double loop on deletion.
    630  1.1   thorpej 	 */
    631  1.2   thorpej 	for (mp = &x->rn_mklist; (m = *mp); mp = &m->rm_mklist) {
    632  1.1   thorpej 		if (m->rm_b < b_leaf)
    633  1.1   thorpej 			continue;
    634  1.1   thorpej 		if (m->rm_b > b_leaf)
    635  1.1   thorpej 			break;
    636  1.1   thorpej 		if (m->rm_flags & RNF_NORMAL) {
    637  1.1   thorpej 			mmask = m->rm_leaf->rn_mask;
    638  1.1   thorpej 			if (tt->rn_flags & RNF_NORMAL) {
    639  1.1   thorpej 				log(LOG_ERR,
    640  1.1   thorpej 				   "Non-unique normal route, mask not entered");
    641  1.1   thorpej 				return tt;
    642  1.1   thorpej 			}
    643  1.1   thorpej 		} else
    644  1.1   thorpej 			mmask = m->rm_mask;
    645  1.1   thorpej 		if (mmask == netmask) {
    646  1.1   thorpej 			m->rm_refs++;
    647  1.1   thorpej 			tt->rn_mklist = m;
    648  1.1   thorpej 			return tt;
    649  1.1   thorpej 		}
    650  1.1   thorpej 		if (rn_refines(netmask, mmask) || rn_lexobetter(netmask, mmask))
    651  1.1   thorpej 			break;
    652  1.1   thorpej 	}
    653  1.1   thorpej 	*mp = rn_new_radix_mask(tt, *mp);
    654  1.1   thorpej 	return tt;
    655  1.1   thorpej }
    656  1.1   thorpej 
    657  1.1   thorpej struct radix_node *
    658  1.3  christos rn_delete(void *v_arg,
    659  1.3  christos 	  void *netmask_arg,
    660  1.3  christos 	  struct radix_node_head *head)
    661  1.1   thorpej {
    662  1.1   thorpej 	register struct radix_node *t, *p, *x, *tt;
    663  1.1   thorpej 	struct radix_mask *m, *saved_m, **mp;
    664  1.1   thorpej 	struct radix_node *dupedkey, *saved_tt, *top;
    665  1.1   thorpej 	caddr_t v, netmask;
    666  1.1   thorpej 	int b, head_off, vlen;
    667  1.1   thorpej 
    668  1.1   thorpej 	v = v_arg;
    669  1.1   thorpej 	netmask = netmask_arg;
    670  1.1   thorpej 	x = head->rnh_treetop;
    671  1.1   thorpej 	tt = rn_search(v, x);
    672  1.1   thorpej 	head_off = x->rn_off;
    673  1.1   thorpej 	vlen =  *(u_char *)v;
    674  1.1   thorpej 	saved_tt = tt;
    675  1.1   thorpej 	top = x;
    676  1.1   thorpej 	if (tt == 0 ||
    677  1.1   thorpej 	    Bcmp(v + head_off, tt->rn_key + head_off, vlen - head_off))
    678  1.1   thorpej 		return (0);
    679  1.1   thorpej 	/*
    680  1.1   thorpej 	 * Delete our route from mask lists.
    681  1.1   thorpej 	 */
    682  1.1   thorpej 	if (netmask) {
    683  1.1   thorpej 		if ((x = rn_addmask(netmask, 1, head_off)) == 0)
    684  1.1   thorpej 			return (0);
    685  1.1   thorpej 		netmask = x->rn_key;
    686  1.1   thorpej 		while (tt->rn_mask != netmask)
    687  1.1   thorpej 			if ((tt = tt->rn_dupedkey) == 0)
    688  1.1   thorpej 				return (0);
    689  1.1   thorpej 	}
    690  1.1   thorpej 	if (tt->rn_mask == 0 || (saved_m = m = tt->rn_mklist) == 0)
    691  1.1   thorpej 		goto on1;
    692  1.1   thorpej 	if (tt->rn_flags & RNF_NORMAL) {
    693  1.1   thorpej 		if (m->rm_leaf != tt || m->rm_refs > 0) {
    694  1.1   thorpej 			log(LOG_ERR, "rn_delete: inconsistent annotation\n");
    695  1.1   thorpej 			return 0;  /* dangling ref could cause disaster */
    696  1.1   thorpej 		}
    697  1.3  christos 	} else {
    698  1.1   thorpej 		if (m->rm_mask != tt->rn_mask) {
    699  1.1   thorpej 			log(LOG_ERR, "rn_delete: inconsistent annotation\n");
    700  1.1   thorpej 			goto on1;
    701  1.1   thorpej 		}
    702  1.1   thorpej 		if (--m->rm_refs >= 0)
    703  1.1   thorpej 			goto on1;
    704  1.1   thorpej 	}
    705  1.1   thorpej 	b = -1 - tt->rn_b;
    706  1.1   thorpej 	t = saved_tt->rn_p;
    707  1.1   thorpej 	if (b > t->rn_b)
    708  1.1   thorpej 		goto on1; /* Wasn't lifted at all */
    709  1.1   thorpej 	do {
    710  1.1   thorpej 		x = t;
    711  1.1   thorpej 		t = t->rn_p;
    712  1.1   thorpej 	} while (b <= t->rn_b && x != top);
    713  1.2   thorpej 	for (mp = &x->rn_mklist; (m = *mp); mp = &m->rm_mklist)
    714  1.1   thorpej 		if (m == saved_m) {
    715  1.1   thorpej 			*mp = m->rm_mklist;
    716  1.1   thorpej 			MKFree(m);
    717  1.1   thorpej 			break;
    718  1.1   thorpej 		}
    719  1.1   thorpej 	if (m == 0) {
    720  1.1   thorpej 		log(LOG_ERR, "rn_delete: couldn't find our annotation\n");
    721  1.1   thorpej 		if (tt->rn_flags & RNF_NORMAL)
    722  1.1   thorpej 			return (0); /* Dangling ref to us */
    723  1.1   thorpej 	}
    724  1.1   thorpej on1:
    725  1.1   thorpej 	/*
    726  1.1   thorpej 	 * Eliminate us from tree
    727  1.1   thorpej 	 */
    728  1.1   thorpej 	if (tt->rn_flags & RNF_ROOT)
    729  1.1   thorpej 		return (0);
    730  1.1   thorpej #ifdef RN_DEBUG
    731  1.1   thorpej 	/* Get us out of the creation list */
    732  1.1   thorpej 	for (t = rn_clist; t && t->rn_ybro != tt; t = t->rn_ybro) {}
    733  1.1   thorpej 	if (t) t->rn_ybro = tt->rn_ybro;
    734  1.1   thorpej #endif
    735  1.1   thorpej 	t = tt->rn_p;
    736  1.2   thorpej 	if ((dupedkey = saved_tt->rn_dupedkey)) {
    737  1.1   thorpej 		if (tt == saved_tt) {
    738  1.1   thorpej 			x = dupedkey; x->rn_p = t;
    739  1.1   thorpej 			if (t->rn_l == tt) t->rn_l = x; else t->rn_r = x;
    740  1.1   thorpej 		} else {
    741  1.1   thorpej 			for (x = p = saved_tt; p && p->rn_dupedkey != tt;)
    742  1.1   thorpej 				p = p->rn_dupedkey;
    743  1.1   thorpej 			if (p) p->rn_dupedkey = tt->rn_dupedkey;
    744  1.1   thorpej 			else log(LOG_ERR, "rn_delete: couldn't find us\n");
    745  1.1   thorpej 		}
    746  1.1   thorpej 		t = tt + 1;
    747  1.1   thorpej 		if  (t->rn_flags & RNF_ACTIVE) {
    748  1.1   thorpej #ifndef RN_DEBUG
    749  1.1   thorpej 			*++x = *t; p = t->rn_p;
    750  1.1   thorpej #else
    751  1.1   thorpej 			b = t->rn_info; *++x = *t; t->rn_info = b; p = t->rn_p;
    752  1.1   thorpej #endif
    753  1.1   thorpej 			if (p->rn_l == t) p->rn_l = x; else p->rn_r = x;
    754  1.1   thorpej 			x->rn_l->rn_p = x; x->rn_r->rn_p = x;
    755  1.1   thorpej 		}
    756  1.1   thorpej 		goto out;
    757  1.1   thorpej 	}
    758  1.1   thorpej 	if (t->rn_l == tt) x = t->rn_r; else x = t->rn_l;
    759  1.1   thorpej 	p = t->rn_p;
    760  1.1   thorpej 	if (p->rn_r == t) p->rn_r = x; else p->rn_l = x;
    761  1.1   thorpej 	x->rn_p = p;
    762  1.1   thorpej 	/*
    763  1.1   thorpej 	 * Demote routes attached to us.
    764  1.1   thorpej 	 */
    765  1.1   thorpej 	if (t->rn_mklist) {
    766  1.1   thorpej 		if (x->rn_b >= 0) {
    767  1.2   thorpej 			for (mp = &x->rn_mklist; (m = *mp);)
    768  1.1   thorpej 				mp = &m->rm_mklist;
    769  1.1   thorpej 			*mp = t->rn_mklist;
    770  1.1   thorpej 		} else {
    771  1.1   thorpej 			/* If there are any key,mask pairs in a sibling
    772  1.1   thorpej 			   duped-key chain, some subset will appear sorted
    773  1.1   thorpej 			   in the same order attached to our mklist */
    774  1.1   thorpej 			for (m = t->rn_mklist; m && x; x = x->rn_dupedkey)
    775  1.1   thorpej 				if (m == x->rn_mklist) {
    776  1.1   thorpej 					struct radix_mask *mm = m->rm_mklist;
    777  1.1   thorpej 					x->rn_mklist = 0;
    778  1.1   thorpej 					if (--(m->rm_refs) < 0)
    779  1.1   thorpej 						MKFree(m);
    780  1.1   thorpej 					m = mm;
    781  1.1   thorpej 				}
    782  1.1   thorpej 			if (m)
    783  1.2   thorpej 				syslog(LOG_ERR, "%s %lx at %lx\n",
    784  1.3  christos 				       "rn_delete: Orphaned Mask",
    785  1.3  christos 				       (unsigned long)m,
    786  1.3  christos 				       (unsigned long)x);
    787  1.1   thorpej 		}
    788  1.1   thorpej 	}
    789  1.1   thorpej 	/*
    790  1.1   thorpej 	 * We may be holding an active internal node in the tree.
    791  1.1   thorpej 	 */
    792  1.1   thorpej 	x = tt + 1;
    793  1.1   thorpej 	if (t != x) {
    794  1.1   thorpej #ifndef RN_DEBUG
    795  1.1   thorpej 		*t = *x;
    796  1.1   thorpej #else
    797  1.1   thorpej 		b = t->rn_info; *t = *x; t->rn_info = b;
    798  1.1   thorpej #endif
    799  1.1   thorpej 		t->rn_l->rn_p = t; t->rn_r->rn_p = t;
    800  1.1   thorpej 		p = x->rn_p;
    801  1.1   thorpej 		if (p->rn_l == x) p->rn_l = t; else p->rn_r = t;
    802  1.1   thorpej 	}
    803  1.1   thorpej out:
    804  1.1   thorpej 	tt->rn_flags &= ~RNF_ACTIVE;
    805  1.1   thorpej 	tt[1].rn_flags &= ~RNF_ACTIVE;
    806  1.1   thorpej 	return (tt);
    807  1.1   thorpej }
    808  1.1   thorpej 
    809  1.1   thorpej int
    810  1.3  christos rn_walktree(struct radix_node_head *h,
    811  1.3  christos 	    register int (*f)(struct radix_node *, struct walkarg *),
    812  1.3  christos 	    struct walkarg *w)
    813  1.1   thorpej {
    814  1.1   thorpej 	int error;
    815  1.1   thorpej 	struct radix_node *base, *next;
    816  1.1   thorpej 	register struct radix_node *rn = h->rnh_treetop;
    817  1.1   thorpej 	/*
    818  1.1   thorpej 	 * This gets complicated because we may delete the node
    819  1.1   thorpej 	 * while applying the function f to it, so we need to calculate
    820  1.1   thorpej 	 * the successor node in advance.
    821  1.1   thorpej 	 */
    822  1.1   thorpej 	/* First time through node, go left */
    823  1.1   thorpej 	while (rn->rn_b >= 0)
    824  1.1   thorpej 		rn = rn->rn_l;
    825  1.1   thorpej 	for (;;) {
    826  1.1   thorpej 		base = rn;
    827  1.1   thorpej 		/* If at right child go back up, otherwise, go right */
    828  1.1   thorpej 		while (rn->rn_p->rn_r == rn && (rn->rn_flags & RNF_ROOT) == 0)
    829  1.1   thorpej 			rn = rn->rn_p;
    830  1.1   thorpej 		/* Find the next *leaf* since next node might vanish, too */
    831  1.1   thorpej 		for (rn = rn->rn_p->rn_r; rn->rn_b >= 0;)
    832  1.1   thorpej 			rn = rn->rn_l;
    833  1.1   thorpej 		next = rn;
    834  1.1   thorpej 		/* Process leaves */
    835  1.2   thorpej 		while ((rn = base)) {
    836  1.1   thorpej 			base = rn->rn_dupedkey;
    837  1.1   thorpej 			if (!(rn->rn_flags & RNF_ROOT) && (error = (*f)(rn, w)))
    838  1.1   thorpej 				return (error);
    839  1.1   thorpej 		}
    840  1.1   thorpej 		rn = next;
    841  1.1   thorpej 		if (rn->rn_flags & RNF_ROOT)
    842  1.1   thorpej 			return (0);
    843  1.1   thorpej 	}
    844  1.1   thorpej 	/* NOTREACHED */
    845  1.1   thorpej }
    846  1.1   thorpej 
    847  1.1   thorpej int
    848  1.3  christos rn_inithead(void **head, int off)
    849  1.1   thorpej {
    850  1.1   thorpej 	register struct radix_node_head *rnh;
    851  1.1   thorpej 	register struct radix_node *t, *tt, *ttt;
    852  1.1   thorpej 	if (*head)
    853  1.1   thorpej 		return (1);
    854  1.1   thorpej 	R_Malloc(rnh, struct radix_node_head *, sizeof (*rnh));
    855  1.1   thorpej 	if (rnh == 0)
    856  1.1   thorpej 		return (0);
    857  1.1   thorpej 	Bzero(rnh, sizeof (*rnh));
    858  1.1   thorpej 	*head = rnh;
    859  1.1   thorpej 	t = rn_newpair(rn_zeros, off, rnh->rnh_nodes);
    860  1.1   thorpej 	ttt = rnh->rnh_nodes + 2;
    861  1.1   thorpej 	t->rn_r = ttt;
    862  1.1   thorpej 	t->rn_p = t;
    863  1.1   thorpej 	tt = t->rn_l;
    864  1.1   thorpej 	tt->rn_flags = t->rn_flags = RNF_ROOT | RNF_ACTIVE;
    865  1.1   thorpej 	tt->rn_b = -1 - off;
    866  1.1   thorpej 	*ttt = *tt;
    867  1.1   thorpej 	ttt->rn_key = rn_ones;
    868  1.1   thorpej 	rnh->rnh_addaddr = rn_addroute;
    869  1.1   thorpej 	rnh->rnh_deladdr = rn_delete;
    870  1.1   thorpej 	rnh->rnh_matchaddr = rn_match;
    871  1.1   thorpej 	rnh->rnh_lookup = rn_lookup;
    872  1.1   thorpej 	rnh->rnh_walktree = rn_walktree;
    873  1.1   thorpej 	rnh->rnh_treetop = t;
    874  1.1   thorpej 	return (1);
    875  1.1   thorpej }
    876  1.1   thorpej 
    877  1.1   thorpej void
    878  1.3  christos rn_init(void)
    879  1.1   thorpej {
    880  1.1   thorpej 	char *cp, *cplim;
    881  1.1   thorpej 	if (max_keylen == 0) {
    882  1.1   thorpej 		printf("rn_init: radix functions require max_keylen be set\n");
    883  1.1   thorpej 		return;
    884  1.1   thorpej 	}
    885  1.1   thorpej 	R_Malloc(rn_zeros, char *, 3 * max_keylen);
    886  1.1   thorpej 	if (rn_zeros == NULL)
    887  1.1   thorpej 		panic("rn_init");
    888  1.1   thorpej 	Bzero(rn_zeros, 3 * max_keylen);
    889  1.1   thorpej 	rn_ones = cp = rn_zeros + max_keylen;
    890  1.1   thorpej 	addmask_key = cplim = rn_ones + max_keylen;
    891  1.1   thorpej 	while (cp < cplim)
    892  1.1   thorpej 		*cp++ = -1;
    893  1.1   thorpej 	if (rn_inithead((void **)&mask_rnhead, 0) == 0)
    894  1.1   thorpej 		panic("rn_init 2");
    895  1.1   thorpej }
    896  1.3  christos 
    897