From f82d4ec6aaaa38116f059b85d75fce329d117e94 Mon Sep 17 00:00:00 2001 From: Abhishek Upperwal Date: Wed, 25 Jul 2018 21:50:54 +0530 Subject: [PATCH 1/9] default listener --- defaults.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/defaults.go b/defaults.go index d3fa618d..7d68eae7 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" ) @@ -52,6 +53,14 @@ var RandomIdentity = func(cfg *Config) error { return cfg.Apply(Identity(priv)) } +var DefaultListenAddrs = func(cfg *Config) error { + defaultListenAddr, err := multiaddr.NewMultiaddr("/ip4/0.0.0.0/tcp/0") + if err != nil { + return err + } + return cfg.Apply(ListenAddrs(defaultListenAddr)) +} + // Complete list of default options and when to fallback on them. // // Please *DON'T* specify default options any other way. Putting this all here @@ -80,6 +89,10 @@ var defaults = []struct { fallback: func(cfg *Config) bool { return cfg.Peerstore == nil }, opt: DefaultPeerstore, }, + { + fallback: func(cfg *Config) bool { return cfg.ListenAddrs == nil }, + opt: DefaultListenAddrs, + }, } // Defaults configures libp2p to use the default options. Can be combined with From d104ff8dea24608646e83c39e95e0acea494f02d Mon Sep 17 00:00:00 2001 From: Abhishek Upperwal Date: Wed, 25 Jul 2018 22:02:37 +0530 Subject: [PATCH 2/9] making go fmt happy --- defaults.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/defaults.go b/defaults.go index 7d68eae7..cca571ed 100644 --- a/defaults.go +++ b/defaults.go @@ -21,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. @@ -53,6 +53,7 @@ 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 { defaultListenAddr, err := multiaddr.NewMultiaddr("/ip4/0.0.0.0/tcp/0") if err != nil { @@ -91,7 +92,7 @@ var defaults = []struct { }, { fallback: func(cfg *Config) bool { return cfg.ListenAddrs == nil }, - opt: DefaultListenAddrs, + opt: DefaultListenAddrs, }, } From 836d042f547e1d48211205b979320c544c04e7d3 Mon Sep 17 00:00:00 2001 From: Abhishek Upperwal Date: Wed, 25 Jul 2018 23:42:03 +0530 Subject: [PATCH 3/9] added ipv6 + checking for default transport --- defaults.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/defaults.go b/defaults.go index cca571ed..973cf5c1 100644 --- a/defaults.go +++ b/defaults.go @@ -55,11 +55,19 @@ var RandomIdentity = func(cfg *Config) error { // DefaultListenAddrs configures libp2p to use default listen address var DefaultListenAddrs = func(cfg *Config) error { - defaultListenAddr, err := multiaddr.NewMultiaddr("/ip4/0.0.0.0/tcp/0") + defaultIP4ListenAddr, err := multiaddr.NewMultiaddr("/ip4/0.0.0.0/tcp/0") if err != nil { return err } - return cfg.Apply(ListenAddrs(defaultListenAddr)) + + defaultIP6ListenAddr, err := multiaddr.NewMultiaddr("/ip6/0.0.0.0/tcp/0") + if err != nil { + return err + } + return cfg.Apply(ListenAddrs( + defaultIP4ListenAddr, + defaultIP6ListenAddr, + )) } // Complete list of default options and when to fallback on them. @@ -70,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, @@ -90,10 +102,6 @@ var defaults = []struct { fallback: func(cfg *Config) bool { return cfg.Peerstore == nil }, opt: DefaultPeerstore, }, - { - fallback: func(cfg *Config) bool { return cfg.ListenAddrs == nil }, - opt: DefaultListenAddrs, - }, } // Defaults configures libp2p to use the default options. Can be combined with From fb4019575dd1cfbfe22b24efeef6bf82faa936e8 Mon Sep 17 00:00:00 2001 From: Abhishek Upperwal Date: Thu, 26 Jul 2018 01:08:20 +0530 Subject: [PATCH 4/9] Added test for DefaultListenAddrs --- libp2p_test.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/libp2p_test.go b/libp2p_test.go index 1b38bbc3..f4f44276 100644 --- a/libp2p_test.go +++ b/libp2p_test.go @@ -8,6 +8,7 @@ import ( 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 +40,39 @@ func TestInsecure(t *testing.T) { h.Close() } +func TestDefaultListenAddrs(t *testing.T) { + ctx := context.Background() + + // Test 1: Listen addr should not set if user defined transport is passed. + h, err := New( + ctx, + Transport(tcp.TcpTransport{}), + ) + if err != nil { + t.Fatal(err) + } + + if len(h.Addrs()) != 0 { + t.Error("expected zero listen addrs as none is set with user defined transport") + } + h.Close() + + // Test 2: User defined listener addrs should overwrite the default options. + h, err = New( + ctx, + Transport(tcp.TcpTransport{}), + ListenAddrStrings("/ip4/127.0.0.1/tcp/0"), + ) + if err != nil { + t.Fatal(err) + } + + if len(h.Addrs()) != 1 { + t.Error("expected one listen addr") + } + h.Close() +} + func makeRandomHost(t *testing.T, port int) (host.Host, error) { ctx := context.Background() priv, _, err := crypto.GenerateKeyPair(crypto.RSA, 2048) From 29af134a7103e8ae1c8660a14ddc533c0115ae40 Mon Sep 17 00:00:00 2001 From: Abhishek Upperwal Date: Thu, 26 Jul 2018 01:14:09 +0530 Subject: [PATCH 5/9] corrected transport init --- libp2p_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libp2p_test.go b/libp2p_test.go index f4f44276..7662fcf6 100644 --- a/libp2p_test.go +++ b/libp2p_test.go @@ -46,7 +46,7 @@ func TestDefaultListenAddrs(t *testing.T) { // Test 1: Listen addr should not set if user defined transport is passed. h, err := New( ctx, - Transport(tcp.TcpTransport{}), + Transport(tcp.NewTCPTransport), ) if err != nil { t.Fatal(err) @@ -60,7 +60,7 @@ func TestDefaultListenAddrs(t *testing.T) { // Test 2: User defined listener addrs should overwrite the default options. h, err = New( ctx, - Transport(tcp.TcpTransport{}), + Transport(tcp.NewTCPTransport), ListenAddrStrings("/ip4/127.0.0.1/tcp/0"), ) if err != nil { From 6372f928d4f214d5f32bb73c860f00db2909a53f Mon Sep 17 00:00:00 2001 From: Abhishek Upperwal Date: Fri, 27 Jul 2018 02:26:29 +0530 Subject: [PATCH 6/9] Correct behaviour test for DefaultListenAddrs --- libp2p_test.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/libp2p_test.go b/libp2p_test.go index 7662fcf6..20370d54 100644 --- a/libp2p_test.go +++ b/libp2p_test.go @@ -43,8 +43,18 @@ func TestInsecure(t *testing.T) { func TestDefaultListenAddrs(t *testing.T) { ctx := context.Background() - // Test 1: Listen addr should not set if user defined transport is passed. - h, err := New( + // Test 1: Setting the correct listen addresses if userDefined.Transport == nil && userDefined.ListenAddrs == nil + h, err := New(ctx) + if err != nil { + t.Fatal(err) + } + if len(h.Addrs()) != 2 { + t.Error("expected 2 default listen addrs") + } + h.Close() + + // Test 2: Listen addr should not set if user defined transport is passed. + h, err = New( ctx, Transport(tcp.NewTCPTransport), ) @@ -57,7 +67,7 @@ func TestDefaultListenAddrs(t *testing.T) { } h.Close() - // Test 2: User defined listener addrs should overwrite the default options. + // Test 3: User defined listener addrs should overwrite the default options. h, err = New( ctx, Transport(tcp.NewTCPTransport), From ecf27b599f6636e9cefa1b1153679afe2bdbb794 Mon Sep 17 00:00:00 2001 From: Abhishek Upperwal Date: Sat, 28 Jul 2018 02:08:45 +0530 Subject: [PATCH 7/9] strict DefaultListenAddrs test w/ regex --- libp2p_test.go | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/libp2p_test.go b/libp2p_test.go index 20370d54..a9308244 100644 --- a/libp2p_test.go +++ b/libp2p_test.go @@ -3,6 +3,7 @@ package libp2p import ( "context" "fmt" + "regexp" "strings" "testing" @@ -43,14 +44,19 @@ func TestInsecure(t *testing.T) { 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) } - if len(h.Addrs()) != 2 { - t.Error("expected 2 default listen addrs") + 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. @@ -62,25 +68,10 @@ func TestDefaultListenAddrs(t *testing.T) { t.Fatal(err) } - if len(h.Addrs()) != 0 { + if len(h.Network().ListenAddresses()) != 0 { t.Error("expected zero listen addrs as none is set with user defined transport") } h.Close() - - // Test 3: User defined listener addrs should overwrite the default options. - h, err = New( - ctx, - Transport(tcp.NewTCPTransport), - ListenAddrStrings("/ip4/127.0.0.1/tcp/0"), - ) - if err != nil { - t.Fatal(err) - } - - if len(h.Addrs()) != 1 { - t.Error("expected one listen addr") - } - h.Close() } func makeRandomHost(t *testing.T, port int) (host.Host, error) { From 617d7a5797e550e4655ba8a8d240fb6d4e11766a Mon Sep 17 00:00:00 2001 From: Abhishek Upperwal Date: Sat, 28 Jul 2018 02:47:27 +0530 Subject: [PATCH 8/9] Corrected ip6 string --- defaults.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/defaults.go b/defaults.go index 973cf5c1..1bfb77f7 100644 --- a/defaults.go +++ b/defaults.go @@ -60,7 +60,7 @@ var DefaultListenAddrs = func(cfg *Config) error { return err } - defaultIP6ListenAddr, err := multiaddr.NewMultiaddr("/ip6/0.0.0.0/tcp/0") + defaultIP6ListenAddr, err := multiaddr.NewMultiaddr("/ip6/::/tcp/0") if err != nil { return err } From 9e952be41289e2cc3d4890c6c186a58883be46ca Mon Sep 17 00:00:00 2001 From: Abhishek Upperwal Date: Sat, 28 Jul 2018 02:48:09 +0530 Subject: [PATCH 9/9] modified regex for ipv6 --- libp2p_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libp2p_test.go b/libp2p_test.go index a9308244..293e0fef 100644 --- a/libp2p_test.go +++ b/libp2p_test.go @@ -44,7 +44,7 @@ func TestInsecure(t *testing.T) { func TestDefaultListenAddrs(t *testing.T) { ctx := context.Background() - re := regexp.MustCompile("/ip[4|6]/0.0.0.0/tcp/") + 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)