一、SNTP介绍
SNTP是一种简单的网络时间协议,它可以通过网络协议同步计算机的时间,时间的精度不如NTP。
关于SNTP介绍,可以参考
https://www.ionos.com/digitalguide/server/know-how/sntp-simple-network-time-protocol/
或者
https://timetoolsltd.com/ntp/sntp-overview/
该原文如下:
Simple Network Time Protocol (SNTP) is an Internet Protocol (IP) used to synchronize the clocks of networks of computers.
SNTP is a subset of the Network Time Protocol (NTP).
The latest version is SNTP v4.
It can synchronize seamlessly to full-blown NTP servers.
Originally developed for small computers and micro-controllers.
Requires less memory and processing power than NTP.
It is used in applications where precise clock synchronization is not critical.
Uses the TCP/IP protocol suite, UDP port 123.
SNTP is a subset of the full-blown Network Time Protocol (NTP).
Complex statistical algorithms have been developed for NTP in order to improve the accuracy of clock synchronization and to reduce clock drift. However, the algorithms consume considerable amounts of memory and processing power.
For many applications, particularly on smaller computers, the ultimate performance of NTP is not needed. For this reason, the Simple Network Time Protocol (SNTP) was developed to provide clock synchronization for less powerful computers, where the complexity of NTP is not required.
SNTP Protocol Suite
SNTP is based upon the TCP/IP protocol suite. It is an application layer time protocol, part of the Network Time Protocol base protocol. Along with NTP, SNTP communicates using the User Datagram Protocol (UDP). By default, UDP port 123 is used.
A common oversight with new SNTP installations is that the UDP port must be left open in any firewall to allow communications transfer. If a client cannot synchronize to a server, check that UDP port 123 is open in the firewall configuration.
SNTP can operate on IPv4 and IPv6 networks and is defined by RFC 4330.
How SNTP Works
The SNTP protocol is a client-server protocol based on the exchange of packets of timing information.
Generally, clients operate in a unicast mode. At periodic intervals, usually in the region of every 64 seconds, a SNTP client sends a request to a NTP server referencing its IP address. The server is generally synchronized to a hardware reference clock, such as GPS.
On receipt of a response from the server, the client can calculate the system time offset between its internal clock and the servers clock. Enhanced synchronization is achieved by calculating packet round trip delays.
If required, SNTP can be configured in Broadcast or Multicast modes. In this instance, a server continually broadcasts timestamp packets at periodic intervals. Clients continually listen for packets and, on receipt, adjust their system clocks accordingly.
The NTP and SNTP protocols are completely interoperable.
Any SNTP client can synchronize to any NTP server. This is primarily because the packets of information exchanged are identical.
The differences between the protocols does not lie in the information exchange, but what the algorithms actually do with the information in order to achieve synchronization.
The Differences Between NTP and SNTP
The main differences between NTP and SNTP are contained within the application.
NTP has developed complex statistical algorithms with calibration techniques, aimed at filtering small discrepancies, to achieve a very high degree of clock synchronization.
Fine time adjustments are made by tweaking the ‘tick’ rate of the host computers system clock. NTP attempts to make time corrections seamlessly by skewing time adjustments, to avoid stepped changes.
Redundancy is implemented by continuously monitoring multiple time references. Complex selection algorithms ascertain the most reliable and stable. Multiple sources of time, including a mixture of hardware clocks and network time sources can be monitored, aiding robustness.
There are also a number of security features implemented in NTP. Secure client-server authentication is achieved by implementing Symmetric Key Cryptography. This allows a client to be sure of the origin of received time-stamps alleviating spoofing.
In contrast, SNTP adopts a much simpler approach to network clock synchronization. Many of the complexities of the NTP algorithms have been removed or simplified.
Rather than skewing time, many SNTP clients step time. However, stepping time can cause problems with event ordering. For example, if time is stepped back between the generation of two transactions, they would not have a correctly sequenced order.
SNTP also lacks the ability to monitor and filter multiple time references. The protocol can generally only be configured to operate from a single source of time. It is not a good choice if multiple redundant time sources are implemented.
For simplicity, many SNTP clients do not implement secure authentication techniques, which can leave systems vulnerable to attack from malicious users.
Modern, powerful, computer systems can easily cope with the additional overheads required by NTP with no significant loss of performance. Therefore, NTP should be used for all timing applications, unless the larger foot-print and resource requirement is a real issue.
Where Can SNTP Be Used in Place of NTP
Simple Network Time Protocol (SNTP) allows computers with less processing power to implement clock synchronization.
Typically, it is used by small network devices, such as IP cameras, DVR’s, IP phones, routers and consumer devices.
A number of operating systems for small computers and workstations also use the SNTP protocol for simplicity and because of it’s smaller foot-print and lower resource requirements.
Essentially, SNTP offers a much lower-quality time synchronization solution than NTP. However, it is ideal where the requirements of accurate time, security and reliability are less of a priority and having an application that uses fewer resources is important.
NTP must be used where clock synchronization is of critical importance, such as financial transaction processing and Air Traffic Control.
Summary
Simple Network Time Protocol was developed for small computers and micro-controllers with limited memory and processing power.
It provides clock synchronization for applications that do not require the precision of full-blown NTP.
NTP is better suited to synchronizing large clusters of computers, such as corporate networks. The robustness of its calibration algorithms helps larger computer systems reliably maintain accurate time synchronization.
二、Mongoose 开源库-SNTP相关API
官方手册介绍相关的API只有2个,如下:
// Connect SNTP server
struct mg_connection *mg_sntp_connect(struct mg_mgr *mgr, const char *url,
mg_event_handler_t fn, void *fn_data)
// Send time request to SNTP server
void mg_sntp_request(struct mg_connection *c)
三、测试代码
测试代码,如下:
#include "mongoose.h"
// SNTP client connection
static struct mg_connection *s_sntp_conn = NULL;
// SNTP client callback
static void sfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data)
{
if (ev == MG_EV_SNTP_TIME)
{
int64_t t = *(int64_t *) ev_data;
MG_INFO(("Got SNTP time: %lld ms from epoch", t));
}
else if (ev == MG_EV_CLOSE)
{
s_sntp_conn = NULL;
}
(void) fn_data;
(void) c;
}
// Called every 5 seconds. Increase that for production case.
static void timer_fn(void *arg)
{
struct mg_mgr *mgr = (struct mg_mgr *) arg;
if (s_sntp_conn == NULL) s_sntp_conn = mg_sntp_connect(mgr, "udp://edu.ntp.org.cn:123", sfn, NULL);
if (s_sntp_conn != NULL) mg_sntp_request(s_sntp_conn);
}
int main(void)
{
struct mg_mgr mgr; // Event manager
mg_mgr_init(&mgr); // Initialise event manager
mg_log_set(MG_LL_DEBUG); // Set log level
mg_timer_add(&mgr, 5000, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_fn, &mgr);
for (;;) mg_mgr_poll(&mgr, 300); // Infinite event loop
mg_mgr_free(&mgr); // Free manager resources
return 0;
}
测试结果:
四、公共 NTP 网络时间服务器
来源:
https://dns.icoa.cn/ntp/
欢迎关注公众号:嵌入式学习与实践