[#522] network: Fix issue with empty endpoint in multiaddr

Before fix `:8080` host address was parsed as `/dns4/tcp/8080`
multiaddress. However such multiaddress is not correct. In this
case `dns4` section should be omitted, but it breaks `manet.DialArgs`.

To solve this issue we explicitly set 0.0.0.0 address.

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2021-05-17 16:05:28 +03:00 committed by Alex Vanin
parent 16f13bc0a5
commit 8d8d9eccbd
2 changed files with 27 additions and 0 deletions

View file

@ -83,6 +83,13 @@ func multiaddrStringFromHostAddr(host string) (string, error) {
return "", err return "", err
} }
// Empty address in host `:8080` generates `/dns4//tcp/8080` multiaddr
// which is invalid. It could be `/tcp/8080` but this breaks
// `manet.DialArgs`. The solution is to manually parse it as 0.0.0.0
if endpoint == "" {
return "/ip4/0.0.0.0/tcp/" + port, nil
}
var ( var (
prefix = "/dns4" prefix = "/dns4"
addr = endpoint addr = endpoint

View file

@ -29,6 +29,26 @@ func TestAddress_NetAddr(t *testing.T) {
require.Equal(t, ip+":"+port, netAddr) require.Equal(t, ip+":"+port, netAddr)
} }
func TestAddressFromString(t *testing.T) {
t.Run("valid addresses", func(t *testing.T) {
testcases := []struct {
inp string
exp multiaddr.Multiaddr
}{
{":8080", buildMultiaddr("/ip4/0.0.0.0/tcp/8080", t)},
{"example.com:7070", buildMultiaddr("/dns4/example.com/tcp/7070", t)},
{"213.44.87.1:32512", buildMultiaddr("/ip4/213.44.87.1/tcp/32512", t)},
{"[2004:eb1::1]:8080", buildMultiaddr("/ip6/2004:eb1::1/tcp/8080", t)},
}
for _, testcase := range testcases {
addr, err := AddressFromString(testcase.inp)
require.NoError(t, err)
require.Equal(t, testcase.exp, addr.ma, testcase.inp)
}
})
}
func TestAddress_HostAddrString(t *testing.T) { func TestAddress_HostAddrString(t *testing.T) {
t.Run("valid addresses", func(t *testing.T) { t.Run("valid addresses", func(t *testing.T) {
testcases := []struct { testcases := []struct {