]> The Tcpdump Group git mirrors - tcpdump/blobdiff - ntp.c
Avoid -E and -M options inconsistencies with no libcrypto
[tcpdump] / ntp.c
diff --git a/ntp.c b/ntp.c
index 4d17932ffdf69a58481c08acc45462f5ffa5bb89..d1825664bbc7cb565c388b80ba64c0f1f8db3f8a 100644 (file)
--- a/ntp.c
+++ b/ntp.c
  *
  */
 
-#ifdef HAVE_CONFIG_H
 #include <config.h>
-#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;
@@ -48,38 +50,37 @@ p_ntp_time(netdissect_options *ndo,
        f = (uint32_t)(ff * 1000000000.0);      /* treat fraction as parts per billion */
        ND_PRINT("%u.%09u", i, f);
 
-#ifdef HAVE_STRFTIME
        /*
         * 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);
        }
-#endif
+}
+
+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);
 }