--- /dev/null
+/*
+ * 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 */
#include "interface.h"
#include "ascii_strcasecmp.h"
+#include "timeval-operations.h"
int32_t thiszone; /* seconds offset from gmt to local time */
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) {
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 */