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.

437 lines
16 KiB

  1. //--------------------------------------------------------------------
  2. // NtpBase - implementation
  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. // (See notes about time formats at end of file)
  9. //--------------------------------------------------------------------
  10. // precompiled headers
  11. #include "pch.h"
  12. // local headers
  13. #include "NtpBase.h"
  14. #include "DebugWPrintf.h"
  15. // inlines
  16. #include "EndianSwap.inl"
  17. //--------------------------------------------------------------------
  18. // conversion constants
  19. #define NTPTIMEOFFSET (0x014F373BFDE04000)
  20. #define FIVETOTHESEVETH (0x001312D)
  21. //--------------------------------------------------------------------
  22. // global constants
  23. const unsigned int NtpConst::nVersionNumber=3;
  24. const unsigned int NtpConst::nPort=123;
  25. const unsigned int NtpConst::nMaxStratum=15;
  26. const signed int NtpConst::nMaxPollInverval=10;
  27. const signed int NtpConst::nMinPollInverval=4; //6
  28. const NtTimePeriod NtpConst::tpMaxClockAge={864000000000};
  29. const NtTimePeriod NtpConst::tpMaxSkew={10000000};
  30. const NtTimePeriod NtpConst::tpMaxDispersion={160000000};
  31. const NtTimePeriod NtpConst::tpMinDispersion={100000};
  32. const NtTimePeriod NtpConst::tpMaxDistance={10000000};
  33. const unsigned int NtpConst::nMinSelectClocks=1;
  34. const unsigned int NtpConst::nMaxSelectClocks=10;
  35. const DWORD NtpConst::dwLocalRefId=0x4C434F4C; // "LOCL"
  36. const unsigned int NtpReachabilityReg::nSize=8;
  37. const NtTimeEpoch gc_teNtpZero={NTPTIMEOFFSET}; // convenient 'zero'
  38. const NtpTimeEpoch gc_teZero={0}; // convenient 'zero'
  39. const NtTimePeriod gc_tpZero={0}; // convenient 'zero'
  40. const NtTimeOffset gc_toZero={0}; // convenient 'zero'
  41. //--------------------------------------------------------------------
  42. // convert from big-endian NTP-stye timestamp to little-endian NT-style timestamp
  43. NtTimeEpoch NtTimeEpochFromNtpTimeEpoch(NtpTimeEpoch te) {
  44. NtTimeEpoch teRet;
  45. //return (qwNtpTime*(10**7)/(2**32))+NTPTIMEOFFSET
  46. // ==>
  47. //return (qwNtpTime*( 5**7)/(2**25))+NTPTIMEOFFSET
  48. // ==>
  49. //return ((qwNTPtime*FIVETOTHESEVETH)>>25)+NTPTIMEOFFSET;
  50. // ==>
  51. // Note: 'After' division, we round (instead of truncate) the result for better precision
  52. unsigned __int64 qwNtpTime=EndianSwap(te.qw);
  53. unsigned __int64 qwTemp=((qwNtpTime&0x00000000FFFFFFFF)*FIVETOTHESEVETH)+0x0000000001000000; //rounding step: if 25th bit is set, round up;
  54. teRet.qw=(qwTemp>>25) + ((qwNtpTime&0xFFFFFFFF00000000)>>25)*FIVETOTHESEVETH + NTPTIMEOFFSET;
  55. return teRet;
  56. }
  57. //--------------------------------------------------------------------
  58. // convert from little-endian NT-style timestamp to big-endian NTP-stye timestamp
  59. NtpTimeEpoch NtpTimeEpochFromNtTimeEpoch(NtTimeEpoch te) {
  60. NtpTimeEpoch teRet;
  61. //return (qwNtTime-NTPTIMEOFFSET)*(2**32)/(10**7);
  62. // ==>
  63. //return (qwNtTime-NTPTIMEOFFSET)*(2**25)/(5**7);
  64. // ==>
  65. //return ((qwNtTime-NTPTIMEOFFSET)<<25)/FIVETOTHESEVETH);
  66. // ==>
  67. // Note: The high bit is lost (and assumed to be zero) but
  68. // it will not be set for another 29,000 years (around year 31587). No big loss.
  69. // Note: 'After' division, we truncate the result because the precision of NTP already excessive
  70. unsigned __int64 qwTemp=(te.qw-NTPTIMEOFFSET)<<1;
  71. unsigned __int64 qwHigh=qwTemp>>8;
  72. unsigned __int64 qwLow=(qwHigh%FIVETOTHESEVETH)<<32 | (qwTemp&0x00000000000000FF)<<24;
  73. teRet.qw=EndianSwap(((qwHigh/FIVETOTHESEVETH)<<32) | (qwLow/FIVETOTHESEVETH));
  74. return teRet;
  75. }
  76. //--------------------------------------------------------------------
  77. // convert from big-endian NTP-stye time interval to little-endian NT-style time interval
  78. NtTimePeriod NtTimePeriodFromNtpTimePeriod(NtpTimePeriod tp) {
  79. NtTimePeriod tpRet;
  80. unsigned __int64 qwNtpTime=tp.dw;
  81. qwNtpTime=EndianSwap(qwNtpTime<<16);
  82. unsigned __int64 qwTemp=((qwNtpTime&0x00000000FFFFFFFF)*FIVETOTHESEVETH)+0x0000000001000000; //rounding step: if 25th bit is set, round up
  83. tpRet.qw=(qwTemp>>25) + ((qwNtpTime&0xFFFFFFFF00000000)>>25)*FIVETOTHESEVETH;
  84. return tpRet;
  85. }
  86. //--------------------------------------------------------------------
  87. // convert from little-endian NT-style time interval to big-endian NTP-stye time interval
  88. NtpTimePeriod NtpTimePeriodFromNtTimePeriod(NtTimePeriod tp) {
  89. NtpTimePeriod tpRet;
  90. unsigned __int64 qwTemp=(tp.qw)<<1;
  91. unsigned __int64 qwHigh=qwTemp>>8;
  92. unsigned __int64 qwLow=(qwHigh%FIVETOTHESEVETH)<<32 | (qwTemp&0x00000000000000FF)<<24;
  93. qwTemp=EndianSwap(((qwHigh/FIVETOTHESEVETH)<<32) | (qwLow/FIVETOTHESEVETH));
  94. tpRet.dw=(unsigned __int32)(qwTemp>>16);
  95. return tpRet;
  96. }
  97. //--------------------------------------------------------------------
  98. // convert from big-endian NTP-stye delay to little-endian NT-style delay
  99. NtTimeOffset NtTimeOffsetFromNtpTimeOffset(NtpTimeOffset to) {
  100. NtTimeOffset toRet;
  101. if (to.dw&0x00000080) {
  102. to.dw=(signed __int32)EndianSwap((unsigned __int32)-(signed __int32)EndianSwap((unsigned __int32)to.dw));
  103. toRet.qw=-(signed __int64)(NtTimePeriodFromNtpTimePeriod(*(NtpTimePeriod*)&to).qw);
  104. } else {
  105. toRet.qw=(signed __int64)(NtTimePeriodFromNtpTimePeriod(*(NtpTimePeriod*)&to).qw);
  106. }
  107. return toRet;
  108. }
  109. //--------------------------------------------------------------------
  110. // convert from little-endian NT-style delay to big-endian NTP-stye delay
  111. NtpTimeOffset NtpTimeOffsetFromNtTimeOffset(NtTimeOffset to) {
  112. NtpTimeOffset toRet;
  113. if (to.qw<0) {
  114. to.qw=-to.qw;
  115. toRet.dw=(signed __int32)(NtpTimePeriodFromNtTimePeriod(*(NtTimePeriod*)&to).dw);
  116. toRet.dw=(signed __int32)EndianSwap((unsigned __int64)-(signed __int64)EndianSwap((unsigned __int32)toRet.dw));
  117. } else {
  118. toRet.dw=(signed __int32)(NtpTimePeriodFromNtTimePeriod(*(NtTimePeriod*)&to).dw);
  119. }
  120. return toRet;
  121. }
  122. //--------------------------------------------------------------------
  123. // Print out the contents of an NTP packet
  124. // If nDestinationTimestamp is zero, no round trip calculations will be done
  125. void DumpNtpPacket(NtpPacket * pnpIn, NtTimeEpoch teDestinationTimestamp) {
  126. DebugWPrintf0(L"/-- NTP Packet:");
  127. DebugWPrintf0(L"\n| LeapIndicator: ");
  128. if (0==pnpIn->nLeapIndicator) {
  129. DebugWPrintf0(L"0 - no warning");
  130. } else if (1==pnpIn->nLeapIndicator) {
  131. DebugWPrintf0(L"1 - last minute has 61 seconds");
  132. } else if (2==pnpIn->nLeapIndicator) {
  133. DebugWPrintf0(L"2 - last minute has 59 seconds");
  134. } else {
  135. DebugWPrintf0(L"3 - not synchronized");
  136. }
  137. DebugWPrintf1(L"; VersionNumber: %u", pnpIn->nVersionNumber);
  138. DebugWPrintf0(L"; Mode: ");
  139. if (0==pnpIn->nMode) {
  140. DebugWPrintf0(L"0 - Reserved");
  141. } else if (1==pnpIn->nMode) {
  142. DebugWPrintf0(L"1 - SymmetricActive");
  143. } else if (2==pnpIn->nMode) {
  144. DebugWPrintf0(L"2 - SymmetricPassive");
  145. } else if (3==pnpIn->nMode) {
  146. DebugWPrintf0(L"3 - Client");
  147. } else if (4==pnpIn->nMode) {
  148. DebugWPrintf0(L"4 - Server");
  149. } else if (5==pnpIn->nMode) {
  150. DebugWPrintf0(L"5 - Broadcast");
  151. } else if (6==pnpIn->nMode) {
  152. DebugWPrintf0(L"6 - Control");
  153. } else {
  154. DebugWPrintf0(L"7 - PrivateUse");
  155. }
  156. DebugWPrintf1(L"; LiVnMode: 0x%02X", ((BYTE*)pnpIn)[0]);
  157. DebugWPrintf1(L"\n| Stratum: %u - ", pnpIn->nStratum);
  158. if (0==pnpIn->nStratum) {
  159. DebugWPrintf0(L"unspecified or unavailable");
  160. } else if (1==pnpIn->nStratum) {
  161. DebugWPrintf0(L"primary reference (syncd by radio clock)");
  162. } else if (pnpIn->nStratum<16) {
  163. DebugWPrintf0(L"secondary reference (syncd by (S)NTP)");
  164. } else {
  165. DebugWPrintf0(L"reserved");
  166. }
  167. DebugWPrintf1(L"\n| Poll Interval: %d - ", pnpIn->nPollInterval);
  168. if (pnpIn->nPollInterval<4 || pnpIn->nPollInterval>14) {
  169. if (0==pnpIn->nPollInterval) {
  170. DebugWPrintf0(L"unspecified");
  171. } else {
  172. DebugWPrintf0(L"out of valid range");
  173. }
  174. } else {
  175. int nSec=1<<pnpIn->nPollInterval;
  176. DebugWPrintf1(L"%ds", nSec);
  177. }
  178. DebugWPrintf1(L"; Precision: %d - ", pnpIn->nPrecision);
  179. if (pnpIn->nPrecision>-2 || pnpIn->nPrecision<-31) {
  180. if (0==pnpIn->nPollInterval) {
  181. DebugWPrintf0(L"unspecified");
  182. } else {
  183. DebugWPrintf0(L"out of valid range");
  184. }
  185. } else {
  186. WCHAR * wszUnit=L"s";
  187. double dTickInterval=1.0/(1<<(-pnpIn->nPrecision));
  188. if (dTickInterval<1) {
  189. dTickInterval*=1000;
  190. wszUnit=L"ms";
  191. }
  192. if (dTickInterval<1) {
  193. dTickInterval*=1000;
  194. wszUnit=L"�s"; // shows up as �s on console
  195. }
  196. if (dTickInterval<1) {
  197. dTickInterval*=1000;
  198. wszUnit=L"ns";
  199. }
  200. DebugWPrintf2(L"%g%s per tick", dTickInterval, wszUnit);
  201. }
  202. DebugWPrintf0(L"\n| RootDelay: ");
  203. {
  204. DWORD dwTemp=EndianSwap((unsigned __int32)pnpIn->toRootDelay.dw);
  205. DebugWPrintf2(L"0x%04X.%04Xs", dwTemp>>16, dwTemp&0x0000FFFF);
  206. if (0==dwTemp) {
  207. DebugWPrintf0(L" - unspecified");
  208. } else {
  209. DebugWPrintf1(L" - %gs", ((double)((signed __int32)dwTemp))/0x00010000);
  210. }
  211. }
  212. DebugWPrintf0(L"; RootDispersion: ");
  213. {
  214. DWORD dwTemp=EndianSwap(pnpIn->tpRootDispersion.dw);
  215. DebugWPrintf2(L"0x%04X.%04Xs", dwTemp>>16, dwTemp&0x0000FFFF);
  216. if (0==dwTemp) {
  217. DebugWPrintf0(L" - unspecified");
  218. } else {
  219. DebugWPrintf1(L" - %gs", ((double)dwTemp)/0x00010000);
  220. }
  221. }
  222. DebugWPrintf0(L"\n| ReferenceClockIdentifier: ");
  223. {
  224. DWORD dwTemp=EndianSwap(pnpIn->refid.nTransmitTimestamp);
  225. DebugWPrintf1(L"0x%08X", dwTemp);
  226. if (0==dwTemp) {
  227. DebugWPrintf0(L" - unspecified");
  228. } else if (0==pnpIn->nStratum || 1==pnpIn->nStratum) {
  229. char szId[5];
  230. szId[0]=pnpIn->refid.rgnName[0];
  231. szId[1]=pnpIn->refid.rgnName[1];
  232. szId[2]=pnpIn->refid.rgnName[2];
  233. szId[3]=pnpIn->refid.rgnName[3];
  234. szId[4]='\0';
  235. DebugWPrintf1(L" - source name: \"%S\"", szId);
  236. } else if (pnpIn->nVersionNumber<4) {
  237. DebugWPrintf4(L" - source IP: %d.%d.%d.%d",
  238. pnpIn->refid.rgnIpAddr[0], pnpIn->refid.rgnIpAddr[1],
  239. pnpIn->refid.rgnIpAddr[2], pnpIn->refid.rgnIpAddr[3]);
  240. } else {
  241. DebugWPrintf1(L" - last reference timestamp fraction: %gs", ((double)dwTemp)/(4294967296.0));
  242. }
  243. }
  244. DebugWPrintf0(L"\n| ReferenceTimestamp: ");
  245. DumpNtpTimeEpoch(pnpIn->teReferenceTimestamp);
  246. DebugWPrintf0(L"\n| OriginateTimestamp: ");
  247. DumpNtpTimeEpoch(pnpIn->teOriginateTimestamp);
  248. DebugWPrintf0(L"\n| ReceiveTimestamp: ");
  249. DumpNtpTimeEpoch(pnpIn->teReceiveTimestamp);
  250. DebugWPrintf0(L"\n| TransmitTimestamp: ");
  251. DumpNtpTimeEpoch(pnpIn->teTransmitTimestamp);
  252. if (0!=teDestinationTimestamp.qw) {
  253. DebugWPrintf0(L"\n>-- Non-packet info:");
  254. NtTimeEpoch teOriginateTimestamp=NtTimeEpochFromNtpTimeEpoch(pnpIn->teOriginateTimestamp);
  255. NtTimeEpoch teReceiveTimestamp=NtTimeEpochFromNtpTimeEpoch(pnpIn->teReceiveTimestamp);
  256. NtTimeEpoch teTransmitTimestamp=NtTimeEpochFromNtpTimeEpoch(pnpIn->teTransmitTimestamp);
  257. DebugWPrintf0(L"\n| DestinationTimestamp: ");
  258. {
  259. NtpTimeEpoch teNtpTemp=NtpTimeEpochFromNtTimeEpoch(teDestinationTimestamp);
  260. NtTimeEpoch teNtTemp=NtTimeEpochFromNtpTimeEpoch(teNtpTemp);
  261. DumpNtpTimeEpoch(teNtpTemp);
  262. unsigned __int32 nConversionError;
  263. if (teNtTemp.qw>teDestinationTimestamp.qw) {
  264. nConversionError=(unsigned __int32)(teNtTemp-teDestinationTimestamp).qw;
  265. } else {
  266. nConversionError=(unsigned __int32)(teDestinationTimestamp-teNtTemp).qw;
  267. }
  268. if (0!=nConversionError) {
  269. DebugWPrintf1(L" - CnvErr:%u00ns", nConversionError);
  270. }
  271. }
  272. DebugWPrintf0(L"\n| RoundtripDelay: ");
  273. {
  274. NtTimeOffset toRoundtripDelay=
  275. (teDestinationTimestamp-teOriginateTimestamp)
  276. - (teTransmitTimestamp-teReceiveTimestamp);
  277. DebugWPrintf1(L"%I64d00ns", toRoundtripDelay.qw);
  278. }
  279. DebugWPrintf0(L"\n| LocalClockOffset: ");
  280. {
  281. NtTimeOffset toLocalClockOffset=
  282. (teReceiveTimestamp-teOriginateTimestamp)
  283. + (teTransmitTimestamp-teDestinationTimestamp);
  284. toLocalClockOffset/=2;
  285. DebugWPrintf1(L"%I64d00ns", toLocalClockOffset.qw);
  286. unsigned __int64 nAbsOffset;
  287. if (toLocalClockOffset.qw<0) {
  288. nAbsOffset=(unsigned __int64)(-toLocalClockOffset.qw);
  289. } else {
  290. nAbsOffset=(unsigned __int64)(toLocalClockOffset.qw);
  291. }
  292. DWORD dwNanoSecs=(DWORD)(nAbsOffset%10000000);
  293. nAbsOffset/=10000000;
  294. DWORD dwSecs=(DWORD)(nAbsOffset%60);
  295. nAbsOffset/=60;
  296. DebugWPrintf3(L" - %I64u:%02u.%07u00s", nAbsOffset, dwSecs, dwNanoSecs);
  297. }
  298. } // <- end if (0!=nDestinationTimestamp)
  299. DebugWPrintf0(L"\n\\--\n");
  300. }
  301. //--------------------------------------------------------------------
  302. // Print out an NTP-style time
  303. void DumpNtpTimeEpoch(NtpTimeEpoch te) {
  304. DebugWPrintf1(L"0x%016I64X", EndianSwap(te.qw));
  305. if (0==te.qw) {
  306. DebugWPrintf0(L" - unspecified");
  307. } else {
  308. DumpNtTimeEpoch(NtTimeEpochFromNtpTimeEpoch(te));
  309. }
  310. }
  311. //--------------------------------------------------------------------
  312. // Print out an NT-style time
  313. void DumpNtTimeEpoch(NtTimeEpoch te) {
  314. DebugWPrintf1(L" - %I64d00ns", te.qw);
  315. DWORD dwNanoSecs=(DWORD)(te.qw%10000000);
  316. te.qw/=10000000;
  317. DWORD dwSecs=(DWORD)(te.qw%60);
  318. te.qw/=60;
  319. DWORD dwMins=(DWORD)(te.qw%60);
  320. te.qw/=60;
  321. DWORD dwHours=(DWORD)(te.qw%24);
  322. DWORD dwDays=(DWORD)(te.qw/24);
  323. DebugWPrintf5(L" - %u %02u:%02u:%02u.%07us", dwDays, dwHours, dwMins, dwSecs, dwNanoSecs);
  324. }
  325. //--------------------------------------------------------------------
  326. void DumpNtTimePeriod(NtTimePeriod tp) {
  327. DebugWPrintf2(L"%02I64u.%07I64us", tp.qw/10000000,tp.qw%10000000);
  328. }
  329. //--------------------------------------------------------------------
  330. void DumpNtTimeOffset(NtTimeOffset to) {
  331. NtTimePeriod tp;
  332. if (to.qw<0) {
  333. DebugWPrintf0(L"-");
  334. tp.qw=(unsigned __int64)-to.qw;
  335. } else {
  336. DebugWPrintf0(L"+");
  337. tp.qw=(unsigned __int64)to.qw;
  338. }
  339. DumpNtTimePeriod(tp);
  340. }
  341. //--------------------------------------------------------------------
  342. // retrieve the system time
  343. NtTimeEpoch GetCurrentSystemNtTimeEpoch(void) {
  344. NtTimeEpoch teRet;
  345. FILETIME ft;
  346. GetSystemTimeAsFileTime(&ft);
  347. teRet.qw=ft.dwLowDateTime | (((unsigned __int64)ft.dwHighDateTime)<<32);
  348. return teRet;
  349. }
  350. /*--------------------------------------------------------------------
  351. Time formats:
  352. NT time: (10^-7)s intervals since (0h 1-Jan 1601)
  353. NTP time: (2^-32)s intervals since (0h 1-Jan 1900)
  354. Offset:
  355. 109207 days between (0h 1-Jan 1601) and (0h 1-Jan 1900)
  356. == 109207*24*60*60*1E7
  357. == 94,354,848,000,000,000 NT intervals (0x014F 373B FDE0 4000)
  358. When will NTP time overflow?
  359. Rollover: 4294967296 s
  360. (0h 1-Jan 2036) = 49673 days.
  361. in 2036, have 3220096 seconds left = 37 days 6 hours 28 minutes 16 seconds.
  362. 4294967296 s
  363. 4291747200 s = 49673 days, remainder == 3220096 s
  364. 3196800 s = 37 days == 23296 s
  365. 21600 s = 6 hours == 1696 s
  366. 1680 s = 28 minutes == 16 s
  367. 16 s = 16 seconds == 0 s
  368. Therefore:
  369. (06:28:16 7-Feb 2036 UTC)==(00:00:00 1-Jan 1900 UTC)
  370. What does that look like in NT time?
  371. (06:28:16 7-Feb 2036 UTC):
  372. 94,354,848,000,000,000 + 42,949,672,960,000,000 = 137,304,520,960,000,000 (0x01E7 CDBB FDE0 4000)
  373. No problem.
  374. When will NT time overflow?
  375. Rollover: 18,446,744,073,70|9,551,616 00ns
  376. (0h 1-Jan 60,056) = 21350250 days.
  377. 1844674407370 s
  378. 1844661600000 s = 21350250 days == 12807370
  379. 12787200 s = 148 days == 20170
  380. 18000 s = 5 hours == 2170
  381. 2160 s = 36 minutes == 10
  382. 10 s = 10 seconds == 0
  383. Therefore:
  384. (05:36:10.9551616 29-May 60056)==(00:00:00 1-Jan 1601)
  385. --------------------------------------------------------------------*/