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-2023 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.2.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 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 //CodeWarrior or Win32 compiler?
122 #if defined(__CWCC__) || defined(_WIN32)
123  #pragma pack(push, 1)
124 #endif
125 
126 
127 /**
128  * @brief NTP timestamp representation
129  **/
130 
131 typedef __start_packed struct
132 {
133  uint32_t seconds;
134  uint32_t fraction;
136 
137 
138 /**
139  * @brief NTP packet header
140  **/
141 
142 typedef __start_packed struct
143 {
144 #if defined(_CPU_BIG_ENDIAN) && !defined(__ICCRX__)
145  uint8_t li : 2; //0
146  uint8_t vn : 3;
147  uint8_t mode : 3;
148 #else
149  uint8_t mode : 3; //0
150  uint8_t vn : 3;
151  uint8_t li : 2;
152 #endif
153  uint8_t stratum; //1
154  uint8_t poll; //2
155  int8_t precision; //3
156  uint32_t rootDelay; //4-7
157  uint32_t rootDispersion; //8-11
158  uint32_t referenceId; //12-15
164 
165 
166 /**
167  * @brief NTP authenticator
168  **/
169 
170 typedef __start_packed struct
171 {
172  uint32_t keyId; //0-3
173  uint8_t messageDigest[16]; //4-19
175 
176 
177 //CodeWarrior or Win32 compiler?
178 #if defined(__CWCC__) || defined(_WIN32)
179  #pragma pack(pop)
180 #endif
181 
182 //C++ guard
183 #ifdef __cplusplus
184 }
185 #endif
186 
187 #endif
@ NTP_KISS_CODE_CRYP
Cryptographic authentication or identification failed.
Definition: ntp_common.h:108
uint8_t vn
Definition: ntp_common.h:150
@ NTP_KISS_CODE_AUTH
Server authentication failed.
Definition: ntp_common.h:105
@ NTP_LI_NO_WARNING
Definition: ntp_common.h:59
NtpTimestamp receiveTimestamp
Definition: ntp_common.h:161
@ NTP_VERSION_2
Definition: ntp_common.h:73
uint32_t keyId
Definition: ntp_common.h:172
@ NTP_MODE_SERVER
Definition: ntp_common.h:88
NtpTimestamp transmitTimestamp
Definition: ntp_common.h:162
#define NTP_KISS_CODE(a, b, c, d)
Definition: ntp_common.h:45
__start_packed struct @2 NtpAuthenticator
NTP authenticator.
uint32_t referenceId
Definition: ntp_common.h:158
NtpTimestamp referenceTimestamp
Definition: ntp_common.h:159
@ NTP_MODE_BROADCAST
Definition: ntp_common.h:89
uint8_t stratum
Definition: ntp_common.h:153
@ NTP_KISS_CODE_RSTR
Access denied due to local policy.
Definition: ntp_common.h:111
@ NTP_MODE_SYMMETRIC_PASSIVE
Definition: ntp_common.h:86
@ NTP_KISS_CODE_MCST
The association belongs to a manycast server.
Definition: ntp_common.h:113
@ NTP_KISS_CODE_RATE
Rate exceeded.
Definition: ntp_common.h:115
__start_packed struct _Ipv4Header __end_packed
uint8_t poll
Definition: ntp_common.h:154
@ NTP_VERSION_3
Definition: ntp_common.h:74
@ 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:104
@ NTP_LI_LAST_MIN_HAS_59_SECS
Definition: ntp_common.h:61
NtpKissCode
Kiss codes.
Definition: ntp_common.h:103
__start_packed struct @1 NtpHeader
NTP packet header.
@ NTP_MODE_CLIENT
Definition: ntp_common.h:87
@ NTP_KISS_CODE_AUTO
Autokey sequence failed.
Definition: ntp_common.h:106
NtpTimestamp originateTimestamp
Definition: ntp_common.h:160
@ NTP_KISS_CODE_DROP
Lost peer in symmetric mode.
Definition: ntp_common.h:110
uint32_t seconds
Definition: ntp_common.h:133
NtpLeapIndicator
Leap indicator.
Definition: ntp_common.h:58
@ NTP_KISS_CODE_DENY
Access denied by remote server.
Definition: ntp_common.h:109
@ NTP_VERSION_1
Definition: ntp_common.h:72
@ NTP_KISS_CODE_BCST
The association belongs to a broadcast server.
Definition: ntp_common.h:107
uint8_t li
Definition: ntp_common.h:151
NtpMode
Protocol modes.
Definition: ntp_common.h:84
NtpVersion
NTP version numbers.
Definition: ntp_common.h:71
uint32_t rootDispersion
Definition: ntp_common.h:157
uint8_t mode
Definition: ntp_common.h:149
@ NTP_MODE_SYMMETRIC_ACTIVE
Definition: ntp_common.h:85
int8_t precision
Definition: ntp_common.h:155
@ NTP_KISS_CODE_RMOT
Somebody is tinkering with the association from a remote host running ntpdc.
Definition: ntp_common.h:116
TCP/IP stack core.
@ NTP_KISS_CODE_INIT
The association has not yet synchronized for the first time.
Definition: ntp_common.h:112
__start_packed struct @0 NtpTimestamp
NTP timestamp representation.
@ NTP_KISS_CODE_STEP
A step change in system time has occurred.
Definition: ntp_common.h:117
@ NTP_VERSION_4
Definition: ntp_common.h:75
uint8_t messageDigest[16]
Definition: ntp_common.h:173
@ NTP_KISS_CODE_NKEY
No key found.
Definition: ntp_common.h:114
uint32_t fraction
Definition: ntp_common.h:134
uint32_t rootDelay
Definition: ntp_common.h:156