diff --git a/swarm_dial.go b/swarm_dial.go index 4dbb1f2dd1c99279cca72a0a1f7cd127e5f67e73..30a9ab06cb9193ba958fb4ca58bd16eef3af0210 100644 --- a/swarm_dial.go +++ b/swarm_dial.go @@ -440,7 +440,7 @@ func (s *Swarm) filterKnownUndialables(p peer.ID, addrs []ma.Multiaddr) []ma.Mul for _, addr := range lisAddrs { protos := addr.Protocols() // we're only sure about filtering out /ip4 and /ip6 addresses, so far - if len(protos) == 2 && (protos[0].Code == ma.P_IP4 || protos[0].Code == ma.P_IP6) { + if protos[0].Code == ma.P_IP4 || protos[0].Code == ma.P_IP6 { ourAddrs = append(ourAddrs, addr) } } diff --git a/swarm_test.go b/swarm_test.go index bd2c844fca1084b2e4266c27493c7fa532e086ad..9b1e9c42d733c681fe0b5aba103fcf447423b0ee 100644 --- a/swarm_test.go +++ b/swarm_test.go @@ -3,8 +3,10 @@ package swarm_test import ( "bytes" "context" + "errors" "fmt" "io" + "strings" "sync" "testing" "time" @@ -20,6 +22,7 @@ import ( logging "github.com/ipfs/go-log" ma "github.com/multiformats/go-multiaddr" + manet "github.com/multiformats/go-multiaddr/net" "github.com/stretchr/testify/require" ) @@ -457,3 +460,31 @@ func TestCloseWithOpenStreams(t *testing.T) { t.Fatal(err) } } + +func TestPreventDialListenAddr(t *testing.T) { + s := GenSwarm(t, context.Background(), OptDialOnly) + if err := s.Listen(ma.StringCast("/ip4/0.0.0.0/udp/0/quic")); err != nil { + t.Fatal(err) + } + addrs, err := s.InterfaceListenAddresses() + if err != nil { + t.Fatal(err) + } + var addr ma.Multiaddr + for _, a := range addrs { + _, s, err := manet.DialArgs(a) + if err != nil { + t.Fatal(err) + } + if strings.Split(s, ":")[0] == "127.0.0.1" { + addr = a + break + } + } + remote := peer.ID("foobar") + s.Peerstore().AddAddr(remote, addr, time.Hour) + _, err = s.DialPeer(context.Background(), remote) + if !errors.Is(err, ErrNoGoodAddresses) { + t.Fatal("expected dial to fail: %w", err) + } +}