Commit 29b43c99 authored by Jakub Sztandera's avatar Jakub Sztandera

nat: add locks around nat

parent feca3184
...@@ -106,7 +106,9 @@ func (m *mapping) ExternalAddr() (ma.Multiaddr, error) { ...@@ -106,7 +106,9 @@ func (m *mapping) ExternalAddr() (ma.Multiaddr, error) {
return nil, ErrNoMapping return nil, ErrNoMapping
} }
m.nat.natmu.Lock()
ip, err := m.nat.nat.GetExternalAddress() ip, err := m.nat.nat.GetExternalAddress()
m.nat.natmu.Unlock()
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
...@@ -52,8 +52,9 @@ func DiscoverNAT() *NAT { ...@@ -52,8 +52,9 @@ func DiscoverNAT() *NAT {
// service that will periodically renew port mappings, // service that will periodically renew port mappings,
// and keep an up-to-date list of all the external addresses. // and keep an up-to-date list of all the external addresses.
type NAT struct { type NAT struct {
nat nat.NAT natmu sync.Mutex
proc goprocess.Process // manages nat mappings lifecycle nat nat.NAT
proc goprocess.Process // manages nat mappings lifecycle
mappingmu sync.RWMutex // guards mappings mappingmu sync.RWMutex // guards mappings
mappings map[*mapping]struct{} mappings map[*mapping]struct{}
...@@ -170,11 +171,13 @@ func (nat *NAT) establishMapping(m *mapping) { ...@@ -170,11 +171,13 @@ func (nat *NAT) establishMapping(m *mapping) {
comment = "libp2p-" + m.comment comment = "libp2p-" + m.comment
} }
nat.natmu.Lock()
newport, err := nat.nat.AddPortMapping(m.Protocol(), m.InternalPort(), comment, MappingDuration) newport, err := nat.nat.AddPortMapping(m.Protocol(), m.InternalPort(), comment, MappingDuration)
if err != nil { if err != nil {
// Some hardware does not support mappings with timeout, so try that // Some hardware does not support mappings with timeout, so try that
newport, err = nat.nat.AddPortMapping(m.Protocol(), m.InternalPort(), comment, 0) newport, err = nat.nat.AddPortMapping(m.Protocol(), m.InternalPort(), comment, 0)
} }
nat.natmu.Lock()
failure := func() { failure := func() {
m.setExternalPort(0) // clear mapping m.setExternalPort(0) // clear mapping
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment