sntp_client.h
Go to the documentation of this file.
1 /**
2  * @file sntp_client.h
3  * @brief SNTP client (Simple Network Time Protocol)
4  *
5  * @section License
6  *
7  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneTCP Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 1.9.0
27  **/
28 
29 #ifndef _SNTP_CLIENT_H
30 #define _SNTP_CLIENT_H
31 
32 //Dependencies
33 #include "core/net.h"
34 #include "core/socket.h"
35 
36 //SNTP client support
37 #ifndef SNTP_CLIENT_SUPPORT
38  #define SNTP_CLIENT_SUPPORT ENABLED
39 #elif (SNTP_CLIENT_SUPPORT != ENABLED && SNTP_CLIENT_SUPPORT != DISABLED)
40  #error SNTP_CLIENT_SUPPORT parameter is not valid
41 #endif
42 
43 //Maximum number of retransmissions of SNTP requests
44 #ifndef SNTP_CLIENT_MAX_RETRIES
45  #define SNTP_CLIENT_MAX_RETRIES 3
46 #elif (SNTP_CLIENT_MAX_RETRIES < 1)
47  #error SNTP_CLIENT_MAX_RETRIES parameter is not valid
48 #endif
49 
50 //Initial retransmission timeout
51 #ifndef SNTP_CLIENT_INIT_TIMEOUT
52  #define SNTP_CLIENT_INIT_TIMEOUT 1000
53 #elif (SNTP_CLIENT_INIT_TIMEOUT < 1000)
54  #error SNTP_CLIENT_INIT_TIMEOUT parameter is not valid
55 #endif
56 
57 //Maximum retransmission timeout
58 #ifndef SNTP_CLIENT_MAX_TIMEOUT
59  #define SNTP_CLIENT_MAX_TIMEOUT 5000
60 #elif (SNTP_CLIENT_MAX_TIMEOUT < 1000)
61  #error SNTP_CLIENT_MAX_TIMEOUT parameter is not valid
62 #endif
63 
64 //NTP port number
65 #define NTP_PORT 123
66 //Maximum size of NTP packets
67 #define NTP_MESSAGE_MAX_SIZE 68
68 
69 //C++ guard
70 #ifdef __cplusplus
71  extern "C" {
72 #endif
73 
74 
75 /**
76  * @brief Leap indicator
77  **/
78 
79 typedef enum
80 {
86 
87 
88 /**
89  * @brief NTP version number
90  **/
91 
92 typedef enum
93 {
98 } NtpVersion;
99 
100 
101 /**
102  * @brief Protocol mode
103  **/
104 
105 typedef enum
106 {
112 } NtpMode;
113 
114 
115 //CodeWarrior or Win32 compiler?
116 #if defined(__CWCC__) || defined(_WIN32)
117  #pragma pack(push, 1)
118 #endif
119 
120 
121 /**
122  * @brief Time representation
123  **/
124 
125 typedef __start_packed struct
126 {
127  uint32_t seconds;
128  uint32_t fraction;
130 
131 
132 /**
133  * @brief NTP packet header
134  **/
135 
136 typedef __start_packed struct
137 {
138 #ifdef _CPU_BIG_ENDIAN
139  uint8_t li : 2; //0
140  uint8_t vn : 3;
141  uint8_t mode : 3;
142 #else
143  uint8_t mode : 3; //0
144  uint8_t vn : 3;
145  uint8_t li : 2;
146 #endif
147  uint8_t stratum; //1
148  uint8_t poll; //2
149  int8_t precision; //3
150  uint32_t rootDelay; //4-7
151  uint32_t rootDispersion; //8-11
152  uint32_t referenceIdentifier; //12-15
158 
159 
160 /**
161  * @brief Authentication data
162  **/
163 
164 typedef __start_packed struct
165 {
166  uint32_t keyIdentifier;
167  uint8_t messageDigest[16];
169 
170 
171 //CodeWarrior or Win32 compiler?
172 #if defined(__CWCC__) || defined(_WIN32)
173  #pragma pack(pop)
174 #endif
175 
176 
177 /**
178  * @brief SNTP client context
179  **/
180 
181 typedef struct
182 {
183  Socket *socket; ///<Underlying socket
184  NtpHeader message; ///<Buffer where to format NTP messages
185  systime_t t1; ///<Time at which the NTP request was sent by the client
186  systime_t t4; ///<Time at which the NTP reply was received by the client
188 
189 
190 //SNTP client related functions
192  const IpAddr *serverIpAddr, NtpTimestamp *timestamp);
193 
196 
198  const NtpHeader *message, size_t length);
199 
200 void sntpDumpMessage(const NtpHeader *message, size_t length);
201 void sntpDumpTimestamp(const NtpTimestamp *timestamp);
202 
203 //C++ guard
204 #ifdef __cplusplus
205  }
206 #endif
207 
208 #endif
uint32_t systime_t
Definition: compiler_port.h:44
SNTP client context.
Definition: sntp_client.h:181
systime_t t1
Time at which the NTP request was sent by the client.
Definition: sntp_client.h:185
uint8_t li
Definition: sntp_client.h:145
error_t sntpClientGetTimestamp(NetInterface *interface, const IpAddr *serverIpAddr, NtpTimestamp *timestamp)
Retrieve current time from NTP server using SNTP protocol.
Definition: sntp_client.c:55
TCP/IP stack core.
void sntpDumpTimestamp(const NtpTimestamp *timestamp)
Dump NTP timestamp.
Definition: sntp_client.c:345
uint8_t message[]
Definition: chap.h:150
__start_packed struct @295 NtpTimestamp
Time representation.
IP network address.
Definition: ip.h:57
uint8_t stratum
Definition: sntp_client.h:147
NtpTimestamp receiveTimestamp
Definition: sntp_client.h:155
error_t sntpParseResponse(SntpClientContext *context, const NtpHeader *message, size_t length)
Parse NTP server response.
Definition: sntp_client.c:238
uint8_t vn
Definition: sntp_client.h:144
void sntpDumpMessage(const NtpHeader *message, size_t length)
Dump NTP message for debugging purpose.
Definition: sntp_client.c:280
error_t sntpSendRequest(SntpClientContext *context)
Send NTP request using SNTP protocol.
Definition: sntp_client.c:142
#define Socket
Definition: socket.h:34
NtpMode
Protocol mode.
Definition: sntp_client.h:105
uint32_t fraction
Definition: sntp_client.h:128
uint32_t rootDispersion
Definition: sntp_client.h:151
uint32_t seconds
Definition: sntp_client.h:127
systime_t t4
Time at which the NTP reply was received by the client.
Definition: sntp_client.h:186
NtpTimestamp transmitTimestamp
Definition: sntp_client.h:156
NtpHeader message
Buffer where to format NTP messages.
Definition: sntp_client.h:184
__start_packed struct _Ipv4Header __end_packed
NtpTimestamp referenceTimestamp
Definition: sntp_client.h:153
error_t
Error codes.
Definition: error.h:40
uint8_t messageDigest[16]
Definition: sntp_client.h:167
NtpLeapIndicator
Leap indicator.
Definition: sntp_client.h:79
__start_packed struct @296 NtpHeader
NTP packet header.
Socket * socket
Underlying socket.
Definition: sntp_client.h:183
#define NetInterface
Definition: net.h:34
__start_packed struct @297 NtpAuthData
Authentication data.
NtpTimestamp originateTimestamp
Definition: sntp_client.h:154
uint8_t mode
Definition: sntp_client.h:143
error_t sntpWaitForResponse(SntpClientContext *context, systime_t timeout)
Wait for a valid response from the NTP server.
Definition: sntp_client.c:186
uint32_t rootDelay
Definition: sntp_client.h:150
uint32_t keyIdentifier
Definition: sntp_client.h:166
Socket API.
uint8_t length
Definition: dtls_misc.h:140
uint8_t poll
Definition: sntp_client.h:148
int8_t precision
Definition: sntp_client.h:149
NtpVersion
NTP version number.
Definition: sntp_client.h:92
uint32_t referenceIdentifier
Definition: sntp_client.h:152