use combination of IP and port, not just IP
This commit is contained in:
parent
9a57ef9cbf
commit
5a2fa71b03
|
@ -2,7 +2,6 @@ package p2p
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
crypto "github.com/tendermint/go-crypto"
|
crypto "github.com/tendermint/go-crypto"
|
||||||
|
@ -113,12 +112,6 @@ func (info NodeInfo) ID() ID {
|
||||||
return PubKeyToID(info.PubKey)
|
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 -
|
// NetAddress returns a NetAddress derived from the NodeInfo -
|
||||||
// it includes the authenticated peer ID and the self-reported
|
// it includes the authenticated peer ID and the self-reported
|
||||||
// ListenAddr. Note that the ListenAddr is not authenticated and
|
// ListenAddr. Note that the ListenAddr is not authenticated and
|
||||||
|
|
|
@ -48,18 +48,18 @@ type AddrBook interface {
|
||||||
type Switch struct {
|
type Switch struct {
|
||||||
cmn.BaseService
|
cmn.BaseService
|
||||||
|
|
||||||
config *cfg.P2PConfig
|
config *cfg.P2PConfig
|
||||||
peerConfig *PeerConfig
|
peerConfig *PeerConfig
|
||||||
listeners []Listener
|
listeners []Listener
|
||||||
reactors map[string]Reactor
|
reactors map[string]Reactor
|
||||||
chDescs []*conn.ChannelDescriptor
|
chDescs []*conn.ChannelDescriptor
|
||||||
reactorsByCh map[byte]Reactor
|
reactorsByCh map[byte]Reactor
|
||||||
peers *PeerSet
|
peers *PeerSet
|
||||||
dialing *cmn.CMap
|
dialing *cmn.CMap
|
||||||
nodeInfo NodeInfo // our node info
|
nodeInfo NodeInfo // our node info
|
||||||
nodeKey *NodeKey // our node privkey
|
nodeKey *NodeKey // our node privkey
|
||||||
nodeIP net.IP // our IP
|
nodePublicAddr *NetAddress
|
||||||
addrBook AddrBook
|
addrBook AddrBook
|
||||||
|
|
||||||
filterConnByAddr func(net.Addr) error
|
filterConnByAddr func(net.Addr) error
|
||||||
filterConnByID func(ID) error
|
filterConnByID func(ID) error
|
||||||
|
@ -149,7 +149,7 @@ func (sw *Switch) IsListening() bool {
|
||||||
// NOTE: Not goroutine safe.
|
// NOTE: Not goroutine safe.
|
||||||
func (sw *Switch) SetNodeInfo(nodeInfo NodeInfo) {
|
func (sw *Switch) SetNodeInfo(nodeInfo NodeInfo) {
|
||||||
sw.nodeInfo = nodeInfo
|
sw.nodeInfo = nodeInfo
|
||||||
sw.nodeIP = nodeInfo.IP()
|
sw.nodePublicAddr = nodeInfo.NetAddress()
|
||||||
}
|
}
|
||||||
|
|
||||||
// NodeInfo returns the switch's NodeInfo.
|
// 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.
|
// 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 {
|
func (sw *Switch) DialPeerWithAddress(addr *NetAddress, persistent bool) error {
|
||||||
// do not dial ourselves
|
// do not dial ourselves
|
||||||
if addr.IP == sw.nodeIP {
|
if addr.Same(sw.nodePublicAddr) {
|
||||||
return ErrSwitchConnectToSelf
|
return ErrSwitchConnectToSelf
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -529,9 +529,9 @@ func (sw *Switch) addPeer(pc peerConn) error {
|
||||||
|
|
||||||
// Avoid self
|
// Avoid self
|
||||||
if sw.nodeKey.ID() == peerID {
|
if sw.nodeKey.ID() == peerID {
|
||||||
// overwrite current IP to avoid dialing ourselves again
|
// overwrite current addr to avoid dialing ourselves again
|
||||||
// it means original nodeIP was different from public one
|
// it means original nodePublicAddr was different from public one
|
||||||
sw.nodeIP = peerNodeInfo.IP()
|
sw.nodePublicAddr = peerNodeInfo.NetAddress()
|
||||||
|
|
||||||
return ErrSwitchConnectToSelf
|
return ErrSwitchConnectToSelf
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue