Skip to content

Commit

Permalink
nat: add locks around nat
Browse files Browse the repository at this point in the history
  • Loading branch information
Kubuxu committed Sep 12, 2016
1 parent 8d44239 commit 39eb32a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 2 additions & 0 deletions p2p/nat/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,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
}
Expand Down
7 changes: 5 additions & 2 deletions p2p/nat/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 39eb32a

Please sign in to comment.