github.com/decred/dcrlnd@v0.7.6/nat/traversal.go (about)

     1  package nat
     2  
     3  import (
     4  	"errors"
     5  	"net"
     6  )
     7  
     8  var (
     9  	// private24BitBlock contains the set of private IPv4 addresses within
    10  	// the 10.0.0.0/8 adddress space.
    11  	private24BitBlock *net.IPNet
    12  
    13  	// private20BitBlock contains the set of private IPv4 addresses within
    14  	// the 172.16.0.0/12 address space.
    15  	private20BitBlock *net.IPNet
    16  
    17  	// private16BitBlock contains the set of private IPv4 addresses within
    18  	// the 192.168.0.0/16 address space.
    19  	private16BitBlock *net.IPNet
    20  
    21  	// ErrMultipleNAT is an error returned when multiple NATs have been
    22  	// detected.
    23  	ErrMultipleNAT = errors.New("multiple NATs detected")
    24  )
    25  
    26  func init() {
    27  	_, private24BitBlock, _ = net.ParseCIDR("10.0.0.0/8")
    28  	_, private20BitBlock, _ = net.ParseCIDR("172.16.0.0/12")
    29  	_, private16BitBlock, _ = net.ParseCIDR("192.168.0.0/16")
    30  }
    31  
    32  // Traversal is an interface that brings together the different NAT traversal
    33  // techniques.
    34  type Traversal interface {
    35  	// ExternalIP returns the external IP address.
    36  	ExternalIP() (net.IP, error)
    37  
    38  	// AddPortMapping adds a port mapping for the given port between the
    39  	// private and public addresses.
    40  	AddPortMapping(port uint16) error
    41  
    42  	// DeletePortMapping deletes a port mapping for the given port between
    43  	// the private and public addresses.
    44  	DeletePortMapping(port uint16) error
    45  
    46  	// ForwardedPorts returns the ports currently being forwarded using NAT
    47  	// traversal.
    48  	ForwardedPorts() []uint16
    49  
    50  	// Name returns the name of the specific NAT traversal technique used.
    51  	Name() string
    52  }
    53  
    54  // isPrivateIP determines if the IP is private.
    55  func isPrivateIP(ip net.IP) bool {
    56  	return private24BitBlock.Contains(ip) ||
    57  		private20BitBlock.Contains(ip) || private16BitBlock.Contains(ip)
    58  }