ams::mitm::bsd::ProxySocketManager
ams::mitm::bsd::ProxySocketManager
Section titled “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.
Members
Section titled “Members”m_mutex
Section titled “m_mutex”Type: os::Mutex
Mutex for thread safety.
m_sockets
Section titled “m_sockets”Type: std::unordered_map< s32, std::unique_ptr<> >
Map of file descriptor to.
m_port_pool
Section titled “m_port_pool”Type: EphemeralPortPool
Ephemeral port pool.
m_local_ip
Section titled “m_local_ip”Type: uint32_t
Local LDN IP address (host byte order)
m_send_callback
Section titled “m_send_callback”Type: SendProxyDataCallback
Callback for sending ProxyData to LDN server.
m_proxy_connect_callback
Section titled “m_proxy_connect_callback”Type: SendProxyConnectCallback
Callback for sending ProxyConnect to LDN server (TCP handshake)
m_proxy_connect_reply_callback
Section titled “m_proxy_connect_reply_callback”Type: SendProxyConnectReplyCallback
Callback for sending ProxyConnectReply to LDN server (TCP accept)
m_proxy_disconnect_callback
Section titled “m_proxy_disconnect_callback”Type: SendProxyDisconnectCallback
Callback for sending ProxyDisconnect to LDN server (TCP close)
m_pending_packets
Section titled “m_pending_packets”Type: std::deque< PendingPacket >
Members
Section titled “Members”MaxPendingPackets
Section titled “MaxPendingPackets”Type: constexpr size_t
Methods
Section titled “Methods”GetInstance
Section titled “GetInstance”& GetInstance()Get the singleton instance.
Reference to the globalreturn
Returns: &
IsLdnAddress
Section titled “IsLdnAddress”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
IsLdnAddress
Section titled “IsLdnAddress”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
Methods
Section titled “Methods”ProxySocketManager
Section titled “ProxySocketManager”void ProxySocketManager(const&)Deleted copy constructor.
Parameters:
param(const&)
operator=
Section titled “operator=”& operator=(const&)Parameters:
param(const&)
Returns: &
CreateProxySocket
Section titled “CreateProxySocket”* 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
Section titled “GetProxySocket”* 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: *
IsProxySocket
Section titled “IsProxySocket”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
CloseProxySocket
Section titled “CloseProxySocket”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
CloseAllProxySockets
Section titled “CloseAllProxySockets”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
AllocatePort
Section titled “AllocatePort”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
ReservePort
Section titled “ReservePort”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
ReleasePort
Section titled “ReleasePort”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)
RouteIncomingData
Section titled “RouteIncomingData”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
SetSendCallback
Section titled “SetSendCallback”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)
SendProxyData
Section titled “SendProxyData”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
SetProxyConnectCallback
Section titled “SetProxyConnectCallback”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)
SendProxyConnect
Section titled “SendProxyConnect”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
SetProxyConnectReplyCallback
Section titled “SetProxyConnectReplyCallback”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)
SendProxyConnectReply
Section titled “SendProxyConnectReply”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
SetProxyDisconnectCallback
Section titled “SetProxyDisconnectCallback”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)
SendProxyDisconnect
Section titled “SendProxyDisconnect”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
RouteConnectResponse
Section titled “RouteConnectResponse”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
RouteConnectRequest
Section titled “RouteConnectRequest”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
SetLocalIp
Section titled “SetLocalIp”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)
GetLocalIp
Section titled “GetLocalIp”uint32_t GetLocalIp()Get the local LDN IP address.
Local IP in host byte order, or 0 if not setreturnThread-safenote
Returns: uint32_t
GetActiveSocketCount
Section titled “GetActiveSocketCount”size_t GetActiveSocketCount()Get the number of active proxy sockets.
Number of proxy sockets currently managedreturn
Returns: size_t
GetAvailablePortCount
Section titled “GetAvailablePortCount”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
DeliverPendingPackets
Section titled “DeliverPendingPackets”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)
Methods
Section titled “Methods”ProxySocketManager
Section titled “ProxySocketManager”void ProxySocketManager()Private constructor (singleton)
~ProxySocketManager
Section titled “~ProxySocketManager”void ~ProxySocketManager()Destructor.
FindSocketByDestination
Section titled “FindSocketByDestination”* 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: *