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-2024 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.4.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 //Difference between NTP and Unix time scales
42 #define NTP_UNIX_EPOCH 2208988800U
43 
44 //Kiss code definition
45 #define NTP_KISS_CODE(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
46 
47 //C++ guard
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
52 
53 /**
54  * @brief Leap indicator
55  **/
56 
57 typedef enum
58 {
64 
65 
66 /**
67  * @brief NTP version numbers
68  **/
69 
70 typedef enum
71 {
75  NTP_VERSION_4 = 4
77 
78 
79 /**
80  * @brief Protocol modes
81  **/
82 
83 typedef enum
84 {
91 
92 
93 /**
94  * @brief Stratum
95  **/
96 
97 typedef enum
98 {
115 } NtpStratum;
116 
117 
118 /**
119  * @brief Kiss codes
120  *
121  * The kiss codes can provide useful information for an intelligent client.
122  * These codes are encoded in four-character ASCII strings left justified
123  * and zero filled
124  *
125  **/
126 
127 typedef enum
128 {
129  NTP_KISS_CODE_ACST = NTP_KISS_CODE('A', 'C', 'S', 'T'), ///<The association belongs to a anycast server
130  NTP_KISS_CODE_AUTH = NTP_KISS_CODE('A', 'U', 'T', 'H'), ///<Server authentication failed
131  NTP_KISS_CODE_AUTO = NTP_KISS_CODE('A', 'U', 'T', 'O'), ///<Autokey sequence failed
132  NTP_KISS_CODE_BCST = NTP_KISS_CODE('B', 'C', 'S', 'T'), ///<The association belongs to a broadcast server
133  NTP_KISS_CODE_CRYP = NTP_KISS_CODE('C', 'R', 'Y', 'P'), ///<Cryptographic authentication or identification failed
134  NTP_KISS_CODE_DENY = NTP_KISS_CODE('D', 'E', 'N', 'Y'), ///<Access denied by remote server
135  NTP_KISS_CODE_DROP = NTP_KISS_CODE('D', 'R', 'O', 'P'), ///<Lost peer in symmetric mode
136  NTP_KISS_CODE_RSTR = NTP_KISS_CODE('R', 'S', 'T', 'R'), ///<Access denied due to local policy
137  NTP_KISS_CODE_INIT = NTP_KISS_CODE('I', 'N', 'I', 'T'), ///<The association has not yet synchronized for the first time
138  NTP_KISS_CODE_MCST = NTP_KISS_CODE('M', 'C', 'S', 'T'), ///<The association belongs to a manycast server
139  NTP_KISS_CODE_NKEY = NTP_KISS_CODE('N', 'K', 'E', 'Y'), ///<No key found
140  NTP_KISS_CODE_RATE = NTP_KISS_CODE('R', 'A', 'T', 'E'), ///<Rate exceeded
141  NTP_KISS_CODE_RMOT = NTP_KISS_CODE('R', 'M', 'O', 'T'), ///<Somebody is tinkering with the association from a remote host running ntpdc
142  NTP_KISS_CODE_STEP = NTP_KISS_CODE('S', 'T', 'E', 'P'), ///<A step change in system time has occurred
143  NTP_KISS_CODE_NTSN = NTP_KISS_CODE('N', 'T', 'S', 'N') ///<NTS negative-acknowledgment (NAK)
145 
146 
147 /**
148  * @brief NTP extensions field types
149  **/
150 
151 typedef enum
152 {
153  NTP_EXTENSION_TYPE_NO_OPERATION_REQ = 0x0002, ///<No-Operation Request
154  NTP_EXTENSION_TYPE_UNIQUE_ID = 0x0104, ///<Unique Identifier
155  NTP_EXTENSION_TYPE_NTS_COOKIE = 0x0204, ///<NTS Cookie
156  NTP_EXTENSION_TYPE_NTS_COOKIE_PLACEHOLDER = 0x0304, ///<NTS Cookie Placeholder
157  NTP_EXTENSION_TYPE_NTS_AEAD = 0x0404, ///<NTS Authenticator and Encrypted Extension Fields
158  NTP_EXTENSION_TYPE_NO_OPERATION_RESP = 0x8002, ///<No-Operation Response
159  NTP_EXTENSION_TYPE_NO_OPERATION_ERROR_RESP = 0xC002 ///<No-Operation Error Response
161 
162 
163 //CC-RX, CodeWarrior or Win32 compiler?
164 #if defined(__CCRX__)
165  #pragma pack
166 #elif defined(__CWCC__) || defined(_WIN32)
167  #pragma pack(push, 1)
168 #endif
169 
170 
171 /**
172  * @brief NTP timestamp representation
173  **/
174 
176 {
177  uint32_t seconds;
178  uint32_t fraction;
180 
181 
182 /**
183  * @brief NTP packet header
184  **/
185 
186 typedef __packed_struct
187 {
188 #if defined(_CPU_BIG_ENDIAN) && !defined(__ICCRX__)
189  uint8_t li : 2; //0
190  uint8_t vn : 3;
191  uint8_t mode : 3;
192 #else
193  uint8_t mode : 3; //0
194  uint8_t vn : 3;
195  uint8_t li : 2;
196 #endif
197  uint8_t stratum; //1
198  uint8_t poll; //2
199  int8_t precision; //3
200  uint32_t rootDelay; //4-7
201  uint32_t rootDispersion; //8-11
202  uint32_t referenceId; //12-15
207  uint8_t extensions[]; //48
209 
210 
211 /**
212  * @brief NTP extension field
213  **/
214 
215 typedef __packed_struct
216 {
217  uint16_t fieldType; //0-1
218  uint16_t length; //2-3
219  uint8_t value[]; //4
221 
222 
223 /**
224  * @brief NTS Authenticator and Encrypted Extension Fields extension
225  **/
226 
227 typedef __packed_struct
228 {
229  uint16_t fieldType; //0-1
230  uint16_t length; //2-3
231  uint16_t nonceLength; //4-5
232  uint16_t ciphertextLength; //6-7
233  uint8_t nonce[]; //8
235 
236 
237 //CC-RX, CodeWarrior or Win32 compiler?
238 #if defined(__CCRX__)
239  #pragma unpack
240 #elif defined(__CWCC__) || defined(_WIN32)
241  #pragma pack(pop)
242 #endif
243 
244 //NTP related functions
245 const NtpExtension *ntpGetExtension(const uint8_t *extensions, size_t length,
246  uint16_t type, uint_t index);
247 
248 //C++ guard
249 #ifdef __cplusplus
250 }
251 #endif
252 
253 #endif
@ NTP_KISS_CODE_CRYP
Cryptographic authentication or identification failed.
Definition: ntp_common.h:133
uint8_t vn
Definition: ntp_common.h:194
@ NTP_STRATUM_SECONDARY_2
Definition: ntp_common.h:101
uint16_t nonceLength
Definition: ntp_common.h:231
@ NTP_EXTENSION_TYPE_NTS_COOKIE
NTS Cookie.
Definition: ntp_common.h:155
uint8_t extensions[]
Definition: ntp_common.h:207
@ NTP_KISS_CODE_AUTH
Server authentication failed.
Definition: ntp_common.h:130
@ NTP_LI_NO_WARNING
Definition: ntp_common.h:59
NtpTimestamp
Definition: ntp_common.h:179
NtpTimestamp receiveTimestamp
Definition: ntp_common.h:205
@ NTP_VERSION_2
Definition: ntp_common.h:73
@ NTP_STRATUM_SECONDARY_13
Definition: ntp_common.h:112
@ NTP_MODE_SERVER
Definition: ntp_common.h:88
uint8_t type
Definition: coap_common.h:176
NtpTimestamp transmitTimestamp
Definition: ntp_common.h:206
@ NTP_STRATUM_SECONDARY_6
Definition: ntp_common.h:105
@ NTP_STRATUM_SECONDARY_7
Definition: ntp_common.h:106
#define NTP_KISS_CODE(a, b, c, d)
Definition: ntp_common.h:45
NtpNtsAeadExtension
Definition: ntp_common.h:234
@ NTP_STRATUM_SECONDARY_8
Definition: ntp_common.h:107
uint32_t referenceId
Definition: ntp_common.h:202
NtpTimestamp referenceTimestamp
Definition: ntp_common.h:203
uint16_t ciphertextLength
Definition: ntp_common.h:232
@ NTP_EXTENSION_TYPE_NO_OPERATION_ERROR_RESP
No-Operation Error Response.
Definition: ntp_common.h:159
@ NTP_MODE_BROADCAST
Definition: ntp_common.h:89
uint8_t stratum
Definition: ntp_common.h:197
@ NTP_KISS_CODE_RSTR
Access denied due to local policy.
Definition: ntp_common.h:136
@ NTP_STRATUM_SECONDARY_4
Definition: ntp_common.h:103
@ NTP_STRATUM_SECONDARY_9
Definition: ntp_common.h:108
@ NTP_EXTENSION_TYPE_NO_OPERATION_REQ
No-Operation Request.
Definition: ntp_common.h:153
@ NTP_MODE_SYMMETRIC_PASSIVE
Definition: ntp_common.h:86
@ NTP_KISS_CODE_MCST
The association belongs to a manycast server.
Definition: ntp_common.h:138
@ NTP_STRATUM_SECONDARY_12
Definition: ntp_common.h:111
typedef __packed_struct
NTP timestamp representation.
Definition: ntp_common.h:176
@ NTP_STRATUM_SECONDARY_10
Definition: ntp_common.h:109
@ NTP_EXTENSION_TYPE_UNIQUE_ID
Unique Identifier.
Definition: ntp_common.h:154
@ NTP_STRATUM_SECONDARY_15
Definition: ntp_common.h:114
@ NTP_KISS_CODE_RATE
Rate exceeded.
Definition: ntp_common.h:140
uint8_t poll
Definition: ntp_common.h:198
@ NTP_VERSION_3
Definition: ntp_common.h:74
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:60
@ NTP_LI_ALARM_CONDITION
Definition: ntp_common.h:62
@ NTP_KISS_CODE_ACST
The association belongs to a anycast server.
Definition: ntp_common.h:129
@ NTP_LI_LAST_MIN_HAS_59_SECS
Definition: ntp_common.h:61
NtpExtensionType
NTP extensions field types.
Definition: ntp_common.h:152
NtpKissCode
Kiss codes.
Definition: ntp_common.h:128
@ NTP_STRATUM_KISS_OF_DEATH
Definition: ntp_common.h:99
@ NTP_MODE_CLIENT
Definition: ntp_common.h:87
@ NTP_STRATUM_SECONDARY_14
Definition: ntp_common.h:113
@ NTP_KISS_CODE_AUTO
Autokey sequence failed.
Definition: ntp_common.h:131
NtpHeader
Definition: ntp_common.h:208
NtpTimestamp originateTimestamp
Definition: ntp_common.h:204
@ NTP_KISS_CODE_DROP
Lost peer in symmetric mode.
Definition: ntp_common.h:135
@ NTP_EXTENSION_TYPE_NO_OPERATION_RESP
No-Operation Response.
Definition: ntp_common.h:158
NtpLeapIndicator
Leap indicator.
Definition: ntp_common.h:58
@ NTP_STRATUM_PRIMARY
Definition: ntp_common.h:100
uint8_t value[]
Definition: ntp_common.h:219
@ NTP_KISS_CODE_DENY
Access denied by remote server.
Definition: ntp_common.h:134
@ NTP_VERSION_1
Definition: ntp_common.h:72
@ NTP_KISS_CODE_BCST
The association belongs to a broadcast server.
Definition: ntp_common.h:132
uint8_t li
Definition: ntp_common.h:195
@ NTP_STRATUM_SECONDARY_11
Definition: ntp_common.h:110
@ NTP_STRATUM_SECONDARY_5
Definition: ntp_common.h:104
NtpStratum
Stratum.
Definition: ntp_common.h:98
NtpMode
Protocol modes.
Definition: ntp_common.h:84
@ NTP_EXTENSION_TYPE_NTS_COOKIE_PLACEHOLDER
NTS Cookie Placeholder.
Definition: ntp_common.h:156
NtpVersion
NTP version numbers.
Definition: ntp_common.h:71
uint32_t rootDispersion
Definition: ntp_common.h:201
@ NTP_STRATUM_SECONDARY_3
Definition: ntp_common.h:102
uint16_t length
Definition: ntp_common.h:218
@ NTP_KISS_CODE_NTSN
NTS negative-acknowledgment (NAK)
Definition: ntp_common.h:143
@ NTP_MODE_SYMMETRIC_ACTIVE
Definition: ntp_common.h:85
NtpExtension
Definition: ntp_common.h:220
int8_t precision
Definition: ntp_common.h:199
@ NTP_KISS_CODE_RMOT
Somebody is tinkering with the association from a remote host running ntpdc.
Definition: ntp_common.h:141
unsigned int uint_t
Definition: compiler_port.h:50
TCP/IP stack core.
@ NTP_KISS_CODE_INIT
The association has not yet synchronized for the first time.
Definition: ntp_common.h:137
@ NTP_KISS_CODE_STEP
A step change in system time has occurred.
Definition: ntp_common.h:142
@ NTP_VERSION_4
Definition: ntp_common.h:75
uint8_t nonce[]
Definition: ntp_common.h:233
@ NTP_KISS_CODE_NKEY
No key found.
Definition: ntp_common.h:139
uint32_t fraction
Definition: ntp_common.h:178
@ NTP_EXTENSION_TYPE_NTS_AEAD
NTS Authenticator and Encrypted Extension Fields.
Definition: ntp_common.h:157
uint32_t rootDelay
Definition: ntp_common.h:200