Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
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
e9c24fdc
Commit
e9c24fdc
authored
7 years ago
by
Enzo Haussecker
Browse files
Options
Download
Email Patches
Plain Diff
Create interface for NAT manager
parent
e571f8a3
master
2018-Q4-OKR
feat/protobuf
feat/udp
fix/473
fix/no-custom-field
fix/reset-ping-stream
fix/revert-correct-external-addr
gx/update-jccl6u
gx/update-nza0mn
jenkinsfile
kevina/fix-go-vet
multistream-ping
punching
revert-276-update-go-detect-race
v6.0.23
v6.0.22
v6.0.21
v6.0.20
v6.0.19
v6.0.18
v6.0.17
v6.0.16
v6.0.15
v6.0.14
v6.0.13
v6.0.12
v6.0.11
v6.0.10
v6.0.9
v6.0.8
v6.0.7
v6.0.6
v6.0.5
v6.0.4
v6.0.3
v6.0.2
v6.0.1
v6.0.0
v5.0.21
v5.0.20
v5.0.19
v5.0.18
v5.0.17
v5.0.16
v5.0.15
v5.0.14
v5.0.13
v5.0.12
v5.0.11
v5.0.10
v5.0.9
v5.0.8
v5.0.7
v5.0.6
v5.0.5
v5.0.4
v5.0.3
v5.0.2
v5.0.1
v5.0.0
v4.5.5
v4.5.4
v4.5.3
v4.5.2
v4.5.1
v4.5.0
v4.4.5
v4.4.4
v4.4.3
v4.4.2
v4.4.1
v4.4.0
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
p2p/host/basic/basic_host.go
+3
-3
p2p/host/basic/basic_host.go
p2p/host/basic/natmgr.go
+44
-26
p2p/host/basic/natmgr.go
with
47 additions
and
29 deletions
+47
-29
p2p/host/basic/basic_host.go
View file @
e9c24fdc
...
...
@@ -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,7 +88,7 @@ type HostOpts struct {
// NATManager takes care of setting NAT port mappings, and discovering external addresses.
// If omitted, this will simply be disabled.
NATManager
*
NATManager
NATManager
NATManager
//
BandwidthReporter
metrics
.
Reporter
...
...
@@ -155,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
...
...
This diff is collapsed.
Click to expand it.
p2p/host/basic/natmgr.go
View file @
e9c24fdc
...
...
@@ -4,31 +4,49 @@ 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"
)
// NATManager takes care of adding + removing port mappings to the nat.
// 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.
//
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 +62,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 +118,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 +135,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 +193,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 +211,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
)
{
...
...
This diff is collapsed.
Click to expand it.
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
Menu
Projects
Groups
Snippets
Help