EOL
[novacoin.git] / src / ntp.cpp
index 1d43fdb..bae50d9 100644 (file)
@@ -1,4 +1,3 @@
-
 #ifdef WIN32
 #include <winsock2.h>
 #else
@@ -13,8 +12,8 @@
 #endif
 
 #include "netbase.h"
-#include "util.h"
 #include "net.h"
+#include "util.h"
 
 extern int GetRandInt(int nMax);
 
@@ -82,7 +81,7 @@ struct pkt {
 
 const int nServersCount = 147;
 
-std::string NtpServers[147] = {
+const std::string NtpServers[147] = {
     // Microsoft
     "time.windows.com",
 
@@ -263,7 +262,7 @@ std::string NtpServers[147] = {
     // ... To be continued
 };
 
-bool InitWithHost(std::string &strHostName, SOCKET &sockfd, socklen_t &servlen, struct sockaddr *pcliaddr) {
+bool InitWithHost(const std::string &strHostName, SOCKET &sockfd, socklen_t &servlen, struct sockaddr *pcliaddr) {
     sockfd = -1;
 
     std::vector<CNetAddr> vIP;
@@ -349,7 +348,7 @@ int64_t DoReq(SOCKET sockfd, socklen_t servlen, struct sockaddr cliaddr) {
     int len=48;
     int retcode = sendto(sockfd, (char *) msg, len, 0, &cliaddr, servlen);
     if (retcode < 0) {
-        printf("sendto() failed: %d", retcode);
+        printf("sendto() failed: %d\n", retcode);
         return -3;
     }
 
@@ -360,25 +359,19 @@ int64_t DoReq(SOCKET sockfd, socklen_t servlen, struct sockaddr cliaddr) {
 
     retcode = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
     if (retcode <= 0) {
-        printf("recvfrom() error");
+        printf("recvfrom() error\n");
         return -4;
     }
 
     recvfrom(sockfd, (char *) msg, len, 0, NULL, NULL);
-
-    ntohl_fp(&msg->rec, &prt->rec);
     ntohl_fp(&msg->xmt, &prt->xmt);
-
-    time_t seconds_receive;
     time_t seconds_transmit;
-
-    Ntp2Unix(prt->rec.Ul_i.Xl_ui, seconds_receive);
     Ntp2Unix(prt->xmt.Ul_i.Xl_ui, seconds_transmit);
 
     delete msg;
     delete prt;
 
-    return (seconds_receive + seconds_transmit) / 2;
+    return seconds_transmit;
 }
 
 int64_t NtpGetTime(CNetAddr& ip) {
@@ -398,7 +391,7 @@ int64_t NtpGetTime(CNetAddr& ip) {
     return nTime;
 }
 
-int64_t NtpGetTime(std::string &strHostName)
+int64_t NtpGetTime(const std::string &strHostName)
 {
     struct sockaddr cliaddr;
 
@@ -415,27 +408,94 @@ int64_t NtpGetTime(std::string &strHostName)
     return nTime;
 }
 
-void ThreadNtpSamples(void* parg)
-{
+// NTP server, which we unconditionally trust. This may be your own installation of ntpd somewhere, for example. 
+// "localhost" means "trust no one"
+std::string strTrustedUpstream = "localhost";
+
+// Current offset
+int64_t nNtpOffset = INT64_MAX;
+
+int64_t GetNtpOffset() {
+    return nNtpOffset;
+}
+
+void ThreadNtpSamples(void* parg) {
+
+    // Maximum offset is 2 hours.
+    const int64_t nMaxOffset = 7200;
+
+    printf("Trying to find NTP server at localhost...\n");
+
+    const std::string strLocalHost = "127.0.0.1";
+    if (NtpGetTime(strLocalHost) == GetTime()) {
+        printf("There is NTP server active at localhost,  we don't need NTP thread.\n");
+
+        nNtpOffset = 0;
+        return;
+    }
+
     printf("ThreadNtpSamples started\n");
     vnThreadsRunning[THREAD_NTP]++;
 
     // Make this thread recognisable as time synchronization thread
     RenameThread("novacoin-ntp-samples");
 
-    while (!fShutdown) {
-        CNetAddr ip;
-        int64_t nTime = NtpGetTime(ip);
+    CMedianFilter<int64_t> vTimeOffsets(200,0);
 
-        if (nTime > 0 && nTime != 2085978496) { // Skip the deliberately wrong timestamps
-            AddTimeData(ip, nTime);
+    while (!fShutdown) {
+        if (strTrustedUpstream != strLocalHost) {
+            // Trying to get new offset sample from trusted NTP server.
+            int64_t nClockOffset = NtpGetTime(strTrustedUpstream) - GetTime();
+
+            if (abs64(nClockOffset) < nMaxOffset) {
+                // Everything seems right, remember new trusted offset.
+                printf("ThreadNtpSamples: new offset sample from %s, offset=%" PRId64 ".\n", strTrustedUpstream.c_str(), nClockOffset);
+                nNtpOffset = nClockOffset;
+            }
+            else {
+                // Something went wrong, disable trusted offset sampling.
+                nNtpOffset = INT64_MAX;
+                strTrustedUpstream = strLocalHost;
+
+                int nSleepMinutes = 1 + GetRandInt(9); // Sleep for 1-10 minutes.
+                for (int i = 0; i < nSleepMinutes * 60 && !fShutdown; i++)
+                    Sleep(1000);
+
+                continue;
+            }
         }
         else {
-            Sleep(600000); // In case of failure wait 600 seconds and then try again
-            continue;
+            // Now, trying to get 2-4 samples from random NTP servers.
+            int nSamplesCount = 2 + GetRandInt(2);
+
+            for (int i = 0; i < nSamplesCount; i++) {
+                CNetAddr ip;
+                int64_t nClockOffset = NtpGetTime(ip) - GetTime();
+
+                if (abs64(nClockOffset) < nMaxOffset) { // Skip the deliberately wrong timestamps
+                    printf("ThreadNtpSamples: new offset sample from %s, offset=%" PRId64 ".\n", ip.ToString().c_str(), nClockOffset);
+                    vTimeOffsets.input(nClockOffset);
+                }
+            }
+
+            if (vTimeOffsets.size() > 2) {
+                nNtpOffset = vTimeOffsets.median();
+            }
+            else {
+                // Not enough offsets yet, try again later.
+                nNtpOffset = INT64_MAX;
+                int nSleepMinutes = 1 + GetRandInt(4); // Sleep for 1-5 minutes.
+                for (int i = 0; i < nSleepMinutes * 60 && !fShutdown; i++) 
+                    Sleep(1000);
+                continue;
+            }
         }
 
-        Sleep(43200000); // Sleep for 12 hours
+        printf("nNtpOffset = %+" PRId64 "  (%+" PRId64 " minutes)\n", nNtpOffset, nNtpOffset/60);
+
+        int nSleepHours = 1 + GetRandInt(5); // Sleep for 1-6 hours.
+        for (int i = 0; i < nSleepHours * 3600 && !fShutdown; i++)
+            Sleep(1000);
     }
 
     vnThreadsRunning[THREAD_NTP]--;