diff --git a/libp2p/NodeTable.cpp b/libp2p/NodeTable.cpp index 154032b6d..24380a10e 100644 --- a/libp2p/NodeTable.cpp +++ b/libp2p/NodeTable.cpp @@ -485,7 +485,7 @@ void NodeTable::onReceived(UDPSocketFace*, bi::udp::endpoint const& _from, bytes case FindNode::type: { FindNode in = FindNode::fromBytesConstRef(_from, rlpBytes); - if (RLPXDatagramFace::secondsSinceEpoch() - in.ts > 3) + if (RLPXDatagramFace::secondsSinceEpoch() > in.ts) { clog(NodeTableTriviaSummary) << "Received expired FindNode from " << _from.address().to_string() << ":" << _from.port(); return; @@ -509,7 +509,7 @@ void NodeTable::onReceived(UDPSocketFace*, bi::udp::endpoint const& _from, bytes PingNode in = PingNode::fromBytesConstRef(_from, rlpBytes); if (in.version != dev::p2p::c_protocolVersion) return; - if (RLPXDatagramFace::secondsSinceEpoch() - in.ts > 3) + if (RLPXDatagramFace::secondsSinceEpoch() > in.ts) { clog(NodeTableTriviaSummary) << "Received expired PingNode from " << _from.address().to_string() << ":" << _from.port(); return; diff --git a/libp2p/NodeTable.h b/libp2p/NodeTable.h index 819ea3ba5..e4eddb957 100644 --- a/libp2p/NodeTable.h +++ b/libp2p/NodeTable.h @@ -300,7 +300,8 @@ struct InvalidRLP: public Exception {}; struct PingNode: RLPXDatagram { /// Constructor used for sending PingNode. - PingNode(NodeIPEndpoint _src, NodeIPEndpoint _dest): RLPXDatagram(_dest), source(_src), destination(_dest), ts(secondsSinceEpoch()) {} + PingNode(NodeIPEndpoint _src, NodeIPEndpoint _dest): RLPXDatagram(_dest), source(_src), destination(_dest), ts(futureFromEpoch(std::chrono::seconds(60))) {} + /// Constructor used to create empty PingNode for parsing inbound packets. PingNode(bi::udp::endpoint _ep): RLPXDatagram(_ep), source(UnspecifiedNodeIPEndpoint), destination(UnspecifiedNodeIPEndpoint) {} @@ -321,7 +322,7 @@ struct PingNode: RLPXDatagram struct Pong: RLPXDatagram { Pong(bi::udp::endpoint const& _ep): RLPXDatagram(_ep), destination(UnspecifiedNodeIPEndpoint) {} - Pong(NodeIPEndpoint const& _dest): RLPXDatagram((bi::udp::endpoint)_dest), destination(_dest), ts(secondsSinceEpoch()) {} + Pong(NodeIPEndpoint const& _dest): RLPXDatagram((bi::udp::endpoint)_dest), destination(_dest), ts(futureFromEpoch(std::chrono::seconds(60))) {} static const uint8_t type = 2; @@ -348,7 +349,7 @@ struct Pong: RLPXDatagram struct FindNode: RLPXDatagram { FindNode(bi::udp::endpoint _ep): RLPXDatagram(_ep) {} - FindNode(bi::udp::endpoint _ep, NodeId _target): RLPXDatagram(_ep), target(_target), ts(secondsSinceEpoch()) {} + FindNode(bi::udp::endpoint _ep, NodeId _target): RLPXDatagram(_ep), target(_target), ts(futureFromEpoch(std::chrono::seconds(60))) {} static const uint8_t type = 3; @@ -374,7 +375,7 @@ struct Neighbours: RLPXDatagram }; Neighbours(bi::udp::endpoint _ep): RLPXDatagram(_ep), ts(secondsSinceEpoch()) {} - Neighbours(bi::udp::endpoint _to, std::vector> const& _nearest, unsigned _offset = 0, unsigned _limit = 0): RLPXDatagram(_to), ts(secondsSinceEpoch()) + Neighbours(bi::udp::endpoint _to, std::vector> const& _nearest, unsigned _offset = 0, unsigned _limit = 0): RLPXDatagram(_to), ts(futureFromEpoch(std::chrono::seconds(60))) { auto limit = _limit ? std::min(_nearest.size(), (size_t)(_offset + _limit)) : _nearest.size(); for (auto i = _offset; i < limit; i++) diff --git a/libp2p/UDP.h b/libp2p/UDP.h index ee9875cf7..97136ee43 100644 --- a/libp2p/UDP.h +++ b/libp2p/UDP.h @@ -61,6 +61,7 @@ protected: */ struct RLPXDatagramFace: public UDPDatagram { + static uint32_t futureFromEpoch(std::chrono::seconds _sec) { return std::chrono::duration_cast((std::chrono::system_clock::now() + _sec).time_since_epoch()).count(); } static uint32_t secondsSinceEpoch() { return std::chrono::duration_cast((std::chrono::system_clock::now()).time_since_epoch()).count(); } static Public authenticate(bytesConstRef _sig, bytesConstRef _rlp);