sntp.sgml

来自「eCos操作系统源码」· SGML 代码 · 共 208 行

SGML
208
字号
<!-- {{{ Banner                         --><!-- =============================================================== --><!--                                                                 --><!--     sntp.sgml                                                   --><!--                                                                 --><!--     Simple Network Time Protocol Client                         --><!--                                                                 --><!-- =============================================================== --><!-- ####COPYRIGHTBEGIN####                                          --><!--                                                                 --><!-- =============================================================== --><!-- Copyright (C) 2003 Andrew Lunn                                  --><!-- This material may be distributed only subject to the terms      --><!-- and conditions set forth in the Open Publication License, v1.0  --><!-- or later (the latest version is presently available at          --><!-- http://www.opencontent.org/openpub/)                            --><!-- Distribution of the work or derivative of the work in any       --><!-- standard (paper) book form is prohibited unless prior           --><!-- permission obtained from the copyright holder                   --><!-- =============================================================== --><!--                                                                 -->      <!-- ####COPYRIGHTEND####                                            --><!-- =============================================================== --><!-- #####DESCRIPTIONBEGIN####                                       --><!--                                                                 --><!-- ####DESCRIPTIONEND####                                          --><!-- =============================================================== --><!-- }}} --><PART ID="net-sntp-client"><TITLE>Simple Network Time Protocol Client</TITLE><PARTINTRO><PARA>The SNTP package provides implementation of a client for RFC 2030, theSimple Network Time Protocol (SNTP). The client listens for broadcastsor IPv6 multicasts from an NTP server and uses the information received toset the system clock.  It can also be configured to send SNTP timerequests to specific NTP servers using SNTP's unicast mode.</PARA></PARTINTRO><CHAPTER id="net-sntp"><TITLE>The SNTP Client</TITLE><SECT1 id="sntp-starting"><TITLE>Starting the SNTP client</TITLE><para>The sntp client is implemented as a thread which listens for NTPbroadcasts and IPv6 multicasts, and optionally sends SNTP unicastrequests to specific NTP servers. This thread may be automaticallystarted by the system if it receives a list of (S)NTP servers from theDHCP server and unicast mode is enabled. Otherwise it must be startedby the user application. The header file<filename>cyg/sntp/sntp.h</filename> declares the function to becalled.  The thread is then started by calling the function:</para><programlisting>void cyg_sntp_start(void);</programlisting><para>It is safe to call this function multiple times. Once started, thethread will run forever.</para></sect1><sect1 id="net-sntp-operation"><title>What it does</title><para>The SNTP client listens for NTP IPv4 broadcasts from any NTP servers,or IPv6 multicasts using the address fe0x:0X::101, where X can be2 (Link Local), 5 (Site-Local) or 0xe (Global). Suchpackets contain a timestamp indicating the current time. The packetalso contains information about where the server is in the hierarchyof time servers. A server at the root of the time server tree normallyhas an atomic clock. Such a server is said to be at stratum 0. A timeserver which is synchronised to a stratum 0 server is said to be atstratum 1 etc. The client will accept any NTP packets fromservers using version 3 or 4 of the protocol. When receiving packetsfrom multiple servers, it will use the packets from the server withthe lowest stratum. However, if there are no packets from this serverfor 10 minutes and another server is sending packets, the client willchange servers.</para><para>If SNTP unicast mode is enabled via the CYGPKG_NET_SNTP_UNICASToption, the SNTP client can additionally be configured with a listof specific NTP servers to query.  The general algorithm is as follows: ifthe system clock has not yet been set via an NTP time update, thenthe client will send out NTP requests every 30 seconds to allconfigured NTP servers.  Once an NTP time update has been received,the client will send out additional NTP requests every 30 minutesin order to update the system clock.  These requests are resentevery 30 seconds until a response is received.</para><para>The system clock in eCos is accurate to 1 second. The SNTP client willchange the system clock when the time difference with the receivedtimestamp is greater than 2 seconds. The change is made as a step. </para></sect1><sect1 id="net-sntp-unicast"><title>Configuring the unicast list of NTP servers</title><para>If SNTP unicast mode is enabled via the CYGPKG_NET_SNTP_UNICASToption, the SNTP client can be configured with a list ofNTP servers to contact for time updates.</para><para>By default, this list is configured with NTP server informationreceived from DHCP.  The number of NTP servers that are extractedfrom DHCP can be configured with the CYGOPT_NET_SNTP_UNICAST_MAXDHCPoption.  This option can also be used to disable DHCP usage entirely.</para><para>The list of NTP servers can be manually configured with the followingAPI function.  Note that manual configuration will override anyservers that were automatically configured by DHCP. But laterreconfigurations by DHCP will override manual configurations. Hence itis not recommended to manually configure servers whenCYGOPT_NET_SNTP_UNICAST is enabled.</para><programlisting>#include &lt;cyg/sntp/sntp.h&gt;void cyg_sntp_set_servers(struct sockaddr *server_list, cyg_uint32 num_servers);</programlisting><para>This function takes an array of sockaddr structures specifying theIP address and UDP port of each NTP server to query.  Currently,both IPv4 and IPv6 sockaddr structures are supported.  Thenum_servers argument specifies how many sockaddr's are containedin the array.  The server_list array must be maintained by the caller.Once the array is registered with this function, it must not bemodified by the caller until it is replaced or unregisteredby another call to this function.</para><para>Calling this function with a server_list of NULL and a num_serversvalue of 0 unregisters any previously configured server_list array.</para><para>Finally, note that if this function is called with a non-empty serverlist, it will implicitly start the SNTP client if it has not alreadybeen started (i.e. it will call cyg_sntp_start()).</para></sect1><sect1 id="net-sntp-warning"><title>Warning: timestamp wrap around</title><para>The timestamp in the NTP packet is a 32bit integer which representsthe number of seconds after 00:00 01/01/1900. This 32bit number willwrap around at 06:28:16 Feb 7 2036. At this point in time, the eCostime will jump back to around 00:00:00 Jan 1 1900 when the nextNTP packet is received. </para><para>YOU HAVE BEEN WARNED!</para></SECT1><sect1 id="net-sntp-testing"><title>The SNTP test program</title><para>The SNTP package contains a simple test program. Testing an SNTPclient is not easy, so the test program should be considered as more aproof of concept. It shows that an NTP packet has been received,and is accurate to within a few days. </para><para>The test program starts the network interfaces using the standardcall. It then starts the SNTP thread. A loop is then entered printingthe current system time every second for two minutes. When the clientreceives an NTP packet the time will jump from 1970 to hopefully thepresent day. Once the two minutes have expired, two simple tests aremade. If the time is still less than 5 minutes since 00:00:0001/01/1970 the test fails. This indicates no NTP messages havebeen received. Check that the server is actually sending packet, usingthe correct port (123), correct IPv6 multicast address, and at asufficiently frequent rate that thetarget has a chance to receive a message within the 2 minuteinterval. If all this is correct, assume the target is broken. </para><para>The second test is that the current system time is compared with thebuild time as reported by the CPP macro __DATE__. If the build date isin the future relative to the system time, the test fails. If thebuild date is more than 90 days in the past relative to the systemtime the test also fails. If such failures are seen, use walk-clocktime to verify the time printed during the test. If this seems correctcheck the build date for the test. This is printed at startup. If allelse fails check that the computer used to build the test has thecorrect time. </para><para>If SNTP unicast mode is enabled, the above tests are run twice.  Thefirst time, the SNTP client is configured with NTP server addressesfrom DHCP.  The second time, unicast mode is disabled and onlymulticasts are listened for.  Note that the unicast test is partiallybogus in the sense that any multicast packet received will also makethe unicast test pass.  To reduce the chance of this happening thetest will wait for a sorter time for replies. This is not ideal, butit is the best that can be done with an automated test.</para></SECT1></CHAPTER></PART>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?