ams::mitm::bsd::EphemeralPortPool
ams::mitm::bsd::EphemeralPortPool
Section titled “ams::mitm::bsd::EphemeralPortPool”Ephemeral Port Pool for Proxy Sockets.
This class manages allocation of ephemeral ports for proxy sockets. It maintains separate pools for UDP and TCP since ports can be shared between protocols.Allocation StrategyPorts are allocated using a simple linear search starting from the last allocated port (round-robin). This provides good distribution and avoids rapid reuse of recently freed ports. UsageUses two bitsets (one per protocol) of 16384 bits each = ~4KB total. This is acceptable for the sysmodule’s memory budget.
Members
Section titled “Members”m_mutex
Section titled “m_mutex”Type: os::Mutex
Mutex for thread safety.
m_udp_ports
Section titled “m_udp_ports”Type: std::bitset<>
Port allocation bitset for UDP.
m_tcp_ports
Section titled “m_tcp_ports”Type: std::bitset<>
Port allocation bitset for TCP.
m_udp_hint
Section titled “m_udp_hint”Type: size_t
Next allocation hint for UDP (round-robin)
m_tcp_hint
Section titled “m_tcp_hint”Type: size_t
Next allocation hint for TCP (round-robin)
Methods
Section titled “Methods”EphemeralPortPool
Section titled “EphemeralPortPool”void EphemeralPortPool()Default constructor.
Initializes the port pool with all ports available. Sets the allocation hint to the start of the range.
~EphemeralPortPool
Section titled “~EphemeralPortPool”void ~EphemeralPortPool()Destructor.
No special cleanup needed - bitsets are value types.
EphemeralPortPool
Section titled “EphemeralPortPool”void EphemeralPortPool(const&)Non-copyable.
Parameters:
param(const&)
operator=
Section titled “operator=”& operator=(const&)Parameters:
param(const&)
Returns: &
AllocatePort
Section titled “AllocatePort”uint16_t AllocatePort(ryu_ldn::bsd::ProtocolType protocol)Allocate an ephemeral port for the given protocol.
Searches for an available port starting from the last allocation point (round-robin strategy). This prevents rapid port reuse which can cause issues with TIME_WAIT states.protocolThe protocol type (TCP or UDP)paramThe allocated port number in host byte order, or 0 if none availablereturnThread-safe. Uses internal mutex.noteO(n) worst case where n = EPHEMERAL_PORT_COUNTnote
Parameters:
protocol(ryu_ldn::bsd::ProtocolType)
Returns: uint16_t
AllocateSpecificPort
Section titled “AllocateSpecificPort”bool AllocateSpecificPort(uint16_t port, ryu_ldn::bsd::ProtocolType protocol)Allocate a specific port for the given protocol.
Attempts to allocate a specific port. If the port is already allocated for this protocol, returns false.portThe port number to allocate (must be in ephemeral range)protocolThe protocol type (TCP or UDP)paramtrue if allocation succeeded, false if already in use or invalidreturnThread-safe. Uses internal mutex.noteO(1) operationnote
Parameters:
port(uint16_t)protocol(ryu_ldn::bsd::ProtocolType)
Returns: bool
ReleasePort
Section titled “ReleasePort”void ReleasePort(uint16_t port, ryu_ldn::bsd::ProtocolType protocol)Release a previously allocated port.
Returns the port to the pool for future allocation. Releasing an unallocated port is a no-op (not an error).portThe port number to release (must be in ephemeral range)protocolThe protocol type (TCP or UDP)paramThread-safe. Uses internal mutex.noteO(1) operationnote
Parameters:
port(uint16_t)protocol(ryu_ldn::bsd::ProtocolType)
IsPortAllocated
Section titled “IsPortAllocated”bool IsPortAllocated(uint16_t port, ryu_ldn::bsd::ProtocolType protocol)Check if a port is currently allocated.
portThe port number to checkprotocolThe protocol type (TCP or UDP)paramtrue if the port is allocated, false otherwisereturnThread-safe. Uses internal mutex.noteO(1) operationnote
Parameters:
port(uint16_t)protocol(ryu_ldn::bsd::ProtocolType)
Returns: bool
GetAvailableCount
Section titled “GetAvailableCount”size_t GetAvailableCount(ryu_ldn::bsd::ProtocolType protocol)Get the number of available ports for a protocol.
protocolThe protocol type (TCP or UDP)paramNumber of ports still available for allocationreturnThread-safe. Uses internal mutex.note
Parameters:
protocol(ryu_ldn::bsd::ProtocolType)
Returns: size_t
ReleaseAll
Section titled “ReleaseAll”void ReleaseAll()Release all allocated ports.
Useful for cleanup when the LDN session ends.Thread-safe. Uses internal mutex.note
Methods
Section titled “Methods”PortToIndex
Section titled “PortToIndex”constexpr size_t PortToIndex(uint16_t port)Convert port number to bitset index.
portPort number in host byte orderparamIndex into the bitset, or EPHEMERAL_PORT_COUNT if invalidreturn
Parameters:
port(uint16_t)
Returns: constexpr size_t
IndexToPort
Section titled “IndexToPort”constexpr uint16_t IndexToPort(size_t index)Convert bitset index to port number.
indexIndex into the bitsetparamPort number in host byte orderreturn
Parameters:
index(size_t)
Returns: constexpr uint16_t
Methods
Section titled “Methods”GetBitset
Section titled “GetBitset”std::bitset<> & GetBitset(ryu_ldn::bsd::ProtocolType protocol)Get the bitset for the given protocol.
protocolThe protocol typeparamReference to the protocol’s bitsetreturn
Parameters:
protocol(ryu_ldn::bsd::ProtocolType)
Returns: std::bitset<> &
GetBitset
Section titled “GetBitset”const std::bitset<> & GetBitset(ryu_ldn::bsd::ProtocolType protocol)Get the bitset for the given protocol (const)
protocolThe protocol typeparamConst reference to the protocol’s bitsetreturn
Parameters:
protocol(ryu_ldn::bsd::ProtocolType)
Returns: const std::bitset<> &