// RecvFrom(u32 sock, u32 flags) -> (i32 ret, u32 bsd_errno, u32 addrlen, buffer<i8, 0x22, 0> message, buffer<nn::socket::sockaddr_in, 0x22, 0x10>) public ResultCode RecvFrom(ServiceCtx context) { int socketFd = context.RequestData.ReadInt32(); BsdSocketFlags socketFlags = (BsdSocketFlags)context.RequestData.ReadInt32(); (ulong receivePosition, ulong receiveLength) = context.Request.GetBufferType0x22(0); (ulong sockAddrOutPosition, ulong sockAddrOutSize) = context.Request.GetBufferType0x22(1); WritableRegion receiveRegion = context.Memory.GetWritableRegion(receivePosition, (int)receiveLength); LinuxError errno = LinuxError.EBADF; ISocket socket = _context.RetrieveSocket(socketFd); int result = -1; if (socket != null) { errno = socket.ReceiveFrom(out result, receiveRegion.Memory.Span, receiveRegion.Memory.Span.Length, socketFlags, out IPEndPoint endPoint); if (errno == LinuxError.SUCCESS) { SetResultErrno(socket, result); receiveRegion.Dispose(); context.Memory.Write(sockAddrOutPosition, BsdSockAddr.FromIPEndPoint(endPoint)); } } return(WriteBsdResult(context, result, errno)); }
public static BsdSockAddr FromIPEndPoint(IPEndPoint endpoint) { BsdSockAddr result = new BsdSockAddr { Length = 0, Family = (byte)endpoint.AddressFamily, Port = (ushort)IPAddress.HostToNetworkOrder((short)endpoint.Port) }; endpoint.Address.GetAddressBytes().AsSpan().CopyTo(result.Address.AsSpan()); return(result); }
private void WriteSockAddr(ServiceCtx context, ulong bufferPosition, ISocket socket, bool isRemote) { IPEndPoint endPoint = isRemote ? socket.RemoteEndPoint : socket.LocalEndPoint; context.Memory.Write(bufferPosition, BsdSockAddr.FromIPEndPoint(endPoint)); }