Initial revision
diff --git a/lib/distribute.c b/lib/distribute.c
new file mode 100644
index 0000000..d5893a5
--- /dev/null
+++ b/lib/distribute.c
@@ -0,0 +1,709 @@
+/* Distribute list functions
+ * Copyright (C) 1998, 1999 Kunihiro Ishiguro
+ *
+ * This file is part of GNU Zebra.
+ *
+ * GNU Zebra is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * GNU Zebra is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GNU Zebra; see the file COPYING.  If not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include <zebra.h>
+
+#include "hash.h"
+#include "if.h"
+#include "filter.h"
+#include "command.h"
+#include "distribute.h"
+#include "memory.h"
+
+/* Hash of distribute list. */
+struct hash *disthash;
+
+/* Hook functions. */
+void (*distribute_add_hook) (struct distribute *);
+void (*distribute_delete_hook) (struct distribute *);
+
+struct distribute *
+distribute_new ()
+{
+  struct distribute *new;
+
+  new = XMALLOC (MTYPE_DISTRIBUTE, sizeof (struct distribute));
+  memset (new, 0, sizeof (struct distribute));
+
+  return new;
+}
+
+/* Free distribute object. */
+void
+distribute_free (struct distribute *dist)
+{
+  if (dist->ifname)
+    free (dist->ifname);
+
+  if (dist->list[DISTRIBUTE_IN])
+    free (dist->list[DISTRIBUTE_IN]);
+  if (dist->list[DISTRIBUTE_OUT])
+    free (dist->list[DISTRIBUTE_OUT]);
+
+  if (dist->prefix[DISTRIBUTE_IN])
+    free (dist->prefix[DISTRIBUTE_IN]);
+  if (dist->prefix[DISTRIBUTE_OUT])
+    free (dist->prefix[DISTRIBUTE_OUT]);
+
+  XFREE (MTYPE_DISTRIBUTE, dist);
+}
+
+/* Lookup interface's distribute list. */
+struct distribute *
+distribute_lookup (char *ifname)
+{
+  struct distribute key;
+  struct distribute *dist;
+
+  key.ifname = ifname;
+
+  dist = hash_lookup (disthash, &key);
+  
+  return dist;
+}
+
+void
+distribute_list_add_hook (void (*func) (struct distribute *))
+{
+  distribute_add_hook = func;
+}
+
+void
+distribute_list_delete_hook (void (*func) (struct distribute *))
+{
+  distribute_delete_hook = func;
+}
+
+void *
+distribute_hash_alloc (struct distribute *arg)
+{
+  struct distribute *dist;
+
+  dist = distribute_new ();
+  if (arg->ifname)
+    dist->ifname = strdup (arg->ifname);
+  else
+    dist->ifname = NULL;
+  return dist;
+}
+
+/* Make new distribute list and push into hash. */
+struct distribute *
+distribute_get (char *ifname)
+{
+  struct distribute key;
+
+  key.ifname = ifname;
+
+  return hash_get (disthash, &key, distribute_hash_alloc);
+}
+
+unsigned int
+distribute_hash_make (struct distribute *dist)
+{
+  unsigned int key;
+  int i;
+
+  key = 0;
+  if (dist->ifname)
+    for (i = 0; i < strlen (dist->ifname); i++)
+      key += dist->ifname[i];
+
+  return key;
+}
+
+/* If two distribute-list have same value then return 1 else return
+   0. This function is used by hash package. */
+int
+distribute_cmp (struct distribute *dist1, struct distribute *dist2)
+{
+  if (dist1->ifname && dist2->ifname)
+    if (strcmp (dist1->ifname, dist2->ifname) == 0)
+      return 1;
+  if (! dist1->ifname && ! dist2->ifname)
+    return 1;
+  return 0;
+}
+
+/* Set access-list name to the distribute list. */
+struct distribute *
+distribute_list_set (char *ifname, enum distribute_type type, char *alist_name)
+{
+  struct distribute *dist;
+
+  dist = distribute_get (ifname);
+
+  if (type == DISTRIBUTE_IN)
+    {
+      if (dist->list[DISTRIBUTE_IN])
+	free (dist->list[DISTRIBUTE_IN]);
+      dist->list[DISTRIBUTE_IN] = strdup (alist_name);
+    }
+  if (type == DISTRIBUTE_OUT)
+    {
+      if (dist->list[DISTRIBUTE_OUT])
+	free (dist->list[DISTRIBUTE_OUT]);
+      dist->list[DISTRIBUTE_OUT] = strdup (alist_name);
+    }
+
+  /* Apply this distribute-list to the interface. */
+  (*distribute_add_hook) (dist);
+  
+  return dist;
+}
+
+/* Unset distribute-list.  If matched distribute-list exist then
+   return 1. */
+int
+distribute_list_unset (char *ifname, enum distribute_type type, 
+		       char *alist_name)
+{
+  struct distribute *dist;
+
+  dist = distribute_lookup (ifname);
+  if (!dist)
+    return 0;
+
+  if (type == DISTRIBUTE_IN)
+    {
+      if (!dist->list[DISTRIBUTE_IN])
+	return 0;
+      if (strcmp (dist->list[DISTRIBUTE_IN], alist_name) != 0)
+	return 0;
+
+      free (dist->list[DISTRIBUTE_IN]);
+      dist->list[DISTRIBUTE_IN] = NULL;      
+    }
+
+  if (type == DISTRIBUTE_OUT)
+    {
+      if (!dist->list[DISTRIBUTE_OUT])
+	return 0;
+      if (strcmp (dist->list[DISTRIBUTE_OUT], alist_name) != 0)
+	return 0;
+
+      free (dist->list[DISTRIBUTE_OUT]);
+      dist->list[DISTRIBUTE_OUT] = NULL;      
+    }
+
+  /* Apply this distribute-list to the interface. */
+  (*distribute_delete_hook) (dist);
+
+  /* If both out and in is NULL then free distribute list. */
+  if (dist->list[DISTRIBUTE_IN] == NULL &&
+      dist->list[DISTRIBUTE_OUT] == NULL &&
+      dist->prefix[DISTRIBUTE_IN] == NULL &&
+      dist->prefix[DISTRIBUTE_OUT] == NULL)
+    {
+      hash_release (disthash, dist);
+      distribute_free (dist);
+    }
+
+  return 1;
+}
+
+/* Set access-list name to the distribute list. */
+struct distribute *
+distribute_list_prefix_set (char *ifname, enum distribute_type type,
+			    char *plist_name)
+{
+  struct distribute *dist;
+
+  dist = distribute_get (ifname);
+
+  if (type == DISTRIBUTE_IN)
+    {
+      if (dist->prefix[DISTRIBUTE_IN])
+	free (dist->prefix[DISTRIBUTE_IN]);
+      dist->prefix[DISTRIBUTE_IN] = strdup (plist_name);
+    }
+  if (type == DISTRIBUTE_OUT)
+    {
+      if (dist->prefix[DISTRIBUTE_OUT])
+	free (dist->prefix[DISTRIBUTE_OUT]);
+      dist->prefix[DISTRIBUTE_OUT] = strdup (plist_name);
+    }
+
+  /* Apply this distribute-list to the interface. */
+  (*distribute_add_hook) (dist);
+  
+  return dist;
+}
+
+/* Unset distribute-list.  If matched distribute-list exist then
+   return 1. */
+int
+distribute_list_prefix_unset (char *ifname, enum distribute_type type,
+			      char *plist_name)
+{
+  struct distribute *dist;
+
+  dist = distribute_lookup (ifname);
+  if (!dist)
+    return 0;
+
+  if (type == DISTRIBUTE_IN)
+    {
+      if (!dist->prefix[DISTRIBUTE_IN])
+	return 0;
+      if (strcmp (dist->prefix[DISTRIBUTE_IN], plist_name) != 0)
+	return 0;
+
+      free (dist->prefix[DISTRIBUTE_IN]);
+      dist->prefix[DISTRIBUTE_IN] = NULL;      
+    }
+
+  if (type == DISTRIBUTE_OUT)
+    {
+      if (!dist->prefix[DISTRIBUTE_OUT])
+	return 0;
+      if (strcmp (dist->prefix[DISTRIBUTE_OUT], plist_name) != 0)
+	return 0;
+
+      free (dist->prefix[DISTRIBUTE_OUT]);
+      dist->prefix[DISTRIBUTE_OUT] = NULL;      
+    }
+
+  /* Apply this distribute-list to the interface. */
+  (*distribute_delete_hook) (dist);
+
+  /* If both out and in is NULL then free distribute list. */
+  if (dist->list[DISTRIBUTE_IN] == NULL &&
+      dist->list[DISTRIBUTE_OUT] == NULL &&
+      dist->prefix[DISTRIBUTE_IN] == NULL &&
+      dist->prefix[DISTRIBUTE_OUT] == NULL)
+    {
+      hash_release (disthash, dist);
+      distribute_free (dist);
+    }
+
+  return 1;
+}
+
+DEFUN (distribute_list_all,
+       distribute_list_all_cmd,
+       "distribute-list WORD (in|out)",
+       "Filter networks in routing updates\n"
+       "Access-list name\n"
+       "Filter incoming routing updates\n"
+       "Filter outgoing routing updates\n")
+{
+  enum distribute_type type;
+  struct distribute *dist;
+
+  /* Check of distribute list type. */
+  if (strncmp (argv[1], "i", 1) == 0)
+    type = DISTRIBUTE_IN;
+  else if (strncmp (argv[1], "o", 1) == 0)
+    type = DISTRIBUTE_OUT;
+  else
+    {
+      vty_out (vty, "distribute list direction must be [in|out]%s",
+	       VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+
+  /* Get interface name corresponding distribute list. */
+  dist = distribute_list_set (NULL, type, argv[0]);
+
+  return CMD_SUCCESS;
+}
+
+DEFUN (no_distribute_list_all,
+       no_distribute_list_all_cmd,
+       "no distribute-list WORD (in|out)",
+       NO_STR
+       "Filter networks in routing updates\n"
+       "Access-list name\n"
+       "Filter incoming routing updates\n"
+       "Filter outgoing routing updates\n")
+{
+  int ret;
+  enum distribute_type type;
+
+  /* Check of distribute list type. */
+  if (strncmp (argv[1], "i", 1) == 0)
+    type = DISTRIBUTE_IN;
+  else if (strncmp (argv[1], "o", 1) == 0)
+    type = DISTRIBUTE_OUT;
+  else
+    {
+      vty_out (vty, "distribute list direction must be [in|out]%s",
+	       VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+
+  ret = distribute_list_unset (NULL, type, argv[0]);
+  if (! ret)
+    {
+      vty_out (vty, "distribute list doesn't exist%s", VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+  return CMD_SUCCESS;
+}
+
+DEFUN (distribute_list,
+       distribute_list_cmd,
+       "distribute-list WORD (in|out) WORD",
+       "Filter networks in routing updates\n"
+       "Access-list name\n"
+       "Filter incoming routing updates\n"
+       "Filter outgoing routing updates\n"
+       "Interface name\n")
+{
+  enum distribute_type type;
+  struct distribute *dist;
+
+  /* Check of distribute list type. */
+  if (strncmp (argv[1], "i", 1) == 0)
+    type = DISTRIBUTE_IN;
+  else if (strncmp (argv[1], "o", 1) == 0)
+    type = DISTRIBUTE_OUT;
+  else
+    {
+      vty_out (vty, "distribute list direction must be [in|out]%s", VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+
+  /* Get interface name corresponding distribute list. */
+  dist = distribute_list_set (argv[2], type, argv[0]);
+
+  return CMD_SUCCESS;
+}       
+
+DEFUN (no_districute_list, no_distribute_list_cmd,
+       "no distribute-list WORD (in|out) WORD",
+       NO_STR
+       "Filter networks in routing updates\n"
+       "Access-list name\n"
+       "Filter incoming routing updates\n"
+       "Filter outgoing routing updates\n"
+       "Interface name\n")
+{
+  int ret;
+  enum distribute_type type;
+
+  /* Check of distribute list type. */
+  if (strncmp (argv[1], "i", 1) == 0)
+    type = DISTRIBUTE_IN;
+  else if (strncmp (argv[1], "o", 1) == 0)
+    type = DISTRIBUTE_OUT;
+  else
+    {
+      vty_out (vty, "distribute list direction must be [in|out]%s", VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+
+  ret = distribute_list_unset (argv[2], type, argv[0]);
+  if (! ret)
+    {
+      vty_out (vty, "distribute list doesn't exist%s", VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+  return CMD_SUCCESS;
+}       
+
+DEFUN (districute_list_prefix_all,
+       distribute_list_prefix_all_cmd,
+       "distribute-list prefix WORD (in|out)",
+       "Filter networks in routing updates\n"
+       "Filter prefixes in routing updates\n"
+       "Name of an IP prefix-list\n"
+       "Filter incoming routing updates\n"
+       "Filter outgoing routing updates\n")
+{
+  enum distribute_type type;
+  struct distribute *dist;
+
+  /* Check of distribute list type. */
+  if (strncmp (argv[1], "i", 1) == 0)
+    type = DISTRIBUTE_IN;
+  else if (strncmp (argv[1], "o", 1) == 0)
+    type = DISTRIBUTE_OUT;
+  else
+    {
+      vty_out (vty, "distribute list direction must be [in|out]%s", 
+	       VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+
+  /* Get interface name corresponding distribute list. */
+  dist = distribute_list_prefix_set (NULL, type, argv[0]);
+
+  return CMD_SUCCESS;
+}       
+
+DEFUN (no_districute_list_prefix_all,
+       no_distribute_list_prefix_all_cmd,
+       "no distribute-list prefix WORD (in|out)",
+       NO_STR
+       "Filter networks in routing updates\n"
+       "Filter prefixes in routing updates\n"
+       "Name of an IP prefix-list\n"
+       "Filter incoming routing updates\n"
+       "Filter outgoing routing updates\n")
+{
+  int ret;
+  enum distribute_type type;
+
+  /* Check of distribute list type. */
+  if (strncmp (argv[1], "i", 1) == 0)
+    type = DISTRIBUTE_IN;
+  else if (strncmp (argv[1], "o", 1) == 0)
+    type = DISTRIBUTE_OUT;
+  else
+    {
+      vty_out (vty, "distribute list direction must be [in|out]%s", 
+	       VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+
+  ret = distribute_list_prefix_unset (NULL, type, argv[0]);
+  if (! ret)
+    {
+      vty_out (vty, "distribute list doesn't exist%s", VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+  return CMD_SUCCESS;
+}       
+
+DEFUN (districute_list_prefix, distribute_list_prefix_cmd,
+       "distribute-list prefix WORD (in|out) WORD",
+       "Filter networks in routing updates\n"
+       "Filter prefixes in routing updates\n"
+       "Name of an IP prefix-list\n"
+       "Filter incoming routing updates\n"
+       "Filter outgoing routing updates\n"
+       "Interface name\n")
+{
+  enum distribute_type type;
+  struct distribute *dist;
+
+  /* Check of distribute list type. */
+  if (strncmp (argv[1], "i", 1) == 0)
+    type = DISTRIBUTE_IN;
+  else if (strncmp (argv[1], "o", 1) == 0)
+    type = DISTRIBUTE_OUT;
+  else
+    {
+      vty_out (vty, "distribute list direction must be [in|out]%s", 
+	       VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+
+  /* Get interface name corresponding distribute list. */
+  dist = distribute_list_prefix_set (argv[2], type, argv[0]);
+
+  return CMD_SUCCESS;
+}       
+
+DEFUN (no_districute_list_prefix, no_distribute_list_prefix_cmd,
+       "no distribute-list prefix WORD (in|out) WORD",
+       NO_STR
+       "Filter networks in routing updates\n"
+       "Filter prefixes in routing updates\n"
+       "Name of an IP prefix-list\n"
+       "Filter incoming routing updates\n"
+       "Filter outgoing routing updates\n"
+       "Interface name\n")
+{
+  int ret;
+  enum distribute_type type;
+
+  /* Check of distribute list type. */
+  if (strncmp (argv[1], "i", 1) == 0)
+    type = DISTRIBUTE_IN;
+  else if (strncmp (argv[1], "o", 1) == 0)
+    type = DISTRIBUTE_OUT;
+  else
+    {
+      vty_out (vty, "distribute list direction must be [in|out]%s", 
+	       VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+
+  ret = distribute_list_prefix_unset (argv[2], type, argv[0]);
+  if (! ret)
+    {
+      vty_out (vty, "distribute list doesn't exist%s", VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+  return CMD_SUCCESS;
+}       
+
+int
+config_show_distribute (struct vty *vty)
+{
+  int i;
+  struct hash_backet *mp;
+  struct distribute *dist;
+
+  /* Output filter configuration. */
+  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");
+      if (dist->list[DISTRIBUTE_OUT])
+	vty_out (vty, " %s", dist->list[DISTRIBUTE_OUT]);
+      if (dist->prefix[DISTRIBUTE_OUT])
+	vty_out (vty, "%s (prefix-list) %s",
+		 dist->list[DISTRIBUTE_OUT] ? "," : "",
+		 dist->prefix[DISTRIBUTE_OUT]);
+      vty_out (vty, "%s", VTY_NEWLINE);
+    }
+  else
+    vty_out (vty, "  Outgoing update filter list for all interface is not set%s", VTY_NEWLINE);
+
+  for (i = 0; i < disthash->size; i++)
+    for (mp = disthash->index[i]; mp; mp = mp->next)
+      {
+	dist = mp->data;
+	if (dist->ifname)
+	  if (dist->list[DISTRIBUTE_OUT] || dist->prefix[DISTRIBUTE_OUT])
+	    {
+	      vty_out (vty, "    %s filtered by", dist->ifname);
+	      if (dist->list[DISTRIBUTE_OUT])
+		vty_out (vty, " %s", dist->list[DISTRIBUTE_OUT]);
+	      if (dist->prefix[DISTRIBUTE_OUT])
+		vty_out (vty, "%s (prefix-list) %s",
+			 dist->list[DISTRIBUTE_OUT] ? "," : "",
+			 dist->prefix[DISTRIBUTE_OUT]);
+	      vty_out (vty, "%s", VTY_NEWLINE);
+	    }
+      }
+
+
+  /* Input filter configuration. */
+  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");
+      if (dist->list[DISTRIBUTE_IN])
+	vty_out (vty, " %s", dist->list[DISTRIBUTE_IN]);
+      if (dist->prefix[DISTRIBUTE_IN])
+	vty_out (vty, "%s (prefix-list) %s",
+		 dist->list[DISTRIBUTE_IN] ? "," : "",
+		 dist->prefix[DISTRIBUTE_IN]);
+      vty_out (vty, "%s", VTY_NEWLINE);
+    }
+  else
+    vty_out (vty, "  Incoming update filter list for all interface is not set%s", VTY_NEWLINE);
+
+  for (i = 0; i < disthash->size; i++)
+    for (mp = disthash->index[i]; mp; mp = mp->next)
+      {
+	dist = mp->data;
+	if (dist->ifname)
+	  if (dist->list[DISTRIBUTE_IN] || dist->prefix[DISTRIBUTE_IN])
+	    {
+	      vty_out (vty, "    %s filtered by", dist->ifname);
+	      if (dist->list[DISTRIBUTE_IN])
+		vty_out (vty, " %s", dist->list[DISTRIBUTE_IN]);
+	      if (dist->prefix[DISTRIBUTE_IN])
+		vty_out (vty, "%s (prefix-list) %s",
+			 dist->list[DISTRIBUTE_IN] ? "," : "",
+			 dist->prefix[DISTRIBUTE_IN]);
+	      vty_out (vty, "%s", VTY_NEWLINE);
+	    }
+      }
+  return 0;
+}
+
+/* Configuration write function. */
+int
+config_write_distribute (struct vty *vty)
+{
+  int i;
+  struct hash_backet *mp;
+  int write = 0;
+
+  for (i = 0; i < disthash->size; i++)
+    for (mp = disthash->index[i]; mp; mp = mp->next)
+      {
+	struct distribute *dist;
+
+	dist = mp->data;
+
+	if (dist->list[DISTRIBUTE_IN])
+	  {
+	    vty_out (vty, " distribute-list %s in %s%s", 
+		     dist->list[DISTRIBUTE_IN],
+		     dist->ifname ? dist->ifname : "",
+		     VTY_NEWLINE);
+	    write++;
+	  }
+
+	if (dist->list[DISTRIBUTE_OUT])
+	  {
+	    vty_out (vty, " distribute-list %s out %s%s", 
+
+		     dist->list[DISTRIBUTE_OUT],
+		     dist->ifname ? dist->ifname : "",
+		     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 : "",
+		     VTY_NEWLINE);
+	    write++;
+	  }
+      }
+  return write;
+}
+
+/* Clear all distribute list. */
+void
+distribute_list_reset ()
+{
+  hash_clean (disthash, (void (*) (void *)) distribute_free);
+}
+
+/* Initialize distribute list related hash. */
+void
+distribute_list_init (int node)
+{
+  disthash = hash_create (distribute_hash_make, distribute_cmp);
+
+  install_element (node, &distribute_list_all_cmd);
+  install_element (node, &no_distribute_list_all_cmd);
+
+  install_element (node, &distribute_list_cmd);
+  install_element (node, &no_distribute_list_cmd);
+
+  install_element (node, &distribute_list_prefix_all_cmd);
+  install_element (node, &no_distribute_list_prefix_all_cmd);
+
+  install_element (node, &distribute_list_prefix_cmd);
+  install_element (node, &no_distribute_list_prefix_cmd);
+}