don't send net route to subnet gw's unless on subnet 0;
[unix-history] / usr / src / sbin / routed / trace.c
index 5411730..b86fd04 100644 (file)
@@ -1,12 +1,19 @@
+/*
+ * Copyright (c) 1983 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)trace.c    4.1 %G%";
-#endif
+static char sccsid[] = "@(#)trace.c    5.3 (Berkeley) %G%";
+#endif not lint
 
 /*
  * Routing Table Management Daemon
  */
 #define        RIPCMDS
 
 /*
  * Routing Table Management Daemon
  */
 #define        RIPCMDS
-#include "router.h"
+#include "defs.h"
+#include <sys/stat.h>
 
 #define        NRECORDS        50              /* size of circular trace buffer */
 #ifdef DEBUG
 
 #define        NRECORDS        50              /* size of circular trace buffer */
 #ifdef DEBUG
@@ -37,6 +44,7 @@ iftraceinit(ifp, ifd)
        if (ifd->ifd_records == 0)
                return (0);
        ifd->ifd_front = ifd->ifd_records;
        if (ifd->ifd_records == 0)
                return (0);
        ifd->ifd_front = ifd->ifd_records;
+       ifd->ifd_count = 0;
        for (t = ifd->ifd_records; t < ifd->ifd_records + NRECORDS; t++) {
                t->ift_size = 0;
                t->ift_packet = 0;
        for (t = ifd->ifd_records; t < ifd->ifd_records + NRECORDS; t++) {
                t->ift_size = 0;
                t->ift_packet = 0;
@@ -48,9 +56,12 @@ iftraceinit(ifp, ifd)
 traceon(file)
        char *file;
 {
 traceon(file)
        char *file;
 {
+       struct stat stbuf;
 
        if (ftrace != NULL)
                return;
 
        if (ftrace != NULL)
                return;
+       if (stat(file, &stbuf) >= 0 && (stbuf.st_mode & S_IFMT) != S_IFREG)
+               return;
        ftrace = fopen(file, "a");
        if (ftrace == NULL)
                return;
        ftrace = fopen(file, "a");
        if (ftrace == NULL)
                return;
@@ -82,18 +93,21 @@ trace(ifd, who, p, len, m)
        t = ifd->ifd_front++;
        if (ifd->ifd_front >= ifd->ifd_records + NRECORDS)
                ifd->ifd_front = ifd->ifd_records;
        t = ifd->ifd_front++;
        if (ifd->ifd_front >= ifd->ifd_records + NRECORDS)
                ifd->ifd_front = ifd->ifd_records;
-       if (t->ift_size > 0 && t->ift_packet)
+       if (ifd->ifd_count < NRECORDS)
+               ifd->ifd_count++;
+       if (t->ift_size > 0 && t->ift_size < len && t->ift_packet) {
                free(t->ift_packet);
                free(t->ift_packet);
-       t->ift_packet = 0;
+               t->ift_packet = 0;
+       }
        t->ift_stamp = time(0);
        t->ift_who = *who;
        t->ift_stamp = time(0);
        t->ift_who = *who;
-       if (len > 0) {
+       if (len > 0 && t->ift_packet == 0) {
                t->ift_packet = malloc(len);
                t->ift_packet = malloc(len);
-               if (t->ift_packet)
-                       bcopy(p, t->ift_packet, len);
-               else
+               if (t->ift_packet == 0)
                        len = 0;
        }
                        len = 0;
        }
+       if (len > 0)
+               bcopy(p, t->ift_packet, len);
        t->ift_size = len;
        t->ift_metric = m;
 }
        t->ift_size = len;
        t->ift_metric = m;
 }
@@ -117,6 +131,9 @@ traceaction(fd, action, rt)
                { RTS_REMOTE,   "REMOTE" },
                { RTS_INTERFACE,"INTERFACE" },
                { RTS_CHANGED,  "CHANGED" },
                { RTS_REMOTE,   "REMOTE" },
                { RTS_INTERFACE,"INTERFACE" },
                { RTS_CHANGED,  "CHANGED" },
+               { RTS_INTERNAL, "INTERNAL" },
+               { RTS_EXTERNAL, "EXTERNAL" },
+               { RTS_SUBNET,   "SUBNET" },
                { 0 }
        };
        register struct bits *p;
                { 0 }
        };
        register struct bits *p;
@@ -129,8 +146,9 @@ traceaction(fd, action, rt)
        fprintf(fd, "%s ", action);
        dst = (struct sockaddr_in *)&rt->rt_dst;
        gate = (struct sockaddr_in *)&rt->rt_router;
        fprintf(fd, "%s ", action);
        dst = (struct sockaddr_in *)&rt->rt_dst;
        gate = (struct sockaddr_in *)&rt->rt_router;
-       fprintf(fd, "dst %x, router %x, metric %d, flags", dst->sin_addr,
-               gate->sin_addr, rt->rt_metric);
+       fprintf(fd, "dst %s, ", inet_ntoa(dst->sin_addr));
+       fprintf(fd, "router %s, metric %d, flags",
+            inet_ntoa(gate->sin_addr), rt->rt_metric);
        cp = " %s";
        for (first = 1, p = flagbits; p->t_bits > 0; p++) {
                if ((rt->rt_flags & p->t_bits) == 0)
        cp = " %s";
        for (first = 1, p = flagbits; p->t_bits > 0; p++) {
                if ((rt->rt_flags & p->t_bits) == 0)
@@ -153,7 +171,7 @@ traceaction(fd, action, rt)
                }
        }
        putc('\n', fd);
                }
        }
        putc('\n', fd);
-       if ((rt->rt_state & RTS_PASSIVE) == 0 && rt->rt_ifp)
+       if (!tracepackets && (rt->rt_state & RTS_PASSIVE) == 0 && rt->rt_ifp)
                dumpif(fd, rt->rt_ifp);
        fflush(fd);
 }
                dumpif(fd, rt->rt_ifp);
        fflush(fd);
 }
@@ -161,13 +179,14 @@ traceaction(fd, action, rt)
 dumpif(fd, ifp)
        register struct interface *ifp;
 {
 dumpif(fd, ifp)
        register struct interface *ifp;
 {
-       register struct ifdebug *ifd;
-       
-       fprintf(fd, "*** Packet history for interface %s ***\n",
-               ifp->int_name);
-       dumptrace(fd, "to", &ifp->int_output);
-       dumptrace(fd, "from", &ifp->int_output);
-       fprintf(fd, "*** end packet history ***\n");
+       if (ifp->int_input.ifd_count || ifp->int_output.ifd_count) {
+               fprintf(fd, "*** Packet history for interface %s ***\n",
+                       ifp->int_name);
+               dumptrace(fd, "to", &ifp->int_output);
+               dumptrace(fd, "from", &ifp->int_input);
+               fprintf(fd, "*** end packet history ***\n");
+       }
+       fflush(fd);
 }
 
 dumptrace(fd, dir, ifd)
 }
 
 dumptrace(fd, dir, ifd)
@@ -181,17 +200,16 @@ dumptrace(fd, dir, ifd)
        if (ifd->ifd_front == ifd->ifd_records &&
            ifd->ifd_front->ift_size == 0) {
                fprintf(fd, "%s: no packets.\n", cp);
        if (ifd->ifd_front == ifd->ifd_records &&
            ifd->ifd_front->ift_size == 0) {
                fprintf(fd, "%s: no packets.\n", cp);
+               fflush(fd);
                return;
        }
        fprintf(fd, "%s trace:\n", cp);
                return;
        }
        fprintf(fd, "%s trace:\n", cp);
-       for (t = ifd->ifd_front; t <= ifd->ifd_records + NRECORDS; t++) {
-               if (t->ift_size == 0)
-                       continue;
-               fprintf(fd, "%.24s: metric=%d\n", ctime(&t->ift_stamp),
-                       t->ift_metric);
-               dumppacket(fd, dir, &t->ift_who, t->ift_packet, t->ift_size);
-       }
-       for (t = ifd->ifd_records; t < ifd->ifd_front; t++) {
+       t = ifd->ifd_front - ifd->ifd_count;
+       if (t < ifd->ifd_records)
+               t += NRECORDS;
+       for ( ; ifd->ifd_count; ifd->ifd_count--, t++) {
+               if (t >= ifd->ifd_records + NRECORDS)
+                       t = ifd->ifd_records;
                if (t->ift_size == 0)
                        continue;
                fprintf(fd, "%.24s: metric=%d\n", ctime(&t->ift_stamp),
                if (t->ift_size == 0)
                        continue;
                fprintf(fd, "%.24s: metric=%d\n", ctime(&t->ift_stamp),
@@ -210,12 +228,13 @@ dumppacket(fd, dir, who, cp, size)
        register struct netinfo *n;
 
        if (msg->rip_cmd && msg->rip_cmd < RIPCMD_MAX)
        register struct netinfo *n;
 
        if (msg->rip_cmd && msg->rip_cmd < RIPCMD_MAX)
-               fprintf(fd, "%s %s %x.%d", ripcmds[msg->rip_cmd],
-                       dir, who->sin_addr, ntohs(who->sin_port));
+               fprintf(fd, "%s %s %s.%d", ripcmds[msg->rip_cmd],
+                   dir, inet_ntoa(who->sin_addr), ntohs(who->sin_port));
        else {
                fprintf(fd, "Bad cmd 0x%x %s %x.%d\n", msg->rip_cmd,
        else {
                fprintf(fd, "Bad cmd 0x%x %s %x.%d\n", msg->rip_cmd,
-                       dir, who->sin_addr, ntohs(who->sin_port));
+                   dir, inet_ntoa(who->sin_addr), ntohs(who->sin_port));
                fprintf(fd, "size=%d cp=%x packet=%x\n", size, cp, packet);
                fprintf(fd, "size=%d cp=%x packet=%x\n", size, cp, packet);
+               fflush(fd);
                return;
        }
        switch (msg->rip_cmd) {
                return;
        }
        switch (msg->rip_cmd) {
@@ -228,9 +247,10 @@ dumppacket(fd, dir, who, cp, size)
                for (; size > 0; n++, size -= sizeof (struct netinfo)) {
                        if (size < sizeof (struct netinfo))
                                break;
                for (; size > 0; n++, size -= sizeof (struct netinfo)) {
                        if (size < sizeof (struct netinfo))
                                break;
-                       fprintf(fd, "\tdst %x metric %d\n",
-                               ((struct sockaddr_in *)&n->rip_dst)->sin_addr,
-                               n->rip_metric);
+                       fprintf(fd, "\tdst %s metric %d\n",
+#define        satosin(sa)     ((struct sockaddr_in *)&sa)
+                            inet_ntoa(satosin(n->rip_dst)->sin_addr),
+                            ntohl(n->rip_metric));
                }
                break;
 
                }
                break;
 
@@ -242,4 +262,5 @@ dumppacket(fd, dir, who, cp, size)
                fprintf(fd, "\n");
                break;
        }
                fprintf(fd, "\n");
                break;
        }
+       fflush(fd);
 }
 }