Remove NtpGetTime() and use NtpTime(CNetAddr&) instead.
authorCryptoManiac <balthazar@yandex.ru>
Sun, 20 Sep 2015 21:04:07 +0000 (00:04 +0300)
committerCryptoManiac <balthazar@yandex.ru>
Sun, 20 Sep 2015 21:04:07 +0000 (00:04 +0300)
src/ntp.cpp
src/ntp.h
src/rpcnet.cpp

index efedce8..b68110e 100644 (file)
@@ -226,51 +226,6 @@ std::string NtpServers[118] = {
     // ... To be continued
 };
 
-bool InitWithRandom(SOCKET &sockfd, socklen_t &servlen, struct sockaddr *pcliaddr) {
-    int nAttempt = 0;
-
-    while(nAttempt < 100) {
-        sockfd = -1;
-        nAttempt++;
-
-        int nServerNum = GetRandInt(nServersCount);
-
-        std::vector<CNetAddr> vIP;
-        bool fRet = LookupHost(NtpServers[nServerNum].c_str(), vIP, 10, true);
-        if (!fRet)
-            continue;
-
-        struct sockaddr_in servaddr;
-        servaddr.sin_family = AF_INET;
-        servaddr.sin_port = htons(123);
-
-        bool found = false;
-        for(unsigned int i = 0; i < vIP.size(); i++) {
-            if ((found = vIP[i].GetInAddr(&servaddr.sin_addr))) {
-                break;
-            }
-        }
-
-        if (!found)
-            continue;
-
-        sockfd = socket(AF_INET, SOCK_DGRAM, 0);
-
-        if (sockfd == INVALID_SOCKET)
-            continue; // socket initialization error
-
-        if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) == -1 ) {
-            continue; // "connection" error
-        }
-
-        *pcliaddr = *((struct sockaddr *) &servaddr);
-        servlen = sizeof(servaddr);
-        return true;
-    }
-
-    return false;
-}
-
 bool InitWithHost(std::string &strHostName, SOCKET &sockfd, socklen_t &servlen, struct sockaddr *pcliaddr) {
     sockfd = -1;
 
@@ -311,6 +266,17 @@ bool InitWithHost(std::string &strHostName, SOCKET &sockfd, socklen_t &servlen,
     return true;
 }
 
+bool InitWithRandom(SOCKET &sockfd, socklen_t &servlen, struct sockaddr *pcliaddr) {
+
+    for (int nAttempt = 0; nAttempt < nServersCount; nAttempt++) {
+        int nServerNum = GetRandInt(nServersCount);
+        if (InitWithHost(NtpServers[nServerNum], sockfd, servlen, pcliaddr)) {
+            return true;
+        }
+    }
+
+    return false;
+}
 
 int64_t DoReq(SOCKET sockfd, socklen_t servlen, struct sockaddr cliaddr) {
 #ifdef WIN32
@@ -378,22 +344,6 @@ int64_t DoReq(SOCKET sockfd, socklen_t servlen, struct sockaddr cliaddr) {
     return (seconds_receive + seconds_transmit) / 2;
 }
 
-int64_t NtpGetTime() {
-    struct sockaddr cliaddr;
-
-    SOCKET sockfd;
-    socklen_t servlen;
-
-    if (!InitWithRandom(sockfd, servlen, &cliaddr))
-        return -1;
-
-    int64_t nTime = DoReq(sockfd, servlen, cliaddr);
-
-    closesocket(sockfd);
-
-    return nTime;
-}
-
 int64_t NtpGetTime(CNetAddr& ip) {
     struct sockaddr cliaddr;
 
index aa48864..8be3864 100644 (file)
--- a/src/ntp.h
+++ b/src/ntp.h
@@ -1,6 +1,3 @@
-// Get time from random server.
-int64_t NtpGetTime();
-
 // Get time from random server and return server address.
 int64_t NtpGetTime(CNetAddr& ip);
 
index c746674..35e4cb9 100644 (file)
@@ -362,13 +362,6 @@ Value getnettotals(const Array& params, bool fHelp)
 "time" : "2015-09-17 12:53:56 UTC"
 }
 
-05:53:59 ntptime time.windows.com
-05:54:00
-{
-"epoch" : 1442494439,
-"time" : "2015-09-17 12:53:59 UTC"
-}
-
 05:54:33 ntptime time-a.nist.gov
 05:54:34
 {
@@ -384,17 +377,17 @@ Value ntptime(const Array& params, bool fHelp)
             "Returns current time from specific or random NTP server.");
 
     int64_t nTime;
-    if (params.size() > 0)
-    {
+    if (params.size() > 0) {
         string strHostName = params[0].get_str();
         nTime = NtpGetTime(strHostName);
     }
-    else
-        nTime = NtpGetTime();
+    else {
+        CNetAddr ip;
+        nTime = NtpGetTime(ip);
+    }
 
     Object obj;
-    switch (nTime)
-    {
+    switch (nTime) {
     case -1:
         throw runtime_error("Socket initialization error");
     case -2:
@@ -404,8 +397,7 @@ Value ntptime(const Array& params, bool fHelp)
     case -4:
         throw runtime_error("Receive timed out");
     default:
-        if (nTime > 0 && nTime != 2085978496)
-        {
+        if (nTime > 0 && nTime != 2085978496) {
             obj.push_back(Pair("epoch", nTime));
             obj.push_back(Pair("time", DateTimeStrFormat(nTime)));
         }