X-Git-Url: https://git.tcpdump.org/tcpdump/blobdiff_plain/7ceb6016de4e188a215427bc035a941ad1fceb1c..0023eaa78f123676bfa9c5fba72ea4b8a59aaa70:/ntp.c?ds=sidebyside diff --git a/ntp.c b/ntp.c index 10fabe4b..d1825664 100644 --- a/ntp.c +++ b/ntp.c @@ -20,18 +20,20 @@ * */ -#ifdef HAVE_CONFIG_H #include -#endif #include "ntp.h" #include "extract.h" -#define JAN_1970 INT64_T_CONSTANT(2208988800) /* 1970 - 1900 in seconds */ +/* NTP epoch 1: January 1, 1900, 00:00:00 UTC. */ +#define DIFF_1970_1900 INT64_C(2208988800) /* 1970 - 1900 in seconds */ +/* RFC4330 - 3. NTP Timestamp Format - 6h 28m 16s UTC on 7 February 2036 */ +/* NTP epoch 2: February 7, 2036, 06:28:16 UTC. */ +#define DIFF_2036_1970 INT64_C(2085978496) /* 2036 - 1970 in seconds */ void -p_ntp_time(netdissect_options *ndo, +p_ntp_time_fmt(netdissect_options *ndo, const char *fmt, const struct l_fixedpt *lfp) { uint32_t i; @@ -52,32 +54,33 @@ p_ntp_time(netdissect_options *ndo, * print the UTC time in human-readable format. */ if (i) { - int64_t seconds_64bit = (int64_t)i - JAN_1970; + int64_t seconds_64bit; time_t seconds; - struct tm *tm; char time_buf[128]; + const char *time_string; + if ((i & 0x80000000) != 0) + seconds_64bit = (int64_t)i - DIFF_1970_1900; + else + seconds_64bit = (int64_t)i + DIFF_2036_1970; seconds = (time_t)seconds_64bit; if (seconds != seconds_64bit) { /* * It doesn't fit into a time_t, so we can't hand it * to gmtime. */ - ND_PRINT(" (unrepresentable)"); + time_string = "[timestamp overflow]"; } else { - tm = gmtime(&seconds); - if (tm == NULL) { - /* - * gmtime() can't handle it. - * (Yes, that might happen with some version of - * Microsoft's C library.) - */ - ND_PRINT(" (unrepresentable)"); - } else { - /* use ISO 8601 (RFC3339) format */ - strftime(time_buf, sizeof (time_buf), "%Y-%m-%dT%H:%M:%SZ", tm); - ND_PRINT(" (%s)", time_buf); - } + time_string = nd_format_time(time_buf, sizeof (time_buf), + fmt, gmtime(&seconds)); } + ND_PRINT(" (%s)", time_string); } } + +void +p_ntp_time(netdissect_options *ndo, const struct l_fixedpt *lfp) +{ + /* use ISO 8601 (RFC3339) format */ + p_ntp_time_fmt(ndo, "%Y-%m-%dT%H:%M:%SZ", lfp); +}