ntp_packet.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 Luminița Lăzărescu <cluminita.lazarescu@gmail.com>
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
22 #ifndef NET_NTP_PACKET_H
23 #define NET_NTP_PACKET_H
24 
25 #include <stdint.h>
26 #include "byteorder.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
39 #define NTP_PACKET_LI_POS (6U)
40 #define NTP_PACKET_LI_MASK (0xc0)
41 #define NTP_PACKET_VN_POS (3U)
42 #define NTP_PACKET_VN_MASK (0x38)
43 #define NTP_PACKET_MODE_MASK (0x07)
46 #define NTP_VERSION (4U)
47 #define NTP_PORT (123U)
53 #define NTP_UNIX_OFFSET (2208988800)
54 
58 typedef enum {
66 } ntp_mode_t;
67 
73 typedef struct __attribute__((packed)) {
77 
83 typedef struct __attribute__((packed)) {
84  uint8_t li_vn_mode;
85  uint8_t stratum;
86  uint8_t poll;
87  uint8_t precision;
95 } ntp_packet_t;
96 
103 static inline void ntp_packet_set_li(ntp_packet_t *packet, uint8_t li)
104 {
105  packet->li_vn_mode &= ~NTP_PACKET_LI_MASK;
106  packet->li_vn_mode |= li << NTP_PACKET_LI_POS;
107 }
108 
114 static inline void ntp_packet_set_vn(ntp_packet_t *packet)
115 {
116  packet->li_vn_mode &= ~NTP_PACKET_VN_MASK;
118 }
119 
126 static inline void ntp_packet_set_mode(ntp_packet_t *packet, ntp_mode_t mode)
127 {
128  packet->li_vn_mode &= ~NTP_PACKET_MODE_MASK;
129  packet->li_vn_mode |= mode & NTP_PACKET_MODE_MASK;
130 }
131 
139 static inline uint8_t ntp_packet_get_li(ntp_packet_t *packet)
140 {
141  return (packet->li_vn_mode & NTP_PACKET_LI_MASK) >> NTP_PACKET_LI_POS;
142 }
143 
151 static inline uint8_t ntp_packet_get_vn(ntp_packet_t *packet)
152 {
153  return (packet->li_vn_mode & NTP_PACKET_VN_MASK) >> NTP_PACKET_VN_POS;
154 }
155 
164 {
165  return (packet->li_vn_mode & NTP_PACKET_MODE_MASK);
166 }
167 
168 #ifdef __cplusplus
169 }
170 #endif
171 
172 #endif /* NET_NTP_PACKET_H */
173 
be_uint32_t
A 32 bit integer in big endian aka network byte order.
Definition: byteorder.h:87
ntp_packet_get_vn
static uint8_t ntp_packet_get_vn(ntp_packet_t *packet)
Get version from a NTP packet.
Definition: ntp_packet.h:151
ntp_packet_set_mode
static void ntp_packet_set_mode(ntp_packet_t *packet, ntp_mode_t mode)
Set mode in a NTP packet.
Definition: ntp_packet.h:126
ntp_packet_t::li_vn_mode
uint8_t li_vn_mode
leap indicator, version and mode
Definition: ntp_packet.h:84
byteorder.h
Functions to work with different byte orders.
ntp_packet_t::root_dispersion
network_uint32_t root_dispersion
root dispersion in NTP short format
Definition: ntp_packet.h:89
NTP_MODE_RESERVED
@ NTP_MODE_RESERVED
reserved
Definition: ntp_packet.h:59
NTP_MODE_SERVER
@ NTP_MODE_SERVER
server
Definition: ntp_packet.h:63
ntp_packet_t::poll
uint8_t poll
poll in log2 seconds
Definition: ntp_packet.h:86
NTP_PACKET_LI_POS
#define NTP_PACKET_LI_POS
Bit positions and masks for ntp_packet_t::li_vn_mode.
Definition: ntp_packet.h:39
ntp_mode_t
ntp_mode_t
NTP modes.
Definition: ntp_packet.h:58
ntp_packet_t::root_delay
network_uint32_t root_delay
root delay in NTP short format
Definition: ntp_packet.h:88
ntp_packet_set_vn
static void ntp_packet_set_vn(ntp_packet_t *packet)
Set version in a NTP packet.
Definition: ntp_packet.h:114
ntp_packet_get_li
static uint8_t ntp_packet_get_li(ntp_packet_t *packet)
Get leap indicator from a NTP packet.
Definition: ntp_packet.h:139
ntp_packet_set_li
static void ntp_packet_set_li(ntp_packet_t *packet, uint8_t li)
Set leap indicator in a NTP packet.
Definition: ntp_packet.h:103
NTP_MODE_SYM_ACTIVE
@ NTP_MODE_SYM_ACTIVE
symmetric active
Definition: ntp_packet.h:60
ntp_timestamp_t
NTP timestamp.
Definition: ntp_packet.h:73
ntp_timestamp_t::fraction
network_uint32_t fraction
fraction of seconds in 232 picoseconds
Definition: ntp_packet.h:75
ntp_packet_t::reference_id
network_uint32_t reference_id
reference ID
Definition: ntp_packet.h:90
NTP_PACKET_VN_MASK
#define NTP_PACKET_VN_MASK
version mask
Definition: ntp_packet.h:42
ntp_packet_t::precision
uint8_t precision
precision in log2 seconds
Definition: ntp_packet.h:87
NTP_MODE_SYM_PASSIVE
@ NTP_MODE_SYM_PASSIVE
symmetric passive
Definition: ntp_packet.h:61
NTP_PACKET_MODE_MASK
#define NTP_PACKET_MODE_MASK
mode mask
Definition: ntp_packet.h:43
NTP_MODE_PRIV
@ NTP_MODE_PRIV
reserved for private use
Definition: ntp_packet.h:65
NTP_MODE_CLIENT
@ NTP_MODE_CLIENT
client
Definition: ntp_packet.h:62
ntp_timestamp_t::seconds
network_uint32_t seconds
seconds since 1 January 1900 00:00 UTC
Definition: ntp_packet.h:74
ntp_packet_t::reference
ntp_timestamp_t reference
reference timestamp
Definition: ntp_packet.h:91
NTP_VERSION
#define NTP_VERSION
NTP version.
Definition: ntp_packet.h:46
ntp_packet_get_mode
static ntp_mode_t ntp_packet_get_mode(ntp_packet_t *packet)
Get mode from a NTP packet.
Definition: ntp_packet.h:163
ntp_packet_t::origin
ntp_timestamp_t origin
origin timesptamp
Definition: ntp_packet.h:92
NTP_PACKET_LI_MASK
#define NTP_PACKET_LI_MASK
leap indicator mask
Definition: ntp_packet.h:40
NTP_MODE_BROADCAST
@ NTP_MODE_BROADCAST
broadcast
Definition: ntp_packet.h:64
ntp_packet_t::stratum
uint8_t stratum
stratum
Definition: ntp_packet.h:85
ntp_packet_t
NTP packet.
Definition: ntp_packet.h:83
ntp_packet_t::transmit
ntp_timestamp_t transmit
transmit timestamp
Definition: ntp_packet.h:94
ntp_packet_t::receive
ntp_timestamp_t receive
receive timestamp
Definition: ntp_packet.h:93
NTP_PACKET_VN_POS
#define NTP_PACKET_VN_POS
version position
Definition: ntp_packet.h:41