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-2019 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 1.9.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 2208988800
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 {
76 } NtpVersion;
77 
78 
79 /**
80  * @brief Protocol modes
81  **/
82 
83 typedef enum
84 {
90 } NtpMode;
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
118 } NtpKissCode;
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 #ifdef _CPU_BIG_ENDIAN
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
uint32_t rootDelay
Definition: ntp_common.h:156
#define NTP_KISS_CODE(a, b, c, d)
Definition: ntp_common.h:45
A step change in system time has occurred.
Definition: ntp_common.h:117
Cryptographic authentication or identification failed.
Definition: ntp_common.h:108
NtpKissCode
Kiss codes.
Definition: ntp_common.h:102
__start_packed struct @294 NtpAuthenticator
NTP authenticator.
NtpTimestamp referenceTimestamp
Definition: ntp_common.h:159
TCP/IP stack core.
NtpTimestamp receiveTimestamp
Definition: ntp_common.h:161
Access denied by remote server.
Definition: ntp_common.h:109
uint32_t seconds
Definition: ntp_common.h:133
NtpLeapIndicator
Leap indicator.
Definition: ntp_common.h:57
NtpTimestamp transmitTimestamp
Definition: ntp_common.h:162
The association belongs to a broadcast server.
Definition: ntp_common.h:107
The association belongs to a anycast server.
Definition: ntp_common.h:104
No key found.
Definition: ntp_common.h:114
uint32_t keyId
Definition: ntp_common.h:172
Somebody is tinkering with the association from a remote host running ntpdc.
Definition: ntp_common.h:116
The association has not yet synchronized for the first time.
Definition: ntp_common.h:112
uint8_t li
Definition: ntp_common.h:151
NtpVersion
NTP version numbers.
Definition: ntp_common.h:70
Server authentication failed.
Definition: ntp_common.h:105
Access denied due to local policy.
Definition: ntp_common.h:111
Rate exceeded.
Definition: ntp_common.h:115
__start_packed struct @293 NtpHeader
NTP packet header.
uint32_t fraction
Definition: ntp_common.h:134
uint8_t stratum
Definition: ntp_common.h:153
uint8_t vn
Definition: ntp_common.h:150
Lost peer in symmetric mode.
Definition: ntp_common.h:110
__start_packed struct @292 NtpTimestamp
NTP timestamp representation.
Autokey sequence failed.
Definition: ntp_common.h:106
uint8_t mode
Definition: ntp_common.h:149
__start_packed struct _Ipv4Header __end_packed
uint32_t referenceId
Definition: ntp_common.h:158
uint8_t messageDigest[16]
Definition: ntp_common.h:173
NtpMode
Protocol modes.
Definition: ntp_common.h:83
NtpTimestamp originateTimestamp
Definition: ntp_common.h:160
int8_t precision
Definition: ntp_common.h:155
uint8_t poll
Definition: ntp_common.h:154
The association belongs to a manycast server.
Definition: ntp_common.h:113
uint32_t rootDispersion
Definition: ntp_common.h:157