1 /* 2 * Copyright (c) 1988, 1989 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)radix.h 7.8 (Berkeley) 04/08/93 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_mask *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 /* 53 * Annotations to tree concerning potential routes applying to subtrees. 54 */ 55 56 extern struct radix_mask { 57 short rm_b; /* bit offset; -1-index(netmask) */ 58 char rm_unused; /* cf. rn_bmask */ 59 u_char rm_flags; /* cf. rn_flags */ 60 struct radix_mask *rm_mklist; /* more masks to try */ 61 caddr_t rm_mask; /* the mask */ 62 int rm_refs; /* # of references to this struct */ 63 } *rn_mkfreelist; 64 65 #define MKGet(m) {\ 66 if (rn_mkfreelist) {\ 67 m = rn_mkfreelist; \ 68 rn_mkfreelist = (m)->rm_mklist; \ 69 } else \ 70 R_Malloc(m, struct radix_mask *, sizeof (*(m))); }\ 71 72 #define MKFree(m) { (m)->rm_mklist = rn_mkfreelist; rn_mkfreelist = (m);} 73 74 struct radix_node_head { 75 struct radix_node *rnh_treetop; 76 int rnh_addrsize; /* permit, but not require fixed keys */ 77 int rnh_pktsize; /* permit, but not require fixed keys */ 78 struct radix_node *(*rnh_addaddr) /* add based on sockaddr */ 79 __P((caddr_t v, caddr_t mask, 80 struct radix_node_head *head, struct radix_node nodes[])); 81 struct radix_node *(*rnh_addpkt) /* add based on packet hdr */ 82 __P((caddr_t v, caddr_t mask, 83 struct radix_node_head *head, struct radix_node nodes[])); 84 struct radix_node *(*rnh_deladdr) /* remove based on sockaddr */ 85 __P((caddr_t v, caddr_t mask, struct radix_node_head *head)); 86 struct radix_node *(*rnh_delpkt) /* remove based on packet hdr */ 87 __P((caddr_t v, caddr_t mask, struct radix_node_head *head)); 88 struct radix_node *(*rnh_matchaddr) /* locate based on sockaddr */ 89 __P((caddr_t v, struct radix_node_head *head)); 90 struct radix_node *(*rnh_matchpkt) /* locate based on packet hdr */ 91 __P((caddr_t v, struct radix_node_head *head)); 92 int (*rnh_walktree) /* traverse tree */ 93 __P((struct radix_node_head *head, int (*f)(), void *w)); 94 struct radix_node rnh_nodes[3]; /* empty tree for common case */ 95 }; 96 97 98 #ifndef KERNEL 99 #define Bcmp(a, b, n) bcmp(((char *)(a)), ((char *)(b)), (n)) 100 #define Bzero(p, n) bzero((char *)(p), (int)(n)); 101 #define R_Malloc(p, t, n) (p = (t) malloc((unsigned int)(n))) 102 #define Free(p) free((char *)p); 103 #else 104 #define Bcmp(a, b, n) bcmp(((caddr_t)(a)), ((caddr_t)(b)), (unsigned)(n)) 105 #define Bcopy(a, b, n) bcopy(((caddr_t)(a)), ((caddr_t)(b)), (unsigned)(n)) 106 #define Bzero(p, n) bzero((caddr_t)(p), (unsigned)(n)); 107 #define R_Malloc(p, t, n) (p = (t) malloc((unsigned long)(n), M_RTABLE, M_DONTWAIT)) 108 #define Free(p) free((caddr_t)p, M_RTABLE); 109 110 int rn_inithead __P((void **, int)); 111 #endif /*KERNEL*/ 112 #endif /* _RADIX_H_ */ 113