From 8d8d9eccbd241e4c4546a4bb1b16ce0f0e0c13b8 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Mon, 17 May 2021 16:05:28 +0300 Subject: [PATCH] [#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 --- pkg/network/address.go | 7 +++++++ pkg/network/address_test.go | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/pkg/network/address.go b/pkg/network/address.go index 4c459357..b26d4723 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 a838871e..51b06d74 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 {