Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter Interface Addresses #936

Merged
merged 7 commits into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
github.com/libp2p/go-libp2p-tls v0.1.3
github.com/libp2p/go-libp2p-transport-upgrader v0.3.0
github.com/libp2p/go-libp2p-yamux v0.2.7
github.com/libp2p/go-netroute v0.1.2
github.com/libp2p/go-stream-muxer-multistream v0.3.0
github.com/libp2p/go-tcp-transport v0.2.0
github.com/libp2p/go-ws-transport v0.3.1
Expand Down
78 changes: 70 additions & 8 deletions p2p/host/basic/basic_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
inat "github.com/libp2p/go-libp2p-nat"
"github.com/libp2p/go-libp2p/p2p/protocol/identify"
"github.com/libp2p/go-libp2p/p2p/protocol/ping"
"github.com/libp2p/go-netroute"

logging "github.com/ipfs/go-log"

Expand Down Expand Up @@ -103,6 +104,9 @@ type BasicHost struct {

signKey crypto.PrivKey
caBook peerstore.CertifiedAddrBook

localIPv4Addr net.IP
localIPv6Addr net.IP
}

var _ host.Host = (*BasicHost)(nil)
Expand Down Expand Up @@ -141,11 +145,11 @@ type HostOpts struct {
}

// NewHost constructs a new *BasicHost and activates it by attaching its stream and connection handlers to the given inet.Network.
func NewHost(ctx context.Context, net network.Network, opts *HostOpts) (*BasicHost, error) {
func NewHost(ctx context.Context, network network.Network, opts *HostOpts) (*BasicHost, error) {
aarshkshah1992 marked this conversation as resolved.
Show resolved Hide resolved
hostCtx, cancel := context.WithCancel(ctx)

h := &BasicHost{
network: net,
network: network,
mux: msmux.NewMultistreamMuxer(),
negtimeout: DefaultNegotiationTimeout,
AddrsFactory: DefaultAddrsFactory,
Expand All @@ -157,14 +161,29 @@ func NewHost(ctx context.Context, net network.Network, opts *HostOpts) (*BasicHo
}

var err error

localIpv4, err := getLocalIPAddrFor(net.IPv4(0, 0, 0, 0))
aarshkshah1992 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
log.Debugw("failed to fetch local IPv4 address", "err", err)
} else {
h.localIPv4Addr = localIpv4
Stebalien marked this conversation as resolved.
Show resolved Hide resolved
}

localIpv6, err := getLocalIPAddrFor(net.ParseIP("::"))
aarshkshah1992 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
log.Debugw("failed to fetch local IPv6 address", "err", err)
} else {
h.localIPv6Addr = localIpv6
}

if h.emitters.evtLocalProtocolsUpdated, err = h.eventbus.Emitter(&event.EvtLocalProtocolsUpdated{}); err != nil {
return nil, err
}
if h.emitters.evtLocalAddrsUpdated, err = h.eventbus.Emitter(&event.EvtLocalAddressesUpdated{}); err != nil {
return nil, err
}

cab, ok := peerstore.GetCertifiedAddrBook(net.Peerstore())
cab, ok := peerstore.GetCertifiedAddrBook(network.Peerstore())
if !ok {
return nil, errors.New("peerstore should also be a certified address book")
}
Expand All @@ -191,7 +210,7 @@ func NewHost(ctx context.Context, net network.Network, opts *HostOpts) (*BasicHo
}

if opts.NATManager != nil {
h.natmgr = opts.NATManager(net)
h.natmgr = opts.NATManager(network)
}

if opts.MultiaddrResolver != nil {
Expand All @@ -202,14 +221,14 @@ func NewHost(ctx context.Context, net network.Network, opts *HostOpts) (*BasicHo
h.cmgr = &connmgr.NullConnMgr{}
} else {
h.cmgr = opts.ConnManager
net.Notify(h.cmgr.Notifee())
network.Notify(h.cmgr.Notifee())
}

if opts.EnablePing {
h.pings = ping.NewPingService(h)
}

net.SetStreamHandler(h.newStreamHandler)
network.SetStreamHandler(h.newStreamHandler)

// persist a signed peer record for self to the peerstore.
rec := peer.PeerRecordFromAddrInfo(peer.AddrInfo{h.ID(), h.Addrs()})
Expand All @@ -224,6 +243,19 @@ func NewHost(ctx context.Context, net network.Network, opts *HostOpts) (*BasicHo
return h, nil
}

func getLocalIPAddrFor(ip net.IP) (net.IP, error) {
r, err := netroute.New()
aarshkshah1992 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}
_, _, src, err := r.Route(ip)
if err != nil {
return nil, err
}

return src, err
}

// New constructs and sets up a new *BasicHost with given Network and options.
// The following options can be passed:
// * NATPortMap
Expand Down Expand Up @@ -700,9 +732,39 @@ func (h *BasicHost) AllAddrs() []ma.Multiaddr {
natMappings = h.natmgr.NAT().Mappings()
}

finalAddrs := listenAddrs
if len(natMappings) > 0 {
var finalAddrs []ma.Multiaddr

for _, a := range listenAddrs {
Stebalien marked this conversation as resolved.
Show resolved Hide resolved
// add loopback addresses for all transports.
if manet.IsIPLoopback(a) {
finalAddrs = append(finalAddrs, a)
continue
}

// add public addresses for all transports
if manet.IsPublicAddr(a) {
finalAddrs = append(finalAddrs, a)
continue
}

ip, err := manet.ToIP(a)
if err != nil {
continue
}
// add default local IPv4 address for all transports.
if len(ip.To4()) == 4 && h.localIPv4Addr != nil && h.localIPv4Addr.Equal(ip) {
finalAddrs = append(finalAddrs, a)
continue
}

// add default local IPv6 address for all transports.
if h.localIPv6Addr != nil && h.localIPv6Addr.Equal(ip) {
finalAddrs = append(finalAddrs, a)
continue
}
}

if len(natMappings) > 0 {
// We have successfully mapped ports on our NAT. Use those
// instead of observed addresses (mostly).

Expand Down
44 changes: 44 additions & 0 deletions p2p/host/basic/basic_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"io"
"net"
"reflect"
"sync"
"testing"
Expand All @@ -25,6 +26,7 @@ import (

ma "github.com/multiformats/go-multiaddr"
madns "github.com/multiformats/go-multiaddr-dns"
manet "github.com/multiformats/go-multiaddr-net"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -169,6 +171,48 @@ func TestHostAddrsFactory(t *testing.T) {
}
}

func TestAddrs(t *testing.T) {
ctx := context.Background()
h := New(swarmt.GenSwarm(t, ctx))
defer h.Close()

// already listens on loopback
addrs := h.Addrs()
require.Len(t, addrs, 1)
require.True(t, manet.IsIPLoopback(addrs[0]))

// private ip4 addr for IPv4
lip, err := getLocalIPAddrFor(net.IPv4(0, 0, 0, 0))
require.NoError(t, err)
require.NotEmpty(t, lip)

require.NoError(t, h.Network().Listen(ma.StringCast("/ip4/0.0.0.0/tcp/0")))
require.True(t, len(h.Addrs()) > 1)
var hasPrivateIPv4 bool
for _, a := range h.Addrs() {
if !manet.IsIPLoopback(a) && manet.IsPrivateAddr(a) {
hasPrivateIPv4 = true
ip, err := manet.ToIP(a)
require.NoError(t, err)
require.True(t, ip.Equal(lip))
}
}
require.True(t, hasPrivateIPv4)

// private addr for IPv6
require.NoError(t, h.Network().Listen(ma.StringCast("/ip6/::/tcp/0")))
require.True(t, len(h.Addrs()) > 2)
var hasPrivateIPv6 bool
for _, a := range h.Addrs() {
nip, err := manet.ToIP(a)
require.NoError(t, err)
if len(nip.To4()) != 4 && manet.IsPrivateAddr(a) {
hasPrivateIPv6 = true
}
}
require.True(t, hasPrivateIPv6)
}

func getHostPair(ctx context.Context, t *testing.T) (host.Host, host.Host) {
t.Helper()

Expand Down