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