Repository: libp2p/go-libp2p
Pull request #2553 (2023-09-03 13:18:47)

identify: don't filter dns addresses based on remote addr type
closes: #2550 

marten-seemann: > I think it makes sense to modify `multiaddr.IsPublicAddr` to return true for `/dns,/dns4/dns6` addresses, but only in cases where the name is not one of the special use dns names defined in [RFC 6761](https://datatracker.ietf.org/doc/html/rfc6761)

Makes sense.

Patch:
diff --git a/go.mod b/go.mod
index a13fb572..3d946bc6 100644
--- a/go.mod
+++ b/go.mod
@@ -35,7 +35,7 @@ require (
 	github.com/minio/sha256-simd v1.0.1
 	github.com/mr-tron/base58 v1.2.0
 	github.com/multiformats/go-base32 v0.1.0
-	github.com/multiformats/go-multiaddr v0.11.0
+	github.com/multiformats/go-multiaddr v0.12.0
 	github.com/multiformats/go-multiaddr-dns v0.3.1
 	github.com/multiformats/go-multiaddr-fmt v0.1.0
 	github.com/multiformats/go-multibase v0.2.0
diff --git a/go.sum b/go.sum
index 475617a8..d2c1324d 100644
--- a/go.sum
+++ b/go.sum
@@ -351,8 +351,8 @@ github.com/multiformats/go-base36 v0.2.0 h1:lFsAbNOGeKtuKozrtBsAkSVhv1p9D0/qedU9
 github.com/multiformats/go-base36 v0.2.0/go.mod h1:qvnKE++v+2MWCfePClUEjE78Z7P2a1UV0xHgWc0hkp4=
 github.com/multiformats/go-multiaddr v0.1.1/go.mod h1:aMKBKNEYmzmDmxfX88/vz+J5IU55txyt0p4aiWVohjo=
 github.com/multiformats/go-multiaddr v0.2.0/go.mod h1:0nO36NvPpyV4QzvTLi/lafl2y95ncPj0vFwVF6k6wJ4=
-github.com/multiformats/go-multiaddr v0.11.0 h1:XqGyJ8ufbCE0HmTDwx2kPdsrQ36AGPZNZX6s6xfJH10=
-github.com/multiformats/go-multiaddr v0.11.0/go.mod h1:gWUm0QLR4thQ6+ZF6SXUw8YjtwQSPapICM+NmCkxHSM=
+github.com/multiformats/go-multiaddr v0.12.0 h1:1QlibTFkoXJuDjjYsMHhE73TnzJQl8FSWatk/0gxGzE=
+github.com/multiformats/go-multiaddr v0.12.0/go.mod h1:WmZXgObOQOYp9r3cslLlppkrz1FYSHmE834dfz/lWu8=
 github.com/multiformats/go-multiaddr-dns v0.3.1 h1:QgQgR+LQVt3NPTjbrLLpsaT2ufAA2y0Mkk+QRVJbW3A=
 github.com/multiformats/go-multiaddr-dns v0.3.1/go.mod h1:G/245BRQ6FJGmryJCrOuTdB37AMA5AMOVuO6NY3JwTk=
 github.com/multiformats/go-multiaddr-fmt v0.1.0 h1:WLEFClPycPkp4fnIzoFoV9FVd49/eQsuaL3/CWe167E=


Test patch:
diff --git a/p2p/protocol/identify/id_glass_test.go b/p2p/protocol/identify/id_glass_test.go
index 777cef01..ff303b23 100644
--- a/p2p/protocol/identify/id_glass_test.go
+++ b/p2p/protocol/identify/id_glass_test.go
@@ -12,6 +12,7 @@ import (
 	recordPb "github.com/libp2p/go-libp2p/core/record/pb"
 	blhost "github.com/libp2p/go-libp2p/p2p/host/blank"
 	swarmt "github.com/libp2p/go-libp2p/p2p/net/swarm/testing"
+	ma "github.com/multiformats/go-multiaddr"
 	"google.golang.org/protobuf/proto"
 
 	"github.com/stretchr/testify/assert"
@@ -173,3 +174,35 @@ func TestInvalidSignedPeerRecord(t *testing.T) {
 	require.True(t, ok)
 	require.Nil(t, cab.GetPeerRecord(h2.ID()))
 }
+
+func TestIncomingAddrFilter(t *testing.T) {
+	lhAddr := ma.StringCast("/ip4/127.0.0.1/udp/123/quic-v1")
+	privAddr := ma.StringCast("/ip4/192.168.1.101/tcp/123")
+	pubAddr := ma.StringCast("/ip6/2::1/udp/123/quic-v1")
+	pubDNSAddr := ma.StringCast("/dns/example.com/udp/123/quic-v1")
+	privDNSAddr := ma.StringCast("/dns4/localhost/udp/123/quic-v1")
+	tests := []struct {
+		output []ma.Multiaddr
+		remote ma.Multiaddr
+	}{
+		{
+			output: []ma.Multiaddr{lhAddr, privAddr, pubAddr, pubDNSAddr, privDNSAddr},
+			remote: lhAddr,
+		},
+		{
+			output: []ma.Multiaddr{privAddr, pubAddr, pubDNSAddr, privDNSAddr},
+			remote: privAddr,
+		},
+		{
+			output: []ma.Multiaddr{pubAddr, pubDNSAddr},
+			remote: pubAddr,
+		},
+	}
+	for _, tc := range tests {
+		t.Run(fmt.Sprintf("remote:%s", tc.remote), func(t *testing.T) {
+			input := []ma.Multiaddr{lhAddr, privAddr, pubAddr, pubDNSAddr, privDNSAddr}
+			got := filterAddrs(input, tc.remote)
+			require.ElementsMatch(t, tc.output, got, "%s\n%s", tc.output, got)
+		})
+	}
+}
diff --git a/p2p/protocol/identify/id_test.go b/p2p/protocol/identify/id_test.go
index 61c7d87a..4adc84b1 100644
--- a/p2p/protocol/identify/id_test.go
+++ b/p2p/protocol/identify/id_test.go
@@ -844,8 +844,10 @@ func TestIncomingIDStreamsTimeout(t *testing.T) {
 func TestOutOfOrderConnectedNotifs(t *testing.T) {
 	h1, err := libp2p.New(libp2p.NoListenAddrs)
 	require.NoError(t, err)
+	defer h1.Close()
 	h2, err := libp2p.New(libp2p.ListenAddrs(ma.StringCast("/ip4/127.0.0.1/udp/0/quic-v1")))
 	require.NoError(t, err)
+	defer h2.Close()
 
 	doneCh := make(chan struct{})
 	errCh := make(chan error)
diff --git a/test-plans/go.mod b/test-plans/go.mod
index 6ac610bc..fa66a93b 100644
--- a/test-plans/go.mod
+++ b/test-plans/go.mod
@@ -5,7 +5,7 @@ go 1.20
 require (
 	github.com/go-redis/redis/v8 v8.11.5
 	github.com/libp2p/go-libp2p v0.0.0
-	github.com/multiformats/go-multiaddr v0.11.0
+	github.com/multiformats/go-multiaddr v0.12.0
 )
 
 require (
diff --git a/test-plans/go.sum b/test-plans/go.sum
index 71e2e1e2..fcc719b5 100644
--- a/test-plans/go.sum
+++ b/test-plans/go.sum
@@ -200,8 +200,8 @@ github.com/multiformats/go-base36 v0.2.0 h1:lFsAbNOGeKtuKozrtBsAkSVhv1p9D0/qedU9
 github.com/multiformats/go-base36 v0.2.0/go.mod h1:qvnKE++v+2MWCfePClUEjE78Z7P2a1UV0xHgWc0hkp4=
 github.com/multiformats/go-multiaddr v0.1.1/go.mod h1:aMKBKNEYmzmDmxfX88/vz+J5IU55txyt0p4aiWVohjo=
 github.com/multiformats/go-multiaddr v0.2.0/go.mod h1:0nO36NvPpyV4QzvTLi/lafl2y95ncPj0vFwVF6k6wJ4=
-github.com/multiformats/go-multiaddr v0.11.0 h1:XqGyJ8ufbCE0HmTDwx2kPdsrQ36AGPZNZX6s6xfJH10=
-github.com/multiformats/go-multiaddr v0.11.0/go.mod h1:gWUm0QLR4thQ6+ZF6SXUw8YjtwQSPapICM+NmCkxHSM=
+github.com/multiformats/go-multiaddr v0.12.0 h1:1QlibTFkoXJuDjjYsMHhE73TnzJQl8FSWatk/0gxGzE=
+github.com/multiformats/go-multiaddr v0.12.0/go.mod h1:WmZXgObOQOYp9r3cslLlppkrz1FYSHmE834dfz/lWu8=
 github.com/multiformats/go-multiaddr-dns v0.3.1 h1:QgQgR+LQVt3NPTjbrLLpsaT2ufAA2y0Mkk+QRVJbW3A=
 github.com/multiformats/go-multiaddr-dns v0.3.1/go.mod h1:G/245BRQ6FJGmryJCrOuTdB37AMA5AMOVuO6NY3JwTk=
 github.com/multiformats/go-multiaddr-fmt v0.1.0 h1:WLEFClPycPkp4fnIzoFoV9FVd49/eQsuaL3/CWe167E=