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.0
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 Kiss codes
95  *
96  * The kiss codes can provide useful information for an intelligent client.
97  * These codes are encoded in four-character ASCII strings left justified
98  * and zero filled
99  *
100  **/
101 
102 typedef enum
103 {
104  NTP_KISS_CODE_ACST = NTP_KISS_CODE('A', 'C', 'S', 'T'), ///<The association belongs to a anycast server
105  NTP_KISS_CODE_AUTH = NTP_KISS_CODE('A', 'U', 'T', 'H'), ///<Server authentication failed
106  NTP_KISS_CODE_AUTO = NTP_KISS_CODE('A', 'U', 'T', 'O'), ///<Autokey sequence failed
107  NTP_KISS_CODE_BCST = NTP_KISS_CODE('B', 'C', 'S', 'T'), ///<The association belongs to a broadcast server
108  NTP_KISS_CODE_CRYP = NTP_KISS_CODE('C', 'R', 'Y', 'P'), ///<Cryptographic authentication or identification failed
109  NTP_KISS_CODE_DENY = NTP_KISS_CODE('D', 'E', 'N', 'Y'), ///<Access denied by remote server
110  NTP_KISS_CODE_DROP = NTP_KISS_CODE('D', 'R', 'O', 'P'), ///<Lost peer in symmetric mode
111  NTP_KISS_CODE_RSTR = NTP_KISS_CODE('R', 'S', 'T', 'R'), ///<Access denied due to local policy
112  NTP_KISS_CODE_INIT = NTP_KISS_CODE('I', 'N', 'I', 'T'), ///<The association has not yet synchronized for the first time
113  NTP_KISS_CODE_MCST = NTP_KISS_CODE('M', 'C', 'S', 'T'), ///<The association belongs to a manycast server
114  NTP_KISS_CODE_NKEY = NTP_KISS_CODE('N', 'K', 'E', 'Y'), ///<No key found
115  NTP_KISS_CODE_RATE = NTP_KISS_CODE('R', 'A', 'T', 'E'), ///<Rate exceeded
116  NTP_KISS_CODE_RMOT = NTP_KISS_CODE('R', 'M', 'O', 'T'), ///<Somebody is tinkering with the association from a remote host running ntpdc
117  NTP_KISS_CODE_STEP = NTP_KISS_CODE('S', 'T', 'E', 'P') ///<A step change in system time has occurred
119 
120 
121 //CC-RX, CodeWarrior or Win32 compiler?
122 #if defined(__CCRX__)
123  #pragma pack
124 #elif defined(__CWCC__) || defined(_WIN32)
125  #pragma pack(push, 1)
126 #endif
127 
128 
129 /**
130  * @brief NTP timestamp representation
131  **/
132 
134 {
135  uint32_t seconds;
136  uint32_t fraction;
138 
139 
140 /**
141  * @brief NTP packet header
142  **/
143 
144 typedef __packed_struct
145 {
146 #if defined(_CPU_BIG_ENDIAN) && !defined(__ICCRX__)
147  uint8_t li : 2; //0
148  uint8_t vn : 3;
149  uint8_t mode : 3;
150 #else
151  uint8_t mode : 3; //0
152  uint8_t vn : 3;
153  uint8_t li : 2;
154 #endif
155  uint8_t stratum; //1
156  uint8_t poll; //2
157  int8_t precision; //3
158  uint32_t rootDelay; //4-7
159  uint32_t rootDispersion; //8-11
160  uint32_t referenceId; //12-15
166 
167 
168 /**
169  * @brief NTP authenticator
170  **/
171 
172 typedef __packed_struct
173 {
174  uint32_t keyId; //0-3
175  uint8_t messageDigest[16]; //4-19
177 
178 
179 //CC-RX, CodeWarrior or Win32 compiler?
180 #if defined(__CCRX__)
181  #pragma unpack
182 #elif defined(__CWCC__) || defined(_WIN32)
183  #pragma pack(pop)
184 #endif
185 
186 //C++ guard
187 #ifdef __cplusplus
188 }
189 #endif
190 
191 #endif
TCP/IP stack core.
NtpLeapIndicator
Leap indicator.
Definition: ntp_common.h:58
@ NTP_LI_NO_WARNING
Definition: ntp_common.h:59
@ NTP_LI_LAST_MIN_HAS_59_SECS
Definition: ntp_common.h:61
@ NTP_LI_ALARM_CONDITION
Definition: ntp_common.h:62
@ NTP_LI_LAST_MIN_HAS_61_SECS
Definition: ntp_common.h:60
NtpKissCode
Kiss codes.
Definition: ntp_common.h:103
@ NTP_KISS_CODE_RMOT
Somebody is tinkering with the association from a remote host running ntpdc.
Definition: ntp_common.h:116
@ NTP_KISS_CODE_INIT
The association has not yet synchronized for the first time.
Definition: ntp_common.h:112
@ NTP_KISS_CODE_MCST
The association belongs to a manycast server.
Definition: ntp_common.h:113
@ NTP_KISS_CODE_CRYP
Cryptographic authentication or identification failed.
Definition: ntp_common.h:108
@ NTP_KISS_CODE_RATE
Rate exceeded.
Definition: ntp_common.h:115
@ NTP_KISS_CODE_NKEY
No key found.
Definition: ntp_common.h:114
@ NTP_KISS_CODE_DROP
Lost peer in symmetric mode.
Definition: ntp_common.h:110
@ NTP_KISS_CODE_ACST
The association belongs to a anycast server.
Definition: ntp_common.h:104
@ NTP_KISS_CODE_BCST
The association belongs to a broadcast server.
Definition: ntp_common.h:107
@ NTP_KISS_CODE_DENY
Access denied by remote server.
Definition: ntp_common.h:109
@ NTP_KISS_CODE_RSTR
Access denied due to local policy.
Definition: ntp_common.h:111
@ NTP_KISS_CODE_AUTH
Server authentication failed.
Definition: ntp_common.h:105
@ NTP_KISS_CODE_STEP
A step change in system time has occurred.
Definition: ntp_common.h:117
@ NTP_KISS_CODE_AUTO
Autokey sequence failed.
Definition: ntp_common.h:106
NtpAuthenticator
Definition: ntp_common.h:176
uint8_t vn
Definition: ntp_common.h:152
uint32_t rootDispersion
Definition: ntp_common.h:159
uint8_t poll
Definition: ntp_common.h:156
NtpVersion
NTP version numbers.
Definition: ntp_common.h:71
@ NTP_VERSION_3
Definition: ntp_common.h:74
@ NTP_VERSION_2
Definition: ntp_common.h:73
@ NTP_VERSION_4
Definition: ntp_common.h:75
@ NTP_VERSION_1
Definition: ntp_common.h:72
NtpMode
Protocol modes.
Definition: ntp_common.h:84
@ NTP_MODE_CLIENT
Definition: ntp_common.h:87
@ NTP_MODE_SYMMETRIC_ACTIVE
Definition: ntp_common.h:85
@ NTP_MODE_SERVER
Definition: ntp_common.h:88
@ NTP_MODE_BROADCAST
Definition: ntp_common.h:89
@ NTP_MODE_SYMMETRIC_PASSIVE
Definition: ntp_common.h:86
NtpTimestamp
Definition: ntp_common.h:137
uint32_t rootDelay
Definition: ntp_common.h:158
#define NTP_KISS_CODE(a, b, c, d)
Definition: ntp_common.h:45
NtpTimestamp receiveTimestamp
Definition: ntp_common.h:163
uint8_t li
Definition: ntp_common.h:153
NtpTimestamp transmitTimestamp
Definition: ntp_common.h:164
NtpHeader
Definition: ntp_common.h:165
uint32_t fraction
Definition: ntp_common.h:136
uint8_t messageDigest[16]
Definition: ntp_common.h:175
uint32_t referenceId
Definition: ntp_common.h:160
typedef __packed_struct
NTP timestamp representation.
Definition: ntp_common.h:134
NtpTimestamp referenceTimestamp
Definition: ntp_common.h:161
NtpTimestamp originateTimestamp
Definition: ntp_common.h:162
int8_t precision
Definition: ntp_common.h:157
uint8_t stratum
Definition: ntp_common.h:155