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