]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Improve NTP timestamp from realtime and fix the double host to network translation...
authorRoy Marples <roy@marples.name>
Fri, 18 Nov 2016 15:27:20 +0000 (15:27 +0000)
committerRoy Marples <roy@marples.name>
Fri, 18 Nov 2016 15:27:20 +0000 (15:27 +0000)
auth.c

diff --git a/auth.c b/auth.c
index dc8f0274bea61e791a56b531f424f0b6e34e6af8..c73f893a9b2358bd93bdd8f8318f4a4b9e56ac6d 100644 (file)
--- a/auth.c
+++ b/auth.c
@@ -439,22 +439,21 @@ get_next_rdm_monotonic_counter(struct auth *auth)
        return rdm;
 }
 
-#define JAN_1970       2208988800U    /* 1970 - 1900 in seconds */
+#define        NTP_EPOCH       2208988800U     /* 1970 - 1900 in seconds */
+#define        NTP_SCALE_FRAC  4294967295.0    /* max value of the fractional part */
 static uint64_t
 get_next_rdm_monotonic_clock(struct auth *auth)
 {
        struct timespec ts;
-       uint32_t pack[2];
+       uint64_t secs, rdm;
        double frac;
-       uint64_t rdm;
 
        if (clock_gettime(CLOCK_REALTIME, &ts) != 0)
                return ++auth->last_replay; /* report error? */
-       pack[0] = htonl((uint32_t)ts.tv_sec + JAN_1970);
-       frac = ((double)ts.tv_nsec / 1e9 * 0x100000000ULL);
-       pack[1] = htonl((uint32_t)frac);
 
-       memcpy(&rdm, &pack, sizeof(rdm));
+       secs = (uint64_t)ts.tv_sec + NTP_EPOCH;
+       frac = ((double)ts.tv_nsec / 1e9 * NTP_SCALE_FRAC);
+       rdm = (secs << 32) | (uint64_t)frac;
        return rdm;
 }