BSD 4_3_Net_2 release
[unix-history] / usr / src / sbin / routed / trace.c
index 98f4748..6e0b478 100644 (file)
@@ -2,21 +2,37 @@
  * Copyright (c) 1983, 1988 Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1983, 1988 Regents of the University of California.
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley.  The name of the
- * University may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)trace.c    5.7 (Berkeley) %G%";
+static char sccsid[] = "@(#)trace.c    5.11 (Berkeley) 2/28/91";
 #endif /* not lint */
 
 /*
 #endif /* not lint */
 
 /*
@@ -24,19 +40,24 @@ static char sccsid[] = "@(#)trace.c 5.7 (Berkeley) %G%";
  */
 #define        RIPCMDS
 #include "defs.h"
  */
 #define        RIPCMDS
 #include "defs.h"
-#include <sys/file.h>
 #include <sys/stat.h>
 #include <sys/signal.h>
 #include <sys/stat.h>
 #include <sys/signal.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include "pathnames.h"
 
 #define        NRECORDS        50              /* size of circular trace buffer */
 #ifdef DEBUG
 FILE   *ftrace = stdout;
 
 #define        NRECORDS        50              /* size of circular trace buffer */
 #ifdef DEBUG
 FILE   *ftrace = stdout;
-int    traceactions = 1;
+int    traceactions = 0;
 #endif
 #endif
+static struct timeval lastlog;
+static char *savetracename;
 
 traceinit(ifp)
        register struct interface *ifp;
 {
 
 traceinit(ifp)
        register struct interface *ifp;
 {
+       static int iftraceinit();
 
        if (iftraceinit(ifp, &ifp->int_input) &&
            iftraceinit(ifp, &ifp->int_output))
 
        if (iftraceinit(ifp, &ifp->int_input) &&
            iftraceinit(ifp, &ifp->int_output))
@@ -75,12 +96,15 @@ traceon(file)
                return;
        if (stat(file, &stbuf) >= 0 && (stbuf.st_mode & S_IFMT) != S_IFREG)
                return;
                return;
        if (stat(file, &stbuf) >= 0 && (stbuf.st_mode & S_IFMT) != S_IFREG)
                return;
+       savetracename = file;
+       (void) gettimeofday(&now, (struct timezone *)NULL);
        ftrace = fopen(file, "a");
        if (ftrace == NULL)
                return;
        dup2(fileno(ftrace), 1);
        dup2(fileno(ftrace), 2);
        traceactions = 1;
        ftrace = fopen(file, "a");
        if (ftrace == NULL)
                return;
        dup2(fileno(ftrace), 1);
        dup2(fileno(ftrace), 2);
        traceactions = 1;
+       fprintf(ftrace, "Tracing enabled %s\n", ctime((time_t *)&now.tv_sec));
 }
 
 traceoff()
 }
 
 traceoff()
@@ -88,9 +112,11 @@ traceoff()
        if (!traceactions)
                return;
        if (ftrace != NULL) {
        if (!traceactions)
                return;
        if (ftrace != NULL) {
-               int fd = open("/dev/null", O_RDWR);
+               int fd = open(_PATH_DEVNULL, O_RDWR);
 
 
-               fprintf(ftrace, "Tracing disabled\n");
+               fprintf(ftrace, "Tracing disabled %s\n",
+                   ctime((time_t *)&now.tv_sec));
+               fflush(ftrace);
                (void) dup2(fd, 1);
                (void) dup2(fd, 2);
                (void) close(fd);
                (void) dup2(fd, 1);
                (void) dup2(fd, 2);
                (void) close(fd);
@@ -99,22 +125,62 @@ traceoff()
        }
        traceactions = 0;
        tracehistory = 0;
        }
        traceactions = 0;
        tracehistory = 0;
+       tracepackets = 0;
+       tracecontents = 0;
 }
 
 }
 
+void
 sigtrace(s)
        int s;
 {
 sigtrace(s)
        int s;
 {
-       if (s == SIGUSR2) {
+
+       if (s == SIGUSR2)
                traceoff();
                traceoff();
-               tracepackets = 0;
-       } else if (traceactions == 0)
+       else if (ftrace == NULL && savetracename)
+               traceon(savetracename);
+       else
+               bumploglevel();
+}
+
+/*
+ * Move to next higher level of tracing when -t option processed or
+ * SIGUSR1 is received.  Successive levels are:
+ *     traceactions
+ *     traceactions + tracepackets
+ *     traceactions + tracehistory (packets and contents after change)
+ *     traceactions + tracepackets + tracecontents
+ */
+bumploglevel()
+{
+
+       (void) gettimeofday(&now, (struct timezone *)NULL);
+       if (traceactions == 0) {
                traceactions++;
                traceactions++;
-       else if (tracehistory == 0)
+               if (ftrace)
+                       fprintf(ftrace, "Tracing actions started %s\n",
+                           ctime((time_t *)&now.tv_sec));
+       } else if (tracepackets == 0) {
+               tracepackets++;
+               tracehistory = 0;
+               tracecontents = 0;
+               if (ftrace)
+                       fprintf(ftrace, "Tracing packets started %s\n",
+                           ctime((time_t *)&now.tv_sec));
+       } else if (tracehistory == 0) {
                tracehistory++;
                tracehistory++;
-       else {
+               if (ftrace)
+                       fprintf(ftrace, "Tracing history started %s\n",
+                           ctime((time_t *)&now.tv_sec));
+       } else {
                tracepackets++;
                tracepackets++;
+               tracecontents++;
                tracehistory = 0;
                tracehistory = 0;
+               if (ftrace)
+                       fprintf(ftrace, "Tracing packet contents started %s\n",
+                           ctime((time_t *)&now.tv_sec));
        }
        }
+       if (ftrace)
+               fflush(ftrace);
 }
 
 trace(ifd, who, p, len, m)
 }
 
 trace(ifd, who, p, len, m)
@@ -136,7 +202,7 @@ trace(ifd, who, p, len, m)
                free(t->ift_packet);
                t->ift_packet = 0;
        }
                free(t->ift_packet);
                t->ift_packet = 0;
        }
-       t->ift_stamp = time(0);
+       t->ift_stamp = now;
        t->ift_who = *who;
        if (len > 0 && t->ift_packet == 0) {
                t->ift_packet = malloc(len);
        t->ift_who = *who;
        if (len > 0 && t->ift_packet == 0) {
                t->ift_packet = malloc(len);
@@ -180,9 +246,9 @@ traceaction(fd, action, rt)
 
        if (fd == NULL)
                return;
 
        if (fd == NULL)
                return;
-       if (curtime) {
-               fprintf(fd, "\n%s", curtime);
-               curtime = NULL;
+       if (lastlog.tv_sec != now.tv_sec || lastlog.tv_usec != now.tv_usec) {
+               fprintf(fd, "\n%.19s:\n", ctime((time_t *)&now.tv_sec));
+               lastlog = now;
        }
        fprintf(fd, "%s ", action);
        dst = (struct sockaddr_in *)&rt->rt_dst;
        }
        fprintf(fd, "%s ", action);
        dst = (struct sockaddr_in *)&rt->rt_dst;
@@ -212,9 +278,12 @@ traceaction(fd, action, rt)
                }
        }
        fprintf(fd, " timer %d\n", rt->rt_timer);
                }
        }
        fprintf(fd, " timer %d\n", rt->rt_timer);
-       if (!tracepackets && (rt->rt_state & RTS_PASSIVE) == 0 && rt->rt_ifp)
+       if (tracehistory && !tracepackets &&
+           (rt->rt_state & RTS_PASSIVE) == 0 && rt->rt_ifp)
                dumpif(fd, rt->rt_ifp);
        fflush(fd);
                dumpif(fd, rt->rt_ifp);
        fflush(fd);
+       if (ferror(fd))
+               traceoff();
 }
 
 tracenewmetric(fd, rt, newmetric)
 }
 
 tracenewmetric(fd, rt, newmetric)
@@ -226,15 +295,22 @@ tracenewmetric(fd, rt, newmetric)
 
        if (fd == NULL)
                return;
 
        if (fd == NULL)
                return;
+       if (lastlog.tv_sec != now.tv_sec || lastlog.tv_usec != now.tv_usec) {
+               fprintf(fd, "\n%.19s:\n", ctime((time_t *)&now.tv_sec));
+               lastlog = now;
+       }
        dst = (struct sockaddr_in *)&rt->rt_dst;
        gate = (struct sockaddr_in *)&rt->rt_router;
        fprintf(fd, "CHANGE metric dst %s, ", inet_ntoa(dst->sin_addr));
        fprintf(fd, "router %s, from %d to %d\n",
             inet_ntoa(gate->sin_addr), rt->rt_metric, newmetric);
        fflush(fd);
        dst = (struct sockaddr_in *)&rt->rt_dst;
        gate = (struct sockaddr_in *)&rt->rt_router;
        fprintf(fd, "CHANGE metric dst %s, ", inet_ntoa(dst->sin_addr));
        fprintf(fd, "router %s, from %d to %d\n",
             inet_ntoa(gate->sin_addr), rt->rt_metric, newmetric);
        fflush(fd);
+       if (ferror(fd))
+               traceoff();
 }
 
 dumpif(fd, ifp)
 }
 
 dumpif(fd, ifp)
+       FILE *fd;
        register struct interface *ifp;
 {
        if (ifp->int_input.ifd_count || ifp->int_output.ifd_count) {
        register struct interface *ifp;
 {
        if (ifp->int_input.ifd_count || ifp->int_output.ifd_count) {
@@ -246,7 +322,6 @@ dumpif(fd, ifp)
                dumptrace(fd, "from", &ifp->int_input);
                fprintf(fd, "*** end packet history ***\n");
        }
                dumptrace(fd, "from", &ifp->int_input);
                fprintf(fd, "*** end packet history ***\n");
        }
-       fflush(fd);
 }
 
 dumptrace(fd, dir, ifd)
 }
 
 dumptrace(fd, dir, ifd)
@@ -277,28 +352,34 @@ dumptrace(fd, dir, ifd)
        }
 }
 
        }
 }
 
-dumppacket(fd, dir, who, cp, size, tstamp)
+dumppacket(fd, dir, who, cp, size, stamp)
        FILE *fd;
        struct sockaddr_in *who;                /* should be sockaddr */
        char *dir, *cp;
        register int size;
        FILE *fd;
        struct sockaddr_in *who;                /* should be sockaddr */
        char *dir, *cp;
        register int size;
-       time_t *tstamp;
+       struct timeval *stamp;
 {
        register struct rip *msg = (struct rip *)cp;
        register struct netinfo *n;
 
 {
        register struct rip *msg = (struct rip *)cp;
        register struct netinfo *n;
 
+       if (fd == NULL)
+               return;
        if (msg->rip_cmd && msg->rip_cmd < RIPCMD_MAX)
        if (msg->rip_cmd && msg->rip_cmd < RIPCMD_MAX)
-               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 %.24s\n", msg->rip_cmd,
+               fprintf(fd, "%s %s %s.%d %.19s:\n", ripcmds[msg->rip_cmd],
                    dir, inet_ntoa(who->sin_addr), ntohs(who->sin_port),
                    dir, inet_ntoa(who->sin_addr), ntohs(who->sin_port),
-                   ctime(tstamp));
-               fprintf(fd, "size=%d cp=%x packet=%x\n", size, cp, packet);
+                   ctime((time_t *)&stamp->tv_sec));
+       else {
+               fprintf(fd, "Bad cmd 0x%x %s %x.%d %.19s\n", msg->rip_cmd,
+                   dir, inet_ntoa(who->sin_addr), ntohs(who->sin_port));
+               fprintf(fd, "size=%d cp=%x packet=%x\n", size, cp, packet,
+                   ctime((time_t *)&stamp->tv_sec));
+               fflush(fd);
+               return;
+       }
+       if (tracepackets && tracecontents == 0) {
                fflush(fd);
                return;
        }
                fflush(fd);
                return;
        }
-       fprintf(fd, " %.24s:\n", ctime(tstamp));
        switch (msg->rip_cmd) {
 
        case RIPCMD_REQUEST:
        switch (msg->rip_cmd) {
 
        case RIPCMD_REQUEST:
@@ -306,12 +387,29 @@ dumppacket(fd, dir, who, cp, size, tstamp)
                size -= 4 * sizeof (char);
                n = msg->rip_nets;
                for (; size > 0; n++, size -= sizeof (struct netinfo)) {
                size -= 4 * sizeof (char);
                n = msg->rip_nets;
                for (; size > 0; n++, size -= sizeof (struct netinfo)) {
-                       if (size < sizeof (struct netinfo))
+                       if (size < sizeof (struct netinfo)) {
+                               fprintf(fd, "(truncated record, len %d)\n",
+                                   size);
                                break;
                                break;
-                       fprintf(fd, "\tdst %s metric %d\n",
+                       }
+                       if (sizeof(n->rip_dst.sa_family) > 1)
+                           n->rip_dst.sa_family = ntohs(n->rip_dst.sa_family);
+
+                       switch ((int)n->rip_dst.sa_family) {
+
+                       case AF_INET:
+                               fprintf(fd, "\tdst %s metric %d\n",
 #define        satosin(sa)     ((struct sockaddr_in *)&sa)
 #define        satosin(sa)     ((struct sockaddr_in *)&sa)
-                            inet_ntoa(satosin(n->rip_dst)->sin_addr),
-                            ntohl(n->rip_metric));
+                                    inet_ntoa(satosin(n->rip_dst)->sin_addr),
+                                    ntohl(n->rip_metric));
+                               break;
+
+                       default:
+                               fprintf(fd, "\taf %d? metric %d\n",
+                                    n->rip_dst.sa_family,
+                                    ntohl(n->rip_metric));
+                               break;
+                       }
                }
                break;
 
                }
                break;
 
@@ -323,4 +421,6 @@ dumppacket(fd, dir, who, cp, size, tstamp)
                break;
        }
        fflush(fd);
                break;
        }
        fflush(fd);
+       if (ferror(fd))
+               traceoff();
 }
 }