]> 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:26:45 +0000 (15:26 +0000)
committerRoy Marples <roy@marples.name>
Fri, 18 Nov 2016 15:26:45 +0000 (15:26 +0000)
auth.c

diff --git a/auth.c b/auth.c
index 48bf04aba9ccda688b87bd0bee196692aca4cc09..45fe9aa57ffc36cc408300ccb6aaba9830bed538 100644 (file)
--- a/auth.c
+++ b/auth.c
@@ -433,22 +433,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;
 }