network: detect duplicate connections before handshake completes

We can have more than one connection attempt in progress and not yet completed
the handshake, so if there is a Version already received we should look it.
This commit is contained in:
Roman Khimov 2020-01-28 13:54:09 +03:00
parent 48ca0e1126
commit b79abd1e27

View file

@ -350,8 +350,12 @@ func (s *Server) handleVersionCmd(p Peer, version *payload.Version) error {
peerAddr := p.PeerAddr().String() peerAddr := p.PeerAddr().String()
s.lock.RLock() s.lock.RLock()
for peer := range s.peers { for peer := range s.peers {
if p == peer {
continue
}
ver := peer.Version()
// Already connected, drop this connection. // Already connected, drop this connection.
if peer.Handshaked() && peer.PeerAddr().String() == peerAddr && peer.Version().Nonce == version.Nonce { if ver != nil && ver.Nonce == version.Nonce && peer.PeerAddr().String() == peerAddr {
s.lock.RUnlock() s.lock.RUnlock()
return errAlreadyConnected return errAlreadyConnected
} }