Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #237 from libp2p/prevent-self-dial
Browse files Browse the repository at this point in the history
prevent dialing addresses that we're listening on
  • Loading branch information
Stebalien authored Feb 9, 2021
2 parents 95888d6 + 88c64b7 commit 3feb612
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion swarm_dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
31 changes: 31 additions & 0 deletions swarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package swarm_test
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"strings"
"sync"
"testing"
"time"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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)
}
}

0 comments on commit 3feb612

Please sign in to comment.