]> The Tcpdump Group git mirrors - tcpdump/commitdiff
- add the -ttttt timestamp option which prints the time difference
authorhannes <hannes>
Tue, 13 Dec 2005 08:37:22 +0000 (08:37 +0000)
committerhannes <hannes>
Tue, 13 Dec 2005 08:37:22 +0000 (08:37 +0000)
  (in micro-second resolution) between the first and current packet.

- cleanup the the ts_print code a bit -> add a ts_format helper

tcpdump.1
tcpdump.c
util.c

index cff629d3335f0e286b54041d44a82573e87c904b..d3281c3eaa3955397d131057333e0e3edff13f2a 100644 (file)
--- a/tcpdump.1
+++ b/tcpdump.1
@@ -1,4 +1,4 @@
-.\" @(#) $Header: /tcpdump/master/tcpdump/Attic/tcpdump.1,v 1.179 2005-12-05 20:10:58 guy Exp $ (LBL)
+.\" @(#) $Header: /tcpdump/master/tcpdump/Attic/tcpdump.1,v 1.180 2005-12-13 08:37:23 hannes Exp $ (LBL)
 .\"
 .\"    $NetBSD: tcpdump.8,v 1.9 2003/03/31 00:18:17 perry Exp $
 .\"
@@ -496,12 +496,16 @@ and
 Print an unformatted timestamp on each dump line.
 .TP
 .B \-ttt
-Print a delta (in micro-seconds) between current and previous line
+Print a delta (micro-second resolution) between current and previous line
 on each dump line.
 .TP
 .B \-tttt
 Print a timestamp in default format proceeded by date on each dump line.
 .TP
+.B \-ttttt
+Print a delta (micro-second resolution) between current and first line
+on each dump line.
+.TP
 .B \-u
 Print undecoded NFS handles.
 .TP
index 8a9ac9d9f053c35d828803e7cd4fbcddf9607347..116f3daa7abdc1c06465cb46c1badaaec720edaa 100644 (file)
--- a/tcpdump.c
+++ b/tcpdump.c
@@ -30,7 +30,7 @@ static const char copyright[] _U_ =
     "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
 The Regents of the University of California.  All rights reserved.\n";
 static const char rcsid[] _U_ =
-    "@(#) $Header: /tcpdump/master/tcpdump/tcpdump.c,v 1.264 2005-12-05 20:24:48 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/tcpdump/tcpdump.c,v 1.265 2005-12-13 08:37:22 hannes Exp $ (LBL)";
 #endif
 
 /*
@@ -834,10 +834,11 @@ main(int argc, char **argv)
        case 1: /* No time stamp */
        case 2: /* Unix timeval style */
        case 3: /* Microseconds since previous packet */
+        case 5: /* Microseconds since first packet */
                break;
 
        default: /* Not supported */
-               error("only -t, -tt, -ttt, and -tttt are supported");
+               error("only -t, -tt, -ttt, -tttt and -ttttt are supported");
                break;
        }
 
diff --git a/util.c b/util.c
index 28c0f33de7743e83110fcccf666ef76f5826da9c..13d299749ac8e380bd16179e0b9698269a5c791c 100644 (file)
--- a/util.c
+++ b/util.c
@@ -21,7 +21,7 @@
 
 #ifndef lint
 static const char rcsid[] _U_ =
-    "@(#) $Header: /tcpdump/master/tcpdump/util.c,v 1.102 2005-12-05 20:24:48 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/tcpdump/util.c,v 1.103 2005-12-13 08:37:23 hannes Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -44,6 +44,8 @@ static const char rcsid[] _U_ =
 
 #include "interface.h"
 
+char * ts_format(register int, register int);
+
 /*
  * Print out a null-terminated filename (or other ascii string).
  * If ep is NULL, assume no truncation check is needed.
@@ -138,6 +140,19 @@ fn_printzp(register const u_char *s, register u_int n,
        return (n == 0) ? 0 : ret;
 }
 
+/*
+ * Format the timestamp
+ */
+char *
+ts_format(register int sec, register int usec)
+{
+        static char buf[sizeof("00:00:00.000000")];
+        (void)snprintf(buf, sizeof(buf), "%02d:%02d:%02d.%06u",
+               sec / 3600, (sec % 3600) / 60, sec % 60, usec);
+
+        return buf;
+}
+
 /*
  * Print the timestamp
  */
@@ -154,9 +169,7 @@ ts_print(register const struct timeval *tvp)
 
        case 0: /* Default */
                s = (tvp->tv_sec + thiszone) % 86400;
-               (void)printf("%02d:%02d:%02d.%06u ",
-                            s / 3600, (s % 3600) / 60, s % 60,
-                            (unsigned)tvp->tv_usec);
+                (void)printf("%s ", ts_format(s, tvp->tv_usec));
                break;
 
        case 1: /* No time stamp */
@@ -169,22 +182,27 @@ ts_print(register const struct timeval *tvp)
                break;
 
        case 3: /* Microseconds since previous packet */
-               if (b_sec == 0) {
-                       printf("000000 ");
-               } else {
-                       int d_usec = tvp->tv_usec - b_usec;
-                       int d_sec = tvp->tv_sec - b_sec;
-
-                       while (d_usec < 0) {
-                               d_usec += 1000000;
-                               d_sec--;
-                       }
-                       if (d_sec)
-                               printf("%d. ", d_sec);
-                       printf("%06d ", d_usec);
-               }
-               b_sec = tvp->tv_sec;
-               b_usec = tvp->tv_usec;
+        case 5: /* Microseconds since first packet */
+               if (b_sec == 0 && tflag == 5) {
+                        /* init timestamp for first packet */
+                        b_usec = tvp->tv_usec;
+                        b_sec = tvp->tv_sec;                        
+                }
+
+                int d_usec = tvp->tv_usec - b_usec;
+                int d_sec = tvp->tv_sec - b_sec;
+                
+                while (d_usec < 0) {
+                    d_usec += 1000000;
+                    d_sec--;
+                }
+
+                (void)printf("%s ", ts_format(d_sec, d_usec));
+
+                if (tflag == 3) { /* set timestamp for last packet */
+                    b_sec = tvp->tv_sec;
+                    b_usec = tvp->tv_usec;
+                }
                break;
 
        case 4: /* Default + Date*/
@@ -194,10 +212,9 @@ ts_print(register const struct timeval *tvp)
                if (!tm)
                        printf("Date fail  ");
                else
-                       printf("%04d-%02d-%02d ",
-                                  tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
-               printf("%02d:%02d:%02d.%06u ",
-                          s / 3600, (s % 3600) / 60, s % 60, (unsigned)tvp->tv_usec);
+                       printf("%04d-%02d-%02d %s ",
+                               tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
+                               ts_format(s, tvp->tv_usec));
                break;
        }
 }