Skip to content

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.

Type: os::Mutex

Mutex for thread safety.

Type: std::bitset<>

Port allocation bitset for UDP.

Type: std::bitset<>

Port allocation bitset for TCP.

Type: size_t

Next allocation hint for UDP (round-robin)

Type: size_t

Next allocation hint for TCP (round-robin)

void EphemeralPortPool()

Default constructor.

Initializes the port pool with all ports available. Sets the allocation hint to the start of the range.

void ~EphemeralPortPool()

Destructor.

No special cleanup needed - bitsets are value types.

void EphemeralPortPool(const&)

Non-copyable.

Parameters:

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

Parameters:

  • param (const&)

Returns: &

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

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

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)
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

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

void ReleaseAll()

Release all allocated ports.

Useful for cleanup when the LDN session ends.Thread-safe. Uses internal mutex.note

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

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

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<> &

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<> &