5 #include <sys/socket.h>
7 #include <netinet/in.h>
19 extern int GetRandInt(int nMax);
22 * NTP uses two fixed point formats. The first (l_fp) is the "long"
23 * format and is 64 bits long with the decimal between bits 31 and 32.
24 * This is used for time stamps in the NTP packet header (in network
25 * byte order) and for internal computations of offsets (in local host
26 * byte order). We use the same structure for both signed and unsigned
27 * values, which is a big hack but saves rewriting all the operators
28 * twice. Just to confuse this, we also sometimes just carry the
29 * fractional part in calculations, in both signed and unsigned forms.
30 * Anyway, an l_fp looks like:
33 * 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
34 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 * REF http://www.eecis.udel.edu/~mills/database/rfc/rfc2030.txt
55 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) {
62 (h)->Ul_i.Xl_ui = ntohl((n)->Ul_i.Xl_ui);
63 (h)->Ul_f.Xl_uf = ntohl((n)->Ul_f.Xl_uf);
67 uint8_t li_vn_mode; /* leap indicator, version and mode */
68 uint8_t stratum; /* peer stratum */
69 uint8_t ppoll; /* peer poll interval */
70 int8_t precision; /* peer clock precision */
71 uint32_t rootdelay; /* distance to primary clock */
72 uint32_t rootdispersion; /* clock dispersion */
73 uint32_t refid; /* reference clock ID */
74 l_fp ref; /* time peer clock was last updated */
75 l_fp org; /* originate time stamp */
76 l_fp rec; /* receive time stamp */
77 l_fp xmt; /* transmit time stamp */
79 uint32_t exten[1]; /* misused */
80 uint8_t mac[5 * sizeof(uint32_t)]; /* mac */
83 int nServersCount = 107;
85 std::string NtpServers[107] = {
113 "ntp1.niiftri.irkutsk.ru",
114 "ntp2.niiftri.irkutsk.ru",
125 "ntp-01.caltech.edu",
126 "ntp-02.caltech.edu",
127 "ntp-03.caltech.edu",
128 "ntp-04.caltech.edu",
129 "nist1-pa.ustiming.org",
134 "nist1-macon.macon.ga.us",
135 "nist.netservicesgroup.com",
136 "nisttime.carsoncity.k12.mi.us",
137 "nist1-lnk.binary.net",
139 "time-a.timefreq.bldrdoc.gov",
140 "time-b.timefreq.bldrdoc.gov",
141 "time-c.timefreq.bldrdoc.gov",
143 "utcnist.colorado.edu",
144 "utcnist2.colorado.edu",
145 "ntp-nist.ldsbc.net",
146 "nist1-lv.ustiming.org",
148 "nist-time-server.eoni.com",
149 "nist-time-server.eoni.com",
159 "ntp1.meraka.csir.co.za",
163 "ntp1.neology.co.za",
164 "ntp2.neology.co.za",
165 "tick.meraka.csir.co.za",
166 "tock.meraka.csir.co.za",
168 "ntp1.meraka.csir.co.za",
169 "ntp2.meraka.csir.co.za",
197 "ntp.cis.strath.ac.uk",
211 // ... To be continued
214 bool InitWithRandom(SOCKET &sockfd, socklen_t &servlen, struct sockaddr *pcliaddr) {
217 while(nAttempt < 100) {
221 int nServerNum = GetRandInt(nServersCount);
223 std::vector<CNetAddr> vIP;
224 bool fRet = LookupHost(NtpServers[nServerNum].c_str(), vIP, 10, true);
228 struct sockaddr_in servaddr;
229 servaddr.sin_family = AF_INET;
230 servaddr.sin_port = htons(123);
233 for(unsigned int i = 0; i < vIP.size(); i++) {
234 if ((found = vIP[i].GetInAddr(&servaddr.sin_addr))) {
242 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
244 if (sockfd == INVALID_SOCKET)
245 continue; // socket initialization error
247 if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) == -1 ) {
248 continue; // "connection" error
251 *pcliaddr = *((struct sockaddr *) &servaddr);
252 servlen = sizeof(servaddr);
259 bool InitWithHost(std::string &strHostName, SOCKET &sockfd, socklen_t &servlen, struct sockaddr *pcliaddr) {
262 std::vector<CNetAddr> vIP;
263 bool fRet = LookupHost(strHostName.c_str(), vIP, 10, true);
268 struct sockaddr_in servaddr;
269 servaddr.sin_family = AF_INET;
270 servaddr.sin_port = htons(123);
273 for(unsigned int i = 0; i < vIP.size(); i++) {
274 if ((found = vIP[i].GetInAddr(&servaddr.sin_addr))) {
283 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
285 if (sockfd == INVALID_SOCKET)
286 return false; // socket initialization error
288 if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) == -1 ) {
289 return false; // "connection" error
293 *pcliaddr = *((struct sockaddr *) &servaddr);
294 servlen = sizeof(servaddr);
300 int64_t DoReq(SOCKET sockfd, socklen_t servlen, struct sockaddr cliaddr) {
303 if (ioctlsocket(sockfd, FIONBIO, &nOne) == SOCKET_ERROR) {
304 printf("ConnectSocket() : ioctlsocket non-blocking setting failed, error %d\n", WSAGetLastError());
306 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) == SOCKET_ERROR) {
307 printf("ConnectSocket() : fcntl non-blocking setting failed, error %d\n", errno);
312 struct pkt *msg = new pkt;
313 struct pkt *prt = new pkt;
320 msg->rootdispersion=0;
322 msg->ref.Ul_i.Xl_i=0;
323 msg->ref.Ul_f.Xl_f=0;
324 msg->org.Ul_i.Xl_i=0;
325 msg->org.Ul_f.Xl_f=0;
326 msg->rec.Ul_i.Xl_i=0;
327 msg->rec.Ul_f.Xl_f=0;
328 msg->xmt.Ul_i.Xl_i=0;
329 msg->xmt.Ul_f.Xl_f=0;
332 int retcode = sendto(sockfd, (char *) msg, len, 0, &cliaddr, servlen);
334 printf("sendto() failed: %d", retcode);
339 struct timeval timeout = {10, 0};
341 FD_SET(sockfd, &fdset);
343 retcode = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
345 printf("recvfrom() error");
349 recvfrom(sockfd, (char *) msg, len, 0, NULL, NULL);
351 ntohl_fp(&msg->rec, &prt->rec);
352 ntohl_fp(&msg->xmt, &prt->xmt);
354 time_t seconds_receive;
355 time_t seconds_transmit;
357 Ntp2Unix(prt->rec.Ul_i.Xl_ui, seconds_receive);
358 Ntp2Unix(prt->xmt.Ul_i.Xl_ui, seconds_transmit);
363 return (seconds_receive + seconds_transmit) / 2;
366 int64_t NtpGetTime() {
367 struct sockaddr cliaddr;
372 if (!InitWithRandom(sockfd, servlen, &cliaddr))
375 int64_t nTime = DoReq(sockfd, servlen, cliaddr);
382 int64_t NtpGetTime(CNetAddr& ip) {
383 struct sockaddr cliaddr;
388 if (!InitWithRandom(sockfd, servlen, &cliaddr))
391 ip = CNetAddr(((sockaddr_in *)&cliaddr)->sin_addr);
392 int64_t nTime = DoReq(sockfd, servlen, cliaddr);
399 int64_t NtpGetTime(std::string &strHostName)
401 struct sockaddr cliaddr;
406 if (!InitWithHost(strHostName, sockfd, servlen, &cliaddr))
409 int64_t nTime = DoReq(sockfd, servlen, cliaddr);
416 void ThreadNtpSamples(void* parg)
418 printf("ThreadNtpSamples started\n");
419 vnThreadsRunning[THREAD_NTP]++;
421 // Make this thread recognisable as time synchronization thread
422 RenameThread("novacoin-ntp-samples");
426 int64_t nTime = NtpGetTime(ip);
428 if (nTime > 0 && nTime != 2085978496) { // Skip the deliberately wrong timestamps
429 AddTimeData(ip, nTime);
432 Sleep(600000); // In case of failure wait 600 seconds and then try again
436 Sleep(43200000); // Sleep for 12 hours
439 vnThreadsRunning[THREAD_NTP]--;
440 printf("ThreadNtpSamples exited\n");