]> git.proxmox.com Git - mirror_frr.git/blobdiff - zebra/zserv.c
2004-11-19 Andrew J. Schorr <ajschorr@alumni.princeton.edu>
[mirror_frr.git] / zebra / zserv.c
index 3c582afb7724687a1655473d97b66eaac2c2d855..09dddf63b70b58674231cb42e6df05c94520aa06 100644 (file)
@@ -36,6 +36,7 @@
 #include "privs.h"
 
 #include "zebra/zserv.h"
+#include "zebra/router-id.h"
 #include "zebra/redistribute.h"
 #include "zebra/debug.h"
 #include "zebra/ipforward.h"
@@ -50,7 +51,7 @@ static void zebra_event (enum event event, int sock, struct zserv *client);
 extern struct zebra_privs_t zserv_privs;
 \f
 /* For logging of zebra meesages. */
-static char *zebra_command_str [] =
+static const char *zebra_command_str [] =
 {
   "NULL",
   "ZEBRA_INTERFACE_ADD",
@@ -70,7 +71,10 @@ static char *zebra_command_str [] =
   "ZEBRA_IPV4_NEXTHOP_LOOKUP",
   "ZEBRA_IPV6_NEXTHOP_LOOKUP",
   "ZEBRA_IPV4_IMPORT_LOOKUP",
-  "ZEBRA_IPV6_IMPORT_LOOKUP"
+  "ZEBRA_IPV6_IMPORT_LOOKUP",
+  "ZEBRA_ROUTER_ID_ADD",
+  "ZEBRA_ROUTER_ID_DELETE",
+  "ZEBRA_ROUTER_ID_UPDATE"
 };
 \f
 struct zebra_message_queue
@@ -165,7 +169,8 @@ zebra_server_send_message (int sock, u_char *buf, unsigned long length)
       else
        return -1;
     }
-  else if (nbytes != length)
+  /* It's clear that nbytes is positive at this point. */
+  else if ((unsigned) nbytes != length)
     zebra_server_enqueue (sock, buf, length, nbytes);
 
   return 0;
@@ -497,7 +502,7 @@ zsend_route_multipath (int cmd, struct zserv *client, struct prefix *p,
                     || cmd == ZEBRA_IPV4_ROUTE_DELETE)
                   {
                     struct in_addr empty;
-                    memset (&empty, 0, sizeof (struct in6_addr));
+                    memset (&empty, 0, sizeof (struct in_addr));
                     stream_write (s, (u_char *) &empty, IPV4_MAX_BYTELEN);
                   }
                 else
@@ -521,7 +526,7 @@ zsend_route_multipath (int cmd, struct zserv *client, struct prefix *p,
 
   /* Write next-hop number */
   if (nhnummark)
-    stream_putw_at (s, nhnummark, nhnum);
+    stream_putc_at (s, nhnummark, nhnum);
   
   /* Write packet size. */
   stream_putw_at (s, 0, stream_get_endp (s));
@@ -721,13 +726,45 @@ zsend_ipv4_import_lookup (struct zserv *client, struct prefix_ipv4 *p)
   return 0;
 }
 \f
+/* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
+int
+zsend_router_id_update (struct zserv *client, struct prefix *p)
+{
+  struct stream *s;
+  int blen;
+
+  /* Check this client need interface information. */
+  if (!client->ridinfo)
+    return -1;
+
+  s = client->obuf;
+  stream_reset (s);
+
+  /* Place holder for size. */
+  stream_putw (s, 0);
+
+  /* Message type. */
+  stream_putc (s, ZEBRA_ROUTER_ID_UPDATE);
+
+  /* Prefix information. */
+  stream_putc (s, p->family);
+  blen = prefix_blen (p);
+  stream_put (s, &p->u.prefix, blen);
+  stream_putc (s, p->prefixlen);
+
+  /* Write packet size. */
+  stream_putw_at (s, 0, stream_get_endp (s));
+
+  return writen (client->sock, s->data, stream_get_endp (s));
+}
+\f
 /* Register zebra server interface information.  Send current all
    interface and address information. */
 static void
 zread_interface_add (struct zserv *client, u_short length)
 {
-  listnode ifnode;
-  listnode cnode;
+  struct listnode *ifnode;
+  struct listnode *cnode;
   struct interface *ifp;
   struct connected *c;
 
@@ -1122,6 +1159,27 @@ zread_ipv6_nexthop_lookup (struct zserv *client, u_short length)
 }
 #endif /* HAVE_IPV6 */
 
+/* Register zebra server router-id information.  Send current router-id */
+void
+zread_router_id_add (struct zserv *client, u_short length)
+{
+  struct prefix p;
+
+  /* Router-id information is needed. */
+  client->ridinfo = 1;
+
+  router_id_get (&p);
+
+  zsend_router_id_update (client,&p);
+}
+
+/* Unregister zebra server router-id information. */
+void
+zread_router_id_delete (struct zserv *client, u_short length)
+{
+  client->ridinfo = 0;
+}
+
 /* Close zebra client. */
 static void
 zebra_client_close (struct zserv *client)
@@ -1233,6 +1291,12 @@ zebra_client_read (struct thread *thread)
 
   switch (command) 
     {
+    case ZEBRA_ROUTER_ID_ADD:
+      zread_router_id_add (client, length);
+      break;
+    case ZEBRA_ROUTER_ID_DELETE:
+      zread_router_id_delete (client, length);
+      break;
     case ZEBRA_INTERFACE_ADD:
       zread_interface_add (client, length);
       break;
@@ -1305,7 +1369,7 @@ zebra_accept (struct thread *thread)
 
   if (client_sock < 0)
     {
-      zlog_warn ("Can't accept zebra socket: %s", strerror (errno));
+      zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno));
       return -1;
     }
 
@@ -1336,7 +1400,7 @@ zebra_serv ()
 
   if (accept_sock < 0) 
     {
-      zlog_warn ("Can't bind to socket: %s", strerror (errno));
+      zlog_warn ("Can't bind to socket: %s", safe_strerror (errno));
       zlog_warn ("zebra can't provice full functionality due to above error");
       return;
     }
@@ -1359,7 +1423,7 @@ zebra_serv ()
               sizeof (struct sockaddr_in));
   if (ret < 0)
     {
-      zlog_warn ("Can't bind to socket: %s", strerror (errno));
+      zlog_warn ("Can't bind to socket: %s", safe_strerror (errno));
       zlog_warn ("zebra can't provice full functionality due to above error");
       close (accept_sock);      /* Avoid sd leak. */
       return;
@@ -1371,7 +1435,7 @@ zebra_serv ()
   ret = listen (accept_sock, 1);
   if (ret < 0)
     {
-      zlog_warn ("Can't listen to socket: %s", strerror (errno));
+      zlog_warn ("Can't listen to socket: %s", safe_strerror (errno));
       zlog_warn ("zebra can't provice full functionality due to above error");
       close (accept_sock);     /* Avoid sd leak. */
       return;
@@ -1386,7 +1450,7 @@ zebra_serv ()
 
 /* zebra server UNIX domain socket. */
 static void
-zebra_serv_un (char *path)
+zebra_serv_un (const char *path)
 {
   int ret;
   int sock, len;
@@ -1488,14 +1552,9 @@ DEFUN (ip_forwarding,
   int ret;
 
   ret = ipforward ();
+  if (ret == 0)
+    ret = ipforward_on ();
 
-  if (ret != 0)
-    {
-      vty_out (vty, "IP forwarding is already on%s", VTY_NEWLINE);
-      return CMD_ERR_NOTHING_TODO;
-    }
-
-  ret = ipforward_on ();
   if (ret == 0)
     {
       vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE);
@@ -1515,14 +1574,9 @@ DEFUN (no_ip_forwarding,
   int ret;
 
   ret = ipforward ();
+  if (ret != 0)
+    ret = ipforward_off ();
 
-  if (ret == 0)
-    {
-      vty_out (vty, "IP forwarding is already off%s", VTY_NEWLINE); 
-      return CMD_ERR_NOTHING_TODO;
-    }
-
-  ret = ipforward_off ();
   if (ret != 0)
     {
       vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE);
@@ -1540,7 +1594,7 @@ DEFUN (show_zebra_client,
        "Zebra information"
        "Client information")
 {
-  listnode node;
+  struct listnode *node;
   struct zserv *client;
 
   for (node = listhead (zebrad.client_list); node; nextnode (node))
@@ -1628,13 +1682,9 @@ DEFUN (ipv6_forwarding,
   int ret;
 
   ret = ipforward_ipv6 ();
-  if (ret != 0)
-    {
-      vty_out (vty, "IPv6 forwarding is already on%s", VTY_NEWLINE);
-      return CMD_ERR_NOTHING_TODO;
-    }
+  if (ret == 0)
+    ret = ipforward_ipv6_on ();
 
-  ret = ipforward_ipv6_on ();
   if (ret == 0)
     {
       vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE);
@@ -1654,13 +1704,9 @@ DEFUN (no_ipv6_forwarding,
   int ret;
 
   ret = ipforward_ipv6 ();
-  if (ret == 0)
-    {
-      vty_out (vty, "IP forwarding is already off%s", VTY_NEWLINE);
-      return CMD_ERR_NOTHING_TODO;
-    }
+  if (ret != 0)
+    ret = ipforward_ipv6_off ();
 
-  ret = ipforward_ipv6_off ();
   if (ret != 0)
     {
       vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE);
@@ -1676,11 +1722,14 @@ DEFUN (no_ipv6_forwarding,
 int
 config_write_forwarding (struct vty *vty)
 {
-  if (! ipforward ())
-    vty_out (vty, "no ip forwarding%s", VTY_NEWLINE);
+  /* FIXME: Find better place for that. */
+  router_id_write (vty);
+
+  if (ipforward ())
+    vty_out (vty, "ip forwarding%s", VTY_NEWLINE);
 #ifdef HAVE_IPV6
-  if (ipforward_ipv6 ())
-    vty_out (vty, "no ipv6 forwarding%s", VTY_NEWLINE);
+  if (ipforward_ipv6 ())
+    vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE);
 #endif /* HAVE_IPV6 */
   vty_out (vty, "!%s", VTY_NEWLINE);
   return 0;
@@ -1702,12 +1751,6 @@ zebra_init ()
   /* Client list init. */
   zebrad.client_list = list_new ();
 
-  /* Forwarding is on by default. */
-  ipforward_on ();
-#ifdef HAVE_IPV6
-  ipforward_ipv6_on ();
-#endif /* HAVE_IPV6 */
-
   /* Make zebra server socket. */
 #ifdef HAVE_TCP_ZEBRA
   zebra_serv ();