Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
go-libp2p
Commits
e571f8a3
Commit
e571f8a3
authored
Jun 03, 2017
by
Enzo Haussecker
Browse files
Publicize NAT manager struct
parent
08c8d6f7
Changes
2
Hide whitespace changes
Inline
Side-by-side
p2p/host/basic/basic_host.go
View file @
e571f8a3
...
...
@@ -56,7 +56,7 @@ type BasicHost struct {
network
inet
.
Network
mux
*
msmux
.
MultistreamMuxer
ids
*
identify
.
IDService
natmgr
*
nat
Manager
natmgr
*
NAT
Manager
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
...
...
@@ -159,7 +155,7 @@ func New(net inet.Network, opts ...interface{}) *BasicHost {
case
Option
:
switch
o
{
case
NATPortMap
:
hostopts
.
NATManager
=
n
ewN
at
Manager
(
net
)
hostopts
.
NATManager
=
N
ewN
AT
Manager
(
net
)
}
case
metrics
.
Reporter
:
hostopts
.
BandwidthReporter
=
o
...
...
p2p/host/basic/natmgr.go
View file @
e571f8a3
...
...
@@ -12,23 +12,23 @@ import (
ma
"github.com/multiformats/go-multiaddr"
)
//
nat
Manager takes care of adding + removing port mappings to the nat.
//
NAT
Manager takes care of adding + removing port mappings to the nat.
// Initialized with the host if it has a NATPortMap option enabled.
//
nat
Manager receives signals from the network, and check on nat mappings:
// *
nat
Manager listens to the network and adds or closes port mappings
//
NAT
Manager receives signals from the network, and check on nat mappings:
// *
NAT
Manager listens to the network and adds or closes port mappings
// as the network signals Listen() or ListenClose().
// * closing the
nat
Manager closes the nat and its mappings.
type
nat
Manager
struct
{
// * closing the
NAT
Manager closes the nat and its mappings.
type
NAT
Manager
struct
{
net
inet
.
Network
natmu
sync
.
RWMutex
// guards nat (ready could obviate this mutex, but safety first.)
nat
*
inat
.
NAT
ready
chan
struct
{}
// closed once the nat is ready to process port mappings
proc
goprocess
.
Process
//
nat
Manager has a process + children. can be closed.
proc
goprocess
.
Process
//
NAT
Manager has a process + children. can be closed.
}
func
n
ewN
at
Manager
(
net
inet
.
Network
)
*
nat
Manager
{
nmgr
:=
&
nat
Manager
{
func
N
ewN
AT
Manager
(
net
inet
.
Network
)
*
NAT
Manager
{
nmgr
:=
&
NAT
Manager
{
net
:
net
,
ready
:
make
(
chan
struct
{}),
}
...
...
@@ -44,19 +44,19 @@ func newNatManager(net inet.Network) *natManager {
return
nmgr
}
// Close closes the
nat
Manager, closing the underlying nat
// Close closes the
NAT
Manager, closing the underlying nat
// and unregistering from network events.
func
(
nmgr
*
nat
Manager
)
Close
()
error
{
func
(
nmgr
*
NAT
Manager
)
Close
()
error
{
return
nmgr
.
proc
.
Close
()
}
// Ready returns a channel which will be closed when the NAT has been found
// and is ready to be used, or the search process is done.
func
(
nmgr
*
nat
Manager
)
Ready
()
<-
chan
struct
{}
{
func
(
nmgr
*
NAT
Manager
)
Ready
()
<-
chan
struct
{}
{
return
nmgr
.
ready
}
func
(
nmgr
*
nat
Manager
)
discoverNAT
()
{
func
(
nmgr
*
NAT
Manager
)
discoverNAT
()
{
nmgr
.
proc
.
Go
(
func
(
worker
goprocess
.
Process
)
{
// inat.DiscoverNAT blocks until the nat is found or a timeout
...
...
@@ -100,7 +100,7 @@ func (nmgr *natManager) discoverNAT() {
// signal that we're ready to process nat mappings:
close
(
nmgr
.
ready
)
// sign
nat
Manager up for network notifications
// sign
NAT
Manager up for network notifications
// we need to sign up here to avoid missing some notifs
// before the NAT has been found.
nmgr
.
net
.
Notify
((
*
nmgrNetNotifiee
)(
nmgr
))
...
...
@@ -117,19 +117,19 @@ func (nmgr *natManager) discoverNAT() {
})
}
// NAT returns the
nat
Manager's nat object. this may be nil, if
// NAT returns the
NAT
Manager's nat object. this may be nil, if
// (a) the search process is still ongoing, or (b) the search process
// found no nat. Clients must check whether the return value is nil.
func
(
nmgr
*
nat
Manager
)
NAT
()
*
inat
.
NAT
{
func
(
nmgr
*
NAT
Manager
)
NAT
()
*
inat
.
NAT
{
nmgr
.
natmu
.
Lock
()
defer
nmgr
.
natmu
.
Unlock
()
return
nmgr
.
nat
}
func
addPortMapping
(
nmgr
*
nat
Manager
,
intaddr
ma
.
Multiaddr
)
{
func
addPortMapping
(
nmgr
*
NAT
Manager
,
intaddr
ma
.
Multiaddr
)
{
nat
:=
nmgr
.
NAT
()
if
nat
==
nil
{
panic
(
"
nat
Manager addPortMapping called without a nat."
)
panic
(
"
NAT
Manager addPortMapping called without a nat."
)
}
// first, check if the port mapping already exists.
...
...
@@ -175,10 +175,10 @@ func addPortMapping(nmgr *natManager, intaddr ma.Multiaddr) {
log
.
Infof
(
"established nat port mapping: %s <--> %s"
,
intaddr
,
extaddr
)
}
func
rmPortMapping
(
nmgr
*
nat
Manager
,
intaddr
ma
.
Multiaddr
)
{
func
rmPortMapping
(
nmgr
*
NAT
Manager
,
intaddr
ma
.
Multiaddr
)
{
nat
:=
nmgr
.
NAT
()
if
nat
==
nil
{
panic
(
"
nat
Manager rmPortMapping called without a nat."
)
panic
(
"
NAT
Manager rmPortMapping called without a nat."
)
}
// list the port mappings (it may be gone on it's own, so we need to
...
...
@@ -193,12 +193,12 @@ func rmPortMapping(nmgr *natManager, intaddr ma.Multiaddr) {
}
// nmgrNetNotifiee implements the network notification listening part
// of the
nat
Manager. this is merely listening to Listen() and ListenClose()
// of the
NAT
Manager. this is merely listening to Listen() and ListenClose()
// events.
type
nmgrNetNotifiee
nat
Manager
type
nmgrNetNotifiee
NAT
Manager
func
(
nn
*
nmgrNetNotifiee
)
natManager
()
*
nat
Manager
{
return
(
*
nat
Manager
)(
nn
)
func
(
nn
*
nmgrNetNotifiee
)
natManager
()
*
NAT
Manager
{
return
(
*
NAT
Manager
)(
nn
)
}
func
(
nn
*
nmgrNetNotifiee
)
Listen
(
n
inet
.
Network
,
addr
ma
.
Multiaddr
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment