Skip to content

ams::mitm::bsd::ProxySocket

Proxy Socket for LDN Network Communication.

This class represents a virtual socket that routes traffic through the Ryujinx LDN server via ProxyData packets instead of using real network sockets.Key FeaturesMimics BSD socket API (bind, connect, send, recv)Routes data via ProxyData protocol to serverMaintains receive queue for incoming dataSupports both UDP and TCP semanticsThread-safe receive queue Usage//CreateandbindaUDPproxysocketnormalnormalautosocket=std::make_unique<ProxySocket>(SocketType::Dgram,ProtocolType::Udp);socket->Bind(local_addr);normalnormal//Senddatatoapeernormalsocket->SendTo(data,data_len,remote_addr);normalnormal//Receivedata(blockingifnodataavailable)normalsocket->RecvFrom(buffer,buffer_size,&from_addr);.cpp

Type: ryu_ldn::bsd::SocketType

Socket type (Stream or Dgram)

Type: ryu_ldn::bsd::ProtocolType

Protocol type (Tcp or Udp)

Type: ProxySocketState

Current socket state.

Type: bool

Non-blocking mode flag.

Type: bool

Shutdown flags.

Type: bool

Type: ryu_ldn::bsd::SockAddrIn

Local address (set by Bind)

Type: ryu_ldn::bsd::SockAddrIn

Remote address (set by Connect)

Type: os::Mutex

Receive queue mutex.

Type: std::deque<>

Receive queue (incoming packets)

Type: os::Event

Event signaled when data is available.

Type: std::deque< std::unique_ptr<> >

TCP accept queue (pending connections)

Type: os::Event

Event signaled when accept queue has connections.

Type: bool

TCP connect response received flag.

Type: ryu_ldn::protocol::ProxyConnectResponse

TCP connect response data (valid when m_connect_response_received is true)

Type: os::Event

Event signaled when connect response is received.

Type: bool

Broadcast flag (SO_BROADCAST)

Type: uint32_t

Broadcast address for filtering.

Type: std::unordered_map< uint32_t, s32 >

Socket options storage.

void ProxySocket(ryu_ldn::bsd::SocketType type, ryu_ldn::bsd::ProtocolType protocol)

Construct a new proxy socket.

Creates an unbound, unconnected proxy socket of the specified type.typeSocket type (Stream for TCP, Dgram for UDP)protocolProtocol type (Tcp or Udp)param

Parameters:

  • type (ryu_ldn::bsd::SocketType)
  • protocol (ryu_ldn::bsd::ProtocolType)
void ~ProxySocket()

Destructor.

Cleans up resources. Does NOT send ProxyDisconnect - callfirst.

void ProxySocket(const&)

Non-copyable (socket identity is unique)

Parameters:

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

Parameters:

  • param (const&)

Returns: &

void ProxySocket(&&)

Move constructor.

Parameters:

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

Parameters:

  • param (&&)

Returns: &

ProxySocketState GetState()

Get the current socket state.

Current state (Created, Bound, Connected, etc.)return

Returns: ProxySocketState

ryu_ldn::bsd::SocketType GetType()

Get the socket type.

Socket type (Stream or Dgram)return

Returns: ryu_ldn::bsd::SocketType

ryu_ldn::bsd::ProtocolType GetProtocol()

Get the protocol type.

Protocol type (Tcp or Udp)return

Returns: ryu_ldn::bsd::ProtocolType

bool IsNonBlocking()

Check if socket is non-blocking.

true if non-blocking mode is enabledreturn

Returns: bool

void SetNonBlocking(bool non_blocking)

Set non-blocking mode.

non_blockingtrue to enable non-blocking modeparam

Parameters:

  • non_blocking (bool)
Result Bind(const& addr)

Bind the socket to a local address.

Assigns a local address and port to the socket. If the port is 0, the caller should allocate an ephemeral port before calling this.addrLocal address to bind to (must be in LDN network 10.114.x.x)paramSuccess or error (e.g., already bound, invalid address)returnDoes not validate that the address is actually ours - caller must checknote

Parameters:

  • addr (const&)

Returns: Result

Result Connect(const& addr)

Connect to a remote address.

For TCP, this initiates a connection to the remote peer. For UDP, this sets the default destination for.addrRemote address to connect toparamSuccess or errorreturnFor TCP, this should trigger sending ProxyConnect to the servernote

Parameters:

  • addr (const&)

Returns: Result

Result GetSockName(* out_addr)

Get the local address.

out_addrOutput: local addressparamSuccess or error if not boundreturn

Parameters:

  • out_addr (*)

Returns: Result

Result GetPeerName(* out_addr)

Get the remote address (connected sockets only)

out_addrOutput: remote addressparamSuccess or error if not connectedreturn

Parameters:

  • out_addr (*)

Returns: Result

const& GetLocalAddr()

Get the local address reference.

Reference to local address structurereturn

Returns: const&

const& GetRemoteAddr()

Get the remote address reference.

Reference to remote address structurereturn

Returns: const&

s32 Send(const void * data, size_t len, s32 flags)

Send data (connected sockets only)

Sends data to the connected peer. Socket must be connected first.dataPointer to data to sendlenLength of dataflagsSend flags (currently ignored)paramBytes sent or negative errno on errorreturn

Parameters:

  • data (const void *)
  • len (size_t)
  • flags (s32)

Returns: s32

s32 SendTo(const void * data, size_t len, s32 flags, const& dest)

Send data to a specific address.

Sends data to the specified destination. Does not require connection.dataPointer to data to sendlenLength of dataflagsSend flags (currently ignored)destDestination addressparamBytes sent or negative errno on errorreturn

Parameters:

  • data (const void *)
  • len (size_t)
  • flags (s32)
  • dest (const&)

Returns: s32

s32 Recv(void * buffer, size_t len, s32 flags)

Receive data (connected sockets only)

Receives data from the connected peer.bufferBuffer to receive intolenBuffer sizeflagsReceive flags (currently supports MSG_PEEK, MSG_DONTWAIT)paramBytes received, 0 if connection closed, or negative errno on errorreturn

Parameters:

  • buffer (void *)
  • len (size_t)
  • flags (s32)

Returns: s32

s32 RecvFrom(void * buffer, size_t len, s32 flags, * from)

Receive data with source address.

Receives data and returns the source address.bufferBuffer to receive intolenBuffer sizeflagsReceive flagsfromOutput: source address (can be nullptr)paramBytes received, 0 if connection closed, or negative errno on errorreturn

Parameters:

  • buffer (void *)
  • len (size_t)
  • flags (s32)
  • from (*)

Returns: s32

void IncomingData(const void * data, size_t len, const& from)

Queue incoming data from a ProxyData packet.

Called by thewhen a ProxyData packet arrives that matches this socket.dataPacket payloadlenPayload lengthfromSource addressparamThread-safe. Signals the receive event.note

Parameters:

  • data (const void *)
  • len (size_t)
  • from (const&)
Result SetSockOpt(s32 level, s32 optname, const void * optval, size_t optlen)

Set a socket option.

Most options are stored locally and don’t affect real network behavior since we’re proxying through the server.levelOption level (SOL_SOCKET, etc.)optnameOption nameoptvalOption valueoptlenOption value lengthparamSuccess or errorreturn

Parameters:

  • level (s32)
  • optname (s32)
  • optval (const void *)
  • optlen (size_t)

Returns: Result

Result GetSockOpt(s32 level, s32 optname, void * optval, size_t * optlen)

Get a socket option.

levelOption leveloptnameOption nameoptvalOutput: option valueoptlenInput/Output: option value lengthparamSuccess or errorreturn

Parameters:

  • level (s32)
  • optname (s32)
  • optval (void *)
  • optlen (size_t *)

Returns: Result

Result Listen(s32 backlog)

Start listening for connections (TCP only)

backlogMaximum pending connections (currently ignored)paramSuccess or error if not TCP or not boundreturn

Parameters:

  • backlog (s32)

Returns: Result

std::unique_ptr<> Accept(* out_addr)

Accept a connection (TCP only)

Blocks until a connection is available (unless non-blocking).out_addrOutput: connecting peer’s addressparamNew connectedor nullptr on errorreturnThe returned socket is owned by the callernote

Parameters:

  • out_addr (*)

Returns: std::unique_ptr<>

void IncomingConnection(const ryu_ldn::protocol::ProxyConnectRequest & request)

Queue an incoming connection request (TCP listen socket)

Called bywhen a ProxyConnect packet arrives for this listening socket.requestThe connection request infoparam

Parameters:

  • request (const ryu_ldn::protocol::ProxyConnectRequest &)
void HandleConnectResponse(const ryu_ldn::protocol::ProxyConnectResponse & response)

Handle connect response (for TCP connect handshake)

Called bywhen ProxyConnectReply arrives.responseThe connect responseparam

Parameters:

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

Check if accept queue has pending connections.

true if there are pending connectionsreturn

Returns: bool

bool IsBroadcastEnabled()

Get broadcast flag.

true if SO_BROADCAST is enabledreturn

Returns: bool

void SetBroadcastEnabled(bool enabled)

Set broadcast flag (SO_BROADCAST)

enabledtrue to enable broadcast receptionparam

Parameters:

  • enabled (bool)
void SetBroadcastAddress(uint32_t addr)

Set broadcast address for filtering.

addrBroadcast address (e.g., 10.114.255.255)param

Parameters:

  • addr (uint32_t)
Result Shutdown(ryu_ldn::bsd::ShutdownHow how)

Shutdown the socket.

howHow to shutdown (read, write, or both)paramSuccess or errorreturn

Parameters:

  • how (ryu_ldn::bsd::ShutdownHow)

Returns: Result

Result Close()

Close the socket.

Marks the socket as closed and releases resources. For TCP, this may send ProxyDisconnect to the server.Success or errorreturn

Returns: Result

bool HasPendingData()

Check if data is available to read.

true if receive queue is not emptyreturn

Returns: bool

size_t GetPendingDataSize()

Get the number of bytes available to read.

Total bytes in receive queuereturn

Returns: size_t

bool WaitForData(u64 timeout_ms)

Wait for data to be available.

Blocks until data is available or timeout expires.timeout_msTimeout in milliseconds (0 = infinite)paramtrue if data is available, false if timeoutreturn

Parameters:

  • timeout_ms (u64)

Returns: bool

os::Event & GetReceiveEvent()

Get the receive event handle.

Can be used with poll/select to wait for data.Reference to the receive eventreturn

Returns: os::Event &

ReceivedPacket PopFrontPacket(bool peek)

Pop the front packet from the receive queue.

peekIf true, don’t remove from queue (MSG_PEEK)paramThe front packet, or empty if queue is emptyreturnCaller must hold m_queue_mutexnote

Parameters:

  • peek (bool)

Returns: ReceivedPacket

constexpr uint32_t MakeOptionKey(s32 level, s32 optname)

Helper to get option key from level and optname.

Parameters:

  • level (s32)
  • optname (s32)

Returns: constexpr uint32_t