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