From 5a2fa71b033c576177060845c49b995af820668a Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Wed, 21 Mar 2018 20:04:22 +0100 Subject: [PATCH] use combination of IP and port, not just IP --- p2p/node_info.go | 7 ------- p2p/switch.go | 34 +++++++++++++++++----------------- 2 files changed, 17 insertions(+), 24 deletions(-) diff --git a/p2p/node_info.go b/p2p/node_info.go index 46c9a1629..346de37d3 100644 --- a/p2p/node_info.go +++ b/p2p/node_info.go @@ -2,7 +2,6 @@ package p2p import ( "fmt" - "net" "strings" crypto "github.com/tendermint/go-crypto" @@ -113,12 +112,6 @@ func (info NodeInfo) ID() ID { return PubKeyToID(info.PubKey) } -// IP returns node listen addr's IP. -func (info NodeInfo) IP() net.IP { - hostPort := strings.SplitN(info.ListenAddr, ":", 2) - return net.ParseIP(hostPort[0]) -} - // NetAddress returns a NetAddress derived from the NodeInfo - // it includes the authenticated peer ID and the self-reported // ListenAddr. Note that the ListenAddr is not authenticated and diff --git a/p2p/switch.go b/p2p/switch.go index f50cab802..d0e6df721 100644 --- a/p2p/switch.go +++ b/p2p/switch.go @@ -48,18 +48,18 @@ type AddrBook interface { type Switch struct { cmn.BaseService - config *cfg.P2PConfig - peerConfig *PeerConfig - listeners []Listener - reactors map[string]Reactor - chDescs []*conn.ChannelDescriptor - reactorsByCh map[byte]Reactor - peers *PeerSet - dialing *cmn.CMap - nodeInfo NodeInfo // our node info - nodeKey *NodeKey // our node privkey - nodeIP net.IP // our IP - addrBook AddrBook + config *cfg.P2PConfig + peerConfig *PeerConfig + listeners []Listener + reactors map[string]Reactor + chDescs []*conn.ChannelDescriptor + reactorsByCh map[byte]Reactor + peers *PeerSet + dialing *cmn.CMap + nodeInfo NodeInfo // our node info + nodeKey *NodeKey // our node privkey + nodePublicAddr *NetAddress + addrBook AddrBook filterConnByAddr func(net.Addr) error filterConnByID func(ID) error @@ -149,7 +149,7 @@ func (sw *Switch) IsListening() bool { // NOTE: Not goroutine safe. func (sw *Switch) SetNodeInfo(nodeInfo NodeInfo) { sw.nodeInfo = nodeInfo - sw.nodeIP = nodeInfo.IP() + sw.nodePublicAddr = nodeInfo.NetAddress() } // NodeInfo returns the switch's NodeInfo. @@ -384,7 +384,7 @@ func (sw *Switch) DialPeersAsync(addrBook AddrBook, peers []string, persistent b // If `persistent == true`, the switch will always try to reconnect to this peer if the connection ever fails. func (sw *Switch) DialPeerWithAddress(addr *NetAddress, persistent bool) error { // do not dial ourselves - if addr.IP == sw.nodeIP { + if addr.Same(sw.nodePublicAddr) { return ErrSwitchConnectToSelf } @@ -529,9 +529,9 @@ func (sw *Switch) addPeer(pc peerConn) error { // Avoid self if sw.nodeKey.ID() == peerID { - // overwrite current IP to avoid dialing ourselves again - // it means original nodeIP was different from public one - sw.nodeIP = peerNodeInfo.IP() + // overwrite current addr to avoid dialing ourselves again + // it means original nodePublicAddr was different from public one + sw.nodePublicAddr = peerNodeInfo.NetAddress() return ErrSwitchConnectToSelf }