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
78eb2172
Commit
78eb2172
authored
Sep 08, 2015
by
Jeromy
Browse files
WIP: quick fix to the NAT spam too many open fd's bug
License: MIT Signed-off-by:
Jeromy
<
jeromyj@gmail.com
>
parent
72696c98
Changes
1
Hide whitespace changes
Inline
Side-by-side
nat/nat.go
View file @
78eb2172
...
@@ -29,12 +29,17 @@ var log = eventlog.Logger("nat")
...
@@ -29,12 +29,17 @@ var log = eventlog.Logger("nat")
// Port mappings are renewed every (MappingDuration / 3)
// Port mappings are renewed every (MappingDuration / 3)
const
MappingDuration
=
time
.
Second
*
60
const
MappingDuration
=
time
.
Second
*
60
// CacheTime is the time a mapping will cache an external address for
const
CacheTime
=
time
.
Second
*
15
// DiscoverNAT looks for a NAT device in the network and
// DiscoverNAT looks for a NAT device in the network and
// returns an object that can manage port mappings.
// returns an object that can manage port mappings.
func
DiscoverNAT
()
*
NAT
{
func
DiscoverNAT
()
*
NAT
{
log
.
Error
(
"DISCOVER NAT"
)
nat
,
err
:=
nat
.
DiscoverGateway
()
nat
,
err
:=
nat
.
DiscoverGateway
()
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Debug
(
"DiscoverGateway error:"
,
err
)
log
.
Debug
(
"DiscoverGateway error:"
,
err
)
log
.
Error
(
"DISCOVER GATEWAY ERROR: "
,
err
)
return
nil
return
nil
}
}
addr
,
err
:=
nat
.
GetDeviceAddress
()
addr
,
err
:=
nat
.
GetDeviceAddress
()
...
@@ -43,6 +48,7 @@ func DiscoverNAT() *NAT {
...
@@ -43,6 +48,7 @@ func DiscoverNAT() *NAT {
}
else
{
}
else
{
log
.
Debug
(
"DiscoverGateway address:"
,
addr
)
log
.
Debug
(
"DiscoverGateway address:"
,
addr
)
}
}
log
.
Error
(
"NEW NAT!"
)
return
newNAT
(
nat
)
return
newNAT
(
nat
)
}
}
...
@@ -159,6 +165,9 @@ type mapping struct {
...
@@ -159,6 +165,9 @@ type mapping struct {
extport
int
extport
int
intaddr
ma
.
Multiaddr
intaddr
ma
.
Multiaddr
proc
goprocess
.
Process
proc
goprocess
.
Process
cached
ma
.
Multiaddr
cacheTime
time
.
Time
}
}
func
(
m
*
mapping
)
NAT
()
*
NAT
{
func
(
m
*
mapping
)
NAT
()
*
NAT
{
...
@@ -198,6 +207,10 @@ func (m *mapping) InternalAddr() ma.Multiaddr {
...
@@ -198,6 +207,10 @@ func (m *mapping) InternalAddr() ma.Multiaddr {
}
}
func
(
m
*
mapping
)
ExternalAddr
()
(
ma
.
Multiaddr
,
error
)
{
func
(
m
*
mapping
)
ExternalAddr
()
(
ma
.
Multiaddr
,
error
)
{
if
time
.
Now
()
.
Sub
(
m
.
cacheTime
)
<
CacheTime
{
return
m
.
cached
,
nil
}
if
m
.
ExternalPort
()
==
0
{
// dont even try right now.
if
m
.
ExternalPort
()
==
0
{
// dont even try right now.
return
nil
,
ErrNoMapping
return
nil
,
ErrNoMapping
}
}
...
@@ -224,6 +237,9 @@ func (m *mapping) ExternalAddr() (ma.Multiaddr, error) {
...
@@ -224,6 +237,9 @@ func (m *mapping) ExternalAddr() (ma.Multiaddr, error) {
}
}
maddr2
:=
ipmaddr
.
Encapsulate
(
tcp
)
maddr2
:=
ipmaddr
.
Encapsulate
(
tcp
)
m
.
cached
=
maddr2
m
.
cacheTime
=
time
.
Now
()
return
maddr2
,
nil
return
maddr2
,
nil
}
}
...
@@ -266,6 +282,7 @@ func (nat *NAT) rmMapping(m *mapping) {
...
@@ -266,6 +282,7 @@ func (nat *NAT) rmMapping(m *mapping) {
// Clients should not store the mapped results, but rather always
// Clients should not store the mapped results, but rather always
// poll our object for the latest mappings.
// poll our object for the latest mappings.
func
(
nat
*
NAT
)
NewMapping
(
maddr
ma
.
Multiaddr
)
(
Mapping
,
error
)
{
func
(
nat
*
NAT
)
NewMapping
(
maddr
ma
.
Multiaddr
)
(
Mapping
,
error
)
{
log
.
Error
(
"NEW MAPPING!"
)
if
nat
==
nil
{
if
nat
==
nil
{
return
nil
,
fmt
.
Errorf
(
"no nat available"
)
return
nil
,
fmt
.
Errorf
(
"no nat available"
)
}
}
...
@@ -313,6 +330,7 @@ func (nat *NAT) NewMapping(maddr ma.Multiaddr) (Mapping, error) {
...
@@ -313,6 +330,7 @@ func (nat *NAT) NewMapping(maddr ma.Multiaddr) (Mapping, error) {
}
}
func
(
nat
*
NAT
)
establishMapping
(
m
*
mapping
)
{
func
(
nat
*
NAT
)
establishMapping
(
m
*
mapping
)
{
log
.
Error
(
"establishMapping!"
)
oldport
:=
m
.
ExternalPort
()
oldport
:=
m
.
ExternalPort
()
log
.
Debugf
(
"Attempting port map: %s/%d"
,
m
.
Protocol
(),
m
.
InternalPort
())
log
.
Debugf
(
"Attempting port map: %s/%d"
,
m
.
Protocol
(),
m
.
InternalPort
())
newport
,
err
:=
nat
.
nat
.
AddPortMapping
(
m
.
Protocol
(),
m
.
InternalPort
(),
"http"
,
MappingDuration
)
newport
,
err
:=
nat
.
nat
.
AddPortMapping
(
m
.
Protocol
(),
m
.
InternalPort
(),
"http"
,
MappingDuration
)
...
@@ -413,6 +431,7 @@ func (nat *NAT) MappedAddrs() map[ma.Multiaddr]ma.Multiaddr {
...
@@ -413,6 +431,7 @@ func (nat *NAT) MappedAddrs() map[ma.Multiaddr]ma.Multiaddr {
// This set of mappings _may not_ be correct, as NAT devices are finicky.
// This set of mappings _may not_ be correct, as NAT devices are finicky.
// Consider this with _best effort_ semantics.
// Consider this with _best effort_ semantics.
func
(
nat
*
NAT
)
ExternalAddrs
()
[]
ma
.
Multiaddr
{
func
(
nat
*
NAT
)
ExternalAddrs
()
[]
ma
.
Multiaddr
{
log
.
Error
(
"EXTERNAL ADDRS"
)
mappings
:=
nat
.
Mappings
()
mappings
:=
nat
.
Mappings
()
addrs
:=
make
([]
ma
.
Multiaddr
,
0
,
len
(
mappings
))
addrs
:=
make
([]
ma
.
Multiaddr
,
0
,
len
(
mappings
))
for
_
,
m
:=
range
mappings
{
for
_
,
m
:=
range
mappings
{
...
...
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