ntp_common.h
Go to the documentation of this file.
1 /**
2  * @file ntp_common.h
3  * @brief Definitions common to NTP client and server
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2025 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.5.4
29  **/
30 
31 #ifndef _NTP_COMMON_H
32 #define _NTP_COMMON_H
33 
34 //Dependencies
35 #include "core/net.h"
36 
37 //NTP port number
38 #define NTP_PORT 123
39 //Maximum size of NTP messages
40 #define NTP_MAX_MSG_SIZE 68
41 
42 //Difference between NTP and Unix time scales
43 #define NTP_UNIX_EPOCH 2208988800U
44 
45 //NTP area 0 (1 Jan 1900 00:00:00)
46 #define NTP_AREA_0 0ULL
47 //NTP area 1 (7 Feb 2036 06:28:16)
48 #define NTP_AREA_1 4294967296ULL
49 
50 //Kiss code definition
51 #define NTP_KISS_CODE(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
52 
53 //C++ guard
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57 
58 
59 /**
60  * @brief Leap indicator
61  **/
62 
63 typedef enum
64 {
70 
71 
72 /**
73  * @brief NTP version numbers
74  **/
75 
76 typedef enum
77 {
81  NTP_VERSION_4 = 4
83 
84 
85 /**
86  * @brief Protocol modes
87  **/
88 
89 typedef enum
90 {
97 
98 
99 /**
100  * @brief Stratum
101  **/
102 
103 typedef enum
104 {
121 } NtpStratum;
122 
123 
124 /**
125  * @brief Kiss codes
126  *
127  * The kiss codes can provide useful information for an intelligent client.
128  * These codes are encoded in four-character ASCII strings left justified
129  * and zero filled
130  *
131  **/
132 
133 typedef enum
134 {
135  NTP_KISS_CODE_ACST = NTP_KISS_CODE('A', 'C', 'S', 'T'), ///<The association belongs to a anycast server
136  NTP_KISS_CODE_AUTH = NTP_KISS_CODE('A', 'U', 'T', 'H'), ///<Server authentication failed
137  NTP_KISS_CODE_AUTO = NTP_KISS_CODE('A', 'U', 'T', 'O'), ///<Autokey sequence failed
138  NTP_KISS_CODE_BCST = NTP_KISS_CODE('B', 'C', 'S', 'T'), ///<The association belongs to a broadcast server
139  NTP_KISS_CODE_CRYP = NTP_KISS_CODE('C', 'R', 'Y', 'P'), ///<Cryptographic authentication or identification failed
140  NTP_KISS_CODE_DENY = NTP_KISS_CODE('D', 'E', 'N', 'Y'), ///<Access denied by remote server
141  NTP_KISS_CODE_DROP = NTP_KISS_CODE('D', 'R', 'O', 'P'), ///<Lost peer in symmetric mode
142  NTP_KISS_CODE_RSTR = NTP_KISS_CODE('R', 'S', 'T', 'R'), ///<Access denied due to local policy
143  NTP_KISS_CODE_INIT = NTP_KISS_CODE('I', 'N', 'I', 'T'), ///<The association has not yet synchronized for the first time
144  NTP_KISS_CODE_MCST = NTP_KISS_CODE('M', 'C', 'S', 'T'), ///<The association belongs to a manycast server
145  NTP_KISS_CODE_NKEY = NTP_KISS_CODE('N', 'K', 'E', 'Y'), ///<No key found
146  NTP_KISS_CODE_RATE = NTP_KISS_CODE('R', 'A', 'T', 'E'), ///<Rate exceeded
147  NTP_KISS_CODE_RMOT = NTP_KISS_CODE('R', 'M', 'O', 'T'), ///<Somebody is tinkering with the association from a remote host running ntpdc
148  NTP_KISS_CODE_STEP = NTP_KISS_CODE('S', 'T', 'E', 'P'), ///<A step change in system time has occurred
149  NTP_KISS_CODE_NTSN = NTP_KISS_CODE('N', 'T', 'S', 'N') ///<NTS negative-acknowledgment (NAK)
151 
152 
153 /**
154  * @brief NTP extensions field types
155  **/
156 
157 typedef enum
158 {
159  NTP_EXTENSION_TYPE_NO_OPERATION_REQ = 0x0002, ///<No-Operation Request
160  NTP_EXTENSION_TYPE_UNIQUE_ID = 0x0104, ///<Unique Identifier
161  NTP_EXTENSION_TYPE_NTS_COOKIE = 0x0204, ///<NTS Cookie
162  NTP_EXTENSION_TYPE_NTS_COOKIE_PLACEHOLDER = 0x0304, ///<NTS Cookie Placeholder
163  NTP_EXTENSION_TYPE_NTS_AEAD = 0x0404, ///<NTS Authenticator and Encrypted Extension Fields
164  NTP_EXTENSION_TYPE_NO_OPERATION_RESP = 0x8002, ///<No-Operation Response
165  NTP_EXTENSION_TYPE_NO_OPERATION_ERROR_RESP = 0xC002 ///<No-Operation Error Response
167 
168 
169 //CC-RX, CodeWarrior or Win32 compiler?
170 #if defined(__CCRX__)
171  #pragma pack
172 #elif defined(__CWCC__) || defined(_WIN32)
173  #pragma pack(push, 1)
174 #endif
175 
176 
177 /**
178  * @brief NTP timestamp representation
179  **/
180 
182 {
183  uint32_t seconds;
184  uint32_t fraction;
186 
187 
188 /**
189  * @brief NTP packet header
190  **/
191 
192 typedef __packed_struct
193 {
194 #if defined(_CPU_BIG_ENDIAN) && !defined(__ICCRX__)
195  uint8_t li : 2; //0
196  uint8_t vn : 3;
197  uint8_t mode : 3;
198 #else
199  uint8_t mode : 3; //0
200  uint8_t vn : 3;
201  uint8_t li : 2;
202 #endif
203  uint8_t stratum; //1
204  uint8_t poll; //2
205  int8_t precision; //3
206  uint32_t rootDelay; //4-7
207  uint32_t rootDispersion; //8-11
208  uint32_t referenceId; //12-15
213  uint8_t extensions[]; //48
215 
216 
217 /**
218  * @brief NTP extension field
219  **/
220 
221 typedef __packed_struct
222 {
223  uint16_t fieldType; //0-1
224  uint16_t length; //2-3
225  uint8_t value[]; //4
227 
228 
229 /**
230  * @brief NTS Authenticator and Encrypted Extension Fields extension
231  **/
232 
233 typedef __packed_struct
234 {
235  uint16_t fieldType; //0-1
236  uint16_t length; //2-3
237  uint16_t nonceLength; //4-5
238  uint16_t ciphertextLength; //6-7
239  uint8_t nonce[]; //8
241 
242 
243 //CC-RX, CodeWarrior or Win32 compiler?
244 #if defined(__CCRX__)
245  #pragma unpack
246 #elif defined(__CWCC__) || defined(_WIN32)
247  #pragma pack(pop)
248 #endif
249 
250 //NTP related functions
251 const NtpExtension *ntpGetExtension(const uint8_t *extensions, size_t length,
252  uint16_t type, uint_t index);
253 
254 //C++ guard
255 #ifdef __cplusplus
256 }
257 #endif
258 
259 #endif
@ NTP_KISS_CODE_CRYP
Cryptographic authentication or identification failed.
Definition: ntp_common.h:139
uint8_t vn
Definition: ntp_common.h:200
@ NTP_STRATUM_SECONDARY_2
Definition: ntp_common.h:107
uint16_t nonceLength
Definition: ntp_common.h:237
@ NTP_EXTENSION_TYPE_NTS_COOKIE
NTS Cookie.
Definition: ntp_common.h:161
uint8_t extensions[]
Definition: ntp_common.h:213
@ NTP_KISS_CODE_AUTH
Server authentication failed.
Definition: ntp_common.h:136
@ NTP_LI_NO_WARNING
Definition: ntp_common.h:65
NtpTimestamp
Definition: ntp_common.h:185
NtpTimestamp receiveTimestamp
Definition: ntp_common.h:211
@ NTP_VERSION_2
Definition: ntp_common.h:79
@ NTP_STRATUM_SECONDARY_13
Definition: ntp_common.h:118
@ NTP_MODE_SERVER
Definition: ntp_common.h:94
uint8_t type
Definition: coap_common.h:176
NtpTimestamp transmitTimestamp
Definition: ntp_common.h:212
@ NTP_STRATUM_SECONDARY_6
Definition: ntp_common.h:111
@ NTP_STRATUM_SECONDARY_7
Definition: ntp_common.h:112
#define NTP_KISS_CODE(a, b, c, d)
Definition: ntp_common.h:51
NtpNtsAeadExtension
Definition: ntp_common.h:240
@ NTP_STRATUM_SECONDARY_8
Definition: ntp_common.h:113
uint32_t referenceId
Definition: ntp_common.h:208
NtpTimestamp referenceTimestamp
Definition: ntp_common.h:209
uint16_t ciphertextLength
Definition: ntp_common.h:238
@ NTP_EXTENSION_TYPE_NO_OPERATION_ERROR_RESP
No-Operation Error Response.
Definition: ntp_common.h:165
@ NTP_MODE_BROADCAST
Definition: ntp_common.h:95
uint8_t stratum
Definition: ntp_common.h:203
@ NTP_KISS_CODE_RSTR
Access denied due to local policy.
Definition: ntp_common.h:142
@ NTP_STRATUM_SECONDARY_4
Definition: ntp_common.h:109
@ NTP_STRATUM_SECONDARY_9
Definition: ntp_common.h:114
@ NTP_EXTENSION_TYPE_NO_OPERATION_REQ
No-Operation Request.
Definition: ntp_common.h:159
@ NTP_MODE_SYMMETRIC_PASSIVE
Definition: ntp_common.h:92
@ NTP_KISS_CODE_MCST
The association belongs to a manycast server.
Definition: ntp_common.h:144
@ NTP_STRATUM_SECONDARY_12
Definition: ntp_common.h:117
typedef __packed_struct
NTP timestamp representation.
Definition: ntp_common.h:182
@ NTP_STRATUM_SECONDARY_10
Definition: ntp_common.h:115
@ NTP_EXTENSION_TYPE_UNIQUE_ID
Unique Identifier.
Definition: ntp_common.h:160
@ NTP_STRATUM_SECONDARY_15
Definition: ntp_common.h:120
@ NTP_KISS_CODE_RATE
Rate exceeded.
Definition: ntp_common.h:146
uint8_t poll
Definition: ntp_common.h:204
@ NTP_VERSION_3
Definition: ntp_common.h:80
const NtpExtension * ntpGetExtension(const uint8_t *extensions, size_t length, uint16_t type, uint_t index)
Search a NTP packet for a given extension.
Definition: ntp_common.c:53
@ NTP_LI_LAST_MIN_HAS_61_SECS
Definition: ntp_common.h:66
@ NTP_LI_ALARM_CONDITION
Definition: ntp_common.h:68
@ NTP_KISS_CODE_ACST
The association belongs to a anycast server.
Definition: ntp_common.h:135
@ NTP_LI_LAST_MIN_HAS_59_SECS
Definition: ntp_common.h:67
NtpExtensionType
NTP extensions field types.
Definition: ntp_common.h:158
NtpKissCode
Kiss codes.
Definition: ntp_common.h:134
@ NTP_STRATUM_KISS_OF_DEATH
Definition: ntp_common.h:105
@ NTP_MODE_CLIENT
Definition: ntp_common.h:93
@ NTP_STRATUM_SECONDARY_14
Definition: ntp_common.h:119
@ NTP_KISS_CODE_AUTO
Autokey sequence failed.
Definition: ntp_common.h:137
NtpHeader
Definition: ntp_common.h:214
NtpTimestamp originateTimestamp
Definition: ntp_common.h:210
@ NTP_KISS_CODE_DROP
Lost peer in symmetric mode.
Definition: ntp_common.h:141
@ NTP_EXTENSION_TYPE_NO_OPERATION_RESP
No-Operation Response.
Definition: ntp_common.h:164
NtpLeapIndicator
Leap indicator.
Definition: ntp_common.h:64
@ NTP_STRATUM_PRIMARY
Definition: ntp_common.h:106
uint8_t value[]
Definition: ntp_common.h:225
@ NTP_KISS_CODE_DENY
Access denied by remote server.
Definition: ntp_common.h:140
@ NTP_VERSION_1
Definition: ntp_common.h:78
@ NTP_KISS_CODE_BCST
The association belongs to a broadcast server.
Definition: ntp_common.h:138
uint8_t li
Definition: ntp_common.h:201
@ NTP_STRATUM_SECONDARY_11
Definition: ntp_common.h:116
@ NTP_STRATUM_SECONDARY_5
Definition: ntp_common.h:110
NtpStratum
Stratum.
Definition: ntp_common.h:104
NtpMode
Protocol modes.
Definition: ntp_common.h:90
@ NTP_EXTENSION_TYPE_NTS_COOKIE_PLACEHOLDER
NTS Cookie Placeholder.
Definition: ntp_common.h:162
NtpVersion
NTP version numbers.
Definition: ntp_common.h:77
uint32_t rootDispersion
Definition: ntp_common.h:207
@ NTP_STRATUM_SECONDARY_3
Definition: ntp_common.h:108
uint16_t length
Definition: ntp_common.h:224
@ NTP_KISS_CODE_NTSN
NTS negative-acknowledgment (NAK)
Definition: ntp_common.h:149
@ NTP_MODE_SYMMETRIC_ACTIVE
Definition: ntp_common.h:91
NtpExtension
Definition: ntp_common.h:226
int8_t precision
Definition: ntp_common.h:205
@ NTP_KISS_CODE_RMOT
Somebody is tinkering with the association from a remote host running ntpdc.
Definition: ntp_common.h:147
unsigned int uint_t
Definition: compiler_port.h:57
TCP/IP stack core.
@ NTP_KISS_CODE_INIT
The association has not yet synchronized for the first time.
Definition: ntp_common.h:143
@ NTP_KISS_CODE_STEP
A step change in system time has occurred.
Definition: ntp_common.h:148
@ NTP_VERSION_4
Definition: ntp_common.h:81
uint8_t nonce[]
Definition: ntp_common.h:239
@ NTP_KISS_CODE_NKEY
No key found.
Definition: ntp_common.h:145
uint32_t fraction
Definition: ntp_common.h:184
@ NTP_EXTENSION_TYPE_NTS_AEAD
NTS Authenticator and Encrypted Extension Fields.
Definition: ntp_common.h:163
uint32_t rootDelay
Definition: ntp_common.h:206