diff --git a/connection_maker.go b/connection_maker.go index 77845ce8..a9d6edf4 100644 --- a/connection_maker.go +++ b/connection_maker.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "math/rand" + "net" "time" ) @@ -42,7 +43,10 @@ func (cm *ConnectionMaker) InitiateConnection(address string) { } func (cm *ConnectionMaker) EnsureConnection(address string) { - address = StripPortFromAddr(address) + // If we've been given a port number, take it off + if addrHost, _, err := net.SplitHostPort(address); err == nil { + address = addrHost + } cm.queryChan <- &ConnectionMakerInteraction{ Interaction: Interaction{code: CMEnsure}, acceptAnyPeer: false, @@ -50,7 +54,10 @@ func (cm *ConnectionMaker) EnsureConnection(address string) { } func (cm *ConnectionMaker) ConnectionEstablished(address string) { - address = StripPortFromAddr(address) + // If we've been given a port number, take it off + if addrHost, _, err := net.SplitHostPort(address); err == nil { + address = addrHost + } cm.queryChan <- &ConnectionMakerInteraction{ Interaction: Interaction{code: CMEstablished}, address: address} diff --git a/peer.go b/peer.go index b09771ee..9bdd3968 100644 --- a/peer.go +++ b/peer.go @@ -57,8 +57,19 @@ func (peer *Peer) ConnectionTo(name PeerName) (Connection, bool) { return conn, found // yes, you really can't inline that. FFS. } +// given an address like '1.2.3.4:567', return the address if it has a port, +// otherwise return the address with weave's standard port number +func normalisePeerAddr(peerAddr string) string { + _, _, err := net.SplitHostPort(peerAddr) + if err == nil { + return peerAddr + } else { + return fmt.Sprintf("%s:%d", peerAddr, Port) + } +} + func (peer *Peer) ConnectionOn(peerAddr string) (Connection, bool) { - peerAddr = NormalisePeerAddr(peerAddr) + peerAddr = normalisePeerAddr(peerAddr) peer.RLock() defer peer.RUnlock() for _, conn := range peer.connections { @@ -209,7 +220,7 @@ func (peer *Peer) CreateConnection(peerAddr string, acceptNewPeer bool) error { return err } // We're dialing the remote so that means connections will come from random ports - addrStr := NormalisePeerAddr(peerAddr) + addrStr := normalisePeerAddr(peerAddr) tcpAddr, tcpErr := net.ResolveTCPAddr("tcp4", addrStr) udpAddr, udpErr := net.ResolveUDPAddr("udp4", addrStr) if tcpErr != nil || udpErr != nil { diff --git a/utils.go b/utils.go index 8d9a54bc..22d7235b 100644 --- a/utils.go +++ b/utils.go @@ -97,22 +97,3 @@ func (lop ListOfPeers) Swap(i, j int) { func (lop ListOfPeers) Less(i, j int) bool { return lop[i].Name < lop[i].Name } - -func StripPortFromAddr(address string) string { - // If we've been given a port number, take it off - if addrHost, _, err := net.SplitHostPort(address); err == nil { - return addrHost - } - return address -} - -// given an address like '1.2.3.4:567', return the address if it has a port, -// otherwise return the address with weave's standard port number -func NormalisePeerAddr(peerAddr string) string { - _, _, err := net.SplitHostPort(peerAddr) - if err == nil { - return peerAddr - } else { - return fmt.Sprintf("%s:%d", peerAddr, Port) - } -}