-#define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1)
-
-#ifndef ADJ_SETOFFSET
-#define ADJ_SETOFFSET 0x0100 /* add 'time' to current time */
-#endif
-
-/* expected accuracy of time synchronization; used to adjust the poll interval */
-#define NTP_ACCURACY_SEC 0.2
-
-/*
- * "A client MUST NOT under any conditions use a poll interval less
- * than 15 seconds."
- */
-#define NTP_POLL_INTERVAL_MIN_SEC 32
-#define NTP_POLL_INTERVAL_MAX_SEC 2048
-
-/*
- * Maximum delta in seconds which the system clock is gradually adjusted
- * (slew) to approach the network time. Deltas larger that this are set by
- * letting the system time jump. The kernel's limit for adjtime is 0.5s.
- */
-#define NTP_MAX_ADJUST 0.4
-
-/* NTP protocol, packet header */
-#define NTP_LEAP_PLUSSEC 1
-#define NTP_LEAP_MINUSSEC 2
-#define NTP_LEAP_NOTINSYNC 3
-#define NTP_MODE_CLIENT 3
-#define NTP_MODE_SERVER 4
-#define NTP_FIELD_LEAP(f) (((f) >> 6) & 3)
-#define NTP_FIELD_VERSION(f) (((f) >> 3) & 7)
-#define NTP_FIELD_MODE(f) ((f) & 7)
-#define NTP_FIELD(l, v, m) (((l) << 6) | ((v) << 3) | (m))
-
-/*
- * "NTP timestamps are represented as a 64-bit unsigned fixed-point number,
- * in seconds relative to 0h on 1 January 1900."
- */
-#define OFFSET_1900_1970 2208988800UL
-
-struct ntp_ts {
- be32_t sec;
- be32_t frac;
-} _packed_;
-
-struct ntp_ts_short {
- be16_t sec;
- be16_t frac;
-} _packed_;
-
-struct ntp_msg {
- uint8_t field;
- uint8_t stratum;
- int8_t poll;
- int8_t precision;
- struct ntp_ts_short root_delay;
- struct ntp_ts_short root_dispersion;
- char refid[4];
- struct ntp_ts reference_time;
- struct ntp_ts origin_time;
- struct ntp_ts recv_time;
- struct ntp_ts trans_time;
-} _packed_;
-
-typedef struct Manager Manager;
-struct Manager {
- sd_event *event;
-
- /* peer */
- sd_event_source *event_receive;
- char *server;
- struct sockaddr_in server_addr;
- int server_socket;
- uint64_t packet_count;
-
- /* last sent packet */
- struct timespec trans_time_mon;
- struct timespec trans_time;
- usec_t retry_interval;
- bool pending;
-
- /* poll timer */
- sd_event_source *event_timer;
- usec_t poll_interval_usec;
- bool poll_resync;
-
- /* history data */
- struct {
- double offset;
- double delay;
- } samples[8];
- unsigned int samples_idx;
- double samples_jitter;
-
- /* last change */
- bool jumped;
- int drift_ppm;
-
- /* watch for time changes */
- sd_event_source *event_clock_watch;
- int clock_watch_fd;
-};
-
-static void manager_free(Manager *m);
-DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
-#define _cleanup_manager_free_ _cleanup_(manager_freep)
-
-static int sntp_arm_timer(Manager *m, usec_t next);
-static int sntp_clock_watch_setup(Manager *m);
-static void sntp_server_disconnect(Manager *m);
-
-static double ntp_ts_to_d(const struct ntp_ts *ts) {
- return be32toh(ts->sec) + ((double)be32toh(ts->frac) / UINT_MAX);
-}
-
-static double ts_to_d(const struct timespec *ts) {
- return ts->tv_sec + (1.0e-9 * ts->tv_nsec);
-}
-
-static double tv_to_d(const struct timeval *tv) {
- return tv->tv_sec + (1.0e-6 * tv->tv_usec);
-}
-
-static double square(double d) {
- return d * d;
-}
-
-static int sntp_send_request(Manager *m) {
- struct ntp_msg ntpmsg = {};
- struct sockaddr_in addr = {};
- ssize_t len;
- int r;
-
- /*
- * "The client initializes the NTP message header, sends the request
- * to the server, and strips the time of day from the Transmit
- * Timestamp field of the reply. For this purpose, all the NTP
- * header fields are set to 0, except the Mode, VN, and optional
- * Transmit Timestamp fields."
- */
- ntpmsg.field = NTP_FIELD(0, 4, NTP_MODE_CLIENT);
-
- /*
- * Set transmit timestamp, remember it; the server will send that back
- * as the origin timestamp and we have an indication that this is the
- * matching answer to our request.
- *
- * The actual value does not matter, We do not care about the correct
- * NTP UINT_MAX fraction; we just pass the plain nanosecond value.
- */
- clock_gettime(CLOCK_MONOTONIC, &m->trans_time_mon);
- clock_gettime(CLOCK_REALTIME, &m->trans_time);
- ntpmsg.trans_time.sec = htobe32(m->trans_time.tv_sec + OFFSET_1900_1970);
- ntpmsg.trans_time.frac = htobe32(m->trans_time.tv_nsec);
-
- addr.sin_family = AF_INET;
- addr.sin_port = htobe16(123);
- addr.sin_addr.s_addr = inet_addr(m->server);
- len = sendto(m->server_socket, &ntpmsg, sizeof(ntpmsg), MSG_DONTWAIT, &addr, sizeof(addr));
- if (len == sizeof(ntpmsg)) {
- m->pending = true;
- log_debug("Sent NTP request to: %s", m->server);
- } else
- log_debug("Sending NTP request to %s failed: %m", m->server);