<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="ntp4.book.css" type="text/css" charset="UTF-8"?>
<XML>
<TITLE> </TITLE><Body>
<A ID="pgfId-933658"></A>
NTP Working Group	David L. Mills
Technical Report 06-6-1	 University of Delaware
	June 2006	</Body>
<ReportTitle>
<A ID="pgfId-938476"></A>
Network Time Protocol Version 4
Reference and Implementation Guide</ReportTitle>
<ReportAuthor>
<A ID="pgfId-933801"></A>
Abstract</ReportAuthor>
<Body>
<A ID="pgfId-934034"></A>
This document describes the Network Time Protocol Version 4 (NTPv4), which is widely used to synchronize the time for Internet hosts, routers and ancillary devices to Coordinated Universal Time (UTC) as disseminated by national standards laboratories. It describes the core architecture, protocol, state machine, data structures and algorithms. It explains the fundamental on-wire protocol used to exchange time values between peers, servers and clients. It summarizes the clock offset, roundtrip delay and various other statistics used by the mitigation algorithms to calculate the maximum error and nominal error inherent in computing these values. It describes several changes from Version 3 of NTP (NTPv3) originally described in RFC 1305, including the introduction of a modified protocol header to accommodate Internet Protocol Version 6 and a new header extension field to support the Autokey public key authentication scheme.</Body>
<Body>
<A ID="pgfId-932397"></A>
This document is based on the reference implementation available at www.ntp.org. It is intended as a reference and implementation guide, not as a formal standard. The main body of the document describes the basic model data structures and algorithms necessary for an implementation which can interoperate properly with another implementation faithful to this model. This document describes a number of crafted mitigation algorithms which can improve the accuracy and stability of the timekeeping function, especially in NTP subnets with many servers and clients. It also describes the clock discipline function used to adjust the system clock in time and frequency to agree with the available sources of synchronization.</Body>
<Body>
<A ID="pgfId-934875"></A>
As an implementation aid, a code skeleton for the reference implementation is presented in an appendix. It includes most of the data structures and algorithms of that program, but certain features, such as the control and monitoring protocol, Autokey public key authentication scheme, huff-’n-puff scheme and server discovery schemes are not included. These are discussed in companion documents on the Web and in print.</Body>
<Body>
<A ID="pgfId-931852"></A>
Keywords: network time synchronization, computer time synchronization, time synchronization protocol</Body>
<Body>
<A ID="pgfId-932378"></A>
Sponsored by: Naval Surface Weapons Center (Dahlgren) Contract N00178-04-1-9001.</Body>
<ReportAuthor>
<A ID="pgfId-919822"></A>
Table of Contents</ReportAuthor>
<Heading1TOC>
<A ID="pgfId-942174"></A>
1.	Introduction	1</Heading1TOC>
<Heading1TOC>
<A ID="pgfId-942175"></A>
2.	Modes of Operation	2</Heading1TOC>
<Heading1TOC>
<A ID="pgfId-942176"></A>
3.	Definitions	4</Heading1TOC>
<Heading1TOC>
<A ID="pgfId-942177"></A>
4.	Implementation Model	5</Heading1TOC>
<Heading1TOC>
<A ID="pgfId-942178"></A>
5.	Data Types	6</Heading1TOC>
<Heading1TOC>
<A ID="pgfId-942179"></A>
6.	Data Structures	9</Heading1TOC>
<Heading2TOC>
<A ID="pgfId-942180"></A>
6.1	Structure Conventions	9</Heading2TOC>
<Heading2TOC>
<A ID="pgfId-942181"></A>
6.2	Global Parameters	10</Heading2TOC>
<Heading2TOC>
<A ID="pgfId-942182"></A>
6.3	Packet Header Variables	11</Heading2TOC>
<Heading1TOC>
<A ID="pgfId-942183"></A>
7.	On-Wire Protocol	14</Heading1TOC>
<Heading1TOC>
<A ID="pgfId-942184"></A>
8.	Peer Process	16</Heading1TOC>
<Heading2TOC>
<A ID="pgfId-942185"></A>
8.1	Peer Process Variables	16</Heading2TOC>
<Heading2TOC>
<A ID="pgfId-942186"></A>
8.2	Peer Process Operations	18</Heading2TOC>
<Heading2TOC>
<A ID="pgfId-942187"></A>
8.3	Clock Filter Algorithm	22</Heading2TOC>
<Heading1TOC>
<A ID="pgfId-942188"></A>
9.	System Process	24</Heading1TOC>
<Heading2TOC>
<A ID="pgfId-942189"></A>
9.1	System Process Variables	24</Heading2TOC>
<Heading2TOC>
<A ID="pgfId-942190"></A>
9.2	System Process Operations	25</Heading2TOC>
<Heading3TOC>
<A ID="pgfId-942191"></A>
9.2.1	Selection Algorithm	26</Heading3TOC>
<Heading3TOC>
<A ID="pgfId-942192"></A>
9.2.2	Clustering Algorithm	28</Heading3TOC>
<Heading3TOC>
<A ID="pgfId-942193"></A>
9.2.3	Combining Algorithm	29</Heading3TOC>
<Heading3TOC>
<A ID="pgfId-942194"></A>
9.2.4	Clock Discipline Algorithm	31</Heading3TOC>
<Heading2TOC>
<A ID="pgfId-942195"></A>
9.3	Clock Adjust Process	36</Heading2TOC>
<Heading1TOC>
<A ID="pgfId-942196"></A>
10.	Poll Process	36</Heading1TOC>
<Heading2TOC>
<A ID="pgfId-942197"></A>
10.1	Poll Process Variables and Parameters	37</Heading2TOC>
<Heading2TOC>
<A ID="pgfId-942198"></A>
10.2	Poll Process Operations	37</Heading2TOC>
<Heading1TOC>
<A ID="pgfId-942199"></A>
11.	References	39</Heading1TOC>
<Heading1TOC>
<A ID="pgfId-942200"></A>
Appendix A.	NTPv4 Packet Formats	41</Heading1TOC>
<Heading2TOC>
<A ID="pgfId-942201"></A>
A.1	NTP Header Field Format	41</Heading2TOC>
<Heading2TOC>
<A ID="pgfId-942202"></A>
A.2	NTPv4 Extension Field Format	42</Heading2TOC>
<Heading1TOC>
<A ID="pgfId-942203"></A>
Appendix B.	Code Skeleton	44</Heading1TOC>
<Heading2TOC>
<A ID="pgfId-942204"></A>
B.1	Global Definitions	44</Heading2TOC>
<Heading3TOC>
<A ID="pgfId-942205"></A>
B.1.1	Definitions, Constants and Parameters	44</Heading3TOC>
<Heading3TOC>
<A ID="pgfId-942206"></A>
B.1.2	Packet Data Structures	47</Heading3TOC>
<Heading3TOC>
<A ID="pgfId-942207"></A>
B.1.3	Association Data Structures	48</Heading3TOC>
<Heading3TOC>
<A ID="pgfId-942208"></A>
B.1.4	System Data Structures	49</Heading3TOC>
<Heading3TOC>
<A ID="pgfId-942209"></A>
B.1.5	Local Clock Data Structure	50</Heading3TOC>
<Heading3TOC>
<A ID="pgfId-942210"></A>
B.1.6	Function Prototypes	50</Heading3TOC>
<Heading2TOC>
<A ID="pgfId-942211"></A>
B.2	Main Program and Utility Routines	51</Heading2TOC>
<Heading2TOC>
<A ID="pgfId-942212"></A>
B.3	Kernel Input/Output Interface	55</Heading2TOC>
<Heading2TOC>
<A ID="pgfId-942213"></A>
B.4	Kernel System Clock Interface	55</Heading2TOC>
<Heading2TOC>
<A ID="pgfId-942214"></A>
B.5	Peer Process	57</Heading2TOC>
<Heading3TOC>
<A ID="pgfId-942215"></A>
B.5.1	receive()	58</Heading3TOC>
<Heading3TOC>
<A ID="pgfId-942216"></A>
B.5.2	packet()	62</Heading3TOC>
<Heading3TOC>
<A ID="pgfId-942217"></A>
B.5.3	clock_filter()	64</Heading3TOC>
<Heading3TOC>
<A ID="pgfId-942218"></A>
B.5.4	fast_xmit()	65</Heading3TOC>
<Heading3TOC>
<A ID="pgfId-942219"></A>
B.5.5	access()	66</Heading3TOC>
<Heading2TOC>
<A ID="pgfId-942220"></A>
B.6	System Process	66</Heading2TOC>
<Heading3TOC>
<A ID="pgfId-942221"></A>
B.6.1	clock_select()	67</Heading3TOC>
<Heading3TOC>
<A ID="pgfId-942222"></A>
B.6.2	root_dist()	70</Heading3TOC>
<Heading3TOC>
<A ID="pgfId-942223"></A>
B.6.3	accept()	70</Heading3TOC>
<Heading3TOC>
<A ID="pgfId-942224"></A>
B.6.4	clock_update()	71</Heading3TOC>
<Heading3TOC>
<A ID="pgfId-942225"></A>
B.6.5	clock_combine()	73</Heading3TOC>
<Heading3TOC>
<A ID="pgfId-942226"></A>
B.6.6	local_clock()	73</Heading3TOC>
<Heading3TOC>
<A ID="pgfId-942227"></A>
B.6.7	rstclock()	78</Heading3TOC>
<Heading2TOC>
<A ID="pgfId-942228"></A>
B.7	Clock Adjust Process	79</Heading2TOC>
<Heading3TOC>
<A ID="pgfId-942229"></A>
B.7.1	clock_adjust()	79</Heading3TOC>
<Heading2TOC>
<A ID="pgfId-942230"></A>
B.8	Poll Process	80</Heading2TOC>
<Heading3TOC>
<A ID="pgfId-942231"></A>
B.8.1	poll()	80</Heading3TOC>
<Heading3TOC>
<A ID="pgfId-942232"></A>
B.8.2	poll_update()	82</Heading3TOC>
<Heading3TOC>
<A ID="pgfId-942233"></A>
B.8.3	transmit()	83</Heading3TOC>
<ReportAuthor>
<A ID="pgfId-572332"></A>
List of Figures</ReportAuthor>
<FigureLOF>
<A ID="pgfId-595836"></A>
<A href="ntp4.xml#id(pgfId-1012456)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Figure 1.	Implementation Model	5</A></FigureLOF>
<FigureLOF>
<A ID="pgfId-595838"></A>
<A href="ntp4.xml#id(pgfId-998356)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Figure 2.	NTP Time Formats	7</A></FigureLOF>
<FigureLOF>
<A ID="pgfId-595840"></A>
<A href="ntp4.xml#id(pgfId-998413)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Figure 3.	On-Wire Protocol	14</A></FigureLOF>
<FigureLOF>
<A ID="pgfId-595842"></A>
<A href="ntp4.xml#id(pgfId-1003914)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Figure 4.	Receive Processing	18</A></FigureLOF>
<FigureLOF>
<A ID="pgfId-595844"></A>
<A href="ntp4.xml#id(pgfId-1004100)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Figure 5.	Client Packet Processing	19</A></FigureLOF>
<FigureLOF>
<A ID="pgfId-595846"></A>
<A href="ntp4.xml#id(pgfId-1003971)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Figure 6.	Timestamp Processing	20</A></FigureLOF>
<FigureLOF>
<A ID="pgfId-595848"></A>
<A href="ntp4.xml#id(pgfId-998475)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Figure 7.	Packet Processing	20</A></FigureLOF>
<FigureLOF>
<A ID="pgfId-595850"></A>
<A href="ntp4.xml#id(pgfId-1005994)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Figure 8.	Clock Filter Processing	22</A></FigureLOF>
<FigureLOF>
<A ID="pgfId-595852"></A>
<A href="ntp4.xml#id(pgfId-1014630)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Figure 9.	clock_select() Routine	26</A></FigureLOF>
<FigureLOF>
<A ID="pgfId-595854"></A>
<A href="ntp4.xml#id(pgfId-1014476)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Figure 10.	Selection Algorithm	27</A></FigureLOF>
<FigureLOF>
<A ID="pgfId-595856"></A>
<A href="ntp4.xml#id(pgfId-1003873)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Figure 11.	accept() Routine	27</A></FigureLOF>
<FigureLOF>
<A ID="pgfId-595858"></A>
<A href="ntp4.xml#id(pgfId-1014503)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Figure 12.	Clustering Algorithm	28</A></FigureLOF>
<FigureLOF>
<A ID="pgfId-595860"></A>
<A href="ntp4.xml#id(pgfId-1014557)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Figure 13.	clock_combine() Routine	29</A></FigureLOF>
<FigureLOF>
<A ID="pgfId-595862"></A>
<A href="ntp4.xml#id(pgfId-1015488)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Figure 14.	clock_update() Routine	29</A></FigureLOF>
<FigureLOF>
<A ID="pgfId-595864"></A>
<A href="ntp4.xml#id(pgfId-1015628)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Figure 15.	System Variables Processing	30</A></FigureLOF>
<FigureLOF>
<A ID="pgfId-595866"></A>
<A href="ntp4.xml#id(pgfId-1013637)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Figure 16.	Clock Discipline Feedback Loop	31</A></FigureLOF>
<FigureLOF>
<A ID="pgfId-595868"></A>
<A href="ntp4.xml#id(pgfId-1013659)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Figure 17.	Clock Discipline Loop Filter	32</A></FigureLOF>
<FigureLOF>
<A ID="pgfId-595870"></A>
<A href="ntp4.xml#id(pgfId-1015728)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Figure 18.	local_clock() Routine (1 of 2)	34</A></FigureLOF>
<FigureLOF>
<A ID="pgfId-595872"></A>
<A href="ntp4.xml#id(pgfId-1015933)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Figure 19.	local_clock() Routine (2 of 2)	35</A></FigureLOF>
<FigureLOF>
<A ID="pgfId-595874"></A>
<A href="ntp4.xml#id(pgfId-1015702)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Figure 20.	clock_adjust() Routine	36</A></FigureLOF>
<FigureLOF>
<A ID="pgfId-595876"></A>
<A href="ntp4.xml#id(pgfId-1007728)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Figure 21.	poll() Routine	38</A></FigureLOF>
<FigureLOF>
<A ID="pgfId-595878"></A>
<A href="ntp4.xml#id(pgfId-998533)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Figure 22.	transmit() Routine	39</A></FigureLOF>
<FigureLOF>
<A ID="pgfId-595880"></A>
<A href="ntp4.xml#id(pgfId-1016116)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Figure 23.	poll_update() Routine	39</A></FigureLOF>
<FigureLOF>
<A ID="pgfId-595882"></A>
<A href="ntp4apx.xml#id(pgfId-947429)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Figure 24.	NPv4 Header Format	41</A></FigureLOF>
<FigureLOF>
<A ID="pgfId-595884"></A>
<A href="ntp4apx.xml#id(pgfId-960174)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Figure 25.	NTPv4 Extension Field Format	42</A></FigureLOF>
<ReportAuthor>
<A ID="pgfId-164030"></A>
<A href="ntp4apx.xml#id(pgfId-938652)" xml:link="simple" show="replace" actuate="user" CLASS="Index">List of Tables</A></ReportAuthor>
<TableLOT>
<A ID="pgfId-180808"></A>
<A href="ntp4.xml#id(pgfId-1001802)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Table 1.	Association and Packet Modes	3</A></TableLOT>
<TableLOT>
<A ID="pgfId-180810"></A>
<A href="ntp4.xml#id(pgfId-1019010)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Table 2.	Interesting Historic NTP Dates	8</A></TableLOT>
<TableLOT>
<A ID="pgfId-180812"></A>
<A href="ntp4.xml#id(pgfId-998368)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Table 3.	Name Prefix Conventions	10</A></TableLOT>
<TableLOT>
<A ID="pgfId-180814"></A>
<A href="ntp4.xml#id(pgfId-1001266)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Table 4.	Global Parameters	10</A></TableLOT>
<TableLOT>
<A ID="pgfId-180816"></A>
<A href="ntp4.xml#id(pgfId-998381)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Table 5.	Packet Header Variables	11</A></TableLOT>
<TableLOT>
<A ID="pgfId-180818"></A>
<A href="ntp4.xml#id(pgfId-998438)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Table 6.	Peer Process Variables	17</A></TableLOT>
<TableLOT>
<A ID="pgfId-180820"></A>
<A href="ntp4.xml#id(pgfId-1014110)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Table 7.	Packet Error Checks	21</A></TableLOT>
<TableLOT>
<A ID="pgfId-180822"></A>
<A href="ntp4.xml#id(pgfId-998500)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Table 8.	System Process Variables and Parameters	25</A></TableLOT>
<TableLOT>
<A ID="pgfId-180824"></A>
<A href="ntp4.xml#id(pgfId-1018208)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Table 9.	Clock Discipline Variables and Parameters	33</A></TableLOT>
<TableLOT>
<A ID="pgfId-180826"></A>
<A href="ntp4.xml#id(pgfId-1015821)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Table 10.	Clock Discipline State Transition Table	33</A></TableLOT>
<TableLOT>
<A ID="pgfId-180828"></A>
<A href="ntp4.xml#id(pgfId-998519)" xml:link="simple" show="replace" actuate="user" CLASS="Index">Table 11.	Poll Process Variables and Parameters	37</A></TableLOT>
<Heading1>
<A ID="pgfId-998302"></A>
Introduction</Heading1>
<Body>
<A ID="pgfId-1005657"></A>
This document is a reference and implementation guide for the Network Time Protocol Version 4 (NTPv4), which is widely used to synchronize the system clocks among a set of distributed time servers and clients. This document defines the core architecture, protocol, state machines, data structures and algorithms. It is intended primarily for developers and not as a formal standard. This and related documents collectively replace the Network Time Protocol Version 3 (NTPv3) specification RFC 1305 <A href="ntp4.book.xml#id(47618)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">[6]</A> and previous versions of that specification. The core protocol continues to be compatible with all prior versions except the original (unnumbered) version of the protocol. While certain minor changes have been made in some protocol header fields, these do not affect the interoperability between NTPv4 and previous versions.</Body>
<Body>
<A ID="pgfId-1007140"></A>
The NTP subnet model includes a number of widely accessible primary time servers synchronized by wire or radio to national standards. The purpose of the NTP protocol is to convey timekeeping information from these primary servers to secondary time servers and clients via both private networks and the public Internet. Crafted algorithms mitigate errors that may result from network disruptions, server failures and possible hostile action. Servers and clients are configured as a forest where time values flow from the primary servers at the root via branching secondary servers toward clients at the leaves of the forest.</Body>
<Body>
<A ID="pgfId-998303"></A>
The NTPv4 reference implementation available at www.ntp.org is consistent with the model described in this document. However, neither this implementation nor this document is intended as a definitive formal standard. Formal standards documents consistent with IETF requirements are in process at the time of writing. When a conflict is found between this document and the formal standards documents, the latter prevail.</Body>
<Body>
<A ID="pgfId-1013912"></A>
The NTPv4 design overcomes significant shortcomings in the NTPv3 design, corrects certain bugs and incorporates new features. In particular, the reference implementation uses floating double data types throughout, except for the first-order timestamp differences required to calculate offset and delay. The time resolution is better than one nanosecond and frequency resolution better than one nanosecond per second. Additional improvements include a new clock discipline algorithm which is more responsive to system clock hardware frequency fluctuations. Typical primary servers using modern machines are precise within a few tens of microseconds. Typical secondary servers and clients on fast LANs are within a few hundred microseconds with poll intervals up to 1024 seconds, which was the maximum with NTPv3. With NTPv4, servers and clients are within a few tens of milliseconds with poll intervals up to 36 hours.</Body>
<Body>
<A ID="pgfId-998307"></A>
The main body of this document describes only the core protocol and data structures necessary to interoperate between conforming implementations. Additional detail is provided in the form of a skeleton program included as an appendix. This program includes data structures and code segments for the core algorithms and in addition the mitigation algorithms used to enhance reliability and accuracy. While the skeleton and other descriptions in this document apply to a particular implementation, they are not intended as the only way the required functions can be implemented. While the NTPv3 symmetric key authentication scheme described in this document carries over from NTPv3, the Autokey public key authentication scheme new to NTPv4 is described in <A href="ntp4.book.xml#id(91215)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">[3]</A>.</Body>
<Body>
<A ID="pgfId-1013955"></A>
The NTP protocol includes the modes of operation described in Section <A href="ntp4.book.xml#id(25843)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">2</A> using the data types described in Section <A href="ntp4.book.xml#id(58649)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">5</A> and the data structures in Section <A href="ntp4.book.xml#id(54150)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">6</A>. The implementation model described in Section <A href="ntp4.book.xml#id(65086)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">4</A> is based on a multiple-process, threaded architecture, although other architectures could be used as well. The on-wire protocol described in Section <A href="ntp4.book.xml#id(69016)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">7</A> is based on a returnable-time design which depends only on measured clock offsets, but does not require reliable message delivery. The synchronization subnet is a self-organizing, hierarchical, master-slave network with synchronization paths determined by a shortest-path spanning tree and defined metric. While multiple masters (primary servers) may exist, there is no requirement for an election protocol.</Body>
<Body>
<A ID="pgfId-998308"></A>
This remaining sections of this document define the data structures and algorithms suitable for a fully featured NTPv4 implementation. Details specific to NTP packet formats commonly used with the User Datagram Protocol (UDP) are presented in Appendix <A href="ntp4.book.xml#id(47726)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">A</A>. Appendix B contains the code skeleton with definitions, structures and code segments that represent the basic structure of the reference implementation.</Body>
<Heading1>
<A ID="pgfId-998311"></A>
<A ID="25843"></A>
Modes of Operation</Heading1>
<Body>
<A ID="pgfId-1013964"></A>
An NTP implementation operates as a primary server, secondary server or client. A primary server is synchronized directly to a reference clock, such as a GPS receiver or telephone modem service. A client is synchronized to one or more upstream servers, but does not provide synchronization to dependent clients. A secondary server has one or more upstream servers and one or more downstream servers or clients. All servers and clients claiming full NTPv4 compliance must implement the entire suite of algorithms described in this document. In order to maintain stability in large NTP subnets, secondary servers must be fully NTPv4 compliant.</Body>
<Body>
<A ID="pgfId-1016789"></A>
Primary servers and clients complying with a subset of NTP, called the Simple Network Time Protocol (SNTPv4) <A href="ntp4.book.xml#id(96511)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">[4]</A>, do not need to implement all algorithms. SNTP is intended for primary servers equipped with a single reference clock, as well as clients with a single upstream server and no dependent clients. The fully developed NTPv4 implementation is intended for secondary servers with multiple upstream servers and multiple downstream servers or clients. Other than these considerations, NTP and SNTP servers and clients are completely interoperable and can be mixed and matched in NTP subnest.</Body>
<Body>
<A ID="pgfId-1001866"></A>
Servers retain no state after returning the response to a client packet; clients retain state in the form of a data structure called an association. Persistent associations are mobilized when the service starts and are never demobilized. Ephemeral associations are mobilized during operation, such as upon the arrival of a broadcast packet, and demobilized by timeout or error. Preemptable associations are mobilized when or after the service starts and demobilized when deemed no longer useful for synchronization. The code skeleton and reference implementation summarized in Appendix B includes suitable algorithms for ephemeral and preemptable associations, but they are not discussed in the main body of this document.</Body>
<Body>
<A ID="pgfId-998313"></A>
There are three NTP protocol variants, symmetric, client/server and broadcast. Each is associated with an association mode as shown in Table <A href="ntp4.xml#id(45878)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">1</A>. In the client/server variant a client association sends mode-3 (client) packets to a server, which returns mode-4 (server) packets. Servers provide synchronization to one or more clients, but do not accept synchronization from them. A server can also be a reference clock which obtains time directly from a standard source such as a GPS receiver or telephone modem service. We say that clients pull synchronization from servers.</Body>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-1.gif" show="embed" actuate="auto"/>
</DIV>
<Body>
<A ID="pgfId-1001743"></A>
In the symmetric variant a peer operates as both a server and client using either a symmetric-active or symmetric-passive association. A symmetric-active association sends mode-1 (symmetric-active) packets to a symmetric-active peer association. Alternatively, a symmetric-passive association can be mobilized upon arrival of a mode-1 packet. That association sends mode-2 (symmetric-passive) packets and persists until error or timeout. We say that peers both push and pull synchronization to and from each other. For the purposes of this document, a peer operates like a client, so a reference to client implies peer as well.</Body>
<Body>
<A ID="pgfId-1004828"></A>
In the broadcast variant a broadcast server association sends periodic mode-5 (broadcast) packets which are received by multiple mode-6 (broadcast client) associations. It is useful to provide an initial volley where the client operating in mode 3 exchanges several packets with the server in order to calibrate the propagation delay and to run the Autokey security protocol, after which the client reverts to mode 6. We say that broadcast servers push synchronization to willing consumers.</Body>
<Body>
<A ID="pgfId-998314"></A>
Following conventions established by the telephone industry, the level of each server in the hierarchy is defined by a number called the stratum, with the primary servers assigned stratum one and the secondary servers at each level assigned one greater than the preceding level. As the stratum increases from one, the accuracies achievable degrade somewhat depending on the particular network path and system clock stability. It is useful to assume that mean errors, and thus a metric called the synchronization distance, increase approximately in proportion to the stratum and measured roundtrip delay.</Body>
<Body>
<A ID="pgfId-998315"></A>
Drawing from the experience of the telephone industry, which learned such lessons at considerable cost, the subnet topology should be organized to produce the lowest synchronization distances, but must never be allowed to form a loop. In NTP the subnet topology is determined using a variant of the Bellman-Ford distributed routing algorithm, which computes the shortest-distance spanning tree rooted on the primary servers. As a result of this design, the algorithm automatically reorganizes the subnet to produce the most accurate and reliable time, even when one or more primary or secondary servers or the network paths between them fail.</Body>
<Heading1>
<A ID="pgfId-998316"></A>
Definitions</Heading1>
<Body>
<A ID="pgfId-998317"></A>
A number of terms used throughout this document have a precise technical definition. A timescale is a frame of reference where time is expressed as the value of a monotonic-increasing binary counter with an indefinite number of bits. It counts in seconds and fraction with the decimal point somewhere in the middle. The Coordinated Universal Time (UTC) timescale represents mean solar time as disseminated by national standards laboratories. The system time is represented by the system clock maintained by the operating system kernel. The goal of the NTP algorithms is to minimize both the time difference and frequency difference between UTC and the system clock. When these differences have been reduced below nominal tolerances, the system clock is said to be synchronized to UTC.</Body>
<Body>
<A ID="pgfId-998318"></A>
The date of an event is the UTC time at which it takes place. Dates are ephemeral values which always increase in step with reality and are designated with upper case T in this document. It is convenient to define another timescale coincident with the running time of the NTP program that provides the synchronization function. This is convenient in order to determine intervals for the various repetitive functions like poll events. Running time is usually designated with lower case t.</Body>
<Body>
<A ID="pgfId-998319"></A>
A timestamp T(t) represents either the UTC date or time offset from UTC at running time t. Which meaning is intended should be clear from context. Let T(t) be the time offset, R(t) the frequency offset, D(t) the ageing rate (first derivative of R(t) with respect to t). Then, if T(t0) is the UTC time offset determined at t = t0, the UTC time offset after some interval t is</Body>
<Equation>
<A ID="pgfId-998323"></A>
<IMAGE xml:link="simple" href="ntp4.book-2.gif" show="embed" actuate="auto"/>
,</Equation>
<Body>
<A ID="pgfId-998324"></A>
where e is a stochastic error term discussed later in this document. While the D(t) term is important when characterizing precision oscillators, it is ordinary neglected for computer oscillators. In this document all time values are in seconds (s) and all frequency values in seconds-per- second (s/s). It is sometimes convenient to express frequency offsets in parts-per-million (PPM), where 1 PPM is equal to 1e<Symbol>
−</Symbol>
6 s/s.</Body>
<Body>
<A ID="pgfId-998325"></A>
It is important in computer timekeeping applications to assess the performance of the timekeeping function. The NTP performance model includes four statistics which are updated each time a client makes a measurement with a server. The offset θ represents the maximum-likelihood time offset of the server clock relative to the system clock. The delay δ represents the roundtrip delay between the client and server. The dispersion ε represents the maximum error inherent in the measurement. It increases at a rate equal to the maximum disciplined system clock frequency tolerance Φ, typically 15 PPM. The jitter ϕ, defined as the root-mean-square (RMS) average of the most recent time offset differences, represents the nominal error in estimating θ.</Body>
<Body>
<A ID="pgfId-1016889"></A>
While the <Symbol>
θ</Symbol>
, <Symbol>
δ</Symbol>
, <Symbol>
ε</Symbol>
, and <Symbol>
ϕ</Symbol>
 statistics represent measurements of the system clock relative to the each server clock separately, the NTP protocol includes mechanisms to combine the statistics of several servers to more accurately discipline and calibrate the system clock. The system offset <Symbol>
Θ</Symbol>
 represents the maximum-likelihood offset estimate for the server population. The system jitter <Symbol>
ϑ</Symbol>
 represents the nominal error in estimating <Symbol>
Θ</Symbol>
. The <Symbol>
δ</Symbol>
 and <Symbol>
ε</Symbol>
 statistics are accumulated at each stratum level from the reference clocks to produce the root delay <Symbol>
Δ</Symbol>
 and root dispersion <Symbol>
Ε </Symbol>
statistics<Symbol>
. </Symbol>
The synchronization distance <IMAGE xml:link="simple" href="ntp4.book-3.gif" show="embed" actuate="auto"/>
 represents the maximum error due all causes. The detailed formulations of these statistics are given later in this document. They are available to the dependent applications in order to assess the performance of the synchronization function.</Body>
<Heading1>
<A ID="pgfId-1002539"></A>
<A ID="65086"></A>
Implementation Model</Heading1>
<Body>
<A ID="pgfId-1018879"></A>
Figure <A href="ntp4.xml#id(31984)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">1</A> shows two processes dedicated to each server, a peer process to receive messages from the server or reference clock and a poll process to transmit messages to the server or reference clock. These processes operate on a common data structure called an association, which contains the statistics described above along with various other data described later. A client sends an NTP packet to one or more servers and processes the replies as received. The server interchanges addresses and ports, overwrites certain fields in the packet and returns it immediately (client/server mode) or at some time later (symmetric modes). As each NTP message is received, the offset θ between the peer clock and the system clock is computed along with the associated statistics δ, ε and ϕ.</Body>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-4.gif" show="embed" actuate="auto"/>
</DIV>
<Body>
<A ID="pgfId-998344"></A>
The system process includes the selection, clustering and combining algorithms which mitigate among the various servers and reference clocks to determine the most accurate and reliable candidates to synchronize the system clock. The selection algorithm uses Byzantine principles to cull the falsetickers from the incident population leaving the truechimers as result. The clustering algorithm uses statistical principles to sift the most accurate truechimers leaving the survivors as result. The combining algorithm develops the final clock offset as a statistical average of the survivors.</Body>
<Body>
<A ID="pgfId-998345"></A>
The clock discipline process, which is actually part of the system process, includes engineered algorithms to control the time and frequency of the system clock, here represented as a variable frequency oscillator (VFO). Timestamps struck from the VFO close the feedback loop which maintains the system clock time. Associated with the clock discipline process is the clock adjust process, which runs once each second to inject a computed time offset and maintain constant frequency. The RMS average of past time offset differences represents the nominal error or system jitter <Symbol>
ϑ</Symbol>
. The RMS average of past frequency offset differences represents the oscillator frequency stability or frequency wander <Symbol>
Ψ</Symbol>
.</Body>
<Body>
<A ID="pgfId-1013240"></A>
A client sends messages to each server with a poll interval of 2τ seconds, as determined by the poll exponent τ. In NTPv4 τ ranges from 4 (16 s) through 17 (36 h). The value of τ is determined by the clock discipline algorithm to match the loop time constant <IMAGE xml:link="simple" href="ntp4.book-5.gif" show="embed" actuate="auto"/>
. A server responds with messages at an update interval of μ seconds. For stateless servers, <IMAGE xml:link="simple" href="ntp4.book-6.gif" show="embed" actuate="auto"/>
, since the server responds immediately. However, in symmetric modes each of two peers manages the time constant as a function of current system offset and system jitter, so may not agree with the same τ. It is important that the dynamic behavior of the clock discipline algorithms be carefully controlled in order to maintain stability in the NTP subnet at large. This requires that the peers agree on a common τ equal to the minimum poll exponent of both peers. The NTP protocol includes provisions to properly negotiate this value.</Body>
<Body>
<A ID="pgfId-1003178"></A>
While not shown in the figure, the implementation model includes some means to set and adjust the system clock. The operating system is assumed to provide two functions, one to set the time directly, for example the Unix settimeofday() function, and another to adjust the time in small increments advancing or retarding the time by a designated amount, for example the Unix adjtime()<A href="#id(pgfId-1016942)" xml:link="simple" show="replace" actuate="user" CLASS="footnote">1</A> function. In the intended design the clock discipline process uses the adjtime() function if the adjustment is less than a designated threshold, and the settimeofday() function if above the threshold. The manner in which this is done and the value of the threshold is described later.</Body>
<Heading1>
<A ID="pgfId-1003031"></A>
<A ID="58649"></A>
Data Types</Heading1>
<Body>
<A ID="pgfId-998348"></A>
All NTP time values are represented in twos-complement format, with bits numbered in big-endian fashion from zero starting at the left, or high-order, position. There are three NTP time formats, a 128-bit date format, a 64-bit timestamp format and a 32-bit short format, as shown in Figure <A href="ntp4.xml#id(24422)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">2</A>. The 128-bit date format is used where sufficient storage and word size are available. It includes a 64-bit signed seconds field spanning 584 billion years and a 64-bit fraction field resolving .05 attosecond. For convenience in mapping between formats, the seconds field is divided into a 32-bit era field and a 32-bit timestamp field. Eras cannot be produced by NTP directly, nor is there need to do so. When necessary, they can be derived from external means, such as the filesystem or dedicated hardware.</Body>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-7.gif" show="embed" actuate="auto"/>
</DIV>
<Body>
<A ID="pgfId-1004133"></A>
The 64-bit timestamp format is used in packet headers and other places with limited word size. It includes a 32-bit unsigned seconds field spanning 136 years and a 32 bit fraction field resolving 232 picoseconds. The 32-bit short format is used in delay and dispersion header fields where the full resolution and range of the other formats are not justified. It includes a 16-bit unsigned seconds field and a 16-bit fraction field.</Body>
<Body>
<A ID="pgfId-998357"></A>
In the date format the prime epoch, or base date of era 0, is 0 h 1 January 1900 UTC<A href="#id(pgfId-1019028)" xml:link="simple" show="replace" actuate="user" CLASS="footnote">2</A>, when all bits are zero. Dates are relative to the prime epoch; values greater than zero represent times after that date; values less than zero represent times before it. Timestamps are unsigned values and operations on them produce a result in the same or adjacent eras. Era 0 includes dates from the prime epoch to some time in 2036, when the timestamp field wraps around and the base date for era 1 is established. In either format a value of zero is a special case representing unknown or unsynchronized time. Table <A href="ntp4.xml#id(27361)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">2</A> shows a number of historic NTP dates together with their correspondnig Modified Julian Day (MJD), NTP era and NTP timestamp.</Body>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-8.gif" show="embed" actuate="auto"/>
</DIV>
<Body>
<A ID="pgfId-1018955"></A>
Let p be the number of significant bits in the second fraction. The clock resolution is defined <IMAGE xml:link="simple" href="ntp4.book-9.gif" show="embed" actuate="auto"/>
, in seconds. In order to minimize bias and help make timestamps unpredictable to an intruder, the nonsignificant bits should be set to an unbiased random bit string. The clock precision is defined as the running time to read the system clock, in seconds. Note that the precision defined in this way can be larger or smaller than the resolution. The term <Symbol>
ρ,</Symbol>
 representing the precision used in this document, is the larger of the two.</Body>
<Body>
<A ID="pgfId-1003272"></A>
The only operation permitted with dates and timestamps is twos-complement subtraction, yielding a 127-bit or 63-bit signed result. It is critical that the first-order differences between two dates preserve the full 128-bit precision and the first-order differences between two timestamps preserve the full 64-bit precision. However, the differences are ordinarily small compared to the seconds span, so they can be converted to floating double format for further processing and without compromising the precision.</Body>
<Body>
<A ID="pgfId-1008043"></A>
It is important to note that twos-complement arithmetic does not know the difference between signed and unsigned values; only the conditional branch instructions. Thus, although the distinction is made between signed dates and unsigned timestamps, they are processed the same way. A perceived hazard with 64-bit timestamp calculations spanning an era, such as could happen in 2036, might result in incorrect values. In point of fact, if the client is set within 68 years of the server before the protocol is started, correct values are obtained even if the client and server are in adjacent eras. Further discussion on this issue is on the NTP project page linked from www.ntp.org.</Body>
<Body>
<A ID="pgfId-1006349"></A>
Some time values are represented in exponent format, including the precision, time constant and poll interval values. These are in 8-bit signed integer format in log2 (log to the base 2) seconds. The only operations permitted on them are increment and decrement. For the purpose of this document and to simplify the presentation, a reference to one of these state variables by name means the exponentiated value, e.g., the poll interval is 1024 s, while reference by name and exponent means the actual value, e.g., the poll exponent is 10.</Body>
<Body>
<A ID="pgfId-1007844"></A>
To convert system time in any format to NTP date and timestamp formats requires that the number of seconds s from the prime epoch to the system time be determined. The era is the integer quotient and the timestamp the integer remainder as in</Body>
<Equation>
<A ID="pgfId-1016961"></A>
<IMAGE xml:link="simple" href="ntp4.book-10.gif" show="embed" actuate="auto"/>
 and <IMAGE xml:link="simple" href="ntp4.book-11.gif" show="embed" actuate="auto"/>
,</Equation>
<Body>
<A ID="pgfId-1014641"></A>
which works for positive and negative dates. To convert from NTP era and timestamp to system time requires the calculation</Body>
<Equation>
<A ID="pgfId-1014682"></A>
<IMAGE xml:link="simple" href="ntp4.book-12.gif" show="embed" actuate="auto"/>
</Equation>
<Body>
<A ID="pgfId-1014684"></A>
to determine the number of seconds since the prime epoch. Converting between NTP and system time can be a little messy, but beyond the scope of this document. Note that the number of days in era 0 is one more than the number of days in most other eras and this won't happen again until the year 2400 in era 3.</Body>
<Body>
<A ID="pgfId-1012565"></A>
In the description of state variables to follow, explicit reference to integer type implies a 32-bit unsigned integer. This simplifies bounds checks, since only the upper limit needs to be defined. Without explicit reference, the default type is 64-bit floating double. Exceptions will be noted as necessary.</Body>
<Heading1>
<A ID="pgfId-998359"></A>
<A ID="54150"></A>
Data Structures</Heading1>
<Body>
<A ID="pgfId-998360"></A>
The NTP protocol state machines described in following sections are defined using state variables and flow chart fragments. State variables are separated into classes according to their function in packet headers, peer and poll processes, the system process and the clock discipline process. Packet variables represent the NTP header values in transmitted and received packets. Peer and poll variables represent the contents of the association for each server separately. System variables represent the state of the server as seen by its dependent clients. Clock discipline variables represent the internal workings of the clock discipline algorithm. Additional constant and variable classes are defined in Appendix B.</Body>
<Heading2>
<A ID="pgfId-998361"></A>
Structure Conventions</Heading2>
<Body>
<A ID="pgfId-1017020"></A>
In the text and diagrams to follow, state variables are rendered in fixed-width font, while equation variables are rendered in italic or Greek font. Ordinary text and named routines are rendered in native font. In order to distinguish between different variables of the same name but used in different processes, the following Unix-like structure member naming convention is adopted. Table <A href="ntp4.xml#id(54062)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">3</A> summarizes the naming conventions in this and subsequent figures and tables in this document. A receive packet variable <TT>
v</TT>
 is a member of the packet structure <TT>
r</TT>
 with fully qualified name <TT>
r.v</TT>
. In a similar manner <TT>
x.v</TT>
 is a transmit packet variable, <TT>
p.v</TT>
 is a peer variable, <TT>
s.v</TT>
 is a system variable and <TT>
c.v</TT>
 is a clock discipline variable. There is a set of peer variables for each association; there is only one set of system and clock variables.</Body>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-13.gif" show="embed" actuate="auto"/>
</DIV>
<Body>
<A ID="pgfId-1017031"></A>
Most flow chart fragments begin with a statement label and end with a named go-to or exit. A subroutine call includes a dummy () following the name and return at the end.to the point following the call.</Body>
<Heading2>
<A ID="pgfId-998372"></A>
Global Parameters</Heading2>
<Body>
<A ID="pgfId-1004007"></A>
In addition to the variable classes a number of global parameters are defined in this document, including those shown with values in Table <A href="ntp4.xml#id(59273)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">4</A>. While these are the only parameters needed in this document, a larger collection is necessary in the skeleton and larger still for the reference implementation. Section <A href="ntp4.book.xml#id(58319)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">B.1</A> contains those used by the skeleton for the mitigation algorithms, clock discipline algorithm and related implementation-dependent functions. Some of these parameter values are cast in stone, like the NTP port number assigned by the IANA and the version number assigned NTPv4 itself. Others like the frequency tolerance, involve an assumption about the worst case behavior of a system clock once synchronized and then allowed to drift when its sources have become unreachable. The minimum and maximum parameters define the limits of state variables as described in later sections.</Body>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-14.gif" show="embed" actuate="auto"/>
</DIV>
<Body>
<A ID="pgfId-1017841"></A>
While shown with fixed values in this document, some implementations may make them variables adjustable by configuration commands. For instance, the reference implementation computes the value of PRECISION as log2 of the minimum time in several iterations to read the system clock.</Body>
<Heading2>
<A ID="pgfId-998374"></A>
Packet Header Variables</Heading2>
<Body>
<A ID="pgfId-1004011"></A>
The most important state variables from an external point of view are the packet header variables described below. The NTP packet header follows the UDP and IP headers and the physical header specific to the underlying transport network. It consists of a number of 32-bit (4-octet) words, although some fields use multiple words and others are packed in smaller fields within a word. The NTP packet header shown in Appendix A has 12 words followed by optional extension fields and finally an optional message authentication code (MAC) consisting of the key identifier and message digest fields.</Body>
<Body>
<A ID="pgfId-998375"></A>
The optional extension fields described in Appendix A are used by the Autokey security protocol <A href="ntp4.book.xml#id(91215)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">[3]</A>, which is not described here. The MAC is used by both Autokey and the symmetric key authentication scheme described in Appendix A. As is the convention in other Internet protocols, all fields are in network byte order, commonly called big-endian.</Body>
<Body>
<A ID="pgfId-1003999"></A>
A list of the packet header variables is shown in Table <A href="ntp4.xml#id(84331)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">5</A> and described in detail below. The packet header fields apply to both transmitted (<TT>
x</TT>
 prefix) and received packets (<TT>
r</TT>
 prefix). The variables are interpreted as follows:</Body>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-15.gif" show="embed" actuate="auto"/>
</DIV>
<Hang>
<A ID="pgfId-998383"></A>
<TT>
leap</TT>
	2-bit integer warning of an impending leap second to be inserted or deleted in the last minute of the current month, coded as follows:</Hang>
<Hand1>
<A ID="pgfId-998384"></A>
0	no warning
1	last minute of the day has 61 seconds
2	last minute of the day has 59 seconds
3	alarm condition (the clock has never been synchronized)</Hand1>
<Hang>
<A ID="pgfId-998385"></A>
<TT>
version</TT>
. 3-bit integer representing the NTP version number, currently 4.</Hang>
<Hang>
<A ID="pgfId-998386"></A>
<TT>
mode</TT>
	3-bit integer representing the mode, with values defined as follows:</Hang>
<Hand1>
<A ID="pgfId-998387"></A>
0	reserved
1	symmetric active
2	symmetric passive
3	client
4	server
5	broadcast
6	NTP control message
7	reserved for private use</Hand1>
<Hang>
<A ID="pgfId-998388"></A>
<TT>
stratum</TT>
	8-bit integer representing the stratum, with values defined as follows:</Hang>
<Hand1>
<A ID="pgfId-998389"></A>
0 unspecified or invalid
1 primary server (e.g., equipped with a GPS receiver)
2-255 secondary server (via NTP)</Hand1>
<Hand1>
<A ID="pgfId-1003357"></A>
It is customary to map the stratum value 0 in received packets to MAXSTRAT (16) in the peer variable <TT>
p.stratum</TT>
 and to map <TT>
p.stratum</TT>
 values of MAXSTRAT or greater to 0 in transmitted packets. This allows reference clocks, which normally appear at stratum 0, to be conveniently mitigated using the same algorithms used for external sources.</Hand1>
<Hang>
<A ID="pgfId-998391"></A>
<TT>
poll</TT>
	8-bit signed integer representing the maximum interval between successive messages, in log2 seconds. In the reference implementation the limits are set by MINPOLL (4) and MAXPOLL (17), but the default limits are 6 and 10, respectively.</Hang>
<Hang>
<A ID="pgfId-998392"></A>
<TT>
precision</TT>
	8-bit signed integer representing the precision of the system clock, in log2 seconds. For instance a value of <Symbol>
−</Symbol>
18 corresponds to a precision of about one microsecond. The precision is normally determined when the service first starts up as the minimum time of several iterations to read the system clock.</Hang>
<Hang>
<A ID="pgfId-998393"></A>
<TT>
rootdelay</TT>
	Total roundtrip delay to the reference clock, in NTP short format.</Hang>
<Hang>
<A ID="pgfId-998394"></A>
<TT>
rootdisp</TT>
	Total dispersion to the reference clock, in NTP short format.</Hang>
<Hang>
<A ID="pgfId-998395"></A>
<TT>
refid</TT>
	32-bit code identifying the particular server or reference clock. The interpretation depends on the value in the stratum field. For packet stratum 0 (unspecified or invalid) this is a four-character ASCII string, called the kiss code, used for debugging and monitoring purposes. For stratum 1 (reference clock) this is a four-octet, left-justified, zero-padded ASCII string assigned to the radio clock. While not specifically enumerated in this document, the following have been used as ASCII identifiers:</Hang>
<Hand1>
<A ID="pgfId-998396"></A>
<TT>
GOES</TT>
	Geosynchronous Orbit Environment Satellite
<TT>
GPS</TT>
	Global Position System
<TT>
PPS</TT>
	Generic pulse-per-second
<TT>
IRIG</TT>
	Inter-Range Instrumentation Group
<TT>
WWVB</TT>
	LF Radio WWVB Ft. Collins, CO 60 kHz
<TT>
DCF77</TT>
	LF Radio DCF77 Mainflingen, DE 77.5 kHz
<TT>
HBG</TT>
	LF Radio HBG Prangins, HB 75 kHz
<TT>
MSF</TT>
	LF Radio MSF Rugby, UK 60 kHz
<TT>
JJY</TT>
	LF Radio JJY Fukushima, JP 40 kHz, Saga, JP 60 kHz
<TT>
LORC</TT>
	MF Radio LORAN C 100 kHz
<TT>
TDF</TT>
	MF Radio Allouis, FR 162 kHz
<TT>
CHU</TT>
	HF Radio CHU Ottawa, Ontario
<TT>
WWV</TT>
	HF Radio WWV Ft. Collins, CO
<TT>
WWVH</TT>
	HF Radio WWVH Kaui, HI
<TT>
NIST</TT>
	NIST telephone modem
<TT>
USNO</TT>
	USNO telephone modem
<TT>
PTB</TT>
	etc. European telephone modem</Hand1>
<Hand1>
<A ID="pgfId-998397"></A>
Above stratum 1 (secondary servers and clients) this is the reference identifier of the server. If using the IPv4 address family, the identifier is the four-octet IPv4 address. If using the IPv6 address family, it is the first four octets of the MD5 hash of the IPv6 address.</Hand1>
<Hang>
<A ID="pgfId-998398"></A>
<TT>
reftime</TT>
	Time when the system clock was last set or corrected, in NTP timestamp format.</Hang>
<Hang>
<A ID="pgfId-998401"></A>
<TT>
org</TT>
	Time at the client when the request departed for the server, in NTP timestamp format.</Hang>
<Hang>
<A ID="pgfId-998402"></A>
<TT>
rec</TT>
	Time at the server when the request arrived from the client, in NTP timestamp format.</Hang>
<Hang>
<A ID="pgfId-998403"></A>
<TT>
xmt</TT>
	 Time at the server when the response left for the client, in NTP timestamp format.</Hang>
<Hang>
<A ID="pgfId-998404"></A>
<TT>
dst</TT>
	Time at the client when the reply arrived from the server, in NTP timestamp format. Note: This value is not included in a header field; it is determined upon arrival of the packet and made avaiable in the packet buffer data structure.</Hang>
<Hang>
<A ID="pgfId-1003475"></A>
<TT>
keyid</TT>
	32-bit unsigned integer used by the client and server to designate a secret 128-bit MD5 key. Together, the keyid and digest fields collectively are called message authentication code (MAC).</Hang>
<Hang>
<A ID="pgfId-1003524"></A>
<TT>
digest</TT>
	128-bit bitstring computed by the keyed MD5 message digest algorithm described in Appendix A.</Hang>
<Heading1>
<A ID="pgfId-998405"></A>
<A ID="69016"></A>
On-Wire Protocol</Heading1>
<Body>
<A ID="pgfId-998406"></A>
The NTP on-wire protocol is the core mechanism to exchange time values between servers, peers and clients. It is inherently resistant to lost or duplicate data packets. Data integrity is provided by the IP and UDP checksums. No flow-control or retransmission facilities are provided or necessary. The protocol uses timestamps, either extracted from packet headers or struck from the system clock upon the arrival or departure of a packet. Timestamps are precision data and should be restruck in case of link level retransmission and corrected for the time to compute a MAC on transmit<A href="#id(pgfId-1007346)" xml:link="simple" show="replace" actuate="user" CLASS="footnote">3</A>.</Body>
<Body>
<A ID="pgfId-998407"></A>
The on-wire protocol uses four timestamps numbered T1 through T4 and three state variables <TT>
org</TT>
, <TT>
rec</TT>
 and <TT>
xmt</TT>
, as shown in Figure <A href="ntp4.xml#id(75056)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">3</A>. This figure shows the most general case where each of two peers, A and B, independently measure the offset and delay relative to the other. For purposes of illustration the individual timestamp values are shown in lower case with subscripts indicating the order of transmission and reception.</Body>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-16.gif" show="embed" actuate="auto"/>
</DIV>
<Body>
<A ID="pgfId-998414"></A>
In the figure the first packet transmitted by A containing only the transmit timestamp T3 with value t1. B receives the packet at t2 and saves the origin timestamp T1 with value t1 in state variable <TT>
org</TT>
 and the destination timestamp T4 with value t2 in state variable <TT>
rec</TT>
. At this time or some time later B sends a packet to A containing the <TT>
org</TT>
 and <TT>
rec</TT>
 state variables in T1 and T2, respectively and in addition the transmit timestamp T3 with value t3, which is saved in the <TT>
xmt</TT>
 state variable. When this packet arrives at A the packet header variables T1, T2, T3 and destination timestamp T4 represent the four timestamps necessary to compute the offset and delay of B relative to A, as described later.</Body>
<Body>
<A ID="pgfId-998415"></A>
Before the A state variables are updated, two sanity checks are performed in order to protect against duplicate or bogus packets. A packet is a duplicate if the transmit timestamp T3 in the packet matches the <TT>
xmt</TT>
 state variable. A packet is bogus if the origin timestamp T1 in the packet does not match the <TT>
org</TT>
 state variable. In either of these cases the state variables are updated, but the packet is discarded.</Body>
<Body>
<A ID="pgfId-998416"></A>
The four most recent timestamps, T1 through T4, are used to compute the offset of B relative to A</Body>
<Equation>
<A ID="pgfId-998420"></A>
<IMAGE xml:link="simple" href="ntp4.book-17.gif" show="embed" actuate="auto"/>
</Equation>
<Body>
<A ID="pgfId-998421"></A>
and the roundtrip delay</Body>
<Equation>
<A ID="pgfId-998425"></A>
<IMAGE xml:link="simple" href="ntp4.book-18.gif" show="embed" actuate="auto"/>
.</Equation>
<Body>
<A ID="pgfId-998426"></A>
Note that the quantities within parentheses are computed from 64-bit unsigned timestamps and result in signed values with 63 significant bits plus sign. These values can represent dates from 68 years in the past to 68 years in the future. However, the offset and delay are computed as the sum and difference of these values, which contain 62 significant bits and two sign bits, so can represent unambiguous values from 34 years in the past to 34 years in the future. In other words, the time of the client must be set within 34 years of the server before the service is started. This is a fundamental limitation with 64-bit integer arithmetic.</Body>
<Body>
<A ID="pgfId-998427"></A>
In implementations where floating double arithmetic is available, the first-order differences can be converted to floating double and the second-order sums and differences computed in that arithmetic. Since the second-order terms are typically very small relative to the timestamps themselves, there is no loss in significance, yet the unambiguous range is increased from 34 years to 68 years. Additional considerations on these issues, as well as the behavior when moving beyond the prime era, are discussed in online white papers at www.ntp.org but beyond the scope of this document.</Body>
<Body>
<A ID="pgfId-998428"></A>
In some scenarios where the frequency offset between the client and server is relatively large and the actual propagation time small, it is possible that the delay computation becomes negative. For instance, if the frequency difference is 100 PPM and the interval T4 <Symbol>
−</Symbol>
 T1 is 64 s, the apparent delay is −6.4 ms. Since negative values are misleading in subsequent computations, the value of <Symbol>
δ</Symbol>
 should be clamped not less than the system precision <TT>
s.precision</TT>
 <Symbol>
ρ</Symbol>
 defined below.</Body>
<Body>
<A ID="pgfId-1012588"></A>
The discussion above assumes the most general case where two symmetric peers independently measure the offsets and delays between them. In the case of a stateless server, the protocol can be simplified. A stateless server copies T3 and T4 from the client packet to T1 and T2 of the server packet and tacks on the transmit timestamp T3 before sending it to the client. Additional details for filling in the remaining protocol fields are given in the next section and in Appendix B.</Body>
<Body>
<A ID="pgfId-1012592"></A>
A SNTP primary server implementing the on-wire protocol has no upstream servers except a single reference clock In principle, it is indistinguishable from an NTP primary server which has the mitigation algorithms, presumably to mitigate between multiple reference clocks. Upon receiving a client request, a SNTP primary server constructs and sends the reply packet as shown in Figure <A href="ntp4.xml#id(50998)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">5</A> below. Note that the dispersion field in the packet header must be calculated in the same way as in the NTP case.</Body>
<Body>
<A ID="pgfId-1015025"></A>
A SNTP client using the on-wire protocol has a single server and no downstream clients. It can operate with any subset of the NTP on-wire protocol, the simplest using only the transmit timestamp of the server packet and ignoring all other fields. However, the additional complexity to implement the full on-wire protocol is minimal and is encouraged.</Body>
<Heading1>
<A ID="pgfId-998430"></A>
Peer Process</Heading1>
<Body>
<A ID="pgfId-998431"></A>
The peer process is called upon arrival of a server packet. It runs the on-wire protocol to determine the clock offset and roundtrip delay and in addition computes statistics used by the system and poll processes. Peer variables are instantiated in the association data structure when the structure is initialized and updated by arriving packets. There is a peer process, poll process and association for each server.</Body>
<Body>
<A ID="pgfId-1013467"></A>
The discussion in this section covers only the variables and routines necessary for a conforming NTPv4 implementation. Additional implementation details are in Section <A href="ntp4.book.xml#id(96242)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">B.5</A>. The engineering principles and measured performance with the reference implementation are discussed in <A href="ntp4.book.xml#id(86162)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">[2]</A>.</Body>
<Heading2>
<A ID="pgfId-1004030"></A>
Peer Process Variables</Heading2>
<Body>
<A ID="pgfId-1004039"></A>
Table <A href="ntp4.xml#id(67519)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">6</A> summarizes the common names, formula names and a short description of each peer variable, all of which have prefix <TT>
p</TT>
. The following configuration variables are normally initialized when the association is mobilized, either from a configuration file or upon arrival of the first packet for an ephemeral association.</Body>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-19.gif" show="embed" actuate="auto"/>
</DIV>
<Hang>
<A ID="pgfId-998443"></A>
<TT>
p.srcadr</TT>
	IP address of the remote server or reference clock. In the reference implementation reference clock addresses are by convention in IPv4 format with prefix 127.127.<TT>
t</TT>
.<TT>
u</TT>
, where <TT>
t</TT>
 is the device driver number and <TT>
u</TT>
 the instantiation number. This becomes the destination IP address in packets sent from this association.</Hang>
<Hang>
<A ID="pgfId-1001446"></A>
<TT>
p.srcport</TT>
	UDP port number of the server or reference clock. This becomes the destination port number in packets sent from this association. When operating in symmetric modes (1 and 2) this field must contain the NTP port number <TT>
PORT</TT>
 (123) assigned by the IANA. In other modes it can contain any number consistent with local policy.</Hang>
<Hang>
<A ID="pgfId-998444"></A>
<TT>
p.dstadr</TT>
	IP address of the client. This becomes the source IP address in packets sent from this association.</Hang>
<Hang>
<A ID="pgfId-1001459"></A>
<TT>
p.dstport</TT>
	UDP port number of the client, ordinarily the NTP port number <TT>
PORT</TT>
 (123) assigned by the IANA. This becomes the source port number in packets sent from this association. </Hang>
<Hang>
<A ID="pgfId-998447"></A>
<TT>
p.keyid</TT>
	Symmetric key ID for the 128-bit MD5 key used to generate and verify the MAC. The client and server or peer can use different values, but they must map to the same key.</Hang>
<Body>
<A ID="pgfId-1004980"></A>
The variables defined below are updated from the packet header as each packet arrives. They are interpreted in the same way as the as the packet variables of the same names.</Body>
<hang1>
<A ID="pgfId-998450"></A>
<TT>
p.leap</TT>
,<TT>
 p.version</TT>
,<TT>
 p.mode</TT>
, <TT>
p.stratum</TT>
, <TT>
p.ppoll</TT>
, <TT>
p.rootdelay</TT>
,<TT>
 p.rootdisp</TT>
, <TT>
p.refid</TT>
, <TT>
p.reftime</TT>
</hang1>
<Body>
<A ID="pgfId-1004987"></A>
It is convenient for later processing to convert the NTP short format packet values <TT>
p.rootdelay</TT>
 and <TT>
p.rootdisp</TT>
 to floating doubles as peer variables.</Body>
<Body>
<A ID="pgfId-1007536"></A>
The <TT>
p.org</TT>
, <TT>
p.rec</TT>
, <TT>
p.xmt</TT>
 variables represent the timestamps computed by the on-wire protocol described previously. The <TT>
p.offset</TT>
, <TT>
p.delay</TT>
, <TT>
p.disp</TT>
, <TT>
p.jitter</TT>
 variables represent the current time values and statistics produced by the clock filter algorithm. The offset and delay are computed by the on-wire protocol; the dispersion and jitter are calculated as described below. Strictly speaking, the epoch <TT>
p.t</TT>
 is not a timestamp; it records the system timer upon arrival of the latest packet selected by the clock filter algorithm.</Body>
<Heading2>
<A ID="pgfId-1003938"></A>
Peer Process Operations</Heading2>
<Body>
<A ID="pgfId-1003989"></A>
Figure <A href="ntp4.xml#id(91273)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">4</A> shows the peer process code flow upon the arrival of a packet. Additional details specific to the reference implementation are shown in the receive() and access() routines in Section <A href="ntp4.book.xml#id(96242)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">B.5</A>. There is no specific method required for access control, although it is recommended that implementations include a match-and-mask scheme similar to many others now in widespread use, as well as in the reference implementation. Format checks require correct field length and alignment, acceptable version number (1-4) and correct extension field syntax, if present. There is no specific requirement for authentication; however, if authentication is implemented, the symmetric key scheme described in Appendix A must be included among the supported. This scheme uses the MD5 keyed hash algorithm Section <A href="ntp4.book.xml#id(50403)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">B.2</A>. For the most vulnerable applications the Autokey public key scheme described in <A href="ntp4.book.xml#id(91215)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">[3]</A> and supported by the reference implementation in is recommended.</Body>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-20.gif" show="embed" actuate="auto"/>
</DIV>
<Body>
<A ID="pgfId-1015089"></A>
Next, the association table is searched for matching source address and source port using the find_assoc() routine in Section <A href="ntp4.book.xml#id(96242)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">B.5</A>. The dispatch table near the beginning of that section is indexed by the packet mode and association mode (0 if no matching association) to determine the dispatch code and thus the case target. The significant cases are FXMT, NEWPS and NEWBC.</Body>
<Body>
<A ID="pgfId-1015086"></A>
FXMIT. This is a client (mode 3) packet matching no association. The server constructs a server (mode 4) packet and returns it to the client without retaining state. The server packet is constructed as in Figure <A href="ntp4.xml#id(50998)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">5</A> and the fast_xmit() routine in Section <A href="ntp4.book.xml#id(96242)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">B.5</A>. If the <TT>
s.rootdelay</TT>
 and <TT>
s.rootdisp</TT>
 system variables are stored in floating double, they must be converted to NTP short format first. Note that, if authentication fails, the server returns a special message called a crypto-NAK. This message includes the normal NTP header data shown in the figure, but with a MAC consisting of four octets of zeros. The client is free to accept or reject the data in the message.</Body>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-21.gif" show="embed" actuate="auto"/>
</DIV>
<Body>
<A ID="pgfId-1003904"></A>
NEWBC. This is a broadcast (mode 5) packet matching no association. The client mobilizes a client (mode 3) association as shown in the mobilize() and clear() routines in Section <A href="ntp4.book.xml#id(50403)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">B.2</A>. The reference implementation first performs the necessary steps to run the Autokey protocol and determine the propagation delay, then continues in listen-only (mode 6) to receive further packets. Note the distinction between a mode-6 packet, which is reserved for the NTP monitor and control functions, and a mode-6 association.</Body>
<Body>
<A ID="pgfId-1015116"></A>
NEWPS. This is a symmetric active (1) packet matching no association. The client mobilizes a symmetric passive (mode 2) association as shown in the mobilize() and clear() routines in Section <A href="ntp4.book.xml#id(50403)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">B.2</A>. Code flow continues to the match_assoc fragment described below.</Body>
<Body>
<A ID="pgfId-1015108"></A>
In other cases the packet matches an existing association and code flows to the match_assoc fragment in Figure <A href="ntp4.xml#id(58908)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">6</A>. The packet timestamps are carefully checked to avoid invalid, duplicate or bogus packets, as shown in the figure. Note that a crypto-NAK is considered valid only if it survives these tests. Next, the peer variables are copied from the packet header variables as shown in Figure <A href="ntp4.xml#id(44402)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">7</A> and the packet() routine in Section <A href="ntp4.book.xml#id(96242)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">B.5</A>. The reference implementation includes a number of data range checks shown in Table <A href="ntp4.xml#id(80361)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">7</A> and discards the packet if the ranges are exceeded; however, the header fields are copied even if errors occur, since they are necessary in symmetric modes to construct the subsequent poll message.</Body>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-22.gif" show="embed" actuate="auto"/>
</DIV>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-23.gif" show="embed" actuate="auto"/>
</DIV>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-24.gif" show="embed" actuate="auto"/>
</DIV>
<Body>
<A ID="pgfId-1005950"></A>
The 8-bit <TT>
p.reach</TT>
 shift register in the poll process described later is used to determine whether the server is reachable or not and provide information useful to insure the server is reachable and the data are fresh. The register is shifted left by one bit when a packet is sent and the rightmost bit is set to zero. As valid packets arrive, the rightmost bit is set to one. If the register contains any nonzero bits, the server is considered reachable; otherwise, it is unreachable. Since the peer poll interval might have changed since the last packet, the poll_update() routine in Section <A href="ntp4.book.xml#id(33659)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">B.8</A> is called to redetermine the host poll interval.</Body>
<Body>
<A ID="pgfId-1006011"></A>
The on-wire protocol calculates the clock offset <Symbol>
θ</Symbol>
 and roundtrip delay <Symbol>
δ </Symbol>
from the four most recent timestamps as shown in Figure <A href="ntp4.xml#id(75056)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">3</A>. While it is in principle possible to do all calculations except the first-order timestamp differences in fixed-point arithmetic, it is much easier to convert the first-order differences to floating doubles and do the remaining calculations in that arithmetic,. and this will be assumed in the following description. The dispersion statistic <Symbol>
ε(</Symbol>
t<Symbol>
)</Symbol>
 represents the maximum error due to the frequency tolerance and time since the last measurement. It is initialized</Body>
<Equation>
<A ID="pgfId-1006015"></A>
<IMAGE xml:link="simple" href="ntp4.book-25.gif" show="embed" actuate="auto"/>
</Equation>
<Body>
<A ID="pgfId-1006016"></A>
when the measurement is made at t0. Here <Symbol>
ρ</Symbol>
R is the peer precision in the packet header <TT>
r.precision</TT>
 and <Symbol>
ρ</Symbol>
 the system precision <TT>
s.precision</TT>
, both<TT>
 </TT>
expressed in seconds. These terms are necessary to account for the uncertainty in reading the system clock in both the server and the client. The dispersion then grows at constant rate TOLERANCE (<Symbol>
Φ</Symbol>
); in other words, at time t, <IMAGE xml:link="simple" href="ntp4.book-26.gif" show="embed" actuate="auto"/>
. With the default value <Symbol>
Φ </Symbol>
=<Symbol>
 </Symbol>
15 PPM, this amounts to about 1.3 s per day. With this understanding, the argument t will be dropped and the dispersion represented simply as <Symbol>
ε</Symbol>
. The remaining statistics are computed by the clock filter algorithm described in the next section.</Body>
<Heading2>
<A ID="pgfId-1002131"></A>
<A ID="20894"></A>
Clock Filter Algorithm</Heading2>
<Body>
<A ID="pgfId-1003993"></A>
The clock filter algorithm grooms the stream of on-wire data to select the samples most likely to represent the correct time. The algorithm produces the <TT>
p.offset</TT>
 <Symbol>
θ</Symbol>
, <TT>
p.delay</TT>
 <Symbol>
δ</Symbol>
, <TT>
p.dispersion</TT>
 <Symbol>
ε</Symbol>
, <TT>
p.jitter</TT>
 ϕ, and time of arrival <TT>
p.t</TT>
 t used by the mitigation algorithms to determine the best and final offset used to discipline the system clock. They are also used to determine the server health and whether it is suitable for synchronization. The core processing steps of this algorithm are shown in Figure <A href="ntp4.xml#id(12379)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">8</A> with more detail in the clock_filter() routine in Section <A href="ntp4.book.xml#id(96242)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">B.5</A>.</Body>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-27.gif" show="embed" actuate="auto"/>
</DIV>
<Body>
<A ID="pgfId-1005247"></A>
The clock filter algorithm saves the most recent sample tuples (<Symbol>
θ</Symbol>
, <Symbol>
δ, ε, </Symbol>
t) in an 8-stage shift register in the order that packets arrive. Here t is the system timer, not the peer variable of the same name. The following scheme is used to insure sufficient samples are in the register and that old stale data are discarded. Initially, the tuples of all stages are set to the dummy tuple (0, MAXDISP, MAXDISP, t).<TT>
 </TT>
As valid packets arrive, the (<Symbol>
θ</Symbol>
, <Symbol>
δ, ε, </Symbol>
t) tuples are shifted into the register causing old samples to be discarded, so eventually only valid samples remain. If the three low order bits of the reach register are zero, indicating three poll intervals have expired with no valid packets received, the poll process calls the clock filter algorithm with the dummy tuple just as if the tuple had arrived from the network. If this persists for eight poll intervals, the register returns to the initial condition.</Body>
<Body>
<A ID="pgfId-1005248"></A>
In the next step the shift register stages are copied to a temporary list and the list sorted by increasing <Symbol>
δ</Symbol>
. Let j index the stages starting with the lowest <Symbol>
δ. </Symbol>
If the sample epoch t0 is not later than the last valid sample epoch <TT>
p.t</TT>
, the routine exits without affecting the current peer variables. Otherwise, let <Symbol>
ε</Symbol>
j be the dispersion of the jth entry, then</Body>
<Equation>
<A ID="pgfId-1005252"></A>
<IMAGE xml:link="simple" href="ntp4.book-28.gif" show="embed" actuate="auto"/>
</Equation>
<Body>
<A ID="pgfId-1001961"></A>
is the peer dispersion <TT>
p.disp</TT>
. Note the overload of <Symbol>
ε</Symbol>
, whether input to the clock filter or output, the meaning should be clear from context.</Body>
<Body>
<A ID="pgfId-1006079"></A>
The observer should note (a) if all stages contain the dummy tuple with dispersion MAXDISP, the computed dispersion is a little less than 16 s, (b) each time a valid tuple is shifted into the register, the dispersion drops by a little less than half, depending on the valid tuples dispersion, (c) after the fourth valid packet the dispersion is usually a little less than 1 s, which is the assumed value of the MAXDIST<TT>
 </TT>
parameter. used by the selection algorithm to determine whether the peer variables are acceptable or not.</Body>
<Body>
<A ID="pgfId-1005343"></A>
Let the first stage offset in the sorted list be <Symbol>
θ</Symbol>
0; then, for the other stages in any order, the jitter is the RMS average</Body>
<Equation>
<A ID="pgfId-1001965"></A>
<A ID="17209"></A>
<IMAGE xml:link="simple" href="ntp4.book-29.gif" show="embed" actuate="auto"/>
,</Equation>
<Body>
<A ID="pgfId-1001968"></A>
where n is the number of valid tuples in the register. In order to insure consistency and avoid divide exceptions in other computations, the <Symbol>
ϕ</Symbol>
 is bounded from below by the system precision <Symbol>
ρ</Symbol>
 expressed in seconds. While not in general considered a major factor in ranking server quality, jitter is a valuable indicator of fundamental timekeeping performance and network congestion state.</Body>
<Body>
<A ID="pgfId-1006501"></A>
Of particular importance to the mitigation algorithms is the peer synchronization distance, which is computed from the root delay and root dispersion. The root delay is</Body>
<Equation>
<A ID="pgfId-1006505"></A>
<IMAGE xml:link="simple" href="ntp4.book-30.gif" show="embed" actuate="auto"/>
</Equation>
<Body>
<A ID="pgfId-1006506"></A>
and the root dispersion is</Body>
<Equation>
<A ID="pgfId-1006510"></A>
<IMAGE xml:link="simple" href="ntp4.book-31.gif" show="embed" actuate="auto"/>
.</Equation>
<Body>
<A ID="pgfId-1006511"></A>
Note that <Symbol>
ε</Symbol>
 and therefore <IMAGE xml:link="simple" href="ntp4.book-32.gif" show="embed" actuate="auto"/>
 increase at rate <Symbol>
Φ</Symbol>
. The peer synchronization distance is defined</Body>
<Equation>
<A ID="pgfId-1006515"></A>
<A ID="78394"></A>
<IMAGE xml:link="simple" href="ntp4.book-33.gif" show="embed" actuate="auto"/>
</Equation>
<Body>
<A ID="pgfId-1006516"></A>
and recalculated as necessary. The <Symbol>
λ</Symbol>
 is a component of the root synchronization distance <Symbol>
Λ</Symbol>
 used by the mitigation algorithms as a metric to evaluate the quality of time available from each server. Note that there is no state variable for <Symbol>
λ</Symbol>
, as it depends on the time since the last update.</Body>
<Body>
<A ID="pgfId-1015737"></A>
As a practical matter, a not uncommon hazard in global Internet timekeeping is an occasional isolated offset surge, called a popcorn spike, due to some transient delay phenomenon in the network. The reference implementation uses a popcorn spike suppressor to reduce errors due this cause. It operates by tracking the exponentially averaged jitter and discarding an offset spike that exceeds a threshold equal to some multiple of the average. The spike itself is then used to update the average, so the threshold is self-adaptive.</Body>
<Heading1>
<A ID="pgfId-1005226"></A>
System Process</Heading1>
<Body>
<A ID="pgfId-1013717"></A>
As each new sample (<Symbol>
θ</Symbol>
, <Symbol>
δ, ε, </Symbol>
t) is produced by the clock filter algorithm, the sample is processed by the mitigation algorithms consisting of the selection, clustering, combining and clock discipline algorithms in the system process. The selection algorithm scans all associations and casts off the falsetickers, which have demonstrably incorrect time, leaving the truechimers as result. In a series of rounds the clustering algorithm discards the association statistically furthest from the centroid until a minimum number of survivors remain. The combining algorithm produces the best and final offset on a weighted average basis and selects one of the associations as the system peer providing the best statistics for performance evaluation. The final offset is passed to the clock discipline algorithm to steer the system clock to the correct time. The statistics (<Symbol>
θ</Symbol>
, <Symbol>
δ, ε, </Symbol>
t) associated with the system peer are used to construct the system variables inherited by dependent servers and clients and made available to other applications running on the same machine.</Body>
<Body>
<A ID="pgfId-1013476"></A>
The discussion in following sections covers only the basic variables and routines necessary for a conforming NTPv4 implementation. Additional implementation details are in Section <A href="ntp4.book.xml#id(35392)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">B.6</A>. An interface that might be considered in a formal specification is represented by the function prototypes in Section <A href="ntp4.book.xml#id(58319)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">B.1</A>. The engineering principles and measured performance with the reference implementation are discussed in <A href="ntp4.book.xml#id(86162)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">[2]</A>.</Body>
<Heading2>
<A ID="pgfId-1005126"></A>
System Process Variables</Heading2>
<Body>
<A ID="pgfId-1004017"></A>
The variables and parameters associated with the system process are summarized in Table <A href="ntp4.xml#id(18074)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">8</A>, which gives the variable name, formula name and short description. Unless noted otherwisse, all variables have assumed prefix <TT>
s</TT>
. All the variables except <TT>
s.t</TT>
 and <TT>
s.p</TT>
 have the same format and interpretation as the peer variables of the same name. The remaining variables are defined below.</Body>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-34.gif" show="embed" actuate="auto"/>
</DIV>
<Hang>
<A ID="pgfId-1006128"></A>
<TT>
s.t	</TT>
 Integer representing the value of the system timer at the last update.</Hang>
<Hang>
<A ID="pgfId-1019121"></A>
<TT>
s.p</TT>
	System peer association pointer.</Hang>
<Hang>
<A ID="pgfId-1019122"></A>
<TT>
s.precision</TT>
	8-bit signed integer representing the precision of the system clock, in log2 seconds.ZA</Hang>
<Hang>
<A ID="pgfId-1019123"></A>
<TT>
s.offset</TT>
	Offset computed by the combining algorithm.</Hang>
<Hang>
<A ID="pgfId-1008432"></A>
<TT>
s.jitter</TT>
	Jitter computed by the cluster and combining algorithms.</Hang>
<Body>
<A ID="pgfId-1019078"></A>
The variables defined below are updated from the system peer process as described later. They are interpreted in the same way as the as the peer variables of the same names.</Body>
<hang1>
<A ID="pgfId-1019079"></A>
<TT>
s.leap</TT>
,<TT>
 s.stratum</TT>
, <TT>
s.rootdelay</TT>
,<TT>
 s.rootdisp</TT>
, <TT>
s.refid</TT>
, <TT>
s.reftime</TT>
</hang1>
<Body>
<A ID="pgfId-1017212"></A>
Initially, all variables are cleared to zero, then the <TT>
s.leap</TT>
 is set to 3 (unsynchronized) and <TT>
s.stratum</TT>
 is set to MAXSTRAT<TT>
 </TT>
(16). The remaining statistics are determined as described below.</Body>
<Heading2>
<A ID="pgfId-1003739"></A>
System Process Operations</Heading2>
<Body>
<A ID="pgfId-1013708"></A>
The system process implements the selection, clustering, combining and clock discipline algorithms shown in Figure <A href="ntp4.xml#id(31984)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">1</A>. The clock_select() routine in Figure <A href="ntp4.xml#id(96512)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">9</A> includes the selection algorithm of Section <A href="ntp4.book.xml#id(58409)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">9.2.1</A> that produces a majority clique of truechimers based on agreement principles. The clustering algorithm of Section <A href="ntp4.book.xml#id(27305)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">9.2.2</A> discards the outliers of the clique to produce the survivors used by the combining algorithm in Section <A href="ntp4.book.xml#id(44796)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">9.2.3</A>, which in turn provides the final offset for the clock discipline algorithm in Section <A href="ntp4.book.xml#id(98630)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">9.2.4</A>.</Body>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-35.gif" show="embed" actuate="auto"/>
</DIV>
<Body>
<A ID="pgfId-1019181"></A>
If the selection algorithm cannot produce a majority clique, or if the clustering algorithm cannot produce at least CMIN survivors, the system process terminates with no further processing. If successful, the clustering algorithm selects the statistically best candidate as the system peer and its variables are inherited as the system variables.</Body>
<Body>
<A ID="pgfId-1019183"></A>
The selection and clustering algorithms are described below separately, but combined in the code skeleton and reference implementation.</Body>
<Heading3>
<A ID="pgfId-1014312"></A>
<A ID="58409"></A>
Selection Algorithm</Heading3>
<Body>
<A ID="pgfId-1014313"></A>
The selection algorithm operates to find the truechimers using Byzantine agreement principles originally proposed by Marzullo <A href="ntp4.book.xml#id(88741)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">[1]</A>, but modified to improve accuracy. An overview of the algorithm is in Figure <A href="ntp4.xml#id(28056)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">10</A> and the first half of the clock_select() routine in Section <A href="ntp4.book.xml#id(21510)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">B.6.1</A>. First, those servers which are unusable according to the rules of the protocol are detected and discarded by the accept() routine in Figure <A href="ntp4.xml#id(73499)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">11</A> and Section <A href="ntp4.book.xml#id(91918)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">B.6.3</A>. Next, a set of tuples {p, type, edge} is generated for the remaining servers, where p is an association pointer, type and edge identifies the upper (+1), middle (0) and lower (<Symbol>
−</Symbol>
1) endpoint of a correctness interval <IMAGE xml:link="simple" href="ntp4.book-36.gif" show="embed" actuate="auto"/>
, where <Symbol>
λ</Symbol>
 is the root distance calculated in (<A href="ntp4.book.xml#id(78394)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">11</A>).</Body>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-37.gif" show="embed" actuate="auto"/>
</DIV>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-38.gif" show="embed" actuate="auto"/>
</DIV>
<Body>
<A ID="pgfId-1014365"></A>
The tuples are placed on a list and sorted by edge. The list is processed from the lowest to the highest, then from highest to lowest using the algorithm in Figure <A href="ntp4.xml#id(28056)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">10</A> and in Section <A href="ntp4.book.xml#id(21510)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">B.6.1</A> and described in detail in <A href="ntp4.book.xml#id(86162)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">[2]</A>. The algorithm starts with the assumption that there are no falsetickers (f = 0) and attempts to find a nonempty intersection interval containing the midpoints of all correct servers, i.e., truechimers. If a nonempty interval cannot be found, it increases the number of assumed falsetickers by one and tries again. If a nonempty interval is found and the number of falsetickers is less than the number of truechimers, a majority clique has been found and the midpoints (offsets) represent the survivors available for the clustering algorithm. Otherwise, there are no suitable candidates to synchronize the system clock.</Body>
<Body>
<A ID="pgfId-1017948"></A>
While not shown on the flow chart, <Symbol>
λ </Symbol>
is increased by MINDISP (.005 s) when constructing the tuples. The reason for this is to avoid problems with very fast processors and networks. Occasionally, due to random fluctuations and jitter, two legitimate correctness intervals fail to overlap and may cause both to be declared falseticker. The MINDISP increment acts like a shim to decrease the likelihood this might occur. In the reference implementation MINDISP is a configurable value that can be changed to fit each scenario.</Body>
<Heading3>
<A ID="pgfId-1014490"></A>
<A ID="27305"></A>
Clustering Algorithm</Heading3>
<Body>
<A ID="pgfId-1014520"></A>
The members of the majority clique are placed on the survivor list, and sorted first by stratum, then by root distance <Symbol>
λ</Symbol>
. The sorted list is processed by the clustering algorithm in Figure <A href="ntp4.xml#id(26241)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">12</A> and the second half of the clock_select() algorithm in Section <A href="ntp4.book.xml#id(21510)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">B.6.1</A>. It operates in a series of rounds where each round discards the furthest statistical outlier until a specified minimum number of survivors NMIN (3) are left or until no further improvement is possible.</Body>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-39.gif" show="embed" actuate="auto"/>
</DIV>
<Body>
<A ID="pgfId-1017231"></A>
In each round let n be the number of survivors and s index the survivor list. Assume <Symbol>
ϕ</Symbol>
p is the peer jitter of the s survivor. Compute</Body>
<Equation>
<A ID="pgfId-1016340"></A>
<A ID="37667"></A>
<IMAGE xml:link="simple" href="ntp4.book-40.gif" show="embed" actuate="auto"/>
</Equation>
<Body>
<A ID="pgfId-1016371"></A>
as the selection jitter. Then choose <IMAGE xml:link="simple" href="ntp4.book-41.gif" show="embed" actuate="auto"/>
 and <IMAGE xml:link="simple" href="ntp4.book-42.gif" show="embed" actuate="auto"/>
. If <IMAGE xml:link="simple" href="ntp4.book-43.gif" show="embed" actuate="auto"/>
 or <IMAGE xml:link="simple" href="ntp4.book-44.gif" show="embed" actuate="auto"/>
, no further reduction in selection jitter is possible, so the algorithm terminates and the remaining survivors are processed by the combining algorithm. Otherwise, the algorithm casts off the <Symbol>
ϕ</Symbol>
max survivor, reduces n by one and makes another round.</Body>
<Heading3>
<A ID="pgfId-1016394"></A>
<A ID="44796"></A>
Combining Algorithm</Heading3>
<Body>
<A ID="pgfId-1014521"></A>
The remaining survivors are processed by the clock_combine() routine in Figure <A href="ntp4.xml#id(67679)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">13</A> and Section <A href="ntp4.book.xml#id(55098)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">B.6.4</A> to produce the best and final data for the clock discipline algorithm. The routine processes the peer offset <Symbol>
θ</Symbol>
 and jitter <Symbol>
ϕ</Symbol>
 to produce the system offset <Symbol>
Θ </Symbol>
and system peer jitter <Symbol>
ϑ</Symbol>
p, where each server statistic is weighted by the reciprocal of the root distance. and the result normalized. The system peer jitter <Symbol>
ϑ</Symbol>
p is a component of the system jitter described later.</Body>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-45.gif" show="embed" actuate="auto"/>
</DIV>
<Body>
<A ID="pgfId-1015481"></A>
The system statistics are passed to the clock_update() routine in Figure <A href="ntp4.xml#id(40746)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">14</A> and Section <A href="ntp4.book.xml#id(55098)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">B.6.4</A>. If there is only one survivor, the offset passed to the clock discipline algorithm is <IMAGE xml:link="simple" href="ntp4.book-46.gif" show="embed" actuate="auto"/>
 and the system peer jitter is <IMAGE xml:link="simple" href="ntp4.book-47.gif" show="embed" actuate="auto"/>
. Otherwise, the selection jitter <Symbol>
ϑ</Symbol>
s is computed as in (<A href="ntp4.book.xml#id(17209)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">8</A>), where <Symbol>
θ</Symbol>
0 represents the offset of the system peer and j ranges over the survivors.</Body>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-48.gif" show="embed" actuate="auto"/>
</DIV>
<Body>
<A ID="pgfId-1014321"></A>
The first survivor on the survivor list is selected as the system peer, here represented by the statistics (<Symbol>
θ</Symbol>
, <Symbol>
δ</Symbol>
, <Symbol>
ε</Symbol>
, <Symbol>
ϕ</Symbol>
). By rule, an update is discarded if its time of arrival <TT>
p.t</TT>
 is not strictly later than the last update used <TT>
s.t</TT>
. Let <Symbol>
μ</Symbol>
 = <TT>
p.t</TT>
 <Symbol>
−</Symbol>
 <TT>
s.t</TT>
 be the time since the last update or update interval. If the update interval is less than or equal to zero, the update is discarded. Otherwise, the system variables are updated from the system peer variables as shown in Figure <A href="ntp4.xml#id(40746)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">14</A>. Note that <TT>
s.stratum</TT>
 is set to <TT>
p.stratum</TT>
 plus one.</Body>
<Body>
<A ID="pgfId-1015649"></A>
The arrows labelled IGNOR, PANIC, ADJ and STEP refer to return codes from the local_clock() routine described in the next section. IGNORE means the update has been ignored as an outlier. PANIC means the offset is greater than the panic threshold PANICT (1000 s) and normally causes the program to exit with a diagnostic message to the system log. STEP means the offset is less than the panic threshold, but greater than the step threshold STEPT (125 ms). Since this means all peer data have been invalidated, all associations are reset and the client begins as at initial start. ADJ means the offset is less than the step threshold and thus a valid update for the local_clock() routine described later. In this case the system variables are updated as shown in Figure <A href="ntp4.xml#id(40746)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">14</A>.</Body>
<Body>
<A ID="pgfId-1017911"></A>
There is one exception not shown. The dispersion increment is bounded from below by MINDISP. In subnets with very fast processors and netowrks and very small dispersion and delay this forces a monotone-definite increase in <Symbol>
Ε</Symbol>
, which avoids loops between peers operating at the same stratum.</Body>
<Body>
<A ID="pgfId-1015632"></A>
Figure <A href="ntp4.xml#id(14902)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">15</A> shows how the error budget grows from the packet variables, on-wire protocol and system peer processe to produce the system variables that are passed to dependent applications and clients. The system jitter is defined</Body>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-49.gif" show="embed" actuate="auto"/>
</DIV>
<Equation>
<A ID="pgfId-1002397"></A>
<IMAGE xml:link="simple" href="ntp4.book-50.gif" show="embed" actuate="auto"/>
,</Equation>
<Body>
<A ID="pgfId-1002182"></A>
where <Symbol>
ϑ</Symbol>
s is the selection jitter relative to the system peer computed as in (<A href="ntp4.book.xml#id(37667)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">12</A>). The system jitter is passed to dependent applications programs as the nominal error statistic. The root delay <Symbol>
Δ</Symbol>
 and root dispersion <Symbol>
Ε</Symbol>
 statistics are relative to the primary server reference clock and thus inherited by each server along the path. The system synchronization distance is defined</Body>
<Equation>
<A ID="pgfId-1015596"></A>
<IMAGE xml:link="simple" href="ntp4.book-51.gif" show="embed" actuate="auto"/>
,</Equation>
<Body>
<A ID="pgfId-1015608"></A>
which is passed to dependent application programs as the maximum error statistic.</Body>
<Heading3>
<A ID="pgfId-1013616"></A>
<A ID="98630"></A>
Clock Discipline Algorithm</Heading3>
<Body>
<A ID="pgfId-1013617"></A>
The NTPv4 clock discipline algorithm, shortened to discipline in the following, functions as a combination of two philosophically quite different feedback control systems. In a phase-locked loop (PLL) design, periodic phase updates at update intervals μ are used directly to minimize the time error and indirectly the frequency error. In a frequency-locked loop (FLL) design, periodic frequency updates at intervals μ are used directly to minimize the frequency error and indirectly the time error. As shown in <A href="ntp4.book.xml#id(86162)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">[2]</A>, a PLL usually works better when network jitter dominates, while a FLL works better when oscillator wander dominates. This section contains an outline of how the NTPv4 design works. An in-depth discussion of the design principles is provided in <A href="ntp4.book.xml#id(86162)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">[2]</A>, which also includes a performance analysis.</Body>
<Body>
<A ID="pgfId-1015690"></A>
Recall from Figure <A href="ntp4.xml#id(31984)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">1</A> how the clock discipline and clock adjust processes interact with the other algorithms in NTPv4. The output of the combining algorithm represents the best estimate of the system clock offset relative to the server ensemble. The discipline adjusts the frequency of the VFO to minimize this offset. Finally, the timestamps of each server are compared to the timestamps derived from the VFO in order to calculate the server offsets and close the feedback loop.</Body>
<Body>
<A ID="pgfId-1013641"></A>
The discipline is implemented as the feedback control system shown in Figure <A href="ntp4.xml#id(92989)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">16</A>. The variable θr represents the combining algorithm offset (reference phase) and θc the VFO offset (control phase). Each update produces a signal Vd representing the instantaneous phase difference <IMAGE xml:link="simple" href="ntp4.book-52.gif" show="embed" actuate="auto"/>
. The clock filter for each server functions as a tapped delay line, with the output taken at the tap selected by the clock filter algorithm. The selection, clustering and combining algorithms combine the data from multiple filters to produce the signal Vs. The loop filter, with impulse response F(t), produces the signal Vc which controls the VFO frequency ωc and thus its phase <IMAGE xml:link="simple" href="ntp4.book-53.gif" show="embed" actuate="auto"/>
, which closes the loop. The Vc signal is generated by the clock adjust process in Section <A href="ntp4.book.xml#id(21356)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">9.3</A>. It runs at intervals of one second in the NTP daemon or one tick in the kernel implementation. The characteristic behavior of this model, which is determined by F(t) and the various gain factors given in Section <A href="ntp4.book.xml#id(87891)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">B.6.6</A>.</Body>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-54.gif" show="embed" actuate="auto"/>
</DIV>
<Body>
<A ID="pgfId-1013651"></A>
The transient behavior of the PLL/FLL feedback loop is determined by the impulse response of the loop filter F(t). The loop filter shown in Figure <A href="ntp4.xml#id(93641)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">17</A> predicts a phase adjustment x as a function of Vs. The PLL predicts a frequency adjustment <IMAGE xml:link="simple" href="ntp4.book-55.gif" show="embed" actuate="auto"/>
 as an integral <IMAGE xml:link="simple" href="ntp4.book-56.gif" show="embed" actuate="auto"/>
, while the FLL predicts an adjustment <IMAGE xml:link="simple" href="ntp4.book-57.gif" show="embed" actuate="auto"/>
 as a function of <IMAGE xml:link="simple" href="ntp4.book-58.gif" show="embed" actuate="auto"/>
. The two adjustments are combined to correct the frequency y as shown in Figure <A href="ntp4.xml#id(92989)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">16</A>. The x and y are then used by the clock_adjust() routine to control the VFO frequency. The detailed equations that implement these functions are best presented in the routines of Sections <A href="ntp4.book.xml#id(87891)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">B.6.6</A> and <A href="ntp4.book.xml#id(67421)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">B.7.1</A>.</Body>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-59.gif" show="embed" actuate="auto"/>
</DIV>
<Body>
<A ID="pgfId-1018177"></A>
Ordinarily, the pseudo-linear feedback loop described above operates to discipline the system clock. However, there are cases where a nonlinear algorithm offers considerable improvement. One case is when the discipline starts without knowledge of the intrinsic clock frequency. The pseudo-linear loop takes several hours to develop an accurate measurement and during most of that time the poll interval cannot be increased. The nonlinear loop described below does this in 15 minutes. Another case is when occasional bursts of large jitter are present due to congested network links. The state machine described below resists error bursts lasting less than 15 minutes.</Body>
<Body>
<A ID="pgfId-1018119"></A>
The remainder of this section describes how the discipline works. Table <A href="ntp4.xml#id(28764)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">9</A> contains a summary of the variables and parameters including the program name, formula name and short description. Unless noted otherwisse, all variables have assumed prefix <TT>
c</TT>
. The variables <TT>
c.t</TT>
, <TT>
c.tc</TT>
,<TT>
 c.state</TT>
, and <TT>
c.count</TT>
 are integers; the memainder are floating doubles. The function of each will be explained in the algorithm descriptions below.</Body>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-60.gif" show="embed" actuate="auto"/>
</DIV>
<Body>
<A ID="pgfId-1015755"></A>
The discipline is implemented by the local_clock() routine, which is called from the clock_update() routine in Figure <A href="ntp4.xml#id(40746)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">14</A>. The local_clock() routine pseudo code in Section <A href="ntp4.book.xml#id(87891)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">B.6.6</A> has two parts; first the state machine shown in Figure <A href="ntp4.xml#id(64849)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">18</A> and second the algorithm that determines the time constant and thus the poll interval in Figure <A href="ntp4.xml#id(30662)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">19</A>. The state transition function in Figure <A href="ntp4.xml#id(64849)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">18</A> is implemented by the rst() function shown at the lower left of the figure. The local_clock() routine exits immediately if the offset is greater than the panic threshold. The reference implementation sends a message to the operator and system log to set the clock manually within that range. A command line option can be used to override this behavior, but only for the first occurrence. Subsequent behavior depends on the offset magnitude and a state machine that determines if the offset and frequency are adjusted in steps or gradual increments.</Body>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-61.gif" show="embed" actuate="auto"/>
</DIV>
<Body>
<A ID="pgfId-1018228"></A>
The state machine transition function is shown in Table <A href="ntp4.xml#id(52342)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">10</A>.. The first column is the state name; the second column shows the next state and actions if the offset is less than the step threshold; the third column shows the next state and actions if the offset is greater than the step threshold; the fourth shows a brief comment. The next state is indicated by an arrow → followed by the next state name. Some actions are conditional on whether the update interval <Symbol>
μ</Symbol>
 exceeds the stepout threshold WATCH (900 s).</Body>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-62.gif" show="embed" actuate="auto"/>
</DIV>
<Body>
<A ID="pgfId-1015848"></A>
In order to speed convergence when the program is restarted, the reference implementation records the frequency offset in a file once each hour. When the program is started and the file has not been created, the machine starts in the NSET state, where it initializes the time and transitions to the FREQ state. The machine stays in that state until the first update after the stepout interval, when it computes the frequency directly and transitions to the SYNC state. When started after the frequency file has been written, the machine starts in the FSET state, where it initializes the time, reads the frequency file and transitions to the SYNC state, but steps the time if the offset exceeds the step threshold. The calculations are carefully designed so that time corrections and frequency corrections can be done independently.</Body>
<Body>
<A ID="pgfId-1015875"></A>
In SYNC state the time and frequency are normally adjusted in small increments, unless the offset exceeds the step threshold, in which case the state machine transitions to the SPIK state and avoids setting the time. If further updates exceed the step threshold, they are ignored until after the stepout interval, when the time is stepped. If during the stepout interval an offset is less than the step threshold, the machine transitions to SYNC state and operations resume normally.</Body>
<Body>
<A ID="pgfId-1015927"></A>
The remaining portion of the local_clock() routine is shown in Figure <A href="ntp4.xml#id(30662)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">19</A>. The time constant <Symbol>
τ</Symbol>
 is determined by comparing the clock jitter <Symbol>
ϕ</Symbol>
 with the magnitude of the current residual offset <Symbol>
Θ</Symbol>
R. produced by the clock adjust routine in the next section. If the residual offset is greater than PGATE (4) times the clock jitter, be hysteresis counter is reduced by two; otherwise, it is increased by one. If the hysteresis counter increases to the upper limit LIMIT (30), the time constant is increased by one; if it decreases to the lower limit <Symbol>
−</Symbol>
LIMIT (<Symbol>
−</Symbol>
30), the time constant is decreased by one. Normally, the time constant hovers near MAXPOLL, but quickly decreases it frequency surges due to a temperature spike, for example.</Body>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-63.gif" show="embed" actuate="auto"/>
</DIV>
<Body>
<A ID="pgfId-1018351"></A>
The clock jitter statistc <Symbol>
ϑ</Symbol>
 in Figure <A href="ntp4.xml#id(64849)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">18</A> and the clock wander statistic <Symbol>
Ψ</Symbol>
 in Figure <A href="ntp4.xml#id(30662)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">19</A> are implemented as exponential averages of RMS offset differences and RMS frequency differences, respectively. Let xi be a measurement at time i of either <Symbol>
ϑ</Symbol>
 or <Symbol>
Ψ</Symbol>
, <IMAGE xml:link="simple" href="ntp4.book-64.gif" show="embed" actuate="auto"/>
 the first-order sample difference and <IMAGE xml:link="simple" href="ntp4.book-65.gif" show="embed" actuate="auto"/>
 the exponential average,. Then,</Body>
<Equation>
<A ID="pgfId-1018377"></A>
<IMAGE xml:link="simple" href="ntp4.book-66.gif" show="embed" actuate="auto"/>
,</Equation>
<Body>
<A ID="pgfId-1018353"></A>
.where AVG (4) is the averaging parameter in Table <A href="ntp4.xml#id(28764)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">9</A>, is the exponential average at time <IMAGE xml:link="simple" href="ntp4.book-67.gif" show="embed" actuate="auto"/>
. The clock jitter statistic is used by the poll-adjust algorithm above; the clock wander statistic is used only for performance monitoring. It is most useful as a canary to detect stalled fans and failed air conditioning systems.</Body>
<Heading2>
<A ID="pgfId-1015948"></A>
<A ID="21356"></A>
Clock Adjust Process</Heading2>
<Body>
<A ID="pgfId-1013697"></A>
The actual clock adjustment is performed by the clock_adjust() routine shown in Figure <A href="ntp4.xml#id(63321)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">20</A> and Section <A href="ntp4.book.xml#id(67421)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">B.7.1</A>. It runs at one-second intervals to add the frequency offset <Symbol>
φ</Symbol>
 in Figure <A href="ntp4.xml#id(30662)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">19</A> and a fixed percentage of the residual offset <Symbol>
Θ</Symbol>
R. in Figure <A href="ntp4.xml#id(64849)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">18</A>. The <Symbol>
Θ</Symbol>
R. is in effect the exponential decay of the <Symbol>
Θ</Symbol>
 value produced by the loop filter at each update. The TC parameter scales the time constant to match the poll interval for convenience. Note that the dispersion <Symbol>
Ε</Symbol>
 increases by <Symbol>
Φ</Symbol>
 at each second.</Body>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-68.gif" show="embed" actuate="auto"/>
</DIV>
<Body>
<A ID="pgfId-1018749"></A>
The clock adjust process includes a timer interrupt facility driving the system timer <TT>
c.t</TT>
. It begins at zero when the service starts and increments once each second. At each interrupt the clock_adjust() routine is called to incorporate the clock discipline time and frequency adjustments, then the associations are scanned to determine if the system timer equals or exceeds the <TT>
p.next</TT>
 state variable defined in the next section. If so, the poll process is called to send a packet and compute the next <TT>
p.next </TT>
value.</Body>
<Heading1>
<A ID="pgfId-998511"></A>
<A ID="77304"></A>
Poll Process</Heading1>
<Body>
<A ID="pgfId-1013482"></A>
Each association supports a poll process that runs at regular intervals to construct and send packets in symmetric, client and broadcast server associations. It runs continuously, whether or not servers are reachable. The discussion in this section covers only the variables and routines necessary for a conforming NTPv4 implementation. Additional implementation details are in Section <A href="ntp4.book.xml#id(33659)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">B.8</A>. Further details and rationale for the engineering design are discussed in <A href="ntp4.book.xml#id(86162)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">[2]</A>.</Body>
<Heading2>
<A ID="pgfId-1004051"></A>
Poll Process Variables and Parameters</Heading2>
<Body>
<A ID="pgfId-1004151"></A>
The poll process variables are allocated in the association data structure along with the peer process variables. Table <A href="ntp4.xml#id(65174)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">11</A> shows the names, formula names and short definition for each one. Following is a detailed description of the variables, all of which carry the <TT>
p</TT>
 prefix.</Body>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-69.gif" show="embed" actuate="auto"/>
</DIV>
<Hang>
<A ID="pgfId-998523"></A>
<TT>
p.hpoll</TT>
	Signed integer representing the poll exponent, in log2 seconds.</Hang>
<Hang>
<A ID="pgfId-1008076"></A>
<TT>
p.last</TT>
	Integer representing the system timer value when the most recent packet was sent.</Hang>
<Hang>
<A ID="pgfId-1008074"></A>
<TT>
p.next</TT>
	Integer representing the system timer value when the next packet is to be sent.</Hang>
<Hang>
<A ID="pgfId-1005391"></A>
<TT>
p.reach</TT>
	8-bit integer shift register. When a packet is sent, the register is shifted left one bit, with zero entering from the right and overflow bits discarded.</Hang>
<Hang>
<A ID="pgfId-1003761"></A>
<TT>
p.unreach</TT>
	Integer representing the number of seconds the server has been unreachable.</Hang>
<Heading2>
<A ID="pgfId-998525"></A>
Poll Process Operations</Heading2>
<Body>
<A ID="pgfId-1008123"></A>
As described previously, once each second the clock_adjust() routine is called. This routine calls the poll() routine in Figure <A href="ntp4.xml#id(16708)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">21</A> and Section <A href="ntp4.book.xml#id(74485)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">B.8.1</A> for each association in turn. If the time for the next poll message is greater than the system timer, the routine returns immediately. A mode-5 (broadcast server) association always sends a packet, but a mode-6 (broadcast client) association never sends a packet, but runs the routine to update the <TT>
p.reach</TT>
 and <TT>
p.unreach</TT>
 variables. The poll() routine calls the peer_xmit() routine in Figure <A href="ntp4.xml#id(50446)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">22</A> and Section <A href="ntp4.book.xml#id(69143)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">B.8.3</A> to send a packet. If in a burst (<TT>
p.burst</TT>
 &gt; 0), nothing further is done except call the poll_update() routine to set the next poll interval.</Body>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-70.gif" show="embed" actuate="auto"/>
</DIV>
<Body>
<A ID="pgfId-1016019"></A>
If not in a burst, the <TT>
p.reach</TT>
 variable is shifted left by one bit, with zero replacing the rightmost bit. If the server has not been heard for the last three poll intervals, the clock_filter() routine is called to increase the dispersion as described in Section <A href="ntp4.book.xml#id(20894)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">8.3</A>. If the BURST flag is lit and the server is reachable and a valid source of synchronization is available, the client sends a burst of BCOUNT (8) packets at each poll interval. This is useful to accurately measure jitter with long poll intervals. If the IBURST flag is lit and this is the first packet sent when the server becomes unreachable, the client sends a burst. This is useful to quickly reduce the synchronization distance below the distance threshold and synchronize the clock.</Body>
<Body>
<A ID="pgfId-1005445"></A>
The figure also shows the mechanism which backs off the poll interval if the server becomes unreachable. If <TT>
p.reach</TT>
 is nonzero, the server is reachable and <TT>
p.unreach</TT>
 is set to zero; otherwise, <TT>
p.unreach</TT>
 is incremented by one for each poll to the maximum UNREACH (24). Thereafter for each poll <TT>
p.hpoll</TT>
 is increased by one, which doubles the poll interval up to the maximum MAXPOLL determined by the poll_update() routine. When the server again becomes reachable, p.unreach is set to zero, <TT>
p.hpoll</TT>
 is reset to <Symbol>
τ</Symbol>
 and operation resumes normally.</Body>
<Body>
<A ID="pgfId-998526"></A>
When a packet is sent from an association, some header values are copied from the peer variables left by a previous packet and others from the system variables. Figure <A href="ntp4.xml#id(50446)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">22</A> includes a flow diagram and a table showing which values are copied to each header field. In those implementations using floating double data types for root delay and root dispersion, these must be converted to NTP short format. All other fields are either copied intact from peer and system variables or struck as a timestamp from the system clock.</Body>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-71.gif" show="embed" actuate="auto"/>
</DIV>
<Body>
<A ID="pgfId-1016123"></A>
The poll_update() routine shown in Figure <A href="ntp4.xml#id(68263)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">23</A> and Section <A href="ntp4.book.xml#id(32347)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">B.8.2</A> is called when a valid packet is received and immediately after a poll message is sent. If in a burst, the poll interval is fixed at 2 s; otherwise, the host poll exponent is set to the minimum of <TT>
p.poll</TT>
 from the last packet received and <TT>
p.hpoll</TT>
 from the poll() routine, but not less than MINPOLL nor greater than MAXPOLL. Thus the clock discipline can be oversampled, but not undersampled. This is necessary to preserve subnet dynamic behavior and protect against protocol errors. Finally, the poll exponent is converted to an interval which establishes the time at the next poll <TT>
p.next</TT>
.</Body>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-72.gif" show="embed" actuate="auto"/>
</DIV>
<Heading1>
<A ID="pgfId-998546"></A>
References</Heading1>
<EndnoteList>
<Endnote>
<A ID="pgfId-1016461"></A>
<A ID="88741"></A>
Marzullo, K., and S. Owicki. Maintaining the time in a distributed system. ACM Operating Systems Review 19, 3 (July 1985), 44-54.</Endnote>
<Endnote>
<A ID="pgfId-1016458"></A>
<A ID="86162"></A>
Mills, D.L. Computer Network Time Synchronization - the Network Time Protocol. CRC Press, 2006, 304 pp.</Endnote>
</EndnoteList>
<z-Top-of-Form>
<A ID="pgfId-1004745"></A>
</z-Top-of-Form>
<EndnoteList>
<Endnote>
<A ID="pgfId-1006555"></A>
<A ID="91215"></A>
Mills, D.L. The Autokey security architecture, protocol and algorithms. Electrical and Computer Engineering Technical Report 06-1-1, University of Delaware, January 2006, 59 pp.</Endnote>
</EndnoteList>
<z-Top-of-Form>
<A ID="pgfId-1007158"></A>
</z-Top-of-Form>
<EndnoteList>
<Endnote>
<A ID="pgfId-1007156"></A>
<A ID="96511"></A>
Mills, D., D. Plonka and J. Montgomery. Simple network time protocol (SNTP) version 4 for IPv4, IPv6 and OSI. Network Working Group Report RFC-4330, University of Delaware, December 2005, 27 pp. </Endnote>
<Endnote>
<A ID="pgfId-1013150"></A>
<A ID="17819"></A>
Mills, D.L., A. Thyagarajan and B.C. Huffman. Internet timekeeping around the globe. Proc. Precision Time and Time Interval (PTTI) Applications and Planning Meeting (Long Beach CA, December 1997).</Endnote>
<Endnote>
<A ID="pgfId-1013205"></A>
<A ID="47618"></A>
Mills, D.L., Network Time Protocol (Version 3) specification, implementation and analysis. Network Working Group Request for Comments RFC-1305, University of Delaware, March 1992.</Endnote>
</EndnoteList>
<z-Top-of-Form>
<A ID="pgfId-1006570"></A>
</z-Top-of-Form>
<EndnoteList>
<Endnote>
<A ID="pgfId-1006568"></A>
<A ID="35498"></A>
Rivest, R. “The MD5 message-digest algorithm. Network Working Group Request for Comments RFC-1321. MIT Laboratory for Computer Science and RSA Data Security, Inc., April 1992.</Endnote>
</EndnoteList>
<Heading1>
<A ID="pgfId-933085"></A>
<A ID="47726"></A>
NTPv4 Packet Formats</Heading1>
<Body>
<A ID="pgfId-933087"></A>
The NTP packet consists of a number of 32-bit (4 octet) words in network byte order. The packet format consists of three components, the header itself, one or more optional extension fields and an optional message authentication code (MAC). The header component is identical to the NTPv3 header and previous versions. The optional extension fields are used by the Autokey public key cryptographic algorithms described in <A href="ntp4.book.xml#id(91215)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">[3]</A>. The optional MAC is used by both Autokey and the symmetric key cryptographic algorithms described in the main body of this report.</Body>
<Heading2>
<A ID="pgfId-941114"></A>
NTP Header Field Format</Heading2>
<Body>
<A ID="pgfId-941115"></A>
The NTP header format is shown in Figure <A href="ntp4apx.xml#id(10870)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">24</A>, where the size of some multiple-word fields is shown in bits if not the default 32 bits. The header extends from the beginning of the packet to the end of the Transmit Timestamp field. The interpretation of the header fields is shown in the main body of this report. When using the IPv4 address family these fields are backwards compatible with NTPv3. When using the IPv6 address family on an NTPv4 server with a NTPv3 client, the Reference Identifier field appears to be a random value and a timing loop might not be detected. The incidence of this, which would be considered a birthday event, will be very rare.</Body>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-73.gif" show="embed" actuate="auto"/>
</DIV>
<Body>
<A ID="pgfId-960124"></A>
The message authentication code (MAC) consists of a 32-bit Key Identifier followed by a 128-bit Message Digest. The message digest, or cryptosum, is calculated as in RFC-1321 <A href="ntp4.book.xml#id(35498)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">[7]</A> over the fields shown in the figure.</Body>
<Heading2>
<A ID="pgfId-933095"></A>
NTPv4 Extension Field Format</Heading2>
<Body>
<A ID="pgfId-960167"></A>
In NTPv4 one or more extension fields can be inserted after the header and before the MAC, which is always present when extension fields are present. The extension fields can occur in any order; however, in some cases there is a preferred order which improves the protocol efficiency. While previous versions of the Autokey protocol used several different extension field formats, in version 2 of the protocol only a single extension field format is used.</Body>
<Body>
<A ID="pgfId-960178"></A>
An extension field contains a request or response message in the format shown in Figure <A href="ntp4apx.xml#id(40002)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">25</A>. All extension fields are zero-padded to a word (4 octets) boundary. The Length field covers the entire extension field, including the Length and Padding fields. While the minimum field length is 4 words (16 octets), a maximum field length remains to be established. The reference implementation discards any packet with an extension field length more than 1024 octets.</Body>
<DIV>
<IMAGE xml:link="simple" href="ntp4.book-74.gif" show="embed" actuate="auto"/>
</DIV>
<Body>
<A ID="pgfId-933105"></A>
The presence of the MAC and extension fields in the packet is determined from the length of the remaining area after the header to the end of the packet. The parser initializes a pointer just after the header. If the Length field is not a multiple of 4, a format error has occurred and the packet is discarded. The following cases are possible based on the remaining length in words.</Body>
<hang1>
<A ID="pgfId-940417"></A>
0	The packet is not authenticated.
1	The packet is an error report or crypto-NAK.
2, 3, 4	 The packet is discarded with a format error.
5	The remainder of the packet is the MAC.
&gt;5	One or more extension fields are present.</hang1>
<Body>
<A ID="pgfId-933113"></A>
If an extension field is present, the parser examines the Length field. If the length is less than 4 or not a multiple of 4, a format error has occurred and the packet is discarded; otherwise, the parser increments the pointer by this value. The parser now uses the same rules as above to determine whether a MAC is present and/or another extension field. An additional implementation-dependent test is necessary to ensure the pointer does not stray outside the buffer space occupied by the packet.</Body>
<Body>
<A ID="pgfId-933115"></A>
In the Autokey Version 2 format, the 8-bit Code field specifies the request or response operation, while the 6-bit Version Number (VN) field is 2 for the current protocol version. The R bit is lit for a response and the E bit lit for an error. The Timestamp and Filestamp fields carry the seconds field of an NTP timestamp. The Timestamp field establishes the signature epoch of the data field in the message, while the Filestamp field establishes the generation epoch of the file that ultimately produced the data that is signed. The optional Value field carries the data and the optional Signature field the signature that validates the data. Further details are in <A href="ntp4.book.xml#id(91215)" xml:link="simple" show="replace" actuate="user" CLASS="XRef">[3]</A>.</Body>
<Heading1>
<A ID="pgfId-965962"></A>
Code Skeleton</Heading1>
<Body>
<A ID="pgfId-968774"></A>
This appendix is intended to describe the protocol and algorithms of the reference implementation in a general way using what is called a code skeleton program. This consists of a set of definitions, structures and code segments which illustrate the protocol operations without the complexities of the actual reference implementation code. This program is not an executable and is not designed to run in the ordinary sense. It is designed to be compiled only in order to verify consistent variable and type usage. The program is not intended to be fast or compact, just to demonstrate the algorithms with sufficient fidelity to understand how they work.</Body>
<Body>
<A ID="pgfId-968775"></A>
Most of the features of the reference implementation are included in the code skeleton, with the following exceptions: There are no provisions for reference clocks, server discovery or public key (Autokey) cryptography. There is no huff-n’-puff filter, anti-clockhop hysteresis or monitoring provisions. Many of the values that can be tinkered in the reference implementation are assumed constants here. There are only minimal provisions for the kiss-o-death packet and no responding code.</Body>
<Body>
<A ID="pgfId-968975"></A>
The code skeleton consists of five segments, a header segment included by each of the other segments, plus a code segment for the main program and peer, system, clock_adjust and poll processes. These are presented in order below along with definitions and variables specific to each process.</Body>
<Heading2>
<A ID="pgfId-968782"></A>
<A ID="58319"></A>
Global Definitions</Heading2>
<Body>
<A ID="pgfId-968789"></A>
Following are definitions and other data shared by all programs. These values are defined in a header file ntp4.h which is included in all files.</Body>
<Heading3>
<A ID="pgfId-966515"></A>
Definitions, Constants and Parameters</Heading3>
<code>
<A ID="pgfId-966035"></A>
#include &lt;math.h&gt;			/* avoids complaints about sqrt() */</code>
<code>
<A ID="pgfId-966468"></A>
#include &lt;sys/time.h&gt;			/* for gettimeofday() and friends */</code>
<code>
<A ID="pgfId-966469"></A>
#include &lt;stdlib.h&gt;			/* for malloc() and friends */</code>
<code>
<A ID="pgfId-966038"></A>
</code>
<code>
<A ID="pgfId-966039"></A>
/*</code>
<code>
<A ID="pgfId-966040"></A>
 * Data types</code>
<code>
<A ID="pgfId-966041"></A>
 *</code>
<code>
<A ID="pgfId-966042"></A>
 * This program assumes the int data type is 32 bitsand the long data</code>
<code>
<A ID="pgfId-966043"></A>
 * type is 64 bits. The native data type used in most calculations is</code>
<code>
<A ID="pgfId-966044"></A>
 * floating double. The data types used in some packet header fields</code>
<code>
<A ID="pgfId-966045"></A>
 * require conversion to and from this representation. Some header</code>
<code>
<A ID="pgfId-966046"></A>
 * fields involve partitioning an octet, here represeted by individual</code>
<code>
<A ID="pgfId-966047"></A>
 * octets.</code>
<code>
<A ID="pgfId-966048"></A>
 *</code>
<code>
<A ID="pgfId-966049"></A>
 * The 64-bit NTP timestamp format used in timestamp calculations is</code>
<code>
<A ID="pgfId-966050"></A>
 * unsigned seconds and fraction with the decimal point to the left of</code>
<code>
<A ID="pgfId-966051"></A>
 * bit 32. The only operation permitted with these values is</code>
<code>
<A ID="pgfId-966052"></A>
 * subtraction, yielding a signed 31-bit difference. The 32-bit NTP</code>
<code>
<A ID="pgfId-966053"></A>
 * short format used in delay and dispersion calculations is seconds and</code>
<code>
<A ID="pgfId-966054"></A>
 * fraction with the decimal point to the left of bit 16. The only</code>
<code>
<A ID="pgfId-966055"></A>
 * operations permitted with these values are addition and</code>
<code>
<A ID="pgfId-966056"></A>
 * multiplication by a constant.</code>
<code>
<A ID="pgfId-966057"></A>
 *</code>
<code>
<A ID="pgfId-966058"></A>
 * The IPv4 address is 32 bits, while the IPv6 address is 128 bits. The</code>
<code>
<A ID="pgfId-966059"></A>
 * message digest field is 128 bits as constructed by the MD5 algorithm.</code>
<code>
<A ID="pgfId-966060"></A>
 * The precision and poll interval fields are signed log2 seconds. </code>
<code>
<A ID="pgfId-966061"></A>
 */</code>
<code>
<A ID="pgfId-966062"></A>
typedef unsigned long tstamp;				/* NTP timestamp format */</code>
<code>
<A ID="pgfId-966063"></A>
typedef unsigned int tdist;				/* NTP short format */</code>
<code>
<A ID="pgfId-966064"></A>
typedef unsigned long ipaddr;				/* IPv4 or IPv6 address */</code>
<code>
<A ID="pgfId-970170"></A>
typedef unsinged int ipport;				/* IP port number */</code>
<code>
<A ID="pgfId-966065"></A>
typedef unsigned long digest;				/* md5 digest */</code>
<code>
<A ID="pgfId-966066"></A>
typedef signed char s_char;				/* precision and poll interval (log2) */</code>
<code>
<A ID="pgfId-966067"></A>
</code>
<code>
<A ID="pgfId-966068"></A>
/*</code>
<code>
<A ID="pgfId-966069"></A>
 * Arithmetic conversion macroni</code>
<code>
<A ID="pgfId-966070"></A>
 */</code>
<code>
<A ID="pgfId-966071"></A>
#define	LOG2D(a)	((a) &lt; 0 ? 1. / (1L &lt;&lt; -(a)) : \</code>
<code>
<A ID="pgfId-966072"></A>
		1L &lt;&lt; (a))		     /* poll, etc. */</code>
<code>
<A ID="pgfId-966073"></A>
#define	LFP2D(a)	((double)(a) / 0x100000000L) /* NTP timestamp */</code>
<code>
<A ID="pgfId-966074"></A>
#define	D2LFP(a)	((tstamp)((a) * 0x100000000L))</code>
<code>
<A ID="pgfId-966075"></A>
#define	FP2D(a)	(double)(a) / 0x10000L)     /* NTP short */</code>
<code>
<A ID="pgfId-966076"></A>
#define	D2FP(a)	((tdist)((a) * 0x10000L))</code>
<code>
<A ID="pgfId-966077"></A>
#define	SQUARE(x)	(x * x)</code>
<code>
<A ID="pgfId-966078"></A>
#define	SQRT(x)	(sqrt(x))</code>
<code>
<A ID="pgfId-966079"></A>
</code>
<code>
<A ID="pgfId-966080"></A>
/*</code>
<code>
<A ID="pgfId-966081"></A>
 * Global constants. Some of these might be converted to variables</code>
<code>
<A ID="pgfId-966082"></A>
 * which can be tinkered by configuration or computed on-fly. For</code>
<code>
<A ID="pgfId-966083"></A>
 * instance, the reference implementation computes PRECISION on-fly and</code>
<code>
<A ID="pgfId-966084"></A>
 * provides performance tuning for the defines marked with % below. </code>
<code>
<A ID="pgfId-966085"></A>
 */</code>
<code>
<A ID="pgfId-966086"></A>
#define	VERSION		4	/* version number */</code>
<code>
<A ID="pgfId-966087"></A>
#define	PORT		123	/* NTP poert number */</code>
<code>
<A ID="pgfId-970160"></A>
#define	MINDISP		.01	/* % minimum dispersion (s) */</code>
<code>
<A ID="pgfId-966088"></A>
#define	MAXDISP		16	/* % maximum dispersion (s) */</code>
<code>
<A ID="pgfId-966089"></A>
#define	MAXDIST		1	/* % distance threshold (s) */</code>
<code>
<A ID="pgfId-966090"></A>
#define	NOSYNC		3	/* leap unsync */</code>
<code>
<A ID="pgfId-966091"></A>
#define	MAXSTRAT		16	/* maximum stratum (infinity metric) */</code>
<code>
<A ID="pgfId-966092"></A>
#define	MINPOLL		4	/* % minimum poll interval (64 s)*/</code>
<code>
<A ID="pgfId-966093"></A>
#define	MAXPOLL		17	/* % maximum poll interval (36.4 h) */</code>
<code>
<A ID="pgfId-966094"></A>
#define	PHI		15e-6	/* % frequency tolerance (15 PPM) */</code>
<code>
<A ID="pgfId-966095"></A>
#define	NSTAGE		8	/* clock register stages */</code>
<code>
<A ID="pgfId-966096"></A>
#define	NMAX		50	/* % maximum number of peers */</code>
<code>
<A ID="pgfId-966097"></A>
#define	NSANE		1	/* % minimum intersection survivors */</code>
<code>
<A ID="pgfId-966098"></A>
#define	NMIN		3	/* % minimum cluster survivors */</code>
<code>
<A ID="pgfId-966099"></A>
</code>
<code>
<A ID="pgfId-966100"></A>
/*</code>
<code>
<A ID="pgfId-966101"></A>
 * Global return values</code>
<code>
<A ID="pgfId-966102"></A>
 */</code>
<code>
<A ID="pgfId-966103"></A>
#define	TRUE		1	/* boolean true */</code>
<code>
<A ID="pgfId-966104"></A>
#define	FALSE		0	/* boolean false */</code>
<code>
<A ID="pgfId-966105"></A>
#define	NULL		0	/* empty pointer */</code>
<code>
<A ID="pgfId-966106"></A>
</code>
<code>
<A ID="pgfId-966107"></A>
/*</code>
<code>
<A ID="pgfId-966108"></A>
 * Local clock process return codes</code>
<code>
<A ID="pgfId-966109"></A>
 */</code>
<code>
<A ID="pgfId-966110"></A>
#define	IGNORE		0	/* ignore */</code>
<code>
<A ID="pgfId-966111"></A>
#define	SLEW		1	/* slew adjustment */</code>
<code>
<A ID="pgfId-966112"></A>
#define	STEP		2	/* step adjustment */</code>
<code>
<A ID="pgfId-966113"></A>
#define	PANIC		3	/* panic - no adjustment */</code>
<code>
<A ID="pgfId-966114"></A>
</code>
<code>
<A ID="pgfId-966115"></A>
/*</code>
<code>
<A ID="pgfId-966116"></A>
 * System flags</code>
<code>
<A ID="pgfId-966117"></A>
 */</code>
<code>
<A ID="pgfId-966118"></A>
#define	S_FLAGS		0	/* any system flags */</code>
<code>
<A ID="pgfId-966119"></A>
#define	S_BCSTENAB		0x1	/* enable broadcast client */</code>
<code>
<A ID="pgfId-966120"></A>
</code>
<code>
<A ID="pgfId-966121"></A>
/*</code>
<code>
<A ID="pgfId-966122"></A>
 * Peer flags</code>
<code>
<A ID="pgfId-966123"></A>
 */</code>
<code>
<A ID="pgfId-966124"></A>
#define	P_FLAGS		0	/* any peer flags */</code>
<code>
<A ID="pgfId-966125"></A>
#define	P_EPHEM		0x01	/* association is ephemeral */</code>
<code>
<A ID="pgfId-966126"></A>
#define	P_BURST		0x02	/* burst enable */</code>
<code>
<A ID="pgfId-966127"></A>
#define	P_IBURST		0x04	/* intial burst enable */</code>
<code>
<A ID="pgfId-966128"></A>
#define	P_NOTRUST		0x08	/* authenticated access */</code>
<code>
<A ID="pgfId-966129"></A>
#define	P_NOPEER		0x10	/* authenticated mobilization */</code>
<code>
<A ID="pgfId-966130"></A>
</code>
<code>
<A ID="pgfId-966131"></A>
/*</code>
<code>
<A ID="pgfId-966132"></A>
 * Authentication codes</code>
<code>
<A ID="pgfId-966133"></A>
 */</code>
<code>
<A ID="pgfId-966134"></A>
#define	A_NONE		0	/* no authentication */</code>
<code>
<A ID="pgfId-966135"></A>
#define	A_OK		1	/* authentication OK */</code>
<code>
<A ID="pgfId-966136"></A>
#define	A_ERROR		2	/* authentication error */</code>
<code>
<A ID="pgfId-966137"></A>
#define	A_CRYPTO		3	/* crypto-NAK */</code>
<code>
<A ID="pgfId-966138"></A>
</code>
<code>
<A ID="pgfId-966139"></A>
/*</code>
<code>
<A ID="pgfId-966140"></A>
 * Association state codes</code>
<code>
<A ID="pgfId-966141"></A>
 */</code>
<code>
<A ID="pgfId-966142"></A>
#define	X_INIT		0	/* initialization */</code>
<code>
<A ID="pgfId-966143"></A>
#define	X_STALE		1	/* timeout */</code>
<code>
<A ID="pgfId-966144"></A>
#define	X_STEP		2	/* time step */</code>
<code>
<A ID="pgfId-966145"></A>
#define	X_ERROR		3	/* authentication error */</code>
<code>
<A ID="pgfId-966146"></A>
#define	X_CRYPTO		4	/* crypto-NAK received */</code>
<code>
<A ID="pgfId-966147"></A>
#define	X_NKEY		5	/* untrusted key */</code>
<code>
<A ID="pgfId-966148"></A>
</code>
<code>
<A ID="pgfId-966149"></A>
/*</code>
<code>
<A ID="pgfId-966150"></A>
 * Protocol mode definitionss</code>
<code>
<A ID="pgfId-966151"></A>
 */</code>
<code>
<A ID="pgfId-966152"></A>
#define	M_RSVD		0	/* reserved */</code>
<code>
<A ID="pgfId-966153"></A>
#define	M_SACT		1	/* symmetric active */</code>
<code>
<A ID="pgfId-966154"></A>
#define	M_PASV		2	/* symmetric passive */</code>
<code>
<A ID="pgfId-966155"></A>
#define	M_CLNT		3	/* client */</code>
<code>
<A ID="pgfId-966156"></A>
#define	M_SERV		4	/* server */</code>
<code>
<A ID="pgfId-966157"></A>
#define	M_BCST		5	/* broadcast server */</code>
<code>
<A ID="pgfId-966158"></A>
#define	M_BCLN		6	/* broadcast client */</code>
<code>
<A ID="pgfId-966159"></A>
</code>
<code>
<A ID="pgfId-966160"></A>
/*</code>
<code>
<A ID="pgfId-966161"></A>
 * Clock state definitions</code>
<code>
<A ID="pgfId-966162"></A>
 */</code>
<code>
<A ID="pgfId-966163"></A>
#define NSET		0	/* clock never set */</code>
<code>
<A ID="pgfId-966164"></A>
#define FSET		1	/* frequency set from file */</code>
<code>
<A ID="pgfId-966165"></A>
#define SPIK		2	/* spike detected */</code>
<code>
<A ID="pgfId-966166"></A>
#define FREQ		3	/* frequency mode */</code>
<code>
<A ID="pgfId-966167"></A>
#define SYNC		4	/* clock synchronized */</code>
<code>
<A ID="pgfId-966169"></A>
</code>
<Heading3>
<A ID="pgfId-968613"></A>
Packet Data Structures</Heading3>
<code>
<A ID="pgfId-966170"></A>
/*</code>
<code>
<A ID="pgfId-966173"></A>
 * The receive and transmit packets may contain an optional message</code>
<code>
<A ID="pgfId-966174"></A>
 * authentication code (MAC) consisting of a key identifier (keyid) and</code>
<code>
<A ID="pgfId-966175"></A>
 * message digest (mac). NTPv4 supports optional extension fields which</code>
<code>
<A ID="pgfId-966176"></A>
 * are inserted after the the header and before the MAC, but these are</code>
<code>
<A ID="pgfId-966177"></A>
 * not described here.</code>
<code>
<A ID="pgfId-966178"></A>
 *</code>
<code>
<A ID="pgfId-966179"></A>
 * Receive packet</code>
<code>
<A ID="pgfId-966180"></A>
 *</code>
<code>
<A ID="pgfId-966181"></A>
 * Note the dst timestamp is not part of the packet itself. It is</code>
<code>
<A ID="pgfId-966182"></A>
 * captured upon arrival and returned in the receive buffer along with</code>
<code>
<A ID="pgfId-966183"></A>
 * the buffer length and data. Note that some of the char fields are</code>
<code>
<A ID="pgfId-966184"></A>
 * packed in the actual header, but the details are omited here.</code>
<code>
<A ID="pgfId-966185"></A>
 */</code>
<code>
<A ID="pgfId-966186"></A>
struct r {</code>
<code>
<A ID="pgfId-966187"></A>
	ipaddr	srcaddr;		/* source (remote) address */</code>
<code>
<A ID="pgfId-966188"></A>
	ipaddr	dstaddr;		/* destination (local) address */</code>
<code>
<A ID="pgfId-966189"></A>
	char	version;		/* version number */</code>
<code>
<A ID="pgfId-966190"></A>
	char	leap;		/* leap indicator */</code>
<code>
<A ID="pgfId-966191"></A>
	char	mode;		/* mode */</code>
<code>
<A ID="pgfId-966192"></A>
	char	stratum;		/* stratum */</code>
<code>
<A ID="pgfId-966193"></A>
	char	poll;		/* poll interval */</code>
<code>
<A ID="pgfId-966194"></A>
	s_char	precision;		/* precision */</code>
<code>
<A ID="pgfId-966195"></A>
	tdist	rootdelay;		/* root delay */</code>
<code>
<A ID="pgfId-966196"></A>
	tdist	rootdisp;		/* root dispersion */</code>
<code>
<A ID="pgfId-966197"></A>
	char	refid;		/* reference ID */</code>
<code>
<A ID="pgfId-966198"></A>
	tstamp	reftime;		/* reference time */</code>
<code>
<A ID="pgfId-966199"></A>
	tstamp	org;		/* origin timestamp */</code>
<code>
<A ID="pgfId-966200"></A>
	tstamp	rec;		/* receive timestamp */</code>
<code>
<A ID="pgfId-966201"></A>
	tstamp	xmt;		/* transmit timestamp */</code>
<code>
<A ID="pgfId-966202"></A>
	int	keyid;		/* key ID */</code>
<code>
<A ID="pgfId-966203"></A>
	digest	digest;		/* message digest */</code>
<code>
<A ID="pgfId-966204"></A>
	tstamp	dst;		/* destination timestamp */</code>
<code>
<A ID="pgfId-966205"></A>
} r;</code>
<code>
<A ID="pgfId-966206"></A>
</code>
<code>
<A ID="pgfId-966207"></A>
/*</code>
<code>
<A ID="pgfId-966208"></A>
 * Transmit packet</code>
<code>
<A ID="pgfId-966209"></A>
 */</code>
<code>
<A ID="pgfId-966210"></A>
struct x {</code>
<code>
<A ID="pgfId-966211"></A>
	ipaddr	dstaddr;		/* source (local) address */</code>
<code>
<A ID="pgfId-966212"></A>
	ipaddr	srcaddr;		/* destination (remote) address */</code>
<code>
<A ID="pgfId-966213"></A>
	char	version;		/* version number */</code>
<code>
<A ID="pgfId-966214"></A>
	char	leap;		/* leap indicator */</code>
<code>
<A ID="pgfId-966215"></A>
	char	mode;		/* mode */</code>
<code>
<A ID="pgfId-966216"></A>
	char	stratum;		/* stratum */</code>
<code>
<A ID="pgfId-966217"></A>
	char	poll;		/* poll interval */</code>
<code>
<A ID="pgfId-966218"></A>
	s_char	precision;		/* precision */</code>
<code>
<A ID="pgfId-966219"></A>
	tdist	rootdelay;		/* root delay */</code>
<code>
<A ID="pgfId-966220"></A>
	tdist	rootdisp;		/* root dispersion */</code>
<code>
<A ID="pgfId-966221"></A>
	char	refid;		/* reference ID */</code>
<code>
<A ID="pgfId-966222"></A>
	tstamp	reftime;		/* reference time */</code>
<code>
<A ID="pgfId-966223"></A>
	tstamp	org;		/* origin timestamp */</code>
<code>
<A ID="pgfId-966224"></A>
	tstamp	rec;		/* receive timestamp */</code>
<code>
<A ID="pgfId-966225"></A>
	tstamp	xmt;		/* transmit timestamp */</code>
<code>
<A ID="pgfId-966226"></A>
	int	keyid;		/* key ID */</code>
<code>
<A ID="pgfId-966227"></A>
	digest	digest;		/* message digest */</code>
<code>
<A ID="pgfId-966228"></A>
} x;</code>
<code>
<A ID="pgfId-966229"></A>
</code>
<code>
<A ID="pgfId-966230"></A>
</code>
<Heading3>
<A ID="pgfId-968619"></A>
Association Data Structures</Heading3>
<code>
<A ID="pgfId-966231"></A>
/*</code>
<code>
<A ID="pgfId-966234"></A>
 * Filter stage structure. Note the t member in this and other</code>
<code>
<A ID="pgfId-966235"></A>
 * structures refers to process time, not real time. Process time</code>
<code>
<A ID="pgfId-966236"></A>
 * increments by one second for every elapsed second of real time.</code>
<code>
<A ID="pgfId-966237"></A>
 */</code>
<code>
<A ID="pgfId-966238"></A>
struct f {</code>
<code>
<A ID="pgfId-966239"></A>
	tstamp	t;		/* update time */</code>
<code>
<A ID="pgfId-966240"></A>
	double	offset;		/* clock ofset */</code>
<code>
<A ID="pgfId-966241"></A>
	double	delay;		/* roundtrip delay */</code>
<code>
<A ID="pgfId-966242"></A>
	double	disp;		/* dispersion */</code>
<code>
<A ID="pgfId-966243"></A>
} f;</code>
<code>
<A ID="pgfId-966244"></A>
</code>
<code>
<A ID="pgfId-966245"></A>
/*</code>
<code>
<A ID="pgfId-966246"></A>
 * Association structure. This is shared between the peer process and</code>
<code>
<A ID="pgfId-966247"></A>
 * poll process.</code>
<code>
<A ID="pgfId-966248"></A>
 */</code>
<code>
<A ID="pgfId-966249"></A>
struct p {</code>
<code>
<A ID="pgfId-966250"></A>
</code>
<code>
<A ID="pgfId-966251"></A>
	/*</code>
<code>
<A ID="pgfId-966252"></A>
	 * Variables set by configuration</code>
<code>
<A ID="pgfId-966253"></A>
	 */</code>
<code>
<A ID="pgfId-966254"></A>
	ipaddr	srcaddr;		/* source (remote) address */</code>
<code>
<A ID="pgfId-970168"></A>
	ipport	srcport;		/* source port number *.</code>
<code>
<A ID="pgfId-966255"></A>
	ipaddr	dstaddr;		/* destination (local) address */</code>
<code>
<A ID="pgfId-970169"></A>
	ipport	dstport;		/* destination port number */</code>
<code>
<A ID="pgfId-966256"></A>
	char	version;		/* version number */</code>
<code>
<A ID="pgfId-966257"></A>
	char	mode;		/* mode */</code>
<code>
<A ID="pgfId-966258"></A>
	int	keyid;		/* key identifier */</code>
<code>
<A ID="pgfId-966259"></A>
	int	flags;		/* option flags */</code>
<code>
<A ID="pgfId-966260"></A>
</code>
<code>
<A ID="pgfId-966261"></A>
	/*</code>
<code>
<A ID="pgfId-966262"></A>
	 * Variables set by received packet</code>
<code>
<A ID="pgfId-966263"></A>
	 */</code>
<code>
<A ID="pgfId-966264"></A>
	char	leap;		/* leap indicator */</code>
<code>
<A ID="pgfId-966265"></A>
	char	mode;		/* mode */</code>
<code>
<A ID="pgfId-966266"></A>
	char	stratum;		/* stratum */</code>
<code>
<A ID="pgfId-966267"></A>
	char	ppoll;		/* peer poll interval */</code>
<code>
<A ID="pgfId-966268"></A>
	double	rootdelay;		/* root delay */</code>
<code>
<A ID="pgfId-966269"></A>
	double	rootdisp;		/* root dispersion */</code>
<code>
<A ID="pgfId-966270"></A>
	char	refid;		/* reference ID */</code>
<code>
<A ID="pgfId-966271"></A>
	tstamp	reftime;		/* reference time */</code>
<code>
<A ID="pgfId-966272"></A>
#define	begin_clear org			/* beginning of clear area */</code>
<code>
<A ID="pgfId-966273"></A>
	tstamp	org;		/* originate timestamp */</code>
<code>
<A ID="pgfId-966274"></A>
	tstamp	rec;		/* receive timestamp */</code>
<code>
<A ID="pgfId-966275"></A>
	tstamp	xmt;		/* transmit timestamp */</code>
<code>
<A ID="pgfId-966276"></A>
</code>
<code>
<A ID="pgfId-966277"></A>
	/*</code>
<code>
<A ID="pgfId-966278"></A>
	 * Computed data</code>
<code>
<A ID="pgfId-966279"></A>
	 */</code>
<code>
<A ID="pgfId-966280"></A>
	double	t;		/* update time */</code>
<code>
<A ID="pgfId-966281"></A>
	struct f f[NSTAGE];			/* clock filter */</code>
<code>
<A ID="pgfId-966282"></A>
	double	offset;		/* peer offset */</code>
<code>
<A ID="pgfId-966283"></A>
	double	delay;		/* peer delay */</code>
<code>
<A ID="pgfId-966284"></A>
	double	disp;		/* peer dispersion */</code>
<code>
<A ID="pgfId-966285"></A>
	double	jitter;		/* RMS jitter */</code>
<code>
<A ID="pgfId-966286"></A>
</code>
<code>
<A ID="pgfId-966287"></A>
	/*</code>
<code>
<A ID="pgfId-966288"></A>
	 * Poll process variables</code>
<code>
<A ID="pgfId-966289"></A>
	 */</code>
<code>
<A ID="pgfId-966290"></A>
	char	hpoll;		/* host poll interval */</code>
<code>
<A ID="pgfId-966291"></A>
	int	burst;		/* burst counter */</code>
<code>
<A ID="pgfId-966292"></A>
	int	reach;		/* reach register */</code>
<code>
<A ID="pgfId-966293"></A>
#define	end_clear unreach			/* end of clear area */</code>
<code>
<A ID="pgfId-966294"></A>
	int	unreach;		/* unreach counter */</code>
<code>
<A ID="pgfId-966295"></A>
	int	last;		/* last poll time */</code>
<code>
<A ID="pgfId-966296"></A>
	int	next;		/* next poll time */</code>
<code>
<A ID="pgfId-966297"></A>
} p;</code>
<code>
<A ID="pgfId-966299"></A>
</code>
<Heading3>
<A ID="pgfId-968623"></A>
System Data Structures</Heading3>
<code>
<A ID="pgfId-966300"></A>
/*</code>
<code>
<A ID="pgfId-966303"></A>
 * Chime list. This is used by the intersection algorithm.</code>
<code>
<A ID="pgfId-966304"></A>
 */</code>
<code>
<A ID="pgfId-966305"></A>
struct m {				/* m is for Marzullo */</code>
<code>
<A ID="pgfId-966306"></A>
	struct p *p;			/* peer structure pointer */</code>
<code>
<A ID="pgfId-966307"></A>
	int	type;		/* high +1, mid 0, low -1 */</code>
<code>
<A ID="pgfId-966308"></A>
	double	edge;		/* correctness interval edge */</code>
<code>
<A ID="pgfId-966309"></A>
} m;</code>
<code>
<A ID="pgfId-966310"></A>
</code>
<code>
<A ID="pgfId-966311"></A>
/*</code>
<code>
<A ID="pgfId-966312"></A>
 * Survivor list. This is used by the clustering algorithm.</code>
<code>
<A ID="pgfId-966313"></A>
 */</code>
<code>
<A ID="pgfId-966314"></A>
struct v {</code>
<code>
<A ID="pgfId-966315"></A>
	struct p *p;			/* peer structure pointer */</code>
<code>
<A ID="pgfId-966316"></A>
	double	metric;		/* sort metric */</code>
<code>
<A ID="pgfId-966317"></A>
} v;</code>
<code>
<A ID="pgfId-966318"></A>
</code>
<code>
<A ID="pgfId-966319"></A>
</code>
<code>
<A ID="pgfId-966320"></A>
/*</code>
<code>
<A ID="pgfId-966321"></A>
 * System structure</code>
<code>
<A ID="pgfId-966322"></A>
 */</code>
<code>
<A ID="pgfId-966323"></A>
struct s {</code>
<code>
<A ID="pgfId-966324"></A>
	tstamp	t;		/* update time */</code>
<code>
<A ID="pgfId-966325"></A>
	char	leap;		/* leap indicator */</code>
<code>
<A ID="pgfId-966326"></A>
	char	stratum;		/* stratum */</code>
<code>
<A ID="pgfId-966327"></A>
	char	poll;		/* poll interval */</code>
<code>
<A ID="pgfId-966328"></A>
	char	precision;		/* precision */</code>
<code>
<A ID="pgfId-966329"></A>
	double	rootdelay;		/* root delay */</code>
<code>
<A ID="pgfId-966330"></A>
	double	rootdisp;		/* root dispersion */</code>
<code>
<A ID="pgfId-966331"></A>
	char	refid;		/* reference ID */</code>
<code>
<A ID="pgfId-966332"></A>
	tstamp	reftime;		/* reference time */</code>
<code>
<A ID="pgfId-966333"></A>
	struct m m[NMAX];			/* chime list */</code>
<code>
<A ID="pgfId-966334"></A>
	struct v v[NMAX];			/* survivor list */</code>
<code>
<A ID="pgfId-966335"></A>
	struct p *p;			/* association ID */</code>
<code>
<A ID="pgfId-966336"></A>
	double	offset;		/* combined offset */</code>
<code>
<A ID="pgfId-966337"></A>
	double	jitter;		/* combined jitter */</code>
<code>
<A ID="pgfId-966338"></A>
	int	flags;		/* option flags */</code>
<code>
<A ID="pgfId-966339"></A>
} s;</code>
<code>
<A ID="pgfId-966340"></A>
</code>
<Heading3>
<A ID="pgfId-966341"></A>
Local Clock Data Structure</Heading3>
<code>
<A ID="pgfId-966342"></A>
/*</code>
<code>
<A ID="pgfId-966345"></A>
 * Local clock structure</code>
<code>
<A ID="pgfId-966346"></A>
 */</code>
<code>
<A ID="pgfId-966347"></A>
struct c {</code>
<code>
<A ID="pgfId-966348"></A>
	tstamp	t;		/* update time */</code>
<code>
<A ID="pgfId-966349"></A>
	int	state;		/* current state */</code>
<code>
<A ID="pgfId-966350"></A>
	double	offset;		/* current offset */</code>
<code>
<A ID="pgfId-966351"></A>
	double	base;		/* base offset */</code>
<code>
<A ID="pgfId-966352"></A>
	double	last;		/* previous offset */</code>
<code>
<A ID="pgfId-966353"></A>
	int	count;		/* jiggle counter */</code>
<code>
<A ID="pgfId-966354"></A>
	double	freq;		/* frequency */</code>
<code>
<A ID="pgfId-966355"></A>
	double	jitter;		/* RMS jitter */</code>
<code>
<A ID="pgfId-966356"></A>
	double	wander;		/* RMS wander */</code>
<code>
<A ID="pgfId-966357"></A>
} c;</code>
<code>
<A ID="pgfId-966358"></A>
</code>
<Heading3>
<A ID="pgfId-966359"></A>
Function Prototypes</Heading3>
<code>
<A ID="pgfId-966360"></A>
/*</code>
<code>
<A ID="pgfId-966363"></A>
 * Peer process</code>
<code>
<A ID="pgfId-966364"></A>
 */</code>
<code>
<A ID="pgfId-966365"></A>
void	receive(struct r *);			/* receive packet */</code>
<code>
<A ID="pgfId-969670"></A>
void	fast_xmit(struct r *, int, int); /* transmit a reply packet */</code>
<code>
<A ID="pgfId-969843"></A>
struct p *find_assoc(struct r *);				 /* search the association table */</code>
<code>
<A ID="pgfId-966366"></A>
void	packet(struct p *, struct r *); /* process packet */</code>
<code>
<A ID="pgfId-966367"></A>
void	clock_filter(struct p *, double, double, double); /* filter */</code>
<code>
<A ID="pgfId-966369"></A>
int	accept(struct p *);			/* determine fitness of server */</code>
<code>
<A ID="pgfId-966371"></A>
int	access(struct r *);			/* determine access restrictions */</code>
<code>
<A ID="pgfId-966372"></A>
</code>
<code>
<A ID="pgfId-966373"></A>
/*</code>
<code>
<A ID="pgfId-966374"></A>
 * System process</code>
<code>
<A ID="pgfId-966375"></A>
 */</code>
<code>
<A ID="pgfId-966377"></A>
void	clock_select();			/* find the best clocks */</code>
<code>
<A ID="pgfId-966378"></A>
void	clock_update(struct p *);			/* update the system clock */</code>
<code>
<A ID="pgfId-966379"></A>
void	clock_combine();			/* combine the offsets */</code>
<code>
<A ID="pgfId-969902"></A>
double	root_dist(struct p *);			/* calculate root distance */</code>
<code>
<A ID="pgfId-966380"></A>
</code>
<code>
<A ID="pgfId-966381"></A>
/*</code>
<code>
<A ID="pgfId-966382"></A>
 * Clock discipline process</code>
<code>
<A ID="pgfId-966383"></A>
 */</code>
<code>
<A ID="pgfId-966384"></A>
int	local_clock(struct p *, double); /* clock discipline */</code>
<code>
<A ID="pgfId-966385"></A>
void	rstclock(int, double, double); /* clock state transition */</code>
<code>
<A ID="pgfId-966386"></A>
</code>
<code>
<A ID="pgfId-966387"></A>
/*</code>
<code>
<A ID="pgfId-966388"></A>
 * Clock adjust process</code>
<code>
<A ID="pgfId-966389"></A>
 */ </code>
<code>
<A ID="pgfId-966390"></A>
void	clock_adjust();		/* one-second timer process */</code>
<code>
<A ID="pgfId-966391"></A>
</code>
<code>
<A ID="pgfId-968682"></A>
/*</code>
<code>
<A ID="pgfId-966393"></A>
 * Poll process</code>
<code>
<A ID="pgfId-966394"></A>
 */</code>
<code>
<A ID="pgfId-966395"></A>
void	poll(struct p *);			/* poll process */</code>
<code>
<A ID="pgfId-966396"></A>
void	poll_update(struct p *, int); /* update the poll interval */</code>
<code>
<A ID="pgfId-966397"></A>
void	peer_xmit(struct p *);			/* transmit a packet */</code>
<code>
<A ID="pgfId-966399"></A>
</code>
<code>
<A ID="pgfId-968689"></A>
/*</code>
<code>
<A ID="pgfId-966401"></A>
 * Main program and utility routines</code>
<code>
<A ID="pgfId-966402"></A>
 */</code>
<code>
<A ID="pgfId-969861"></A>
int	main();			/* main program */</code>
<code>
<A ID="pgfId-966404"></A>
struct p *mobilize(ipaddr, ipaddr, int, int, int, int); /* mobilize */</code>
<code>
<A ID="pgfId-969849"></A>
void	clear(struct p *, int);			/* clear association */</code>
<code>
<A ID="pgfId-969855"></A>
digest	md5(int);			/* generate a message digest */</code>
<code>
<A ID="pgfId-966406"></A>
</code>
<code>
<A ID="pgfId-966407"></A>
/*</code>
<code>
<A ID="pgfId-966408"></A>
 * Kernel I/O Interface</code>
<code>
<A ID="pgfId-966409"></A>
 */</code>
<code>
<A ID="pgfId-966410"></A>
struct r *recv_packet();				/* wait for packet */</code>
<code>
<A ID="pgfId-966411"></A>
void	xmit_packet(struct x *);			/* send packet */</code>
<code>
<A ID="pgfId-966412"></A>
</code>
<code>
<A ID="pgfId-969836"></A>
.*</code>
<code>
<A ID="pgfId-969837"></A>
 * Kernel system clock interface</code>
<code>
<A ID="pgfId-969838"></A>
 */</code>
<code>
<A ID="pgfId-969839"></A>
void	step_time(double);			/* step time */</code>
<code>
<A ID="pgfId-966413"></A>
void	adjust_time(double);			/* adjust (slew) time */</code>
<code>
<A ID="pgfId-966414"></A>
tstamp	get_time();			/* read time */</code>
<code>
<A ID="pgfId-966561"></A>
</code>
<Heading2>
<A ID="pgfId-966557"></A>
<A ID="50403"></A>
Main Program and Utility Routines</Heading2>
<code>
<A ID="pgfId-966571"></A>
#include “ntp4.h”</code>
<code>
<A ID="pgfId-966572"></A>
</code>
<code>
<A ID="pgfId-966589"></A>
/*</code>
<code>
<A ID="pgfId-966590"></A>
 * Definitions</code>
<code>
<A ID="pgfId-966591"></A>
 */</code>
<code>
<A ID="pgfId-966592"></A>
#define	PRECISION	-18		/* precision (log2 s)  */</code>
<code>
<A ID="pgfId-966593"></A>
#define IPADDR		0		/* any IP address */</code>
<code>
<A ID="pgfId-966594"></A>
#define MODE		0		/* any NTP mode */</code>
<code>
<A ID="pgfId-966595"></A>
#define KEYID		0		/* any key identifier */</code>
<code>
<A ID="pgfId-966596"></A>
</code>
<code>
<A ID="pgfId-966597"></A>
/*</code>
<code>
<A ID="pgfId-966598"></A>
 * main() - main program</code>
<code>
<A ID="pgfId-966599"></A>
 */</code>
<code>
<A ID="pgfId-966600"></A>
int</code>
<code>
<A ID="pgfId-966601"></A>
main()</code>
<code>
<A ID="pgfId-966602"></A>
{</code>
<code>
<A ID="pgfId-966603"></A>
	struct p *p;			/* peer structure pointer */</code>
<code>
<A ID="pgfId-966604"></A>
	struct r *r;			/* receive packet pointer */</code>
<code>
<A ID="pgfId-966605"></A>
</code>
<code>
<A ID="pgfId-966606"></A>
	/*</code>
<code>
<A ID="pgfId-966607"></A>
	 * Read command line options and initialize system variables.</code>
<code>
<A ID="pgfId-966608"></A>
	 * The reference implementation measures the precision specific</code>
<code>
<A ID="pgfId-966609"></A>
	 * to each machine by measuring the clock increments to read the</code>
<code>
<A ID="pgfId-966610"></A>
	 * system clock.</code>
<code>
<A ID="pgfId-966611"></A>
	 */</code>
<code>
<A ID="pgfId-966612"></A>
	memset(&amp;s, sizeof(s), 0);</code>
<code>
<A ID="pgfId-966613"></A>
	s.leap = NOSYNC;</code>
<code>
<A ID="pgfId-966614"></A>
	s.stratum = MAXSTRAT;</code>
<code>
<A ID="pgfId-966615"></A>
	s.poll = MINPOLL;</code>
<code>
<A ID="pgfId-966616"></A>
	s.precision = PRECISION;</code>
<code>
<A ID="pgfId-966617"></A>
	s.p = NULL;</code>
<code>
<A ID="pgfId-966618"></A>
</code>
<code>
<A ID="pgfId-966619"></A>
	/*</code>
<code>
<A ID="pgfId-966620"></A>
	 * Initialize local clock variables</code>
<code>
<A ID="pgfId-966621"></A>
	 */</code>
<code>
<A ID="pgfId-966622"></A>
	memset(&amp;c, sizeof(c), 0);</code>
<code>
<A ID="pgfId-966623"></A>
	if (/* frequency file */ 0) {</code>
<code>
<A ID="pgfId-966624"></A>
		c.freq = /* freq */ 0;</code>
<code>
<A ID="pgfId-966625"></A>
		rstclock(FSET, 0, 0);</code>
<code>
<A ID="pgfId-966626"></A>
	} else {</code>
<code>
<A ID="pgfId-966627"></A>
		rstclock(NSET, 0, 0);</code>
<code>
<A ID="pgfId-966628"></A>
	}</code>
<code>
<A ID="pgfId-966629"></A>
	c.jitter = LOG2D(s.precision);</code>
<code>
<A ID="pgfId-966630"></A>
</code>
<code>
<A ID="pgfId-966631"></A>
	/*</code>
<code>
<A ID="pgfId-966632"></A>
	 * Read the configuration file and mobilize persistent</code>
<code>
<A ID="pgfId-966633"></A>
	 * associations with spcified addresses, version, mode, key ID</code>
<code>
<A ID="pgfId-966634"></A>
	 * and flags.</code>
<code>
<A ID="pgfId-966635"></A>
	 */ </code>
<code>
<A ID="pgfId-966636"></A>
	while (/* mobilize configurated associations */ 0) {		</code>
<code>
<A ID="pgfId-966637"></A>
		p = mobilize(IPADDR, IPADDR, VERSION, MODE, KEYID,</code>
<code>
<A ID="pgfId-966638"></A>
		    P_FLAGS);</code>
<code>
<A ID="pgfId-966639"></A>
	}</code>
<code>
<A ID="pgfId-966640"></A>
</code>
<code>
<A ID="pgfId-966641"></A>
	/*</code>
<code>
<A ID="pgfId-966642"></A>
	 * Start the system timer, which ticks once per second. Then</code>
<code>
<A ID="pgfId-966643"></A>
	 * read packets as they arrive, strike receive timestamp and</code>
<code>
<A ID="pgfId-966644"></A>
	 * call the receive() routine.</code>
<code>
<A ID="pgfId-966645"></A>
	 */</code>
<code>
<A ID="pgfId-966646"></A>
	while (0) {</code>
<code>
<A ID="pgfId-966647"></A>
		r = recv_packet();</code>
<code>
<A ID="pgfId-966648"></A>
		r-&gt;dst = get_time();</code>
<code>
<A ID="pgfId-966649"></A>
		receive(r);</code>
<code>
<A ID="pgfId-966650"></A>
	}</code>
<code>
<A ID="pgfId-966651"></A>
}</code>
<code>
<A ID="pgfId-966652"></A>
</code>
<code>
<A ID="pgfId-968814"></A>
/*
 * mobilize() - mobilize and initialize an association
 */
struct p</code>
<code>
<A ID="pgfId-966659"></A>
*mobilize(</code>
<code>
<A ID="pgfId-966660"></A>
	ipaddr	srcaddr,		/* IP source address */</code>
<code>
<A ID="pgfId-966661"></A>
	ipaddr	dstaddr,		/* IP destination address */</code>
<code>
<A ID="pgfId-966662"></A>
	int	version,		/* version */</code>
<code>
<A ID="pgfId-966663"></A>
	int	mode,		/* host mode */</code>
<code>
<A ID="pgfId-966664"></A>
	int	keyid,		/* key identifier */</code>
<code>
<A ID="pgfId-966665"></A>
	int	flags		/* peer flags */</code>
<code>
<A ID="pgfId-966666"></A>
	)</code>
<code>
<A ID="pgfId-966667"></A>
{</code>
<code>
<A ID="pgfId-966668"></A>
	struct p *p;			/* peer process pointer */</code>
<code>
<A ID="pgfId-966669"></A>
</code>
<code>
<A ID="pgfId-966670"></A>
	/*</code>
<code>
<A ID="pgfId-966671"></A>
	 * Allocate and initialize association memory</code>
<code>
<A ID="pgfId-966672"></A>
	 */</code>
<code>
<A ID="pgfId-966673"></A>
	p = malloc(sizeof(struct p));</code>
<code>
<A ID="pgfId-966674"></A>
	p-&gt;srcaddr = srcaddr;</code>
<code>
<A ID="pgfId-970166"></A>
	p-&gt;srcport = PORT;</code>
<code>
<A ID="pgfId-966675"></A>
	p-&gt;dstaddr = dstaddr;</code>
<code>
<A ID="pgfId-970167"></A>
	p-&gt;dstport = PORT;</code>
<code>
<A ID="pgfId-966676"></A>
	p-&gt;version = version;</code>
<code>
<A ID="pgfId-966677"></A>
	p-&gt;mode = mode;</code>
<code>
<A ID="pgfId-966678"></A>
	p-&gt;keyid = keyid;</code>
<code>
<A ID="pgfId-966679"></A>
	p-&gt;hpoll = MINPOLL;</code>
<code>
<A ID="pgfId-966680"></A>
	clear(p, X_INIT);</code>
<code>
<A ID="pgfId-966681"></A>
	p-&gt;flags == flags;</code>
<code>
<A ID="pgfId-966682"></A>
	return (p);</code>
<code>
<A ID="pgfId-966683"></A>
}</code>
<code>
<A ID="pgfId-969778"></A>
</code>
<code>
<A ID="pgfId-969779"></A>
/*</code>
<code>
<A ID="pgfId-969780"></A>
 * clear() - reinitialize for persistent association, demobilize</code>
<code>
<A ID="pgfId-969781"></A>
 * for ephemeral association.</code>
<code>
<A ID="pgfId-969782"></A>
 */</code>
<code>
<A ID="pgfId-969783"></A>
void</code>
<code>
<A ID="pgfId-969784"></A>
clear(</code>
<code>
<A ID="pgfId-969785"></A>
	struct p *p,			/* peer structure pointer */</code>
<code>
<A ID="pgfId-969786"></A>
	int	kiss		/* kiss code */</code>
<code>
<A ID="pgfId-969787"></A>
	)</code>
<code>
<A ID="pgfId-969788"></A>
{</code>
<code>
<A ID="pgfId-969789"></A>
	int i;</code>
<code>
<A ID="pgfId-969790"></A>
</code>
<code>
<A ID="pgfId-969791"></A>
	/*</code>
<code>
<A ID="pgfId-969792"></A>
	 * The first thing to do is return all resources to the bank.</code>
<code>
<A ID="pgfId-969793"></A>
	 * Typical resources are not detailed here, but they include</code>
<code>
<A ID="pgfId-969794"></A>
	 * dynamically allocated structures for keys, certificates, etc.</code>
<code>
<A ID="pgfId-969795"></A>
	 * If an ephemeral association and not initialization, return</code>
<code>
<A ID="pgfId-969796"></A>
	 * the association memory as well.</code>
<code>
<A ID="pgfId-969797"></A>
	 */</code>
<code>
<A ID="pgfId-969798"></A>
	/* return resources */</code>
<code>
<A ID="pgfId-969799"></A>
	if (s.p == p)</code>
<code>
<A ID="pgfId-969800"></A>
		s.p = NULL;</code>
<code>
<A ID="pgfId-969801"></A>
	if (kiss != X_INIT &amp;&amp; (p-&gt;flags &amp; P_EPHEM)) {</code>
<code>
<A ID="pgfId-969802"></A>
		free(p);</code>
<code>
<A ID="pgfId-969803"></A>
		return;</code>
<code>
<A ID="pgfId-969804"></A>
	}</code>
<code>
<A ID="pgfId-969805"></A>
</code>
<code>
<A ID="pgfId-969806"></A>
	/*</code>
<code>
<A ID="pgfId-969807"></A>
	 * Initialize the association fields for general reset.</code>
<code>
<A ID="pgfId-969808"></A>
	 */</code>
<code>
<A ID="pgfId-969809"></A>
	memset(BEGIN_CLEAR(p), LEN_CLEAR, 0);</code>
<code>
<A ID="pgfId-969810"></A>
	p-&gt;leap = NOSYNC;</code>
<code>
<A ID="pgfId-969811"></A>
	p-&gt;stratum = MAXSTRAT;</code>
<code>
<A ID="pgfId-969812"></A>
	p-&gt;ppoll = MAXPOLL;</code>
<code>
<A ID="pgfId-969813"></A>
	p-&gt;hpoll = MINPOLL;</code>
<code>
<A ID="pgfId-969814"></A>
	p-&gt;disp = MAXDISP;</code>
<code>
<A ID="pgfId-969815"></A>
	p-&gt;jitter = LOG2D(s.precision);</code>
<code>
<A ID="pgfId-969816"></A>
	p-&gt;refid = kiss;</code>
<code>
<A ID="pgfId-969817"></A>
	for (i = 0; i &lt; NSTAGE; i++)</code>
<code>
<A ID="pgfId-969818"></A>
		p-&gt;f[i].disp = MAXDISP;</code>
<code>
<A ID="pgfId-969819"></A>
</code>
<code>
<A ID="pgfId-969820"></A>
	/*</code>
<code>
<A ID="pgfId-969821"></A>
	 * Randomize the first poll just in case thousands of broadcast</code>
<code>
<A ID="pgfId-969822"></A>
	 * clients have just been stirred up after a long absence of the</code>
<code>
<A ID="pgfId-969823"></A>
	 * broadcast server.</code>
<code>
<A ID="pgfId-969824"></A>
	 */</code>
<code>
<A ID="pgfId-969825"></A>
	p-&gt;last = p-&gt;t = c.t;</code>
<code>
<A ID="pgfId-969826"></A>
	p-&gt;next = p-&gt;last + (random() &amp; ((1 &lt;&lt; MINPOLL) - 1));</code>
<code>
<A ID="pgfId-969827"></A>
}</code>
<code>
<A ID="pgfId-966706"></A>
</code>
<code>
<A ID="pgfId-966708"></A>
/*</code>
<code>
<A ID="pgfId-966709"></A>
 * md5() - compute message digest</code>
<code>
<A ID="pgfId-966710"></A>
 */</code>
<code>
<A ID="pgfId-966711"></A>
digest</code>
<code>
<A ID="pgfId-966712"></A>
md5(</code>
<code>
<A ID="pgfId-966713"></A>
	int	keyid		/* key identifier */</code>
<code>
<A ID="pgfId-966714"></A>
	)</code>
<code>
<A ID="pgfId-966715"></A>
{</code>
<code>
<A ID="pgfId-966716"></A>
	/*</code>
<code>
<A ID="pgfId-966717"></A>
	 * Compute a keyed cryptographic message digest. The key</code>
<code>
<A ID="pgfId-966718"></A>
	 * identifier is associated with a key in the local key cache.</code>
<code>
<A ID="pgfId-966719"></A>
	 * The key is prepended to the packet header and extension fieds</code>
<code>
<A ID="pgfId-966720"></A>
	 * and the result hashed by the MD5 algorithm as described in</code>
<code>
<A ID="pgfId-966721"></A>
	 * RFC-1321. Return a MAC consisting of the 32-bit key ID</code>
<code>
<A ID="pgfId-966722"></A>
	 * concatenated with the 128-bit digest.</code>
<code>
<A ID="pgfId-966723"></A>
	 */</code>
<code>
<A ID="pgfId-966724"></A>
	return (/* MD5 digest */ 0);</code>
<code>
<A ID="pgfId-966725"></A>
}</code>
<code>
<A ID="pgfId-966726"></A>
</code>
<Heading2>
<A ID="pgfId-966727"></A>
Kernel Input/Output Interface</Heading2>
<code>
<A ID="pgfId-969751"></A>
/*</code>
<code>
<A ID="pgfId-966728"></A>
 * Kernel interface to transmit and receive packets. Details are</code>
<code>
<A ID="pgfId-966729"></A>
 * deliberately vague and depend on the operating system.</code>
<code>
<A ID="pgfId-966730"></A>
 *</code>
<code>
<A ID="pgfId-966731"></A>
 * recv_packet - receive packet from network</code>
<code>
<A ID="pgfId-966732"></A>
 */</code>
<code>
<A ID="pgfId-966733"></A>
struct r				/* receive packet pointer*/</code>
<code>
<A ID="pgfId-966734"></A>
*recv_packet() {</code>
<code>
<A ID="pgfId-966735"></A>
	return (/* receive packet r */ 0);</code>
<code>
<A ID="pgfId-966736"></A>
}</code>
<code>
<A ID="pgfId-966737"></A>
</code>
<code>
<A ID="pgfId-966739"></A>
/*</code>
<code>
<A ID="pgfId-966740"></A>
 * xmit_packet - transmit packet to network</code>
<code>
<A ID="pgfId-966741"></A>
 */</code>
<code>
<A ID="pgfId-966742"></A>
void</code>
<code>
<A ID="pgfId-966743"></A>
xmit_packet(</code>
<code>
<A ID="pgfId-966744"></A>
	struct x *x		/* transmit packet pointer */</code>
<code>
<A ID="pgfId-966745"></A>
	)</code>
<code>
<A ID="pgfId-966746"></A>
{</code>
<code>
<A ID="pgfId-966747"></A>
	/* send packet x */</code>
<code>
<A ID="pgfId-966748"></A>
}</code>
<code>
<A ID="pgfId-966749"></A>
</code>
<Heading2>
<A ID="pgfId-968847"></A>
Kernel System Clock Interface</Heading2>
<code>
<A ID="pgfId-966753"></A>
*</code>
<code>
<A ID="pgfId-966754"></A>
 * There are three time formats: native (Unix), NTP and floating double.</code>
<code>
<A ID="pgfId-966755"></A>
 * The get_time() routine returns the time in NTP long format. The Unix</code>
<code>
<A ID="pgfId-966756"></A>
 * routines expect arguments as a structure of two signed 32-bit words</code>
<code>
<A ID="pgfId-966757"></A>
 * in seconds and microseconds (timeval) or nanoseconds (timespec). The</code>
<code>
<A ID="pgfId-966758"></A>
 * step_time() and adjust_time() routines expect signed arguments in</code>
<code>
<A ID="pgfId-966759"></A>
 * floating double. The simplified code shown here is for illustration</code>
<code>
<A ID="pgfId-966760"></A>
 * only and has not been verified.</code>
<code>
<A ID="pgfId-966761"></A>
 */</code>
<code>
<A ID="pgfId-966762"></A>
#define	JAN_1970	2208988800UL		/* 1970 - 1900 in seconds */</code>
<code>
<A ID="pgfId-966763"></A>
</code>
<code>
<A ID="pgfId-966764"></A>
/*</code>
<code>
<A ID="pgfId-966765"></A>
 * get_time - read system time and convert to NTP format</code>
<code>
<A ID="pgfId-966766"></A>
 */</code>
<code>
<A ID="pgfId-966767"></A>
tstamp</code>
<code>
<A ID="pgfId-966768"></A>
get_time()</code>
<code>
<A ID="pgfId-966769"></A>
{</code>
<code>
<A ID="pgfId-966770"></A>
	struct timeval unix_time;</code>
<code>
<A ID="pgfId-966771"></A>
</code>
<code>
<A ID="pgfId-966772"></A>
	/*</code>
<code>
<A ID="pgfId-966773"></A>
	 * There are only two calls on this routine in the program. One</code>
<code>
<A ID="pgfId-966774"></A>
	 * when a packet arrives from the network and the other when a</code>
<code>
<A ID="pgfId-966775"></A>
	 * packet is placed on the send queue. Call the kernel time of</code>
<code>
<A ID="pgfId-966776"></A>
	 * day routine (such as gettimeofday()) and convert to NTP</code>
<code>
<A ID="pgfId-966777"></A>
	 * format.</code>
<code>
<A ID="pgfId-966778"></A>
	 */</code>
<code>
<A ID="pgfId-966779"></A>
	gettimeofday(&amp;unix_time, NULL);</code>
<code>
<A ID="pgfId-966780"></A>
</code>
<code>
<A ID="pgfId-966781"></A>
	return ((unix_time.tv_sec + JAN_1970) * 0x100000000L +</code>
<code>
<A ID="pgfId-966782"></A>
	    (unix_time.tv_usec * 0x100000000L) / 1000000);</code>
<code>
<A ID="pgfId-966783"></A>
}</code>
<code>
<A ID="pgfId-966784"></A>
</code>
<code>
<A ID="pgfId-966786"></A>
/*</code>
<code>
<A ID="pgfId-966787"></A>
 * step_time() - step system time to given offset valuet</code>
<code>
<A ID="pgfId-966788"></A>
 */</code>
<code>
<A ID="pgfId-966789"></A>
void</code>
<code>
<A ID="pgfId-966790"></A>
step_time(</code>
<code>
<A ID="pgfId-966791"></A>
	double	offset		/* clock offset */</code>
<code>
<A ID="pgfId-966792"></A>
	)</code>
<code>
<A ID="pgfId-966793"></A>
{</code>
<code>
<A ID="pgfId-966794"></A>
	struct timeval unix_time;</code>
<code>
<A ID="pgfId-966795"></A>
	tstamp	ntp_time;</code>
<code>
<A ID="pgfId-966796"></A>
</code>
<code>
<A ID="pgfId-966797"></A>
	/*</code>
<code>
<A ID="pgfId-966798"></A>
	 * Convert from double to native format (signed) and add to the</code>
<code>
<A ID="pgfId-966799"></A>
	 * current time. Note the addition is done in native format to</code>
<code>
<A ID="pgfId-966800"></A>
	 * avoid overflow or loss of precision.</code>
<code>
<A ID="pgfId-966801"></A>
	 */</code>
<code>
<A ID="pgfId-966802"></A>
	ntp_time = D2LFP(offset);</code>
<code>
<A ID="pgfId-966803"></A>
	gettimeofday(&amp;unix_time, NULL);</code>
<code>
<A ID="pgfId-966804"></A>
	unix_time.tv_sec += ntp_time / 0x100000000L;</code>
<code>
<A ID="pgfId-966805"></A>
	unix_time.tv_usec += ntp_time % 0x100000000L;</code>
<code>
<A ID="pgfId-966806"></A>
	unix_time.tv_sec += unix_time.tv_usec / 1000000;</code>
<code>
<A ID="pgfId-966807"></A>
	unix_time.tv_usec %= 1000000;</code>
<code>
<A ID="pgfId-966808"></A>
	settimeofday(&amp;unix_time, NULL);</code>
<code>
<A ID="pgfId-966809"></A>
}</code>
<code>
<A ID="pgfId-966810"></A>
</code>
<code>
<A ID="pgfId-966812"></A>
/*</code>
<code>
<A ID="pgfId-966813"></A>
 * adjust_time() - slew system clock to given offset value</code>
<code>
<A ID="pgfId-966814"></A>
 */</code>
<code>
<A ID="pgfId-966815"></A>
void</code>
<code>
<A ID="pgfId-966816"></A>
adjust_time(</code>
<code>
<A ID="pgfId-966817"></A>
	double	offset		/* clock offset */</code>
<code>
<A ID="pgfId-966818"></A>
	)</code>
<code>
<A ID="pgfId-966819"></A>
{</code>
<code>
<A ID="pgfId-966820"></A>
	struct timeval unix_time;</code>
<code>
<A ID="pgfId-966821"></A>
	tstamp	ntp_time;</code>
<code>
<A ID="pgfId-966822"></A>
</code>
<code>
<A ID="pgfId-966823"></A>
	/*</code>
<code>
<A ID="pgfId-966824"></A>
	 * Convert from double to native format (signed) and add to the</code>
<code>
<A ID="pgfId-966825"></A>
	 * current time.</code>
<code>
<A ID="pgfId-966826"></A>
	 */</code>
<code>
<A ID="pgfId-966827"></A>
	ntp_time = D2LFP(offset);</code>
<code>
<A ID="pgfId-966828"></A>
	unix_time.tv_sec = ntp_time / 0x100000000L;</code>
<code>
<A ID="pgfId-966829"></A>
	unix_time.tv_usec = ntp_time % 0x100000000L;</code>
<code>
<A ID="pgfId-966830"></A>
	unix_time.tv_sec += unix_time.tv_usec / 1000000;</code>
<code>
<A ID="pgfId-966831"></A>
	unix_time.tv_usec %= 1000000;</code>
<code>
<A ID="pgfId-966832"></A>
	adjtime(&amp;unix_time, NULL);</code>
<code>
<A ID="pgfId-966833"></A>
}</code>
<code>
<A ID="pgfId-966834"></A>
</code>
<Heading2>
<A ID="pgfId-966886"></A>
<A ID="96242"></A>
Peer Process</Heading2>
<code>
<A ID="pgfId-966899"></A>
#include “ntp4.h”</code>
<code>
<A ID="pgfId-966900"></A>
</code>
<code>
<A ID="pgfId-966901"></A>
/*</code>
<code>
<A ID="pgfId-966902"></A>
 * A crypto-NAK packet includes the NTP header followed by a MAC</code>
<code>
<A ID="pgfId-966903"></A>
 * consisting only of the key identifier with value zero. It tells the</code>
<code>
<A ID="pgfId-966904"></A>
 * receiver that a prior request could not be properly authenticated,</code>
<code>
<A ID="pgfId-966905"></A>
 * but the NTP header fields are correct.</code>
<code>
<A ID="pgfId-966906"></A>
 *</code>
<code>
<A ID="pgfId-966907"></A>
 * A kiss-o’-death packet has an NTP header with leap 3 (NOSYNC) and</code>
<code>
<A ID="pgfId-966908"></A>
 * stratum 0. It tells the receiver that something drastic</code>
<code>
<A ID="pgfId-966909"></A>
 * has happened, as revealled by the kiss code in the refid field. The</code>
<code>
<A ID="pgfId-966910"></A>
 * NTP header fields may or may not be correct.</code>
<code>
<A ID="pgfId-966911"></A>
 */</code>
<code>
<A ID="pgfId-966912"></A>
/*</code>
<code>
<A ID="pgfId-966913"></A>
 * Definitions</code>
<code>
<A ID="pgfId-966914"></A>
 */</code>
<code>
<A ID="pgfId-966915"></A>
#define SGATE		3		/* spike gate (clock filter */</code>
<code>
<A ID="pgfId-966916"></A>
#define BDELAY		.004		/* broadcast delay (s) */</code>
<code>
<A ID="pgfId-966917"></A>
</code>
<code>
<A ID="pgfId-966918"></A>
/*</code>
<code>
<A ID="pgfId-966919"></A>
 * Dispatch codes</code>
<code>
<A ID="pgfId-966920"></A>
 */</code>
<code>
<A ID="pgfId-966921"></A>
#define	ERR	-1		/* error */</code>
<code>
<A ID="pgfId-966922"></A>
#define DSCRD		0		/* discard packet */</code>
<code>
<A ID="pgfId-966923"></A>
#define	PROC	1		/* process packet */</code>
<code>
<A ID="pgfId-966924"></A>
#define	BCST	2		/* broadcast packet */</code>
<code>
<A ID="pgfId-966925"></A>
#define	FXMIT	3		/* client packet */</code>
<code>
<A ID="pgfId-966926"></A>
#define	NEWPS	4		/* new symmetric passive client */</code>
<code>
<A ID="pgfId-966927"></A>
#define	NEWBC	5		/* new broadcast client */</code>
<code>
<A ID="pgfId-966928"></A>
</code>
<code>
<A ID="pgfId-966929"></A>
/*</code>
<code>
<A ID="pgfId-966930"></A>
 * Dispatch matrix</code>
<code>
<A ID="pgfId-966931"></A>
 *		active  passv  client server bcast */</code>
<code>
<A ID="pgfId-966932"></A>
int table[7][5] = {</code>
<code>
<A ID="pgfId-966933"></A>
/* nopeer  */	{ NEWPS, DSCRD, FXMIT, DSCRD, NEWBC },</code>
<code>
<A ID="pgfId-966934"></A>
/* active  */	{ PROC,  PROC,  DSCRD, DSCRD, DSCRD },</code>
<code>
<A ID="pgfId-966935"></A>
/* passv   */	{ PROC,  ERR,   DSCRD, DSCRD, DSCRD },</code>
<code>
<A ID="pgfId-966936"></A>
/* client  */	{ DSCRD, DSCRD, DSCRD, PROC,  DSCRD },</code>
<code>
<A ID="pgfId-966937"></A>
/* server  */	{ DSCRD, DSCRD, DSCRD, DSCRD, DSCRD },</code>
<code>
<A ID="pgfId-966938"></A>
/* bcast   */	{ DSCRD, DSCRD, DSCRD, DSCRD, DSCRD },</code>
<code>
<A ID="pgfId-966939"></A>
/* bclient */	{ DSCRD, DSCRD, DSCRD, DSCRD, PROC}</code>
<code>
<A ID="pgfId-966940"></A>
};</code>
<code>
<A ID="pgfId-966941"></A>
</code>
<code>
<A ID="pgfId-966942"></A>
/*</code>
<code>
<A ID="pgfId-966943"></A>
 * Miscellaneous macroni</code>
<code>
<A ID="pgfId-966944"></A>
 *</code>
<code>
<A ID="pgfId-966945"></A>
 * This macro defines the authentication state. If x is 0,</code>
<code>
<A ID="pgfId-966946"></A>
 * authentication is optional, othewise it is required.</code>
<code>
<A ID="pgfId-966947"></A>
 */</code>
<code>
<A ID="pgfId-966948"></A>
#define	AUTH(x, y)	((x) ? (y) == A_OK : (y) == A_OK || \</code>
<code>
<A ID="pgfId-966949"></A>
			    (y) == A_NONE)</code>
<code>
<A ID="pgfId-966950"></A>
</code>
<code>
<A ID="pgfId-966951"></A>
/*</code>
<code>
<A ID="pgfId-966952"></A>
 * These are used by the clear() routine</code>
<code>
<A ID="pgfId-966953"></A>
 */</code>
<code>
<A ID="pgfId-966954"></A>
#define	BEGIN_CLEAR(p)		((char *)&amp;((p)-&gt;begin_clear))</code>
<code>
<A ID="pgfId-966955"></A>
#define	END_CLEAR(p)		((char *)&amp;((p)-&gt;end_clear))</code>
<code>
<A ID="pgfId-966956"></A>
#define	LEN_CLEAR	(END_CLEAR	 ((struct p *)0) - \</code>
<code>
<A ID="pgfId-966957"></A>
			    BEGIN_CLEAR((struct p *)0))</code>
<code>
<A ID="pgfId-966958"></A>
</code>
<Heading3>
<A ID="pgfId-966959"></A>
<A ID="99468"></A>
receive()</Heading3>
<code>
<A ID="pgfId-966960"></A>
/*</code>
<code>
<A ID="pgfId-966961"></A>
 * receive() - receive packet and decode modes</code>
<code>
<A ID="pgfId-966962"></A>
 */</code>
<code>
<A ID="pgfId-966963"></A>
void</code>
<code>
<A ID="pgfId-966964"></A>
receive(</code>
<code>
<A ID="pgfId-966965"></A>
	struct r *r			/* receive packet pointer */</code>
<code>
<A ID="pgfId-966966"></A>
	)</code>
<code>
<A ID="pgfId-966967"></A>
{</code>
<code>
<A ID="pgfId-966968"></A>
	struct p *p;			/* peer structure pointer</code>
<code>
<A ID="pgfId-966969"></A>
	int	auth;		/* authentication code */</code>
<code>
<A ID="pgfId-966970"></A>
	int	has_mac;		/* size of MAC */</code>
<code>
<A ID="pgfId-966971"></A>
	int	synch;		/* synchronized switch */</code>
<code>
<A ID="pgfId-966972"></A>
	int	auth;		/* authentication code */</code>
<code>
<A ID="pgfId-966973"></A>
</code>
<code>
<A ID="pgfId-966974"></A>
	/*</code>
<code>
<A ID="pgfId-966975"></A>
	 * Check access control lists. The intent here is to implement a</code>
<code>
<A ID="pgfId-966976"></A>
	 * whitelist of those IP addresses specifically accepted and/or</code>
<code>
<A ID="pgfId-966977"></A>
	 * a blacklist of those IP addresses specifically rejected.</code>
<code>
<A ID="pgfId-966978"></A>
	 * There could be different lists for authenticated clients and</code>
<code>
<A ID="pgfId-966979"></A>
	 * unauthenticated clients.</code>
<code>
<A ID="pgfId-966980"></A>
	 */</code>
<code>
<A ID="pgfId-966981"></A>
	if (!access(r))</code>
<code>
<A ID="pgfId-966982"></A>
		return;			/* access denied */</code>
<code>
<A ID="pgfId-966983"></A>
</code>
<code>
<A ID="pgfId-966984"></A>
	/*</code>
<code>
<A ID="pgfId-966985"></A>
	 * The version must not be in the future. Format checks include</code>
<code>
<A ID="pgfId-966986"></A>
	 * packet length, MAC length and extension field lengths, if</code>
<code>
<A ID="pgfId-966987"></A>
	 * present. </code>
<code>
<A ID="pgfId-966988"></A>
	 */</code>
<code>
<A ID="pgfId-966989"></A>
	if (r-&gt;version &gt; VERSION /* or format error */)</code>
<code>
<A ID="pgfId-966990"></A>
		return;			/* format error */</code>
<code>
<A ID="pgfId-966991"></A>
</code>
<code>
<A ID="pgfId-966992"></A>
	/*</code>
<code>
<A ID="pgfId-966993"></A>
	 * Authentication is conditioned by two switches which can be</code>
<code>
<A ID="pgfId-966994"></A>
	 * specified on a per-client basis.</code>
<code>
<A ID="pgfId-966995"></A>
	 *</code>
<code>
<A ID="pgfId-966996"></A>
	 * P_NOPEER		do not mobilize an association unless</code>
<code>
<A ID="pgfId-966997"></A>
	 *		authenticated</code>
<code>
<A ID="pgfId-966998"></A>
	 * P_NOTRUST		do not allow access unless authenticated</code>
<code>
<A ID="pgfId-966999"></A>
	 *		(implies P_NOPEER)</code>
<code>
<A ID="pgfId-967000"></A>
	 *</code>
<code>
<A ID="pgfId-967001"></A>
	 * There are four outcomes:</code>
<code>
<A ID="pgfId-967002"></A>
	 *</code>
<code>
<A ID="pgfId-967003"></A>
	 * A_NONE	the packet has no MAC</code>
<code>
<A ID="pgfId-967004"></A>
	 * A_OK		the packet has a MAC and authentication</code>
<code>
<A ID="pgfId-967005"></A>
	 *		succeeds</code>
<code>
<A ID="pgfId-967006"></A>
	 * A_ERROR		the packet has a MAC and authentication fails</code>
<code>
<A ID="pgfId-967007"></A>
	 * A_CRYPTO		crypto-NAK. the MAC has four octets only.</code>
<code>
<A ID="pgfId-967008"></A>
	 *</code>
<code>
<A ID="pgfId-967009"></A>
	 * Note: The AUTH(x, y) macro is used to filter outcomes. If x</code>
<code>
<A ID="pgfId-967010"></A>
	 * is zero, acceptable outcomes of y are NONE and OK. If x is</code>
<code>
<A ID="pgfId-967011"></A>
	 * one, the only acceptable outcome of y is OK.</code>
<code>
<A ID="pgfId-967012"></A>
	 */</code>
<code>
<A ID="pgfId-967013"></A>
	has_mac = /* length of MAC field */ 0;</code>
<code>
<A ID="pgfId-967014"></A>
	if (has_mac == 0) {</code>
<code>
<A ID="pgfId-967015"></A>
		auth = A_NONE;			/* not required */</code>
<code>
<A ID="pgfId-967016"></A>
	} else if (has_mac == 4) {</code>
<code>
<A ID="pgfId-967017"></A>
		auth == A_CRYPTO;			/* crypto-NAK */</code>
<code>
<A ID="pgfId-967018"></A>
	} else {</code>
<code>
<A ID="pgfId-967019"></A>
		if (r-&gt;mac != md5(r-&gt;keyid))</code>
<code>
<A ID="pgfId-967020"></A>
			auth = A_ERROR;		/* auth error */</code>
<code>
<A ID="pgfId-967021"></A>
		else</code>
<code>
<A ID="pgfId-967022"></A>
			auth = A_OK;		/* auth OK */</code>
<code>
<A ID="pgfId-967023"></A>
	}</code>
<code>
<A ID="pgfId-967024"></A>
</code>
<code>
<A ID="pgfId-967025"></A>
	/*</code>
<code>
<A ID="pgfId-967026"></A>
	 * Find association and dispatch code. If there is no</code>
<code>
<A ID="pgfId-967027"></A>
	 * association to match, the value of p-&gt;mode is assumed NULL.</code>
<code>
<A ID="pgfId-967028"></A>
	 */</code>
<code>
<A ID="pgfId-967029"></A>
	p = find_assoc(r);</code>
<code>
<A ID="pgfId-967030"></A>
	switch(table[p-&gt;mode][r-&gt;mode]) {</code>
<code>
<A ID="pgfId-967031"></A>
</code>
<code>
<A ID="pgfId-967032"></A>
	/*</code>
<code>
<A ID="pgfId-967033"></A>
	 * Client packet. Send server reply (no association). If</code>
<code>
<A ID="pgfId-967034"></A>
	 * authentication fails, send a crypto-NAK packet.</code>
<code>
<A ID="pgfId-967035"></A>
	 */</code>
<code>
<A ID="pgfId-967036"></A>
	case FXMIT:</code>
<code>
<A ID="pgfId-967037"></A>
		if (AUTH(p-&gt;flags &amp; P_NOTRUST, auth))</code>
<code>
<A ID="pgfId-967038"></A>
			fast_xmit(r, M_SERV, auth);</code>
<code>
<A ID="pgfId-967039"></A>
		else if (auth == A_ERROR)</code>
<code>
<A ID="pgfId-967040"></A>
			fast_xmit(r, M_SERV, A_CRYPTO);</code>
<code>
<A ID="pgfId-967041"></A>
		return;			/* M_SERV packet sent */</code>
<code>
<A ID="pgfId-967042"></A>
</code>
<code>
<A ID="pgfId-967043"></A>
	/*</code>
<code>
<A ID="pgfId-967044"></A>
	 * New symmetric passive client (ephemeral association). It is</code>
<code>
<A ID="pgfId-967045"></A>
	 * mobilized in the same version as in the packet. If</code>
<code>
<A ID="pgfId-967046"></A>
	 * authentication fails, send a crypto-NAK packet. If restrict</code>
<code>
<A ID="pgfId-967047"></A>
	 * no-moblize, send a symmetric active packet instead.</code>
<code>
<A ID="pgfId-967048"></A>
	 */</code>
<code>
<A ID="pgfId-967049"></A>
	case NEWPS:</code>
<code>
<A ID="pgfId-967050"></A>
		if (!AUTH(p-&gt;flags &amp; P_NOTRUST, auth)) {</code>
<code>
<A ID="pgfId-967051"></A>
			if (auth == A_ERROR)</code>
<code>
<A ID="pgfId-967052"></A>
				fast_xmit(r, M_SACT, A_CRYPTO);</code>
<code>
<A ID="pgfId-967053"></A>
			return;		/* crypto-NAK packet sent */</code>
<code>
<A ID="pgfId-967054"></A>
		}</code>
<code>
<A ID="pgfId-967055"></A>
		if (!AUTH(p-&gt;flags &amp; P_NOPEER, auth)) {</code>
<code>
<A ID="pgfId-967056"></A>
			fast_xmit(r, M_SACT, auth);</code>
<code>
<A ID="pgfId-967057"></A>
			return;		/* M_SACT packet sent */</code>
<code>
<A ID="pgfId-967058"></A>
		}</code>
<code>
<A ID="pgfId-967059"></A>
		p = mobilize(r-&gt;srcaddr, r-&gt;dstaddr, r-&gt;version, M_PASV,</code>
<code>
<A ID="pgfId-967060"></A>
		    r-&gt;keyid, P_EPHEM);</code>
<code>
<A ID="pgfId-967061"></A>
		break;</code>
<code>
<A ID="pgfId-967062"></A>
</code>
<code>
<A ID="pgfId-967063"></A>
	/*</code>
<code>
<A ID="pgfId-967064"></A>
	 * New broadcast client (ephemeral association). It is mobilized</code>
<code>
<A ID="pgfId-967065"></A>
	 * in the same version as in the packet. If authentication</code>
<code>
<A ID="pgfId-967066"></A>
	 * error, ignore the packet. Note this code does not support the</code>
<code>
<A ID="pgfId-967067"></A>
	 * initial volley feature in the reference implementation.</code>
<code>
<A ID="pgfId-967068"></A>
	 */</code>
<code>
<A ID="pgfId-967069"></A>
	case NEWBC:</code>
<code>
<A ID="pgfId-967070"></A>
		if (!AUTH(p-&gt;flags &amp; (P_NOTRUST | P_NOPEER), auth))</code>
<code>
<A ID="pgfId-967071"></A>
			return;		/* authentication error */</code>
<code>
<A ID="pgfId-967072"></A>
</code>
<code>
<A ID="pgfId-967073"></A>
		if (!(s.flags &amp; S_BCSTENAB))</code>
<code>
<A ID="pgfId-967074"></A>
			return;		/* broadcast not enabled */</code>
<code>
<A ID="pgfId-967075"></A>
</code>
<code>
<A ID="pgfId-967076"></A>
		p = mobilize(r-&gt;srcaddr, r-&gt;dstaddr, r-&gt;version, M_BCLN,</code>
<code>
<A ID="pgfId-967077"></A>
		    r-&gt;keyid, P_EPHEM);</code>
<code>
<A ID="pgfId-967078"></A>
		break;			/* processing continues */</code>
<code>
<A ID="pgfId-967079"></A>
</code>
<code>
<A ID="pgfId-967080"></A>
	/*</code>
<code>
<A ID="pgfId-967081"></A>
	 * Process packet. Placeholdler only.</code>
<code>
<A ID="pgfId-967082"></A>
	 */</code>
<code>
<A ID="pgfId-967083"></A>
	case PROC:</code>
<code>
<A ID="pgfId-967084"></A>
		break;			/* processing continues */</code>
<code>
<A ID="pgfId-967085"></A>
</code>
<code>
<A ID="pgfId-967086"></A>
	/*</code>
<code>
<A ID="pgfId-967087"></A>
	 * Invalid mode combination. We get here only in case of</code>
<code>
<A ID="pgfId-967088"></A>
	 * ephemeral associations, so the correct action is simply to</code>
<code>
<A ID="pgfId-967089"></A>
	 * toss it.</code>
<code>
<A ID="pgfId-967090"></A>
	 */</code>
<code>
<A ID="pgfId-967091"></A>
	case ERR:</code>
<code>
<A ID="pgfId-967092"></A>
		clear(p, X_ERROR);</code>
<code>
<A ID="pgfId-967093"></A>
		return;			/* invalid mode combination */</code>
<code>
<A ID="pgfId-967094"></A>
</code>
<code>
<A ID="pgfId-967095"></A>
	/*</code>
<code>
<A ID="pgfId-967096"></A>
	 * No match; just discard the packet.</code>
<code>
<A ID="pgfId-967097"></A>
	 */</code>
<code>
<A ID="pgfId-967098"></A>
	case DSCRD:</code>
<code>
<A ID="pgfId-967099"></A>
		return;			/* orphan abandoned */</code>
<code>
<A ID="pgfId-967100"></A>
	}</code>
<code>
<A ID="pgfId-967101"></A>
</code>
<code>
<A ID="pgfId-967102"></A>
	/*</code>
<code>
<A ID="pgfId-967103"></A>
	 * Next comes a rigorous schedule of timestamp checking. If the</code>
<code>
<A ID="pgfId-967104"></A>
	 * transmit timestamp is zero, the server is horribly broken.</code>
<code>
<A ID="pgfId-967105"></A>
	 */</code>
<code>
<A ID="pgfId-967106"></A>
	if (r-&gt;xmt == 0)</code>
<code>
<A ID="pgfId-967107"></A>
		return;			/* invalid timestamp */</code>
<code>
<A ID="pgfId-967108"></A>
</code>
<code>
<A ID="pgfId-967109"></A>
	/*</code>
<code>
<A ID="pgfId-967110"></A>
	 * If the transmit timestamp duplicates a previous one, the</code>
<code>
<A ID="pgfId-967111"></A>
	 * packet is a replay.</code>
<code>
<A ID="pgfId-967112"></A>
	 */</code>
<code>
<A ID="pgfId-967113"></A>
	if (r-&gt;xmt == p-&gt;xmt)</code>
<code>
<A ID="pgfId-967114"></A>
		return;			/* duplicate packet */</code>
<code>
<A ID="pgfId-967115"></A>
</code>
<code>
<A ID="pgfId-967116"></A>
	/*</code>
<code>
<A ID="pgfId-967117"></A>
	 * If this is a broadcast mode packet, skip further checking.</code>
<code>
<A ID="pgfId-967118"></A>
	 * If the origin timestamp is zero, the sender has not yet heard</code>
<code>
<A ID="pgfId-967119"></A>
	 * from us. Otherwise, if the origin timestamp does not match</code>
<code>
<A ID="pgfId-967120"></A>
	 * the transmit timestamp, the packet is bogus.</code>
<code>
<A ID="pgfId-967121"></A>
	 */</code>
<code>
<A ID="pgfId-967122"></A>
	synch = TRUE;</code>
<code>
<A ID="pgfId-967123"></A>
	if (r-&gt;mode != M_BCST) {</code>
<code>
<A ID="pgfId-967124"></A>
		if (r-&gt;org == 0)</code>
<code>
<A ID="pgfId-967125"></A>
			synch = FALSE;	/* unsynchronized */</code>
<code>
<A ID="pgfId-967126"></A>
			</code>
<code>
<A ID="pgfId-967127"></A>
		else if (r-&gt;org != p-&gt;xmt)</code>
<code>
<A ID="pgfId-967128"></A>
			synch = FALSE;	/* bogus packet */</code>
<code>
<A ID="pgfId-967129"></A>
	}</code>
<code>
<A ID="pgfId-967130"></A>
</code>
<code>
<A ID="pgfId-967131"></A>
	/*</code>
<code>
<A ID="pgfId-967132"></A>
	 * Update the origin and destination timestamps. If</code>
<code>
<A ID="pgfId-967133"></A>
	 * unsynchronized or bogus, abandon ship.</code>
<code>
<A ID="pgfId-967134"></A>
	 */</code>
<code>
<A ID="pgfId-967135"></A>
	p-&gt;org = r-&gt;xmt;</code>
<code>
<A ID="pgfId-967136"></A>
	p-&gt;rec = r-&gt;dst;</code>
<code>
<A ID="pgfId-967137"></A>
	if (!synch)</code>
<code>
<A ID="pgfId-967138"></A>
		return;			/* unsynch */</code>
<code>
<A ID="pgfId-967139"></A>
</code>
<code>
<A ID="pgfId-967140"></A>
	/*</code>
<code>
<A ID="pgfId-967141"></A>
	 * The timestamps are valid and the receive packet matches the</code>
<code>
<A ID="pgfId-967142"></A>
	 * last one sent. If the packet is a crypto-NAK, the server</code>
<code>
<A ID="pgfId-967143"></A>
	 * might have just changed keys. We demobilize the association</code>
<code>
<A ID="pgfId-967144"></A>
	 * and wait for better times.</code>
<code>
<A ID="pgfId-967145"></A>
	 */</code>
<code>
<A ID="pgfId-967146"></A>
	if (auth == A_CRYPTO) {</code>
<code>
<A ID="pgfId-967147"></A>
		clear(p, X_CRYPTO);</code>
<code>
<A ID="pgfId-967148"></A>
		return;			/* crypto-NAK */</code>
<code>
<A ID="pgfId-967149"></A>
	}</code>
<code>
<A ID="pgfId-967150"></A>
</code>
<code>
<A ID="pgfId-967151"></A>
	/* </code>
<code>
<A ID="pgfId-967152"></A>
	 * If the association is authenticated, the key ID is nonzero</code>
<code>
<A ID="pgfId-967153"></A>
	 * and received packets must be authenticated. This is designed</code>
<code>
<A ID="pgfId-967154"></A>
	 * to avoid a bait-and-switch attack, which was possible in past</code>
<code>
<A ID="pgfId-967155"></A>
	 * versions.</code>
<code>
<A ID="pgfId-967156"></A>
	 */</code>
<code>
<A ID="pgfId-967157"></A>
	if (!AUTH(p-&gt;keyid || (p-&gt;flags &amp; P_NOTRUST), auth))</code>
<code>
<A ID="pgfId-967158"></A>
		return;			/* bad auth */</code>
<code>
<A ID="pgfId-967159"></A>
</code>
<code>
<A ID="pgfId-967160"></A>
	/*</code>
<code>
<A ID="pgfId-967161"></A>
	 * Everything possible has been done to validate the timestamps</code>
<code>
<A ID="pgfId-967162"></A>
	 * and prevent bad guys from disrupting the protocol or</code>
<code>
<A ID="pgfId-967163"></A>
	 * injecting bogus data. Earn some revenue.</code>
<code>
<A ID="pgfId-967164"></A>
	 */</code>
<code>
<A ID="pgfId-967165"></A>
	packet(p, r);</code>
<code>
<A ID="pgfId-967166"></A>
}</code>
<code>
<A ID="pgfId-969707"></A>
</code>
<code>
<A ID="pgfId-969710"></A>
/*</code>
<code>
<A ID="pgfId-969711"></A>
 * find_assoc() - find a matching association</code>
<code>
<A ID="pgfId-969712"></A>
 */</code>
<code>
<A ID="pgfId-969713"></A>
struct p				/* peer structure pointer or NULL */</code>
<code>
<A ID="pgfId-969714"></A>
*find_assoc(</code>
<code>
<A ID="pgfId-969715"></A>
	struct r *r			/* receive packet pointer */</code>
<code>
<A ID="pgfId-969716"></A>
	)</code>
<code>
<A ID="pgfId-969717"></A>
{</code>
<code>
<A ID="pgfId-969718"></A>
	struct p *p;			/* dummy peer structure pointer */</code>
<code>
<A ID="pgfId-969719"></A>
</code>
<code>
<A ID="pgfId-969720"></A>
	/*</code>
<code>
<A ID="pgfId-969721"></A>
	 * Search association table for matching source</code>
<code>
<A ID="pgfId-969722"></A>
	 * address and source port.</code>
<code>
<A ID="pgfId-969723"></A>
	 */</code>
<code>
<A ID="pgfId-969724"></A>
	while (/* all associations */ 0) {		 </code>
<code>
<A ID="pgfId-969725"></A>
		if (r-&gt;srcaddr == p-&gt;srcaddr &amp;&amp; r-&gt;port == p-&gt;port)</code>
<code>
<A ID="pgfId-969726"></A>
			return(p);</code>
<code>
<A ID="pgfId-969727"></A>
	}</code>
<code>
<A ID="pgfId-969728"></A>
	return (NULL); </code>
<code>
<A ID="pgfId-969729"></A>
}</code>
<code>
<A ID="pgfId-967168"></A>
</code>
<Heading3>
<A ID="pgfId-967169"></A>
<A ID="41842"></A>
packet()</Heading3>
<code>
<A ID="pgfId-968876"></A>
/*</code>
<code>
<A ID="pgfId-967170"></A>
 * packet() - process packet and compute offset, delay and</code>
<code>
<A ID="pgfId-967171"></A>
 * dispersion.</code>
<code>
<A ID="pgfId-967172"></A>
 */</code>
<code>
<A ID="pgfId-967173"></A>
void</code>
<code>
<A ID="pgfId-967174"></A>
packet(</code>
<code>
<A ID="pgfId-967175"></A>
	struct p *p,			/* peer structure pointer */</code>
<code>
<A ID="pgfId-967176"></A>
	struct r *r			/* receive packet pointer */</code>
<code>
<A ID="pgfId-967177"></A>
	)</code>
<code>
<A ID="pgfId-967178"></A>
{</code>
<code>
<A ID="pgfId-967179"></A>
	double	offset;		/* sample offsset */</code>
<code>
<A ID="pgfId-967180"></A>
	double	delay;		/* sample delay */</code>
<code>
<A ID="pgfId-967181"></A>
	double	disp;		/* sample dispersion */</code>
<code>
<A ID="pgfId-967182"></A>
</code>
<code>
<A ID="pgfId-967183"></A>
	/*</code>
<code>
<A ID="pgfId-967184"></A>
	 * By golly the packet is valid. Light up the remaining header</code>
<code>
<A ID="pgfId-967185"></A>
	 * fields. Note that we map stratum 0 (unspecified) to MAXSTRAT</code>
<code>
<A ID="pgfId-967186"></A>
	 * to make stratum comparisons simpler and to provide a natural</code>
<code>
<A ID="pgfId-967187"></A>
	 * interface for radio clock drivers that operate for</code>
<code>
<A ID="pgfId-967188"></A>
	 *  convenience at stratum 0.</code>
<code>
<A ID="pgfId-967189"></A>
	 */</code>
<code>
<A ID="pgfId-967190"></A>
	p-&gt;leap = r-&gt;leap;</code>
<code>
<A ID="pgfId-967191"></A>
	if (r-&gt;stratum == 0)</code>
<code>
<A ID="pgfId-967192"></A>
		p-&gt;stratum = MAXSTRAT;</code>
<code>
<A ID="pgfId-967193"></A>
	else</code>
<code>
<A ID="pgfId-967194"></A>
		p-&gt;stratum = r-&gt;stratum;</code>
<code>
<A ID="pgfId-967195"></A>
	p-&gt;mode = r-&gt;mode;</code>
<code>
<A ID="pgfId-967196"></A>
	p-&gt;ppoll = r-&gt;poll;</code>
<code>
<A ID="pgfId-967197"></A>
	p-&gt;rootdelay = FP2D(r-&gt;rootdelay);</code>
<code>
<A ID="pgfId-967198"></A>
	p-&gt;rootdisp = FP2D(r-&gt;rootdisp);</code>
<code>
<A ID="pgfId-967199"></A>
	p-&gt;refid = r-&gt;refid;</code>
<code>
<A ID="pgfId-967200"></A>
	p-&gt;reftime = r-&gt;reftime;</code>
<code>
<A ID="pgfId-967201"></A>
</code>
<code>
<A ID="pgfId-967202"></A>
	/*</code>
<code>
<A ID="pgfId-967203"></A>
	 * Verify the server is synchronized with valid stratum and</code>
<code>
<A ID="pgfId-967204"></A>
	 * reference time not later than the transmit time. </code>
<code>
<A ID="pgfId-967205"></A>
	 */</code>
<code>
<A ID="pgfId-967206"></A>
	if (p-&gt;leap == NOSYNC || p-&gt;stratum &gt;= MAXSTRAT)</code>
<code>
<A ID="pgfId-967207"></A>
		return;			/* unsynchronized */</code>
<code>
<A ID="pgfId-967208"></A>
</code>
<code>
<A ID="pgfId-967209"></A>
	/*</code>
<code>
<A ID="pgfId-967210"></A>
	 * Verify valid root distance.</code>
<code>
<A ID="pgfId-967211"></A>
	 */</code>
<code>
<A ID="pgfId-967212"></A>
	if (r-&gt;rootdelay / 2 + r-&gt;rootdisp &gt;= MAXDISP || p-&gt;reftime &gt;</code>
<code>
<A ID="pgfId-967213"></A>
	    r-&gt;xmt)</code>
<code>
<A ID="pgfId-967214"></A>
		return;			/* invalid header values */</code>
<code>
<A ID="pgfId-967215"></A>
</code>
<code>
<A ID="pgfId-967216"></A>
	poll_update(p, p-&gt;hpoll);</code>
<code>
<A ID="pgfId-967217"></A>
	p-&gt;reach |= 1;</code>
<code>
<A ID="pgfId-967218"></A>
</code>
<code>
<A ID="pgfId-967219"></A>
	/*</code>
<code>
<A ID="pgfId-967220"></A>
	 * Calculate offset, delay and dispersion, then pass to the</code>
<code>
<A ID="pgfId-967221"></A>
	 * clock filter. Note carefully the implied processing. The</code>
<code>
<A ID="pgfId-967222"></A>
	 * first-order difference is done directly in 64-bit arithmetic,</code>
<code>
<A ID="pgfId-967223"></A>
	 * then the result is converted to floating double. All further</code>
<code>
<A ID="pgfId-967224"></A>
	 * processing is in floating double arithmetic with rounding</code>
<code>
<A ID="pgfId-967225"></A>
	 * done by the hardware. This is necessary in order to avoid</code>
<code>
<A ID="pgfId-967226"></A>
	 * overflow and preseve precision.</code>
<code>
<A ID="pgfId-967227"></A>
	 *</code>
<code>
<A ID="pgfId-967228"></A>
	 * The delay calculation is a special case. In cases where the</code>
<code>
<A ID="pgfId-967229"></A>
	 * server and client clocks are running at different rates and</code>
<code>
<A ID="pgfId-967230"></A>
	 * with very fast networks, the delay can appear negative. In</code>
<code>
<A ID="pgfId-967231"></A>
	 * order to avoid violating the Principle of Least Astonishment,</code>
<code>
<A ID="pgfId-967232"></A>
	 * the delay is clamped not less than the system precision.</code>
<code>
<A ID="pgfId-967233"></A>
	 */</code>
<code>
<A ID="pgfId-967234"></A>
	if (p-&gt;mode == M_BCST) {</code>
<code>
<A ID="pgfId-967235"></A>
		offset = LFP2D(r-&gt;xmt - r-&gt;dst);</code>
<code>
<A ID="pgfId-967236"></A>
		delay = BDELAY;</code>
<code>
<A ID="pgfId-967237"></A>
		disp = LOG2D(r-&gt;precision) + LOG2D(s.precision) + PHI *</code>
<code>
<A ID="pgfId-967238"></A>
		    2 * BDELAY;</code>
<code>
<A ID="pgfId-967239"></A>
	} else {</code>
<code>
<A ID="pgfId-967240"></A>
		offset = (LFP2D(r-&gt;rec - r-&gt;org) + LFP2D(r-&gt;dst -</code>
<code>
<A ID="pgfId-967241"></A>
		    r-&gt;xmt)) / 2;</code>
<code>
<A ID="pgfId-967242"></A>
		delay = max(LFP2D(r-&gt;dst - r-&gt;org) - LFP2D(r-&gt;rec -</code>
<code>
<A ID="pgfId-967243"></A>
		    r-&gt;xmt), LOG2D(s.precision));</code>
<code>
<A ID="pgfId-967244"></A>
		disp = LOG2D(r-&gt;precision) + LOG2D(s.precision) + PHI *</code>
<code>
<A ID="pgfId-967245"></A>
		    LFP2D(r-&gt;dst - r-&gt;org);</code>
<code>
<A ID="pgfId-967246"></A>
	}</code>
<code>
<A ID="pgfId-967247"></A>
	clock_filter(p, offset, delay, disp);</code>
<code>
<A ID="pgfId-967248"></A>
}</code>
<code>
<A ID="pgfId-967249"></A>
</code>
<Heading3>
<A ID="pgfId-967250"></A>
clock_filter()</Heading3>
<code>
<A ID="pgfId-967251"></A>
/*</code>
<code>
<A ID="pgfId-967252"></A>
 * clock_filter(p, offset, delay, dispersion) - select the best from the</code>
<code>
<A ID="pgfId-967253"></A>
 * latest eight delay/offset samples.</code>
<code>
<A ID="pgfId-967254"></A>
 */</code>
<code>
<A ID="pgfId-967255"></A>
void</code>
<code>
<A ID="pgfId-967256"></A>
clock_filter(</code>
<code>
<A ID="pgfId-967257"></A>
	struct p *p,			/* peer structure pointer */</code>
<code>
<A ID="pgfId-967258"></A>
	double	offset,		/* clock offset */</code>
<code>
<A ID="pgfId-967259"></A>
	double	delay,		/* roundtrip delay */</code>
<code>
<A ID="pgfId-967260"></A>
	double	disp		/* dispersion */</code>
<code>
<A ID="pgfId-967261"></A>
	)</code>
<code>
<A ID="pgfId-967262"></A>
{</code>
<code>
<A ID="pgfId-967263"></A>
	struct f f[NSTAGE];	/* sorted list */</code>
<code>
<A ID="pgfId-967264"></A>
	double	dtemp;</code>
<code>
<A ID="pgfId-967265"></A>
	int	i;</code>
<code>
<A ID="pgfId-967266"></A>
</code>
<code>
<A ID="pgfId-967267"></A>
	/*</code>
<code>
<A ID="pgfId-967268"></A>
	 * The clock filter contents consist of eight tuples (offset,</code>
<code>
<A ID="pgfId-967269"></A>
	 * delay, dispersion, time). Shift each tuple to the left,</code>
<code>
<A ID="pgfId-967270"></A>
	 * discarding the leftmost one. As each tuple is shifted,</code>
<code>
<A ID="pgfId-967271"></A>
	 * increase the dispersion since the last filter update. At the</code>
<code>
<A ID="pgfId-967272"></A>
	 * same time, copy each tuple to a temporary list. After this,</code>
<code>
<A ID="pgfId-967273"></A>
	 * place the (offset, delay, disp, time) in the vacated</code>
<code>
<A ID="pgfId-967274"></A>
	 * rightmost tuple.</code>
<code>
<A ID="pgfId-967275"></A>
	 */</code>
<code>
<A ID="pgfId-967276"></A>
	for (i = 1; i &lt; NSTAGE; i++) {</code>
<code>
<A ID="pgfId-967277"></A>
		p-&gt;f[i] = p-&gt;f[i - 1];</code>
<code>
<A ID="pgfId-967278"></A>
		p-&gt;f[i].disp += PHI * (c.t - p-&gt;t);</code>
<code>
<A ID="pgfId-967279"></A>
		f[i] = p-&gt;f[i];</code>
<code>
<A ID="pgfId-967280"></A>
	}</code>
<code>
<A ID="pgfId-967281"></A>
	p-&gt;f[0].t = c.t;</code>
<code>
<A ID="pgfId-967282"></A>
	p-&gt;f[0].offset = offset;</code>
<code>
<A ID="pgfId-967283"></A>
	p-&gt;f[0].delay = delay;</code>
<code>
<A ID="pgfId-967284"></A>
	p-&gt;f[0].disp = disp;</code>
<code>
<A ID="pgfId-967285"></A>
	f[0] = p-&gt;f[0];</code>
<code>
<A ID="pgfId-967286"></A>
</code>
<code>
<A ID="pgfId-967287"></A>
	/*</code>
<code>
<A ID="pgfId-967288"></A>
	 * Sort the temporary list of tuples by increasing f[].delay.</code>
<code>
<A ID="pgfId-967289"></A>
	 * The first entry on the sorted list represents the best</code>
<code>
<A ID="pgfId-967290"></A>
	 * sample, but it might be old.</code>
<code>
<A ID="pgfId-967291"></A>
	 */</code>
<code>
<A ID="pgfId-967292"></A>
	dtemp = p-&gt;offset;</code>
<code>
<A ID="pgfId-967293"></A>
	p-&gt;offset = f[0].offset;</code>
<code>
<A ID="pgfId-967294"></A>
	p-&gt;delay = f[0].delay;</code>
<code>
<A ID="pgfId-967295"></A>
	for (i = 0; i &lt; NSTAGE; i++) {</code>
<code>
<A ID="pgfId-967296"></A>
		p-&gt;disp += f[i].disp / (2 ^ (i + 1));</code>
<code>
<A ID="pgfId-967297"></A>
		p-&gt;jitter += SQUARE(f[i].offset - f[0].offset);</code>
<code>
<A ID="pgfId-967298"></A>
	}</code>
<code>
<A ID="pgfId-967299"></A>
	p-&gt;jitter = max(SQRT(p-&gt;jitter), LOG2D(s.precision));</code>
<code>
<A ID="pgfId-967300"></A>
</code>
<code>
<A ID="pgfId-967301"></A>
	/*</code>
<code>
<A ID="pgfId-967302"></A>
	 * Prime directive: use a sample only once and never a sample</code>
<code>
<A ID="pgfId-967303"></A>
	 * older than the latest one, but anything goes before first </code>
<code>
<A ID="pgfId-967304"></A>
	 * synchronized.</code>
<code>
<A ID="pgfId-967305"></A>
	 */</code>
<code>
<A ID="pgfId-967306"></A>
	if (f[0].t - p-&gt;t &lt;= 0 &amp;&amp; s.leap != NOSYNC)</code>
<code>
<A ID="pgfId-967307"></A>
		return;</code>
<code>
<A ID="pgfId-967308"></A>
</code>
<code>
<A ID="pgfId-967309"></A>
	/*</code>
<code>
<A ID="pgfId-967310"></A>
	 * Popcorn spike suppressor. Compare the difference between the</code>
<code>
<A ID="pgfId-967311"></A>
	 * last and current offsets to the current jitter. If greater</code>
<code>
<A ID="pgfId-967312"></A>
	 * than SGATE (3) and if the interval since the last offset is</code>
<code>
<A ID="pgfId-967313"></A>
	 * less than twice the system poll interval, dump the spike.</code>
<code>
<A ID="pgfId-967314"></A>
	 * Otherwise, and if not in a burst, shake out the truechimers.</code>
<code>
<A ID="pgfId-967315"></A>
	 */</code>
<code>
<A ID="pgfId-967316"></A>
	if (fabs(p-&gt;offset - dtemp) &gt; SGATE * p-&gt;jitter &amp;&amp; (f[0].t -</code>
<code>
<A ID="pgfId-967317"></A>
	    p-&gt;t) &lt; 2 * s.poll)</code>
<code>
<A ID="pgfId-967318"></A>
		return;</code>
<code>
<A ID="pgfId-967319"></A>
</code>
<code>
<A ID="pgfId-967320"></A>
	p-&gt;t = f[0].t;</code>
<code>
<A ID="pgfId-967321"></A>
	if (p-&gt;burst == 0)</code>
<code>
<A ID="pgfId-967322"></A>
		clock_select();</code>
<code>
<A ID="pgfId-967323"></A>
	return;</code>
<code>
<A ID="pgfId-967324"></A>
}</code>
<code>
<A ID="pgfId-967437"></A>
</code>
<Heading3>
<A ID="pgfId-967438"></A>
<A ID="82407"></A>
fast_xmit()</Heading3>
<code>
<A ID="pgfId-967439"></A>
/*</code>
<code>
<A ID="pgfId-967440"></A>
 * fast_xmit() - transmit a reply packet for receive packet r</code>
<code>
<A ID="pgfId-967441"></A>
 */</code>
<code>
<A ID="pgfId-967442"></A>
void</code>
<code>
<A ID="pgfId-967443"></A>
fast_xmit(</code>
<code>
<A ID="pgfId-967444"></A>
	struct r *r,			/* receive packet pointer */</code>
<code>
<A ID="pgfId-967445"></A>
	int	mode,		/* association mode */</code>
<code>
<A ID="pgfId-967446"></A>
	int	auth		/* authentication code */</code>
<code>
<A ID="pgfId-967447"></A>
	)</code>
<code>
<A ID="pgfId-967448"></A>
{</code>
<code>
<A ID="pgfId-967449"></A>
	struct x x;</code>
<code>
<A ID="pgfId-967450"></A>
</code>
<code>
<A ID="pgfId-967451"></A>
	/*</code>
<code>
<A ID="pgfId-967452"></A>
	 * Initialize header and transmit timestamp. Note that the</code>
<code>
<A ID="pgfId-967453"></A>
	 * transmit version is copied from the receive version. This is</code>
<code>
<A ID="pgfId-967454"></A>
	 * for backward compatibility.</code>
<code>
<A ID="pgfId-967455"></A>
	 */</code>
<code>
<A ID="pgfId-967456"></A>
	x.version = r-&gt;version;</code>
<code>
<A ID="pgfId-967457"></A>
	x.srcaddr = r-&gt;dstaddr;</code>
<code>
<A ID="pgfId-967458"></A>
	x.dstaddr = r-&gt;srcaddr;</code>
<code>
<A ID="pgfId-967459"></A>
	x.leap = s.leap;</code>
<code>
<A ID="pgfId-967460"></A>
	x.mode = mode;</code>
<code>
<A ID="pgfId-967461"></A>
	if (s.stratum == MAXSTRAT)</code>
<code>
<A ID="pgfId-967462"></A>
		x.stratum = 0;</code>
<code>
<A ID="pgfId-967463"></A>
	else</code>
<code>
<A ID="pgfId-967464"></A>
		x.stratum = s.stratum;</code>
<code>
<A ID="pgfId-967465"></A>
	x.poll = r-&gt;poll;</code>
<code>
<A ID="pgfId-967466"></A>
	x.precision = s.precision;</code>
<code>
<A ID="pgfId-967467"></A>
	x.rootdelay = D2FP(s.rootdelay);</code>
<code>
<A ID="pgfId-967468"></A>
	x.rootdisp = D2FP(s.rootdisp);</code>
<code>
<A ID="pgfId-967469"></A>
	x.refid = s.refid;</code>
<code>
<A ID="pgfId-967470"></A>
	x.reftime = s.reftime;</code>
<code>
<A ID="pgfId-967471"></A>
	x.org = r-&gt;xmt;</code>
<code>
<A ID="pgfId-967472"></A>
	x.rec = r-&gt;dst;</code>
<code>
<A ID="pgfId-967473"></A>
	x.xmt = get_time();</code>
<code>
<A ID="pgfId-967474"></A>
</code>
<code>
<A ID="pgfId-967475"></A>
	/*</code>
<code>
<A ID="pgfId-967476"></A>
	 * If the authentication code is A.NONE, include only the</code>
<code>
<A ID="pgfId-967477"></A>
	 * header; if A.CRYPTO, send a crypto-NAK; if A.OK, send a valid</code>
<code>
<A ID="pgfId-967478"></A>
	 * MAC. Use the key ID in the received packet and the key in the</code>
<code>
<A ID="pgfId-967479"></A>
	 * local key cache.</code>
<code>
<A ID="pgfId-967480"></A>
	 */</code>
<code>
<A ID="pgfId-967481"></A>
	if (auth != A_NONE) {</code>
<code>
<A ID="pgfId-967482"></A>
		if (auth == A_CRYPTO) {</code>
<code>
<A ID="pgfId-967483"></A>
			x.keyid = 0;</code>
<code>
<A ID="pgfId-967484"></A>
		} else {</code>
<code>
<A ID="pgfId-967485"></A>
			x.keyid = r-&gt;keyid;</code>
<code>
<A ID="pgfId-967486"></A>
			x.digest = md5(x.keyid);</code>
<code>
<A ID="pgfId-967487"></A>
		}</code>
<code>
<A ID="pgfId-967488"></A>
	}</code>
<code>
<A ID="pgfId-967489"></A>
	xmit_packet(&amp;x);</code>
<code>
<A ID="pgfId-967490"></A>
}</code>
<code>
<A ID="pgfId-967491"></A>
</code>
<Heading3>
<A ID="pgfId-967492"></A>
access()</Heading3>
<code>
<A ID="pgfId-967493"></A>
/*</code>
<code>
<A ID="pgfId-967494"></A>
 * access() - determine access restrictions</code>
<code>
<A ID="pgfId-967495"></A>
 */</code>
<code>
<A ID="pgfId-967496"></A>
int</code>
<code>
<A ID="pgfId-967497"></A>
access(</code>
<code>
<A ID="pgfId-967498"></A>
	struct r *r			/* receive packet pointer */</code>
<code>
<A ID="pgfId-967499"></A>
	)</code>
<code>
<A ID="pgfId-967500"></A>
{</code>
<code>
<A ID="pgfId-967501"></A>
	/*</code>
<code>
<A ID="pgfId-967502"></A>
	 * The access control list is an ordered set of tuples</code>
<code>
<A ID="pgfId-967503"></A>
	 * consisting of an address, mask and restrict word containing</code>
<code>
<A ID="pgfId-967504"></A>
	 * defined bits. The list is searched for the first match on the</code>
<code>
<A ID="pgfId-967505"></A>
	 * source address (r-&gt;srcaddr) and the associated restrict word</code>
<code>
<A ID="pgfId-967506"></A>
	 * is returned.</code>
<code>
<A ID="pgfId-967507"></A>
	 */</code>
<code>
<A ID="pgfId-967508"></A>
	return (/* access bits */ 0);</code>
<code>
<A ID="pgfId-967509"></A>
}</code>
<code>
<A ID="pgfId-967531"></A>
</code>
<Heading2>
<A ID="pgfId-967510"></A>
<A ID="35392"></A>
System Process</Heading2>
<code>
<A ID="pgfId-967540"></A>
#include “ntp4.h”</code>
<code>
<A ID="pgfId-968900"></A>
</code>
<Heading3>
<A ID="pgfId-967541"></A>
<A ID="21510"></A>
clock_select()</Heading3>
<code>
<A ID="pgfId-967542"></A>
/*</code>
<code>
<A ID="pgfId-967543"></A>
 * clock_select() - find the best clocks</code>
<code>
<A ID="pgfId-967544"></A>
 */</code>
<code>
<A ID="pgfId-967545"></A>
void</code>
<code>
<A ID="pgfId-967546"></A>
clock_select() {</code>
<code>
<A ID="pgfId-967547"></A>
	struct p *p, *osys;			/* peer structure pointers */</code>
<code>
<A ID="pgfId-967548"></A>
	double	low, high;		/* correctness interval extents */</code>
<code>
<A ID="pgfId-967549"></A>
	int	allow, found, chime; /* used by intersecion algorithm */</code>
<code>
<A ID="pgfId-967550"></A>
	int	n, i, j;</code>
<code>
<A ID="pgfId-967551"></A>
</code>
<code>
<A ID="pgfId-967552"></A>
	/*</code>
<code>
<A ID="pgfId-967553"></A>
	 * We first cull the falsetickers from the server population,</code>
<code>
<A ID="pgfId-967554"></A>
	 * leaving only the truechimers. The correctness interval for</code>
<code>
<A ID="pgfId-967555"></A>
	 * association p is the interval from offset - root_dist() to</code>
<code>
<A ID="pgfId-967556"></A>
	 * offset + root_dist(). The object of the game is to find a</code>
<code>
<A ID="pgfId-967557"></A>
	 * majority clique; that is, an intersection of correctness</code>
<code>
<A ID="pgfId-967558"></A>
	 * intervals numbering more than half the server population.</code>
<code>
<A ID="pgfId-967559"></A>
	 *</code>
<code>
<A ID="pgfId-967560"></A>
	 * First construct the chime list of tuples (p, type, edge) as</code>
<code>
<A ID="pgfId-967561"></A>
	 * shown below, then sort the list by edge from lowest to</code>
<code>
<A ID="pgfId-967562"></A>
	 * highest.</code>
<code>
<A ID="pgfId-967563"></A>
	 */</code>
<code>
<A ID="pgfId-967564"></A>
	osys = s.p;</code>
<code>
<A ID="pgfId-967565"></A>
	s.p = NULL;</code>
<code>
<A ID="pgfId-967566"></A>
	n = 0;</code>
<code>
<A ID="pgfId-967567"></A>
	while (accept(p)) {</code>
<code>
<A ID="pgfId-967568"></A>
		s.m[n].p = p;</code>
<code>
<A ID="pgfId-967569"></A>
		s.m[n].type = +1;</code>
<code>
<A ID="pgfId-967570"></A>
		s.m[n].edge = p-&gt;offset + root_dist(p);</code>
<code>
<A ID="pgfId-967571"></A>
		n++;</code>
<code>
<A ID="pgfId-967572"></A>
		s.m[n].p = p;</code>
<code>
<A ID="pgfId-967573"></A>
		s.m[n].type = 0;</code>
<code>
<A ID="pgfId-967574"></A>
		s.m[n].edge = p-&gt;offset;</code>
<code>
<A ID="pgfId-967575"></A>
		n++;</code>
<code>
<A ID="pgfId-967576"></A>
		s.m[n].p = p;</code>
<code>
<A ID="pgfId-967577"></A>
		s.m[n].type = -1;</code>
<code>
<A ID="pgfId-967578"></A>
		s.m[n].edge = p-&gt;offset - root_dist(p);</code>
<code>
<A ID="pgfId-967579"></A>
		n++;</code>
<code>
<A ID="pgfId-967580"></A>
	}</code>
<code>
<A ID="pgfId-967581"></A>
</code>
<code>
<A ID="pgfId-967582"></A>
	/*</code>
<code>
<A ID="pgfId-967583"></A>
	 * Find the largest contiguous intersection of correctness</code>
<code>
<A ID="pgfId-967584"></A>
	 * intervals. Allow is the number of allowed falsetickers; found</code>
<code>
<A ID="pgfId-967585"></A>
	 * is the number of midpoints. Note that the edge values are</code>
<code>
<A ID="pgfId-967586"></A>
	 * limited to the range +-(2 ^ 30) &lt; +-2e9 by the timestamp</code>
<code>
<A ID="pgfId-967587"></A>
	 * calculations.</code>
<code>
<A ID="pgfId-967588"></A>
	 */</code>
<code>
<A ID="pgfId-967589"></A>
	low = 2e9; high = -2e9;</code>
<code>
<A ID="pgfId-967590"></A>
	for (allow = 0; 2 * allow &lt; n; allow++) {</code>
<code>
<A ID="pgfId-967591"></A>
</code>
<code>
<A ID="pgfId-967592"></A>
		/*</code>
<code>
<A ID="pgfId-967593"></A>
		 * Scan the chime list from lowest to highest to find</code>
<code>
<A ID="pgfId-967594"></A>
		 * the lower endpoint.</code>
<code>
<A ID="pgfId-967595"></A>
		 */</code>
<code>
<A ID="pgfId-967596"></A>
		found = 0;</code>
<code>
<A ID="pgfId-967597"></A>
		chime = 0;</code>
<code>
<A ID="pgfId-967598"></A>
		for (i = 0; i &lt; n; i++) {</code>
<code>
<A ID="pgfId-967599"></A>
			chime -= s.m[i].type;</code>
<code>
<A ID="pgfId-967600"></A>
			if (chime &gt;= n - found) {</code>
<code>
<A ID="pgfId-967601"></A>
				low = s.m[i].edge;</code>
<code>
<A ID="pgfId-967602"></A>
				break;</code>
<code>
<A ID="pgfId-967603"></A>
			}</code>
<code>
<A ID="pgfId-967604"></A>
			if (s.m[i].type == 0)</code>
<code>
<A ID="pgfId-967605"></A>
				found++;</code>
<code>
<A ID="pgfId-967606"></A>
		}</code>
<code>
<A ID="pgfId-967607"></A>
</code>
<code>
<A ID="pgfId-967608"></A>
		/*</code>
<code>
<A ID="pgfId-967609"></A>
		 * Scan the chime list from highest to lowest to find</code>
<code>
<A ID="pgfId-967610"></A>
		 * the upper endpoint.</code>
<code>
<A ID="pgfId-967611"></A>
		 */</code>
<code>
<A ID="pgfId-967612"></A>
		chime = 0;</code>
<code>
<A ID="pgfId-967613"></A>
		for (i = n - 1; i &gt;= 0; i--) {</code>
<code>
<A ID="pgfId-967614"></A>
			chime += s.m[i].type;</code>
<code>
<A ID="pgfId-967615"></A>
			if (chime &gt;= n - found) {</code>
<code>
<A ID="pgfId-967616"></A>
				high = s.m[i].edge;</code>
<code>
<A ID="pgfId-967617"></A>
				break;</code>
<code>
<A ID="pgfId-967618"></A>
			}</code>
<code>
<A ID="pgfId-967619"></A>
			if (s.m[i].type == 0)</code>
<code>
<A ID="pgfId-967620"></A>
				found++;</code>
<code>
<A ID="pgfId-967621"></A>
		}</code>
<code>
<A ID="pgfId-967622"></A>
</code>
<code>
<A ID="pgfId-967623"></A>
		/*</code>
<code>
<A ID="pgfId-967624"></A>
		 * If the number of midpoints is greater than the number</code>
<code>
<A ID="pgfId-967625"></A>
		 * of allowed falsetickers, the intersection contains at</code>
<code>
<A ID="pgfId-967626"></A>
		 * least one truechimer with no midpoint. If so,</code>
<code>
<A ID="pgfId-967627"></A>
		 * increment the number of allowed falsetickers and go</code>
<code>
<A ID="pgfId-967628"></A>
		 * around again. If not and the intersection is</code>
<code>
<A ID="pgfId-967629"></A>
		 * nonempty, declare success.</code>
<code>
<A ID="pgfId-967630"></A>
		 */</code>
<code>
<A ID="pgfId-967631"></A>
		if (found &gt; allow)</code>
<code>
<A ID="pgfId-967632"></A>
			continue;</code>
<code>
<A ID="pgfId-967633"></A>
</code>
<code>
<A ID="pgfId-967634"></A>
		if (high &gt; low)</code>
<code>
<A ID="pgfId-967635"></A>
			break;</code>
<code>
<A ID="pgfId-967636"></A>
	}</code>
<code>
<A ID="pgfId-967637"></A>
</code>
<code>
<A ID="pgfId-967638"></A>
	/*</code>
<code>
<A ID="pgfId-967639"></A>
	 * Clustering algorithm. Construct a list of survivors (p,</code>
<code>
<A ID="pgfId-967640"></A>
	 * metric) from the chime list, where metric is dominated first</code>
<code>
<A ID="pgfId-967641"></A>
	 * by stratum and then by root distance. All other things being</code>
<code>
<A ID="pgfId-967642"></A>
	 * equal, this is the order of preference.</code>
<code>
<A ID="pgfId-967643"></A>
	 */</code>
<code>
<A ID="pgfId-967644"></A>
	n = 0;</code>
<code>
<A ID="pgfId-967645"></A>
	for (i = 0; i &lt; n; i++) {</code>
<code>
<A ID="pgfId-967646"></A>
		if (s.m[i].edge &lt; low || s.m[i].edge &gt; high)</code>
<code>
<A ID="pgfId-967647"></A>
			continue;</code>
<code>
<A ID="pgfId-967648"></A>
</code>
<code>
<A ID="pgfId-967649"></A>
		p = s.m[i].p;</code>
<code>
<A ID="pgfId-967650"></A>
		s.v[n].p = p;</code>
<code>
<A ID="pgfId-967651"></A>
		s.v[n].metric = MAXDIST * p-&gt;stratum + root_dist(p);</code>
<code>
<A ID="pgfId-967652"></A>
		n++;</code>
<code>
<A ID="pgfId-967653"></A>
	}</code>
<code>
<A ID="pgfId-967654"></A>
</code>
<code>
<A ID="pgfId-967655"></A>
	/*</code>
<code>
<A ID="pgfId-967656"></A>
	 * There must be at least NSANE survivors to satisfy the</code>
<code>
<A ID="pgfId-967657"></A>
	 * correctness assertions. Ordinarily, the Byzantine criteria</code>
<code>
<A ID="pgfId-967658"></A>
	 * require four, susrvivors, but for the demonstration here, one</code>
<code>
<A ID="pgfId-967659"></A>
	 * is acceptable.</code>
<code>
<A ID="pgfId-967660"></A>
	 */</code>
<code>
<A ID="pgfId-967661"></A>
	if (n == NSANE)</code>
<code>
<A ID="pgfId-967662"></A>
		return;</code>
<code>
<A ID="pgfId-967663"></A>
</code>
<code>
<A ID="pgfId-967664"></A>
	/*</code>
<code>
<A ID="pgfId-967665"></A>
	 * For each association p in turn, calculate the selection</code>
<code>
<A ID="pgfId-967666"></A>
	 * jitter p-&gt;sjitter as the square root of the sum of squares</code>
<code>
<A ID="pgfId-967667"></A>
	 * (p-&gt;offset - q-&gt;offset) over all q associations. The idea is</code>
<code>
<A ID="pgfId-967668"></A>
	 * to repeatedly discard the survivor with maximum selection</code>
<code>
<A ID="pgfId-967669"></A>
	 * jitter until a termination condition is met.</code>
<code>
<A ID="pgfId-967670"></A>
	 */</code>
<code>
<A ID="pgfId-967671"></A>
	while (1) {</code>
<code>
<A ID="pgfId-967672"></A>
		struct p *p, *q, *qmax;	/* peer structure pointers */</code>
<code>
<A ID="pgfId-967673"></A>
		double	max, min, dtemp;</code>
<code>
<A ID="pgfId-967674"></A>
</code>
<code>
<A ID="pgfId-967675"></A>
		max = -2e9; min = 2e9;</code>
<code>
<A ID="pgfId-967676"></A>
		for (i = 0; i &lt; n; i++) {</code>
<code>
<A ID="pgfId-967677"></A>
			p = s.v[i].p;</code>
<code>
<A ID="pgfId-967678"></A>
			if (p-&gt;jitter &lt; min)</code>
<code>
<A ID="pgfId-967679"></A>
				min = p-&gt;jitter;</code>
<code>
<A ID="pgfId-967680"></A>
			dtemp = 0;</code>
<code>
<A ID="pgfId-967681"></A>
			for (j = 0; j &lt; n; j++) {</code>
<code>
<A ID="pgfId-967682"></A>
				q = s.v[j].p;</code>
<code>
<A ID="pgfId-967683"></A>
				dtemp += SQUARE(p-&gt;offset - q-&gt;offset);</code>
<code>
<A ID="pgfId-967684"></A>
			}</code>
<code>
<A ID="pgfId-967685"></A>
			dtemp = SQRT(dtemp);</code>
<code>
<A ID="pgfId-967686"></A>
			if (dtemp &gt; max) {</code>
<code>
<A ID="pgfId-967687"></A>
				max = dtemp;</code>
<code>
<A ID="pgfId-967688"></A>
				qmax = q;</code>
<code>
<A ID="pgfId-967689"></A>
			}</code>
<code>
<A ID="pgfId-967690"></A>
		}</code>
<code>
<A ID="pgfId-967691"></A>
</code>
<code>
<A ID="pgfId-967692"></A>
		/*</code>
<code>
<A ID="pgfId-967693"></A>
		 * If the maximum selection jitter is less than the</code>
<code>
<A ID="pgfId-967694"></A>
		 * minimum peer jitter, then tossing out more survivors</code>
<code>
<A ID="pgfId-967695"></A>
		 * will not lower the minimum peer jitter, so we might</code>
<code>
<A ID="pgfId-967696"></A>
		 * as well stop. To make sure a few survivors are left</code>
<code>
<A ID="pgfId-967697"></A>
		 * for the clustering algorithm to chew on, we also stop</code>
<code>
<A ID="pgfId-967698"></A>
		 * if the number of survivors is less than or equal to</code>
<code>
<A ID="pgfId-967699"></A>
		 * NMIN (3).</code>
<code>
<A ID="pgfId-967700"></A>
		 */</code>
<code>
<A ID="pgfId-967701"></A>
		if (max &lt; min || n &lt;= NMIN)</code>
<code>
<A ID="pgfId-967702"></A>
			break;</code>
<code>
<A ID="pgfId-967703"></A>
</code>
<code>
<A ID="pgfId-967704"></A>
		/*</code>
<code>
<A ID="pgfId-967705"></A>
		 * Delete survivor qmax from the list and go around</code>
<code>
<A ID="pgfId-967706"></A>
		 * again.</code>
<code>
<A ID="pgfId-967707"></A>
		 */</code>
<code>
<A ID="pgfId-967708"></A>
		n--;</code>
<code>
<A ID="pgfId-967709"></A>
	}</code>
<code>
<A ID="pgfId-967710"></A>
</code>
<code>
<A ID="pgfId-967711"></A>
	/*</code>
<code>
<A ID="pgfId-967712"></A>
	 * Pick the best clock. If the old system peer is on the list</code>
<code>
<A ID="pgfId-967713"></A>
	 * and at the same stratum as the first survivor on the list,</code>
<code>
<A ID="pgfId-967714"></A>
	 * then don’t do a clock hop. Otherwise, select the first</code>
<code>
<A ID="pgfId-967715"></A>
	 * survivor on the list as the new system peer.</code>
<code>
<A ID="pgfId-967716"></A>
	 */</code>
<code>
<A ID="pgfId-967717"></A>
	if (osys-&gt;stratum == s.v[0].p-&gt;stratum)</code>
<code>
<A ID="pgfId-967718"></A>
		s.p = osys;</code>
<code>
<A ID="pgfId-967719"></A>
	else</code>
<code>
<A ID="pgfId-967720"></A>
		s.p = s.v[0].p;</code>
<code>
<A ID="pgfId-967721"></A>
	clock_update(s.p);</code>
<code>
<A ID="pgfId-967722"></A>
}</code>
<code>
<A ID="pgfId-969909"></A>
</code>
<Heading3>
<A ID="pgfId-969910"></A>
<A ID="52653"></A>
root_dist()</Heading3>
<code>
<A ID="pgfId-969911"></A>
/*</code>
<code>
<A ID="pgfId-969912"></A>
 * root_dist() - calculate root distance</code>
<code>
<A ID="pgfId-969913"></A>
 */</code>
<code>
<A ID="pgfId-969914"></A>
double</code>
<code>
<A ID="pgfId-969915"></A>
root_dist(</code>
<code>
<A ID="pgfId-969916"></A>
	struct p *p		/* peer structure pointer */</code>
<code>
<A ID="pgfId-969917"></A>
	)</code>
<code>
<A ID="pgfId-969918"></A>
{</code>
<code>
<A ID="pgfId-969919"></A>
	/*</code>
<code>
<A ID="pgfId-969920"></A>
	 * The root synchronization distance is the maximum error due to</code>
<code>
<A ID="pgfId-969921"></A>
	 * all causes of the local clock relative to the primary server.</code>
<code>
<A ID="pgfId-969922"></A>
	 * It is defined as half the total delay plus total dispersion</code>
<code>
<A ID="pgfId-969923"></A>
	 * plus peer jitter.</code>
<code>
<A ID="pgfId-969924"></A>
	 */</code>
<code>
<A ID="pgfId-969925"></A>
	return (max(MINDISP, p-&gt;rootdelay + p-&gt;delay) / 2 +</code>
<code>
<A ID="pgfId-969926"></A>
	    p-&gt;rootdisp + p-&gt;disp + PHI * (c.t - p-&gt;t) + p-&gt;jitter);</code>
<code>
<A ID="pgfId-969927"></A>
}</code>
<code>
<A ID="pgfId-969635"></A>
</code>
<Heading3>
<A ID="pgfId-969592"></A>
<A ID="91918"></A>
accept()</Heading3>
<code>
<A ID="pgfId-969593"></A>
/*</code>
<code>
<A ID="pgfId-969594"></A>
 * accept() - test if association p is acceptable for synchronization</code>
<code>
<A ID="pgfId-969595"></A>
 */</code>
<code>
<A ID="pgfId-969596"></A>
int</code>
<code>
<A ID="pgfId-969597"></A>
accept(</code>
<code>
<A ID="pgfId-969598"></A>
	struct p *p		/* peer structure pointer */</code>
<code>
<A ID="pgfId-969599"></A>
	)</code>
<code>
<A ID="pgfId-969600"></A>
{</code>
<code>
<A ID="pgfId-969601"></A>
	/*</code>
<code>
<A ID="pgfId-969602"></A>
	 * A stratum error occurs if (1) the server has never been</code>
<code>
<A ID="pgfId-969603"></A>
	 * synchronized, (2) the server stratum is invalid.</code>
<code>
<A ID="pgfId-969604"></A>
	 */</code>
<code>
<A ID="pgfId-969605"></A>
	if (p-&gt;leap == NOSYNC || p-&gt;stratum &gt;= MAXSTRAT)</code>
<code>
<A ID="pgfId-969606"></A>
		return (FALSE);</code>
<code>
<A ID="pgfId-969607"></A>
</code>
<code>
<A ID="pgfId-969608"></A>
	/*</code>
<code>
<A ID="pgfId-969609"></A>
	 * A distance error occurs if the root distance exceeds the</code>
<code>
<A ID="pgfId-969610"></A>
	 * distance threshold plus an increment equal to one poll</code>
<code>
<A ID="pgfId-969611"></A>
	 * interval.</code>
<code>
<A ID="pgfId-969612"></A>
	 */</code>
<code>
<A ID="pgfId-969613"></A>
	if (root_dist(p) &gt; MAXDIST + PHI * LOG2D(s.poll))</code>
<code>
<A ID="pgfId-969614"></A>
		return (FALSE);</code>
<code>
<A ID="pgfId-969615"></A>
</code>
<code>
<A ID="pgfId-969616"></A>
	/*</code>
<code>
<A ID="pgfId-969617"></A>
	 * A loop error occurs if the remote peer is synchronized to the</code>
<code>
<A ID="pgfId-969618"></A>
	 * local peer or the remote peer is synchronized to the current</code>
<code>
<A ID="pgfId-969619"></A>
	 * system peer. Note this is the behavior for IPv4; for IPv6 the</code>
<code>
<A ID="pgfId-969620"></A>
	 * MD5 hash is used instead.</code>
<code>
<A ID="pgfId-969621"></A>
	 */</code>
<code>
<A ID="pgfId-969622"></A>
	if (p-&gt;refid == p-&gt;dstaddr || p-&gt;refid == s.refid)</code>
<code>
<A ID="pgfId-969623"></A>
		return (FALSE);</code>
<code>
<A ID="pgfId-969624"></A>
</code>
<code>
<A ID="pgfId-969625"></A>
	/*</code>
<code>
<A ID="pgfId-969626"></A>
	 * An unreachable error occurs if the server is unreachable.</code>
<code>
<A ID="pgfId-969627"></A>
	 */</code>
<code>
<A ID="pgfId-969628"></A>
	if (p-&gt;reach == 0)</code>
<code>
<A ID="pgfId-969629"></A>
		return (FALSE);</code>
<code>
<A ID="pgfId-969630"></A>
</code>
<code>
<A ID="pgfId-969631"></A>
	return (TRUE);</code>
<code>
<A ID="pgfId-969632"></A>
}</code>
<code>
<A ID="pgfId-967723"></A>
</code>
<Heading3>
<A ID="pgfId-967724"></A>
<A ID="55098"></A>
clock_update()</Heading3>
<code>
<A ID="pgfId-967725"></A>
/*</code>
<code>
<A ID="pgfId-967726"></A>
 * clock_update() - update the system clock</code>
<code>
<A ID="pgfId-967727"></A>
 */</code>
<code>
<A ID="pgfId-967728"></A>
void</code>
<code>
<A ID="pgfId-967729"></A>
clock_update(</code>
<code>
<A ID="pgfId-967730"></A>
	struct p *p			/* peer structure pointer */</code>
<code>
<A ID="pgfId-967731"></A>
	)</code>
<code>
<A ID="pgfId-967732"></A>
{</code>
<code>
<A ID="pgfId-967733"></A>
	double dtemp;</code>
<code>
<A ID="pgfId-967734"></A>
</code>
<code>
<A ID="pgfId-967735"></A>
	/*</code>
<code>
<A ID="pgfId-967736"></A>
	 * If this is an old update, for instance as the result of a</code>
<code>
<A ID="pgfId-967737"></A>
	 * system peer change, avoid it. We never use an old sample or</code>
<code>
<A ID="pgfId-967738"></A>
	 * the same sample twice.</code>
<code>
<A ID="pgfId-967739"></A>
	 *</code>
<code>
<A ID="pgfId-967740"></A>
	if (s.t &gt;= p-&gt;t)</code>
<code>
<A ID="pgfId-967741"></A>
		return;</code>
<code>
<A ID="pgfId-967742"></A>
</code>
<code>
<A ID="pgfId-967743"></A>
	/*</code>
<code>
<A ID="pgfId-967744"></A>
	 * Combine the survivor offsets and update the system clock; the</code>
<code>
<A ID="pgfId-967745"></A>
	 * local_clock() routine will tell us the good or bad news.</code>
<code>
<A ID="pgfId-967746"></A>
	 */</code>
<code>
<A ID="pgfId-967747"></A>
	s.t = p-&gt;t;</code>
<code>
<A ID="pgfId-967748"></A>
	clock_combine();</code>
<code>
<A ID="pgfId-967749"></A>
	switch (local_clock(p, s.offset)) {</code>
<code>
<A ID="pgfId-967750"></A>
</code>
<code>
<A ID="pgfId-967751"></A>
	/*</code>
<code>
<A ID="pgfId-967752"></A>
	 * The offset is too large and probably bogus. Complain to the</code>
<code>
<A ID="pgfId-967753"></A>
	 * system log and order the operator to set the clock manually</code>
<code>
<A ID="pgfId-967754"></A>
	 * within PANIC range. The reference implementation includes a</code>
<code>
<A ID="pgfId-967755"></A>
	 * command line option to disable this check and to change the</code>
<code>
<A ID="pgfId-967756"></A>
	 * panic threshold from the default 1000 s as required.</code>
<code>
<A ID="pgfId-967757"></A>
	 */</code>
<code>
<A ID="pgfId-967758"></A>
	case PANIC:</code>
<code>
<A ID="pgfId-967759"></A>
		exit (0);</code>
<code>
<A ID="pgfId-967760"></A>
</code>
<code>
<A ID="pgfId-967761"></A>
	/*</code>
<code>
<A ID="pgfId-967762"></A>
	 * The offset is more than the step threshold (0.125 s by</code>
<code>
<A ID="pgfId-967763"></A>
	 * default). After a step, all associations now have</code>
<code>
<A ID="pgfId-967764"></A>
	 * inconsistent time valurs, so they are reset and started</code>
<code>
<A ID="pgfId-967765"></A>
	 * fresh. The step threshold can be changed in the reference</code>
<code>
<A ID="pgfId-967766"></A>
	 * implementation in order to lessen the chance the clock might</code>
<code>
<A ID="pgfId-967767"></A>
	 * be stepped backwards. However, there may be serious</code>
<code>
<A ID="pgfId-967768"></A>
	 * consequences, as noted in the white papers at the NTP project</code>
<code>
<A ID="pgfId-967769"></A>
	 * site.</code>
<code>
<A ID="pgfId-967770"></A>
	 */</code>
<code>
<A ID="pgfId-967771"></A>
	case STEP:</code>
<code>
<A ID="pgfId-967772"></A>
		while (/* all associations */ 0)</code>
<code>
<A ID="pgfId-967773"></A>
			clear(p, X_STEP);</code>
<code>
<A ID="pgfId-967774"></A>
		s.stratum = MAXSTRAT;</code>
<code>
<A ID="pgfId-967775"></A>
		s.poll = MINPOLL;</code>
<code>
<A ID="pgfId-967776"></A>
		break;</code>
<code>
<A ID="pgfId-967777"></A>
</code>
<code>
<A ID="pgfId-967778"></A>
	/*</code>
<code>
<A ID="pgfId-967779"></A>
	 * The offset was less than the step threshold, which is the</code>
<code>
<A ID="pgfId-967780"></A>
	 * normal case. Update the system variables from the peer</code>
<code>
<A ID="pgfId-967781"></A>
	 * variables. The lower clamp on the dispersion increase is to</code>
<code>
<A ID="pgfId-967782"></A>
	 * avoid timing loops and clockhopping when highly precise</code>
<code>
<A ID="pgfId-967783"></A>
	 * sources are in play. The clamp can be changed from the</code>
<code>
<A ID="pgfId-967784"></A>
	 * default .01 s in the reference implementation.</code>
<code>
<A ID="pgfId-967785"></A>
	 */</code>
<code>
<A ID="pgfId-967786"></A>
	case SLEW:</code>
<code>
<A ID="pgfId-967787"></A>
		s.leap = p-&gt;leap;</code>
<code>
<A ID="pgfId-967788"></A>
		s.stratum = p-&gt;stratum + 1;</code>
<code>
<A ID="pgfId-967789"></A>
		s.refid = p-&gt;refid;</code>
<code>
<A ID="pgfId-967790"></A>
		s.reftime = p-&gt;reftime;</code>
<code>
<A ID="pgfId-967791"></A>
		s.rootdelay = p-&gt;rootdelay + p-&gt;delay;</code>
<code>
<A ID="pgfId-967792"></A>
		dtemp = SQRT(SQUARE(p-&gt;jitter) + SQUARE(s.jitter));</code>
<code>
<A ID="pgfId-967793"></A>
		dtemp += max(p-&gt;disp + PHI * (c.t - p-&gt;t) +</code>
<code>
<A ID="pgfId-967794"></A>
		    fabs(p-&gt;offset), MINDISP);</code>
<code>
<A ID="pgfId-967795"></A>
		s.rootdisp = p-&gt;rootdisp + dtemp;</code>
<code>
<A ID="pgfId-967796"></A>
		break;</code>
<code>
<A ID="pgfId-967797"></A>
</code>
<code>
<A ID="pgfId-967798"></A>
	/*</code>
<code>
<A ID="pgfId-967799"></A>
	 * Some samples are discarded while, for instance, a direct</code>
<code>
<A ID="pgfId-967800"></A>
	 * frequency measurement is being made.</code>
<code>
<A ID="pgfId-967801"></A>
	 */</code>
<code>
<A ID="pgfId-967802"></A>
	case IGNORE:</code>
<code>
<A ID="pgfId-967803"></A>
		break;</code>
<code>
<A ID="pgfId-967804"></A>
	}</code>
<code>
<A ID="pgfId-967805"></A>
}</code>
<code>
<A ID="pgfId-967806"></A>
</code>
<Heading3>
<A ID="pgfId-967807"></A>
<A ID="42532"></A>
clock_combine()</Heading3>
<code>
<A ID="pgfId-967808"></A>
/*</code>
<code>
<A ID="pgfId-967809"></A>
 * clock_combine() - combine offsets</code>
<code>
<A ID="pgfId-967810"></A>
 */</code>
<code>
<A ID="pgfId-967811"></A>
void</code>
<code>
<A ID="pgfId-967812"></A>
clock_combine()</code>
<code>
<A ID="pgfId-967813"></A>
{</code>
<code>
<A ID="pgfId-967814"></A>
	struct p *p;		/* peer structure pointer */</code>
<code>
<A ID="pgfId-967815"></A>
	double x, y, z, w;</code>
<code>
<A ID="pgfId-967816"></A>
	int	i;</code>
<code>
<A ID="pgfId-967817"></A>
</code>
<code>
<A ID="pgfId-967818"></A>
	/*</code>
<code>
<A ID="pgfId-967819"></A>
	 * Combine the offsets of the clustering algorithm survivors</code>
<code>
<A ID="pgfId-967820"></A>
	 * using a weighted average with weight determined by the root</code>
<code>
<A ID="pgfId-967821"></A>
	 * distance. Compute the selection jitter as the weighted RMS</code>
<code>
<A ID="pgfId-967822"></A>
	 * difference between the first survivor and the remaining</code>
<code>
<A ID="pgfId-967823"></A>
	 * survivors. In some cases the inherent clock jitter can be</code>
<code>
<A ID="pgfId-967824"></A>
	 * reduced by not using this algorithm, especially when frequent</code>
<code>
<A ID="pgfId-967825"></A>
	 * clockhopping is involved. The reference implementation can be</code>
<code>
<A ID="pgfId-967826"></A>
	 * configured to avoid this algorithm by designating a preferred</code>
<code>
<A ID="pgfId-967827"></A>
	 * peer.</code>
<code>
<A ID="pgfId-967828"></A>
	 */</code>
<code>
<A ID="pgfId-967829"></A>
	y = z = w = 0;</code>
<code>
<A ID="pgfId-967830"></A>
	for (i = 0; s.v[i].p != NULL; i++) {</code>
<code>
<A ID="pgfId-967831"></A>
		p = s.v[i].p;</code>
<code>
<A ID="pgfId-967832"></A>
		x = root_dist(p);</code>
<code>
<A ID="pgfId-967833"></A>
		y += 1 / x;</code>
<code>
<A ID="pgfId-967834"></A>
		z += p-&gt;offset / x;</code>
<code>
<A ID="pgfId-967835"></A>
		w += SQUARE(p-&gt;offset - s.v[0].p-&gt;offset) / x;</code>
<code>
<A ID="pgfId-967836"></A>
	}</code>
<code>
<A ID="pgfId-967870"></A>
	s.offset = z / y;</code>
<code>
<A ID="pgfId-967871"></A>
	s.jitter = SQRT(w / y);</code>
<code>
<A ID="pgfId-967872"></A>
}</code>
<code>
<A ID="pgfId-969104"></A>
</code>
<Heading3>
<A ID="pgfId-969128"></A>
<A ID="87891"></A>
local_clock()</Heading3>
<code>
<A ID="pgfId-969141"></A>
#include “ntp4.h”</code>
<code>
<A ID="pgfId-969142"></A>
</code>
<code>
<A ID="pgfId-969143"></A>
/*</code>
<code>
<A ID="pgfId-969144"></A>
 * Constants</code>
<code>
<A ID="pgfId-969145"></A>
 */</code>
<code>
<A ID="pgfId-969146"></A>
#define STEPT		.128		/* step threshold (s) */</code>
<code>
<A ID="pgfId-969147"></A>
#define WATCH		900		/* stepout threshold (s) */</code>
<code>
<A ID="pgfId-969148"></A>
#define PANICT		1000		/* panic threshold (s) */</code>
<code>
<A ID="pgfId-969149"></A>
#define PLL		65536		/* PLL loop gain */</code>
<code>
<A ID="pgfId-969150"></A>
#define FLL		MAXPOLL + 1		/* FLL loop gain */</code>
<code>
<A ID="pgfId-969151"></A>
#define AVG		4		/* parameter averaging constant */</code>
<code>
<A ID="pgfId-969152"></A>
#define ALLAN		1500		/* compromise Allan intercept (s) */</code>
<code>
<A ID="pgfId-969153"></A>
#define LIMIT		30		/* poll-adjust threshold */</code>
<code>
<A ID="pgfId-969154"></A>
#define MAXFREQ		500e-6		/* maximum frequency tolerance (s/s) */</code>
<code>
<A ID="pgfId-969155"></A>
#define PGATE		4		/* poll-adjust gate */</code>
<code>
<A ID="pgfId-969156"></A>
</code>
<code>
<A ID="pgfId-969483"></A>
/*</code>
<code>
<A ID="pgfId-969161"></A>
 * local_clock() - discipline the local clock</code>
<code>
<A ID="pgfId-969162"></A>
 */</code>
<code>
<A ID="pgfId-969163"></A>
int				/* return code */</code>
<code>
<A ID="pgfId-969164"></A>
local_clock(</code>
<code>
<A ID="pgfId-969165"></A>
	struct p *p,			/* peer structure pointer */</code>
<code>
<A ID="pgfId-969166"></A>
	double	offset		/* clock offset from combine() */</code>
<code>
<A ID="pgfId-969167"></A>
	)</code>
<code>
<A ID="pgfId-969168"></A>
{</code>
<code>
<A ID="pgfId-969169"></A>
	int	state;		/* clock discipline state */</code>
<code>
<A ID="pgfId-969170"></A>
	double	freq;		/* frequency */</code>
<code>
<A ID="pgfId-969171"></A>
	double	mu;		/* interval since last update */</code>
<code>
<A ID="pgfId-969172"></A>
	int	rval;</code>
<code>
<A ID="pgfId-969173"></A>
	double	etemp, dtemp;</code>
<code>
<A ID="pgfId-969174"></A>
</code>
<code>
<A ID="pgfId-969175"></A>
	/*</code>
<code>
<A ID="pgfId-969176"></A>
	 * If the offset is too large, give up and go home.</code>
<code>
<A ID="pgfId-969177"></A>
	 */</code>
<code>
<A ID="pgfId-969178"></A>
	if (fabs(offset) &gt; PANICT)</code>
<code>
<A ID="pgfId-969179"></A>
		return (PANIC);</code>
<code>
<A ID="pgfId-969180"></A>
</code>
<code>
<A ID="pgfId-969181"></A>
	/*</code>
<code>
<A ID="pgfId-969182"></A>
	 * Clock state machine transition function. This is where the</code>
<code>
<A ID="pgfId-969183"></A>
	 * action is and defines how the system reacts to large time</code>
<code>
<A ID="pgfId-969184"></A>
	 * and frequency errors. There are two main regimes: when the</code>
<code>
<A ID="pgfId-969185"></A>
	 * offset exceeds the step threshold and when it does not.</code>
<code>
<A ID="pgfId-969186"></A>
	 */</code>
<code>
<A ID="pgfId-969187"></A>
	rval = SLEW;</code>
<code>
<A ID="pgfId-969188"></A>
	mu = p-&gt;t - s.t;</code>
<code>
<A ID="pgfId-969189"></A>
	freq = 0;</code>
<code>
<A ID="pgfId-969190"></A>
	if (fabs(offset) &gt; STEPT) {</code>
<code>
<A ID="pgfId-969191"></A>
		switch (c.state) {</code>
<code>
<A ID="pgfId-969192"></A>
</code>
<code>
<A ID="pgfId-969193"></A>
		/*</code>
<code>
<A ID="pgfId-969194"></A>
		 * In S_SYNC state we ignore the first outlyer amd</code>
<code>
<A ID="pgfId-969195"></A>
		 * switch to S_SPIK state.</code>
<code>
<A ID="pgfId-969196"></A>
		 */</code>
<code>
<A ID="pgfId-969197"></A>
		case SYNC:</code>
<code>
<A ID="pgfId-969198"></A>
			state = SPIK;</code>
<code>
<A ID="pgfId-969199"></A>
			return (rval);</code>
<code>
<A ID="pgfId-969200"></A>
</code>
<code>
<A ID="pgfId-969201"></A>
		/*</code>
<code>
<A ID="pgfId-969202"></A>
		 * In S_FREQ state we ignore outlyers and inlyers. At</code>
<code>
<A ID="pgfId-969203"></A>
		 * the first outlyer after the stepout threshold,</code>
<code>
<A ID="pgfId-969204"></A>
		 * compute the apparent frequency correction and step</code>
<code>
<A ID="pgfId-969205"></A>
		 * the time.</code>
<code>
<A ID="pgfId-969206"></A>
		 */</code>
<code>
<A ID="pgfId-969207"></A>
		case FREQ:</code>
<code>
<A ID="pgfId-969208"></A>
			if (mu &lt; WATCH)</code>
<code>
<A ID="pgfId-969209"></A>
				return (IGNORE);</code>
<code>
<A ID="pgfId-969210"></A>
</code>
<code>
<A ID="pgfId-969211"></A>
			freq = (offset - c.base - c.offset) / mu;</code>
<code>
<A ID="pgfId-969212"></A>
			/* fall through to S_SPIK */</code>
<code>
<A ID="pgfId-969213"></A>
</code>
<code>
<A ID="pgfId-969214"></A>
		/*</code>
<code>
<A ID="pgfId-969215"></A>
		 * In S_SPIK state we ignore succeeding outlyers until</code>
<code>
<A ID="pgfId-969216"></A>
		 * either an inlyer is found or the stepout threshold is</code>
<code>
<A ID="pgfId-969217"></A>
		 * exceeded.</code>
<code>
<A ID="pgfId-969218"></A>
		 */</code>
<code>
<A ID="pgfId-969219"></A>
		case SPIK:</code>
<code>
<A ID="pgfId-969220"></A>
			if (mu &lt; WATCH)</code>
<code>
<A ID="pgfId-969221"></A>
				return (IGNORE);</code>
<code>
<A ID="pgfId-969222"></A>
</code>
<code>
<A ID="pgfId-969223"></A>
			/* fall through to default */</code>
<code>
<A ID="pgfId-969224"></A>
</code>
<code>
<A ID="pgfId-969225"></A>
		/*</code>
<code>
<A ID="pgfId-969226"></A>
		 * We get here by default in S_NSET and S_FSET states</code>
<code>
<A ID="pgfId-969227"></A>
		 * and from above in S_FREQ state. Step the time and</code>
<code>
<A ID="pgfId-969228"></A>
		 * clamp down the poll interval.</code>
<code>
<A ID="pgfId-969229"></A>
		 *</code>
<code>
<A ID="pgfId-969230"></A>
		 * In S_NSET state an initial frequency correction is</code>
<code>
<A ID="pgfId-969231"></A>
		 * not available, usually because the frequency file has</code>
<code>
<A ID="pgfId-969232"></A>
		 * not yet been written. Since the time is outside the</code>
<code>
<A ID="pgfId-969233"></A>
		 * capture range, the clock is stepped. The frequency</code>
<code>
<A ID="pgfId-969234"></A>
		 * will be set directly following the stepout interval.</code>
<code>
<A ID="pgfId-969235"></A>
		 *</code>
<code>
<A ID="pgfId-969236"></A>
		 * In S_FSET state the initial frequency has been set</code>
<code>
<A ID="pgfId-969237"></A>
		 * from the frequency file. Since the time is outside</code>
<code>
<A ID="pgfId-969238"></A>
		 * the capture range, the clock is stepped immediately,</code>
<code>
<A ID="pgfId-969239"></A>
		 * rather than after the stepout interval. Guys get</code>
<code>
<A ID="pgfId-969240"></A>
		 * nervous if it takes 17 minutes to set the clock for</code>
<code>
<A ID="pgfId-969241"></A>
		 * the first time.</code>
<code>
<A ID="pgfId-969242"></A>
		 *</code>
<code>
<A ID="pgfId-969243"></A>
		 * In S_SPIK state the stepout threshold has expired and</code>
<code>
<A ID="pgfId-969244"></A>
		 * the phase is still above the step threshold. Note</code>
<code>
<A ID="pgfId-969245"></A>
		 * that a single spike greater than the step threshold</code>
<code>
<A ID="pgfId-969246"></A>
		 * is always suppressed, even at the longer poll</code>
<code>
<A ID="pgfId-969247"></A>
		 * intervals.</code>
<code>
<A ID="pgfId-969248"></A>
		 */ </code>
<code>
<A ID="pgfId-969249"></A>
		default:</code>
<code>
<A ID="pgfId-969250"></A>
</code>
<code>
<A ID="pgfId-969251"></A>
			/*</code>
<code>
<A ID="pgfId-969252"></A>
			 * This is the kernel set time function, usually</code>
<code>
<A ID="pgfId-969253"></A>
			 * implemented by the Unix settimeofday() system</code>
<code>
<A ID="pgfId-969254"></A>
			 * call.</code>
<code>
<A ID="pgfId-969255"></A>
			 */</code>
<code>
<A ID="pgfId-969256"></A>
			step_time(offset);</code>
<code>
<A ID="pgfId-969257"></A>
			c.count = 0;</code>
<code>
<A ID="pgfId-969258"></A>
			rval = STEP;</code>
<code>
<A ID="pgfId-969259"></A>
			if (state == NSET) {</code>
<code>
<A ID="pgfId-969260"></A>
				rstclock(FREQ, p-&gt;t, 0);</code>
<code>
<A ID="pgfId-969261"></A>
				return (rval);</code>
<code>
<A ID="pgfId-969262"></A>
			}</code>
<code>
<A ID="pgfId-969263"></A>
			break;</code>
<code>
<A ID="pgfId-969264"></A>
		}</code>
<code>
<A ID="pgfId-969265"></A>
		rstclock(SYNC, p-&gt;t, 0);</code>
<code>
<A ID="pgfId-969266"></A>
	} else {</code>
<code>
<A ID="pgfId-969267"></A>
</code>
<code>
<A ID="pgfId-969268"></A>
		/*</code>
<code>
<A ID="pgfId-969269"></A>
		 * Compute the clock jitter as the RMS of exponentially</code>
<code>
<A ID="pgfId-969270"></A>
		 * weighted offset differences. This is used by the</code>
<code>
<A ID="pgfId-969271"></A>
		 * poll-adjust code.</code>
<code>
<A ID="pgfId-969272"></A>
		 */</code>
<code>
<A ID="pgfId-969273"></A>
		etemp = SQUARE(c.jitter);</code>
<code>
<A ID="pgfId-969274"></A>
		dtemp = SQUARE(max(fabs(offset - c.last),</code>
<code>
<A ID="pgfId-969275"></A>
		    LOG2D(s.precision)));</code>
<code>
<A ID="pgfId-969276"></A>
		c.jitter = SQRT(etemp + (dtemp - etemp) / AVG);</code>
<code>
<A ID="pgfId-969277"></A>
		switch (c.state) {</code>
<code>
<A ID="pgfId-969278"></A>
</code>
<code>
<A ID="pgfId-969279"></A>
		/*</code>
<code>
<A ID="pgfId-969280"></A>
		 * In S_NSET state this is the first update received and</code>
<code>
<A ID="pgfId-969281"></A>
		 * the frequency has not been initialized. The first</code>
<code>
<A ID="pgfId-969282"></A>
		 * thing to do is directly measure the oscillator</code>
<code>
<A ID="pgfId-969283"></A>
		 * frequency.</code>
<code>
<A ID="pgfId-969284"></A>
		 */</code>
<code>
<A ID="pgfId-969285"></A>
		case NSET:</code>
<code>
<A ID="pgfId-969286"></A>
			c.offset = offset;</code>
<code>
<A ID="pgfId-969287"></A>
			rstclock(FREQ, p-&gt;t, offset);</code>
<code>
<A ID="pgfId-969288"></A>
			return (IGNORE);</code>
<code>
<A ID="pgfId-969289"></A>
</code>
<code>
<A ID="pgfId-969290"></A>
		/*</code>
<code>
<A ID="pgfId-969291"></A>
		 * In S_FSET state this is the first update and the</code>
<code>
<A ID="pgfId-969292"></A>
		 * frequency has been initialized. Adjust the phase, but</code>
<code>
<A ID="pgfId-969293"></A>
		 * don’t adjust the frequency until the next update.</code>
<code>
<A ID="pgfId-969294"></A>
		 */</code>
<code>
<A ID="pgfId-969295"></A>
		case FSET:</code>
<code>
<A ID="pgfId-969296"></A>
			c.offset = offset;</code>
<code>
<A ID="pgfId-969297"></A>
			break;</code>
<code>
<A ID="pgfId-969298"></A>
</code>
<code>
<A ID="pgfId-969299"></A>
		/*</code>
<code>
<A ID="pgfId-969300"></A>
		 * In S_FREQ state ignore updates until the stepout</code>
<code>
<A ID="pgfId-969301"></A>
		 * threshold. After that, correct the phase and</code>
<code>
<A ID="pgfId-969302"></A>
		 * frequency and switch to S_SYNC state.</code>
<code>
<A ID="pgfId-969303"></A>
		 */</code>
<code>
<A ID="pgfId-969304"></A>
		case FREQ:</code>
<code>
<A ID="pgfId-969305"></A>
			if (c.t - s.t &lt; WATCH)</code>
<code>
<A ID="pgfId-969306"></A>
				return (IGNORE);</code>
<code>
<A ID="pgfId-969307"></A>
</code>
<code>
<A ID="pgfId-969308"></A>
			freq = (offset - c.base - c.offset) / mu;</code>
<code>
<A ID="pgfId-969309"></A>
			break;</code>
<code>
<A ID="pgfId-969310"></A>
</code>
<code>
<A ID="pgfId-969311"></A>
		/*</code>
<code>
<A ID="pgfId-969312"></A>
		 * We get here by default in S_SYNC and S_SPIK states.</code>
<code>
<A ID="pgfId-969313"></A>
		 * Here we compute the frequency update due to PLL and</code>
<code>
<A ID="pgfId-969314"></A>
		 * FLL contributions.</code>
<code>
<A ID="pgfId-969315"></A>
		 */</code>
<code>
<A ID="pgfId-969316"></A>
		default:</code>
<code>
<A ID="pgfId-969317"></A>
</code>
<code>
<A ID="pgfId-969318"></A>
			/*</code>
<code>
<A ID="pgfId-969319"></A>
			 * The FLL and PLL frequency gain constants</code>
<code>
<A ID="pgfId-969320"></A>
			 * depend on the poll interval and Allan</code>
<code>
<A ID="pgfId-969321"></A>
			 * intercept. The FLL is not used below one-half</code>
<code>
<A ID="pgfId-969322"></A>
			 * the Allan intercept. Above that the loop gain</code>
<code>
<A ID="pgfId-969323"></A>
			 * increases in steps to 1 / AVG.</code>
<code>
<A ID="pgfId-969324"></A>
			 */ </code>
<code>
<A ID="pgfId-969325"></A>
			if (LOG2D(s.poll) &gt; ALLAN / 2) {</code>
<code>
<A ID="pgfId-969326"></A>
				etemp = FLL - s.poll;</code>
<code>
<A ID="pgfId-969327"></A>
				if (etemp &lt; AVG)</code>
<code>
<A ID="pgfId-969328"></A>
					etemp = AVG;</code>
<code>
<A ID="pgfId-969329"></A>
				freq += (offset - c.offset) / (max(mu,</code>
<code>
<A ID="pgfId-969330"></A>
				    ALLAN) * etemp);</code>
<code>
<A ID="pgfId-969331"></A>
			}</code>
<code>
<A ID="pgfId-969332"></A>
</code>
<code>
<A ID="pgfId-969333"></A>
			/*</code>
<code>
<A ID="pgfId-969334"></A>
			 * For the PLL the integration interval</code>
<code>
<A ID="pgfId-969335"></A>
			 * (numerator) is the minimum of the update</code>
<code>
<A ID="pgfId-969336"></A>
			 * interval and poll interval. This allows</code>
<code>
<A ID="pgfId-969337"></A>
			 * oversampling, but not undersampling.</code>
<code>
<A ID="pgfId-969338"></A>
			 */ </code>
<code>
<A ID="pgfId-969339"></A>
			etemp = min(mu, LOG2D(s.poll));</code>
<code>
<A ID="pgfId-969340"></A>
			dtemp = 4 * PLL * LOG2D(s.poll);</code>
<code>
<A ID="pgfId-969341"></A>
			freq += offset * etemp / (dtemp * dtemp);</code>
<code>
<A ID="pgfId-969342"></A>
			break;</code>
<code>
<A ID="pgfId-969343"></A>
		}</code>
<code>
<A ID="pgfId-969344"></A>
		rstclock(SYNC, p-&gt;t, offset);</code>
<code>
<A ID="pgfId-969345"></A>
	}</code>
<code>
<A ID="pgfId-969346"></A>
</code>
<code>
<A ID="pgfId-969347"></A>
	/*</code>
<code>
<A ID="pgfId-969348"></A>
	 * Calculate the new frequency and frequency stability (wander).</code>
<code>
<A ID="pgfId-969349"></A>
	 * Compute the clock wander as the RMS of exponentially weighted</code>
<code>
<A ID="pgfId-969350"></A>
	 * frequency differences. This is not used directly, but can,</code>
<code>
<A ID="pgfId-969351"></A>
	 * along withthe jitter, be a highly useful monitoring and</code>
<code>
<A ID="pgfId-969352"></A>
	 * debugging tool</code>
<code>
<A ID="pgfId-969353"></A>
	 */</code>
<code>
<A ID="pgfId-969354"></A>
	freq += c.freq;</code>
<code>
<A ID="pgfId-969355"></A>
	c.freq = max(min(MAXFREQ, freq), -MAXFREQ);</code>
<code>
<A ID="pgfId-969356"></A>
	etemp = SQUARE(c.wander);</code>
<code>
<A ID="pgfId-969357"></A>
	dtemp = SQUARE(freq);</code>
<code>
<A ID="pgfId-969358"></A>
	c.wander = SQRT(etemp + (dtemp - etemp) / AVG);</code>
<code>
<A ID="pgfId-969359"></A>
</code>
<code>
<A ID="pgfId-969360"></A>
	/*</code>
<code>
<A ID="pgfId-969361"></A>
	 * Here we adjust the poll interval by comparing the current</code>
<code>
<A ID="pgfId-969362"></A>
	 * offset with the clock jitter. If the offset is less than the</code>
<code>
<A ID="pgfId-969363"></A>
	 * clock jitter times a constant, then the averaging interval is</code>
<code>
<A ID="pgfId-969364"></A>
	 * increased, otherwise it is decreased. A bit of hysteresis</code>
<code>
<A ID="pgfId-969365"></A>
	 * helps calm the dance. Works best using burst mode.</code>
<code>
<A ID="pgfId-969366"></A>
	 */</code>
<code>
<A ID="pgfId-969367"></A>
	if (fabs(c.offset) &lt; PGATE * c.jitter) {</code>
<code>
<A ID="pgfId-969368"></A>
		c.count += s.poll;</code>
<code>
<A ID="pgfId-969369"></A>
		if (c.count &gt; LIMIT) {</code>
<code>
<A ID="pgfId-969370"></A>
			c.count = LIMIT;</code>
<code>
<A ID="pgfId-969371"></A>
			if (s.poll &lt; MAXPOLL) {</code>
<code>
<A ID="pgfId-969372"></A>
				c.count = 0;</code>
<code>
<A ID="pgfId-969373"></A>
				s.poll++;</code>
<code>
<A ID="pgfId-969374"></A>
			}</code>
<code>
<A ID="pgfId-969375"></A>
		}</code>
<code>
<A ID="pgfId-969376"></A>
	} else {</code>
<code>
<A ID="pgfId-969377"></A>
		c.count -= s.poll &lt;&lt; 1;</code>
<code>
<A ID="pgfId-970228"></A>
		if (c.count &lt; -LIMIT) {</code>
<code>
<A ID="pgfId-970229"></A>
			c.count = -LIMIT;</code>
<code>
<A ID="pgfId-970230"></A>
			if (s.poll &gt; MINPOLL) {</code>
<code>
<A ID="pgfId-969381"></A>
				c.count = 0;</code>
<code>
<A ID="pgfId-969382"></A>
				s.poll--;</code>
<code>
<A ID="pgfId-969383"></A>
			}</code>
<code>
<A ID="pgfId-969384"></A>
		}</code>
<code>
<A ID="pgfId-969385"></A>
	}</code>
<code>
<A ID="pgfId-969386"></A>
	return (rval);</code>
<code>
<A ID="pgfId-969387"></A>
}</code>
<code>
<A ID="pgfId-969388"></A>
</code>
<Heading3>
<A ID="pgfId-969389"></A>
rstclock()</Heading3>
<code>
<A ID="pgfId-969390"></A>
/*</code>
<code>
<A ID="pgfId-969391"></A>
 * rstclock() - clock state machine</code>
<code>
<A ID="pgfId-969392"></A>
 */</code>
<code>
<A ID="pgfId-969393"></A>
void</code>
<code>
<A ID="pgfId-969394"></A>
rstclock(</code>
<code>
<A ID="pgfId-969395"></A>
	int	state,		/* new state */</code>
<code>
<A ID="pgfId-969396"></A>
	double	offset,		/* new offset */</code>
<code>
<A ID="pgfId-969397"></A>
	double	t		/* new update time */</code>
<code>
<A ID="pgfId-969398"></A>
	)</code>
<code>
<A ID="pgfId-969399"></A>
{</code>
<code>
<A ID="pgfId-969400"></A>
	/*</code>
<code>
<A ID="pgfId-969401"></A>
	 * Enter new state and set state variables. Note we use the time</code>
<code>
<A ID="pgfId-969402"></A>
	 * of the last clock filter sample, which must be earlier than</code>
<code>
<A ID="pgfId-969403"></A>
	 * the current time.</code>
<code>
<A ID="pgfId-969404"></A>
	 */</code>
<code>
<A ID="pgfId-969405"></A>
	c.state = state;</code>
<code>
<A ID="pgfId-969406"></A>
	c.base = offset - c.offset;</code>
<code>
<A ID="pgfId-969407"></A>
	c.last = c.offset = offset;</code>
<code>
<A ID="pgfId-969408"></A>
	s.t = t;</code>
<code>
<A ID="pgfId-969409"></A>
}</code>
<code>
<A ID="pgfId-969410"></A>
</code>
<Heading2>
<A ID="pgfId-969498"></A>
<A ID="39735"></A>
Clock Adjust Process</Heading2>
<Heading3>
<A ID="pgfId-969412"></A>
<A ID="67421"></A>
clock_adjust()</Heading3>
<code>
<A ID="pgfId-969501"></A>
/*</code>
<code>
<A ID="pgfId-969415"></A>
 * clock_adjust() - runs at one-second intervals</code>
<code>
<A ID="pgfId-969416"></A>
 */</code>
<code>
<A ID="pgfId-969417"></A>
void</code>
<code>
<A ID="pgfId-969418"></A>
clock_adjust() {</code>
<code>
<A ID="pgfId-969419"></A>
	double	dtemp;</code>
<code>
<A ID="pgfId-969420"></A>
</code>
<code>
<A ID="pgfId-969421"></A>
	/*</code>
<code>
<A ID="pgfId-969422"></A>
	 * Update the process time c.t. Also increase the dispersion</code>
<code>
<A ID="pgfId-969423"></A>
	 * since the last update. In contrast to NTPv3, NTPv4 does not</code>
<code>
<A ID="pgfId-969424"></A>
	 * declare unsynchronized after one day, since the dispersion</code>
<code>
<A ID="pgfId-969425"></A>
	 * threshold serves this function. When the dispersion exceeds</code>
<code>
<A ID="pgfId-969426"></A>
	 * MAXDIST (1 s), the server is considered unaccept for</code>
<code>
<A ID="pgfId-969427"></A>
	 * synchroniztion.</code>
<code>
<A ID="pgfId-969428"></A>
	 */</code>
<code>
<A ID="pgfId-969429"></A>
	c.t++;</code>
<code>
<A ID="pgfId-969430"></A>
	s.rootdisp += PHI;</code>
<code>
<A ID="pgfId-969431"></A>
</code>
<code>
<A ID="pgfId-969432"></A>
	/*</code>
<code>
<A ID="pgfId-969433"></A>
	 * Implement the phase and frequency adjustments. The gain</code>
<code>
<A ID="pgfId-969434"></A>
	 * factor (denominator) is not allowed to increase beyond the</code>
<code>
<A ID="pgfId-969435"></A>
	 * Allan intercept. It doesn’t make sense to average phase noise</code>
<code>
<A ID="pgfId-969436"></A>
	 * beyond this point and it helps to damp residual offset at the</code>
<code>
<A ID="pgfId-969437"></A>
	 * longer poll intervals.</code>
<code>
<A ID="pgfId-969438"></A>
	 */</code>
<code>
<A ID="pgfId-969439"></A>
	dtemp = c.offset / (PLL * min(LOG2D(s.poll), ALLAN));</code>
<code>
<A ID="pgfId-969440"></A>
	c.offset -= dtemp;</code>
<code>
<A ID="pgfId-969441"></A>
</code>
<code>
<A ID="pgfId-969442"></A>
	/*</code>
<code>
<A ID="pgfId-969443"></A>
	 * This is the kernel adjust time function, usually implemented</code>
<code>
<A ID="pgfId-969444"></A>
	 * by the Unix adjtime() system call.</code>
<code>
<A ID="pgfId-969445"></A>
	 */</code>
<code>
<A ID="pgfId-969446"></A>
	adjust_time(c.freq + dtemp);</code>
<code>
<A ID="pgfId-969447"></A>
</code>
<code>
<A ID="pgfId-969448"></A>
	/*</code>
<code>
<A ID="pgfId-969449"></A>
	 * Peer timer. Call the poll() routine when the poll timer</code>
<code>
<A ID="pgfId-969450"></A>
	 * expires.</code>
<code>
<A ID="pgfId-969451"></A>
	 */</code>
<code>
<A ID="pgfId-969452"></A>
	while (/* all associations */ 0) {</code>
<code>
<A ID="pgfId-969453"></A>
		struct p *p;	/* dummy peer structure pointer */</code>
<code>
<A ID="pgfId-969454"></A>
</code>
<code>
<A ID="pgfId-969455"></A>
		if (c.t &gt;= p-&gt;next)</code>
<code>
<A ID="pgfId-969456"></A>
			poll(p);</code>
<code>
<A ID="pgfId-969457"></A>
	}</code>
<code>
<A ID="pgfId-969458"></A>
</code>
<code>
<A ID="pgfId-969459"></A>
	/*</code>
<code>
<A ID="pgfId-969460"></A>
	 * Once per hour write the clock frequency to a file</code>
<code>
<A ID="pgfId-969461"></A>
	 */</code>
<code>
<A ID="pgfId-969462"></A>
	if (c.t % 3600 == 3599)</code>
<code>
<A ID="pgfId-969463"></A>
		/* write c.freq to file */ 0;</code>
<code>
<A ID="pgfId-969464"></A>
}</code>
<code>
<A ID="pgfId-969465"></A>
</code>
<Heading2>
<A ID="pgfId-969130"></A>
<A ID="33659"></A>
Poll Process</Heading2>
<code>
<A ID="pgfId-968222"></A>
#include “ntp4.h”</code>
<code>
<A ID="pgfId-968223"></A>
</code>
<code>
<A ID="pgfId-968224"></A>
/*</code>
<code>
<A ID="pgfId-968225"></A>
 * Constants</code>
<code>
<A ID="pgfId-968226"></A>
 */</code>
<code>
<A ID="pgfId-968227"></A>
#define	UNREACH		12	/* unreach counter threshold */</code>
<code>
<A ID="pgfId-968228"></A>
#define	BCOUNT		8	/* packets in a burst */</code>
<code>
<A ID="pgfId-968229"></A>
#define	BTIME		2	/* burst interval (s) */</code>
<code>
<A ID="pgfId-968230"></A>
</code>
<Heading3>
<A ID="pgfId-968921"></A>
<A ID="74485"></A>
poll()</Heading3>
<code>
<A ID="pgfId-968231"></A>
/*</code>
<code>
<A ID="pgfId-968232"></A>
 * poll() - determine when to send a packet for association p-&gt;</code>
<code>
<A ID="pgfId-968233"></A>
 */</code>
<code>
<A ID="pgfId-968234"></A>
void</code>
<code>
<A ID="pgfId-968235"></A>
poll(</code>
<code>
<A ID="pgfId-968236"></A>
	struct p *p		/* peer structure pointer */</code>
<code>
<A ID="pgfId-968237"></A>
	)</code>
<code>
<A ID="pgfId-968238"></A>
{</code>
<code>
<A ID="pgfId-968239"></A>
	int	hpoll;</code>
<code>
<A ID="pgfId-968240"></A>
	int	oreach;</code>
<code>
<A ID="pgfId-968241"></A>
</code>
<code>
<A ID="pgfId-968242"></A>
	/*</code>
<code>
<A ID="pgfId-968243"></A>
	 * This routine is called when the current time c.t catches up</code>
<code>
<A ID="pgfId-968244"></A>
	 * to the next poll time p-&gt;next. The value p-&gt;last is</code>
<code>
<A ID="pgfId-968245"></A>
	 * the last time this routine was executed. The poll_update()</code>
<code>
<A ID="pgfId-968246"></A>
	 * routine determines the next execution time p-&gt;next.</code>
<code>
<A ID="pgfId-968247"></A>
	 *</code>
<code>
<A ID="pgfId-968248"></A>
	 * If broadcasting, just do it, but only if we are synchronized.</code>
<code>
<A ID="pgfId-968249"></A>
	 */</code>
<code>
<A ID="pgfId-968250"></A>
	hpoll = p-&gt;hpoll;</code>
<code>
<A ID="pgfId-968251"></A>
	if (p-&gt;mode == M_BCST) {</code>
<code>
<A ID="pgfId-968252"></A>
		p-&gt;last = c.t;</code>
<code>
<A ID="pgfId-968253"></A>
		if (s.p != NULL)</code>
<code>
<A ID="pgfId-968254"></A>
			peer_xmit(p);</code>
<code>
<A ID="pgfId-968255"></A>
		poll_update(p, hpoll);</code>
<code>
<A ID="pgfId-968256"></A>
		return;</code>
<code>
<A ID="pgfId-968257"></A>
	}</code>
<code>
<A ID="pgfId-968258"></A>
	if (p-&gt;burst == 0) {</code>
<code>
<A ID="pgfId-968259"></A>
</code>
<code>
<A ID="pgfId-968260"></A>
		/*</code>
<code>
<A ID="pgfId-968261"></A>
		 * We are not in a burst. Shift the reachability</code>
<code>
<A ID="pgfId-968262"></A>
		 * register to the left. Hopefully, some time before the</code>
<code>
<A ID="pgfId-968263"></A>
		 * next poll a packet will arrive and set the rightmost</code>
<code>
<A ID="pgfId-968264"></A>
		 * bit.</code>
<code>
<A ID="pgfId-968265"></A>
		 */</code>
<code>
<A ID="pgfId-968266"></A>
		p-&gt;last = c.t;</code>
<code>
<A ID="pgfId-968267"></A>
		oreach = p-&gt;reach;</code>
<code>
<A ID="pgfId-968268"></A>
		p-&gt;reach &lt;&lt; 1;</code>
<code>
<A ID="pgfId-968269"></A>
		if (!p-&gt;reach) {</code>
<code>
<A ID="pgfId-968270"></A>
</code>
<code>
<A ID="pgfId-968271"></A>
			/*</code>
<code>
<A ID="pgfId-968272"></A>
			 * The server is unreachable, so bump the</code>
<code>
<A ID="pgfId-968273"></A>
			 * unreach counter. If the unreach threshold has</code>
<code>
<A ID="pgfId-968274"></A>
			 * been reached, double the poll interval to</code>
<code>
<A ID="pgfId-968275"></A>
			 * minimize wasted network traffic.</code>
<code>
<A ID="pgfId-968276"></A>
			 */</code>
<code>
<A ID="pgfId-968277"></A>
			if (p-&gt;flags &amp; P_IBURST &amp;&amp; p-&gt;unreach == 0) {</code>
<code>
<A ID="pgfId-968278"></A>
				p-&gt;burst = BCOUNT;</code>
<code>
<A ID="pgfId-968279"></A>
			} else if (p-&gt;unreach &lt; UNREACH)</code>
<code>
<A ID="pgfId-968280"></A>
				p-&gt;unreach++;</code>
<code>
<A ID="pgfId-968281"></A>
			else</code>
<code>
<A ID="pgfId-968282"></A>
				hpoll++;</code>
<code>
<A ID="pgfId-968283"></A>
			p-&gt;unreach++;</code>
<code>
<A ID="pgfId-968284"></A>
		} else {</code>
<code>
<A ID="pgfId-968285"></A>
</code>
<code>
<A ID="pgfId-968286"></A>
			/*</code>
<code>
<A ID="pgfId-968287"></A>
			 * The server is reachable. However, if has not</code>
<code>
<A ID="pgfId-968288"></A>
			 * been heard for three consecutive poll</code>
<code>
<A ID="pgfId-968289"></A>
			 * intervals, stuff the clock register to</code>
<code>
<A ID="pgfId-968290"></A>
			 * increase the peer dispersion. This makes old</code>
<code>
<A ID="pgfId-968291"></A>
			 * servers less desirable and eventually boots</code>
<code>
<A ID="pgfId-968292"></A>
			 * them off the island.</code>
<code>
<A ID="pgfId-968293"></A>
			 */</code>
<code>
<A ID="pgfId-968294"></A>
			p-&gt;unreach = 0;</code>
<code>
<A ID="pgfId-968295"></A>
			if (!(p-&gt;reach &amp; 0x7))</code>
<code>
<A ID="pgfId-968296"></A>
				clock_filter(p, 0, 0, MAXDISP);</code>
<code>
<A ID="pgfId-968297"></A>
			hpoll = s.poll;</code>
<code>
<A ID="pgfId-968298"></A>
			if (p-&gt;flags &amp; P_BURST &amp;&amp; accept(p))</code>
<code>
<A ID="pgfId-968299"></A>
				p-&gt;burst = BCOUNT;</code>
<code>
<A ID="pgfId-968300"></A>
		}</code>
<code>
<A ID="pgfId-968301"></A>
	} else {</code>
<code>
<A ID="pgfId-968302"></A>
</code>
<code>
<A ID="pgfId-968303"></A>
		/*</code>
<code>
<A ID="pgfId-968304"></A>
		 * If in a burst, count it down. When the reply comes</code>
<code>
<A ID="pgfId-968305"></A>
		 * back the clock_filter() routine will call</code>
<code>
<A ID="pgfId-968306"></A>
		 * clock_select() to process the results of the burst.</code>
<code>
<A ID="pgfId-968307"></A>
		 */</code>
<code>
<A ID="pgfId-968308"></A>
		p-&gt;burst--;</code>
<code>
<A ID="pgfId-968309"></A>
	}</code>
<code>
<A ID="pgfId-968310"></A>
</code>
<code>
<A ID="pgfId-968311"></A>
	/*</code>
<code>
<A ID="pgfId-968312"></A>
	 * Do not transmit if in broadcast client mode. </code>
<code>
<A ID="pgfId-968313"></A>
	 */</code>
<code>
<A ID="pgfId-968314"></A>
	if (p-&gt;mode != M_BCLN)</code>
<code>
<A ID="pgfId-968315"></A>
		peer_xmit(p);</code>
<code>
<A ID="pgfId-968316"></A>
	poll_update(p, hpoll);</code>
<code>
<A ID="pgfId-968317"></A>
}</code>
<code>
<A ID="pgfId-968318"></A>
</code>
<Heading3>
<A ID="pgfId-968319"></A>
<A ID="32347"></A>
poll_update()</Heading3>
<code>
<A ID="pgfId-968320"></A>
/*</code>
<code>
<A ID="pgfId-968321"></A>
 * poll_update() - update the poll interval for association p</code>
<code>
<A ID="pgfId-968322"></A>
 *</code>
<code>
<A ID="pgfId-968323"></A>
 * Note: This routine is called by both the packet() and poll() routine.</code>
<code>
<A ID="pgfId-968324"></A>
 * Since the packet() routine is executed when a network packet arrives</code>
<code>
<A ID="pgfId-968325"></A>
 * and the poll() routine is executed as the result of timeout, a</code>
<code>
<A ID="pgfId-968326"></A>
 * potential race can occur, possibly causing an incorrect interval for</code>
<code>
<A ID="pgfId-968327"></A>
 * the next poll. This is considered so unlikely as to be negligible.</code>
<code>
<A ID="pgfId-968328"></A>
 */</code>
<code>
<A ID="pgfId-968329"></A>
void</code>
<code>
<A ID="pgfId-968330"></A>
poll_update(</code>
<code>
<A ID="pgfId-968331"></A>
	struct p *p,		/* peer structure pointer */</code>
<code>
<A ID="pgfId-968332"></A>
	int	hpoll		/* poll interval (log2 s) */</code>
<code>
<A ID="pgfId-968333"></A>
	)</code>
<code>
<A ID="pgfId-968334"></A>
{</code>
<code>
<A ID="pgfId-970255"></A>
	int	poll;</code>
<code>
<A ID="pgfId-970235"></A>
</code>
<code>
<A ID="pgfId-968335"></A>
	/*</code>
<code>
<A ID="pgfId-968336"></A>
	 * This routine is called by both the poll() and packet()</code>
<code>
<A ID="pgfId-968337"></A>
	 * routines to determine the next poll time. If within a burst</code>
<code>
<A ID="pgfId-968338"></A>
	 * the poll interval is two seconds. Otherwise, it is the</code>
<code>
<A ID="pgfId-968339"></A>
	 * minimum of the host poll interval and peer poll interval, but</code>
<code>
<A ID="pgfId-968340"></A>
	 * not greater than MAXPOLL and not less than MINPOLL. The</code>
<code>
<A ID="pgfId-968341"></A>
	 * design insures that a longer interval can be preempted by a</code>
<code>
<A ID="pgfId-968342"></A>
	 * shorter one if required for rapid response.</code>
<code>
<A ID="pgfId-968343"></A>
	 */</code>
<code>
<A ID="pgfId-970325"></A>
	p-&gt;hpoll = min(MAXPOLL, max(MINPOLL, hpoll));</code>
<code>
<A ID="pgfId-970273"></A>
	if (p-&gt;burst != 0) {</code>
<code>
<A ID="pgfId-970330"></A>
		if(c.t != p-&gt;next)</code>
<code>
<A ID="pgfId-970331"></A>
			return;</code>
<code>
<A ID="pgfId-970332"></A>
</code>
<code>
<A ID="pgfId-970274"></A>
		p-&gt;next += BTIME;</code>
<code>
<A ID="pgfId-970277"></A>
	} else {</code>
<code>
<A ID="pgfId-970250"></A>
		poll = min(p-&gt;hpoll, max(MINPOLL, ppoll));</code>
<code>
<A ID="pgfId-968351"></A>
	}</code>
<code>
<A ID="pgfId-968352"></A>
		/*</code>
<code>
<A ID="pgfId-968353"></A>
		 * While not shown here, the reference implementation</code>
<code>
<A ID="pgfId-968354"></A>
		 * randonizes the poll interval by a small factor.</code>
<code>
<A ID="pgfId-968355"></A>
		 */</code>
<code>
<A ID="pgfId-968356"></A>
		p-&gt;next = p-&gt;last + (1 &lt;&lt; poll);</code>
<code>
<A ID="pgfId-968358"></A>
	}</code>
<code>
<A ID="pgfId-968359"></A>
</code>
<code>
<A ID="pgfId-968360"></A>
	/*</code>
<code>
<A ID="pgfId-968361"></A>
	 * It might happen that the due time has already passed. If so,</code>
<code>
<A ID="pgfId-968362"></A>
	 * make it one second in the future.</code>
<code>
<A ID="pgfId-968363"></A>
	 */</code>
<code>
<A ID="pgfId-968364"></A>
	if (p-&gt;next &lt;= c.t)</code>
<code>
<A ID="pgfId-968365"></A>
		p-&gt;next = c.t + 1;</code>
<code>
<A ID="pgfId-968366"></A>
}</code>
<code>
<A ID="pgfId-968367"></A>
</code>
<Heading3>
<A ID="pgfId-968368"></A>
<A ID="69143"></A>
transmit()</Heading3>
<code>
<A ID="pgfId-968369"></A>
/*</code>
<code>
<A ID="pgfId-968370"></A>
 * transmit() - transmit a packet for association p</code>
<code>
<A ID="pgfId-968371"></A>
 */</code>
<code>
<A ID="pgfId-968372"></A>
void</code>
<code>
<A ID="pgfId-968373"></A>
peer_xmit(</code>
<code>
<A ID="pgfId-968374"></A>
	struct p *p		/* peer structure pointer */</code>
<code>
<A ID="pgfId-968375"></A>
	)</code>
<code>
<A ID="pgfId-968376"></A>
{</code>
<code>
<A ID="pgfId-968377"></A>
	struct x x;		/* transmit packet */</code>
<code>
<A ID="pgfId-968378"></A>
</code>
<code>
<A ID="pgfId-968379"></A>
	/*</code>
<code>
<A ID="pgfId-968380"></A>
	 * Initialize header and transmit timestamp</code>
<code>
<A ID="pgfId-968381"></A>
	 */</code>
<code>
<A ID="pgfId-968382"></A>
	x.srcaddr = p-&gt;dstaddr;</code>
<code>
<A ID="pgfId-968383"></A>
	x.dstaddr = p-&gt;srcaddr;</code>
<code>
<A ID="pgfId-968384"></A>
	x.leap = s.leap;</code>
<code>
<A ID="pgfId-968385"></A>
	x.version = VERSION;</code>
<code>
<A ID="pgfId-968386"></A>
	x.mode = p-&gt;mode;</code>
<code>
<A ID="pgfId-968387"></A>
	if (s.stratum == MAXSTRAT)</code>
<code>
<A ID="pgfId-968388"></A>
		x.stratum = 0;</code>
<code>
<A ID="pgfId-968389"></A>
	else</code>
<code>
<A ID="pgfId-968390"></A>
		x.stratum = s.stratum;</code>
<code>
<A ID="pgfId-968391"></A>
	x.poll = p-&gt;hpoll;</code>
<code>
<A ID="pgfId-968392"></A>
	x.precision = s.precision;</code>
<code>
<A ID="pgfId-968393"></A>
	x.rootdelay = D2FP(s.rootdelay);</code>
<code>
<A ID="pgfId-968394"></A>
	x.rootdisp = D2FP(s.rootdisp);</code>
<code>
<A ID="pgfId-968395"></A>
	x.refid = s.refid;</code>
<code>
<A ID="pgfId-968396"></A>
	x.reftime = s.reftime;</code>
<code>
<A ID="pgfId-968397"></A>
	x.org = p-&gt;org;</code>
<code>
<A ID="pgfId-968398"></A>
	x.rec = p-&gt;rec;</code>
<code>
<A ID="pgfId-968399"></A>
	x.xmt = get_time();</code>
<code>
<A ID="pgfId-968400"></A>
	p-&gt;xmt = x.xmt;</code>
<code>
<A ID="pgfId-968401"></A>
</code>
<code>
<A ID="pgfId-968402"></A>
	/*</code>
<code>
<A ID="pgfId-968403"></A>
	 * If the key ID is nonzero, send a valid MAC using the key ID</code>
<code>
<A ID="pgfId-968404"></A>
	 * of the association and the key in the local key cache. If</code>
<code>
<A ID="pgfId-968405"></A>
	 * something breaks, like a missing trusted key, don’t send the</code>
<code>
<A ID="pgfId-968406"></A>
	 * packet; just reset the association and stop until the problem</code>
<code>
<A ID="pgfId-968407"></A>
	 * is fixed.</code>
<code>
<A ID="pgfId-968408"></A>
	 */</code>
<code>
<A ID="pgfId-968409"></A>
	if (p-&gt;keyid)</code>
<code>
<A ID="pgfId-968410"></A>
		if (/* p-&gt;keyid invalid */ 0) {</code>
<code>
<A ID="pgfId-968411"></A>
			clear(p, X_NKEY);</code>
<code>
<A ID="pgfId-968412"></A>
			return;</code>
<code>
<A ID="pgfId-968413"></A>
		}</code>
<code>
<A ID="pgfId-968414"></A>
		x.digest = md5(p-&gt;keyid);</code>
<code>
<A ID="pgfId-968415"></A>
	xmit_packet(&amp;x);</code>
<code>
<A ID="pgfId-968416"></A>
}</code>
<code>
<A ID="pgfId-968417"></A>
</code>
<FOOTNOTES>
<FOOTNOTE>
<Footnote>
<A ID="pgfId-1016942"></A>
Parens following a name indicate reference to a function rather than a simple variable.</Footnote>
</FOOTNOTE>
<FOOTNOTE>
<Footnote>
<A ID="pgfId-1019028"></A>
Strictly speaking, UTC did not exist prior to 1 January 1972, but it is convenient to assume it has existed for all eternity, even if all knowledge of historic leap seconds has been lost.</Footnote>
</FOOTNOTE>
<FOOTNOTE>
<Footnote>
<A ID="pgfId-1007346"></A>
The reference implementation strikes a timestamp after running the MD5 algorithm and adds the difference between it and the transmit timestamp to the next transmit timestamp.</Footnote>
</FOOTNOTE>
</FOOTNOTES>
</XML>
