From d5fe3261f49e085cb677d8c700294cf19ba23a15 Mon Sep 17 00:00:00 2001 From: John Brzustowski Date: Wed, 13 Jul 2022 04:08:54 -0300 Subject: [PATCH] NTP reponse gives fractional (not micro-) seconds, per RFC5905 --- mongoose.c | 4 ++-- src/sntp.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/mongoose.c b/mongoose.c index 9f31d735..595576fc 100644 --- a/mongoose.c +++ b/mongoose.c @@ -3797,8 +3797,8 @@ int64_t mg_sntp_parse(const unsigned char *buf, size_t len) { } else if (version == 4 || version == 3) { uint32_t *data = (uint32_t *) &buf[40]; unsigned long seconds = mg_ntohl(data[0]) - SNTP_TIME_OFFSET; - unsigned long useconds = mg_ntohl(data[1]); - res = ((int64_t) seconds) * 1000 + (int64_t) ((useconds / 1000) % 1000); + unsigned long fracseconds = mg_ntohl(data[1]); + res = ((int64_t) seconds) * 1000 + (((int64_t) (fracseconds) * 1000) >> 32); } else { MG_ERROR(("unexpected version: %d", version)); } diff --git a/src/sntp.c b/src/sntp.c index f46682e8..dfb645e3 100644 --- a/src/sntp.c +++ b/src/sntp.c @@ -20,8 +20,8 @@ int64_t mg_sntp_parse(const unsigned char *buf, size_t len) { } else if (version == 4 || version == 3) { uint32_t *data = (uint32_t *) &buf[40]; unsigned long seconds = mg_ntohl(data[0]) - SNTP_TIME_OFFSET; - unsigned long useconds = mg_ntohl(data[1]); - res = ((int64_t) seconds) * 1000 + (int64_t) ((useconds / 1000) % 1000); + unsigned long fracseconds = mg_ntohl(data[1]); + res = ((int64_t) seconds) * 1000 + (((int64_t) (fracseconds) * 1000) >> 32); } else { MG_ERROR(("unexpected version: %d", version)); }