Browse Source

Fixes for uninit. shared pointers and add const&.

cl-refactor
subtly 10 years ago
parent
commit
a406402a4c
  1. 29
      libp2p/Host.cpp
  2. 64
      libp2p/NodeTable.cpp
  3. 8
      libp2p/NodeTable.h

29
libp2p/Host.cpp

@ -185,15 +185,24 @@ void Host::onNodeTableEvent(NodeId const& _n, NodeTableEventType const& _e)
auto n = m_nodeTable->node(_n); auto n = m_nodeTable->node(_n);
if (n) if (n)
{ {
RecursiveGuard l(x_sessions); shared_ptr<Peer> p;
auto p = m_peers[_n];
if (!p)
{ {
m_peers[_n] = make_shared<Peer>(); RecursiveGuard l(x_sessions);
p = m_peers[_n]; if (m_peers.count(_n))
p->id = _n; p = m_peers[_n];
else
{
// TODO p2p: construct peer from node
p.reset(new Peer());
p->id = _n;
p->endpoint = NodeIPEndpoint(n.endpoint.udp, n.endpoint.tcp);
p->required = n.required;
m_peers[_n] = p;
clog(NetNote) << "p2p.host.peers.events.peersAdded " << _n << p->endpoint.tcp.address() << p->endpoint.udp.address();
}
p->endpoint.tcp = n.endpoint.tcp;
} }
p->endpoint.tcp = n.endpoint.tcp;
// TODO: Implement similar to discover. Attempt connecting to nodes // TODO: Implement similar to discover. Attempt connecting to nodes
// until ideal peer count is reached; if all nodes are tried, // until ideal peer count is reached; if all nodes are tried,
@ -369,10 +378,8 @@ void Host::doHandshake(bi::tcp::socket* _socket, NodeId _nodeId)
p = m_peers[_nodeId]; p = m_peers[_nodeId];
if (!p) if (!p)
{ p.reset(new Peer());
p = make_shared<Peer>(); p->endpoint.tcp.address(_socket->remote_endpoint().address());
p->endpoint.tcp.address(_socket->remote_endpoint().address());
}
auto ps = std::make_shared<Session>(this, std::move(*_socket), p); auto ps = std::make_shared<Session>(this, std::move(*_socket), p);
ps->start(); ps->start();

64
libp2p/NodeTable.cpp

@ -70,28 +70,37 @@ shared_ptr<NodeEntry> NodeTable::addNode(Public const& _pubk, bi::udp::endpoint
shared_ptr<NodeEntry> NodeTable::addNode(Node const& _node) shared_ptr<NodeEntry> NodeTable::addNode(Node const& _node)
{ {
// ping address if nodeid is empty
if (!_node.id)
{
PingNode p(_node.endpoint.udp, m_node.endpoint.udp.address().to_string(), m_node.endpoint.udp.port());
p.sign(m_secret);
m_socketPointer->send(p);
shared_ptr<NodeEntry> n;
return move(n);
}
Guard l(x_nodes); Guard l(x_nodes);
shared_ptr<NodeEntry> ret = m_nodes[_node.id]; if (m_nodes.count(_node.id))
if (ret)
{ {
// TODO: p2p robust percolation of node-endpoint changes
// // SECURITY: remove this in beta - it's only for lazy connections and presents an easy attack vector. // // SECURITY: remove this in beta - it's only for lazy connections and presents an easy attack vector.
// if (m_server->m_peers.count(id) && isPrivateAddress(m_server->m_peers.at(id)->address.address()) && ep.port() != 0) // if (m_server->m_peers.count(id) && isPrivateAddress(m_server->m_peers.at(id)->address.address()) && ep.port() != 0)
// // Update address if the node if we now have a public IP for it. // // Update address if the node if we now have a public IP for it.
// m_server->m_peers[id]->address = ep; // m_server->m_peers[id]->address = ep;
return m_nodes[_node.id];
} }
else
{ shared_ptr<NodeEntry> ret(new NodeEntry(m_node, _node.id, NodeIPEndpoint(_node.endpoint.udp, _node.endpoint.tcp)));
clog(NodeTableNote) << "p2p.nodes.add " << _node.id.abridged(); m_nodes[_node.id] = ret;
if (m_nodeEventHandler) PingNode p(_node.endpoint.udp, m_node.endpoint.udp.address().to_string(), m_node.endpoint.udp.port());
m_nodeEventHandler->appendEvent(_node.id, NodeEntryAdded); p.sign(m_secret);
m_socketPointer->send(p);
ret.reset(new NodeEntry(m_node, _node.id, NodeIPEndpoint(_node.endpoint.udp, _node.endpoint.tcp)));
m_nodes[_node.id] = ret; // TODO p2p: rename to p2p.nodes.pending, add p2p.nodes.add event (when pong is received)
PingNode p(_node.endpoint.udp, m_node.endpoint.udp.address().to_string(), m_node.endpoint.udp.port()); clog(NodeTableNote) << "p2p.nodes.add " << _node.id.abridged();
p.sign(m_secret); if (m_nodeEventHandler)
m_socketPointer->send(p); m_nodeEventHandler->appendEvent(_node.id, NodeEntryAdded);
}
return move(ret); return move(ret);
} }
@ -119,18 +128,23 @@ list<NodeEntry> NodeTable::snapshot() const
return move(ret); return move(ret);
} }
Node NodeTable::node(NodeId _id) Node NodeTable::node(NodeId const& _id)
{ {
// TODO p2p: eloquent copy operator
Guard l(x_nodes); Guard l(x_nodes);
auto n = m_nodes[_id]; if (m_nodes.count(_id))
return !!n ? *n : Node(); {
auto entry = m_nodes[_id];
Node n(_id, NodeIPEndpoint(entry->endpoint.udp, entry->endpoint.tcp), entry->required);
return move(n);
}
return move(Node());
} }
shared_ptr<NodeEntry> NodeTable::nodeEntry(NodeId _id) shared_ptr<NodeEntry> NodeTable::nodeEntry(NodeId _id)
{ {
Guard l(x_nodes); Guard l(x_nodes);
auto n = m_nodes[_id]; return m_nodes.count(_id) ? move(m_nodes[_id]) : move(shared_ptr<NodeEntry>());
return !!n ? move(n) : move(shared_ptr<NodeEntry>());
} }
void NodeTable::discover(NodeId _node, unsigned _round, shared_ptr<set<shared_ptr<NodeEntry>>> _tried) void NodeTable::discover(NodeId _node, unsigned _round, shared_ptr<set<shared_ptr<NodeEntry>>> _tried)
@ -289,7 +303,9 @@ void NodeTable::noteActiveNode(Public const& _pubk, bi::udp::endpoint const& _en
if (_pubk == m_node.address()) if (_pubk == m_node.address())
return; return;
shared_ptr<NodeEntry> node(addNode(_pubk, _endpoint)); clog(NodeTableNote) << "Noting active node:" << _pubk.abridged() << _endpoint.address().to_string() << ":" << _endpoint.port();
shared_ptr<NodeEntry> node(addNode(_pubk, _endpoint, bi::tcp::endpoint(_endpoint.address(), _endpoint.port())));
// TODO p2p: old bug (maybe gone now) sometimes node is nullptr here // TODO p2p: old bug (maybe gone now) sometimes node is nullptr here
if (!!node) if (!!node)
@ -471,8 +487,8 @@ void NodeTable::doCheckEvictions(boost::system::error_code const& _ec)
Guard le(x_evictions); Guard le(x_evictions);
for (auto& e: m_evictions) for (auto& e: m_evictions)
if (chrono::steady_clock::now() - e.first.second > c_reqTimeout) if (chrono::steady_clock::now() - e.first.second > c_reqTimeout)
if (auto n = m_nodes[e.second]) if (m_nodes.count(e.second))
drop.push_back(n); drop.push_back(m_nodes[e.second]);
evictionsRemain = m_evictions.size() - drop.size() > 0; evictionsRemain = m_evictions.size() - drop.size() > 0;
} }
@ -498,6 +514,7 @@ void NodeTable::doRefreshBuckets(boost::system::error_code const& _ec)
Guard l(x_state); Guard l(x_state);
for (auto& d: m_state) for (auto& d: m_state)
if (chrono::steady_clock::now() - d.modified > c_bucketRefresh) if (chrono::steady_clock::now() - d.modified > c_bucketRefresh)
{
while (!d.nodes.empty()) while (!d.nodes.empty())
{ {
auto n = d.nodes.front(); auto n = d.nodes.front();
@ -509,6 +526,7 @@ void NodeTable::doRefreshBuckets(boost::system::error_code const& _ec)
} }
d.nodes.pop_front(); d.nodes.pop_front();
} }
}
} }
unsigned nextRefresh = connected ? (refreshed ? 200 : c_bucketRefresh.count()*1000) : 10000; unsigned nextRefresh = connected ? (refreshed ? 200 : c_bucketRefresh.count()*1000) : 10000;

8
libp2p/NodeTable.h

@ -115,6 +115,7 @@ inline std::ostream& operator<<(std::ostream& _out, NodeTable const& _nodeTable)
* @todo cache Ping and FindSelf * @todo cache Ping and FindSelf
* *
* [Networking] * [Networking]
* @todo node-endpoint updates
* @todo TCP endpoints * @todo TCP endpoints
* @todo eth/upnp/natpmp/stun/ice/etc for public-discovery * @todo eth/upnp/natpmp/stun/ice/etc for public-discovery
* @todo firewall * @todo firewall
@ -144,7 +145,7 @@ public:
void processEvents(); void processEvents();
/// Add node. Node will be pinged if it's not already known. /// Add node. Node will be pinged if it's not already known.
std::shared_ptr<NodeEntry> addNode(Public const& _pubk, bi::udp::endpoint const& _udp, bi::tcp::endpoint const& _tcp = bi::tcp::endpoint()); std::shared_ptr<NodeEntry> addNode(Public const& _pubk, bi::udp::endpoint const& _udp, bi::tcp::endpoint const& _tcp);
/// Add node. Node will be pinged if it's not already known. /// Add node. Node will be pinged if it's not already known.
std::shared_ptr<NodeEntry> addNode(Node const& _node); std::shared_ptr<NodeEntry> addNode(Node const& _node);
@ -162,10 +163,10 @@ public:
std::list<NodeEntry> snapshot() const; std::list<NodeEntry> snapshot() const;
/// Returns true if node id is in node table. /// Returns true if node id is in node table.
bool haveNode(NodeId _id) { Guard l(x_nodes); return m_nodes.count(_id); } bool haveNode(NodeId const& _id) { Guard l(x_nodes); return m_nodes.count(_id); }
/// Returns the Node to the corresponding node id or the empty Node if that id is not found. /// Returns the Node to the corresponding node id or the empty Node if that id is not found.
Node node(NodeId _id); Node node(NodeId const& _id);
#ifndef BOOST_AUTO_TEST_SUITE #ifndef BOOST_AUTO_TEST_SUITE
private: private:
@ -197,6 +198,7 @@ protected:
unsigned distance; unsigned distance;
TimePoint modified; TimePoint modified;
std::list<std::weak_ptr<NodeEntry>> nodes; std::list<std::weak_ptr<NodeEntry>> nodes;
void touch() { modified = std::chrono::steady_clock::now(); }
}; };
/// Used to ping endpoint. /// Used to ping endpoint.

Loading…
Cancel
Save