Unverified Commit 51ffdf87 authored by Jakub Sztandera's avatar Jakub Sztandera
Browse files

nat: do not shortcircuit permanent mappings

If we use same NAT agent and call for the same permanent mapping
again we get the same mapping, no harm done.

If router dies, we will remap again.
Just pros, no cons.
parent a3bf1687
...@@ -163,6 +163,8 @@ type mapping struct { ...@@ -163,6 +163,8 @@ type mapping struct {
intaddr ma.Multiaddr intaddr ma.Multiaddr
proc goprocess.Process proc goprocess.Process
comment string
cached ma.Multiaddr cached ma.Multiaddr
cacheTime time.Time cacheTime time.Time
cacheLk sync.Mutex cacheLk sync.Mutex
...@@ -198,18 +200,6 @@ func (m *mapping) setExternalPort(p int) { ...@@ -198,18 +200,6 @@ func (m *mapping) setExternalPort(p int) {
m.extport = p m.extport = p
} }
func (m *mapping) setPermanent(p bool) {
m.Lock()
defer m.Unlock()
m.permanent = p
}
func (m *mapping) isPermanent() bool {
m.Lock()
defer m.Unlock()
return m.permanent
}
func (m *mapping) InternalAddr() ma.Multiaddr { func (m *mapping) InternalAddr() ma.Multiaddr {
m.Lock() m.Lock()
defer m.Unlock() defer m.Unlock()
...@@ -346,20 +336,17 @@ func (nat *NAT) NewMapping(maddr ma.Multiaddr) (Mapping, error) { ...@@ -346,20 +336,17 @@ func (nat *NAT) NewMapping(maddr ma.Multiaddr) (Mapping, error) {
func (nat *NAT) establishMapping(m *mapping) { func (nat *NAT) establishMapping(m *mapping) {
oldport := m.ExternalPort() oldport := m.ExternalPort()
if oldport != 0 && m.isPermanent() {
// mapping was already established and it is permanent
return
}
log.Debugf("Attempting port map: %s/%d", m.Protocol(), m.InternalPort()) log.Debugf("Attempting port map: %s/%d", m.Protocol(), m.InternalPort())
permanent := false comment := "libp2p"
if m.comment != "" {
newport, err := nat.nat.AddPortMapping(m.Protocol(), m.InternalPort(), "libp2p", MappingDuration) comment = "libp2p-" + m.comment
}
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(), "libp2p", 0) newport, err = nat.nat.AddPortMapping(m.Protocol(), m.InternalPort(), comment, 0)
permanent = (err == nil)
} }
failure := func() { failure := func() {
...@@ -379,7 +366,6 @@ func (nat *NAT) establishMapping(m *mapping) { ...@@ -379,7 +366,6 @@ func (nat *NAT) establishMapping(m *mapping) {
return return
} }
m.setPermanent(permanent)
m.setExternalPort(newport) m.setExternalPort(newport)
ext, err := m.ExternalAddr() ext, err := m.ExternalAddr()
if err != nil { if err != nil {
......
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