1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include "sparse-endian.h"
5 
6 /* NTP protocol, packet header */
7 #define NTP_LEAP_PLUSSEC                1
8 #define NTP_LEAP_MINUSSEC               2
9 #define NTP_LEAP_NOTINSYNC              3
10 #define NTP_MODE_CLIENT                 3
11 #define NTP_MODE_SERVER                 4
12 #define NTP_FIELD_LEAP(f)               (((f) >> 6) & 3)
13 #define NTP_FIELD_VERSION(f)            (((f) >> 3) & 7)
14 #define NTP_FIELD_MODE(f)               ((f) & 7)
15 #define NTP_FIELD(l, v, m)              (((l) << 6) | ((v) << 3) | (m))
16 
17 /*
18  * "NTP timestamps are represented as a 64-bit unsigned fixed-point number,
19  * in seconds relative to 0h on 1 January 1900."
20  */
21 #define OFFSET_1900_1970        UINT64_C(2208988800)
22 
23 struct ntp_ts {
24         be32_t sec;
25         be32_t frac;
26 } _packed_;
27 
28 struct ntp_ts_short {
29         be16_t sec;
30         be16_t frac;
31 } _packed_;
32 
33 struct ntp_msg {
34         uint8_t field;
35         uint8_t stratum;
36         int8_t poll;
37         int8_t precision;
38         struct ntp_ts_short root_delay;
39         struct ntp_ts_short root_dispersion;
40         char refid[4];
41         struct ntp_ts reference_time;
42         struct ntp_ts origin_time;
43         struct ntp_ts recv_time;
44         struct ntp_ts trans_time;
45 } _packed_;
46