diff options
| author | Roy Marples <roy@marples.name> | 2016-11-18 15:27:20 +0000 |
|---|---|---|
| committer | Roy Marples <roy@marples.name> | 2016-11-18 15:27:20 +0000 |
| commit | b6353940c5d6d901f03d9313dcd03bc2257cdc51 (patch) | |
| tree | 7657d6e9ef8525b86d402b0eaac58dd0720a34f8 | |
| parent | 2de9a84057ca252345ef74982608291f0a879e47 (diff) | |
| download | dhcpcd-b6353940c5d6d901f03d9313dcd03bc2257cdc51.tar.xz | |
Improve NTP timestamp from realtime and fix the double host to network translation on it.
| -rw-r--r-- | auth.c | 13 |
1 files changed, 6 insertions, 7 deletions
@@ -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; } |
