Switch from LOOKUP() to lookup() for rtm_type (see bug #401 for details).
[jleu-quagga.git] / ripd / rip_offset.c
blobe7d71f6cc0ff13b04413c3497eecb8d114c27f4b
1 /* RIP offset-list
2 * Copyright (C) 2000 Kunihiro Ishiguro <kunihiro@zebra.org>
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
22 #include <zebra.h>
24 #include "if.h"
25 #include "prefix.h"
26 #include "filter.h"
27 #include "command.h"
28 #include "linklist.h"
29 #include "memory.h"
31 #include "ripd/ripd.h"
33 #define RIP_OFFSET_LIST_IN 0
34 #define RIP_OFFSET_LIST_OUT 1
35 #define RIP_OFFSET_LIST_MAX 2
37 struct rip_offset_list
39 char *ifname;
41 struct
43 char *alist_name;
44 /* struct access_list *alist; */
45 int metric;
46 } direct[RIP_OFFSET_LIST_MAX];
49 static struct list *rip_offset_list_master;
51 static int
52 strcmp_safe (const char *s1, const char *s2)
54 if (s1 == NULL && s2 == NULL)
55 return 0;
56 if (s1 == NULL)
57 return -1;
58 if (s2 == NULL)
59 return 1;
60 return strcmp (s1, s2);
63 static struct rip_offset_list *
64 rip_offset_list_new (void)
66 struct rip_offset_list *new;
68 new = XMALLOC (MTYPE_RIP_OFFSET_LIST, sizeof (struct rip_offset_list));
69 memset (new, 0, sizeof (struct rip_offset_list));
70 return new;
73 static void
74 rip_offset_list_free (struct rip_offset_list *offset)
76 XFREE (MTYPE_RIP_OFFSET_LIST, offset);
79 static struct rip_offset_list *
80 rip_offset_list_lookup (const char *ifname)
82 struct rip_offset_list *offset;
83 struct listnode *node, *nnode;
85 for (ALL_LIST_ELEMENTS (rip_offset_list_master, node, nnode, offset))
87 if (strcmp_safe (offset->ifname, ifname) == 0)
88 return offset;
90 return NULL;
93 static struct rip_offset_list *
94 rip_offset_list_get (const char *ifname)
96 struct rip_offset_list *offset;
98 offset = rip_offset_list_lookup (ifname);
99 if (offset)
100 return offset;
102 offset = rip_offset_list_new ();
103 if (ifname)
104 offset->ifname = strdup (ifname);
105 listnode_add_sort (rip_offset_list_master, offset);
107 return offset;
110 static int
111 rip_offset_list_set (struct vty *vty, const char *alist, const char *direct_str,
112 const char *metric_str, const char *ifname)
114 int direct;
115 int metric;
116 struct rip_offset_list *offset;
118 /* Check direction. */
119 if (strncmp (direct_str, "i", 1) == 0)
120 direct = RIP_OFFSET_LIST_IN;
121 else if (strncmp (direct_str, "o", 1) == 0)
122 direct = RIP_OFFSET_LIST_OUT;
123 else
125 vty_out (vty, "Invalid direction: %s%s", direct_str, VTY_NEWLINE);
126 return CMD_WARNING;
129 /* Check metric. */
130 metric = atoi (metric_str);
131 if (metric < 0 || metric > 16)
133 vty_out (vty, "Invalid metric: %s%s", metric_str, VTY_NEWLINE);
134 return CMD_WARNING;
137 /* Get offset-list structure with interface name. */
138 offset = rip_offset_list_get (ifname);
140 if (offset->direct[direct].alist_name)
141 free (offset->direct[direct].alist_name);
142 offset->direct[direct].alist_name = strdup (alist);
143 offset->direct[direct].metric = metric;
145 return CMD_SUCCESS;
148 static int
149 rip_offset_list_unset (struct vty *vty, const char *alist,
150 const char *direct_str, const char *metric_str,
151 const char *ifname)
153 int direct;
154 int metric;
155 struct rip_offset_list *offset;
157 /* Check direction. */
158 if (strncmp (direct_str, "i", 1) == 0)
159 direct = RIP_OFFSET_LIST_IN;
160 else if (strncmp (direct_str, "o", 1) == 0)
161 direct = RIP_OFFSET_LIST_OUT;
162 else
164 vty_out (vty, "Invalid direction: %s%s", direct_str, VTY_NEWLINE);
165 return CMD_WARNING;
168 /* Check metric. */
169 metric = atoi (metric_str);
170 if (metric < 0 || metric > 16)
172 vty_out (vty, "Invalid metric: %s%s", metric_str, VTY_NEWLINE);
173 return CMD_WARNING;
176 /* Get offset-list structure with interface name. */
177 offset = rip_offset_list_lookup (ifname);
179 if (offset)
181 if (offset->direct[direct].alist_name)
182 free (offset->direct[direct].alist_name);
183 offset->direct[direct].alist_name = NULL;
185 if (offset->direct[RIP_OFFSET_LIST_IN].alist_name == NULL &&
186 offset->direct[RIP_OFFSET_LIST_OUT].alist_name == NULL)
188 listnode_delete (rip_offset_list_master, offset);
189 if (offset->ifname)
190 free (offset->ifname);
191 rip_offset_list_free (offset);
194 else
196 vty_out (vty, "Can't find offset-list%s", VTY_NEWLINE);
197 return CMD_WARNING;
199 return CMD_SUCCESS;
202 #define OFFSET_LIST_IN_NAME(O) ((O)->direct[RIP_OFFSET_LIST_IN].alist_name)
203 #define OFFSET_LIST_IN_METRIC(O) ((O)->direct[RIP_OFFSET_LIST_IN].metric)
205 #define OFFSET_LIST_OUT_NAME(O) ((O)->direct[RIP_OFFSET_LIST_OUT].alist_name)
206 #define OFFSET_LIST_OUT_METRIC(O) ((O)->direct[RIP_OFFSET_LIST_OUT].metric)
208 /* If metric is modifed return 1. */
210 rip_offset_list_apply_in (struct prefix_ipv4 *p, struct interface *ifp,
211 u_int32_t *metric)
213 struct rip_offset_list *offset;
214 struct access_list *alist;
216 /* Look up offset-list with interface name. */
217 offset = rip_offset_list_lookup (ifp->name);
218 if (offset && OFFSET_LIST_IN_NAME (offset))
220 alist = access_list_lookup (AFI_IP, OFFSET_LIST_IN_NAME (offset));
222 if (alist
223 && access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT)
225 *metric += OFFSET_LIST_IN_METRIC (offset);
226 return 1;
228 return 0;
230 /* Look up offset-list without interface name. */
231 offset = rip_offset_list_lookup (NULL);
232 if (offset && OFFSET_LIST_IN_NAME (offset))
234 alist = access_list_lookup (AFI_IP, OFFSET_LIST_IN_NAME (offset));
236 if (alist
237 && access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT)
239 *metric += OFFSET_LIST_IN_METRIC (offset);
240 return 1;
242 return 0;
244 return 0;
247 /* If metric is modifed return 1. */
249 rip_offset_list_apply_out (struct prefix_ipv4 *p, struct interface *ifp,
250 u_int32_t *metric)
252 struct rip_offset_list *offset;
253 struct access_list *alist;
255 /* Look up offset-list with interface name. */
256 offset = rip_offset_list_lookup (ifp->name);
257 if (offset && OFFSET_LIST_OUT_NAME (offset))
259 alist = access_list_lookup (AFI_IP, OFFSET_LIST_OUT_NAME (offset));
261 if (alist
262 && access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT)
264 *metric += OFFSET_LIST_OUT_METRIC (offset);
265 return 1;
267 return 0;
270 /* Look up offset-list without interface name. */
271 offset = rip_offset_list_lookup (NULL);
272 if (offset && OFFSET_LIST_OUT_NAME (offset))
274 alist = access_list_lookup (AFI_IP, OFFSET_LIST_OUT_NAME (offset));
276 if (alist
277 && access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT)
279 *metric += OFFSET_LIST_OUT_METRIC (offset);
280 return 1;
282 return 0;
284 return 0;
287 DEFUN (rip_offset_list,
288 rip_offset_list_cmd,
289 "offset-list WORD (in|out) <0-16>",
290 "Modify RIP metric\n"
291 "Access-list name\n"
292 "For incoming updates\n"
293 "For outgoing updates\n"
294 "Metric value\n")
296 return rip_offset_list_set (vty, argv[0], argv[1], argv[2], NULL);
299 DEFUN (rip_offset_list_ifname,
300 rip_offset_list_ifname_cmd,
301 "offset-list WORD (in|out) <0-16> IFNAME",
302 "Modify RIP metric\n"
303 "Access-list name\n"
304 "For incoming updates\n"
305 "For outgoing updates\n"
306 "Metric value\n"
307 "Interface to match\n")
309 return rip_offset_list_set (vty, argv[0], argv[1], argv[2], argv[3]);
312 DEFUN (no_rip_offset_list,
313 no_rip_offset_list_cmd,
314 "no offset-list WORD (in|out) <0-16>",
315 NO_STR
316 "Modify RIP metric\n"
317 "Access-list name\n"
318 "For incoming updates\n"
319 "For outgoing updates\n"
320 "Metric value\n")
322 return rip_offset_list_unset (vty, argv[0], argv[1], argv[2], NULL);
325 DEFUN (no_rip_offset_list_ifname,
326 no_rip_offset_list_ifname_cmd,
327 "no offset-list WORD (in|out) <0-16> IFNAME",
328 NO_STR
329 "Modify RIP metric\n"
330 "Access-list name\n"
331 "For incoming updates\n"
332 "For outgoing updates\n"
333 "Metric value\n"
334 "Interface to match\n")
336 return rip_offset_list_unset (vty, argv[0], argv[1], argv[2], argv[3]);
339 static int
340 offset_list_cmp (struct rip_offset_list *o1, struct rip_offset_list *o2)
342 return strcmp_safe (o1->ifname, o2->ifname);
345 static void
346 offset_list_del (struct rip_offset_list *offset)
348 if (OFFSET_LIST_IN_NAME (offset))
349 free (OFFSET_LIST_IN_NAME (offset));
350 if (OFFSET_LIST_OUT_NAME (offset))
351 free (OFFSET_LIST_OUT_NAME (offset));
352 if (offset->ifname)
353 free (offset->ifname);
354 rip_offset_list_free (offset);
357 void
358 rip_offset_init ()
360 rip_offset_list_master = list_new ();
361 rip_offset_list_master->cmp = (int (*)(void *, void *)) offset_list_cmp;
362 rip_offset_list_master->del = (void (*)(void *)) offset_list_del;
364 install_element (RIP_NODE, &rip_offset_list_cmd);
365 install_element (RIP_NODE, &rip_offset_list_ifname_cmd);
366 install_element (RIP_NODE, &no_rip_offset_list_cmd);
367 install_element (RIP_NODE, &no_rip_offset_list_ifname_cmd);
370 void
371 rip_offset_clean ()
373 list_delete (rip_offset_list_master);
375 rip_offset_list_master = list_new ();
376 rip_offset_list_master->cmp = (int (*)(void *, void *)) offset_list_cmp;
377 rip_offset_list_master->del = (void (*)(void *)) offset_list_del;
381 config_write_rip_offset_list (struct vty *vty)
383 struct listnode *node, *nnode;
384 struct rip_offset_list *offset;
386 for (ALL_LIST_ELEMENTS (rip_offset_list_master, node, nnode, offset))
388 if (! offset->ifname)
390 if (offset->direct[RIP_OFFSET_LIST_IN].alist_name)
391 vty_out (vty, " offset-list %s in %d%s",
392 offset->direct[RIP_OFFSET_LIST_IN].alist_name,
393 offset->direct[RIP_OFFSET_LIST_IN].metric,
394 VTY_NEWLINE);
395 if (offset->direct[RIP_OFFSET_LIST_OUT].alist_name)
396 vty_out (vty, " offset-list %s out %d%s",
397 offset->direct[RIP_OFFSET_LIST_OUT].alist_name,
398 offset->direct[RIP_OFFSET_LIST_OUT].metric,
399 VTY_NEWLINE);
401 else
403 if (offset->direct[RIP_OFFSET_LIST_IN].alist_name)
404 vty_out (vty, " offset-list %s in %d %s%s",
405 offset->direct[RIP_OFFSET_LIST_IN].alist_name,
406 offset->direct[RIP_OFFSET_LIST_IN].metric,
407 offset->ifname, VTY_NEWLINE);
408 if (offset->direct[RIP_OFFSET_LIST_OUT].alist_name)
409 vty_out (vty, " offset-list %s out %d %s%s",
410 offset->direct[RIP_OFFSET_LIST_OUT].alist_name,
411 offset->direct[RIP_OFFSET_LIST_OUT].metric,
412 offset->ifname, VTY_NEWLINE);
416 return 0;