3 * Copyright (C) 1998 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 #ifndef _ZEBRA_TABLE_H
24 #define _ZEBRA_TABLE_H
27 * Forward declarations.
33 * route_table_delegate_t
35 * Function vector that can be used by a client to customize the
36 * behavior of one or more route tables.
38 typedef struct route_table_delegate_t_ route_table_delegate_t;
40 typedef struct route_node * (*route_table_create_node_func_t)
41 (route_table_delegate_t *, struct route_table *);
43 typedef void (*route_table_destroy_node_func_t)
44 (route_table_delegate_t *, struct route_table *, struct route_node *);
46 struct route_table_delegate_t_
48 route_table_create_node_func_t create_node;
49 route_table_destroy_node_func_t destroy_node;
52 /* Routing table top structure. */
55 struct route_node *top;
58 * Delegate that performs certain functions for this table.
60 route_table_delegate_t *delegate;
71 * Macro that defines all fields in a route node.
73 #define ROUTE_NODE_FIELDS \
74 /* Actual prefix of this radix. */ \
78 struct route_table *table; \
79 struct route_node *parent; \
80 struct route_node *link[2]; \
82 /* Lock of this radix */ \
85 /* Each node of route. */ \
92 /* Each routing entry. */
97 #define l_left link[0]
98 #define l_right link[1]
101 typedef struct route_table_iter_t_ route_table_iter_t;
106 RT_ITER_STATE_ITERATING,
107 RT_ITER_STATE_PAUSED,
109 } route_table_iter_state_t;
114 * Structure that holds state for iterating over a route table.
116 struct route_table_iter_t_
119 route_table_iter_state_t state;
122 * Routing table that we are iterating over. The caller must ensure
123 * that that table outlives the iterator.
125 struct route_table *table;
128 * The node that the iterator is currently on.
130 struct route_node *current;
133 * The last prefix that the iterator processed before it was paused.
135 struct prefix pause_prefix;
139 extern struct route_table *route_table_init (void);
141 extern struct route_table *
142 route_table_init_with_delegate (route_table_delegate_t *);
144 extern void route_table_finish (struct route_table *);
145 extern void route_unlock_node (struct route_node *node);
146 extern struct route_node *route_top (struct route_table *);
147 extern struct route_node *route_next (struct route_node *);
148 extern struct route_node *route_next_until (struct route_node *,
149 struct route_node *);
150 extern struct route_node *route_node_get (struct route_table *const,
151 const struct prefix *);
152 extern struct route_node *route_node_lookup (const struct route_table *,
153 const struct prefix *);
154 extern struct route_node *route_lock_node (struct route_node *node);
155 extern struct route_node *route_node_match (const struct route_table *,
156 const struct prefix *);
157 extern struct route_node *route_node_match_ipv4 (const struct route_table *,
158 const struct in_addr *);
160 extern struct route_node *route_node_match_ipv6 (const struct route_table *,
161 const struct in6_addr *);
162 #endif /* HAVE_IPV6 */
164 extern unsigned long route_table_count (const struct route_table *);
166 extern struct route_node *
167 route_table_get_next (const struct route_table *table, struct prefix *p);
169 route_table_prefix_iter_cmp (struct prefix *p1, struct prefix *p2);
172 * Iterator functions.
174 extern void route_table_iter_init (route_table_iter_t *iter,
175 struct route_table *table);
176 extern void route_table_iter_pause (route_table_iter_t *iter);
177 extern void route_table_iter_cleanup (route_table_iter_t *iter);
184 * route_table_iter_next
186 * Get the next node in the tree.
188 static inline struct route_node *
189 route_table_iter_next (route_table_iter_t * iter)
191 struct route_node *node;
196 case RT_ITER_STATE_INIT:
199 * We're just starting the iteration.
201 node = route_top (iter->table);
204 case RT_ITER_STATE_ITERATING:
205 node = route_next (iter->current);
208 case RT_ITER_STATE_PAUSED:
211 * Start with the node following pause_prefix.
213 node = route_table_get_next (iter->table, &iter->pause_prefix);
216 case RT_ITER_STATE_DONE:
223 iter->current = node;
225 iter->state = RT_ITER_STATE_ITERATING;
227 iter->state = RT_ITER_STATE_DONE;
233 * route_table_iter_is_done
235 * Returns TRUE if the iteration is complete.
238 route_table_iter_is_done (route_table_iter_t *iter)
240 return iter->state == RT_ITER_STATE_DONE;
244 * route_table_iter_started
246 * Returns TRUE if this iterator has started iterating over the tree.
249 route_table_iter_started (route_table_iter_t *iter)
251 return iter->state != RT_ITER_STATE_INIT;
254 #endif /* _ZEBRA_TABLE_H */