Skip to content

Commit

Permalink
nat: do not shortcircuit permanent mappings
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Kubuxu committed Aug 29, 2016
1 parent db51f28 commit 0fc5e42
Showing 1 changed file with 8 additions and 22 deletions.
30 changes: 8 additions & 22 deletions p2p/nat/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ type mapping struct {
intaddr ma.Multiaddr
proc goprocess.Process

comment string

cached ma.Multiaddr
cacheTime time.Time
cacheLk sync.Mutex
Expand Down Expand Up @@ -198,18 +200,6 @@ func (m *mapping) setExternalPort(p int) {
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 {
m.Lock()
defer m.Unlock()
Expand Down Expand Up @@ -346,20 +336,17 @@ func (nat *NAT) NewMapping(maddr ma.Multiaddr) (Mapping, error) {

func (nat *NAT) establishMapping(m *mapping) {
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())
permanent := false

newport, err := nat.nat.AddPortMapping(m.Protocol(), m.InternalPort(), "libp2p", MappingDuration)
comment := "libp2p"
if m.comment != "" {
comment = "libp2p-" + comment
}

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(), "libp2p", 0)
permanent = (err == nil)
newport, err = nat.nat.AddPortMapping(m.Protocol(), m.InternalPort(), comment, 0)
}

failure := func() {
Expand All @@ -379,7 +366,6 @@ func (nat *NAT) establishMapping(m *mapping) {
return
}

m.setPermanent(permanent)
m.setExternalPort(newport)
ext, err := m.ExternalAddr()
if err != nil {
Expand Down

0 comments on commit 0fc5e42

Please sign in to comment.