ntp_packet.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2016 Luminița Lăzărescu <cluminita.lazarescu@gmail.com>
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
23 #include <stdint.h>
24 #include "byteorder.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
37 #define NTP_PACKET_LI_POS (6U)
38 #define NTP_PACKET_LI_MASK (0xc0)
39 #define NTP_PACKET_VN_POS (3U)
40 #define NTP_PACKET_VN_MASK (0x38)
41 #define NTP_PACKET_MODE_MASK (0x07)
44 #define NTP_VERSION (4U)
45 #define NTP_PORT (123U)
51 #define NTP_UNIX_OFFSET (2208988800)
52 
56 typedef enum {
65 
71 typedef struct __attribute__((packed)) {
75 
81 typedef struct __attribute__((packed)) {
82  uint8_t li_vn_mode;
83  uint8_t stratum;
84  uint8_t poll;
85  uint8_t precision;
93 } ntp_packet_t;
94 
101 static inline void ntp_packet_set_li(ntp_packet_t *packet, uint8_t li)
102 {
103  packet->li_vn_mode &= ~NTP_PACKET_LI_MASK;
104  packet->li_vn_mode |= li << NTP_PACKET_LI_POS;
105 }
106 
112 static inline void ntp_packet_set_vn(ntp_packet_t *packet)
113 {
114  packet->li_vn_mode &= ~NTP_PACKET_VN_MASK;
116 }
117 
124 static inline void ntp_packet_set_mode(ntp_packet_t *packet, ntp_mode_t mode)
125 {
126  packet->li_vn_mode &= ~NTP_PACKET_MODE_MASK;
127  packet->li_vn_mode |= mode & NTP_PACKET_MODE_MASK;
128 }
129 
137 static inline uint8_t ntp_packet_get_li(ntp_packet_t *packet)
138 {
139  return (packet->li_vn_mode & NTP_PACKET_LI_MASK) >> NTP_PACKET_LI_POS;
140 }
141 
149 static inline uint8_t ntp_packet_get_vn(ntp_packet_t *packet)
150 {
151  return (packet->li_vn_mode & NTP_PACKET_VN_MASK) >> NTP_PACKET_VN_POS;
152 }
153 
162 {
163  return (packet->li_vn_mode & NTP_PACKET_MODE_MASK);
164 }
165 
166 #ifdef __cplusplus
167 }
168 #endif
169 
Functions to work with different byte orders.
#define NTP_VERSION
NTP version.
Definition: ntp_packet.h:44
static uint8_t ntp_packet_get_li(ntp_packet_t *packet)
Get leap indicator from a NTP packet.
Definition: ntp_packet.h:137
ntp_mode_t
NTP modes.
Definition: ntp_packet.h:56
static void ntp_packet_set_mode(ntp_packet_t *packet, ntp_mode_t mode)
Set mode in a NTP packet.
Definition: ntp_packet.h:124
static void ntp_packet_set_li(ntp_packet_t *packet, uint8_t li)
Set leap indicator in a NTP packet.
Definition: ntp_packet.h:101
#define NTP_PACKET_VN_POS
version position
Definition: ntp_packet.h:39
static ntp_mode_t ntp_packet_get_mode(ntp_packet_t *packet)
Get mode from a NTP packet.
Definition: ntp_packet.h:161
#define NTP_PACKET_LI_POS
Bit positions and masks for ntp_packet_t::li_vn_mode.
Definition: ntp_packet.h:37
#define NTP_PACKET_VN_MASK
version mask
Definition: ntp_packet.h:40
static void ntp_packet_set_vn(ntp_packet_t *packet)
Set version in a NTP packet.
Definition: ntp_packet.h:112
#define NTP_PACKET_MODE_MASK
mode mask
Definition: ntp_packet.h:41
#define NTP_PACKET_LI_MASK
leap indicator mask
Definition: ntp_packet.h:38
static uint8_t ntp_packet_get_vn(ntp_packet_t *packet)
Get version from a NTP packet.
Definition: ntp_packet.h:149
@ NTP_MODE_CLIENT
client
Definition: ntp_packet.h:60
@ NTP_MODE_RESERVED
reserved
Definition: ntp_packet.h:57
@ NTP_MODE_SERVER
server
Definition: ntp_packet.h:61
@ NTP_MODE_SYM_ACTIVE
symmetric active
Definition: ntp_packet.h:58
@ NTP_MODE_SYM_PASSIVE
symmetric passive
Definition: ntp_packet.h:59
@ NTP_MODE_PRIV
reserved for private use
Definition: ntp_packet.h:63
@ NTP_MODE_BROADCAST
broadcast
Definition: ntp_packet.h:62
NTP packet.
Definition: ntp_packet.h:81
ntp_timestamp_t receive
receive timestamp
Definition: ntp_packet.h:91
uint8_t li_vn_mode
leap indicator, version and mode
Definition: ntp_packet.h:82
ntp_timestamp_t reference
reference timestamp
Definition: ntp_packet.h:89
uint8_t precision
precision in log2 seconds
Definition: ntp_packet.h:85
uint8_t poll
poll in log2 seconds
Definition: ntp_packet.h:84
uint8_t stratum
stratum
Definition: ntp_packet.h:83
ntp_timestamp_t transmit
transmit timestamp
Definition: ntp_packet.h:92
network_uint32_t root_dispersion
root dispersion in NTP short format
Definition: ntp_packet.h:87
ntp_timestamp_t origin
origin timesptamp
Definition: ntp_packet.h:90
network_uint32_t reference_id
reference ID
Definition: ntp_packet.h:88
network_uint32_t root_delay
root delay in NTP short format
Definition: ntp_packet.h:86
NTP timestamp.
Definition: ntp_packet.h:71
network_uint32_t seconds
seconds since 1 January 1900 00:00 UTC
Definition: ntp_packet.h:72
network_uint32_t fraction
fraction of seconds in 232 picoseconds
Definition: ntp_packet.h:73
A 32 bit integer in big endian aka network byte order.
Definition: byteorder.h:80