Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swarm: don't dial unspecified addresses #2560

Merged
merged 2 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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