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