118316Swollman/* 218316Swollman * Copyright (c) 1988, 1989, 1993 318316Swollman * The Regents of the University of California. All rights reserved. 418316Swollman * 518316Swollman * Redistribution and use in source and binary forms, with or without 618316Swollman * modification, are permitted provided that the following conditions 718316Swollman * are met: 818316Swollman * 1. Redistributions of source code must retain the above copyright 918316Swollman * notice, this list of conditions and the following disclaimer. 1018316Swollman * 2. Redistributions in binary form must reproduce the above copyright 1118316Swollman * notice, this list of conditions and the following disclaimer in the 1218316Swollman * documentation and/or other materials provided with the distribution. 1318316Swollman * 4. Neither the name of the University nor the names of its contributors 1418316Swollman * may be used to endorse or promote products derived from this software 1518316Swollman * without specific prior written permission. 1618316Swollman * 1718316Swollman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1818316Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1918316Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2018316Swollman * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2118316Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2218316Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2318316Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2418316Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2518316Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2618316Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2718316Swollman * SUCH DAMAGE. 2818316Swollman * 2918316Swollman * @(#)radix.h 8.2 (Berkeley) 10/31/94 3046303Smarkm * 3150476Speter * $FreeBSD$ 3218316Swollman */ 3318316Swollman 3418316Swollman#ifndef __RADIX_H_ 3518316Swollman#define __RADIX_H_ 3618316Swollman 3718316Swollman#include <sys/cdefs.h> 3818316Swollmanstruct walkarg; 3918316Swollman 4018316Swollman/* 4118316Swollman * Radix search tree node layout. 4218316Swollman */ 4318316Swollman 4418316Swollmanstruct radix_node { 4518316Swollman struct radix_mask *rn_mklist; /* list of masks contained in subtree */ 4618316Swollman struct radix_node *rn_p; /* parent */ 4718316Swollman short rn_b; /* bit offset; -1-index(netmask) */ 4818316Swollman char rn_bmask; /* node: mask for bit test*/ 4918316Swollman u_char rn_flags; /* enumerated next */ 5018316Swollman#define RNF_NORMAL 1 /* leaf contains normal route */ 5118316Swollman#define RNF_ROOT 2 /* leaf is root leaf for tree */ 5218316Swollman#define RNF_ACTIVE 4 /* This node is alive (for rtfree) */ 5318316Swollman union { 5418316Swollman struct { /* leaf only data: */ 5518316Swollman caddr_t rn_Key; /* object of search */ 5618316Swollman caddr_t rn_Mask; /* netmask, if present */ 5718316Swollman struct radix_node *rn_Dupedkey; 5818316Swollman } rn_leaf; 5918316Swollman struct { /* node only data: */ 6018316Swollman int rn_Off; /* where to start compare */ 6118316Swollman struct radix_node *rn_L;/* progeny */ 6218316Swollman struct radix_node *rn_R;/* progeny */ 6318316Swollman }rn_node; 6418316Swollman } rn_u; 6518316Swollman#ifdef RN_DEBUG 6618316Swollman int rn_info; 6718316Swollman struct radix_node *rn_twin; 6818316Swollman struct radix_node *rn_ybro; 6918316Swollman#endif 7018316Swollman}; 7118316Swollman 7218316Swollman#define rn_dupedkey rn_u.rn_leaf.rn_Dupedkey 7318316Swollman#define rn_key rn_u.rn_leaf.rn_Key 7418316Swollman#define rn_mask rn_u.rn_leaf.rn_Mask 7518316Swollman#define rn_off rn_u.rn_node.rn_Off 7618316Swollman#define rn_l rn_u.rn_node.rn_L 7718316Swollman#define rn_r rn_u.rn_node.rn_R 7818316Swollman 7918316Swollman/* 8018316Swollman * Annotations to tree concerning potential routes applying to subtrees. 8118316Swollman */ 8218316Swollman 83190715Sphkstruct radix_mask { 8418316Swollman short rm_b; /* bit offset; -1-index(netmask) */ 8518316Swollman char rm_unused; /* cf. rn_bmask */ 8618316Swollman u_char rm_flags; /* cf. rn_flags */ 8718316Swollman struct radix_mask *rm_mklist; /* more masks to try */ 8818316Swollman union { 8918316Swollman caddr_t rmu_mask; /* the mask */ 9018316Swollman struct radix_node *rmu_leaf; /* for normal routes */ 9118316Swollman } rm_rmu; 9218316Swollman int rm_refs; /* # of references to this struct */ 93190715Sphk}; 9418316Swollman 9518316Swollman#define rm_mask rm_rmu.rmu_mask 9618316Swollman#define rm_leaf rm_rmu.rmu_leaf /* extra field would make 32 bytes */ 9718316Swollman 9818316Swollman#define MKGet(m) {\ 9918316Swollman if (rn_mkfreelist) {\ 10018316Swollman m = rn_mkfreelist; \ 10118316Swollman rn_mkfreelist = (m)->rm_mklist; \ 10218316Swollman } else \ 10346303Smarkm m = (struct radix_mask *)rtmalloc(sizeof(*(m)), "MKGet"); }\ 10418316Swollman 10518316Swollman#define MKFree(m) { (m)->rm_mklist = rn_mkfreelist; rn_mkfreelist = (m);} 10618316Swollman 10718316Swollmanstruct radix_node_head { 10818316Swollman struct radix_node *rnh_treetop; 10918316Swollman int rnh_addrsize; /* permit, but not require fixed keys */ 11018316Swollman int rnh_pktsize; /* permit, but not require fixed keys */ 11118316Swollman struct radix_node *(*rnh_addaddr) /* add based on sockaddr */ 11292883Simp (void *v, void *mask, 11392883Simp struct radix_node_head *head, struct radix_node nodes[]); 11418316Swollman struct radix_node *(*rnh_addpkt) /* add based on packet hdr */ 11592883Simp (void *v, void *mask, 11692883Simp struct radix_node_head *head, struct radix_node nodes[]); 11718316Swollman struct radix_node *(*rnh_deladdr) /* remove based on sockaddr */ 11892883Simp (void *v, void *mask, struct radix_node_head *head); 11918316Swollman struct radix_node *(*rnh_delpkt) /* remove based on packet hdr */ 12092883Simp (void *v, void *mask, struct radix_node_head *head); 12118316Swollman struct radix_node *(*rnh_matchaddr) /* locate based on sockaddr */ 12292883Simp (void *v, struct radix_node_head *head); 12318316Swollman struct radix_node *(*rnh_lookup) /* locate based on sockaddr */ 12492883Simp (void *v, void *mask, struct radix_node_head *head); 12518316Swollman struct radix_node *(*rnh_matchpkt) /* locate based on packet hdr */ 12692883Simp (void *v, struct radix_node_head *head); 12718316Swollman int (*rnh_walktree) /* traverse tree */ 12818316Swollman (struct radix_node_head *head, 12918316Swollman int (*f)(struct radix_node *, struct walkarg *), 13018316Swollman struct walkarg *w); 13118316Swollman struct radix_node rnh_nodes[3]; /* empty tree for common case */ 13218316Swollman}; 13318316Swollman 13418316Swollman 13546303Smarkm#define Bcopy(a, b, n) memmove(((void *)(b)), ((void *)(a)), (size_t)(n)) 13646303Smarkm#define Bzero(p, n) memset((void *)(p), 0, (size_t)(n)); 13746303Smarkm#define Free(p) free((void *)p); 13818316Swollman 13992883Simpvoid rn_init(void); 140190712Sphkint rn_inithead(struct radix_node_head **head, int off); 14192883Simpint rn_walktree(struct radix_node_head *, 14292883Simp int (*)(struct radix_node *, struct walkarg *), 14392883Simp struct walkarg *); 14446303Smarkm 14518316Swollman#endif /* __RADIX_H_ */ 146