Unverified Commit c984cc61 authored by Steven Allen's avatar Steven Allen Committed by GitHub
Browse files

Merge pull request #378 from upperwal/default_listener

Added a Default listener
parents d8ee5b3c 9e952be4
...@@ -10,6 +10,7 @@ import ( ...@@ -10,6 +10,7 @@ import (
secio "github.com/libp2p/go-libp2p-secio" secio "github.com/libp2p/go-libp2p-secio"
tcp "github.com/libp2p/go-tcp-transport" tcp "github.com/libp2p/go-tcp-transport"
ws "github.com/libp2p/go-ws-transport" ws "github.com/libp2p/go-ws-transport"
multiaddr "github.com/multiformats/go-multiaddr"
mplex "github.com/whyrusleeping/go-smux-multiplex" mplex "github.com/whyrusleeping/go-smux-multiplex"
yamux "github.com/whyrusleeping/go-smux-yamux" yamux "github.com/whyrusleeping/go-smux-yamux"
) )
...@@ -20,7 +21,7 @@ import ( ...@@ -20,7 +21,7 @@ import (
// security protocols. // security protocols.
var DefaultSecurity = Security(secio.ID, secio.New) 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 // Use this option when you want to *extend* the set of multiplexers used by
// libp2p instead of replacing them. // libp2p instead of replacing them.
...@@ -52,6 +53,23 @@ var RandomIdentity = func(cfg *Config) error { ...@@ -52,6 +53,23 @@ var RandomIdentity = func(cfg *Config) error {
return cfg.Apply(Identity(priv)) 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. // Complete list of default options and when to fallback on them.
// //
// Please *DON'T* specify default options any other way. Putting this all here // Please *DON'T* specify default options any other way. Putting this all here
...@@ -60,6 +78,10 @@ var defaults = []struct { ...@@ -60,6 +78,10 @@ var defaults = []struct {
fallback func(cfg *Config) bool fallback func(cfg *Config) bool
opt Option 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 }, fallback: func(cfg *Config) bool { return cfg.Transports == nil },
opt: DefaultTransports, opt: DefaultTransports,
......
...@@ -3,11 +3,13 @@ package libp2p ...@@ -3,11 +3,13 @@ package libp2p
import ( import (
"context" "context"
"fmt" "fmt"
"regexp"
"strings" "strings"
"testing" "testing"
crypto "github.com/libp2p/go-libp2p-crypto" crypto "github.com/libp2p/go-libp2p-crypto"
host "github.com/libp2p/go-libp2p-host" host "github.com/libp2p/go-libp2p-host"
"github.com/libp2p/go-tcp-transport"
) )
func TestNewHost(t *testing.T) { func TestNewHost(t *testing.T) {
...@@ -39,6 +41,39 @@ func TestInsecure(t *testing.T) { ...@@ -39,6 +41,39 @@ func TestInsecure(t *testing.T) {
h.Close() 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) { func makeRandomHost(t *testing.T, port int) (host.Host, error) {
ctx := context.Background() ctx := context.Background()
priv, _, err := crypto.GenerateKeyPair(crypto.RSA, 2048) priv, _, err := crypto.GenerateKeyPair(crypto.RSA, 2048)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment