X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fntp.cpp;h=64fd46c90b3a5df1deabc985b75edea858efbff4;hb=cecf7a56ed5a5efd939b21c760c69da616306005;hp=4cc422b0037f9f490a51c20f57972c970e23fc0c;hpb=1d8129375a4d646abb5c49075d2b1f065d932680;p=novacoin.git diff --git a/src/ntp.cpp b/src/ntp.cpp index 4cc422b..64fd46c 100644 --- a/src/ntp.cpp +++ b/src/ntp.cpp @@ -52,7 +52,7 @@ typedef struct { } l_fp; -inline void Ntp2Unix(uint32_t &n, time_t &u) { +inline void Ntp2Unix(const uint32_t &n, time_t &u) { // Ntp's time scale starts in 1900, Unix in 1970. u = n - 0x83aa7e80; // 2208988800 1970 - 1900 in seconds @@ -80,9 +80,9 @@ struct pkt { uint8_t mac[5 * sizeof(uint32_t)]; /* mac */ }; -const int nServersCount = 147; +const int nServersCount = 162; -std::string NtpServers[147] = { +std::string NtpServers[162] = { // Microsoft "time.windows.com", @@ -151,10 +151,12 @@ std::string NtpServers[147] = { "ntp-03.caltech.edu", "ntp-04.caltech.edu", "nist1-pa.ustiming.org", - "time-a.nist.gov ", - "time-b.nist.gov ", - "time-c.nist.gov ", - "time-d.nist.gov ", + "time.nist.gov", + "time-a.nist.gov", + "time-b.nist.gov", + "time-c.nist.gov", + "time-d.nist.gov", + "time-nw.nist.gov", "nist1-macon.macon.ga.us", "nist.netservicesgroup.com", "nisttime.carsoncity.k12.mi.us", @@ -163,7 +165,6 @@ std::string NtpServers[147] = { "time-a.timefreq.bldrdoc.gov", "time-b.timefreq.bldrdoc.gov", "time-c.timefreq.bldrdoc.gov", - "time.nist.gov", "utcnist.colorado.edu", "utcnist2.colorado.edu", "ntp-nist.ldsbc.net", @@ -178,6 +179,13 @@ std::string NtpServers[147] = { "1.us.pool.ntp.org", "2.us.pool.ntp.org", "3.us.pool.ntp.org", + "wwv.otc.psu.edu", + "otc1.psu.edu", + "otc2.psu.edu", + "now.okstate.edu", + "ntp.colby.edu", + "bonehed.lcs.mit.edu", + "ntp-s1.cise.ufl.edu", // South Africa "ntp1.meraka.csir.co.za", @@ -197,6 +205,9 @@ std::string NtpServers[147] = { "3.za.pool.ntp.org", // Italy + "ntp0.ien.it", + "ntp1.ien.it", + "ntp2.ien.it", "ntp1.inrim.it", "ntp2.inrim.it", "0.it.pool.ntp.org", @@ -257,14 +268,21 @@ std::string NtpServers[147] = { // Slovenia "time.ijs.si", + // Austria + "0.at.pool.ntp.org", + "1.at.pool.ntp.org", + "2.at.pool.ntp.org", + "3.at.pool.ntp.org", + // ??? "clepsydra.dec.com", // ... To be continued }; -bool InitWithHost(std::string &strHostName, SOCKET &sockfd, socklen_t &servlen, struct sockaddr *pcliaddr) { - sockfd = -1; +bool InitWithHost(const std::string &strHostName, SOCKET &sockfd, socklen_t &servlen, struct sockaddr *pcliaddr) { + + sockfd = INVALID_SOCKET; std::vector vIP; bool fRet = LookupHost(strHostName.c_str(), vIP, 10, true); @@ -278,7 +296,7 @@ bool InitWithHost(std::string &strHostName, SOCKET &sockfd, socklen_t &servlen, bool found = false; for(unsigned int i = 0; i < vIP.size(); i++) { - if ((found = vIP[i].GetInAddr(&servaddr.sin_addr))) { + if ((found = vIP[i].GetInAddr(&servaddr.sin_addr)) != false) { break; } } @@ -316,6 +334,8 @@ bool InitWithRandom(SOCKET &sockfd, socklen_t &servlen, struct sockaddr *pcliadd } int64_t DoReq(SOCKET sockfd, socklen_t servlen, struct sockaddr cliaddr) { + + #ifdef WIN32 u_long nOne = 1; if (ioctlsocket(sockfd, FIONBIO, &nOne) == SOCKET_ERROR) { @@ -327,8 +347,11 @@ int64_t DoReq(SOCKET sockfd, socklen_t servlen, struct sockaddr cliaddr) { return -2; } + struct timeval timeout = {10, 0}; struct pkt *msg = new pkt; struct pkt *prt = new pkt; + time_t seconds_transmit; + int len = 48; msg->li_vn_mode=227; msg->stratum=0; @@ -346,29 +369,30 @@ int64_t DoReq(SOCKET sockfd, socklen_t servlen, struct sockaddr cliaddr) { msg->xmt.Ul_i.Xl_i=0; msg->xmt.Ul_f.Xl_f=0; - int len=48; int retcode = sendto(sockfd, (char *) msg, len, 0, &cliaddr, servlen); if (retcode < 0) { printf("sendto() failed: %d\n", retcode); - return -3; + seconds_transmit = -3; + goto _end; } fd_set fdset; - struct timeval timeout = {10, 0}; FD_ZERO(&fdset); FD_SET(sockfd, &fdset); retcode = select(sockfd + 1, &fdset, NULL, NULL, &timeout); if (retcode <= 0) { printf("recvfrom() error\n"); - return -4; + seconds_transmit = -4; + goto _end; } recvfrom(sockfd, (char *) msg, len, 0, NULL, NULL); ntohl_fp(&msg->xmt, &prt->xmt); - time_t seconds_transmit; Ntp2Unix(prt->xmt.Ul_i.Xl_ui, seconds_transmit); + _end: + delete msg; delete prt; @@ -387,12 +411,12 @@ int64_t NtpGetTime(CNetAddr& ip) { ip = CNetAddr(((sockaddr_in *)&cliaddr)->sin_addr); int64_t nTime = DoReq(sockfd, servlen, cliaddr); - closesocket(sockfd); + CloseSocket(sockfd); return nTime; } -int64_t NtpGetTime(std::string &strHostName) +int64_t NtpGetTime(const std::string &strHostName) { struct sockaddr cliaddr; @@ -404,7 +428,7 @@ int64_t NtpGetTime(std::string &strHostName) int64_t nTime = DoReq(sockfd, servlen, cliaddr); - closesocket(sockfd); + CloseSocket(sockfd); return nTime; } @@ -421,7 +445,7 @@ int64_t GetNtpOffset() { } void ThreadNtpSamples(void* parg) { - const int64_t nMaxOffset = 86400; // Not a real limit, just sanity threshold. + const int64_t nMaxOffset = nOneDay; // Not a real limit, just sanity threshold. printf("Trying to find NTP server at localhost...\n");