1 /* 2 * Copyright (c) 1988, 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)radix.h 8.1.1.1 (Berkeley) 09/23/94 8 */ 9 10 #ifndef _RADIX_H_ 11 #define _RADIX_H_ 12 13 /* 14 * Radix search tree node layout. 15 */ 16 17 struct radix_node { 18 struct radix_node *rn_mklist; /* list of masks contained in subtree */ 19 struct radix_node *rn_p; /* parent */ 20 short rn_b; /* bit offset; -1-index(netmask) */ 21 char rn_bmask; /* node: mask for bit test*/ 22 u_char rn_flags; /* enumerated next */ 23 #define RNF_NORMAL 1 /* leaf contains normal route */ 24 #define RNF_ROOT 2 /* leaf is root leaf for tree */ 25 #define RNF_ACTIVE 4 /* This node is alive (for rtfree) */ 26 union { 27 struct { /* leaf only data: */ 28 caddr_t rn_Key; /* object of search */ 29 caddr_t rn_Mask; /* netmask, if present */ 30 struct radix_node *rn_Dupedkey; 31 } rn_leaf; 32 struct { /* node only data: */ 33 int rn_Off; /* where to start compare */ 34 struct radix_node *rn_L;/* progeny */ 35 struct radix_node *rn_R;/* progeny */ 36 }rn_node; 37 } rn_u; 38 #ifdef RN_DEBUG 39 int rn_info; 40 struct radix_node *rn_twin; 41 struct radix_node *rn_ybro; 42 #endif 43 }; 44 45 #define rn_dupedkey rn_u.rn_leaf.rn_Dupedkey 46 #define rn_key rn_u.rn_leaf.rn_Key 47 #define rn_mask rn_u.rn_leaf.rn_Mask 48 #define rn_off rn_u.rn_node.rn_Off 49 #define rn_l rn_u.rn_node.rn_L 50 #define rn_r rn_u.rn_node.rn_R 51 52 struct radix_node_head { 53 struct radix_node *rnh_treetop; 54 int rnh_addrsize; /* permit, but not require fixed keys */ 55 int rnh_pktsize; /* permit, but not require fixed keys */ 56 struct radix_node *(*rnh_addaddr) /* add based on sockaddr */ 57 __P((void *v, void *mask, 58 struct radix_node_head *head, struct radix_node nodes[])); 59 struct radix_node *(*rnh_addpkt) /* add based on packet hdr */ 60 __P((void *v, void *mask, 61 struct radix_node_head *head, struct radix_node nodes[])); 62 struct radix_node *(*rnh_deladdr) /* remove based on sockaddr */ 63 __P((void *v, void *mask, struct radix_node_head *head)); 64 struct radix_node *(*rnh_delpkt) /* remove based on packet hdr */ 65 __P((void *v, void *mask, struct radix_node_head *head)); 66 struct radix_node *(*rnh_matchaddr) /* locate based on sockaddr */ 67 __P((void *v, struct radix_node_head *head)); 68 struct radix_node *(*rnh_matchpkt) /* locate based on packet hdr */ 69 __P((void *v, struct radix_node_head *head)); 70 int (*rnh_walktree) /* traverse tree */ 71 __P((struct radix_node_head *head, int (*f)(), void *w)); 72 /* mapping stuff */ 73 struct radix_index_table { 74 short limit; 75 short offset; 76 } *rnh_table; /* how to re-order the bits */ 77 int rnh_offset; /* for martialed keys */ 78 int (*rnh_bits_matched) /* used in matching, insert */ 79 __P((void *trial, void *ref, 80 struct radix_node_head *head, int masklen)); 81 int (*rnh_set_mask) /* used in insertion */ 82 __P((int index, 83 struct radix_node *rn, struct radix_node_head *rnh)); 84 /* the treetop itself */ 85 struct radix_node rnh_nodes[3]; /* empty tree for common case */ 86 }; 87 88 89 #ifndef KERNEL 90 #define Bcmp(a, b, n) bcmp(((char *)(a)), ((char *)(b)), (n)) 91 #define Bzero(p, n) bzero((char *)(p), (int)(n)); 92 #define R_Malloc(p, t, n) (p = (t) malloc((unsigned int)(n))) 93 #define Free(p) free((char *)p); 94 #else 95 #define Bcmp(a, b, n) bcmp(((caddr_t)(a)), ((caddr_t)(b)), (unsigned)(n)) 96 #define Bcopy(a, b, n) bcopy(((caddr_t)(a)), ((caddr_t)(b)), (unsigned)(n)) 97 #define Bzero(p, n) bzero((caddr_t)(p), (unsigned)(n)); 98 #define R_Malloc(p, t, n) (p = (t) malloc((unsigned long)(n), M_RTABLE, M_DONTWAIT)) 99 #define Free(p) free((caddr_t)p, M_RTABLE); 100 #endif /*KERNEL*/ 101 102 void rn_init __P((void)); 103 int rn_inithead __P((void **, int)); 104 int rn_refines __P((void *, void *)); 105 int rn_walktree __P((struct radix_node_head *, int (*)(), void *)); 106 struct radix_node 107 *rn_addmask __P((void *, int, int)), 108 *rn_addroute __P((void *, void *, struct radix_node_head *, 109 struct radix_node [2])), 110 *rn_delete __P((void *, void *, struct radix_node_head *)), 111 *rn_insert __P((void *, struct radix_node_head *, int *, 112 struct radix_node [2])), 113 *rn_match __P((void *, struct radix_node_head *)), 114 *rn_newpair __P((void *, int, struct radix_node[2])), 115 *rn_search __P((void *, struct radix_node *)), 116 *rn_search_unmapped __P((void *, struct radix_node_head *)); 117 118 int rn_set_unmapped_mask 119 __P((int, struct radix_node *, struct radix_node_head *)), 120 rn_set_mapped_mask 121 __P((int, struct radix_node *, struct radix_node_head *)), 122 rn_mapped_bits_matched 123 __P((void *, void *, struct radix_node_head *, int)), 124 rn_unmapped_bits_matched 125 __P((void *, void *, struct radix_node_head *, int)); 126 127 128 #endif /* _RADIX_H_ */ 129