SatCat5
ntp_header.h
1 // Copyright 2024-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
5 // Message headers for the Network Time Protocol (NTP / IETF RFC-5905)
6 
7 #pragma once
8 
9 #include <satcat5/io_readable.h>
10 #include <satcat5/io_writeable.h>
11 
12 namespace satcat5 {
13  namespace ntp {
20  struct Header {
21  // Message header fields (Section 7.3)
22  u8 lvm;
23  u8 stratum;
24  s8 poll;
25  s8 precision;
26  u32 rootdelay;
27  u32 rootdisp;
28  u32 refid;
29  u64 ref;
30  u64 org;
31  u64 rec;
32  u64 xmt;
33 
35  static constexpr unsigned HEADER_LEN = 48;
36 
38  static constexpr u8
39  LI_MASK = (3 << 6), // LI = Bits 7:6
40  LEAP_NONE = (0 << 6), // No leap second expected
41  LEAP_61 = (1 << 6), // Insert leap-second (Append 23:59:60)
42  LEAP_59 = (2 << 6), // Remove leap-second (Skip 23:59:59)
43  LEAP_UNK = (3 << 6); // Unknown / clock unsynchronized
44 
46  static constexpr u8
47  VN_MASK = (7 << 3), // VN = Bits 5:3
48  VERSION_3 = (3 << 3), // Version 3 (RFC-958, published 1992)
49  VERSION_4 = (4 << 3); // Version 4 (RFC-5905, published 2010)
50 
52  static constexpr u8
53  MODE_MASK = 0x7, // Mode = Bits 2:0
54  MODE_RSVD = 0, // Reserved
55  MODE_SYMM1 = 1, // Symmetric active
56  MODE_SYMM0 = 2, // Symmetric passive
57  MODE_CLIENT = 3, // Client (issues request)
58  MODE_SERVER = 4, // Server (issues reply)
59  MODE_BCAST = 5, // Broadcast
60  MODE_CTRL = 6, // NTP control message
61  MODE_PRIVAT = 7; // Reserved for private use
62 
64  static constexpr u32
65  KISS_ACST = 0x41435354u,
66  KISS_AUTH = 0x41555448u,
67  KISS_AUTO = 0x4155544Fu,
68  KISS_BCST = 0x42435354u,
69  KISS_CRYP = 0x43525950u,
70  KISS_DENY = 0x44454E59u,
71  KISS_DROP = 0x44524F50u,
72  KISS_RSTR = 0x52535452u,
73  KISS_INIT = 0x494E4954u,
74  KISS_MCST = 0x4D435354u,
75  KISS_NKEY = 0x4E4B4559u,
76  KISS_RATE = 0x52415445u,
77  KISS_RMOT = 0x524D4F54u,
78  KISS_STEP = 0x53544550u;
79 
81  static constexpr s8
82  TIME_1HOUR = 12,
83  TIME_32MIN = 11,
84  TIME_16MIN = 10,
85  TIME_8MIN = 9,
86  TIME_4MIN = 8,
87  TIME_2MIN = 7,
88  TIME_1MIN = 6,
89  TIME_32SEC = 5,
90  TIME_16SEC = 4,
91  TIME_8SEC = 3,
92  TIME_4SEC = 2,
93  TIME_2SEC = 1,
94  TIME_1SEC = 0,
95  TIME_500MSEC = -1,
96  TIME_250MSEC = -2,
97  TIME_125MSEC = -3,
98  TIME_64MSEC = -4,
99  TIME_32MSEC = -5,
100  TIME_16MSEC = -6,
101  TIME_8MSEC = -7,
102  TIME_4MSEC = -8,
103  TIME_2MSEC = -9,
104  TIME_1MSEC = -10,
105  TIME_500USEC = -11,
106  TIME_250USEC = -12,
107  TIME_125USEC = -13,
108  TIME_64USEC = -14,
109  TIME_32USEC = -15,
110  TIME_16USEC = -16,
111  TIME_8USEC = -17,
112  TIME_4USEC = -18,
113  TIME_2USEC = -19,
114  TIME_1USEC = -20,
115  TIME_500NSEC = -21,
116  TIME_250NSEC = -22,
117  TIME_125NSEC = -23,
118  TIME_64NSEC = -24,
119  TIME_32NSEC = -25,
120  TIME_16NSEC = -26,
121  TIME_8NSEC = -27,
122  TIME_4NSEC = -28,
123  TIME_2NSEC = -29,
124  TIME_1NSEC = -30;
125 
126  // Explicitly declare default constructor and assignment methods.
127  Header() = default;
128  Header(const Header& t) = default;
129  Header& operator=(const Header& t) = default;
130 
133  inline u8 li() const { return lvm & LI_MASK; }
134  inline u8 vn() const { return lvm & VN_MASK; }
135  inline u8 mode() const { return lvm & MODE_MASK; }
137 
139  void log_to(satcat5::log::LogBuffer& wr) const;
143  void write_to(satcat5::io::Writeable* wr) const;
144  };
145  }
146 }
Abstract API for reading byte-streams and packets.
Definition: io_readable.h:68
Abstract API for writing byte-streams and packets.
Definition: io_writeable.h:24
Internal buffer used by the Log class.
Definition: log.h:134
"Readable" I/O interface core definitions
"Writeable" I/O interface core definitions
Message headers for the Network Time Protocol (NTP / IETF RFC-5905).
Definition: ntp_header.h:20
u8 vn() const
Accessors for splitting LI, VN, and Mode fields.
Definition: ntp_header.h:134
u64 org
T1 (Client transmit time)
Definition: ntp_header.h:30
void write_to(satcat5::io::Writeable *wr) const
Write this header to a data sink.
Definition: ntp_header.cc:46
static constexpr u8 VN_MASK
Version number is always 4.
Definition: ntp_header.h:47
u8 mode() const
Accessors for splitting LI, VN, and Mode fields.
Definition: ntp_header.h:135
static constexpr s8 TIME_1HOUR
Named constants for polling intervals and dispersion.
Definition: ntp_header.h:82
u64 xmt
T3 (Server transmit time)
Definition: ntp_header.h:32
static constexpr unsigned HEADER_LEN
The basic header is exactly 12 words = 48 bytes long.
Definition: ntp_header.h:35
u8 li() const
Accessors for splitting LI, VN, and Mode fields.
Definition: ntp_header.h:133
static constexpr u8 LI_MASK
Leap second indictor (LI) for last minute of current day.
Definition: ntp_header.h:39
u64 ref
Time of last sync to parent.
Definition: ntp_header.h:29
u32 rootdelay
Round trip delay to grandmaster.
Definition: ntp_header.h:26
bool read_from(satcat5::io::Readable *rd)
Read this header from a data source.
Definition: ntp_header.cc:27
u32 rootdisp
Total dispersion to grandmaster.
Definition: ntp_header.h:27
static constexpr u32 KISS_ACST
Reserved RefIDs, aka "kiss codes" (Section 7.4)
Definition: ntp_header.h:65
u8 stratum
Hops to grandmaster (1-15)
Definition: ntp_header.h:23
s8 precision
Precision = 2^N seconds.
Definition: ntp_header.h:25
u64 rec
T2 (Server receive time)
Definition: ntp_header.h:31
void log_to(satcat5::log::LogBuffer &wr) const
Human-readable formatting of the header contents.
Definition: ntp_header.cc:11
u32 refid
Server-ID or KoD code.
Definition: ntp_header.h:28
static constexpr u8 MODE_MASK
Mode number indicates client or server role.
Definition: ntp_header.h:53
s8 poll
Interval = 2^N seconds.
Definition: ntp_header.h:24
u8 lvm
Combined LI + VN + Mode.
Definition: ntp_header.h:22