]> The Tcpdump Group git mirrors - tcpdump/commitdiff
Fix display of timestamps with -ttt and -ttttt options
authorFrancois-Xavier Le Bail <[email protected]>
Thu, 2 Jul 2015 17:14:49 +0000 (19:14 +0200)
committerFrancois-Xavier Le Bail <[email protected]>
Thu, 2 Jul 2015 17:14:49 +0000 (19:14 +0200)
- Fix display of some nanoseconds timestamps
For example, bad print 00:-16:-2.000851566 is now 00:00:00.037851566

- Fix display of timestamp of a packet when it is lower than previous one
For example, bad print 00:00:-1.000999790 is now -00:00:00.000000210

INSTALL.txt
Makefile.in
timeval-operations.h [new file with mode: 0644]
util-print.c

index 5aefc458388329d79fb6305ee5b196237c9266f0..39e12f8b12619afcffb6b48f61009213aa8d324e 100644 (file)
@@ -207,6 +207,7 @@ stime.awk   - TCP send awk script
 tcp.h          - TCP definitions
 tcpdump.1      - manual entry
 tcpdump.c      - main program
+timeval-operations.h - timeval operations macros
 udp.h          - UDP definitions
 util.c         - utility routines
 vfprintf.c     - emulation routine
index 7e4f7b1106cc9be9978e9839c93bb2cca8b29ebc..cfd619c12b6378d27a14a43c644c6250003f0be0 100644 (file)
@@ -277,6 +277,7 @@ HDR = \
        smb.h \
        tcp.h \
        tcpdump-stdinc.h \
+       timeval-operations.h \
        udp.h
 
 TAGHDR = \
diff --git a/timeval-operations.h b/timeval-operations.h
new file mode 100644 (file)
index 0000000..e365939
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2015 The TCPDUMP project
+ * All rights reserved.
+ *
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
+ * COPYRIGHT HOLDER 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 tcpdump_timeval_operations_h
+#define tcpdump_timeval_operations_h
+
+/* Operations on timevals. */
+
+#ifndef _MICRO_PER_SEC
+#define _MICRO_PER_SEC 1000000
+#endif
+
+#ifndef _NANO_PER_SEC
+#define _NANO_PER_SEC 1000000000
+#endif
+
+#define tcpdump_timevalclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
+
+#define tcpdump_timevalisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
+
+#define tcpdump_timevalcmp(tvp, uvp, cmp)      \
+       (((tvp)->tv_sec == (uvp)->tv_sec) ?    \
+        ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
+        ((tvp)->tv_sec cmp (uvp)->tv_sec))
+
+#define tcpdump_timevaladd(tvp, uvp, vvp, nano_prec)              \
+       do {                                                      \
+               (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;    \
+               (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
+               if (nano_prec) {                                  \
+                       if ((vvp)->tv_usec >= _NANO_PER_SEC) {    \
+                               (vvp)->tv_sec++;                  \
+                               (vvp)->tv_usec -= _NANO_PER_SEC;  \
+                       }                                         \
+               } else {                                          \
+                       if ((vvp)->tv_usec >= _MICRO_PER_SEC) {   \
+                               (vvp)->tv_sec++;                  \
+                               (vvp)->tv_usec -= _MICRO_PER_SEC; \
+                       }                                         \
+               }                                                 \
+       } while (0)
+
+#define tcpdump_timevalsub(tvp, uvp, vvp, nano_prec)               \
+       do {                                                       \
+               (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;     \
+               (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;  \
+               if ((vvp)->tv_usec < 0) {                          \
+                   (vvp)->tv_sec--;                               \
+                   (vvp)->tv_usec += (nano_prec ? _NANO_PER_SEC : \
+                                      _MICRO_PER_SEC);            \
+               }                                                  \
+       } while (0)
+
+#endif /* tcpdump_timeval_operations_h */
index 758aa77331bcae80af9d2a12f3d607c2a8f074e7..cfdcfe8c063ee59d3af366729bf4a3d177ff32a3 100644 (file)
@@ -53,6 +53,7 @@
 
 #include "interface.h"
 #include "ascii_strcasecmp.h"
+#include "timeval-operations.h"
 
 int32_t thiszone;              /* seconds offset from gmt to local time */
 
@@ -241,11 +242,11 @@ ts_print(netdissect_options *ndo,
        register int s;
        struct tm *tm;
        time_t Time;
-       static unsigned b_sec;
-       static unsigned b_usec;
-       int d_usec;
-       int d_sec;
        char buf[TS_BUF_SIZE];
+       static struct timeval tv_ref;
+       struct timeval tv_result;
+       int negative_offset;
+       int nano_prec;
 
        switch (ndo->ndo_tflag) {
 
@@ -262,28 +263,39 @@ ts_print(netdissect_options *ndo,
                          tvp->tv_sec, tvp->tv_usec, buf)));
                break;
 
-       case 3: /* Microseconds since previous packet */
-        case 5: /* Microseconds since first packet */
-               if (b_sec == 0) {
-                        /* init timestamp for first packet */
-                        b_usec = tvp->tv_usec;
-                        b_sec = tvp->tv_sec;
-                }
+       case 3: /* Microseconds/nanoseconds since previous packet */
+        case 5: /* Microseconds/nanoseconds since first packet */
+#ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
+               switch (ndo->ndo_tstamp_precision) {
+               case PCAP_TSTAMP_PRECISION_MICRO:
+                       nano_prec = 0;
+                       break;
+               case PCAP_TSTAMP_PRECISION_NANO:
+                       nano_prec = 1;
+                       break;
+               default:
+                       nano_prec = 0;
+                       break;
+               }
+#else
+               nano_prec = 0;
+#endif
+               if (!(tcpdump_timevalisset(&tv_ref)))
+                       tv_ref = *tvp; /* set timestamp for first packet */
 
-                d_usec = tvp->tv_usec - b_usec;
-                d_sec = tvp->tv_sec - b_sec;
+               negative_offset = tcpdump_timevalcmp(tvp, &tv_ref, <);
+               if (negative_offset)
+                       tcpdump_timevalsub(&tv_ref, tvp, &tv_result, nano_prec);
+               else
+                       tcpdump_timevalsub(tvp, &tv_ref, &tv_result, nano_prec);
 
-                while (d_usec < 0) {
-                    d_usec += 1000000;
-                    d_sec--;
-                }
+               ND_PRINT((ndo, (negative_offset ? "-" : " ")));
 
-                ND_PRINT((ndo, "%s ", ts_format(ndo, d_sec, d_usec, buf)));
+               ND_PRINT((ndo, "%s ", ts_format(ndo,
+                         tv_result.tv_sec, tv_result.tv_usec, buf)));
 
-                if (ndo->ndo_tflag == 3) { /* set timestamp for last packet */
-                    b_sec = tvp->tv_sec;
-                    b_usec = tvp->tv_usec;
-                }
+                if (ndo->ndo_tflag == 3)
+                       tv_ref = *tvp; /* set timestamp for previous packet */
                break;
 
        case 4: /* Default + Date */