Webserver+3d print
Embed:
(wiki syntax)
Show/hide line numbers
sntp_client.h
Go to the documentation of this file.
00001 /** 00002 * @file sntp_client.h 00003 * @brief SNTP client (Simple Network Time Protocol) 00004 * 00005 * @section License 00006 * 00007 * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved. 00008 * 00009 * This file is part of CycloneTCP Open. 00010 * 00011 * This program is free software; you can redistribute it and/or 00012 * modify it under the terms of the GNU General Public License 00013 * as published by the Free Software Foundation; either version 2 00014 * of the License, or (at your option) any later version. 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU General Public License for more details. 00020 * 00021 * You should have received a copy of the GNU General Public License 00022 * along with this program; if not, write to the Free Software Foundation, 00023 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00024 * 00025 * @author Oryx Embedded SARL (www.oryx-embedded.com) 00026 * @version 1.7.6 00027 **/ 00028 00029 //Dependencies 00030 #include "core/net.h" 00031 #include "core/socket.h" 00032 00033 //SNTP client support 00034 #ifndef SNTP_CLIENT_SUPPORT 00035 #define SNTP_CLIENT_SUPPORT ENABLED 00036 #elif (SNTP_CLIENT_SUPPORT != ENABLED && SNTP_CLIENT_SUPPORT != DISABLED) 00037 #error SNTP_CLIENT_SUPPORT parameter is not valid 00038 #endif 00039 00040 //Maximum number of retransmissions of SNTP requests 00041 #ifndef SNTP_CLIENT_MAX_RETRIES 00042 #define SNTP_CLIENT_MAX_RETRIES 3 00043 #elif (SNTP_CLIENT_MAX_RETRIES < 1) 00044 #error SNTP_CLIENT_MAX_RETRIES parameter is not valid 00045 #endif 00046 00047 //Initial retransmission timeout 00048 #ifndef SNTP_CLIENT_INIT_TIMEOUT 00049 #define SNTP_CLIENT_INIT_TIMEOUT 1000 00050 #elif (SNTP_CLIENT_INIT_TIMEOUT < 1000) 00051 #error SNTP_CLIENT_INIT_TIMEOUT parameter is not valid 00052 #endif 00053 00054 //Maximum retransmission timeout 00055 #ifndef SNTP_CLIENT_MAX_TIMEOUT 00056 #define SNTP_CLIENT_MAX_TIMEOUT 5000 00057 #elif (SNTP_CLIENT_MAX_TIMEOUT < 1000) 00058 #error SNTP_CLIENT_MAX_TIMEOUT parameter is not valid 00059 #endif 00060 00061 //NTP port number 00062 #define NTP_PORT 123 00063 //Maximum size of NTP packets 00064 #define NTP_MESSAGE_MAX_SIZE 68 00065 00066 /** 00067 * @brief Leap indicator 00068 **/ 00069 00070 typedef enum 00071 { 00072 NTP_LI_NO_WARNING = 0, 00073 NTP_LI_LAST_MIN_HAS_61_SECS = 1, 00074 NTP_LI_LAST_MIN_HAS_59_SECS = 2, 00075 NTP_LI_ALARM_CONDITION = 3 00076 } NtpLeapIndicator; 00077 00078 00079 /** 00080 * @brief NTP version number 00081 **/ 00082 00083 typedef enum 00084 { 00085 NTP_VERSION_1 = 1, 00086 NTP_VERSION_2 = 2, 00087 NTP_VERSION_3 = 3, 00088 NTP_VERSION_4 = 4 00089 } NtpVersion; 00090 00091 00092 /** 00093 * @brief Protocol mode 00094 **/ 00095 00096 typedef enum 00097 { 00098 NTP_MODE_SYMMETRIC_ACTIVE = 1, 00099 NTP_MODE_SYMMETRIC_PASSIVE = 2, 00100 NTP_MODE_CLIENT = 3, 00101 NTP_MODE_SERVER = 4, 00102 NTP_MODE_BROADCAST = 5 00103 } NtpMode; 00104 00105 00106 //CodeWarrior or Win32 compiler? 00107 #if defined(__CWCC__) || defined(_WIN32) 00108 #pragma pack(push, 1) 00109 #endif 00110 00111 00112 /** 00113 * @brief Time representation 00114 **/ 00115 00116 typedef __start_packed struct 00117 { 00118 uint32_t seconds; 00119 uint32_t fraction; 00120 } __end_packed NtpTimestamp; 00121 00122 00123 /** 00124 * @brief NTP packet header 00125 **/ 00126 00127 typedef __start_packed struct 00128 { 00129 #ifdef _CPU_BIG_ENDIAN 00130 uint8_t li : 2; //0 00131 uint8_t vn : 3; 00132 uint8_t mode : 3; 00133 #else 00134 uint8_t mode : 3; //0 00135 uint8_t vn : 3; 00136 uint8_t li : 2; 00137 #endif 00138 uint8_t stratum; //1 00139 uint8_t poll; //2 00140 int8_t precision; //3 00141 uint32_t rootDelay; //4-7 00142 uint32_t rootDispersion; //8-11 00143 uint32_t referenceIdentifier; //12-15 00144 NtpTimestamp referenceTimestamp; //16-23 00145 NtpTimestamp originateTimestamp; //24-31 00146 NtpTimestamp receiveTimestamp; //32-39 00147 NtpTimestamp transmitTimestamp; //40-47 00148 } __end_packed NtpHeader; 00149 00150 00151 /** 00152 * @brief Authentication data 00153 **/ 00154 00155 typedef __start_packed struct 00156 { 00157 uint32_t keyIdentifier; 00158 uint8_t messageDigest[16]; 00159 } __end_packed NtpAuthData; 00160 00161 00162 //CodeWarrior or Win32 compiler? 00163 #if defined(__CWCC__) || defined(_WIN32) 00164 #pragma pack(pop) 00165 #endif 00166 00167 00168 /** 00169 * @brief SNTP client context 00170 **/ 00171 00172 typedef struct 00173 { 00174 Socket *socket; ///<Underlying socket 00175 NtpHeader message; ///<Buffer where to format NTP messages 00176 systime_t t1; ///<Time at which the NTP request was sent by the client 00177 systime_t t4; ///<Time at which the NTP reply was received by the client 00178 } SntpClientContext; 00179 00180 00181 //SNTP client related functions 00182 error_t sntpClientGetTimestamp(NetInterface *interface, 00183 const IpAddr *serverIpAddr, NtpTimestamp *timestamp); 00184 00185 error_t sntpSendRequest(SntpClientContext *context); 00186 error_t sntpWaitForResponse(SntpClientContext *context, systime_t timeout); 00187 00188 error_t sntpParseResponse(SntpClientContext *context, 00189 const NtpHeader *message, size_t length); 00190 00191 void sntpDumpMessage(const NtpHeader *message, size_t length); 00192 void sntpDumpTimestamp(const NtpTimestamp *timestamp); 00193
Generated on Tue Jul 12 2022 17:10:16 by
