Browse Source

avoid connection to self

pull/110/head
Ethan Buchman 9 years ago
parent
commit
af5b763112
3 changed files with 15 additions and 4 deletions
  1. +5
    -4
      p2p/pex_reactor.go
  2. +6
    -0
      p2p/switch.go
  3. +4
    -0
      p2p/switch_test.go

+ 5
- 4
p2p/pex_reactor.go View File

@ -212,10 +212,11 @@ func (pexR *PEXReactor) ensurePeers() {
// if no addresses to dial, pick a random connected peer and ask for more peers
if toDial.Size() == 0 {
peers := pexR.sw.Peers().List()
i := rand.Int() % len(peers)
log.Debug("No addresses to dial. Sending pexRequest to random peer", "peer", peers[i])
pexR.RequestPEX(peers[i])
if peers := pexR.sw.Peers().List(); len(peers) > 0 {
i := rand.Int() % len(peers)
log.Debug("No addresses to dial. Sending pexRequest to random peer", "peer", peers[i])
pexR.RequestPEX(peers[i])
}
}
}


+ 6
- 0
p2p/switch.go View File

@ -171,10 +171,16 @@ func (sw *Switch) AddPeerWithConnection(conn net.Conn, outbound bool) (*Peer, er
conn.Close()
return nil, err
}
// check version, chain id
if err := sw.nodeInfo.CompatibleWith(peerNodeInfo); err != nil {
conn.Close()
return nil, err
}
// avoid self
if peerNodeInfo.UUID == sw.nodeInfo.UUID {
conn.Close()
return nil, fmt.Errorf("Ignoring connection from self")
}
// the peerNodeInfo is not verified,
// so we overwrite the IP with that from the conn


+ 4
- 0
p2p/switch_test.go View File

@ -9,6 +9,8 @@ import (
"github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common"
"github.com/tendermint/tendermint/types"
"github.com/tendermint/tendermint/Godeps/_workspace/src/code.google.com/p/go-uuid/uuid"
)
type PeerMessage struct {
@ -78,12 +80,14 @@ func makeSwitchPair(t testing.TB, initSwitch func(*Switch) *Switch) (*Switch, *S
Moniker: "switch1",
ChainID: "testing",
Version: "123.123.123",
UUID: uuid.New(),
})
s2 := initSwitch(NewSwitch())
s2.SetNodeInfo(&types.NodeInfo{
Moniker: "switch2",
ChainID: "testing",
Version: "123.123.123",
UUID: uuid.New(),
})
// Start switches


Loading…
Cancel
Save