2003-06-15 Paul Jakma <paul@dishone.st>

	* lib/vty.{c,h}: Remove vty layer depending on a 'master' global,
	  pass the thread master in explicitly to vty_init. Sort out some
	  header dependency problems with lib/command.h
	* zebra/: Move globals to struct zebrad. Update vty_init().
	* (.*)/\1_main.c: update call to vty_init().
diff --git a/bgpd/bgp_main.c b/bgpd/bgp_main.c
index 3e1d80f..933ca57 100644
--- a/bgpd/bgp_main.c
+++ b/bgpd/bgp_main.c
@@ -282,7 +282,7 @@
   signal_init ();
   zprivs_init (&bgpd_privs);
   cmd_init (1);
-  vty_init ();
+  vty_init (master);
   memory_init ();
 
   /* BGP related initialization.  */
diff --git a/lib/command.c b/lib/command.c
index 5d42933..ade413b 100644
--- a/lib/command.c
+++ b/lib/command.c
@@ -20,11 +20,14 @@
 
 #include <zebra.h>
 
-#include "command.h"
+
 #include "memory.h"
 #include "log.h"
 #include "version.h"
 #include "thread.h"
+#include "vector.h"
+#include "vty.h"
+#include "command.h"
 
 /* Command vector which includes some level of command lists. Normally
    each daemon maintains each own cmdvec. */
diff --git a/lib/vty.c b/lib/vty.c
index 4d6eb30..c38ae94 100644
--- a/lib/vty.c
+++ b/lib/vty.c
@@ -23,16 +23,17 @@
 #include <zebra.h>
 
 #include "linklist.h"
+#include "thread.h"
 #include "buffer.h"
 #include "version.h"
 #include "command.h"
 #include "sockunion.h"
-#include "thread.h"
 #include "memory.h"
 #include "str.h"
 #include "log.h"
 #include "prefix.h"
 #include "filter.h"
+#include "vty.h"
 #include "privs.h"
 
 /* Vty events */
@@ -2342,8 +2343,7 @@
 }
 
 /* Master of the threads. */
-extern struct thread_master *master;
-/* struct thread_master *master; */
+static struct thread_master *master;
 
 static void
 vty_event (enum event event, int sock, struct vty *vty)
@@ -2791,13 +2791,15 @@
 
 /* Install vty's own commands like `who' command. */
 void
-vty_init ()
+vty_init (struct thread_master *master_thread)
 {
   /* For further configuration read, preserve current directory. */
   vty_save_cwd ();
 
   vtyvec = vector_init (VECTOR_MIN_SIZE);
 
+  master = master_thread;
+
   /* Initilize server thread vector. */
   Vvty_serv_thread = vector_init (VECTOR_MIN_SIZE);
 
diff --git a/lib/vty.h b/lib/vty.h
index 4d2a6a0..aaf8a5b 100644
--- a/lib/vty.h
+++ b/lib/vty.h
@@ -21,6 +21,8 @@
 #ifndef _ZEBRA_VTY_H
 #define _ZEBRA_VTY_H
 
+#include "thread.h"
+
 #define VTY_BUFSIZ 512
 #define VTY_MAXHIST 20
 
@@ -184,7 +186,7 @@
 extern char integrate_default[];
 
 /* Prototypes. */
-void vty_init (void);
+void vty_init (struct thread_master *);
 void vty_init_vtysh (void);
 void vty_reset (void);
 void vty_finish (void);
diff --git a/ospf6d/ospf6_main.c b/ospf6d/ospf6_main.c
index e6cd6aa..519cde7 100644
--- a/ospf6d/ospf6_main.c
+++ b/ospf6d/ospf6_main.c
@@ -317,7 +317,7 @@
 	zprivs_init (&ospf6d_privs);
   signal_init ();
   cmd_init (1);
-  vty_init ();
+  vty_init (master);
   ospf6_init ();
   memory_init ();
   sort_node ();
diff --git a/ospfd/ospf_main.c b/ospfd/ospf_main.c
index b1d1fd4..236d8c2 100644
--- a/ospfd/ospf_main.c
+++ b/ospfd/ospf_main.c
@@ -288,7 +288,7 @@
   signal_init ();
   cmd_init (1);
   debug_init ();
-  vty_init ();
+  vty_init (master);
   memory_init ();
 
   access_list_init ();
diff --git a/ripd/rip_main.c b/ripd/rip_main.c
index 9526d7a..84ea75c 100644
--- a/ripd/rip_main.c
+++ b/ripd/rip_main.c
@@ -270,7 +270,7 @@
   zprivs_init (&ripd_privs);
   signal_init ();
   cmd_init (1);
-  vty_init ();
+  vty_init (master);
   memory_init ();
   keychain_init ();
 
diff --git a/ripngd/ripng_main.c b/ripngd/ripng_main.c
index bd1972a..5fb0444 100644
--- a/ripngd/ripng_main.c
+++ b/ripngd/ripng_main.c
@@ -274,7 +274,7 @@
   zprivs_init (&ripngd_privs);
   signal_init ();
   cmd_init (1);
-  vty_init ();
+  vty_init (master);
   memory_init ();
 
   /* RIPngd inits. */
diff --git a/zebra/main.c b/zebra/main.c
index 85707f4..c47b83e 100644
--- a/zebra/main.c
+++ b/zebra/main.c
@@ -36,8 +36,11 @@
 #include "zebra/debug.h"
 #include "zebra/rib.h"
 
-/* Master of threads. */
-struct thread_master *master;
+/* Zebra instance */
+struct zebra_t zebrad =
+{
+  .rtm_table_default = 0,
+};
 
 /* process id. */
 pid_t old_pid;
@@ -280,7 +283,7 @@
     }
 
   /* Make master thread emulator. */
-  master = thread_master_create ();
+  zebrad.master = thread_master_create ();
 
   /* privs initialise */
   zprivs_init (&zserv_privs);
@@ -288,7 +291,7 @@
   /* Vty related initialize. */
   signal_init ();
   cmd_init (1);
-  vty_init ();
+  vty_init (zebrad.master);
   memory_init ();
 
   /* Zebra related initialize. */
@@ -345,7 +348,7 @@
   /* Make vty server socket. */
   vty_serv_sock (vty_addr, vty_port, ZEBRA_VTYSH_PATH);
 
-  while (thread_fetch (master, &thread))
+  while (thread_fetch (zebrad.master, &thread))
     thread_call (&thread);
 
   /* Not reached... */
diff --git a/zebra/redistribute.c b/zebra/redistribute.c
index a3d4bad..2d30f79 100644
--- a/zebra/redistribute.c
+++ b/zebra/redistribute.c
@@ -36,6 +36,9 @@
 #include "zebra/redistribute.h"
 #include "zebra/debug.h"
 
+/* master zebra server structure */
+extern struct zebra_t zebrad;
+
 int
 zebra_check_addr (struct prefix *p)
 {
@@ -162,15 +165,13 @@
 #endif /* HAVE_IPV6 */
 }
 
-extern list client_list;
-
 void
 redistribute_add (struct prefix *p, struct rib *rib)
 {
   listnode node;
   struct zserv *client;
 
-  for (node = listhead (client_list); node; nextnode (node))
+  for (node = listhead (zebrad.client_list); node; nextnode (node))
     if ((client = getdata (node)) != NULL)
       {
 	if (is_default (p))
@@ -207,7 +208,7 @@
   if (rib->distance == DISTANCE_INFINITY)
     return;
 
-  for (node = listhead (client_list); node; nextnode (node))
+  for (node = listhead (zebrad.client_list); node; nextnode (node))
     if ((client = getdata (node)) != NULL)
       {
 	if (is_default (p))
@@ -310,7 +311,7 @@
   if (IS_ZEBRA_DEBUG_EVENT)
     zlog_info ("MESSAGE: ZEBRA_INTERFACE_UP %s", ifp->name);
 
-  for (node = listhead (client_list); node; nextnode (node))
+  for (node = listhead (zebrad.client_list); node; nextnode (node))
     if ((client = getdata (node)) != NULL)
       zsend_interface_up (client, ifp);
 }
@@ -325,7 +326,7 @@
   if (IS_ZEBRA_DEBUG_EVENT)
     zlog_info ("MESSAGE: ZEBRA_INTERFACE_DOWN %s", ifp->name);
 
-  for (node = listhead (client_list); node; nextnode (node))
+  for (node = listhead (zebrad.client_list); node; nextnode (node))
     if ((client = getdata (node)) != NULL)
       zsend_interface_down (client, ifp);
 }
@@ -340,7 +341,7 @@
   if (IS_ZEBRA_DEBUG_EVENT)
     zlog_info ("MESSAGE: ZEBRA_INTERFACE_ADD %s", ifp->name);
     
-  for (node = listhead (client_list); node; nextnode (node))
+  for (node = listhead (zebrad.client_list); node; nextnode (node))
     if ((client = getdata (node)) != NULL)
       if (client->ifinfo)
 	zsend_interface_add (client, ifp);
@@ -355,7 +356,7 @@
   if (IS_ZEBRA_DEBUG_EVENT)
     zlog_info ("MESSAGE: ZEBRA_INTERFACE_DELETE %s", ifp->name);
 
-  for (node = listhead (client_list); node; nextnode (node))
+  for (node = listhead (zebrad.client_list); node; nextnode (node))
     if ((client = getdata (node)) != NULL)
       if (client->ifinfo)
 	zsend_interface_delete (client, ifp);
@@ -379,7 +380,7 @@
 		 p->prefixlen, ifc->ifp->name);
     }
 
-  for (node = listhead (client_list); node; nextnode (node))
+  for (node = listhead (zebrad.client_list); node; nextnode (node))
     if ((client = getdata (node)) != NULL)
       if (client->ifinfo && CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
 	zsend_interface_address_add (client, ifp, ifc);
@@ -403,7 +404,7 @@
 		 p->prefixlen, ifc->ifp->name);
     }
 
-  for (node = listhead (client_list); node; nextnode (node))
+  for (node = listhead (zebrad.client_list); node; nextnode (node))
     if ((client = getdata (node)) != NULL)
       if (client->ifinfo && CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
 	zsend_interface_address_delete (client, ifp, ifc);
diff --git a/zebra/rt_netlink.c b/zebra/rt_netlink.c
index e151462..6a3df19 100644
--- a/zebra/rt_netlink.c
+++ b/zebra/rt_netlink.c
@@ -66,7 +66,7 @@
   {0,            NULL}
 };
 
-extern int rtm_table_default;
+extern struct zebra_t zebrad;
 
 extern struct zebra_privs_t zserv_privs;
 
@@ -615,7 +615,7 @@
 
   table = rtm->rtm_table;
 #if 0		/* we weed them out later in rib_weed_tables () */
-  if (table != RT_TABLE_MAIN && table != rtm_table_default)
+  if (table != RT_TABLE_MAIN && table != zebrad.rtm_table_default)
     return 0;
 #endif
 
@@ -734,7 +734,7 @@
     }
 
   table = rtm->rtm_table;
-  if (table != RT_TABLE_MAIN && table != rtm_table_default)
+  if (table != RT_TABLE_MAIN && table != zebrad.rtm_table_default)
     {
       return 0;
     }
@@ -1600,7 +1600,7 @@
 
   sock = THREAD_FD (thread);
   ret = netlink_parse_info (netlink_information_fetch, &netlink);
-  thread_add_read (master, kernel_read, NULL, netlink.sock);
+  thread_add_read (zebrad.master, kernel_read, NULL, netlink.sock);
 
   return 0;
 }
@@ -1621,5 +1621,5 @@
 
   /* Register kernel socket. */
   if (netlink.sock > 0)
-    thread_add_read (master, kernel_read, NULL, netlink.sock);
+    thread_add_read (zebrad.master, kernel_read, NULL, netlink.sock);
 }
diff --git a/zebra/rtadv.c b/zebra/rtadv.c
index ab0d49f..a6e2d92 100644
--- a/zebra/rtadv.c
+++ b/zebra/rtadv.c
@@ -34,6 +34,7 @@
 #include "zebra/interface.h"
 #include "zebra/rtadv.h"
 #include "zebra/debug.h"
+#include "zebra/zserv.h"
 
 extern struct zebra_privs_t zserv_privs;
 
@@ -50,6 +51,8 @@
 #define ALLNODE   "ff02::1"
 #define ALLROUTER "ff02::2"
 
+extern struct zebra_t zebrad;
+
 enum rtadv_event {RTADV_START, RTADV_STOP, RTADV_TIMER, RTADV_READ};
 
 void rtadv_event (enum rtadv_event, int);
@@ -999,7 +1002,6 @@
     }
 }
 
-extern struct thread_master *master;
 
 void
 rtadv_event (enum rtadv_event event, int val)
@@ -1008,9 +1010,9 @@
     {
     case RTADV_START:
       if (! rtadv->ra_read)
-	rtadv->ra_read = thread_add_read (master, rtadv_read, NULL, val);
+	rtadv->ra_read = thread_add_read (zebrad.master, rtadv_read, NULL, val);
       if (! rtadv->ra_timer)
-	rtadv->ra_timer = thread_add_event (master, rtadv_timer, NULL, 0);
+	rtadv->ra_timer = thread_add_event (zebrad.master, rtadv_timer, NULL, 0);
       break;
     case RTADV_STOP:
       if (rtadv->ra_timer)
@@ -1026,11 +1028,11 @@
       break;
     case RTADV_TIMER:
       if (! rtadv->ra_timer)
-	rtadv->ra_timer = thread_add_timer (master, rtadv_timer, NULL, val);
+	rtadv->ra_timer = thread_add_timer (zebrad.master, rtadv_timer, NULL, val);
       break;
     case RTADV_READ:
       if (! rtadv->ra_read)
-	rtadv->ra_read = thread_add_read (master, rtadv_read, NULL, val);
+	rtadv->ra_read = thread_add_read (zebrad.master, rtadv_read, NULL, val);
       break;
     default:
       break;
diff --git a/zebra/zebra_rib.c b/zebra/zebra_rib.c
index 4098db2..2209364 100644
--- a/zebra/zebra_rib.c
+++ b/zebra/zebra_rib.c
@@ -37,7 +37,7 @@
 #include "zebra/debug.h"
 
 /* Default rtm_table for all clients */
-extern int rtm_table_default;
+extern struct zebra_t zebrad;
 
 /* Each route type's string and default distance value. */
 struct
@@ -2147,7 +2147,7 @@
 	{
 	  next = rib->next;
 
-	  if (rib->table != rtm_table_default &&
+	  if (rib->table != zebrad.rtm_table_default &&
 	      rib->table != RT_TABLE_MAIN)
 	    {
 	      rib_delnode (rn, rib);
diff --git a/zebra/zserv.c b/zebra/zserv.c
index 975574a..3ea1a16 100644
--- a/zebra/zserv.c
+++ b/zebra/zserv.c
@@ -43,17 +43,11 @@
 /* Event list of zebra. */
 enum event { ZEBRA_SERV, ZEBRA_READ, ZEBRA_WRITE };
 
-/* Zebra client list. */
-list client_list;
-
-/* Default rtm_table for all clients */
-int rtm_table_default = 0;
+extern struct zebra_t zebrad;
 
 void zebra_event (enum event event, int sock, struct zserv *client);
 
 extern struct zebra_privs_t zserv_privs;
-
-extern struct thread_master *master;
 
 /* For logging of zebra meesages. */
 char *zebra_command_str [] =
@@ -126,7 +120,8 @@
     }
 
   if (FIFO_TOP (&message_queue))
-    THREAD_WRITE_ON (master, t_write, zebra_server_dequeue, NULL, sock);
+    THREAD_WRITE_ON (zebrad.master, t_write, zebra_server_dequeue, 
+                     NULL, sock);
 
   return 0;
 }
@@ -146,7 +141,7 @@
 
   FIFO_ADD (&message_queue, queue);
 
-  THREAD_WRITE_ON (master, t_write, zebra_server_dequeue, NULL, sock);
+  THREAD_WRITE_ON (zebrad.master, t_write, zebra_server_dequeue, NULL, sock);
 }
 
 int
@@ -1422,7 +1417,7 @@
     thread_cancel (client->t_write);
 
   /* Free client structure. */
-  listnode_delete (client_list, client);
+  listnode_delete (zebrad.client_list, client);
   XFREE (0, client);
 }
 
@@ -1440,10 +1435,10 @@
   client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
 
   /* Set table number. */
-  client->rtm_table = rtm_table_default;
+  client->rtm_table = zebrad.rtm_table_default;
 
   /* Add this client to linked list. */
-  listnode_add (client_list, client);
+  listnode_add (zebrad.client_list, client);
   
   /* Make new read thread. */
   zebra_event (ZEBRA_READ, sock, client);
@@ -1726,8 +1721,6 @@
   zebra_event (ZEBRA_SERV, sock, NULL);
 }
 
-/* Zebra's event management function. */
-extern struct thread_master *master;
 
 void
 zebra_event (enum event event, int sock, struct zserv *client)
@@ -1735,11 +1728,11 @@
   switch (event)
     {
     case ZEBRA_SERV:
-      thread_add_read (master, zebra_accept, client, sock);
+      thread_add_read (zebrad.master, zebra_accept, client, sock);
       break;
     case ZEBRA_READ:
       client->t_read = 
-	thread_add_read (master, zebra_client_read, client, sock);
+	thread_add_read (zebrad.master, zebra_client_read, client, sock);
       break;
     case ZEBRA_WRITE:
       /**/
@@ -1754,7 +1747,7 @@
        SHOW_STR
        "default routing table to use for all clients\n")
 {
-  vty_out (vty, "table %d%s", rtm_table_default,
+  vty_out (vty, "table %d%s", zebrad.rtm_table_default,
 	   VTY_NEWLINE);
   return CMD_SUCCESS;
 }
@@ -1765,7 +1758,7 @@
        "Configure target kernel routing table\n"
        "TABLE integer\n")
 {
-  rtm_table_default = strtol (argv[0], (char**)0, 10);
+  zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10);
   return CMD_SUCCESS;
 }
 
@@ -1833,7 +1826,7 @@
   listnode node;
   struct zserv *client;
 
-  for (node = listhead (client_list); node; nextnode (node))
+  for (node = listhead (zebrad.client_list); node; nextnode (node))
     {
       client = getdata (node);
       vty_out (vty, "Client fd %d%s", client->sock, VTY_NEWLINE);
@@ -1845,8 +1838,8 @@
 int
 config_write_table (struct vty *vty)
 {
-  if (rtm_table_default)
-    vty_out (vty, "table %d%s", rtm_table_default,
+  if (zebrad.rtm_table_default)
+    vty_out (vty, "table %d%s", zebrad.rtm_table_default,
 	     VTY_NEWLINE);
   return 0;
 }
@@ -1958,7 +1951,7 @@
 zebra_init ()
 {
   /* Client list init. */
-  client_list = list_new ();
+  zebrad.client_list = list_new ();
 
   /* Forwarding is on by default. */
   ipforward_on ();
diff --git a/zebra/zserv.h b/zebra/zserv.h
index ec3c9f4..42fb64c 100644
--- a/zebra/zserv.h
+++ b/zebra/zserv.h
@@ -56,6 +56,18 @@
   u_char ifinfo;
 };
 
+/* Zebra instance */
+struct zebra_t
+{
+  /* Thread master */
+  struct thread_master *master;
+  list client_list;
+
+  /* default table */
+  int rtm_table_default;
+  
+};
+
 /* Count prefix size from mask length */
 #define PSIZE(a) (((a) + 7) / (8))