X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fntp.cpp;h=d7016d6c40f621ba2910cbfa5d5b2dcffa11bdc2;hb=8b4e446342ab1af810732445592253cd467dc7b1;hp=6ca84a2a5a723f247a02dd9b35bed895e6b0bc37;hpb=46a9e486b0b9e68b825c7d89ed4d226108600abc;p=novacoin.git diff --git a/src/ntp.cpp b/src/ntp.cpp index 6ca84a2..d7016d6 100644 --- a/src/ntp.cpp +++ b/src/ntp.cpp @@ -1,9 +1,9 @@ -#include #ifdef WIN32 #include #else #include +#include #include #include #include @@ -13,6 +13,7 @@ #endif #include "netbase.h" +#include "util.h" extern int GetRandInt(int nMax); @@ -57,12 +58,6 @@ inline void Ntp2Unix(uint32_t &n, time_t &u) u = n - 0x83aa7e80; // 2208988800 1970 - 1900 in seconds } -inline void HTONL_FP(l_fp *h, l_fp *n) -{ - (n)->Ul_i.Xl_ui = htonl((h)->Ul_i.Xl_ui); - (n)->Ul_f.Xl_uf = htonl((h)->Ul_f.Xl_uf); -} - inline void ntohl_fp(l_fp *n, l_fp *h) { (h)->Ul_i.Xl_ui = ntohl((n)->Ul_i.Xl_ui); @@ -86,22 +81,20 @@ struct pkt { uint8_t mac[5 * sizeof(uint32_t)]; /* mac */ }; -int nServersCount = 65; +int nServersCount = 103; -std::string NtpServers[65] = { +std::string NtpServers[103] = { // Microsoft "time.windows.com", // Google "time1.google.com", "time2.google.com", + "time3.google.com", + "time4.google.com", // Russian Federation "ntp.ix.ru", - "0.ru.pool.ntp.org", - "1.ru.pool.ntp.org", - "2.ru.pool.ntp.org", - "3.ru.pool.ntp.org", "ntp1.stratum2.ru", "ntp2.stratum2.ru", "ntp3.stratum2.ru", @@ -122,6 +115,12 @@ std::string NtpServers[65] = { "ntp2.niiftri.irkutsk.ru", "vniiftri.khv.ru", "vniiftri2.khv.ru", + "ntp0.zenon.net", + "ntp.mobatime.ru", + "0.ru.pool.ntp.org", + "1.ru.pool.ntp.org", + "2.ru.pool.ntp.org", + "3.ru.pool.ntp.org", // United States "nist1-pa.ustiming.org", @@ -148,6 +147,10 @@ std::string NtpServers[65] = { "ntp1.bu.edu", "ntp2.bu.edu", "ntp3.bu.edu", + "0.us.pool.ntp.org", + "1.us.pool.ntp.org", + "2.us.pool.ntp.org", + "3.us.pool.ntp.org", // South Africa "ntp1.meraka.csir.co.za", @@ -161,19 +164,51 @@ std::string NtpServers[65] = { "ntp.time.org.za", "ntp1.meraka.csir.co.za", "ntp2.meraka.csir.co.za", + "0.za.pool.ntp.org", + "1.za.pool.ntp.org", + "2.za.pool.ntp.org", + "3.za.pool.ntp.org", // Italy "ntp1.inrim.it", "ntp2.inrim.it", + "0.it.pool.ntp.org", + "1.it.pool.ntp.org", + "2.it.pool.ntp.org", + "3.it.pool.ntp.org", + + // Netherlands + "ntp0.nl.net", + "ntp1.nl.net", + "ntp2.nl.net", + "ntp.utwente.nl", + "0.nl.pool.ntp.org", + "1.nl.pool.ntp.org", + "2.nl.pool.ntp.org", + "3.nl.pool.ntp.org", + + // United Kingdom + "ntp2d.mcc.ac.uk", + "ntp2c.mcc.ac.uk", + "ntp.exnet.com", + "ntp.cis.strath.ac.uk", + "ntppub.le.ac.uk", + "0.uk.pool.ntp.org", + "1.uk.pool.ntp.org", + "2.uk.pool.ntp.org", + "3.uk.pool.ntp.org", + + // Japan + "ntp.nict.jp", + "0.jp.pool.ntp.org", + "1.jp.pool.ntp.org", + "2.jp.pool.ntp.org", + "3.jp.pool.ntp.org", // ... To be continued }; -#ifdef WIN32 -bool InitWithRandom(SOCKET &sockfd, int &servlen, struct sockaddr *pcliaddr) -#else -bool InitWithRandom(int &sockfd, socklen_t &servlen, struct sockaddr *pcliaddr) -#endif +bool InitWithRandom(SOCKET &sockfd, socklen_t &servlen, struct sockaddr *pcliaddr) { int nAttempt = 0; @@ -207,7 +242,7 @@ bool InitWithRandom(int &sockfd, socklen_t &servlen, struct sockaddr *pcliaddr) sockfd = socket(AF_INET, SOCK_DGRAM, 0); - if (sockfd == -1) + if (sockfd == INVALID_SOCKET) continue; // socket initialization error if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) == -1 ) @@ -223,11 +258,7 @@ bool InitWithRandom(int &sockfd, socklen_t &servlen, struct sockaddr *pcliaddr) return false; } -#ifdef WIN32 -bool InitWithHost(std::string &strHostName, SOCKET &sockfd, int &servlen, struct sockaddr *pcliaddr) -#else -bool InitWithHost(std::string &strHostName, int &sockfd, socklen_t &servlen, struct sockaddr *pcliaddr) -#endif +bool InitWithHost(std::string &strHostName, SOCKET &sockfd, socklen_t &servlen, struct sockaddr *pcliaddr) { sockfd = -1; @@ -254,7 +285,7 @@ bool InitWithHost(std::string &strHostName, int &sockfd, socklen_t &servlen, str sockfd = socket(AF_INET, SOCK_DGRAM, 0); - if (sockfd == -1) + if (sockfd == INVALID_SOCKET) return false; // socket initialization error if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) == -1 ) @@ -262,6 +293,7 @@ bool InitWithHost(std::string &strHostName, int &sockfd, socklen_t &servlen, str return false; // "connection" error } + *pcliaddr = *((struct sockaddr *) &servaddr); servlen = sizeof(servaddr); @@ -269,12 +301,22 @@ bool InitWithHost(std::string &strHostName, int &sockfd, socklen_t &servlen, str } +int64_t DoReq(SOCKET sockfd, socklen_t servlen, struct sockaddr cliaddr) +{ + #ifdef WIN32 -int64_t DoReq(SOCKET sockfd, int servlen, struct sockaddr cliaddr) + u_long nOne = 1; + if (ioctlsocket(sockfd, FIONBIO, &nOne) == SOCKET_ERROR) + { + printf("ConnectSocket() : ioctlsocket non-blocking setting failed, error %d\n", WSAGetLastError()); #else -int64_t DoReq(int sockfd, socklen_t servlen, struct sockaddr cliaddr) + if (fcntl(sockfd, F_SETFL, O_NONBLOCK) == SOCKET_ERROR) + { + printf("ConnectSocket() : fcntl non-blocking setting failed, error %d\n", errno); #endif -{ + return -2; + } + struct pkt *msg = new pkt; struct pkt *prt = new pkt; @@ -295,8 +337,25 @@ int64_t DoReq(int sockfd, socklen_t servlen, struct sockaddr cliaddr) msg->xmt.Ul_f.Xl_f=0; int len=48; - sendto(sockfd, (char *) msg, len, 0, &cliaddr, servlen); - int n = recvfrom(sockfd, (char *) msg, len, 0, NULL, NULL); + int retcode = sendto(sockfd, (char *) msg, len, 0, &cliaddr, servlen); + if (retcode < 0) { + printf("sendto() failed: %d", retcode); + return -3; + } + + fd_set fdset; + struct timeval timeout = {5, 0}; + FD_ZERO(&fdset); + FD_SET(sockfd, &fdset); + + retcode = select(sockfd + 1, &fdset, NULL, NULL, &timeout); + if (retcode <= 0) + { + printf("recvfrom() error"); + return -4; + } + + recvfrom(sockfd, (char *) msg, len, 0, NULL, NULL); ntohl_fp(&msg->rec, &prt->rec); ntohl_fp(&msg->xmt, &prt->xmt); @@ -317,24 +376,33 @@ int64_t NtpGetTime() { struct sockaddr cliaddr; -#ifdef WIN32 SOCKET sockfd; - int servlen; -#else - int sockfd; socklen_t servlen; -#endif if (!InitWithRandom(sockfd, servlen, &cliaddr)) return -1; int64_t nTime = DoReq(sockfd, servlen, cliaddr); -#ifdef WIN32 closesocket(sockfd); -#else - close(sockfd); -#endif + + return nTime; +} + +int64_t NtpGetTime(CNetAddr& ip) +{ + struct sockaddr cliaddr; + + SOCKET sockfd; + socklen_t servlen; + + if (!InitWithRandom(sockfd, servlen, &cliaddr)) + return -1; + + ip = CNetAddr(((sockaddr_in *)&cliaddr)->sin_addr); + int64_t nTime = DoReq(sockfd, servlen, cliaddr); + + closesocket(sockfd); return nTime; } @@ -343,24 +411,15 @@ int64_t NtpGetTime(std::string &strHostName) { struct sockaddr cliaddr; -#ifdef WIN32 SOCKET sockfd; - int servlen; -#else - int sockfd; socklen_t servlen; -#endif if (!InitWithHost(strHostName, sockfd, servlen, &cliaddr)) return -1; int64_t nTime = DoReq(sockfd, servlen, cliaddr); -#ifdef WIN32 closesocket(sockfd); -#else - close(sockfd); -#endif return nTime; }