mirror of
https://github.com/the-tcpdump-group/tcpdump.git
synced 2025-01-19 06:08:24 +08:00
Fix local time printing
Using seconds offset from GMT to local time to compute local time give errors when printing times outside the same daylight saving time period. Use the localtime() function for default and -tttt cases. Use the gmtime() function for -ttt and -ttttt cases. Rename ts_hmsfrac_print() to ts_date_hmsfrac_print(). Remove some variables now useless. Update some comments.
This commit is contained in:
parent
7df3ddfdcd
commit
d77ac851c7
@ -119,7 +119,6 @@ typedef unsigned char nd_byte;
|
||||
#include "ip.h" /* struct ip for nextproto4_cksum() */
|
||||
#include "ip6.h" /* struct ip6 for nextproto6_cksum() */
|
||||
|
||||
extern int32_t thiszone; /* seconds offset from gmt to local time */
|
||||
/* invalid string to print '(invalid)' for malformed or corrupted packets */
|
||||
extern const char istr[];
|
||||
|
||||
|
4
print.c
4
print.c
@ -243,11 +243,9 @@ static int ndo_printf(netdissect_options *ndo,
|
||||
PRINTFLIKE(2, 3);
|
||||
|
||||
void
|
||||
init_print(netdissect_options *ndo, uint32_t localnet, uint32_t mask,
|
||||
uint32_t timezone_offset)
|
||||
init_print(netdissect_options *ndo, uint32_t localnet, uint32_t mask)
|
||||
{
|
||||
|
||||
thiszone = timezone_offset;
|
||||
init_addrtoname(ndo, localnet, mask);
|
||||
init_checksum();
|
||||
}
|
||||
|
3
print.h
3
print.h
@ -28,8 +28,7 @@
|
||||
#ifndef print_h
|
||||
#define print_h
|
||||
|
||||
void init_print(netdissect_options *ndo, uint32_t localnet, uint32_t mask,
|
||||
uint32_t timezone_offset);
|
||||
void init_print(netdissect_options *ndo, uint32_t localnet, uint32_t mask);
|
||||
|
||||
int has_printer(int type);
|
||||
|
||||
|
13
tcpdump.c
13
tcpdump.c
@ -132,7 +132,6 @@ The Regents of the University of California. All rights reserved.\n";
|
||||
#include "interface.h"
|
||||
#include "addrtoname.h"
|
||||
#include "machdep.h"
|
||||
#include "gmt2local.h"
|
||||
#include "pcap-missing.h"
|
||||
#include "ascii_strcasecmp.h"
|
||||
|
||||
@ -1412,7 +1411,6 @@ main(int argc, char **argv)
|
||||
{
|
||||
int cnt, op, i;
|
||||
bpf_u_int32 localnet = 0, netmask = 0;
|
||||
int timezone_offset = 0;
|
||||
char *cp, *infile, *cmdbuf, *device, *RFileName, *VFileName, *WFileName;
|
||||
char *endp;
|
||||
pcap_handler callback;
|
||||
@ -1869,14 +1867,11 @@ main(int argc, char **argv)
|
||||
switch (ndo->ndo_tflag) {
|
||||
|
||||
case 0: /* Default */
|
||||
case 4: /* Default + Date*/
|
||||
timezone_offset = gmt2local(0);
|
||||
break;
|
||||
|
||||
case 1: /* No time stamp */
|
||||
case 2: /* Unix timeval style */
|
||||
case 3: /* Microseconds since previous packet */
|
||||
case 5: /* Microseconds since first packet */
|
||||
case 3: /* Microseconds/nanoseconds since previous packet */
|
||||
case 4: /* Date + Default */
|
||||
case 5: /* Microseconds/nanoseconds since first packet */
|
||||
break;
|
||||
|
||||
default: /* Not supported */
|
||||
@ -2138,7 +2133,7 @@ main(int argc, char **argv)
|
||||
capdns = capdns_setup();
|
||||
#endif /* HAVE_CASPER */
|
||||
|
||||
init_print(ndo, localnet, netmask, timezone_offset);
|
||||
init_print(ndo, localnet, netmask);
|
||||
|
||||
#ifndef _WIN32
|
||||
(void)setsignal(SIGPIPE, cleanup);
|
||||
|
69
util-print.c
69
util-print.c
@ -57,12 +57,15 @@
|
||||
#include "ascii_strcasecmp.h"
|
||||
#include "timeval-operations.h"
|
||||
|
||||
int32_t thiszone; /* seconds offset from gmt to local time */
|
||||
/* invalid string to print '(invalid)' for malformed or corrupted packets */
|
||||
const char istr[] = " (invalid)";
|
||||
|
||||
#define TOKBUFSIZE 128
|
||||
|
||||
|
||||
enum date_flag { WITHOUT_DATE = 0, WITH_DATE = 1 };
|
||||
enum time_flag { UTC_TIME = 0, LOCAL_TIME = 1 };
|
||||
|
||||
/*
|
||||
* Print out a character, filtering out the non-printable ones
|
||||
*/
|
||||
@ -229,32 +232,50 @@ nd_printzp(netdissect_options *ndo,
|
||||
}
|
||||
|
||||
/*
|
||||
* Print the timestamp as HH:MM:SS.FRAC.
|
||||
* Print the timestamp as [YY:MM:DD] HH:MM:SS.FRAC.
|
||||
* if time_flag == LOCAL_TIME print local time else UTC/GMT time
|
||||
* if date_flag == WITH_DATE print YY:MM:DD before HH:MM:SS.FRAC
|
||||
*/
|
||||
static void
|
||||
ts_hmsfrac_print(netdissect_options *ndo, int sec, int usec)
|
||||
ts_date_hmsfrac_print(netdissect_options *ndo, int sec, int usec,
|
||||
enum date_flag date_flag, enum time_flag time_flag)
|
||||
{
|
||||
time_t Time = sec;
|
||||
struct tm *tm;
|
||||
char timestr[32];
|
||||
|
||||
if (time_flag == LOCAL_TIME)
|
||||
tm = localtime(&Time);
|
||||
else
|
||||
tm = gmtime(&Time);
|
||||
|
||||
if (!tm) {
|
||||
ND_PRINT("[Error converting time]");
|
||||
return;
|
||||
}
|
||||
if (date_flag == WITH_DATE)
|
||||
strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S", tm);
|
||||
else
|
||||
strftime(timestr, sizeof(timestr), "%H:%M:%S", tm);
|
||||
ND_PRINT("%s", timestr);
|
||||
|
||||
#ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
|
||||
switch (ndo->ndo_tstamp_precision) {
|
||||
|
||||
case PCAP_TSTAMP_PRECISION_MICRO:
|
||||
ND_PRINT("%02d:%02d:%02d.%06u", sec / 3600, (sec % 3600) / 60,
|
||||
sec % 60, usec);
|
||||
ND_PRINT(".%06u", usec);
|
||||
break;
|
||||
|
||||
case PCAP_TSTAMP_PRECISION_NANO:
|
||||
ND_PRINT("%02d:%02d:%02d.%09u", sec / 3600, (sec % 3600) / 60,
|
||||
sec % 60, usec);
|
||||
ND_PRINT(".%09u", usec);
|
||||
break;
|
||||
|
||||
default:
|
||||
ND_PRINT("%02d:%02d:%02d.{unknown}", sec / 3600, (sec % 3600) / 60,
|
||||
sec % 60);
|
||||
ND_PRINT(".{unknown}");
|
||||
break;
|
||||
}
|
||||
#else
|
||||
ND_PRINT("%02d:%02d:%02d.%06u", sec / 3600, (sec % 3600) / 60,
|
||||
sec % 60, usec);
|
||||
ND_PRINT(".%06u", usec);
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -291,9 +312,6 @@ void
|
||||
ts_print(netdissect_options *ndo,
|
||||
const struct timeval *tvp)
|
||||
{
|
||||
int s;
|
||||
struct tm *tm;
|
||||
time_t Time;
|
||||
static struct timeval tv_ref;
|
||||
struct timeval tv_result;
|
||||
int negative_offset;
|
||||
@ -302,8 +320,8 @@ ts_print(netdissect_options *ndo,
|
||||
switch (ndo->ndo_tflag) {
|
||||
|
||||
case 0: /* Default */
|
||||
s = (tvp->tv_sec + thiszone) % 86400;
|
||||
ts_hmsfrac_print(ndo, s, tvp->tv_usec);
|
||||
ts_date_hmsfrac_print(ndo, tvp->tv_sec, tvp->tv_usec,
|
||||
WITHOUT_DATE, LOCAL_TIME);
|
||||
ND_PRINT(" ");
|
||||
break;
|
||||
|
||||
@ -342,25 +360,18 @@ ts_print(netdissect_options *ndo,
|
||||
netdissect_timevalsub(tvp, &tv_ref, &tv_result, nano_prec);
|
||||
|
||||
ND_PRINT((negative_offset ? "-" : " "));
|
||||
ts_hmsfrac_print(ndo, tv_result.tv_sec, tv_result.tv_usec);
|
||||
ts_date_hmsfrac_print(ndo, tv_result.tv_sec, tv_result.tv_usec,
|
||||
WITHOUT_DATE, UTC_TIME);
|
||||
ND_PRINT(" ");
|
||||
|
||||
if (ndo->ndo_tflag == 3)
|
||||
tv_ref = *tvp; /* set timestamp for previous packet */
|
||||
break;
|
||||
|
||||
case 4: /* Default + Date */
|
||||
s = (tvp->tv_sec + thiszone) % 86400;
|
||||
Time = (tvp->tv_sec + thiszone) - s;
|
||||
tm = gmtime (&Time);
|
||||
if (!tm)
|
||||
ND_PRINT("Date fail ");
|
||||
else {
|
||||
ND_PRINT("%04d-%02d-%02d ",
|
||||
tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
|
||||
ts_hmsfrac_print(ndo, s, tvp->tv_usec);
|
||||
ND_PRINT(" ");
|
||||
}
|
||||
case 4: /* Date + Default */
|
||||
ts_date_hmsfrac_print(ndo, tvp->tv_sec, tvp->tv_usec,
|
||||
WITH_DATE, LOCAL_TIME);
|
||||
ND_PRINT(" ");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user