Skip to content

ams::mitm::bsd::ProxySocketManager

Proxy Socket Manager.

Central registry for all proxy sockets. Manages the mapping between BSD file descriptors andinstances.Key OperationsCreateProxySocket: Allocate a new proxy socket for an fdGetProxySocket: Look up a proxy socket by fdCloseProxySocket: Clean up a proxy socketRouteIncomingData: Route ProxyData packets to the correct socket Descriptor StrategyWe use the game’s real file descriptors (from Socket() calls to the real BSD service) as keys. When we detect an LDN address in Bind/Connect, we create aassociated with that fd. Subsequent Send/Recv calls check if the fd has an associatedand route accordingly.

Type: os::Mutex

Mutex for thread safety.

Type: std::unordered_map< s32, std::unique_ptr<> >

Map of file descriptor to.

Type: EphemeralPortPool

Ephemeral port pool.

Type: uint32_t

Local LDN IP address (host byte order)

Type: SendProxyDataCallback

Callback for sending ProxyData to LDN server.

Type: SendProxyConnectCallback

Callback for sending ProxyConnect to LDN server (TCP handshake)

Type: SendProxyConnectReplyCallback

Callback for sending ProxyConnectReply to LDN server (TCP accept)

Type: SendProxyDisconnectCallback

Callback for sending ProxyDisconnect to LDN server (TCP close)

Type: std::deque< PendingPacket >

Type: constexpr size_t

& GetInstance()

Get the singleton instance.

Reference to the globalreturn

Returns: &

bool IsLdnAddress(uint32_t ip)

Check if an IP address is in the LDN network.

ipIP address in host byte orderparamtrue if ip is in 10.114.0.0/16return

Parameters:

  • ip (uint32_t)

Returns: bool

bool IsLdnAddress(const& addr)

Check if a sockaddr_in is in the LDN network.

addrAddress to checkparamtrue if address is in 10.114.0.0/16return

Parameters:

  • addr (const&)

Returns: bool

void ProxySocketManager(const&)

Deleted copy constructor.

Parameters:

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

Parameters:

  • param (const&)

Returns: &

* CreateProxySocket(s32 fd, ryu_ldn::bsd::SocketType type, ryu_ldn::bsd::ProtocolType protocol)

Create a new proxy socket for the given file descriptor.

Called when we detect that a socket should be proxied (LDN address in Bind or Connect).fdFile descriptor from the real BSD servicetypeSocket type (Stream or Dgram)protocolProtocol type (Tcp or Udp)paramPointer to the created, or nullptr if failedreturnThread-safenoteReturns nullptr if fd already has a proxy socketnote

Parameters:

  • fd (s32)
  • type (ryu_ldn::bsd::SocketType)
  • protocol (ryu_ldn::bsd::ProtocolType)

Returns: *

* GetProxySocket(s32 fd)

Get the proxy socket for a file descriptor.

fdFile descriptor to look upparamPointer to the, or nullptr if not a proxy socketreturnThread-safenoteThe returned pointer is valid as long as the socket is not closednote

Parameters:

  • fd (s32)

Returns: *

bool IsProxySocket(s32 fd)

Check if a file descriptor has an associated proxy socket.

fdFile descriptor to checkparamtrue if fd is a proxy socket, false otherwisereturnThread-safenote

Parameters:

  • fd (s32)

Returns: bool

bool CloseProxySocket(s32 fd)

Close and remove a proxy socket.

Called when the game closes the socket.fdFile descriptor to closeparamtrue if a proxy socket was closed, false if not foundreturnThread-safenoteAlso releases any allocated ephemeral portnote

Parameters:

  • fd (s32)

Returns: bool

void CloseAllProxySockets()

Close all proxy sockets.

Called when the LDN session ends or during cleanup.Thread-safenote

void Reset()

Reset the entire manager state.

Closes all sockets, clears pending packets, resets local IP. Should be called when the LDN session ends to prevent memory leaks.Thread-safenote

uint16_t AllocatePort(ryu_ldn::bsd::ProtocolType protocol)

Allocate an ephemeral port.

protocolProtocol type (Tcp or Udp)paramAllocated port in host byte order, or 0 if none availablereturnThread-safe (delegated to)note

Parameters:

  • protocol (ryu_ldn::bsd::ProtocolType)

Returns: uint16_t

bool ReservePort(uint16_t port, ryu_ldn::bsd::ProtocolType protocol)

Reserve a specific port.

Used when the game binds to a specific port.portPort number in host byte orderprotocolProtocol typeparamtrue if reserved, false if already in usereturnThread-safenote

Parameters:

  • port (uint16_t)
  • protocol (ryu_ldn::bsd::ProtocolType)

Returns: bool

void ReleasePort(uint16_t port, ryu_ldn::bsd::ProtocolType protocol)

Release a port back to the pool.

portPort number in host byte orderprotocolProtocol typeparamThread-safenote

Parameters:

  • port (uint16_t)
  • protocol (ryu_ldn::bsd::ProtocolType)
bool RouteIncomingData(uint32_t source_ip, uint16_t source_port, uint32_t dest_ip, uint16_t dest_port, ryu_ldn::bsd::ProtocolType protocol, const void * data, size_t data_len)

Route incoming ProxyData to the correct socket.

Called by the LDN MITM service when a ProxyData packet is received. Finds the socket that matches the destination address/port and queues the data for that socket.source_ipSource IP (host byte order)source_portSource port (host byte order)dest_ipDestination IP (host byte order)dest_portDestination port (host byte order)protocolProtocol typedataPacket payloaddata_lenPayload lengthparamtrue if data was routed to a socket, false if no matching socketreturnThread-safenote

Parameters:

  • source_ip (uint32_t)
  • source_port (uint16_t)
  • dest_ip (uint32_t)
  • dest_port (uint16_t)
  • protocol (ryu_ldn::bsd::ProtocolType)
  • data (const void *)
  • data_len (size_t)

Returns: bool

void SetSendCallback(SendProxyDataCallback callback)

Set the callback for sending ProxyData to the LDN server.

Called by the LDN MITM service during initialization.callbackFunction to call when proxy sockets need to send dataparamThread-safenote

Parameters:

  • callback (SendProxyDataCallback)
bool SendProxyData(uint32_t source_ip, uint16_t source_port, uint32_t dest_ip, uint16_t dest_port, ryu_ldn::bsd::ProtocolType protocol, const void * data, size_t data_len)

Send data through a proxy socket.

Called byto actually transmit data via LDN.source_ipSource IP (host byte order)source_portSource port (host byte order)dest_ipDestination IP (host byte order)dest_portDestination port (host byte order)protocolProtocol typedataPacket payloaddata_lenPayload lengthparamtrue if data was sent, false if no callback registered or send failedreturnThread-safenote

Parameters:

  • source_ip (uint32_t)
  • source_port (uint16_t)
  • dest_ip (uint32_t)
  • dest_port (uint16_t)
  • protocol (ryu_ldn::bsd::ProtocolType)
  • data (const void *)
  • data_len (size_t)

Returns: bool

void SetProxyConnectCallback(SendProxyConnectCallback callback)

Set the callback for sending ProxyConnect to the LDN server.

Called by the LDN MITM service during initialization.callbackFunction to call when TCP proxy sockets call Connect()paramThread-safenote

Parameters:

  • callback (SendProxyConnectCallback)
bool SendProxyConnect(uint32_t source_ip, uint16_t source_port, uint32_t dest_ip, uint16_t dest_port, ryu_ldn::bsd::ProtocolType protocol)

Send ProxyConnect request for TCP connection handshake.

Called byfor TCP sockets.source_ipSource IP (host byte order)source_portSource port (host byte order)dest_ipDestination IP (host byte order)dest_portDestination port (host byte order)protocolProtocol type (TCP)paramtrue if request was sent, false if no callback or send failedreturnThread-safenote

Parameters:

  • source_ip (uint32_t)
  • source_port (uint16_t)
  • dest_ip (uint32_t)
  • dest_port (uint16_t)
  • protocol (ryu_ldn::bsd::ProtocolType)

Returns: bool

void SetProxyConnectReplyCallback(SendProxyConnectReplyCallback callback)

Set the callback for sending ProxyConnectReply to the LDN server.

callbackFunction to call when TCP proxy sockets accept connectionsparam

Parameters:

  • callback (SendProxyConnectReplyCallback)
bool SendProxyConnectReply(uint32_t source_ip, uint16_t source_port, uint32_t dest_ip, uint16_t dest_port, ryu_ldn::bsd::ProtocolType protocol)

Send ProxyConnectReply for accepted TCP connection.

Called byto confirm a connection was accepted.source_ipOur IP (host byte order)source_portOur port (host byte order)dest_ipRemote IP (host byte order)dest_portRemote port (host byte order)protocolProtocol type (TCP)paramtrue if reply was sent, false if no callback or send failedreturn

Parameters:

  • source_ip (uint32_t)
  • source_port (uint16_t)
  • dest_ip (uint32_t)
  • dest_port (uint16_t)
  • protocol (ryu_ldn::bsd::ProtocolType)

Returns: bool

void SetProxyDisconnectCallback(SendProxyDisconnectCallback callback)

Set the callback for sending ProxyDisconnect to the LDN server.

callbackFunction to call when TCP proxy sockets are closedparam

Parameters:

  • callback (SendProxyDisconnectCallback)
bool SendProxyDisconnect(uint32_t source_ip, uint16_t source_port, uint32_t dest_ip, uint16_t dest_port, ryu_ldn::bsd::ProtocolType protocol)

Send ProxyDisconnect for closed TCP connection.

Called byto notify peer of connection close.source_ipOur IP (host byte order)source_portOur port (host byte order)dest_ipRemote IP (host byte order)dest_portRemote port (host byte order)protocolProtocol type (TCP)paramtrue if disconnect was sent, false if no callback or send failedreturn

Parameters:

  • source_ip (uint32_t)
  • source_port (uint16_t)
  • dest_ip (uint32_t)
  • dest_port (uint16_t)
  • protocol (ryu_ldn::bsd::ProtocolType)

Returns: bool

bool RouteConnectResponse(const ryu_ldn::protocol::ProxyConnectResponse & response)

Route incoming ProxyConnectReply to the connecting socket.

Called by the LDN MITM service when a ProxyConnectReply packet arrives.responseThe connect responseparamtrue if routed successfully, false if no matching socketreturnThread-safenote

Parameters:

  • response (const ryu_ldn::protocol::ProxyConnectResponse &)

Returns: bool

bool RouteConnectRequest(const ryu_ldn::protocol::ProxyConnectRequest & request)

Route incoming ProxyConnect to a listening socket (accept queue)

Called by the LDN MITM service when a ProxyConnect packet arrives for a listening socket (incoming TCP connection).requestThe connect requestparamtrue if routed successfully, false if no matching listenerreturnThread-safenote

Parameters:

  • request (const ryu_ldn::protocol::ProxyConnectRequest &)

Returns: bool

void SetLocalIp(uint32_t ip)

Set the local LDN IP address.

Called when the game receives its IP from GetIpv4Address.ipLocal IP address in host byte order (e.g., 0x0A720001 for 10.114.0.1)paramThread-safenote

Parameters:

  • ip (uint32_t)
uint32_t GetLocalIp()

Get the local LDN IP address.

Local IP in host byte order, or 0 if not setreturnThread-safenote

Returns: uint32_t

size_t GetActiveSocketCount()

Get the number of active proxy sockets.

Number of proxy sockets currently managedreturn

Returns: size_t

size_t GetAvailablePortCount(ryu_ldn::bsd::ProtocolType protocol)

Get the number of available ephemeral ports.

protocolProtocol typeparamAvailable port countreturn

Parameters:

  • protocol (ryu_ldn::bsd::ProtocolType)

Returns: size_t

void DeliverPendingPackets(* socket, uint16_t port, ryu_ldn::bsd::ProtocolType protocol)

Deliver pending packets to a newly bound socket.

Called after Bind() to deliver any packets that arrived before the socket was bound.socketThe socket to deliver toportThe port the socket is bound toprotocolThe protocol typeparam

Parameters:

  • socket (*)
  • port (uint16_t)
  • protocol (ryu_ldn::bsd::ProtocolType)
void ProxySocketManager()

Private constructor (singleton)

void ~ProxySocketManager()

Destructor.

* FindSocketByDestination(uint32_t dest_ip, uint16_t dest_port, ryu_ldn::bsd::ProtocolType protocol)

Find a socket matching the given destination.

dest_ipDestination IP (host byte order)dest_portDestination port (host byte order)protocolProtocol typeparamPointer to matching socket, or nullptr if not foundreturnCaller must hold m_mutexnote

Parameters:

  • dest_ip (uint32_t)
  • dest_port (uint16_t)
  • protocol (ryu_ldn::bsd::ProtocolType)

Returns: *