many cleanups; avoid interface timing out; put it back asap
[unix-history] / usr / src / sbin / routed / trace.c
index 5411730..ce3e1d2 100644 (file)
@@ -1,12 +1,12 @@
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)trace.c    4.1 %G%";
+static char sccsid[] = "@(#)trace.c    4.6 (Berkeley) %G%";
 #endif
 
 /*
  * Routing Table Management Daemon
  */
 #define        RIPCMDS
 #endif
 
 /*
  * Routing Table Management Daemon
  */
 #define        RIPCMDS
-#include "router.h"
+#include "defs.h"
 
 #define        NRECORDS        50              /* size of circular trace buffer */
 #ifdef DEBUG
 
 #define        NRECORDS        50              /* size of circular trace buffer */
 #ifdef DEBUG
@@ -37,6 +37,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;
@@ -82,6 +83,8 @@ 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 (ifd->ifd_count < NRECORDS)
+               ifd->ifd_count++;
        if (t->ift_size > 0 && t->ift_packet)
                free(t->ift_packet);
        t->ift_packet = 0;
        if (t->ift_size > 0 && t->ift_packet)
                free(t->ift_packet);
        t->ift_packet = 0;
@@ -129,8 +132,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 +157,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 +165,13 @@ 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");
+       }
 }
 
 dumptrace(fd, dir, ifd)
 }
 
 dumptrace(fd, dir, ifd)
@@ -184,14 +188,12 @@ dumptrace(fd, dir, ifd)
                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,11 +212,11 @@ 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);
                return;
        }
                fprintf(fd, "size=%d cp=%x packet=%x\n", size, cp, packet);
                return;
        }
@@ -228,9 +230,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;