Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-p2p-nat
Commits
29b43c99
Commit
29b43c99
authored
8 years ago
by
Jakub Sztandera
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
nat: add locks around nat
parent
feca3184
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
7 additions
and
2 deletions
+7
-2
mapping.go
mapping.go
+2
-0
nat.go
nat.go
+5
-2
No files found.
mapping.go
View file @
29b43c99
...
...
@@ -106,7 +106,9 @@ func (m *mapping) ExternalAddr() (ma.Multiaddr, error) {
return
nil
,
ErrNoMapping
}
m
.
nat
.
natmu
.
Lock
()
ip
,
err
:=
m
.
nat
.
nat
.
GetExternalAddress
()
m
.
nat
.
natmu
.
Unlock
()
if
err
!=
nil
{
return
nil
,
err
}
...
...
This diff is collapsed.
Click to expand it.
nat.go
View file @
29b43c99
...
...
@@ -52,8 +52,9 @@ func DiscoverNAT() *NAT {
// service that will periodically renew port mappings,
// and keep an up-to-date list of all the external addresses.
type
NAT
struct
{
nat
nat
.
NAT
proc
goprocess
.
Process
// manages nat mappings lifecycle
natmu
sync
.
Mutex
nat
nat
.
NAT
proc
goprocess
.
Process
// manages nat mappings lifecycle
mappingmu
sync
.
RWMutex
// guards mappings
mappings
map
[
*
mapping
]
struct
{}
...
...
@@ -170,11 +171,13 @@ func (nat *NAT) establishMapping(m *mapping) {
comment
=
"libp2p-"
+
m
.
comment
}
nat
.
natmu
.
Lock
()
newport
,
err
:=
nat
.
nat
.
AddPortMapping
(
m
.
Protocol
(),
m
.
InternalPort
(),
comment
,
MappingDuration
)
if
err
!=
nil
{
// Some hardware does not support mappings with timeout, so try that
newport
,
err
=
nat
.
nat
.
AddPortMapping
(
m
.
Protocol
(),
m
.
InternalPort
(),
comment
,
0
)
}
nat
.
natmu
.
Lock
()
failure
:=
func
()
{
m
.
setExternalPort
(
0
)
// clear mapping
...
...
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