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 22, 2022
1 parent 6472f8c commit 1190e90
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
18 changes: 14 additions & 4 deletions defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,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 @@ -92,15 +92,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 @@ -114,7 +116,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
5 changes: 4 additions & 1 deletion p2p/test/resource-manager/echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/peerstore"
"github.com/libp2p/go-libp2p/p2p/transport/tcp"

"github.com/stretchr/testify/require"
)
Expand All @@ -15,7 +16,9 @@ func createEchos(t *testing.T, count int, makeOpts ...func(int) libp2p.Option) [
result := make([]*Echo, 0, count)

for i := 0; i < count; i++ {
opts := make([]libp2p.Option, 0, len(makeOpts))
opts := make([]libp2p.Option, 0, len(makeOpts)+2)
// only use a single transport, otherwise we might end up with a TCP and a QUIC connection to the same host
opts = append(opts, libp2p.Transport(tcp.NewTCPTransport), libp2p.DefaultListenAddrs)
for _, makeOpt := range makeOpts {
opts = append(opts, makeOpt(i))
}
Expand Down
12 changes: 6 additions & 6 deletions p2p/test/resource-manager/rcmgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import (
"github.com/stretchr/testify/require"
)

func makeRcmgrOption(t *testing.T, cfg rcmgr.LimitConfig, test string) func(int) libp2p.Option {
func makeRcmgrOption(t *testing.T, cfg rcmgr.LimitConfig) func(int) libp2p.Option {
return func(i int) libp2p.Option {
var opts []rcmgr.Option
if os.Getenv("LIBP2P_TEST_RCMGR_TRACE") == "1" {
opts = append(opts, rcmgr.WithTrace(fmt.Sprintf("%s-%d.json.gz", test, i)))
opts = append(opts, rcmgr.WithTrace(fmt.Sprintf("%s-%d.json.gz", t.Name(), i)))
}

mgr, err := rcmgr.NewResourceManager(rcmgr.NewFixedLimiter(cfg), opts...)
Expand Down Expand Up @@ -54,7 +54,7 @@ func TestResourceManagerConnInbound(t *testing.T) {
cfg.PeerDefault.ConnsInbound = 1
cfg.PeerDefault.ConnsOutbound = 1

echos := createEchos(t, 5, makeRcmgrOption(t, cfg, "TestResourceManagerConnInbound"))
echos := createEchos(t, 5, makeRcmgrOption(t, cfg))
defer closeEchos(echos)
defer closeRcmgrs(echos)

Expand Down Expand Up @@ -89,7 +89,7 @@ func TestResourceManagerConnOutbound(t *testing.T) {
cfg.PeerDefault.Conns = 1
cfg.PeerDefault.ConnsInbound = 1
cfg.PeerDefault.ConnsOutbound = 1
echos := createEchos(t, 5, makeRcmgrOption(t, cfg, "TestResourceManagerConnOutbound"))
echos := createEchos(t, 5, makeRcmgrOption(t, cfg))
defer closeEchos(echos)
defer closeRcmgrs(echos)

Expand Down Expand Up @@ -121,7 +121,7 @@ func TestResourceManagerServiceInbound(t *testing.T) {
cfg.ServiceDefault.StreamsInbound = 3
cfg.ServiceDefault.StreamsOutbound = 1024
cfg.ServiceDefault.Streams = 1024
echos := createEchos(t, 5, makeRcmgrOption(t, cfg, "TestResourceManagerServiceInbound"))
echos := createEchos(t, 5, makeRcmgrOption(t, cfg))
defer closeEchos(echos)
defer closeRcmgrs(echos)

Expand Down Expand Up @@ -178,7 +178,7 @@ func TestResourceManagerServicePeerInbound(t *testing.T) {
)
limits := cfg.AutoScale()

echos := createEchos(t, 5, makeRcmgrOption(t, limits, "TestResourceManagerServicePeerInbound"))
echos := createEchos(t, 5, makeRcmgrOption(t, limits))
defer closeEchos(echos)
defer closeRcmgrs(echos)

Expand Down

0 comments on commit 1190e90

Please sign in to comment.