Extended MOTD with GUI
[tomato.git] / release / src / router / zebra / ospfd / ospf_lsdb.c
blobe4edb2b49e74c4ef9b0da2a7d23247323188e54d
1 /*
2 * OSPF LSDB support.
3 * Copyright (C) 1999, 2000 Alex Zinin, Kunihiro Ishiguro, Toshiaki Takada
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
10 * later version.
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
20 * 02111-1307, USA.
23 #include <zebra.h>
25 #include "prefix.h"
26 #include "table.h"
27 #include "memory.h"
28 #include "log.h"
30 #include "ospfd/ospfd.h"
31 #include "ospfd/ospf_asbr.h"
32 #include "ospfd/ospf_lsa.h"
33 #include "ospfd/ospf_lsdb.h"
35 struct ospf_lsdb *
36 ospf_lsdb_new ()
38 struct ospf_lsdb *new;
40 new = XCALLOC (MTYPE_OSPF_LSDB, sizeof (struct ospf_lsdb));
41 ospf_lsdb_init (new);
43 return new;
46 void
47 ospf_lsdb_init (struct ospf_lsdb *lsdb)
49 int i;
51 for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++)
52 lsdb->type[i].db = route_table_init ();
55 void
56 ospf_lsdb_free (struct ospf_lsdb *lsdb)
58 ospf_lsdb_cleanup (lsdb);
59 XFREE (MTYPE_OSPF_LSDB, lsdb);
62 void
63 ospf_lsdb_cleanup (struct ospf_lsdb *lsdb)
65 int i;
66 assert (lsdb);
67 assert (lsdb->total == 0);
69 ospf_lsdb_delete_all (lsdb);
71 for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++)
72 route_table_finish (lsdb->type[i].db);
75 void
76 lsdb_prefix_set (struct prefix_ls *lp, struct ospf_lsa *lsa)
78 memset (lp, 0, sizeof (struct prefix_ls));
79 lp->family = 0;
80 lp->prefixlen = 64;
81 lp->id = lsa->data->id;
82 lp->adv_router = lsa->data->adv_router;
85 /* Add new LSA to lsdb. */
86 void
87 ospf_lsdb_add (struct ospf_lsdb *lsdb, struct ospf_lsa *lsa)
89 struct route_table *table;
90 struct prefix_ls lp;
91 struct route_node *rn;
93 table = lsdb->type[lsa->data->type].db;
94 lsdb_prefix_set (&lp, lsa);
95 rn = route_node_get (table, (struct prefix *)&lp);
96 if (!rn->info)
98 if (IS_LSA_SELF (lsa))
99 lsdb->type[lsa->data->type].count_self++;
100 lsdb->type[lsa->data->type].count++;
101 lsdb->total++;
103 else
105 if (rn->info == lsa)
106 return;
108 ospf_lsa_unlock (rn->info);
109 route_unlock_node (rn);
112 #ifdef MONITOR_LSDB_CHANGE
113 if (lsdb->new_lsa_hook != NULL)
114 (* lsdb->new_lsa_hook)(lsa);
115 #endif /* MONITOR_LSDB_CHANGE */
116 rn->info = ospf_lsa_lock (lsa);
119 void
120 ospf_lsdb_delete (struct ospf_lsdb *lsdb, struct ospf_lsa *lsa)
122 struct route_table *table;
123 struct prefix_ls lp;
124 struct route_node *rn;
126 table = lsdb->type[lsa->data->type].db;
127 lsdb_prefix_set (&lp, lsa);
128 rn = route_node_lookup (table, (struct prefix *) &lp);
129 if (rn)
130 if (rn->info == lsa)
132 if (IS_LSA_SELF (lsa))
133 lsdb->type[lsa->data->type].count_self--;
134 lsdb->type[lsa->data->type].count--;
135 lsdb->total--;
136 rn->info = NULL;
137 route_unlock_node (rn);
138 route_unlock_node (rn);
139 #ifdef MONITOR_LSDB_CHANGE
140 if (lsdb->del_lsa_hook != NULL)
141 (* lsdb->del_lsa_hook)(lsa);
142 #endif /* MONITOR_LSDB_CHANGE */
143 ospf_lsa_unlock (lsa);
144 return;
148 void
149 ospf_lsdb_delete_all (struct ospf_lsdb *lsdb)
151 struct route_table *table;
152 struct route_node *rn;
153 struct ospf_lsa *lsa;
154 int i;
156 for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++)
158 table = lsdb->type[i].db;
159 for (rn = route_top (table); rn; rn = route_next (rn))
160 if ((lsa = (rn->info)) != NULL)
162 if (IS_LSA_SELF (lsa))
163 lsdb->type[i].count_self--;
164 lsdb->type[i].count--;
165 lsdb->total--;
166 rn->info = NULL;
167 route_unlock_node (rn);
168 #ifdef MONITOR_LSDB_CHANGE
169 if (lsdb->del_lsa_hook != NULL)
170 (* lsdb->del_lsa_hook)(lsa);
171 #endif /* MONITOR_LSDB_CHANGE */
172 ospf_lsa_unlock (lsa);
177 struct ospf_lsa *
178 ospf_lsdb_lookup (struct ospf_lsdb *lsdb, struct ospf_lsa *lsa)
180 struct route_table *table;
181 struct prefix_ls lp;
182 struct route_node *rn;
183 struct ospf_lsa *find;
185 table = lsdb->type[lsa->data->type].db;
186 lsdb_prefix_set (&lp, lsa);
187 rn = route_node_lookup (table, (struct prefix *) &lp);
188 if (rn)
190 find = rn->info;
191 route_unlock_node (rn);
192 return find;
194 return NULL;
197 struct ospf_lsa *
198 ospf_lsdb_lookup_by_id (struct ospf_lsdb *lsdb, u_char type,
199 struct in_addr id, struct in_addr adv_router)
201 struct route_table *table;
202 struct prefix_ls lp;
203 struct route_node *rn;
204 struct ospf_lsa *find;
206 table = lsdb->type[type].db;
208 memset (&lp, 0, sizeof (struct prefix_ls));
209 lp.family = 0;
210 lp.prefixlen = 64;
211 lp.id = id;
212 lp.adv_router = adv_router;
214 rn = route_node_lookup (table, (struct prefix *) &lp);
215 if (rn)
217 find = rn->info;
218 route_unlock_node (rn);
219 return find;
221 return NULL;
224 struct ospf_lsa *
225 ospf_lsdb_lookup_by_id_next (struct ospf_lsdb *lsdb, u_char type,
226 struct in_addr id, struct in_addr adv_router,
227 int first)
229 struct route_table *table;
230 struct prefix_ls lp;
231 struct route_node *rn;
232 struct ospf_lsa *find;
234 table = lsdb->type[type].db;
236 memset (&lp, 0, sizeof (struct prefix_ls));
237 lp.family = 0;
238 lp.prefixlen = 64;
239 lp.id = id;
240 lp.adv_router = adv_router;
242 if (first)
243 rn = route_top (table);
244 else
246 rn = route_node_get (table, (struct prefix *) &lp);
247 rn = route_next (rn);
250 for (; rn; rn = route_next (rn))
251 if (rn->info)
252 break;
254 if (rn && rn->info)
256 find = rn->info;
257 route_unlock_node (rn);
258 return find;
260 return NULL;
263 unsigned long
264 ospf_lsdb_count_all (struct ospf_lsdb *lsdb)
266 return lsdb->total;
269 unsigned long
270 ospf_lsdb_count (struct ospf_lsdb *lsdb, int type)
272 return lsdb->type[type].count;
275 unsigned long
276 ospf_lsdb_count_self (struct ospf_lsdb *lsdb, int type)
278 return lsdb->type[type].count_self;
281 unsigned long
282 ospf_lsdb_isempty (struct ospf_lsdb *lsdb)
284 return (lsdb->total == 0);