Skip to content

Commit

Permalink
nat: add HasDiscoveredNAT method for checking NAT environments (#2358)
Browse files Browse the repository at this point in the history
* nat: add HasNAT method for checking NAT environments

* rename to better variable
  • Loading branch information
sukunrt authored Jun 12, 2023
1 parent 046f112 commit f87f6db
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
5 changes: 2 additions & 3 deletions p2p/host/basic/basic_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -843,11 +843,10 @@ func (h *BasicHost) AllAddrs() []ma.Multiaddr {

finalAddrs = network.DedupAddrs(finalAddrs)

// natmgr is nil if we do not use nat option;
if h.natmgr != nil {
// use nat mappings if we have them
if h.natmgr != nil && h.natmgr.HasDiscoveredNAT() {
// We have successfully mapped ports on our NAT. Use those
// instead of observed addresses (mostly).

// Next, apply this mapping to our addresses.
for _, listen := range listenAddrs {
extMaddr := h.natmgr.GetMapping(listen)
Expand Down
7 changes: 7 additions & 0 deletions p2p/host/basic/natmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
// and tries to obtain port mappings for those.
type NATManager interface {
GetMapping(ma.Multiaddr) ma.Multiaddr
HasDiscoveredNAT() bool
io.Closer
}

Expand Down Expand Up @@ -86,6 +87,12 @@ func (nmgr *natManager) Close() error {
return nil
}

func (nmgr *natManager) HasDiscoveredNAT() bool {
nmgr.natMx.RLock()
defer nmgr.natMx.RUnlock()
return nmgr.nat != nil
}

func (nmgr *natManager) background(ctx context.Context) {
defer nmgr.refCount.Done()

Expand Down

0 comments on commit f87f6db

Please sign in to comment.