ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/include/patricia.h
Revision: 5042
Committed: Sat Dec 13 18:19:17 2014 UTC (9 years, 4 months ago) by michael
Content type: text/x-chdr
File size: 4442 byte(s)
Log Message:
- Added latest patricia.c, patricia.h from Net-Patricia-1.22 for later use

File Contents

# User Rev Content
1 michael 5042 /*
2     * $Id: patricia.h,v 1.6 2005/12/07 20:53:01 dplonka Exp $
3     * Dave Plonka <plonka@doit.wisc.edu>
4     *
5     * This product includes software developed by the University of Michigan,
6     * Merit Network, Inc., and their contributors.
7     *
8     * This file had been called "radix.h" in the MRT sources.
9     *
10     * I renamed it to "patricia.h" since it's not an implementation of a general
11     * radix trie. Also, pulled in various requirements from "mrt.h" and added
12     * some other things it could be used as a standalone API.
13     */
14    
15     #ifndef _PATRICIA_H
16     #define _PATRICIA_H
17    
18     #define HAVE_IPV6
19    
20     /* typedef unsigned int u_int; */
21     typedef void (*void_fn_t)();
22     /* { from defs.h */
23     #define prefix_touchar(prefix) ((u_char *)&(prefix)->add.sin)
24     #define MAXLINE 1024
25     #define BIT_TEST(f, b) ((f) & (b))
26     /* } */
27    
28     #define addroute make_and_lookup
29    
30     #include <sys/types.h> /* for u_* definitions (on FreeBSD 5) */
31    
32     #include <errno.h> /* for EAFNOSUPPORT */
33     #ifndef EAFNOSUPPORT
34     # defined EAFNOSUPPORT WSAEAFNOSUPPORT
35     # include <winsock.h>
36     #else
37     # include <netinet/in.h> /* for struct in_addr */
38     #endif
39    
40     #include <sys/socket.h> /* for AF_INET */
41    
42     /* { from mrt.h */
43    
44     typedef struct _prefix4_t {
45     u_short family; /* AF_INET | AF_INET6 */
46     u_short bitlen; /* same as mask? */
47     int ref_count; /* reference count */
48     struct in_addr sin;
49     } prefix4_t;
50    
51     typedef struct _prefix_t {
52     u_short family; /* AF_INET | AF_INET6 */
53     u_short bitlen; /* same as mask? */
54     int ref_count; /* reference count */
55     union {
56     struct in_addr sin;
57     #ifdef HAVE_IPV6
58     struct in6_addr sin6;
59     #endif /* IPV6 */
60     } add;
61     } prefix_t;
62    
63     /* } */
64    
65     typedef struct _patricia_node_t {
66     u_int bit; /* flag if this node used */
67     prefix_t *prefix; /* who we are in patricia tree */
68     struct _patricia_node_t *l, *r; /* left and right children */
69     struct _patricia_node_t *parent;/* may be used */
70     void *data; /* pointer to data */
71     void *user1; /* pointer to usr data (ex. route flap info) */
72     } patricia_node_t;
73    
74     typedef struct _patricia_tree_t {
75     patricia_node_t *head;
76     u_int maxbits; /* for IP, 32 bit addresses */
77     int num_active_node; /* for debug purpose */
78     } patricia_tree_t;
79    
80    
81     patricia_node_t *patricia_search_exact (patricia_tree_t *patricia, prefix_t *prefix);
82     patricia_node_t *patricia_search_best (patricia_tree_t *patricia, prefix_t *prefix);
83     patricia_node_t * patricia_search_best2 (patricia_tree_t *patricia, prefix_t *prefix,
84     int inclusive);
85     patricia_node_t *patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix);
86     void patricia_remove (patricia_tree_t *patricia, patricia_node_t *node);
87     patricia_tree_t *New_Patricia (int maxbits);
88     void Clear_Patricia (patricia_tree_t *patricia, void_fn_t func);
89     void Destroy_Patricia (patricia_tree_t *patricia, void_fn_t func);
90    
91     void patricia_process (patricia_tree_t *patricia, void_fn_t func);
92    
93     char *prefix_toa (prefix_t * prefix);
94    
95     /* { from demo.c */
96    
97     prefix_t *
98     ascii2prefix (int family, char *string);
99    
100     patricia_node_t *
101     make_and_lookup (patricia_tree_t *tree, char *string);
102    
103     /* } */
104    
105     #define PATRICIA_MAXBITS (sizeof(struct in6_addr) * 8)
106     #define PATRICIA_NBIT(x) (0x80 >> ((x) & 0x7f))
107     #define PATRICIA_NBYTE(x) ((x) >> 3)
108    
109     #define PATRICIA_DATA_GET(node, type) (type *)((node)->data)
110     #define PATRICIA_DATA_SET(node, value) ((node)->data = (void *)(value))
111    
112     #define PATRICIA_WALK(Xhead, Xnode) \
113     do { \
114     patricia_node_t *Xstack[PATRICIA_MAXBITS+1]; \
115     patricia_node_t **Xsp = Xstack; \
116     patricia_node_t *Xrn = (Xhead); \
117     while ((Xnode = Xrn)) { \
118     if (Xnode->prefix)
119    
120     #define PATRICIA_WALK_ALL(Xhead, Xnode) \
121     do { \
122     patricia_node_t *Xstack[PATRICIA_MAXBITS+1]; \
123     patricia_node_t **Xsp = Xstack; \
124     patricia_node_t *Xrn = (Xhead); \
125     while ((Xnode = Xrn)) { \
126     if (1)
127    
128     #define PATRICIA_WALK_BREAK { \
129     if (Xsp != Xstack) { \
130     Xrn = *(--Xsp); \
131     } else { \
132     Xrn = (patricia_node_t *) 0; \
133     } \
134     continue; }
135    
136     #define PATRICIA_WALK_END \
137     if (Xrn->l) { \
138     if (Xrn->r) { \
139     *Xsp++ = Xrn->r; \
140     } \
141     Xrn = Xrn->l; \
142     } else if (Xrn->r) { \
143     Xrn = Xrn->r; \
144     } else if (Xsp != Xstack) { \
145     Xrn = *(--Xsp); \
146     } else { \
147     Xrn = (patricia_node_t *) 0; \
148     } \
149     } \
150     } while (0)
151    
152     #endif /* _PATRICIA_H */