lib: simplify distribute.c's code.

Use loops and variables instead of doing each cases by hand.
Use static functions instead of rewriting code.
This commit is contained in:
Matthieu Boutier 2016-09-22 18:11:04 -03:00 committed by Donald Sharp
parent 0fc452dc57
commit ee5bb56117

View File

@ -49,22 +49,35 @@ distribute_new (void)
static void static void
distribute_free (struct distribute *dist) distribute_free (struct distribute *dist)
{ {
int i = 0;
if (dist->ifname) if (dist->ifname)
XFREE (MTYPE_DISTRIBUTE_IFNAME, dist->ifname); XFREE (MTYPE_DISTRIBUTE_IFNAME, dist->ifname);
if (dist->list[DISTRIBUTE_IN]) for (i = 0; i < DISTRIBUTE_MAX; i++)
XFREE(MTYPE_DISTRIBUTE_NAME, dist->list[DISTRIBUTE_IN]); if (dist->list[i])
if (dist->list[DISTRIBUTE_OUT]) XFREE(MTYPE_DISTRIBUTE_NAME, dist->list[i]);
XFREE(MTYPE_DISTRIBUTE_NAME, dist->list[DISTRIBUTE_OUT]);
if (dist->prefix[DISTRIBUTE_IN]) for (i = 0; i < DISTRIBUTE_MAX; i++)
XFREE(MTYPE_DISTRIBUTE_NAME, dist->prefix[DISTRIBUTE_IN]); if (dist->prefix[i])
if (dist->prefix[DISTRIBUTE_OUT]) XFREE(MTYPE_DISTRIBUTE_NAME, dist->prefix[i]);
XFREE(MTYPE_DISTRIBUTE_NAME, dist->prefix[DISTRIBUTE_OUT]);
XFREE (MTYPE_DISTRIBUTE, dist); XFREE (MTYPE_DISTRIBUTE, dist);
} }
static void
distribute_free_if_empty(struct distribute *dist)
{
int i;
for (i = 0; i < DISTRIBUTE_MAX; i++)
if (dist->list[i] != NULL || dist->prefix[i] != NULL)
return;
hash_release (disthash, dist);
distribute_free (dist);
}
/* Lookup interface's distribute list. */ /* Lookup interface's distribute list. */
struct distribute * struct distribute *
distribute_lookup (const char *ifname) distribute_lookup (const char *ifname)
@ -156,18 +169,9 @@ distribute_list_set (const char *ifname, enum distribute_type type,
dist = distribute_get (ifname); dist = distribute_get (ifname);
if (type == DISTRIBUTE_IN) if (dist->list[type])
{ XFREE(MTYPE_DISTRIBUTE_NAME, dist->list[type]);
if (dist->list[DISTRIBUTE_IN]) dist->list[type] = XSTRDUP(MTYPE_DISTRIBUTE_NAME, alist_name);
XFREE(MTYPE_DISTRIBUTE_NAME, dist->list[DISTRIBUTE_IN]);
dist->list[DISTRIBUTE_IN] = XSTRDUP(MTYPE_DISTRIBUTE_NAME, alist_name);
}
if (type == DISTRIBUTE_OUT)
{
if (dist->list[DISTRIBUTE_OUT])
XFREE(MTYPE_DISTRIBUTE_NAME, dist->list[DISTRIBUTE_OUT]);
dist->list[DISTRIBUTE_OUT] = XSTRDUP(MTYPE_DISTRIBUTE_NAME, alist_name);
}
/* Apply this distribute-list to the interface. */ /* Apply this distribute-list to the interface. */
(*distribute_add_hook) (dist); (*distribute_add_hook) (dist);
@ -187,41 +191,19 @@ distribute_list_unset (const char *ifname, enum distribute_type type,
if (!dist) if (!dist)
return 0; return 0;
if (type == DISTRIBUTE_IN) if (!dist->list[type])
{ return 0;
if (!dist->list[DISTRIBUTE_IN]) if (strcmp (dist->list[type], alist_name) != 0)
return 0; return 0;
if (strcmp (dist->list[DISTRIBUTE_IN], alist_name) != 0)
return 0;
XFREE(MTYPE_DISTRIBUTE_NAME, dist->list[DISTRIBUTE_IN]); XFREE(MTYPE_DISTRIBUTE_NAME, dist->list[type]);
dist->list[DISTRIBUTE_IN] = NULL; dist->list[type] = NULL;
}
if (type == DISTRIBUTE_OUT)
{
if (!dist->list[DISTRIBUTE_OUT])
return 0;
if (strcmp (dist->list[DISTRIBUTE_OUT], alist_name) != 0)
return 0;
XFREE(MTYPE_DISTRIBUTE_NAME, dist->list[DISTRIBUTE_OUT]);
dist->list[DISTRIBUTE_OUT] = NULL;
}
/* Apply this distribute-list to the interface. */ /* Apply this distribute-list to the interface. */
(*distribute_delete_hook) (dist); (*distribute_delete_hook) (dist);
/* If both out and in is NULL then free distribute list. */ /* If all dist are NULL, then free distribute list. */
if (dist->list[DISTRIBUTE_IN] == NULL && distribute_free_if_empty(dist);
dist->list[DISTRIBUTE_OUT] == NULL &&
dist->prefix[DISTRIBUTE_IN] == NULL &&
dist->prefix[DISTRIBUTE_OUT] == NULL)
{
hash_release (disthash, dist);
distribute_free (dist);
}
return 1; return 1;
} }
@ -234,18 +216,9 @@ distribute_list_prefix_set (const char *ifname, enum distribute_type type,
dist = distribute_get (ifname); dist = distribute_get (ifname);
if (type == DISTRIBUTE_IN) if (dist->prefix[type])
{ XFREE(MTYPE_DISTRIBUTE_NAME, dist->prefix[type]);
if (dist->prefix[DISTRIBUTE_IN]) dist->prefix[type] = XSTRDUP(MTYPE_DISTRIBUTE_NAME, plist_name);
XFREE(MTYPE_DISTRIBUTE_NAME, dist->prefix[DISTRIBUTE_IN]);
dist->prefix[DISTRIBUTE_IN] = XSTRDUP(MTYPE_DISTRIBUTE_NAME, plist_name);
}
if (type == DISTRIBUTE_OUT)
{
if (dist->prefix[DISTRIBUTE_OUT])
XFREE(MTYPE_DISTRIBUTE_NAME, dist->prefix[DISTRIBUTE_OUT]);
dist->prefix[DISTRIBUTE_OUT] = XSTRDUP(MTYPE_DISTRIBUTE_NAME, plist_name);
}
/* Apply this distribute-list to the interface. */ /* Apply this distribute-list to the interface. */
(*distribute_add_hook) (dist); (*distribute_add_hook) (dist);
@ -265,41 +238,19 @@ distribute_list_prefix_unset (const char *ifname, enum distribute_type type,
if (!dist) if (!dist)
return 0; return 0;
if (type == DISTRIBUTE_IN) if (!dist->prefix[type])
{ return 0;
if (!dist->prefix[DISTRIBUTE_IN]) if (strcmp (dist->prefix[type], plist_name) != 0)
return 0; return 0;
if (strcmp (dist->prefix[DISTRIBUTE_IN], plist_name) != 0)
return 0;
XFREE(MTYPE_DISTRIBUTE_NAME, dist->prefix[DISTRIBUTE_IN]); XFREE(MTYPE_DISTRIBUTE_NAME, dist->prefix[type]);
dist->prefix[DISTRIBUTE_IN] = NULL; dist->prefix[type] = NULL;
}
if (type == DISTRIBUTE_OUT)
{
if (!dist->prefix[DISTRIBUTE_OUT])
return 0;
if (strcmp (dist->prefix[DISTRIBUTE_OUT], plist_name) != 0)
return 0;
XFREE(MTYPE_DISTRIBUTE_NAME, dist->prefix[DISTRIBUTE_OUT]);
dist->prefix[DISTRIBUTE_OUT] = NULL;
}
/* Apply this distribute-list to the interface. */ /* Apply this distribute-list to the interface. */
(*distribute_delete_hook) (dist); (*distribute_delete_hook) (dist);
/* If both out and in is NULL then free distribute list. */ /* If all dist are NULL, then free distribute list. */
if (dist->list[DISTRIBUTE_IN] == NULL && distribute_free_if_empty(dist);
dist->list[DISTRIBUTE_OUT] == NULL &&
dist->prefix[DISTRIBUTE_IN] == NULL &&
dist->prefix[DISTRIBUTE_OUT] == NULL)
{
hash_release (disthash, dist);
distribute_free (dist);
}
return 1; return 1;
} }
@ -623,80 +574,96 @@ ALIAS (no_distribute_list_prefix, no_ipv6_distribute_list_prefix_cmd,
"Filter outgoing routing updates\n" "Filter outgoing routing updates\n"
"Interface name\n") "Interface name\n")
static int
distribute_print (struct vty *vty, char *tab[], int is_prefix,
enum distribute_type type, int has_print)
{
if (tab[type]) {
vty_out (vty, "%s %s%s",
has_print ? "," : "",
is_prefix ? "(prefix-list) " : "",
tab[type]);
return 1;
}
return has_print;
}
int int
config_show_distribute (struct vty *vty) config_show_distribute (struct vty *vty)
{ {
unsigned int i; unsigned int i;
int has_print = 0;
struct hash_backet *mp; struct hash_backet *mp;
struct distribute *dist; struct distribute *dist;
/* Output filter configuration. */ /* Output filter configuration. */
dist = distribute_lookup (NULL); dist = distribute_lookup (NULL);
if (dist && (dist->list[DISTRIBUTE_OUT] || dist->prefix[DISTRIBUTE_OUT])) vty_out(vty, " Outgoing update filter list for all interface is");
has_print = 0;
if (dist)
{ {
vty_out (vty, " Outgoing update filter list for all interface is"); has_print = distribute_print(vty, dist->list, 0,
if (dist->list[DISTRIBUTE_OUT]) DISTRIBUTE_OUT, has_print);
vty_out (vty, " %s", dist->list[DISTRIBUTE_OUT]); has_print = distribute_print(vty, dist->prefix, 1,
if (dist->prefix[DISTRIBUTE_OUT]) DISTRIBUTE_OUT, has_print);
vty_out (vty, "%s (prefix-list) %s",
dist->list[DISTRIBUTE_OUT] ? "," : "",
dist->prefix[DISTRIBUTE_OUT]);
vty_out (vty, "%s", VTY_NEWLINE);
} }
if (has_print)
vty_out (vty, "%s", VTY_NEWLINE);
else else
vty_out (vty, " Outgoing update filter list for all interface is not set%s", VTY_NEWLINE); vty_out (vty, " not set%s", VTY_NEWLINE);
for (i = 0; i < disthash->size; i++) for (i = 0; i < disthash->size; i++)
for (mp = disthash->index[i]; mp; mp = mp->next) for (mp = disthash->index[i]; mp; mp = mp->next)
{ {
dist = mp->data; dist = mp->data;
if (dist->ifname) if (dist->ifname)
if (dist->list[DISTRIBUTE_OUT] || dist->prefix[DISTRIBUTE_OUT]) {
{ vty_out (vty, " %s filtered by", dist->ifname);
vty_out (vty, " %s filtered by", dist->ifname); has_print = 0;
if (dist->list[DISTRIBUTE_OUT]) has_print = distribute_print(vty, dist->list, 0,
vty_out (vty, " %s", dist->list[DISTRIBUTE_OUT]); DISTRIBUTE_OUT, has_print);
if (dist->prefix[DISTRIBUTE_OUT]) has_print = distribute_print(vty, dist->prefix, 1,
vty_out (vty, "%s (prefix-list) %s", DISTRIBUTE_OUT, has_print);
dist->list[DISTRIBUTE_OUT] ? "," : "", if (has_print)
dist->prefix[DISTRIBUTE_OUT]); vty_out (vty, "%s", VTY_NEWLINE);
vty_out (vty, "%s", VTY_NEWLINE); else
} vty_out(vty, " nothing%s", VTY_NEWLINE);
}
} }
/* Input filter configuration. */ /* Input filter configuration. */
dist = distribute_lookup (NULL); dist = distribute_lookup (NULL);
if (dist && (dist->list[DISTRIBUTE_IN] || dist->prefix[DISTRIBUTE_IN])) vty_out(vty, " Incoming update filter list for all interface is");
has_print = 0;
if (dist)
{ {
vty_out (vty, " Incoming update filter list for all interface is"); has_print = distribute_print(vty, dist->list, 0,
if (dist->list[DISTRIBUTE_IN]) DISTRIBUTE_IN, has_print);
vty_out (vty, " %s", dist->list[DISTRIBUTE_IN]); has_print = distribute_print(vty, dist->prefix, 1,
if (dist->prefix[DISTRIBUTE_IN]) DISTRIBUTE_IN, has_print); }
vty_out (vty, "%s (prefix-list) %s", if (has_print)
dist->list[DISTRIBUTE_IN] ? "," : "", vty_out (vty, "%s", VTY_NEWLINE);
dist->prefix[DISTRIBUTE_IN]);
vty_out (vty, "%s", VTY_NEWLINE);
}
else else
vty_out (vty, " Incoming update filter list for all interface is not set%s", VTY_NEWLINE); vty_out (vty, " not set%s", VTY_NEWLINE);
for (i = 0; i < disthash->size; i++) for (i = 0; i < disthash->size; i++)
for (mp = disthash->index[i]; mp; mp = mp->next) for (mp = disthash->index[i]; mp; mp = mp->next)
{ {
dist = mp->data; dist = mp->data;
if (dist->ifname) if (dist->ifname)
if (dist->list[DISTRIBUTE_IN] || dist->prefix[DISTRIBUTE_IN]) {
{ vty_out (vty, " %s filtered by", dist->ifname);
vty_out (vty, " %s filtered by", dist->ifname); has_print = 0;
if (dist->list[DISTRIBUTE_IN]) has_print = distribute_print(vty, dist->list, 0,
vty_out (vty, " %s", dist->list[DISTRIBUTE_IN]); DISTRIBUTE_IN, has_print);
if (dist->prefix[DISTRIBUTE_IN]) has_print = distribute_print(vty, dist->prefix, 1,
vty_out (vty, "%s (prefix-list) %s", DISTRIBUTE_IN, has_print);
dist->list[DISTRIBUTE_IN] ? "," : "", if (has_print)
dist->prefix[DISTRIBUTE_IN]); vty_out (vty, "%s", VTY_NEWLINE);
vty_out (vty, "%s", VTY_NEWLINE); else
} vty_out(vty, " nothing%s", VTY_NEWLINE);
}
} }
return 0; return 0;
} }
@ -706,6 +673,8 @@ int
config_write_distribute (struct vty *vty) config_write_distribute (struct vty *vty)
{ {
unsigned int i; unsigned int i;
int j;
int output;
struct hash_backet *mp; struct hash_backet *mp;
int write = 0; int write = 0;
@ -716,38 +685,23 @@ config_write_distribute (struct vty *vty)
dist = mp->data; dist = mp->data;
if (dist->list[DISTRIBUTE_IN]) for (j = 0; j < DISTRIBUTE_MAX; j++)
{ if (dist->list[j]) {
vty_out (vty, " distribute-list %s in %s%s", output = j == DISTRIBUTE_OUT;
dist->list[DISTRIBUTE_IN], vty_out (vty, " distribute-list %s %s %s%s",
dist->list[j],
output ? "out" : "in",
dist->ifname ? dist->ifname : "", dist->ifname ? dist->ifname : "",
VTY_NEWLINE); VTY_NEWLINE);
write++; write++;
} }
if (dist->list[DISTRIBUTE_OUT]) for (j = 0; j < DISTRIBUTE_MAX; j++)
{ if (dist->prefix[j]) {
vty_out (vty, " distribute-list %s out %s%s", output = j == DISTRIBUTE_OUT;
vty_out (vty, " distribute-list prefix %s %s %s%s",
dist->list[DISTRIBUTE_OUT], dist->prefix[j],
dist->ifname ? dist->ifname : "", output ? "out" : "in",
VTY_NEWLINE);
write++;
}
if (dist->prefix[DISTRIBUTE_IN])
{
vty_out (vty, " distribute-list prefix %s in %s%s",
dist->prefix[DISTRIBUTE_IN],
dist->ifname ? dist->ifname : "",
VTY_NEWLINE);
write++;
}
if (dist->prefix[DISTRIBUTE_OUT])
{
vty_out (vty, " distribute-list prefix %s out %s%s",
dist->prefix[DISTRIBUTE_OUT],
dist->ifname ? dist->ifname : "", dist->ifname ? dist->ifname : "",
VTY_NEWLINE); VTY_NEWLINE);
write++; write++;