Browse Source

Merge pull request #1360 from ethereum/p2p

work in progress
cl-refactor
Gav Wood 10 years ago
parent
commit
cceedcaa38
  1. 7
      libp2p/NodeTable.cpp
  2. 2
      libp2p/NodeTable.h
  3. 12
      libp2p/RLPxHandshake.cpp
  4. 14
      libp2p/RLPxHandshake.h

7
libp2p/NodeTable.cpp

@ -463,6 +463,13 @@ void NodeTable::onReceived(UDPSocketFace*, bi::udp::endpoint const& _from, bytes
case PingNode::type:
{
PingNode in = PingNode::fromBytesConstRef(_from, rlpBytes);
if (in.version != 0x2)
{
if (auto n = nodeEntry(nodeid))
dropNode(n);
return;
}
addNode(nodeid, _from, bi::tcp::endpoint(bi::address::from_string(in.ipAddress), in.port));
Pong p(_from);

2
libp2p/NodeTable.h

@ -315,7 +315,7 @@ struct PingNode: RLPXDatagram<PingNode>
static const uint8_t type = 1;
unsigned version = 1;
unsigned version = 2;
std::string ipAddress;
unsigned port;
unsigned expiration;

12
libp2p/RLPxHandshake.cpp

@ -139,7 +139,7 @@ void RLPXHandshake::error()
void RLPXHandshake::transition(boost::system::error_code _ech)
{
if (_ech || m_nextState == Error)
if (_ech || m_nextState == Error || m_cancel)
return error();
auto self(shared_from_this());
@ -259,4 +259,14 @@ void RLPXHandshake::transition(boost::system::error_code _ech)
}
});
}
m_idleTimer.expires_from_now(c_timeout);
m_idleTimer.async_wait([this, self](boost::system::error_code const& _ec)
{
if (!_ec)
{
clog(NetWarn) << "Disconnecting " << m_socket->remoteEndpoint() << " (Handshake Timeout)";
cancel();
}
});
}

14
libp2p/RLPxHandshake.h

@ -62,17 +62,18 @@ class RLPXHandshake: public std::enable_shared_from_this<RLPXHandshake>
public:
/// Setup incoming connection.
RLPXHandshake(Host* _host, std::shared_ptr<RLPXSocket> const& _socket): m_host(_host), m_originated(false), m_socket(_socket) { crypto::Nonce::get().ref().copyTo(m_nonce.ref()); }
RLPXHandshake(Host* _host, std::shared_ptr<RLPXSocket> const& _socket): m_host(_host), m_originated(false), m_socket(_socket), m_idleTimer(m_socket->ref().get_io_service()) { crypto::Nonce::get().ref().copyTo(m_nonce.ref()); }
/// Setup outbound connection.
RLPXHandshake(Host* _host, std::shared_ptr<RLPXSocket> const& _socket, NodeId _remote): m_host(_host), m_remote(_remote), m_originated(true), m_socket(_socket) { crypto::Nonce::get().ref().copyTo(m_nonce.ref()); }
RLPXHandshake(Host* _host, std::shared_ptr<RLPXSocket> const& _socket, NodeId _remote): m_host(_host), m_remote(_remote), m_originated(true), m_socket(_socket), m_idleTimer(m_socket->ref().get_io_service()) { crypto::Nonce::get().ref().copyTo(m_nonce.ref()); }
~RLPXHandshake() {}
/// Start handshake.
void start() { transition(); }
void cancel() { m_nextState = Error; }
/// Cancels handshake preventing
void cancel() { m_cancel = true; }
protected:
/// Write Auth message to socket and transitions to AckAuth.
@ -93,7 +94,11 @@ protected:
/// Performs transition for m_nextState.
void transition(boost::system::error_code _ech = boost::system::error_code());
/// Timeout for remote to respond to transition events. Enforced by m_idleTimer and refreshed by transition().
boost::posix_time::milliseconds const c_timeout = boost::posix_time::milliseconds(1000);
State m_nextState = New; ///< Current or expected state of transition.
bool m_cancel = false; ///< Will be set to true if connection was canceled.
Host* m_host; ///< Host which provides m_alias, protocolVersion(), m_clientVersion, caps(), and TCP listenPort().
@ -119,7 +124,8 @@ protected:
/// Passed onto Host which will take ownership.
RLPXFrameIO* m_io = nullptr;
std::shared_ptr<RLPXSocket> m_socket; ///< Socket.
std::shared_ptr<RLPXSocket> m_socket; ///< Socket.
boost::asio::deadline_timer m_idleTimer; ///< Timer which enforces c_timeout.
};
}

Loading…
Cancel
Save