Browse Source

Drop unsolicited neighbours packets. Resolves #1556.

cl-refactor
subtly 10 years ago
parent
commit
08a913921b
  1. 15
      libp2p/NodeTable.cpp
  2. 8
      libp2p/NodeTable.h

15
libp2p/NodeTable.cpp

@ -178,6 +178,7 @@ void NodeTable::discover(NodeId _node, unsigned _round, shared_ptr<set<shared_pt
tried.push_back(r);
FindNode p(r->endpoint.udp, _node);
p.sign(m_secret);
m_findNodeTimout.push_back(make_pair(_node, chrono::steady_clock::now()));
m_socketPointer->send(p);
}
@ -457,6 +458,20 @@ void NodeTable::onReceived(UDPSocketFace*, bi::udp::endpoint const& _from, bytes
case Neighbours::type:
{
bool expected = false;
m_findNodeTimout.remove_if([&](NodeIdTimePoint const& t)
{
if (t.first == nodeid && chrono::steady_clock::now() - t.second < c_reqTimeout)
expected = true;
return t.first == nodeid;
});
if (!expected)
{
clog(NetConnect) << "Dropping unsolicited Neighbours packet from " << _from.address();
break;
}
Neighbours in = Neighbours::fromBytesConstRef(_from, rlpBytes);
for (auto n: in.nodes)
addNode(n.node, bi::udp::endpoint(bi::address::from_string(n.ipAddress), n.port), bi::tcp::endpoint(bi::address::from_string(n.ipAddress), n.port));

8
libp2p/NodeTable.h

@ -131,8 +131,9 @@ class NodeTable: UDPSocketEvents, public std::enable_shared_from_this<NodeTable>
{
friend std::ostream& operator<<(std::ostream& _out, NodeTable const& _nodeTable);
using NodeSocket = UDPSocket<NodeTable, 1280>;
using TimePoint = std::chrono::steady_clock::time_point;
using EvictionTimeout = std::pair<std::pair<NodeId, TimePoint>, NodeId>; ///< First NodeId may be evicted and replaced with second NodeId.
using TimePoint = std::chrono::steady_clock::time_point; ///< Steady time point.
using NodeIdTimePoint = std::pair<NodeId, TimePoint>;
using EvictionTimeout = std::pair<NodeIdTimePoint, NodeId>; ///< First NodeId (NodeIdTimePoint) may be evicted and replaced with second NodeId.
public:
/// Constructor requiring host for I/O, credentials, and IP Address and port to listen on.
@ -271,6 +272,9 @@ private:
Mutex x_pubkDiscoverPings; ///< LOCK x_nodes first if both x_nodes and x_pubkDiscoverPings locks are required.
std::map<bi::address, TimePoint> m_pubkDiscoverPings; ///< List of pending pings where node entry wasn't created due to unkown pubk.
Mutex x_findNodeTimeout;
std::list<NodeIdTimePoint> m_findNodeTimout; ///< Timeouts for pending Ping and FindNode requests.
ba::io_service& m_io; ///< Used by bucket refresh timer.
std::shared_ptr<NodeSocket> m_socket; ///< Shared pointer for our UDPSocket; ASIO requires shared_ptr.
NodeSocket* m_socketPointer; ///< Set to m_socket.get(). Socket is created in constructor and disconnected in destructor to ensure access to pointer is safe.

Loading…
Cancel
Save