Skip to content

ams::mitm::p2p::UpnpPortMapper

UPnP Port Mapper.

Manages UPnP IGD discovery and port mapping for P2P connections. This is a singleton class as there’s only one network interface.Thread SafetyAll methods are thread-safe. Discovery and port mapping operations are protected by a mutex. HandlingAll methods return bool indicating success/failure. Detailed error information is logged but not exposed to callers.

Type: os::Mutex

Mutex for thread safety.

Type: UPNPUrls *

UPnP URLs structure (allocated dynamically)

Type: IGDdatas *

IGD data structure (allocated dynamically)

Type: char

Local IP address string.

Type: bool

Whether discovery succeeded.

& GetInstance()

Get the singleton instance.

Reference to the globalreturn

Returns: &

void UpnpPortMapper(const&)

Deleted copy constructor.

Parameters:

  • param (const&)
& operator=(const&)

Parameters:

  • param (const&)

Returns: &

bool Discover()

Discover UPnP IGD on the network.

Performs SSDP discovery to find a UPnP-enabled router. This is a blocking operation with a 2.5 second timeout.true if an IGD was found, false otherwisereturnThread-safenoteMust be called before any port mapping operationsnote

Returns: bool

bool IsAvailable()

Check if UPnP is available.

true ifsucceeded and IGD is availablereturn

Returns: bool

bool AddPortMapping(uint16_t internal_port, uint16_t external_port, const char * description, int lease_duration)

Add a TCP port mapping.

Opens a port on the router, forwarding external traffic to this device.internal_portLocal port to forward to (host byte order)external_portExternal port to open (host byte order)descriptionHuman-readable description for the mappinglease_durationMapping duration in seconds (0 = permanent)paramtrue if mapping was created, false on errorreturnThread-safenoteRequiresto have succeedednote

Parameters:

  • internal_port (uint16_t)
  • external_port (uint16_t)
  • description (const char *)
  • lease_duration (int)

Returns: bool

bool DeletePortMapping(uint16_t external_port)

Delete a TCP port mapping.

Removes a previously created port mapping.external_portExternal port to close (host byte order)paramtrue if mapping was deleted, false on errorreturnThread-safenote

Parameters:

  • external_port (uint16_t)

Returns: bool

bool RefreshPortMapping(uint16_t internal_port, uint16_t external_port, const char * description)

Refresh an existing port mapping lease.

Should be called periodically (every PORT_LEASE_RENEW seconds) to keep the mapping active.internal_portLocal portexternal_portExternal portdescriptionDescription (must match original)paramtrue if lease was refreshed, false on errorreturn

Parameters:

  • internal_port (uint16_t)
  • external_port (uint16_t)
  • description (const char *)

Returns: bool

bool GetExternalIPAddress(char * ip_out, size_t ip_len)

Get the external (public) IP address.

ip_outBuffer to receive IP address stringip_lenSize of buffer (should be at least 16 bytes)paramtrue if IP was retrieved, false on errorreturnThread-safenote

Parameters:

  • ip_out (char *)
  • ip_len (size_t)

Returns: bool

bool GetLocalIPAddress(char * ip_out, size_t ip_len)

Get the local (LAN) IP address.

ip_outBuffer to receive IP address stringip_lenSize of buffer (should be at least 16 bytes)paramtrue if IP was retrieved, false on errorreturn

Parameters:

  • ip_out (char *)
  • ip_len (size_t)

Returns: bool

uint32_t GetLocalIPv4()

Get the local IP as a 32-bit value.

Local IP in host byte order, or 0 if not availablereturn

Returns: uint32_t

void Cleanup()

Release UPnP resources.

Frees internal UPnP structures. Called automatically on destruction but can be called manually to release resources early.Thread-safenote

void UpnpPortMapper()

Private constructor (singleton)

void ~UpnpPortMapper()

Destructor.