diff --git a/defaults.go b/defaults.go index d3fa618d6f50471909f5500a66441c8ce09740d2..1bfb77f789879eb4003f06074960707999c13214 100644 --- a/defaults.go +++ b/defaults.go @@ -10,6 +10,7 @@ import ( secio "github.com/libp2p/go-libp2p-secio" tcp "github.com/libp2p/go-tcp-transport" ws "github.com/libp2p/go-ws-transport" + multiaddr "github.com/multiformats/go-multiaddr" mplex "github.com/whyrusleeping/go-smux-multiplex" yamux "github.com/whyrusleeping/go-smux-yamux" ) @@ -20,7 +21,7 @@ import ( // security protocols. var DefaultSecurity = Security(secio.ID, secio.New) -// DefaultMuxer configures libp2p to use the stream connection multiplexers. +// DefaultMuxers configures libp2p to use the stream connection multiplexers. // // Use this option when you want to *extend* the set of multiplexers used by // libp2p instead of replacing them. @@ -52,6 +53,23 @@ var RandomIdentity = func(cfg *Config) error { return cfg.Apply(Identity(priv)) } +// 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") + if err != nil { + return err + } + + defaultIP6ListenAddr, err := multiaddr.NewMultiaddr("/ip6/::/tcp/0") + if err != nil { + return err + } + return cfg.Apply(ListenAddrs( + defaultIP4ListenAddr, + defaultIP6ListenAddr, + )) +} + // Complete list of default options and when to fallback on them. // // Please *DON'T* specify default options any other way. Putting this all here @@ -60,6 +78,10 @@ var defaults = []struct { fallback func(cfg *Config) bool opt Option }{ + { + fallback: func(cfg *Config) bool { return cfg.Transports == nil && cfg.ListenAddrs == nil }, + opt: DefaultListenAddrs, + }, { fallback: func(cfg *Config) bool { return cfg.Transports == nil }, opt: DefaultTransports, diff --git a/libp2p_test.go b/libp2p_test.go index 1b38bbc3697a5fa957dac32ea63d21260d9776ad..293e0fef7c6afbe69bca5fee3a7e08bfcebbd107 100644 --- a/libp2p_test.go +++ b/libp2p_test.go @@ -3,11 +3,13 @@ package libp2p import ( "context" "fmt" + "regexp" "strings" "testing" crypto "github.com/libp2p/go-libp2p-crypto" host "github.com/libp2p/go-libp2p-host" + "github.com/libp2p/go-tcp-transport" ) func TestNewHost(t *testing.T) { @@ -39,6 +41,39 @@ func TestInsecure(t *testing.T) { h.Close() } +func TestDefaultListenAddrs(t *testing.T) { + ctx := context.Background() + + re := regexp.MustCompile("/(ip)[4|6]/((0.0.0.0)|(::))/tcp/") + + // Test 1: Setting the correct listen addresses if userDefined.Transport == nil && userDefined.ListenAddrs == nil + h, err := New(ctx) + if err != nil { + t.Fatal(err) + } + for _, addr := range h.Network().ListenAddresses() { + if re.FindStringSubmatchIndex(addr.String()) == nil { + t.Error("expected ip4 or ip6 interface") + } + } + + h.Close() + + // Test 2: Listen addr should not set if user defined transport is passed. + h, err = New( + ctx, + Transport(tcp.NewTCPTransport), + ) + if err != nil { + t.Fatal(err) + } + + if len(h.Network().ListenAddresses()) != 0 { + t.Error("expected zero listen addrs as none is set with user defined transport") + } + h.Close() +} + func makeRandomHost(t *testing.T, port int) (host.Host, error) { ctx := context.Background() priv, _, err := crypto.GenerateKeyPair(crypto.RSA, 2048)