Browse Source

Merge pull request #979 from tendermint/934-node-fails-to-parse-seeds

strip protocol if defined
pull/992/head
Ethan Buchman 7 years ago
committed by GitHub
parent
commit
652d1e3de8
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 25 deletions
  1. +7
    -7
      p2p/listener.go
  2. +10
    -1
      p2p/netaddress.go
  3. +19
    -17
      p2p/netaddress_test.go

+ 7
- 7
p2p/listener.go View File

@ -38,11 +38,11 @@ const (
func splitHostPort(addr string) (host string, port int) {
host, portStr, err := net.SplitHostPort(addr)
if err != nil {
cmn.PanicSanity(err)
panic(err)
}
port, err = strconv.Atoi(portStr)
if err != nil {
cmn.PanicSanity(err)
panic(err)
}
return host, port
}
@ -64,7 +64,7 @@ func NewDefaultListener(protocol string, lAddr string, skipUPNP bool, logger log
}
}
if err != nil {
cmn.PanicCrisis(err)
panic(err)
}
// Actual listener local IP & port
listenerIP, listenerPort := splitHostPort(listener.Addr().String())
@ -74,7 +74,7 @@ func NewDefaultListener(protocol string, lAddr string, skipUPNP bool, logger log
var intAddr *NetAddress
intAddr, err = NewNetAddressString(lAddr)
if err != nil {
cmn.PanicCrisis(err)
panic(err)
}
// Determine external address...
@ -90,7 +90,7 @@ func NewDefaultListener(protocol string, lAddr string, skipUPNP bool, logger log
extAddr = getNaiveExternalAddress(listenerPort, false, logger)
}
if extAddr == nil {
cmn.PanicCrisis("Could not determine external address!")
panic("Could not determine external address!")
}
dl := &DefaultListener{
@ -132,7 +132,7 @@ func (l *DefaultListener) listenRoutine() {
// listener wasn't stopped,
// yet we encountered an error.
if err != nil {
cmn.PanicCrisis(err)
panic(err)
}
l.connections <- conn
@ -205,7 +205,7 @@ func getUPNPExternalAddress(externalPort, internalPort int, logger log.Logger) *
func getNaiveExternalAddress(port int, settleForLocal bool, logger log.Logger) *NetAddress {
addrs, err := net.InterfaceAddrs()
if err != nil {
cmn.PanicCrisis(cmn.Fmt("Could not fetch interface addresses: %v", err))
panic(cmn.Fmt("Could not fetch interface addresses: %v", err))
}
for _, a := range addrs {


+ 10
- 1
p2p/netaddress.go View File

@ -9,6 +9,7 @@ import (
"fmt"
"net"
"strconv"
"strings"
"time"
cmn "github.com/tendermint/tmlibs/common"
@ -45,7 +46,7 @@ func NewNetAddress(addr net.Addr) *NetAddress {
// address in the form of "IP:Port". Also resolves the host if host
// is not an IP.
func NewNetAddressString(addr string) (*NetAddress, error) {
host, portStr, err := net.SplitHostPort(addr)
host, portStr, err := net.SplitHostPort(removeProtocolIfDefined(addr))
if err != nil {
return nil, err
}
@ -251,3 +252,11 @@ func (na *NetAddress) RFC4843() bool { return rfc4843.Contains(na.IP) }
func (na *NetAddress) RFC4862() bool { return rfc4862.Contains(na.IP) }
func (na *NetAddress) RFC6052() bool { return rfc6052.Contains(na.IP) }
func (na *NetAddress) RFC6145() bool { return rfc6145.Contains(na.IP) }
func removeProtocolIfDefined(addr string) string {
if strings.Contains(addr, "://") {
return strings.Split(addr, "://")[1]
} else {
return addr
}
}

+ 19
- 17
p2p/netaddress_test.go View File

@ -23,29 +23,31 @@ func TestNewNetAddress(t *testing.T) {
}
func TestNewNetAddressString(t *testing.T) {
assert := assert.New(t)
tests := []struct {
addr string
correct bool
testCases := []struct {
addr string
expected string
correct bool
}{
{"127.0.0.1:8080", true},
{"127.0.0.1:8080", "127.0.0.1:8080", true},
{"tcp://127.0.0.1:8080", "127.0.0.1:8080", true},
{"udp://127.0.0.1:8080", "127.0.0.1:8080", true},
{"udp//127.0.0.1:8080", "", false},
// {"127.0.0:8080", false},
{"notahost", false},
{"127.0.0.1:notapath", false},
{"notahost:8080", false},
{"8082", false},
{"127.0.0:8080000", false},
{"notahost", "", false},
{"127.0.0.1:notapath", "", false},
{"notahost:8080", "", false},
{"8082", "", false},
{"127.0.0:8080000", "", false},
}
for _, t := range tests {
addr, err := NewNetAddressString(t.addr)
if t.correct {
if assert.Nil(err, t.addr) {
assert.Equal(t.addr, addr.String())
for _, tc := range testCases {
addr, err := NewNetAddressString(tc.addr)
if tc.correct {
if assert.Nil(t, err, tc.addr) {
assert.Equal(t, tc.expected, addr.String())
}
} else {
assert.NotNil(err, t.addr)
assert.NotNil(t, err, tc.addr)
}
}
}


Loading…
Cancel
Save