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