ams::mitm::p2p::UpnpPortMapper
ams::mitm::p2p::UpnpPortMapper
Section titled “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.
Members
Section titled “Members”m_mutex
Section titled “m_mutex”Type: os::Mutex
Mutex for thread safety.
m_urls
Section titled “m_urls”Type: UPNPUrls *
UPnP URLs structure (allocated dynamically)
m_data
Section titled “m_data”Type: IGDdatas *
IGD data structure (allocated dynamically)
m_lan_addr
Section titled “m_lan_addr”Type: char
Local IP address string.
m_available
Section titled “m_available”Type: bool
Whether discovery succeeded.
Methods
Section titled “Methods”GetInstance
Section titled “GetInstance”& GetInstance()Get the singleton instance.
Reference to the globalreturn
Returns: &
Methods
Section titled “Methods”UpnpPortMapper
Section titled “UpnpPortMapper”void UpnpPortMapper(const&)Deleted copy constructor.
Parameters:
param(const&)
operator=
Section titled “operator=”& operator=(const&)Parameters:
param(const&)
Returns: &
Discover
Section titled “Discover”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
IsAvailable
Section titled “IsAvailable”bool IsAvailable()Check if UPnP is available.
true ifsucceeded and IGD is availablereturn
Returns: bool
AddPortMapping
Section titled “AddPortMapping”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
DeletePortMapping
Section titled “DeletePortMapping”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
RefreshPortMapping
Section titled “RefreshPortMapping”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
GetExternalIPAddress
Section titled “GetExternalIPAddress”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
GetLocalIPAddress
Section titled “GetLocalIPAddress”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
GetLocalIPv4
Section titled “GetLocalIPv4”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
Cleanup
Section titled “Cleanup”void Cleanup()Release UPnP resources.
Frees internal UPnP structures. Called automatically on destruction but can be called manually to release resources early.Thread-safenote
Methods
Section titled “Methods”UpnpPortMapper
Section titled “UpnpPortMapper”void UpnpPortMapper()Private constructor (singleton)
~UpnpPortMapper
Section titled “~UpnpPortMapper”void ~UpnpPortMapper()Destructor.