diff --git a/p2p/host/basic/basic_host.go b/p2p/host/basic/basic_host.go index 6e75aeb44f..f0297f1975 100644 --- a/p2p/host/basic/basic_host.go +++ b/p2p/host/basic/basic_host.go @@ -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 @@ -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 diff --git a/p2p/host/basic/natmgr.go b/p2p/host/basic/natmgr.go index 9a99436288..5ac40a6614 100644 --- a/p2p/host/basic/natmgr.go +++ b/p2p/host/basic/natmgr.go @@ -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: