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