@@ -40,6 +40,9 @@ using namespace system;
4040using namespace messages ;
4141using namespace std ::placeholders;
4242
43+ // Drop peer it its clock deviates more than 2 hours from own clock.
44+ constexpr auto allowed_timestamp_deviation = hours{ 2 };
45+
4346// Require the configured minimum protocol and services by default.
4447protocol_version_31402::protocol_version_31402 (const session::ptr& session,
4548 const channel::ptr& channel) NOEXCEPT
@@ -273,6 +276,16 @@ bool protocol_version_31402::handle_receive_acknowledge(const code& ec,
273276// Incoming [receive_version => send_acknowledge].
274277// ----------------------------------------------------------------------------
275278
279+ // private
280+ bool protocol_version_31402::is_disallowed_deviation (
281+ uint64_t timestamp) NOEXCEPT
282+ {
283+ const auto now = wall_clock::now ();
284+ const auto time = wall_clock::from_time_t (timestamp);
285+ return time < (now - allowed_timestamp_deviation)
286+ || time > (now + allowed_timestamp_deviation);
287+ }
288+
276289bool protocol_version_31402::handle_receive_version (const code& ec,
277290 const version::cptr& message) NOEXCEPT
278291{
@@ -320,6 +333,15 @@ bool protocol_version_31402::handle_receive_version(const code& ec,
320333 return false ;
321334 }
322335
336+ if (is_disallowed_deviation (message->timestamp ))
337+ {
338+ LOGP (" Timestamp out of range (" << message->value << " ) "
339+ " for [" << authority () << " ]." );
340+
341+ rejection (error::peer_timestamp);
342+ return false ;
343+ }
344+
323345 const auto version = std::min (message->value , maximum_version_);
324346 set_negotiated_version (version);
325347 set_peer_version (message);
0 commit comments