2 * Copyright (c) 1988, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * @(#)radix.c 8.6 (Berkeley) 10/17/95
30 * Routines to build and maintain radix trees for routing lookups.
32 #if defined(KERNEL) || defined(_KERNEL)
38 #define __SYS_ATOMIC_OPS_H__
39 #if !defined(__svr4__) && !defined(__SVR4) && !defined(__osf__) && \
40 !defined(__hpux) && !defined(__sgi)
41 #include <sys/cdefs.h>
52 # define _IPV6_SWTAB_H
53 # define _PROTO_NET_H_
54 # define _PROTO_IPV6_H
55 # include <sys/malloc.h>
58 #include <sys/param.h>
60 #include <sys/systm.h>
62 void panic __P((char *str));
71 #include <sys/syslog.h>
74 #include <netinet/in.h>
75 #include <sys/socket.h>
77 #include "netinet/ip_compat.h"
78 #include "netinet/ip_fil.h"
80 #include "radix_ipf.h"
89 static struct radix_mask *rn_mkfreelist;
90 static struct radix_node_head *mask_rnhead;
91 static char *addmask_key;
92 static u_char normal_chars[] = {0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
93 static char *rn_zeros = NULL, *rn_ones = NULL;
95 #define rn_masktop (mask_rnhead->rnh_treetop)
97 #define Bcmp(a, b, l) (l == 0 ? 0 : bcmp((caddr_t)(a), (caddr_t)(b), (u_long)l))
99 static int rn_satisfies_leaf __P((char *, struct radix_node *, int));
100 static int rn_lexobetter __P((void *, void *));
101 static struct radix_mask *rn_new_radix_mask __P((struct radix_node *,
102 struct radix_mask *));
103 static int rn_freenode __P((struct radix_node *, void *));
104 #if defined(AIX) && !defined(_KERNEL)
105 struct radix_node *rn_match __P((void *, struct radix_node_head *));
106 struct radix_node *rn_addmask __P((int, int, void *));
107 #define FreeS(x, y) KFREES(x, y)
108 #define Bcopy(x, y, z) bcopy(x, y, z)
112 * The data structure for the keys is a radix tree with one way
113 * branching removed. The index rn_b at an internal node n represents a bit
114 * position to be tested. The tree is arranged so that all descendants
115 * of a node n have keys whose bits all agree up to position rn_b - 1.
116 * (We say the index of n is rn_b.)
118 * There is at least one descendant which has a one bit at position rn_b,
119 * and at least one with a zero there.
121 * A route is determined by a pair of key and mask. We require that the
122 * bit-wise logical and of the key and mask to be the key.
123 * We define the index of a route to associated with the mask to be
124 * the first bit number in the mask where 0 occurs (with bit number 0
125 * representing the highest order bit).
127 * We say a mask is normal if every bit is 0, past the index of the mask.
128 * If a node n has a descendant (k, m) with index(m) == index(n) == rn_b,
129 * and m is a normal mask, then the route applies to every descendant of n.
130 * If the index(m) < rn_b, this implies the trailing last few bits of k
131 * before bit b are all 0, (and hence consequently true of every descendant
132 * of n), so the route applies to all descendants of the node as well.
134 * Similar logic shows that a non-normal mask m such that
135 * index(m) <= index(n) could potentially apply to many children of n.
136 * Thus, for each non-host route, we attach its mask to a list at an internal
137 * node as high in the tree as we can go.
139 * The present version of the code makes use of normal routes in short-
140 * circuiting an explicit mask and compare operation when testing whether
141 * a key satisfies a normal route, and also in remembering the unique leaf
142 * that governs a subtree.
146 rn_search(v_arg, head)
148 struct radix_node *head;
150 struct radix_node *x;
153 for (x = head, v = v_arg; x->rn_b >= 0;) {
154 if (x->rn_bmask & v[x->rn_off])
163 rn_search_m(v_arg, head, m_arg)
164 struct radix_node *head;
167 struct radix_node *x;
168 caddr_t v = v_arg, m = m_arg;
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]))
181 rn_refines(m_arg, n_arg)
184 caddr_t m = m_arg, n = n_arg;
185 caddr_t lim, lim2 = lim = n + *(u_char *)n;
186 int longer = (*(u_char *)n++) - (int)(*(u_char *)m++);
187 int masks_are_equal = 1;
200 if (masks_are_equal && (longer < 0))
201 for (lim2 = m - longer; m < lim2; )
204 return (!masks_are_equal);
208 rn_lookup(v_arg, m_arg, head)
210 struct radix_node_head *head;
212 struct radix_node *x;
216 if ((x = rn_addmask(m_arg, 1, head->rnh_treetop->rn_off)) == 0)
220 x = rn_match(v_arg, head);
222 while (x && x->rn_mask != netmask)
229 rn_satisfies_leaf(trial, leaf, skip)
231 struct radix_node *leaf;
234 char *cp = trial, *cp2 = leaf->rn_key, *cp3 = leaf->rn_mask;
236 int length = min(*(u_char *)cp, *(u_char *)cp2);
241 length = min(length, *(u_char *)cp3);
245 for (cp += skip; cp < cplim; cp++, cp2++, cp3++)
246 if ((*cp ^ *cp2) & *cp3)
252 rn_match(v_arg, head)
254 struct radix_node_head *head;
257 struct radix_node *t = head->rnh_treetop, *x;
260 struct radix_node *saved_t, *top = t;
261 int off = t->rn_off, vlen = *(u_char *)cp, matched_off;
265 * Open code rn_search(v, top) to avoid overhead of extra
268 for (; t->rn_b >= 0; ) {
269 if (t->rn_bmask & cp[t->rn_off])
275 * See if we match exactly as a host destination
276 * or at least learn how many bits match, for normal mask finesse.
278 * It doesn't hurt us to limit how many bytes to check
279 * to the length of the mask, since if it matches we had a genuine
280 * match and the leaf we have is the most specific one anyway;
281 * if it didn't match with a shorter length it would fail
282 * with a long one. This wins big for class B&C netmasks which
283 * are probably the most common case...
286 vlen = *(u_char *)t->rn_mask;
288 cp2 = t->rn_key + off;
290 for (; cp < cplim; cp++, cp2++)
294 * This extra grot is in case we are explicitly asked
295 * to look up the default. Ugh!
297 if ((t->rn_flags & RNF_ROOT) && t->rn_dupedkey)
301 test = (*cp ^ *cp2) & 0xff; /* find first bit that differs */
302 for (b = 7; (test >>= 1) > 0;)
304 matched_off = cp - v;
305 b += matched_off << 3;
308 * If there is a host route in a duped-key chain, it will be first.
310 if ((saved_t = t)->rn_mask == 0)
312 for (; t; t = t->rn_dupedkey)
314 * Even if we don't match exactly as a host,
315 * we may match if the leaf we wound up at is
318 if (t->rn_flags & RNF_NORMAL) {
321 } else if (rn_satisfies_leaf(v, t, matched_off))
324 /* start searching up the tree */
326 struct radix_mask *m;
331 * If non-contiguous masks ever become important
332 * we can restore the masking and open coding of
333 * the search and satisfaction test and put the
334 * calculation of "off" back before the "do".
337 if (m->rm_flags & RNF_NORMAL) {
341 off = min(t->rn_off, matched_off);
342 x = rn_search_m(v, t, m->rm_mask);
343 while (x && x->rn_mask != m->rm_mask)
345 if (x && rn_satisfies_leaf(v, x, off))
357 struct radix_node *rn_clist;
363 rn_newpair(v, b, nodes)
366 struct radix_node nodes[2];
368 struct radix_node *tt = nodes, *t = tt + 1;
370 t->rn_bmask = 0x80 >> (b & 7);
374 tt->rn_key = (caddr_t)v;
376 tt->rn_flags = t->rn_flags = RNF_ACTIVE;
378 tt->rn_info = rn_nodenum++;
379 t->rn_info = rn_nodenum++;
381 tt->rn_ybro = rn_clist;
388 rn_insert(v_arg, head, dupentry, nodes)
390 struct radix_node_head *head;
392 struct radix_node nodes[2];
395 struct radix_node *top = head->rnh_treetop;
396 int head_off = top->rn_off, vlen = (int)*((u_char *)v);
397 struct radix_node *t = rn_search(v_arg, top);
398 caddr_t cp = v + head_off;
400 struct radix_node *tt;
404 log(LOG_DEBUG, "rn_insert(%p,%p,%p,%p)\n", v_arg, head, dupentry, nodes);
407 * Find first bit at which v and t->rn_key differ
410 caddr_t cp2 = t->rn_key + head_off;
412 caddr_t cplim = v + vlen;
421 cmp_res = (cp[-1] ^ cp2[-1]) & 0xff;
422 for (b = (cp - v) << 3; cmp_res; b--)
426 struct radix_node *p, *x = top;
430 if (cp[x->rn_off] & x->rn_bmask)
434 } while (b > (unsigned) x->rn_b); /* x->rn_b < b && x->rn_b >= 0 */
437 log(LOG_DEBUG, "rn_insert: Going In:\n"); // traverse(p);
439 t = rn_newpair(v_arg, b, nodes);
441 if ((cp[p->rn_off] & p->rn_bmask) == 0)
446 t->rn_p = p; /* frees x, p as temp vars below */
447 if ((cp[t->rn_off] & t->rn_bmask) == 0) {
455 log(LOG_DEBUG, "rn_insert: Coming Out:\n"); // traverse(p);
462 rn_addmask(n_arg, search, skip)
466 caddr_t netmask = (caddr_t)n_arg;
467 struct radix_node *x;
470 int maskduplicated, m0, isnormal;
471 struct radix_node *saved_x;
472 static int last_zeroed = 0;
476 log(LOG_DEBUG, "rn_addmask(%p,%d,%d)\n", n_arg, search, skip);
478 mlen = *(u_char *)netmask;
479 if ((mlen = *(u_char *)netmask) > max_keylen)
484 return (mask_rnhead->rnh_nodes);
486 Bcopy(rn_ones + 1, addmask_key + 1, skip - 1);
487 if ((m0 = mlen) > skip)
488 Bcopy(netmask + skip, addmask_key + skip, mlen - skip);
490 * Trim trailing zeroes.
492 for (cp = addmask_key + mlen; (cp > addmask_key) && cp[-1] == 0;)
494 mlen = cp - addmask_key;
496 if (m0 >= last_zeroed)
498 return (mask_rnhead->rnh_nodes);
500 if (m0 < last_zeroed)
501 Bzero(addmask_key + m0, last_zeroed - m0);
502 *addmask_key = last_zeroed = mlen;
503 x = rn_search(addmask_key, rn_masktop);
504 if (Bcmp(addmask_key, x->rn_key, mlen) != 0)
508 R_Malloc(x, struct radix_node *, max_keylen + 2 * sizeof (*x));
509 if ((saved_x = x) == 0)
511 Bzero(x, max_keylen + 2 * sizeof (*x));
512 netmask = cp = (caddr_t)(x + 2);
513 Bcopy(addmask_key, cp, mlen);
514 x = rn_insert(cp, mask_rnhead, &maskduplicated, x);
515 if (maskduplicated) {
517 log(LOG_ERR, "rn_addmask: mask impossibly already in tree\n");
523 * Calculate index of mask, and check for normalcy.
525 cplim = netmask + mlen;
527 for (cp = netmask + skip; (cp < cplim) && *(u_char *)cp == 0xff;)
530 for (j = 0x80; (j & *cp) != 0; j >>= 1)
532 if (*cp != normal_chars[b] || cp != (cplim - 1))
535 b += (cp - netmask) << 3;
538 x->rn_flags |= RNF_NORMAL;
542 static int /* XXX: arbitrary ordering for non-contiguous masks */
543 rn_lexobetter(m_arg, n_arg)
546 u_char *mp = m_arg, *np = n_arg, *lim;
549 return 1; /* not really, but need to check longer one first */
551 for (lim = mp + *mp; mp < lim;)
557 static struct radix_mask *
558 rn_new_radix_mask(tt, next)
559 struct radix_node *tt;
560 struct radix_mask *next;
562 struct radix_mask *m;
567 log(LOG_ERR, "Mask for route not entered\n");
573 m->rm_flags = tt->rn_flags;
574 if (tt->rn_flags & RNF_NORMAL)
577 m->rm_mask = tt->rn_mask;
584 rn_addroute(v_arg, n_arg, head, treenodes)
586 struct radix_node_head *head;
587 struct radix_node treenodes[2];
589 caddr_t v = (caddr_t)v_arg, netmask = (caddr_t)n_arg;
590 struct radix_node *t, *x = NULL, *tt;
591 struct radix_node *saved_tt, *top = head->rnh_treetop;
592 short b = 0, b_leaf = 0;
595 struct radix_mask *m, **mp;
599 log(LOG_DEBUG, "rn_addroute(%p,%p,%p,%p)\n", v_arg, n_arg, head, treenodes);
602 * In dealing with non-contiguous masks, there may be
603 * many different routes which have the same mask.
604 * We will find it useful to have a unique pointer to
605 * the mask to speed avoiding duplicate references at
606 * nodes and possibly save time in calculating indices.
609 if ((x = rn_addmask(netmask, 0, top->rn_off)) == 0)
616 * Deal with duplicated keys: attach node to previous instance
618 saved_tt = tt = rn_insert(v, head, &keyduplicated, treenodes);
620 for (t = tt; tt; t = tt, tt = tt->rn_dupedkey) {
621 if (tt->rn_mask == netmask)
625 ((b_leaf < tt->rn_b) || /* index(netmask) > node */
626 rn_refines(netmask, tt->rn_mask) ||
627 rn_lexobetter(netmask, tt->rn_mask))))
631 * If the mask is not duplicated, we wouldn't
632 * find it among possible duplicate key entries
633 * anyway, so the above test doesn't hurt.
635 * We sort the masks for a duplicated key the same way as
636 * in a masklist -- most specific to least specific.
637 * This may require the unfortunate nuisance of relocating
638 * the head of the list.
640 * We also reverse, or doubly link the list through the
643 if (tt == saved_tt) {
644 struct radix_node *xx = x;
645 /* link in at head of list */
646 (tt = treenodes)->rn_dupedkey = t;
647 tt->rn_flags = t->rn_flags;
648 tt->rn_p = x = t->rn_p;
657 (tt = treenodes)->rn_dupedkey = t->rn_dupedkey;
661 tt->rn_dupedkey->rn_p = tt;
665 tt->rn_info = rn_nodenum++;
666 t->rn_info = rn_nodenum++;
668 tt->rn_ybro = rn_clist;
671 tt->rn_key = (caddr_t) v;
673 tt->rn_flags = RNF_ACTIVE;
679 tt->rn_mask = netmask;
681 tt->rn_flags |= x->rn_flags & RNF_NORMAL;
686 b_leaf = -1 - t->rn_b;
687 if (t->rn_r == saved_tt)
691 /* Promote general routes from below */
693 for (mp = &t->rn_mklist; x; x = x->rn_dupedkey)
694 if (x->rn_mask && (x->rn_b >= b_leaf) && x->rn_mklist == 0) {
695 *mp = m = rn_new_radix_mask(x, 0);
699 } else if (x->rn_mklist) {
701 * Skip over masks whose index is > that of new node
703 for (mp = &x->rn_mklist; (m = *mp) != NULL; mp = &m->rm_mklist)
704 if (m->rm_b >= b_leaf)
710 /* Add new route to highest possible ancestor's list */
711 if ((netmask == 0) || (b > t->rn_b ))
712 return tt; /* can't lift at all */
717 } while (b <= t->rn_b && x != top);
719 * Search through routes associated with node to
720 * insert new route according to index.
721 * Need same criteria as when sorting dupedkeys to avoid
722 * double loop on deletion.
724 for (mp = &x->rn_mklist; (m = *mp) != NULL; mp = &m->rm_mklist) {
725 if (m->rm_b < b_leaf)
727 if (m->rm_b > b_leaf)
729 if (m->rm_flags & RNF_NORMAL) {
730 mmask = m->rm_leaf->rn_mask;
731 if (tt->rn_flags & RNF_NORMAL) {
733 log(LOG_ERR, "Non-unique normal route,"
734 " mask not entered\n");
740 if (mmask == netmask) {
745 if (rn_refines(netmask, mmask)
746 || rn_lexobetter(netmask, mmask))
749 *mp = rn_new_radix_mask(tt, *mp);
754 rn_delete(v_arg, netmask_arg, head)
755 void *v_arg, *netmask_arg;
756 struct radix_node_head *head;
758 struct radix_node *t, *p, *x, *tt;
759 struct radix_mask *m, *saved_m, **mp;
760 struct radix_node *dupedkey, *saved_tt, *top;
762 int b, head_off, vlen;
765 netmask = netmask_arg;
766 x = head->rnh_treetop;
767 tt = rn_search(v, x);
768 head_off = x->rn_off;
773 Bcmp(v + head_off, tt->rn_key + head_off, vlen - head_off))
776 * Delete our route from mask lists.
779 if ((x = rn_addmask(netmask, 1, head_off)) == 0)
782 while (tt->rn_mask != netmask)
783 if ((tt = tt->rn_dupedkey) == 0)
786 if (tt->rn_mask == 0 || (saved_m = m = tt->rn_mklist) == 0)
788 if (tt->rn_flags & RNF_NORMAL) {
789 if (m->rm_leaf != tt || m->rm_refs > 0) {
791 log(LOG_ERR, "rn_delete: inconsistent annotation\n");
793 return 0; /* dangling ref could cause disaster */
796 if (m->rm_mask != tt->rn_mask) {
798 log(LOG_ERR, "rn_delete: inconsistent annotation\n");
802 if (--m->rm_refs >= 0)
808 goto on1; /* Wasn't lifted at all */
812 } while (b <= t->rn_b && x != top);
813 for (mp = &x->rn_mklist; (m = *mp) != NULL; mp = &m->rm_mklist)
821 log(LOG_ERR, "rn_delete: couldn't find our annotation\n");
823 if (tt->rn_flags & RNF_NORMAL)
824 return (0); /* Dangling ref to us */
828 * Eliminate us from tree
830 if (tt->rn_flags & RNF_ROOT)
833 /* Get us out of the creation list */
834 for (t = rn_clist; t && t->rn_ybro != tt; t = t->rn_ybro)
836 if (t) t->rn_ybro = tt->rn_ybro;
839 dupedkey = saved_tt->rn_dupedkey;
842 * Here, tt is the deletion target and
843 * saved_tt is the head of the dupedkey chain.
845 if (tt == saved_tt) {
853 /* find node in front of tt on the chain */
854 for (x = p = saved_tt; p && p->rn_dupedkey != tt;)
857 p->rn_dupedkey = tt->rn_dupedkey;
859 tt->rn_dupedkey->rn_p = p;
863 log(LOG_ERR, "rn_delete: couldn't find us\n");
867 if (t->rn_flags & RNF_ACTIVE) {
897 * Demote routes attached to us.
901 for (mp = &x->rn_mklist; (m = *mp) != NULL;)
905 /* If there are any key,mask pairs in a sibling
906 duped-key chain, some subset will appear sorted
907 in the same order attached to our mklist */
908 for (m = t->rn_mklist; m && x; x = x->rn_dupedkey)
909 if (m == x->rn_mklist) {
910 struct radix_mask *mm = m->rm_mklist;
912 if (--(m->rm_refs) < 0)
918 log(LOG_ERR, "%s %p at %p\n",
919 "rn_delete: Orphaned Mask", m, x);
924 * We may be holding an active internal node in the tree.
944 tt->rn_flags &= ~RNF_ACTIVE;
945 tt[1].rn_flags &= ~RNF_ACTIVE;
951 struct radix_node_head *h;
952 int (*f) __P((struct radix_node *, void *));
956 struct radix_node *base, *next;
957 struct radix_node *rn = h->rnh_treetop;
959 * This gets complicated because we may delete the node
960 * while applying the function f to it, so we need to calculate
961 * the successor node in advance.
963 /* First time through node, go left */
964 while (rn->rn_b >= 0)
968 /* If at right child go back up, otherwise, go right */
969 while (rn->rn_p->rn_r == rn && (rn->rn_flags & RNF_ROOT) == 0)
971 /* Find the next *leaf* since next node might vanish, too */
972 for (rn = rn->rn_p->rn_r; rn->rn_b >= 0;)
976 while ((rn = base) != NULL) {
977 base = rn->rn_dupedkey;
978 if (!(rn->rn_flags & RNF_ROOT)
979 && (error = (*f)(rn, w)))
983 if (rn->rn_flags & RNF_ROOT)
990 rn_inithead(head, off)
994 struct radix_node_head *rnh;
998 R_Malloc(rnh, struct radix_node_head *, sizeof (*rnh));
1002 return rn_inithead0(rnh, off);
1006 rn_inithead0(rnh, off)
1007 struct radix_node_head *rnh;
1010 struct radix_node *t, *tt, *ttt;
1012 Bzero(rnh, sizeof (*rnh));
1013 t = rn_newpair(rn_zeros, off, rnh->rnh_nodes);
1014 ttt = rnh->rnh_nodes + 2;
1018 tt->rn_flags = t->rn_flags = RNF_ROOT | RNF_ACTIVE;
1019 tt->rn_b = -1 - off;
1021 ttt->rn_key = rn_ones;
1022 rnh->rnh_addaddr = rn_addroute;
1023 rnh->rnh_deladdr = rn_delete;
1024 rnh->rnh_matchaddr = rn_match;
1025 rnh->rnh_lookup = rn_lookup;
1026 rnh->rnh_walktree = rn_walktree;
1027 rnh->rnh_treetop = t;
1036 if (max_keylen == 0) {
1039 "rn_init: radix functions require max_keylen be set\n");
1043 if (rn_zeros == NULL) {
1044 R_Malloc(rn_zeros, char *, 3 * max_keylen);
1046 if (rn_zeros == NULL)
1048 Bzero(rn_zeros, 3 * max_keylen);
1049 rn_ones = cp = rn_zeros + max_keylen;
1050 addmask_key = cplim = rn_ones + max_keylen;
1053 if (rn_inithead((void *)&mask_rnhead, 0) == 0)
1059 rn_freenode(struct radix_node *n, void *p)
1061 struct radix_node_head *rnh = p;
1062 struct radix_node *d;
1064 d = rnh->rnh_deladdr(n->rn_key, NULL, rnh);
1066 FreeS(d, max_keylen + 2 * sizeof (*d));
1074 struct radix_node_head *rnh;
1077 (void)rn_walktree(rnh, rn_freenode, rnh);
1079 rnh->rnh_addaddr = NULL;
1080 rnh->rnh_deladdr = NULL;
1081 rnh->rnh_matchaddr = NULL;
1082 rnh->rnh_lookup = NULL;
1083 rnh->rnh_walktree = NULL;
1092 struct radix_mask *m;
1094 if (rn_zeros != NULL) {
1095 FreeS(rn_zeros, 3 * max_keylen);
1099 if (mask_rnhead != NULL) {
1100 rn_freehead(mask_rnhead);
1104 while ((m = rn_mkfreelist) != NULL) {
1105 rn_mkfreelist = m->rm_mklist;
1113 typedef struct myst {
1116 struct radix_node nodes[2];
1120 main(int argc, char *argv[])
1122 struct radix_node_head *rnh;
1123 struct radix_node *rn;
1124 addrfamily_t af, mf;
1125 myst_t st1, st2, *stp;
1127 memset(&st1, 0, sizeof(st1));
1128 memset(&st2, 0, sizeof(st2));
1129 memset(&af, 0, sizeof(af));
1134 rn_inithead(&rnh, offsetof(addrfamily_t, adf_addr) << 3);
1136 st1.dst.adf_len = sizeof(st1);
1137 st1.mask.adf_len = sizeof(st1);
1138 st1.dst.adf_addr.in4.s_addr = inet_addr("127.0.0.0");
1139 st1.mask.adf_addr.in4.s_addr = inet_addr("255.0.0.0");
1140 rn = rnh->rnh_addaddr(&st1.dst, &st1.mask, rnh, st1.nodes);
1141 printf("add.1 %p\n", rn);
1143 st2.dst.adf_len = sizeof(st2);
1144 st2.mask.adf_len = sizeof(st2);
1145 st2.dst.adf_addr.in4.s_addr = inet_addr("127.0.1.0");
1146 st2.mask.adf_addr.in4.s_addr = inet_addr("255.255.255.0");
1147 rn = rnh->rnh_addaddr(&st2.dst, &st2.mask, rnh, st2.nodes);
1148 printf("add.2 %p\n", rn);
1150 af.adf_len = sizeof(af);
1151 af.adf_addr.in4.s_addr = inet_addr("127.0.1.0");
1152 rn = rnh->rnh_matchaddr(&af, rnh);
1154 printf("1.lookup = %p key %p mask %p\n", rn, rn->rn_key, rn->rn_mask);
1156 printf("%s/", inet_ntoa(stp->dst.adf_addr.in4));
1158 printf("%s\n", inet_ntoa(stp->dst.adf_addr.in4));
1161 mf.adf_len = sizeof(mf);
1162 mf.adf_addr.in4.s_addr = inet_addr("255.255.255.0");
1163 rn = rnh->rnh_lookup(&af, &mf, rnh);
1165 printf("2.lookup = %p key %p mask %p\n", rn, rn->rn_key, rn->rn_mask);
1167 printf("%s/", inet_ntoa(stp->dst.adf_addr.in4));
1169 printf("%s\n", inet_ntoa(stp->dst.adf_addr.in4));
1172 af.adf_len = sizeof(af);
1173 af.adf_addr.in4.s_addr = inet_addr("126.0.0.1");
1174 rn = rnh->rnh_matchaddr(&af, rnh);
1176 printf("3.lookup = %p key %p mask %p\n", rn, rn->rn_key, rn->rn_mask);
1178 printf("%s/", inet_ntoa(stp->dst.adf_addr.in4));
1180 printf("%s\n", inet_ntoa(stp->dst.adf_addr.in4));
1188 log(int level, char *format, ...)
1192 va_start(ap, format);
1193 vfprintf(stderr, format, ap);