From fe0c34c639a9912898ce982d9b4f1637d5b5875f Mon Sep 17 00:00:00 2001 From: TSR Berry <20988865+TSRBerry@users.noreply.github.com> Date: Wed, 14 Aug 2024 03:53:56 +0200 Subject: [PATCH] Update RyuSocks to v0.3.1-alpha --- Directory.Packages.props | 4 +- .../Sockets/Bsd/Impl/ManagedProxySocket.cs | 100 ++++++++++++++---- 2 files changed, 84 insertions(+), 20 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index 72d2442a35..461388511b 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -36,7 +36,7 @@ - + @@ -50,4 +50,4 @@ - + \ No newline at end of file diff --git a/src/Ryujinx.HLE/HOS/Services/Sockets/Bsd/Impl/ManagedProxySocket.cs b/src/Ryujinx.HLE/HOS/Services/Sockets/Bsd/Impl/ManagedProxySocket.cs index b2461836ae..73608c0308 100644 --- a/src/Ryujinx.HLE/HOS/Services/Sockets/Bsd/Impl/ManagedProxySocket.cs +++ b/src/Ryujinx.HLE/HOS/Services/Sockets/Bsd/Impl/ManagedProxySocket.cs @@ -23,13 +23,11 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd.Impl public SocksClient ProxyClient { get; } - // TODO: Make sure Blocking is used properly - public bool Blocking { get; set; } + public bool Blocking { get => ProxyClient.Blocking; set => ProxyClient.Blocking = value; } public int RefCount { get; set; } - // TODO: Assign LocalEndPoint and RemoteEndPoint - public IPEndPoint RemoteEndPoint { get; private set; } - public IPEndPoint LocalEndPoint { get; private set; } + public IPEndPoint RemoteEndPoint => (IPEndPoint)ProxyClient.ProxiedRemoteEndPoint; + public IPEndPoint LocalEndPoint => (IPEndPoint)ProxyClient.ProxiedLocalEndPoint; public AddressFamily AddressFamily => ProxyClient.AddressFamily; public SocketType SocketType => ProxyClient.SocketType; @@ -70,11 +68,9 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd.Impl RefCount = 1; } - private ManagedProxySocket(ManagedProxySocket oldSocket, SocksClient proxyClient) + private ManagedProxySocket(SocksClient proxyClient) { ProxyClient = proxyClient; - LocalEndPoint = oldSocket.LocalEndPoint; - RemoteEndPoint = oldSocket.RemoteEndPoint; _acceptedConnection = true; RefCount = 1; } @@ -273,22 +269,82 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd.Impl throw new NotImplementedException(); } + /// + /// Adapted from + /// public LinuxError GetSocketOption(BsdSocketOption option, SocketOptionLevel level, Span optionValue) { - // TODO: Call ProxyClient.GetSocketOption() when it's implemented - throw new NotImplementedException(); + try + { + LinuxError result = WinSockHelper.ValidateSocketOption(option, level, write: false); + + if (result != LinuxError.SUCCESS) + { + Logger.Warning?.Print(LogClass.ServiceBsd, $"Invalid GetSockOpt Option: {option} Level: {level}"); + + return result; + } + + if (!WinSockHelper.TryConvertSocketOption(option, level, out SocketOptionName optionName)) + { + Logger.Warning?.Print(LogClass.ServiceBsd, $"Unsupported GetSockOpt Option: {option} Level: {level}"); + optionValue.Clear(); + + return LinuxError.SUCCESS; + } + + byte[] tempOptionValue = new byte[optionValue.Length]; + + ProxyClient.GetSocketOption(level, optionName, tempOptionValue); + + tempOptionValue.AsSpan().CopyTo(optionValue); + + return LinuxError.SUCCESS; + } + catch (SocketException exception) + { + return WinSockHelper.ConvertError((WsaError)exception.ErrorCode); + } } + /// + /// Adapted from + /// public LinuxError SetSocketOption(BsdSocketOption option, SocketOptionLevel level, ReadOnlySpan optionValue) { - // TODO: Call ProxyClient.SetSocketOption() when it's implemented - throw new NotImplementedException(); + try + { + LinuxError result = WinSockHelper.ValidateSocketOption(option, level, write: true); + + if (result != LinuxError.SUCCESS) + { + Logger.Warning?.Print(LogClass.ServiceBsd, $"Invalid SetSockOpt Option: {option} Level: {level}"); + + return result; + } + + if (!WinSockHelper.TryConvertSocketOption(option, level, out SocketOptionName optionName)) + { + Logger.Warning?.Print(LogClass.ServiceBsd, $"Unsupported SetSockOpt Option: {option} Level: {level}"); + + return LinuxError.SUCCESS; + } + + byte[] value = optionValue.ToArray(); + + ProxyClient.SetSocketOption(level, optionName, value); + + return LinuxError.SUCCESS; + } + catch (SocketException exception) + { + return WinSockHelper.ConvertError((WsaError)exception.ErrorCode); + } } public bool Poll(int microSeconds, SelectMode mode) { - // TODO: Call ProxyClient.Poll() when it's implemented - throw new NotImplementedException(); + return ProxyClient.Poll(microSeconds, mode); } public LinuxError Bind(IPEndPoint localEndPoint) @@ -370,7 +426,7 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd.Impl try { SocksClient newProxyClient = ProxyClient.Accept(); - newSocket = new ManagedProxySocket(this, newProxyClient); + newSocket = new ManagedProxySocket(newProxyClient); } catch (ProxyException exception) { @@ -391,13 +447,21 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd.Impl public void Disconnect() { - // TODO: Call ProxyClient.Disconnect() when it's implemented + ProxyClient.Disconnect(); } public LinuxError Shutdown(BsdSocketShutdownFlags how) { - // TODO: Call ProxyClient.Shutdown() when it's implemented - return LinuxError.SUCCESS; + try + { + ProxyClient.Shutdown((SocketShutdown)how); + + return LinuxError.SUCCESS; + } + catch (SocketException exception) + { + return WinSockHelper.ConvertError((WsaError)exception.ErrorCode); + } } public void Close()