diff --git a/pkg/network/address.go b/pkg/network/address.go index 4c459357d..b26d47232 100644 --- a/pkg/network/address.go +++ b/pkg/network/address.go @@ -83,6 +83,13 @@ func multiaddrStringFromHostAddr(host string) (string, error) { 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 ( prefix = "/dns4" addr = endpoint diff --git a/pkg/network/address_test.go b/pkg/network/address_test.go index a838871ee..51b06d74c 100644 --- a/pkg/network/address_test.go +++ b/pkg/network/address_test.go @@ -29,6 +29,26 @@ func TestAddress_NetAddr(t *testing.T) { 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) { t.Run("valid addresses", func(t *testing.T) { testcases := []struct {