Merge pull request #1629 from jbrzusto/fix-mg_ntp_parse

NTP reponse gives fractional (not micro-) seconds, per RFC5905
This commit is contained in:
Sergey Lyubka 2022-07-15 00:43:31 +01:00 committed by GitHub
commit 86aa38bfd5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 4 deletions

View File

@ -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));
}

View File

@ -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));
}