forked from navcoin/navcoin-core
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathntpclient.cpp
210 lines (148 loc) · 5.28 KB
/
ntpclient.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright (c) 2018 alex v
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <init.h>
#include <ntpclient.h>
#include <random.h>
#include <timedata.h>
#include <util.h>
#include <sstream>
#include <iomanip>
using namespace boost;
using namespace boost::asio;
bool CNtpClient::getTimestamp(uint64_t &timeRecv)
{
io_service io_service;
LogPrint("ntp", "[NTP] Opening socket to NTP server %s.\n", sHostName);
try
{
ip::udp::resolver resolver(io_service);
ip::udp::resolver::query query(boost::asio::ip::udp::v4(), sHostName, "ntp");
ip::udp::endpoint receiver_endpoint = *resolver.resolve(query);
ip::udp::socket socket(io_service);
socket.open(ip::udp::v4());
boost::array<unsigned char, 48> sendBuf = {{010,0,0,0,0,0,0,0,0}};
socket.send_to(boost::asio::buffer(sendBuf), receiver_endpoint);
boost::array<uint32_t, 1024> recvBuf;
ip::udp::endpoint sender_endpoint;
try
{
fd_set fileDescriptorSet;
struct timeval timeStruct;
timeStruct.tv_sec = GetArg("-ntptimeout", DEFAULT_NTP_TIMEOUT);
timeStruct.tv_usec = 0;
FD_ZERO(&fileDescriptorSet);
int nativeSocket = socket.native_handle();
FD_SET(nativeSocket,&fileDescriptorSet);
select(nativeSocket+1,&fileDescriptorSet,NULL,NULL,&timeStruct);
if(!FD_ISSET(nativeSocket,&fileDescriptorSet))
{
LogPrint("ntp", "[NTP] Could not read socket from NTP server %s (Read timeout)\n", sHostName);
}
else
{
socket.receive_from(boost::asio::buffer(recvBuf), sender_endpoint);
timeRecv = ntohl((uint32_t)recvBuf[8]);
if(timeRecv > 2208988800U) // Sanity check
{
timeRecv-= 2208988800U; // Substract 01/01/1970 == 2208988800U
LogPrint("ntp", "[NTP] Received timestamp: %ll\n", (uint64_t)timeRecv);
return true;
}
else
{
LogPrintf("[NTP] Received wrong clock from NTP server %s (bad timestamp format)\n", sHostName);
}
}
}
catch (std::exception& e)
{
LogPrintf("[NTP] Could not read clock from NTP server %s (%s)\n", sHostName, e.what());
}
}
catch (std::exception& e)
{
LogPrintf("[NTP] Could not open socket to NTP server %s (%s)\n", sHostName, e.what());
}
return false;
}
bool NtpClockSync()
{
LogPrintf("[NTP] Starting clock sync...\n");
std::vector<std::string> vNtpServers;
std::vector<int64_t> vResults;
if (mapMultiArgs["-ntpserver"].size() > 0)
{
vNtpServers = mapMultiArgs["-ntpserver"];
}
else
{
vNtpServers = vDefaultNtpServers;
}
string sReport = "";
string sPrevServer = "";
int64_t nPrevMeasure = -1;
std::random_shuffle(vNtpServers.begin(), vNtpServers.end(), GetRandInt);
unsigned int nMeasureCount = 0;
for(unsigned int i = 0; i < vNtpServers.size(); i++)
{
string s = vNtpServers[i];
CNtpClient ntpClient(s);
uint64_t nTimestamp = 0;
if (ShutdownRequested())
return false;
if(ntpClient.getTimestamp(nTimestamp))
{
int64_t nClockDrift = GetTimeNow() - nTimestamp;
nMeasureCount++;
// We push if its the first entry
if(nMeasureCount == 1)
{
vResults.push_back(nClockDrift);
string sign = "";
if(nClockDrift > 0)
sign = "+";
sReport += s + "[" + sign + to_string(nClockDrift) + "sec.] ";
}
// or if n.measure is odd, including prev measure too
else if (nMeasureCount % 2 == 1)
{
vResults.push_back(nClockDrift);
string sign = "";
if(nClockDrift > 0)
sign = "+";
sReport += s + "[" + sign + to_string(nClockDrift) + "sec.] ";
vResults.push_back(nPrevMeasure);
sign = "";
if(nPrevMeasure > 0)
sign = "+";
sReport += sPrevServer + "[" + sign + to_string(nPrevMeasure) + "sec.] ";
}
else
{
nPrevMeasure = nClockDrift;
sPrevServer = s;
}
if (vResults.size() >= 5)
break;
}
}
assert(vResults.size() % 2 == 1 || vResults.size() == 0);
unsigned int nMin = GetArg("-ntpminmeasures", MINIMUM_NTP_MEASURE);
if (vResults.size() >= nMin)
{
LogPrintf("[NTP] Measured: %s\n", sReport);
static CMedianFilter<int64_t> vNtpTimeOffsets(vResults.size(), 0);
for(unsigned int i = 0; i < vResults.size(); i++)
{
vNtpTimeOffsets.input(vResults[i]);
}
SetNtpTimeOffset(vNtpTimeOffsets.median());
LogPrintf("[NTP] Calculated offset from median: %d\n", GetNtpTimeOffset());
return true;
}
else
{
return false;
}
}