From ad5b7b9ba65d7e9509b3f52f2697bdf03b9fcb3e Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 16 Aug 2018 11:16:02 -0700 Subject: [PATCH 1/2] add options for disabling the default listen address/transports So, in go-ipfs at least, we *don't* pass the listen addresses into the libp2p constructor. Instead, we specify them later. This option allows us to tell libp2p to not listen by default. --- options.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/options.go b/options.go index a17fc02e..ee147fa9 100644 --- a/options.go +++ b/options.go @@ -241,3 +241,21 @@ func NATManager(nm config.NATManagerC) Option { return nil } } + +// NoListen will configure libp2p to not listen by default. +// +// This will both clear any configured listen addrs and prevent libp2p from +// applying the default listen address option. +var NoListen = func(cfg *Config) error { + cfg.ListenAddrs = []ma.Multiaddr{} + return nil +} + +// NoTransports will configure libp2p to not enable any transports. +// +// This will both clear any configured transports (specified in prior libp2p +// options) and prevent libp2p from applying the default transports. +var NoTransports = func(cfg *Config) error { + cfg.Transports = []config.TptC{} + return nil +} From a36d3834139ee632285ff745321010a97441be37 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 16 Aug 2018 13:20:17 -0700 Subject: [PATCH 2/2] add tests for NoTransports and NoListen --- libp2p_test.go | 36 ++++++++++++++++++++++++++++++++++++ options.go | 4 ++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/libp2p_test.go b/libp2p_test.go index 293e0fef..0685f4b6 100644 --- a/libp2p_test.go +++ b/libp2p_test.go @@ -9,6 +9,7 @@ import ( crypto "github.com/libp2p/go-libp2p-crypto" host "github.com/libp2p/go-libp2p-host" + pstore "github.com/libp2p/go-libp2p-peerstore" "github.com/libp2p/go-tcp-transport" ) @@ -32,6 +33,41 @@ func TestBadTransportConstructor(t *testing.T) { } } +func TestNoListenAddrs(t *testing.T) { + ctx := context.Background() + h, err := New(ctx, NoListenAddrs) + if err != nil { + t.Fatal(err) + } + defer h.Close() + if len(h.Addrs()) != 0 { + t.Fatal("expected no addresses") + } +} + +func TestNoTransports(t *testing.T) { + ctx := context.Background() + a, err := New(ctx, NoTransports) + if err != nil { + t.Fatal(err) + } + defer a.Close() + + b, err := New(ctx, ListenAddrStrings("/ip4/127.0.0.1/tcp/0")) + if err != nil { + t.Fatal(err) + } + defer b.Close() + + err = a.Connect(ctx, pstore.PeerInfo{ + ID: b.ID(), + Addrs: b.Addrs(), + }) + if err == nil { + t.Error("dial should have failed as no transports have been configured") + } +} + func TestInsecure(t *testing.T) { ctx := context.Background() h, err := New(ctx, NoSecurity) diff --git a/options.go b/options.go index ee147fa9..ed8e89cf 100644 --- a/options.go +++ b/options.go @@ -242,11 +242,11 @@ func NATManager(nm config.NATManagerC) Option { } } -// NoListen will configure libp2p to not listen by default. +// NoListenAddrs will configure libp2p to not listen by default. // // This will both clear any configured listen addrs and prevent libp2p from // applying the default listen address option. -var NoListen = func(cfg *Config) error { +var NoListenAddrs = func(cfg *Config) error { cfg.ListenAddrs = []ma.Multiaddr{} return nil }