5 #include <sys/socket.h>
7 #include <netinet/in.h>
18 extern int GetRandInt(int nMax);
21 * NTP uses two fixed point formats. The first (l_fp) is the "long"
22 * format and is 64 bits long with the decimal between bits 31 and 32.
23 * This is used for time stamps in the NTP packet header (in network
24 * byte order) and for internal computations of offsets (in local host
25 * byte order). We use the same structure for both signed and unsigned
26 * values, which is a big hack but saves rewriting all the operators
27 * twice. Just to confuse this, we also sometimes just carry the
28 * fractional part in calculations, in both signed and unsigned forms.
29 * Anyway, an l_fp looks like:
32 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
33 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 * REF http://www.eecis.udel.edu/~mills/database/rfc/rfc2030.txt
54 inline void Ntp2Unix(uint32_t &n, time_t &u)
56 // Ntp's time scale starts in 1900, Unix in 1970.
58 u = n - 0x83aa7e80; // 2208988800 1970 - 1900 in seconds
61 inline void ntohl_fp(l_fp *n, l_fp *h)
63 (h)->Ul_i.Xl_ui = ntohl((n)->Ul_i.Xl_ui);
64 (h)->Ul_f.Xl_uf = ntohl((n)->Ul_f.Xl_uf);
68 uint8_t li_vn_mode; /* leap indicator, version and mode */
69 uint8_t stratum; /* peer stratum */
70 uint8_t ppoll; /* peer poll interval */
71 int8_t precision; /* peer clock precision */
72 uint32_t rootdelay; /* distance to primary clock */
73 uint32_t rootdispersion; /* clock dispersion */
74 uint32_t refid; /* reference clock ID */
75 l_fp ref; /* time peer clock was last updated */
76 l_fp org; /* originate time stamp */
77 l_fp rec; /* receive time stamp */
78 l_fp xmt; /* transmit time stamp */
80 uint32_t exten[1]; /* misused */
81 uint8_t mac[5 * sizeof(uint32_t)]; /* mac */
84 int nServersCount = 107;
86 std::string NtpServers[107] = {
114 "ntp1.niiftri.irkutsk.ru",
115 "ntp2.niiftri.irkutsk.ru",
126 "ntp-01.caltech.edu",
127 "ntp-02.caltech.edu",
128 "ntp-03.caltech.edu",
129 "ntp-04.caltech.edu",
130 "nist1-pa.ustiming.org",
135 "nist1-macon.macon.ga.us",
136 "nist.netservicesgroup.com",
137 "nisttime.carsoncity.k12.mi.us",
138 "nist1-lnk.binary.net",
140 "time-a.timefreq.bldrdoc.gov",
141 "time-b.timefreq.bldrdoc.gov",
142 "time-c.timefreq.bldrdoc.gov",
144 "utcnist.colorado.edu",
145 "utcnist2.colorado.edu",
146 "ntp-nist.ldsbc.net",
147 "nist1-lv.ustiming.org",
149 "nist-time-server.eoni.com",
150 "nist-time-server.eoni.com",
160 "ntp1.meraka.csir.co.za",
164 "ntp1.neology.co.za",
165 "ntp2.neology.co.za",
166 "tick.meraka.csir.co.za",
167 "tock.meraka.csir.co.za",
169 "ntp1.meraka.csir.co.za",
170 "ntp2.meraka.csir.co.za",
198 "ntp.cis.strath.ac.uk",
212 // ... To be continued
215 bool InitWithRandom(SOCKET &sockfd, socklen_t &servlen, struct sockaddr *pcliaddr)
219 while(nAttempt < 100)
224 int nServerNum = GetRandInt(nServersCount);
226 std::vector<CNetAddr> vIP;
227 bool fRet = LookupHost(NtpServers[nServerNum].c_str(), vIP, 10, true);
231 struct sockaddr_in servaddr;
232 servaddr.sin_family = AF_INET;
233 servaddr.sin_port = htons(123);
236 for(unsigned int i = 0; i < vIP.size(); i++)
238 if ((found = vIP[i].GetInAddr(&servaddr.sin_addr)))
247 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
249 if (sockfd == INVALID_SOCKET)
250 continue; // socket initialization error
252 if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) == -1 )
254 continue; // "connection" error
257 *pcliaddr = *((struct sockaddr *) &servaddr);
258 servlen = sizeof(servaddr);
265 bool InitWithHost(std::string &strHostName, SOCKET &sockfd, socklen_t &servlen, struct sockaddr *pcliaddr)
269 std::vector<CNetAddr> vIP;
270 bool fRet = LookupHost(strHostName.c_str(), vIP, 10, true);
274 struct sockaddr_in servaddr;
275 servaddr.sin_family = AF_INET;
276 servaddr.sin_port = htons(123);
279 for(unsigned int i = 0; i < vIP.size(); i++)
281 if ((found = vIP[i].GetInAddr(&servaddr.sin_addr)))
290 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
292 if (sockfd == INVALID_SOCKET)
293 return false; // socket initialization error
295 if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) == -1 )
297 return false; // "connection" error
301 *pcliaddr = *((struct sockaddr *) &servaddr);
302 servlen = sizeof(servaddr);
308 int64_t DoReq(SOCKET sockfd, socklen_t servlen, struct sockaddr cliaddr)
313 if (ioctlsocket(sockfd, FIONBIO, &nOne) == SOCKET_ERROR)
315 printf("ConnectSocket() : ioctlsocket non-blocking setting failed, error %d\n", WSAGetLastError());
317 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) == SOCKET_ERROR)
319 printf("ConnectSocket() : fcntl non-blocking setting failed, error %d\n", errno);
324 struct pkt *msg = new pkt;
325 struct pkt *prt = new pkt;
332 msg->rootdispersion=0;
334 msg->ref.Ul_i.Xl_i=0;
335 msg->ref.Ul_f.Xl_f=0;
336 msg->org.Ul_i.Xl_i=0;
337 msg->org.Ul_f.Xl_f=0;
338 msg->rec.Ul_i.Xl_i=0;
339 msg->rec.Ul_f.Xl_f=0;
340 msg->xmt.Ul_i.Xl_i=0;
341 msg->xmt.Ul_f.Xl_f=0;
344 int retcode = sendto(sockfd, (char *) msg, len, 0, &cliaddr, servlen);
346 printf("sendto() failed: %d", retcode);
351 struct timeval timeout = {10, 0};
353 FD_SET(sockfd, &fdset);
355 retcode = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
358 printf("recvfrom() error");
362 recvfrom(sockfd, (char *) msg, len, 0, NULL, NULL);
364 ntohl_fp(&msg->rec, &prt->rec);
365 ntohl_fp(&msg->xmt, &prt->xmt);
367 time_t seconds_receive;
368 time_t seconds_transmit;
370 Ntp2Unix(prt->rec.Ul_i.Xl_ui, seconds_receive);
371 Ntp2Unix(prt->xmt.Ul_i.Xl_ui, seconds_transmit);
376 return (seconds_receive + seconds_transmit) / 2;
381 struct sockaddr cliaddr;
386 if (!InitWithRandom(sockfd, servlen, &cliaddr))
389 int64_t nTime = DoReq(sockfd, servlen, cliaddr);
396 int64_t NtpGetTime(CNetAddr& ip)
398 struct sockaddr cliaddr;
403 if (!InitWithRandom(sockfd, servlen, &cliaddr))
406 ip = CNetAddr(((sockaddr_in *)&cliaddr)->sin_addr);
407 int64_t nTime = DoReq(sockfd, servlen, cliaddr);
414 int64_t NtpGetTime(std::string &strHostName)
416 struct sockaddr cliaddr;
421 if (!InitWithHost(strHostName, sockfd, servlen, &cliaddr))
424 int64_t nTime = DoReq(sockfd, servlen, cliaddr);