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

Publicize NAT manager struct #199

Merged
2 commits merged into from
Jun 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 2 additions & 6 deletions p2p/host/basic/basic_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type BasicHost struct {
network inet.Network
mux *msmux.MultistreamMuxer
ids *identify.IDService
natmgr *natManager
natmgr NATManager
addrs AddrsFactory

negtimeout time.Duration
Expand Down Expand Up @@ -88,11 +88,7 @@ type HostOpts struct {

// NATManager takes care of setting NAT port mappings, and discovering external addresses.
// If omitted, this will simply be disabled.
//
// TODO: Currently the NATManager can only be enabled by calling New,
// since the underlying struct and functions are still private.
// Once they are public, NATManager can be used through NewHost as well.
NATManager *natManager
NATManager NATManager

//
BandwidthReporter metrics.Reporter
Expand Down
24 changes: 21 additions & 3 deletions p2p/host/basic/natmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,32 @@ import (
"context"
"sync"

inat "github.com/libp2p/go-libp2p-nat"

goprocess "github.com/jbenet/goprocess"
lgbl "github.com/libp2p/go-libp2p-loggables"
inat "github.com/libp2p/go-libp2p-nat"
inet "github.com/libp2p/go-libp2p-net"
lgbl "github.com/libp2p/go-libp2p-loggables"
ma "github.com/multiformats/go-multiaddr"
)

// A simple interface to manage NAT devices.
type NATManager interface {

// Get the NAT device managed by the NAT manager.
NAT() *inat.NAT

// Receive a notification when the NAT device is ready for use.
Ready() <-chan struct{}

// Close all resources associated with a NAT manager.
Close() error

}

// Create a NAT manager.
func NewNATManager(net inet.Network) NATManager {
return newNatManager(net)
}

// natManager takes care of adding + removing port mappings to the nat.
// Initialized with the host if it has a NATPortMap option enabled.
// natManager receives signals from the network, and check on nat mappings:
Expand Down