package nat

import "github.com/libp2p/go-libp2p/p2p/net/nat"

Index

Constants

const CacheTime = 15 * time.Second

CacheTime is the time a mapping will cache an external address for

const MappingDuration = time.Minute

MappingDuration is a default port mapping duration. Port mappings are renewed every (MappingDuration / 3)

Variables

var ErrNoMapping = errors.New("mapping not established")

ErrNoMapping signals no mapping exists for an address

Types

type NAT

type NAT struct {
	// contains filtered or unexported fields
}

NAT is an object that manages address port mappings in NATs (Network Address Translators). It is a long-running service that will periodically renew port mappings, and keep an up-to-date list of all the external addresses.

func DiscoverNAT

func DiscoverNAT(ctx context.Context) (*NAT, error)

DiscoverNAT looks for a NAT device in the network and returns an object that can manage port mappings.

func (*NAT) AddMapping

func (nat *NAT) AddMapping(ctx context.Context, protocol string, port int) error

AddMapping attempts to construct a mapping on protocol and internal port. It blocks until a mapping was established. Once added, it periodically renews the mapping.

May not succeed, and mappings may change over time; NAT devices may not respect our port requests, and even lie.

func (*NAT) Close

func (nat *NAT) Close() error

Close shuts down all port mappings. NAT can no longer be used.

func (*NAT) GetMapping

func (nat *NAT) GetMapping(protocol string, port int) (addr netip.AddrPort, found bool)

func (*NAT) RemoveMapping

func (nat *NAT) RemoveMapping(ctx context.Context, protocol string, port int) error

RemoveMapping removes a port mapping. It blocks until the NAT has removed the mapping.

Source Files

nat.go

Directories

PathSynopsis
p2p/net/nat/internal
Version
v0.42.0 (latest)
Published
Jun 18, 2025
Platform
linux/amd64
Imports
9 packages
Last checked
3 weeks ago

Tools for package owners.