ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
ntp.h File Reference

The NTP packet on the wire (RFC 5905 sec 7.3, Figure 8), shared by the client and server. More...

#include <stdint.h>
#include "protocore_config.h"

Go to the source code of this file.

Macros

#define PROTOCORE_NTP_PACKET_LEN   48u
 One NTP packet on the wire is exactly 48 octets (no extension or MAC fields).
 
#define PROTOCORE_NTP_UNIX_OFFSET   2208988800u
 Seconds between the NTP prime epoch (1900-01-01) and the Unix epoch (1970-01-01).
 
#define PROTOCORE_NTP_PORT   123u
 UDP port NTP answers on (RFC 5905 sec 7.2).
 
#define PROTOCORE_NTP_OFF_LI_VN_MODE   0u
 Leap indicator, version and mode, packed into the first octet.
 
#define PROTOCORE_NTP_OFF_STRATUM   1u
 Stratum of the sender.
 
#define PROTOCORE_NTP_OFF_POLL   2u
 Poll interval, log2 seconds, signed.
 
#define PROTOCORE_NTP_OFF_PRECISION   3u
 Clock precision, log2 seconds, signed.
 
#define PROTOCORE_NTP_OFF_ROOT_DELAY   4u
 Round-trip delay to the reference clock, 16.16 seconds.
 
#define PROTOCORE_NTP_OFF_ROOT_DISP   8u
 Dispersion to the reference clock, 16.16 seconds.
 
#define PROTOCORE_NTP_OFF_REFID   12u
 Reference identifier: a kiss code at stratum 0, a source ID at stratum 1.
 
#define PROTOCORE_NTP_OFF_REF_SEC   16u
 Reference timestamp, when the sender's clock was last set.
 
#define PROTOCORE_NTP_OFF_REF_FRAC   20u
 
#define PROTOCORE_NTP_OFF_ORIGIN_SEC   24u
 Origin timestamp: the client's transmit stamp, echoed by the server.
 
#define PROTOCORE_NTP_OFF_ORIGIN_FRAC   28u
 
#define PROTOCORE_NTP_OFF_RX_SEC   32u
 Receive timestamp: when the request reached the server.
 
#define PROTOCORE_NTP_OFF_RX_FRAC   36u
 
#define PROTOCORE_NTP_OFF_TX_SEC   40u
 Transmit timestamp: when the reply left, and the field a client reads the time from.
 
#define PROTOCORE_NTP_OFF_TX_FRAC   44u
 
#define PROTOCORE_NTP_LI_VN_MODE(li, vn, mode)   ((uint8_t)(((li) << 6) | ((vn) << 3) | (mode)))
 li (2) | vn (3) | mode (3), packed into the first octet.
 
#define PROTOCORE_NTP_LI_OF(b)   ((uint8_t)((b) >> 6))
 The leap indicator carried in the first octet.
 
#define PROTOCORE_NTP_VN_OF(b)   ((uint8_t)(((b) >> 3) & 0x07u))
 The version carried in the first octet.
 
#define PROTOCORE_NTP_MODE_OF(b)   ((uint8_t)((b) & 0x07u))
 The mode carried in the first octet.
 
#define PROTOCORE_NTP_VERSION   4u
 NTP version this speaks (RFC 5905).
 
#define PROTOCORE_NTP_MODE_RESERVED   0u
 
#define PROTOCORE_NTP_MODE_SYM_ACTIVE   1u
 
#define PROTOCORE_NTP_MODE_SYM_PASSIVE   2u
 
#define PROTOCORE_NTP_MODE_CLIENT   3u
 
#define PROTOCORE_NTP_MODE_SERVER   4u
 
#define PROTOCORE_NTP_MODE_BROADCAST   5u
 
#define PROTOCORE_NTP_MODE_CONTROL   6u
 
#define PROTOCORE_NTP_MODE_PRIVATE   7u
 
#define PROTOCORE_NTP_LI_NONE   0u
 
#define PROTOCORE_NTP_LI_ADD_SEC   1u
 
#define PROTOCORE_NTP_LI_DEL_SEC   2u
 
#define PROTOCORE_NTP_LI_UNSYNC   3u
 Leap indicator 3, clock unsynchronized: RFC 4330 sec 5 discards a reply carrying it.
 
#define PROTOCORE_NTP_STRATUM_KOD   0u
 
#define PROTOCORE_NTP_STRATUM_PRIMARY   1u
 
#define PROTOCORE_NTP_STRATUM_MAX   15u
 
#define PROTOCORE_NTP_STRATUM_UNSYNC   16u
 
#define PROTOCORE_NTP_REFID_LOCL   0x4C4F434Cu
 Reference ID "LOCL" - an undisciplined local clock (RFC 5905 sec 7.3).
 
#define PROTOCORE_NTP_REFID_GPS   0x47505320u
 Reference ID "GPS " - a GPS-disciplined reference clock (use with stratum 1).
 

Detailed Description

The NTP packet on the wire (RFC 5905 sec 7.3, Figure 8), shared by the client and server.

Layout, field offsets, and the enumerated values either role tests. The client role lives in ntp_service/, the server role in ntp_server/; both read the format from here and neither states any of it a second time.

The header is 48 octets, twelve 32-bit words:

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|LI | VN  |Mode |    Stratum    |     Poll      |   Precision   |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                          Root Delay                           |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                       Root Dispersion                         |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                         Reference ID                          |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                    Reference Timestamp (64)                   |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                     Origin Timestamp (64)                     |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                     Receive Timestamp (64)                    |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                     Transmit Timestamp (64)                   |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

A timestamp is 32 bits of seconds since the prime epoch then 32 bits of fraction, so each one takes two offsets here. Every multi-octet field is big-endian.

Author
Douglas Quigg (dstroy0)
Date
2026

Definition in file ntp.h.

Macro Definition Documentation

◆ PROTOCORE_NTP_PACKET_LEN

#define PROTOCORE_NTP_PACKET_LEN   48u

One NTP packet on the wire is exactly 48 octets (no extension or MAC fields).

Definition at line 51 of file ntp.h.

◆ PROTOCORE_NTP_UNIX_OFFSET

#define PROTOCORE_NTP_UNIX_OFFSET   2208988800u

Seconds between the NTP prime epoch (1900-01-01) and the Unix epoch (1970-01-01).

Definition at line 54 of file ntp.h.

◆ PROTOCORE_NTP_PORT

#define PROTOCORE_NTP_PORT   123u

UDP port NTP answers on (RFC 5905 sec 7.2).

Definition at line 57 of file ntp.h.

◆ PROTOCORE_NTP_OFF_LI_VN_MODE

#define PROTOCORE_NTP_OFF_LI_VN_MODE   0u

Leap indicator, version and mode, packed into the first octet.

Definition at line 62 of file ntp.h.

◆ PROTOCORE_NTP_OFF_STRATUM

#define PROTOCORE_NTP_OFF_STRATUM   1u

Stratum of the sender.

Definition at line 65 of file ntp.h.

◆ PROTOCORE_NTP_OFF_POLL

#define PROTOCORE_NTP_OFF_POLL   2u

Poll interval, log2 seconds, signed.

Definition at line 68 of file ntp.h.

◆ PROTOCORE_NTP_OFF_PRECISION

#define PROTOCORE_NTP_OFF_PRECISION   3u

Clock precision, log2 seconds, signed.

Definition at line 71 of file ntp.h.

◆ PROTOCORE_NTP_OFF_ROOT_DELAY

#define PROTOCORE_NTP_OFF_ROOT_DELAY   4u

Round-trip delay to the reference clock, 16.16 seconds.

Definition at line 74 of file ntp.h.

◆ PROTOCORE_NTP_OFF_ROOT_DISP

#define PROTOCORE_NTP_OFF_ROOT_DISP   8u

Dispersion to the reference clock, 16.16 seconds.

Definition at line 77 of file ntp.h.

◆ PROTOCORE_NTP_OFF_REFID

#define PROTOCORE_NTP_OFF_REFID   12u

Reference identifier: a kiss code at stratum 0, a source ID at stratum 1.

Definition at line 80 of file ntp.h.

◆ PROTOCORE_NTP_OFF_REF_SEC

#define PROTOCORE_NTP_OFF_REF_SEC   16u

Reference timestamp, when the sender's clock was last set.

Definition at line 83 of file ntp.h.

◆ PROTOCORE_NTP_OFF_REF_FRAC

#define PROTOCORE_NTP_OFF_REF_FRAC   20u

Definition at line 84 of file ntp.h.

◆ PROTOCORE_NTP_OFF_ORIGIN_SEC

#define PROTOCORE_NTP_OFF_ORIGIN_SEC   24u

Origin timestamp: the client's transmit stamp, echoed by the server.

Definition at line 87 of file ntp.h.

◆ PROTOCORE_NTP_OFF_ORIGIN_FRAC

#define PROTOCORE_NTP_OFF_ORIGIN_FRAC   28u

Definition at line 88 of file ntp.h.

◆ PROTOCORE_NTP_OFF_RX_SEC

#define PROTOCORE_NTP_OFF_RX_SEC   32u

Receive timestamp: when the request reached the server.

Definition at line 91 of file ntp.h.

◆ PROTOCORE_NTP_OFF_RX_FRAC

#define PROTOCORE_NTP_OFF_RX_FRAC   36u

Definition at line 92 of file ntp.h.

◆ PROTOCORE_NTP_OFF_TX_SEC

#define PROTOCORE_NTP_OFF_TX_SEC   40u

Transmit timestamp: when the reply left, and the field a client reads the time from.

Definition at line 95 of file ntp.h.

◆ PROTOCORE_NTP_OFF_TX_FRAC

#define PROTOCORE_NTP_OFF_TX_FRAC   44u

Definition at line 96 of file ntp.h.

◆ PROTOCORE_NTP_LI_VN_MODE

#define PROTOCORE_NTP_LI_VN_MODE (   li,
  vn,
  mode 
)    ((uint8_t)(((li) << 6) | ((vn) << 3) | (mode)))

li (2) | vn (3) | mode (3), packed into the first octet.

Definition at line 101 of file ntp.h.

◆ PROTOCORE_NTP_LI_OF

#define PROTOCORE_NTP_LI_OF (   b)    ((uint8_t)((b) >> 6))

The leap indicator carried in the first octet.

Definition at line 104 of file ntp.h.

◆ PROTOCORE_NTP_VN_OF

#define PROTOCORE_NTP_VN_OF (   b)    ((uint8_t)(((b) >> 3) & 0x07u))

The version carried in the first octet.

Definition at line 107 of file ntp.h.

◆ PROTOCORE_NTP_MODE_OF

#define PROTOCORE_NTP_MODE_OF (   b)    ((uint8_t)((b) & 0x07u))

The mode carried in the first octet.

Definition at line 110 of file ntp.h.

◆ PROTOCORE_NTP_VERSION

#define PROTOCORE_NTP_VERSION   4u

NTP version this speaks (RFC 5905).

Definition at line 113 of file ntp.h.

◆ PROTOCORE_NTP_MODE_RESERVED

#define PROTOCORE_NTP_MODE_RESERVED   0u

Definition at line 117 of file ntp.h.

◆ PROTOCORE_NTP_MODE_SYM_ACTIVE

#define PROTOCORE_NTP_MODE_SYM_ACTIVE   1u

Definition at line 118 of file ntp.h.

◆ PROTOCORE_NTP_MODE_SYM_PASSIVE

#define PROTOCORE_NTP_MODE_SYM_PASSIVE   2u

Definition at line 119 of file ntp.h.

◆ PROTOCORE_NTP_MODE_CLIENT

#define PROTOCORE_NTP_MODE_CLIENT   3u

Definition at line 120 of file ntp.h.

◆ PROTOCORE_NTP_MODE_SERVER

#define PROTOCORE_NTP_MODE_SERVER   4u

Definition at line 121 of file ntp.h.

◆ PROTOCORE_NTP_MODE_BROADCAST

#define PROTOCORE_NTP_MODE_BROADCAST   5u

Definition at line 122 of file ntp.h.

◆ PROTOCORE_NTP_MODE_CONTROL

#define PROTOCORE_NTP_MODE_CONTROL   6u

Definition at line 123 of file ntp.h.

◆ PROTOCORE_NTP_MODE_PRIVATE

#define PROTOCORE_NTP_MODE_PRIVATE   7u

Definition at line 124 of file ntp.h.

◆ PROTOCORE_NTP_LI_NONE

#define PROTOCORE_NTP_LI_NONE   0u

Definition at line 127 of file ntp.h.

◆ PROTOCORE_NTP_LI_ADD_SEC

#define PROTOCORE_NTP_LI_ADD_SEC   1u

Definition at line 128 of file ntp.h.

◆ PROTOCORE_NTP_LI_DEL_SEC

#define PROTOCORE_NTP_LI_DEL_SEC   2u

Definition at line 129 of file ntp.h.

◆ PROTOCORE_NTP_LI_UNSYNC

#define PROTOCORE_NTP_LI_UNSYNC   3u

Leap indicator 3, clock unsynchronized: RFC 4330 sec 5 discards a reply carrying it.

Definition at line 132 of file ntp.h.

◆ PROTOCORE_NTP_STRATUM_KOD

#define PROTOCORE_NTP_STRATUM_KOD   0u

Definition at line 136 of file ntp.h.

◆ PROTOCORE_NTP_STRATUM_PRIMARY

#define PROTOCORE_NTP_STRATUM_PRIMARY   1u

Definition at line 137 of file ntp.h.

◆ PROTOCORE_NTP_STRATUM_MAX

#define PROTOCORE_NTP_STRATUM_MAX   15u

Definition at line 138 of file ntp.h.

◆ PROTOCORE_NTP_STRATUM_UNSYNC

#define PROTOCORE_NTP_STRATUM_UNSYNC   16u

Definition at line 139 of file ntp.h.

◆ PROTOCORE_NTP_REFID_LOCL

#define PROTOCORE_NTP_REFID_LOCL   0x4C4F434Cu

Reference ID "LOCL" - an undisciplined local clock (RFC 5905 sec 7.3).

Definition at line 144 of file ntp.h.

◆ PROTOCORE_NTP_REFID_GPS

#define PROTOCORE_NTP_REFID_GPS   0x47505320u

Reference ID "GPS " - a GPS-disciplined reference clock (use with stratum 1).

Definition at line 147 of file ntp.h.