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