Skip to content

Commit

Permalink
Merge pull request #378 from upperwal/default_listener
Browse files Browse the repository at this point in the history
Added a Default listener
  • Loading branch information
Stebalien authored Jul 27, 2018
2 parents d8ee5b3 + 9e952be commit c984cc6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
24 changes: 23 additions & 1 deletion defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
35 changes: 35 additions & 0 deletions libp2p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c984cc6

Please sign in to comment.