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