2021-07-24 18:40:52 +08:00
|
|
|
#include "sntp.h"
|
2020-12-05 19:26:32 +08:00
|
|
|
#include "arch.h"
|
|
|
|
#include "event.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2021-12-22 05:50:18 +08:00
|
|
|
#define SNTP_INTERVAL_SEC 3600
|
2021-07-24 18:40:52 +08:00
|
|
|
#define SNTP_TIME_OFFSET 2208988800UL
|
2020-12-05 19:26:32 +08:00
|
|
|
|
2021-12-22 05:50:18 +08:00
|
|
|
int64_t mg_sntp_parse(const unsigned char *buf, size_t len) {
|
|
|
|
int64_t res = -1;
|
|
|
|
int mode = len > 0 ? buf[0] & 7 : 0;
|
2022-05-26 23:22:44 +08:00
|
|
|
int version = len > 0 ? (buf[0] >> 3) & 7 : 0;
|
2020-12-05 19:26:32 +08:00
|
|
|
if (len < 48) {
|
2022-02-13 02:17:25 +08:00
|
|
|
MG_ERROR(("%s", "corrupt packet"));
|
2020-12-05 19:26:32 +08:00
|
|
|
} else if (mode != 4 && mode != 5) {
|
2022-02-13 02:17:25 +08:00
|
|
|
MG_ERROR(("%s", "not a server reply"));
|
2020-12-05 19:26:32 +08:00
|
|
|
} else if (buf[1] == 0) {
|
2022-02-13 02:17:25 +08:00
|
|
|
MG_ERROR(("%s", "server sent a kiss of death"));
|
2022-05-26 23:22:44 +08:00
|
|
|
} else if (version == 4 || version == 3) {
|
2020-12-05 19:26:32 +08:00
|
|
|
uint32_t *data = (uint32_t *) &buf[40];
|
2021-12-22 05:50:18 +08:00
|
|
|
unsigned long seconds = mg_ntohl(data[0]) - SNTP_TIME_OFFSET;
|
2022-07-13 15:08:54 +08:00
|
|
|
unsigned long fracseconds = mg_ntohl(data[1]);
|
|
|
|
res = ((int64_t) seconds) * 1000 + (((int64_t) (fracseconds) * 1000) >> 32);
|
2022-05-26 23:22:44 +08:00
|
|
|
} else {
|
|
|
|
MG_ERROR(("unexpected version: %d", version));
|
2020-12-05 19:26:32 +08:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sntp_cb(struct mg_connection *c, int ev, void *evd, void *fnd) {
|
|
|
|
if (ev == MG_EV_READ) {
|
2021-12-22 05:50:18 +08:00
|
|
|
int64_t milliseconds = mg_sntp_parse(c->recv.buf, c->recv.len);
|
|
|
|
if (milliseconds > 0) {
|
2022-04-07 20:50:25 +08:00
|
|
|
mg_call(c, MG_EV_SNTP_TIME, (uint64_t *) &milliseconds);
|
2022-05-07 03:19:40 +08:00
|
|
|
MG_VERBOSE(("%u.%u", (unsigned) (milliseconds / 1000),
|
|
|
|
(unsigned) (milliseconds % 1000)));
|
2020-12-05 19:26:32 +08:00
|
|
|
}
|
2022-05-16 01:41:01 +08:00
|
|
|
mg_iobuf_del(&c->recv, 0, c->recv.len); // Free receive buffer
|
2021-08-08 05:55:13 +08:00
|
|
|
} else if (ev == MG_EV_CONNECT) {
|
2022-05-16 01:41:01 +08:00
|
|
|
mg_sntp_request(c);
|
2020-12-05 19:26:32 +08:00
|
|
|
} else if (ev == MG_EV_CLOSE) {
|
|
|
|
}
|
|
|
|
(void) fnd;
|
|
|
|
(void) evd;
|
|
|
|
}
|
|
|
|
|
2022-05-16 01:41:01 +08:00
|
|
|
void mg_sntp_request(struct mg_connection *c) {
|
2020-12-05 19:26:32 +08:00
|
|
|
if (c->is_resolving) {
|
2022-02-13 02:17:25 +08:00
|
|
|
MG_ERROR(("%lu wait until resolved", c->id));
|
2022-02-23 04:20:56 +08:00
|
|
|
} else {
|
2020-12-05 19:26:32 +08:00
|
|
|
uint8_t buf[48] = {0};
|
2021-10-14 05:02:58 +08:00
|
|
|
buf[0] = (0 << 6) | (4 << 3) | 3;
|
2020-12-05 19:26:32 +08:00
|
|
|
mg_send(c, buf, sizeof(buf));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct mg_connection *mg_sntp_connect(struct mg_mgr *mgr, const char *url,
|
|
|
|
mg_event_handler_t fn, void *fnd) {
|
|
|
|
struct mg_connection *c = NULL;
|
|
|
|
if (url == NULL) url = "udp://time.google.com:123";
|
|
|
|
if ((c = mg_connect(mgr, url, fn, fnd)) != NULL) c->pfn = sntp_cb;
|
|
|
|
return c;
|
|
|
|
}
|