Skip to content

Commit

Permalink
add default listen addresses for QUIC
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Aug 9, 2022
1 parent 61f0605 commit a67a2bb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
18 changes: 14 additions & 4 deletions defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,28 @@ var RandomIdentity = func(cfg *Config) error {

// DefaultListenAddrs configures libp2p to use default listen address.
var DefaultListenAddrs = func(cfg *Config) error {
defaultIP4ListenAddr, err := multiaddr.NewMultiaddr("/ip4/0.0.0.0/tcp/0")
defaultIP4TCPListenAddr, err := multiaddr.NewMultiaddr("/ip4/0.0.0.0/tcp/0")
if err != nil {
return err
}
defaultIP4QUICListenAddr, err := multiaddr.NewMultiaddr("/ip4/0.0.0.0/udp/0/quic")
if err != nil {
return err
}

defaultIP6ListenAddr, err := multiaddr.NewMultiaddr("/ip6/::/tcp/0")
defaultIP6TCPListenAddr, err := multiaddr.NewMultiaddr("/ip6/::/tcp/0")
if err != nil {
return err
}
defaultIP6QUICListenAddr, err := multiaddr.NewMultiaddr("/ip6/::/udp/0/quic")
if err != nil {
return err
}
return cfg.Apply(ListenAddrs(
defaultIP4ListenAddr,
defaultIP6ListenAddr,
defaultIP4TCPListenAddr,
defaultIP4QUICListenAddr,
defaultIP6TCPListenAddr,
defaultIP6QUICListenAddr,
))
}

Expand Down
12 changes: 7 additions & 5 deletions libp2p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,17 @@ func TestAutoNATService(t *testing.T) {
}

func TestDefaultListenAddrs(t *testing.T) {
re := regexp.MustCompile("/(ip)[4|6]/((0.0.0.0)|(::))/tcp/")
re2 := regexp.MustCompile("/p2p-circuit")
reTCP := regexp.MustCompile("/(ip)[4|6]/((0.0.0.0)|(::))/tcp/")
reQUIC := regexp.MustCompile("/(ip)[4|6]/((0.0.0.0)|(::))/udp/([0-9]*)/quic")
reCircuit := regexp.MustCompile("/p2p-circuit")

// Test 1: Setting the correct listen addresses if userDefined.Transport == nil && userDefined.ListenAddrs == nil
h, err := New()
require.NoError(t, err)
for _, addr := range h.Network().ListenAddresses() {
if re.FindStringSubmatchIndex(addr.String()) == nil &&
re2.FindStringSubmatchIndex(addr.String()) == nil {
if reTCP.FindStringSubmatchIndex(addr.String()) == nil &&
reQUIC.FindStringSubmatchIndex(addr.String()) == nil &&
reCircuit.FindStringSubmatchIndex(addr.String()) == nil {
t.Error("expected ip4 or ip6 or relay interface")
}
}
Expand All @@ -115,7 +117,7 @@ func TestDefaultListenAddrs(t *testing.T) {
if len(h.Network().ListenAddresses()) != 1 {
t.Error("expected one listen addr with user defined transport")
}
if re2.FindStringSubmatchIndex(h.Network().ListenAddresses()[0].String()) == nil {
if reCircuit.FindStringSubmatchIndex(h.Network().ListenAddresses()[0].String()) == nil {
t.Error("expected relay address")
}
h.Close()
Expand Down

0 comments on commit a67a2bb

Please sign in to comment.