radix.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
3  *
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.
7  */
8 
9 /*
10  * Copyright (c) 1988, 1989, 1993
11  * The Regents of the University of California. All rights reserved.
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.
21  * 3. Neither the name of the University nor the names of its contributors
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  *
37  * @(#)radix.c 8.4 (Berkeley) 11/2/94
38  */
39 
40 /*
41  * DEBUG: section 53 Radix Tree data structure implementation
42  */
43 
44 #include "squid.h"
45 #include "radix.h"
46 #include "util.h"
47 
48 #if HAVE_UNISTD_H
49 #include <unistd.h>
50 #endif
51 #if HAVE_STDLIB_H
52 #include <stdlib.h>
53 #endif
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>
68 #endif
69 #if HAVE_GNUMALLOC_H
70 #include <gnumalloc.h>
71 #elif HAVE_MALLOC_H
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
80 #if HAVE_ASSERT_H
81 #include <assert.h>
82 #endif
83 
87 static char *addmask_key;
88 static unsigned char normal_chars[] = {0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xFF};
89 static char *rn_zeros, *rn_ones;
90 
91 /* aliases */
92 #define rn_masktop (squid_mask_rnhead->rnh_treetop)
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
98 #define rm_leaf rm_rmu.rmu_leaf /* extra field would make 32 bytes */
99 
100 /* Helper macros */
101 #define squid_Bcmp(a, b, l) (l == 0 ? 0 : memcmp((caddr_t)(a), (caddr_t)(b), (u_long)l))
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) {\
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  }
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
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).
132  *
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.
139  *
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
143  * node as high in the tree as we can go.
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 
151 struct squid_radix_node *
152 squid_rn_search(void *v_arg, struct squid_radix_node *head) {
153  register struct squid_radix_node *x;
154  register caddr_t v;
155 
156  for (x = head, v = v_arg; x->rn_b >= 0;) {
157  if (x->rn_bmask & v[x->rn_off])
158  x = x->rn_r;
159  else
160  x = x->rn_l;
161  }
162  return (x);
163 }
164 
165 struct squid_radix_node *
166 squid_rn_search_m(void *v_arg, struct squid_radix_node *head, void *m_arg) {
167  register struct squid_radix_node *x;
168  register caddr_t v = v_arg, m = m_arg;
169 
170  for (x = head; x->rn_b >= 0;) {
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;
176  }
177  return x;
178 }
179 
180 int
181 squid_rn_refines(void *m_arg, void *n_arg)
182 {
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)
189  lim -= longer;
190  while (n < lim) {
191  if (*n & ~(*m))
192  return 0;
193  if (*n++ != *m++)
194  masks_are_equal = 0;
195  }
196  while (n < lim2)
197  if (*n++)
198  return 0;
199  if (masks_are_equal && (longer < 0))
200  for (lim2 = m - longer; m < lim2;)
201  if (*m++)
202  return 1;
203  return (!masks_are_equal);
204 }
205 
206 struct squid_radix_node *
207 squid_rn_lookup(void *v_arg, void *m_arg, struct squid_radix_node_head *head) {
208  register struct squid_radix_node *x;
209  caddr_t netmask = 0;
210 
211  if (m_arg) {
212  if ((x = squid_rn_addmask(m_arg, 1, head->rnh_treetop->rn_off)) == 0)
213  return (0);
214  netmask = x->rn_key;
215  }
216  x = squid_rn_match(v_arg, head);
217  if (x && netmask) {
218  while (x && x->rn_mask != netmask)
219  x = x->rn_dupedkey;
220  }
221  return x;
222 }
223 
224 static int
225 rn_satsifies_leaf(char *trial, register struct squid_radix_node *leaf, int skip)
226 {
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)
232  cp3 = rn_ones;
233  else
234  length = min(length, *(u_char *) cp3);
235  cplim = cp + length;
236  cp3 += skip;
237  cp2 += skip;
238  for (cp += skip; cp < cplim; cp++, cp2++, cp3++)
239  if ((*cp ^ *cp2) & *cp3)
240  return 0;
241  return 1;
242 }
243 
244 struct squid_radix_node *
246  caddr_t v = v_arg;
247  register struct squid_radix_node *t = head->rnh_treetop, *x;
248  register caddr_t cp = v, cp2;
249  caddr_t cplim;
250  struct squid_radix_node *saved_t, *top = t;
251  int off = t->rn_off, vlen = *(u_char *) cp, matched_off;
252  register int test, b, rn_b;
253 
254  /*
255  * Open code squid_rn_search(v, top) to avoid overhead of extra
256  * subroutine call.
257  */
258  for (; t->rn_b >= 0;) {
259  if (t->rn_bmask & cp[t->rn_off])
260  t = t->rn_r;
261  else
262  t = t->rn_l;
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)
276  vlen = *(u_char *) t->rn_mask;
277  cp += off;
278  cp2 = t->rn_key + off;
279  cplim = v + vlen;
280  for (; cp < cplim; cp++, cp2++)
281  if (*cp != *cp2)
282  goto on1;
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)
288  t = t->rn_dupedkey;
289  return t;
290 on1:
291  test = (*cp ^ *cp2) & 0xff; /* find first bit that differs */
292  for (b = 7; (test >>= 1) > 0;)
293  b--;
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)
301  t = t->rn_dupedkey;
302  for (; t; t = t->rn_dupedkey)
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;
313  t = saved_t;
314  /* start searching up the tree */
315  do {
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  }
339  } while (t != top);
340  return 0;
341 }
342 
343 #ifdef RN_DEBUG
344 int rn_nodenum;
345 struct squid_radix_node *rn_clist;
346 int rn_saveinfo;
347 int rn_debug = 1;
348 #endif
349 
350 struct squid_radix_node *
351 squid_rn_newpair(void *v, int b, struct squid_radix_node nodes[2]) {
352  register struct squid_radix_node *tt = nodes, *t = tt + 1;
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;
361 #ifdef RN_DEBUG
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;
367 #endif
368  return t;
369 }
370 
371 struct squid_radix_node *
372 squid_rn_insert(void *v_arg, struct squid_radix_node_head *head, int *dupentry, struct squid_radix_node nodes[2]) {
373  caddr_t v = v_arg;
374  struct squid_radix_node *top = head->rnh_treetop;
375  int head_off = top->rn_off, vlen = (int) *((u_char *) v);
376  register struct squid_radix_node *t = squid_rn_search(v_arg, top);
377  register caddr_t cp = v + head_off;
378  register int b;
379  struct squid_radix_node *tt;
380  /*
381  * Find first bit at which v and t->rn_key differ
382  */
383  {
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;
393 on1:
394  *dupentry = 0;
395  cmp_res = (cp[-1] ^ cp2[-1]) & 0xff;
396  for (b = (cp - v) << 3; cmp_res; b--)
397  cmp_res >>= 1;
398  }
399  {
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;
408  } while (b > (unsigned) x->rn_b); /* x->rn_b < b && x->rn_b >= 0 */
409 #ifdef RN_DEBUG
410  if (rn_debug)
411  fprintf(stderr, "squid_rn_insert: Going In:\n");
412  traverse(p);
413 #endif
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;
421  t->rn_p = p; /* frees x, p as temp vars below */
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  }
428 #ifdef RN_DEBUG
429  if (rn_debug)
430  log(LOG_DEBUG, "squid_rn_insert: Coming Out:\n"), traverse(p);
431 #endif
432  }
433  return (tt);
434 }
435 
436 struct squid_radix_node *
437 squid_rn_addmask(void *n_arg, int search, int skip) {
438  caddr_t netmask = (caddr_t) n_arg;
439  register struct squid_radix_node *x;
440  register caddr_t cp, cplim;
441  register int b = 0, mlen, j;
442  int maskduplicated, m0, isnormal;
443  struct squid_radix_node *saved_x;
444  static int last_zeroed = 0;
445 
446  if ((mlen = *(u_char *) netmask) > squid_max_keylen)
447  mlen = squid_max_keylen;
448  if (skip == 0)
449  skip = 1;
450  if (mlen <= skip)
451  return (squid_mask_rnhead->rnh_nodes);
452  if (skip > 1)
453  memcpy(addmask_key + 1, rn_ones + 1, skip - 1);
454  if ((m0 = mlen) > skip)
455  memcpy(addmask_key + skip, netmask + skip, mlen - skip);
456  /*
457  * Trim trailing zeroes.
458  */
459  for (cp = addmask_key + mlen; (cp > addmask_key) && cp[-1] == 0;)
460  cp--;
461  mlen = cp - addmask_key;
462  if (mlen <= skip) {
463  if (m0 >= last_zeroed)
464  last_zeroed = mlen;
465  return (squid_mask_rnhead->rnh_nodes);
466  }
467  if (m0 < last_zeroed)
468  memset(addmask_key + m0, '\0', last_zeroed - m0);
469  *addmask_key = last_zeroed = mlen;
470  x = squid_rn_search(addmask_key, rn_masktop);
471  if (memcmp(addmask_key, x->rn_key, mlen) != 0)
472  x = 0;
473  if (x || search)
474  return (x);
475  squid_R_Malloc(x, struct squid_radix_node *, squid_max_keylen + 2 * sizeof(*x));
476  if ((saved_x = x) == 0)
477  return (0);
478  memset(x, '\0', squid_max_keylen + 2 * sizeof(*x));
479  netmask = cp = (caddr_t) (x + 2);
480  memcpy(cp, addmask_key, mlen);
481  x = squid_rn_insert(cp, squid_mask_rnhead, &maskduplicated, x);
482  if (maskduplicated) {
483  fprintf(stderr, "squid_rn_addmask: mask impossibly already in tree");
484  squid_Free(saved_x);
485  return (x);
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;)
493  cp++;
494  if (cp != cplim) {
495  for (j = 0x80; (j & *cp) != 0; j >>= 1)
496  b++;
497  if (*cp != normal_chars[b] || cp != (cplim - 1))
498  isnormal = 0;
499  }
500  b += (cp - netmask) << 3;
501  x->rn_b = -1 - b;
502  if (isnormal)
503  x->rn_flags |= RNF_NORMAL;
504  return (x);
505 }
506 
507 static int /* XXX: arbitrary ordering for non-contiguous masks */
508 rn_lexobetter(void *m_arg, void *n_arg)
509 {
510  register u_char *mp = m_arg, *np = n_arg, *lim;
511 
512  if (*mp > *np)
513  return 1; /* not really, but need to check longer one first */
514  if (*mp == *np)
515  for (lim = mp + *mp; mp < lim;)
516  if (*mp++ > *np++)
517  return 1;
518  return 0;
519 }
520 
521 static struct squid_radix_mask *
523  register struct squid_radix_mask *m;
524 
525  squid_MKGet(m);
526  if (m == 0) {
527  fprintf(stderr, "Mask for route not entered\n");
528  return (0);
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)
534  m->rm_leaf = tt;
535  else
536  m->rm_mask = tt->rn_mask;
537  m->rm_mklist = next;
538  tt->rn_mklist = m;
539  return m;
540 }
541 
542 struct squid_radix_node *
543 squid_rn_addroute(void *v_arg, void *n_arg, struct squid_radix_node_head *head, struct squid_radix_node treenodes[2]) {
544  caddr_t v = (caddr_t) v_arg, netmask = (caddr_t) n_arg;
545  register struct squid_radix_node *t, *x = NULL, *tt;
546  struct squid_radix_node *saved_tt, *top = head->rnh_treetop;
547  short b = 0, b_leaf = 0;
548  int keyduplicated;
549  caddr_t mmask;
550  struct squid_radix_mask *m, **mp;
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) {
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;
565  }
566  /*
567  * Deal with duplicated keys: attach node to previous instance
568  */
569  saved_tt = tt = squid_rn_insert(v, head, &keyduplicated, treenodes);
570  if (keyduplicated) {
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 &&
576  ((b_leaf < tt->rn_b) || /* index(netmask) > node */
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 */
594  tt = treenodes;
595  tt->rn_dupedkey = t;
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 {
605  tt = treenodes;
606  tt->rn_dupedkey = t->rn_dupedkey;
607  t->rn_dupedkey = tt;
608  }
609 #ifdef RN_DEBUG
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;
616 #endif
617  tt->rn_key = (caddr_t) v;
618  tt->rn_b = -1;
619  tt->rn_flags = RNF_ACTIVE;
620  }
621  /*
622  * Put mask in tree.
623  */
624  if (netmask) {
625  tt->rn_mask = netmask;
626  tt->rn_b = x->rn_b;
627  tt->rn_flags |= x->rn_flags & RNF_NORMAL;
628  }
629  t = saved_tt->rn_p;
630  if (keyduplicated)
631  goto on2;
632  b_leaf = -1 - t->rn_b;
633  if (t->rn_r == saved_tt)
634  x = t->rn_l;
635  else
636  x = t->rn_r;
637  /* Promote general routes from below */
638  if (x->rn_b < 0) {
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  }
644  } else if (x->rn_mklist) {
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;
653  }
654 on2:
655  /* Add new route to highest possible ancestor's list */
656  if ((netmask == 0) || (b > t->rn_b))
657  return tt; /* can't lift at all */
658  b_leaf = tt->rn_b;
659  do {
660  x = t;
661  t = t->rn_p;
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) {
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;
690  }
691  *mp = rn_new_radix_mask(tt, *mp);
692  return tt;
693 }
694 
695 struct squid_radix_node *
696 squid_rn_delete(void *v_arg, void *netmask_arg, struct squid_radix_node_head *head) {
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;
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;
706  tt = squid_rn_search(v, x);
707  head_off = x->rn_off;
708  vlen = *(u_char *) v;
709  saved_tt = tt;
710  top = x;
711  if (tt == 0 ||
712  memcmp(v + head_off, tt->rn_key + head_off, vlen - head_off))
713  return (0);
714  /*
715  * Delete our route from mask lists.
716  */
717  if (netmask) {
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);
724  }
725  if (tt->rn_mask == 0 || (saved_m = m = tt->rn_mklist) == 0)
726  goto on1;
727  if (tt->rn_flags & RNF_NORMAL) {
728  if (m->rm_leaf != tt || m->rm_refs > 0) {
729  fprintf(stderr, "squid_rn_delete: inconsistent annotation\n");
730  return 0; /* dangling ref could cause disaster */
731  }
732  } else {
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;
739  }
740  b = -1 - tt->rn_b;
741  t = saved_tt->rn_p;
742  if (b > t->rn_b)
743  goto on1; /* Wasn't lifted at all */
744  do {
745  x = t;
746  t = t->rn_p;
747  } while (b <= t->rn_b && x != top);
748  for (mp = &x->rn_mklist; (m = *mp); mp = &m->rm_mklist)
749  if (m == saved_m) {
750  *mp = m->rm_mklist;
751  squid_MKFree(m);
752  break;
753  }
754  if (m == 0) {
755  fprintf(stderr, "squid_rn_delete: couldn't find our annotation\n");
756  if (tt->rn_flags & RNF_NORMAL)
757  return (0); /* Dangling ref to us */
758  }
759 on1:
760  /*
761  * Eliminate us from tree
762  */
763  if (tt->rn_flags & RNF_ROOT)
764  return (0);
765 #ifdef RN_DEBUG
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)
770  t->rn_ybro = tt->rn_ybro;
771 #endif
772  t = tt->rn_p;
773  if ((dupedkey = saved_tt->rn_dupedkey)) {
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) {
791 #ifndef RN_DEBUG
792  *++x = *t;
793  p = t->rn_p;
794 #else
795  b = t->rn_info;
796  *++x = *t;
797  t->rn_info = b;
798  p = t->rn_p;
799 #endif
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;
808  }
809  if (t->rn_l == tt)
810  x = t->rn_r;
811  else
812  x = t->rn_l;
813  p = t->rn_p;
814  if (p->rn_r == t)
815  p->rn_r = x;
816  else
817  p->rn_l = x;
818  x->rn_p = p;
819  /*
820  * Demote routes attached to us.
821  */
822  if (t->rn_mklist) {
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  }
839 #if RN_DEBUG
840  if (m)
841  fprintf(stderr, "%s %x at %x\n",
842  "squid_rn_delete: Orphaned Mask", (int) m, (int) x);
843 #else
844  assert(m == NULL);
845 #endif
846  }
847  }
848  /*
849  * We may be holding an active internal node in the tree.
850  */
851  x = tt + 1;
852  if (t != x) {
853 #ifndef RN_DEBUG
854  *t = *x;
855 #else
856  b = t->rn_info;
857  *t = *x;
858  t->rn_info = b;
859 #endif
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;
867  }
868 out:
869  tt->rn_flags &= ~RNF_ACTIVE;
870  tt[1].rn_flags &= ~RNF_ACTIVE;
871  return (tt);
872 }
873 
874 int
875 squid_rn_walktree(struct squid_radix_node_head *h, int (*f) (struct squid_radix_node *, void *), void *w)
876 {
877  int error;
878  struct squid_radix_node *base, *next;
879  register struct squid_radix_node *rn = h->rnh_treetop;
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)
887  rn = rn->rn_l;
888  for (;;) {
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);
906  }
907  /* NOTREACHED */
908 }
909 
910 int
912 {
913  register struct squid_radix_node_head *rnh;
914  register struct squid_radix_node *t, *tt, *ttt;
915  if (*head)
916  return (1);
917  squid_R_Malloc(rnh, struct squid_radix_node_head *, sizeof(*rnh));
918  if (rnh == 0)
919  return (0);
920  memset(rnh, '\0', sizeof(*rnh));
921  *head = rnh;
922  t = squid_rn_newpair(rn_zeros, off, rnh->rnh_nodes);
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;
936  rnh->rnh_treetop = t;
937  return (1);
938 }
939 
940 void
942 {
943  char *cp, *cplim;
944 #ifdef KERNEL
945  struct domain *dom;
946 
947  for (dom = domains; dom; dom = dom->dom_next)
948  if (dom->dom_maxrtkey > squid_max_keylen)
949  squid_max_keylen = dom->dom_maxrtkey;
950 #endif
951  if (squid_max_keylen == 0) {
952  fprintf(stderr,
953  "squid_rn_init: radix functions require squid_max_keylen be set\n");
954  return;
955  }
956  squid_R_Malloc(rn_zeros, char *, 3 * squid_max_keylen);
957  if (rn_zeros == NULL) {
958  fprintf(stderr, "squid_rn_init failed.\n");
959  exit(-1);
960  }
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;
964  while (cp < cplim)
965  *cp++ = -1;
966  if (squid_rn_inithead(&squid_mask_rnhead, 0) == 0) {
967  fprintf(stderr, "rn_init2 failed.\n");
968  exit(-1);
969  }
970 }
971 
struct squid_radix_node * squid_rn_delete(void *v_arg, void *netmask_arg, struct squid_radix_node_head *head)
Definition: radix.c:696
struct squid_radix_mask * rm_mklist
Definition: radix.h:103
struct squid_radix_mask * squid_rn_mkfreelist
Definition: radix.c:85
#define assert(EX)
Definition: assert.h:17
#define rn_masktop
Definition: radix.c:92
int squid_rn_refines(void *m_arg, void *n_arg)
Definition: radix.c:181
#define squid_Free(p)
Definition: radix.c:103
unsigned char rm_flags
Definition: radix.h:101
struct squid_radix_node * squid_rn_addmask(void *n_arg, int search, int skip)
Definition: radix.c:437
char rn_bmask
Definition: radix.h:58
#define squid_MKFree(m)
Definition: radix.c:112
struct squid_radix_node * squid_rn_newpair(void *v, int b, struct squid_radix_node nodes[2])
Definition: radix.c:351
void error(char *format,...)
void log(char *format,...)
#define RNF_NORMAL
Definition: radix.h:60
static char * addmask_key
Definition: radix.c:87
short rm_b
Definition: radix.h:99
char * p
Definition: membanger.c:43
struct squid_radix_node * rn_p
Definition: radix.h:56
static struct squid_radix_mask * rn_new_radix_mask(struct squid_radix_node *tt, struct squid_radix_mask *next)
Definition: radix.c:522
#define min(x, y)
Definition: radix.c:115
struct squid_radix_node rnh_nodes[3]
Definition: radix.h:143
static unsigned char normal_chars[]
Definition: radix.c:88
void squid_rn_init(void)
Definition: radix.c:941
struct squid_radix_node * squid_rn_insert(void *v_arg, struct squid_radix_node_head *head, int *dupentry, struct squid_radix_node nodes[2])
Definition: radix.c:372
struct squid_radix_node * squid_rn_match(void *v_arg, struct squid_radix_node_head *head)
Definition: radix.c:245
int squid_max_keylen
Definition: radix.c:84
#define squid_R_Malloc(p, t, n)
Definition: radix.c:102
struct squid_radix_node * rnh_lookup(void *v, void *mask, struct squid_radix_node_head *head)
#define RNF_ACTIVE
Definition: radix.h:62
unsigned char rn_flags
Definition: radix.h:59
static int rn_lexobetter(void *m_arg, void *n_arg)
Definition: radix.c:508
struct squid_radix_node * squid_rn_search_m(void *v_arg, struct squid_radix_node *head, void *m_arg)
Definition: radix.c:166
struct squid_radix_node * squid_rn_lookup(void *v_arg, void *m_arg, struct squid_radix_node_head *head)
Definition: radix.c:207
bool SIGHDLR int STUB void int
Definition: stub_tools.cc:68
struct squid_radix_node * squid_rn_addroute(void *v_arg, void *n_arg, struct squid_radix_node_head *head, struct squid_radix_node treenodes[2])
Definition: radix.c:543
struct squid_radix_node * rnh_treetop
Definition: radix.h:114
struct squid_radix_node * rnh_addaddr(void *v, void *mask, struct squid_radix_node_head *head, struct squid_radix_node nodes[])
static char * rn_zeros
Definition: radix.c:89
int squid_rn_inithead(struct squid_radix_node_head **head, int off)
Definition: radix.c:911
short rn_b
Definition: radix.h:57
struct squid_radix_node * squid_rn_search(void *v_arg, struct squid_radix_node *head)
Definition: radix.c:152
struct squid_radix_node_head * squid_mask_rnhead
Definition: radix.c:86
int rnh_walktree(struct squid_radix_node_head *head, int(*f)(struct squid_radix_node *, void *), void *w)
struct squid_radix_mask * rn_mklist
Definition: radix.h:54
struct squid_radix_node * rnh_matchaddr(void *v, struct squid_radix_node_head *head)
static char * rn_ones
Definition: radix.c:89
int squid_rn_walktree(struct squid_radix_node_head *h, int(*f)(struct squid_radix_node *, void *), void *w)
Definition: radix.c:875
static int rn_satsifies_leaf(char *trial, register struct squid_radix_node *leaf, int skip)
Definition: radix.c:225
squidaio_request_t * head
Definition: aiops.cc:127
#define NULL
Definition: types.h:166
#define RNF_ROOT
Definition: radix.h:61
#define squid_MKGet(m)
Definition: radix.c:104
struct squid_radix_node * rnh_deladdr(void *v, void *mask, struct squid_radix_node_head *head)

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors