Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

339 lines
15 KiB

  1. //--------------------------------------------------------------------
  2. // NtpBase - header
  3. // Copyright (C) Microsoft Corporation, 1999
  4. //
  5. // Created by: Louis Thomas (louisth), 4-16-99
  6. //
  7. // The basic message structure, definitions, and helper functions
  8. //
  9. #ifndef NTPBASE_H
  10. #define NTPBASE_H
  11. //--------------------------------------------------------------------
  12. // Time formats
  13. // a clock reading, little-endian, in (10^-7)s
  14. struct NtTimeEpoch {
  15. unsigned __int64 qw;
  16. void dump(void);
  17. };
  18. // a signed time offset, little-endian, in (10^-7)s
  19. struct NtTimeOffset {
  20. signed __int64 qw;
  21. void dump(void);
  22. };
  23. // a length of time, little-endian, in (10^-7)s
  24. struct NtTimePeriod {
  25. unsigned __int64 qw;
  26. void dump(void);
  27. };
  28. // a clock reading, big-endian, in (2^-32)s
  29. struct NtpTimeEpoch {
  30. unsigned __int64 qw;
  31. };
  32. // a signed time offset, big-endian, in (2^-16)s
  33. struct NtpTimeOffset {
  34. signed __int32 dw;
  35. };
  36. // a length of time, big-endian, in (2^-16)s
  37. struct NtpTimePeriod {
  38. unsigned __int32 dw;
  39. };
  40. extern const NtTimeEpoch gc_teNtpZero; // convenient 'zero'
  41. extern const NtpTimeEpoch gc_teZero; // convenient 'zero'
  42. extern const NtTimePeriod gc_tpZero; // convenient 'zero'
  43. extern const NtTimeOffset gc_toZero; // convenient 'zero'
  44. //--------------------------------------------------------------------
  45. // helpful conversion functions
  46. NtTimeEpoch NtTimeEpochFromNtpTimeEpoch(NtpTimeEpoch te);
  47. NtpTimeEpoch NtpTimeEpochFromNtTimeEpoch(NtTimeEpoch te);
  48. NtTimePeriod NtTimePeriodFromNtpTimePeriod(NtpTimePeriod tp);
  49. NtpTimePeriod NtpTimePeriodFromNtTimePeriod(NtTimePeriod tp);
  50. NtTimeOffset NtTimeOffsetFromNtpTimeOffset(NtpTimeOffset to);
  51. NtpTimeOffset NtpTimeOffsetFromNtTimeOffset(NtTimeOffset to);
  52. //--------------------------------------------------------------------
  53. // Math operators
  54. static inline NtTimeOffset operator -(const NtTimeOffset toRight) {
  55. NtTimeOffset toRet;
  56. toRet.qw=-toRight.qw;
  57. return toRet;
  58. }
  59. static inline NtTimeOffset operator -(const NtTimeEpoch teLeft, const NtTimeEpoch teRight) {
  60. NtTimeOffset toRet;
  61. toRet.qw=teLeft.qw-teRight.qw;
  62. return toRet;
  63. }
  64. static inline NtTimeOffset operator -(const NtTimeOffset toLeft, const NtTimeOffset toRight) {
  65. NtTimeOffset toRet;
  66. toRet.qw=toLeft.qw-toRight.qw;
  67. return toRet;
  68. }
  69. static inline NtTimeOffset operator +(const NtTimeOffset toLeft, const NtTimeOffset toRight) {
  70. NtTimeOffset toRet;
  71. toRet.qw=toLeft.qw+toRight.qw;
  72. return toRet;
  73. }
  74. static inline NtTimeOffset & operator /=(NtTimeOffset &toLeft, const int nDiv) {
  75. toLeft.qw/=nDiv;
  76. return toLeft;
  77. }
  78. static inline NtTimeOffset & operator -=(NtTimeOffset &toLeft, const NtTimeOffset toRight) {
  79. toLeft.qw-=toRight.qw;
  80. return toLeft;
  81. }
  82. static inline NtTimeOffset & operator +=(NtTimeOffset &toLeft, const NtTimeOffset toRight) {
  83. toLeft.qw-=toRight.qw;
  84. return toLeft;
  85. }
  86. static inline NtTimeEpoch operator +(const NtTimeEpoch teLeft, const NtTimePeriod tpRight) {
  87. NtTimeEpoch teRet;
  88. teRet.qw=teLeft.qw+tpRight.qw;
  89. return teRet;
  90. }
  91. static inline NtTimePeriod operator *(const NtTimePeriod tpLeft, const unsigned __int64 qwMult) {
  92. NtTimePeriod tpRet;
  93. tpRet.qw=tpLeft.qw*qwMult;
  94. return tpRet;
  95. }
  96. static inline NtTimePeriod & operator *=(NtTimePeriod &tpLeft, const unsigned __int64 qwMult) {
  97. tpLeft.qw*=qwMult;
  98. return tpLeft;
  99. }
  100. static inline NtTimePeriod operator /(const NtTimePeriod tpLeft, const int nDiv) {
  101. NtTimePeriod tpRet;
  102. tpRet.qw=tpLeft.qw/nDiv;
  103. return tpRet;
  104. }
  105. static inline NtTimePeriod & operator +=(NtTimePeriod &tpLeft, const NtTimePeriod tpRight) {
  106. tpLeft.qw+=tpRight.qw;
  107. return tpLeft;
  108. }
  109. static inline NtTimePeriod operator +(const NtTimePeriod tpLeft, const NtTimePeriod tpRight) {
  110. NtTimePeriod tpRet;
  111. tpRet.qw=tpLeft.qw+tpRight.qw;
  112. return tpRet;
  113. }
  114. static inline NtTimePeriod & operator -=(NtTimePeriod &tpLeft, const NtTimePeriod tpRight) {
  115. tpLeft.qw-=tpRight.qw;
  116. return tpLeft;
  117. }
  118. static inline NtTimePeriod operator -(const NtTimePeriod tpLeft, const NtTimePeriod tpRight) {
  119. NtTimePeriod tpRet;
  120. tpRet.qw=tpLeft.qw-tpRight.qw;
  121. return tpRet;
  122. }
  123. static inline bool operator <(const NtTimeEpoch teLeft, const NtTimeEpoch teRight) {
  124. return teLeft.qw<teRight.qw;
  125. }
  126. static inline bool operator <=(const NtTimeEpoch teLeft, const NtTimeEpoch teRight) {
  127. return teLeft.qw<=teRight.qw;
  128. }
  129. static inline bool operator >(const NtTimeEpoch teLeft, const NtTimeEpoch teRight) {
  130. return teLeft.qw>teRight.qw;
  131. }
  132. static inline bool operator >=(const NtTimeEpoch teLeft, const NtTimeEpoch teRight) {
  133. return teLeft.qw>=teRight.qw;
  134. }
  135. static inline bool operator ==(const NtTimeEpoch teLeft, const NtTimeEpoch teRight) {
  136. return teLeft.qw==teRight.qw;
  137. }
  138. static inline bool operator !=(const NtTimeEpoch teLeft, const NtTimeEpoch teRight) {
  139. return teLeft.qw!=teRight.qw;
  140. }
  141. static inline bool operator <(const NtTimePeriod tpLeft, const NtTimePeriod tpRight) {
  142. return tpLeft.qw<tpRight.qw;
  143. }
  144. static inline bool operator <=(const NtTimePeriod tpLeft, const NtTimePeriod tpRight) {
  145. return tpLeft.qw<=tpRight.qw;
  146. }
  147. static inline bool operator >(const NtTimePeriod tpLeft, const NtTimePeriod tpRight) {
  148. return tpLeft.qw>tpRight.qw;
  149. }
  150. static inline bool operator >=(const NtTimePeriod tpLeft, const NtTimePeriod tpRight) {
  151. return tpLeft.qw>=tpRight.qw;
  152. }
  153. static inline bool operator ==(const NtTimePeriod tpLeft, const NtTimePeriod tpRight) {
  154. return tpLeft.qw==tpRight.qw;
  155. }
  156. static inline bool operator !=(const NtTimePeriod tpLeft, const NtTimePeriod tpRight) {
  157. return tpLeft.qw!=tpRight.qw;
  158. }
  159. static inline bool operator <(const NtTimeOffset toLeft, const NtTimeOffset toRight) {
  160. return toLeft.qw<toRight.qw;
  161. }
  162. static inline bool operator <=(const NtTimeOffset toLeft, const NtTimeOffset toRight) {
  163. return toLeft.qw<=toRight.qw;
  164. }
  165. static inline bool operator >(const NtTimeOffset toLeft, const NtTimeOffset toRight) {
  166. return toLeft.qw>toRight.qw;
  167. }
  168. static inline bool operator >=(const NtTimeOffset toLeft, const NtTimeOffset toRight) {
  169. return toLeft.qw>=toRight.qw;
  170. }
  171. static inline bool operator ==(const NtTimeOffset toLeft, const NtTimeOffset toRight) {
  172. return toLeft.qw==toRight.qw;
  173. }
  174. static inline bool operator !=(const NtTimeOffset toLeft, const NtTimeOffset toRight) {
  175. return toLeft.qw!=toRight.qw;
  176. }
  177. static inline bool operator ==(const NtpTimeEpoch teLeft, const NtpTimeEpoch teRight) {
  178. return teLeft.qw==teRight.qw;
  179. }
  180. static inline bool operator !=(const NtpTimeEpoch teLeft, const NtpTimeEpoch teRight) {
  181. return teLeft.qw!=teRight.qw;
  182. }
  183. static inline NtTimePeriod abs(const NtTimeOffset to) {
  184. NtTimePeriod tpRet;
  185. tpRet.qw=((to.qw<0)?((unsigned __int64)(-to.qw)):((unsigned __int64)(to.qw)));
  186. return tpRet;
  187. }
  188. //--------------------------------------------------------------------
  189. static inline NtTimePeriod minimum(NtTimePeriod tpLeft, NtTimePeriod tpRight) {
  190. return ((tpLeft<tpRight)?tpLeft:tpRight);
  191. }
  192. //--------------------------------------------------------------------
  193. // identifies the particular reference source
  194. union NtpRefId {
  195. unsigned __int8 rgnIpAddr[4]; // an IP address
  196. unsigned __int8 rgnName[4]; // 4 ascii characters
  197. unsigned __int32 nTransmitTimestamp; // the low order 32 bits of the latest transmit timestamp of the reference source
  198. unsigned __int32 value; // for copying purposes
  199. };
  200. //--------------------------------------------------------------------
  201. // The format of a standard NTP packet
  202. struct NtpPacket {
  203. struct {
  204. unsigned __int8 nMode:3; // the mode. Valid range: 0-7
  205. unsigned __int8 nVersionNumber:3; // the NTP/SNTP version number. Valid range: 1-4
  206. unsigned __int8 nLeapIndicator:2; // a warning of an impending leap second to be inserted/deleted in the last minute of the current day
  207. };
  208. unsigned __int8 nStratum; // the stratum level of the local clock. Valid Range: 0-15
  209. signed __int8 nPollInterval; // the maximum interval between successive messages, in s, log base 2. Valid range:4(16s)-14(16284s)
  210. signed __int8 nPrecision; // the precision of the local clock, in s, log base 2
  211. NtpTimeOffset toRootDelay; // the total roundtrip delay to the primary reference source, in (2^-16)s
  212. NtpTimePeriod tpRootDispersion; // the nominal error relative to the primary reference, in (2^-16)s
  213. NtpRefId refid; // identifies the particular reference source
  214. NtpTimeEpoch teReferenceTimestamp; // the time at which the local clock was last set or corrected, in (2^-32)s
  215. NtpTimeEpoch teOriginateTimestamp; // the time at which the request departed the client for the server, in (2^-32)s
  216. NtpTimeEpoch teReceiveTimestamp; // the time at which the request arrived at the server, in (2^-32)s
  217. NtpTimeEpoch teTransmitTimestamp; // the time at which the reply departed the server for the client, in (2^-32)s
  218. };
  219. #define SizeOfNtpPacket 48
  220. //--------------------------------------------------------------------
  221. // The format of an authenticated NTP packet
  222. struct AuthenticatedNtpPacket {
  223. struct {
  224. unsigned __int8 nMode:3; // the mode. Valid range: 0-7
  225. unsigned __int8 nVersionNumber:3; // the NTP/SNTP version number. Valid range: 1-4
  226. unsigned __int8 nLeapIndicator:2; // a warning of an impending leap second to be inserted/deleted in the last minute of the current day
  227. };
  228. unsigned __int8 nStratum; // the stratum level of the local clock. Valid Range: 0-15
  229. signed __int8 nPollInterval; // the maximum interval between successive messages, in s, log base 2. Valid range:4(16s)-14(16284s)
  230. signed __int8 nPrecision; // the precision of the local clock, in s, log base 2
  231. NtpTimeOffset toRootDelay; // the total roundtrip delay to the primary reference source, in (2^-16)s
  232. NtpTimePeriod tpRootDispersion; // the nominal error relative to the primary reference, in (2^-16)s
  233. NtpRefId refid; // identifies the particular reference source
  234. NtpTimeEpoch teReferenceTimestamp; // the time at which the local clock was last set or corrected, in (2^-32)s
  235. NtpTimeEpoch teOriginateTimestamp; // the time at which the request departed the client for the server, in (2^-32)s
  236. NtpTimeEpoch teReceiveTimestamp; // the time at which the request arrived at the server, in (2^-32)s
  237. NtpTimeEpoch teTransmitTimestamp; // the time at which the reply departed the server for the client, in (2^-32)s
  238. unsigned __int32 nKeyIdentifier; // implementation specific, for authentication
  239. unsigned __int8 rgnMessageDigest[16]; // implementation specific, for authentication
  240. };
  241. // We define this because of structure packing issues - our structure
  242. // contains qwords, but is not a multiple of 8 in size, so sizeof()
  243. // incorrectly reports the size. If we were to adjust the packing,
  244. // we might misalign the qwords. Interestingly, in the NTP spec,
  245. // the rgnMessageDigest is 12 bytes, so the packet is a multiple of 8.
  246. #define SizeOfNtAuthenticatedNtpPacket 68
  247. //--------------------------------------------------------------------
  248. // The allowed NTP modes
  249. enum NtpMode {
  250. e_Reserved=0,
  251. e_SymmetricActive=1,
  252. e_SymmetricPassive=2,
  253. e_Client=3,
  254. e_Server=4,
  255. e_Broadcast=5,
  256. e_Control=6,
  257. e_PrivateUse=7,
  258. };
  259. //--------------------------------------------------------------------
  260. // The allowed NTP modes
  261. enum NtpLeapIndicator {
  262. e_NoWarning=0,
  263. e_AddSecond=1,
  264. e_SubtractSecond=2,
  265. e_ClockNotSynchronized=3,
  266. };
  267. //--------------------------------------------------------------------
  268. // NTP constants
  269. struct NtpConst {
  270. static const unsigned int nVersionNumber; // 3 // the current NTP version number
  271. static const unsigned int nPort; // 123 // the port number assigned by the Internet Assigned Numbers Authority to NTP
  272. static const unsigned int nMaxStratum; // 15 // the maximum stratum value that can be encoded as a packet value, also interpreted as "infinity" or unreachable
  273. static const signed int nMaxPollInverval; // 10 // the maximum poll interval allowed by any peer, in s, log base 2 (10=1024s)
  274. static const signed int nMinPollInverval; // 6 // the minimum poll interval allowed by any peer, in s, log base 2 (6=64s)
  275. static const NtTimePeriod tpMaxClockAge; // 86400.0000000 // the maximum inverval a reference clock will be considered valid after its last update, in (10^-7)s
  276. static const NtTimePeriod tpMaxSkew; // 1.0000000 // the maximum offset error due to skew of the local clock over the interval determined by NTPCONST_MaxAge, in (10^-7)s
  277. static const NtTimePeriod tpMaxDispersion; // 16.0000000 // the maximum peer dispersion and the dispersion assumed for missing data, in (10^-7)s
  278. static const NtTimePeriod tpMinDispersion; // 0.0100000 // the minimum dispersion increment for each stratum level, in (10^-7)s
  279. static const NtTimePeriod tpMaxDistance; // 1.0000000 // the maximum synchronization distance for peers acceptible for synchronization, in (10^-7)s
  280. static const unsigned int nMinSelectClocks; // 1 // the minimum number of peers acceptable for synchronization
  281. static const unsigned int nMaxSelectClocks; // 10 // the maximum number of peers considered for selection
  282. static const DWORD dwLocalRefId; // LOCL // the reference identifier for the local clock
  283. static NtTimePeriod timesMaxSkewRate(NtTimePeriod tp) { // MaxSkewRate == phi == NTPCONST_MaxSkew / NTPCONST_MaxClockAge; in s per s (==11.5740740...PPM)
  284. NtTimePeriod tpRet;
  285. tpRet.qw=tp.qw/86400;
  286. return tpRet;
  287. }
  288. static void weightFilter(NtTimePeriod &tp) { tp.qw/=2; } // weight the filter dispersion during computation (x * 1/2)
  289. static void weightSelect(unsigned __int64 &tp) { tp*=3;tp/=4; } // weight the select dispersion during computation (x * 3/2)
  290. };
  291. struct NtpReachabilityReg {
  292. static const unsigned int nSize; // 8 // the size of the reachability register, in bits
  293. unsigned __int8 nReg;
  294. };
  295. //--------------------------------------------------------------------
  296. // helpful debug dump functions
  297. void DumpNtpPacket(NtpPacket * pnpIn, NtTimeEpoch teDestinationTimestamp);
  298. void DumpNtpTimeEpoch(NtpTimeEpoch te);
  299. void DumpNtTimeEpoch(NtTimeEpoch te);
  300. void DumpNtTimePeriod(NtTimePeriod tp);
  301. void DumpNtTimeOffset(NtTimeOffset to);
  302. inline void NtTimeEpoch::dump(void) { DumpNtTimeEpoch(*this); }
  303. inline void NtTimePeriod::dump(void) { DumpNtTimePeriod(*this); }
  304. inline void NtTimeOffset::dump(void) { DumpNtTimeOffset(*this); }
  305. NtTimeEpoch GetCurrentSystemNtTimeEpoch(void);
  306. #endif // NTPBASE_H