Skip to content

Commit

Permalink
swarm: don't dial unspecified addresses (#2560)
Browse files Browse the repository at this point in the history
* swarm: don't dial unspecified addresses

* add test
  • Loading branch information
sukunrt authored Sep 7, 2023
1 parent dce9424 commit 0509445
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions p2p/net/swarm/swarm_dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,14 @@ func (s *Swarm) filterKnownUndialables(p peer.ID, addrs []ma.Multiaddr) (goodAdd
}

return ma.FilterAddrs(addrs,
// Linux and BSD treat an unspecified address when dialing as a localhost address.
// Windows doesn't support this. We filter all such addresses out because peers
// listening on unspecified addresses will advertise more specific addresses.
// https://unix.stackexchange.com/a/419881
// https://superuser.com/a/1755455
func(addr ma.Multiaddr) bool {
return !manet.IsIPUnspecified(addr)
},
func(addr ma.Multiaddr) bool {
if ma.Contains(ourAddrs, addr) {
addrErrs = append(addrErrs, TransportError{Address: addr, Cause: ErrDialToSelf})
Expand Down
8 changes: 8 additions & 0 deletions p2p/net/swarm/swarm_dial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ func TestAddrsForDialFiltering(t *testing.T) {
t1 := ma.StringCast("/ip4/1.2.3.4/tcp/1")
ws1 := ma.StringCast("/ip4/1.2.3.4/tcp/1/ws")

unSpecQ := ma.StringCast("/ip4/0.0.0.0/udp/2/quic-v1")
unSpecT := ma.StringCast("/ip6/::/tcp/2/")

resolver, err := madns.NewResolver(madns.WithDefaultResolver(&madns.MockResolver{}))
require.NoError(t, err)
s := newTestSwarmWithResolver(t, resolver)
Expand Down Expand Up @@ -307,6 +310,11 @@ func TestAddrsForDialFiltering(t *testing.T) {
input: append([]ma.Multiaddr{q1}, ourAddrs...),
output: []ma.Multiaddr{q1},
},
{
name: "unspecified-filtered",
input: []ma.Multiaddr{q1v1, t1, unSpecQ, unSpecT},
output: []ma.Multiaddr{q1v1, t1},
},
}

ctx := context.Background()
Expand Down

0 comments on commit 0509445

Please sign in to comment.