2023-02-28 04:41:44 +01:00
|
|
|
|
using Ryujinx.HLE.HOS.Services.Sockets.Bsd.Types;
|
|
|
|
|
using System;
|
2022-12-12 14:59:31 +01:00
|
|
|
|
using System.Collections.Generic;
|
2022-01-12 19:31:08 +01:00
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
|
2022-12-12 14:59:31 +01:00
|
|
|
|
namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd.Impl
|
2022-01-12 19:31:08 +01:00
|
|
|
|
{
|
|
|
|
|
static class WinSockHelper
|
|
|
|
|
{
|
|
|
|
|
private static readonly Dictionary<WsaError, LinuxError> _errorMap = new()
|
|
|
|
|
{
|
|
|
|
|
// WSAEINTR
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAEINTR, LinuxError.EINTR },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAEWOULDBLOCK
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAEWOULDBLOCK, LinuxError.EWOULDBLOCK },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAEINPROGRESS
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAEINPROGRESS, LinuxError.EINPROGRESS },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAEALREADY
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAEALREADY, LinuxError.EALREADY },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAENOTSOCK
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAENOTSOCK, LinuxError.ENOTSOCK },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAEDESTADDRREQ
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAEDESTADDRREQ, LinuxError.EDESTADDRREQ },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAEMSGSIZE
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAEMSGSIZE, LinuxError.EMSGSIZE },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAEPROTOTYPE
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAEPROTOTYPE, LinuxError.EPROTOTYPE },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAENOPROTOOPT
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAENOPROTOOPT, LinuxError.ENOPROTOOPT },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAEPROTONOSUPPORT
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAEPROTONOSUPPORT, LinuxError.EPROTONOSUPPORT },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAESOCKTNOSUPPORT
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAESOCKTNOSUPPORT, LinuxError.ESOCKTNOSUPPORT },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAEOPNOTSUPP
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAEOPNOTSUPP, LinuxError.EOPNOTSUPP },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAEPFNOSUPPORT
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAEPFNOSUPPORT, LinuxError.EPFNOSUPPORT },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAEAFNOSUPPORT
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAEAFNOSUPPORT, LinuxError.EAFNOSUPPORT },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAEADDRINUSE
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAEADDRINUSE, LinuxError.EADDRINUSE },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAEADDRNOTAVAIL
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAEADDRNOTAVAIL, LinuxError.EADDRNOTAVAIL },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAENETDOWN
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAENETDOWN, LinuxError.ENETDOWN },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAENETUNREACH
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAENETUNREACH, LinuxError.ENETUNREACH },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAENETRESET
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAENETRESET, LinuxError.ENETRESET },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAECONNABORTED
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAECONNABORTED, LinuxError.ECONNABORTED },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAECONNRESET
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAECONNRESET, LinuxError.ECONNRESET },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAENOBUFS
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAENOBUFS, LinuxError.ENOBUFS },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAEISCONN
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAEISCONN, LinuxError.EISCONN },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAENOTCONN
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAENOTCONN, LinuxError.ENOTCONN },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAESHUTDOWN
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAESHUTDOWN, LinuxError.ESHUTDOWN },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAETOOMANYREFS
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAETOOMANYREFS, LinuxError.ETOOMANYREFS },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAETIMEDOUT
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAETIMEDOUT, LinuxError.ETIMEDOUT },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAECONNREFUSED
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAECONNREFUSED, LinuxError.ECONNREFUSED },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAELOOP
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAELOOP, LinuxError.ELOOP },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAENAMETOOLONG
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAENAMETOOLONG, LinuxError.ENAMETOOLONG },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAEHOSTDOWN
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAEHOSTDOWN, LinuxError.EHOSTDOWN },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAEHOSTUNREACH
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAEHOSTUNREACH, LinuxError.EHOSTUNREACH },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAENOTEMPTY
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAENOTEMPTY, LinuxError.ENOTEMPTY },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAEUSERS
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAEUSERS, LinuxError.EUSERS },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAEDQUOT
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAEDQUOT, LinuxError.EDQUOT },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAESTALE
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAESTALE, LinuxError.ESTALE },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAEREMOTE
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAEREMOTE, LinuxError.EREMOTE },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAEINVAL
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAEINVAL, LinuxError.EINVAL },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// WSAEFAULT
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ WsaError.WSAEFAULT, LinuxError.EFAULT },
|
2022-01-12 19:31:08 +01:00
|
|
|
|
// NOERROR
|
2023-02-28 04:41:44 +01:00
|
|
|
|
{ 0, 0 }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private static readonly Dictionary<int, LinuxError> _errorMapMacOs = new()
|
|
|
|
|
{
|
|
|
|
|
{ 35, LinuxError.EAGAIN },
|
|
|
|
|
{ 11, LinuxError.EDEADLOCK },
|
|
|
|
|
{ 91, LinuxError.ENOMSG },
|
|
|
|
|
{ 90, LinuxError.EIDRM },
|
|
|
|
|
{ 77, LinuxError.ENOLCK },
|
|
|
|
|
{ 70, LinuxError.ESTALE },
|
|
|
|
|
{ 36, LinuxError.EINPROGRESS },
|
|
|
|
|
{ 37, LinuxError.EALREADY },
|
|
|
|
|
{ 38, LinuxError.ENOTSOCK },
|
|
|
|
|
{ 39, LinuxError.EDESTADDRREQ },
|
|
|
|
|
{ 40, LinuxError.EMSGSIZE },
|
|
|
|
|
{ 41, LinuxError.EPROTOTYPE },
|
|
|
|
|
{ 42, LinuxError.ENOPROTOOPT },
|
|
|
|
|
{ 43, LinuxError.EPROTONOSUPPORT },
|
|
|
|
|
{ 44, LinuxError.ESOCKTNOSUPPORT },
|
|
|
|
|
{ 45, LinuxError.EOPNOTSUPP },
|
|
|
|
|
{ 46, LinuxError.EPFNOSUPPORT },
|
|
|
|
|
{ 47, LinuxError.EAFNOSUPPORT },
|
|
|
|
|
{ 48, LinuxError.EADDRINUSE },
|
|
|
|
|
{ 49, LinuxError.EADDRNOTAVAIL },
|
|
|
|
|
{ 50, LinuxError.ENETDOWN },
|
|
|
|
|
{ 51, LinuxError.ENETUNREACH },
|
|
|
|
|
{ 52, LinuxError.ENETRESET },
|
|
|
|
|
{ 53, LinuxError.ECONNABORTED },
|
|
|
|
|
{ 54, LinuxError.ECONNRESET },
|
|
|
|
|
{ 55, LinuxError.ENOBUFS },
|
|
|
|
|
{ 56, LinuxError.EISCONN },
|
|
|
|
|
{ 57, LinuxError.ENOTCONN },
|
|
|
|
|
{ 58, LinuxError.ESHUTDOWN },
|
|
|
|
|
{ 60, LinuxError.ETIMEDOUT },
|
|
|
|
|
{ 61, LinuxError.ECONNREFUSED },
|
|
|
|
|
{ 64, LinuxError.EHOSTDOWN },
|
|
|
|
|
{ 65, LinuxError.EHOSTUNREACH },
|
|
|
|
|
{ 68, LinuxError.EUSERS },
|
|
|
|
|
{ 62, LinuxError.ELOOP },
|
|
|
|
|
{ 63, LinuxError.ENAMETOOLONG },
|
|
|
|
|
{ 66, LinuxError.ENOTEMPTY },
|
|
|
|
|
{ 69, LinuxError.EDQUOT },
|
|
|
|
|
{ 71, LinuxError.EREMOTE },
|
|
|
|
|
{ 78, LinuxError.ENOSYS },
|
|
|
|
|
{ 59, LinuxError.ETOOMANYREFS },
|
|
|
|
|
{ 92, LinuxError.EILSEQ },
|
|
|
|
|
{ 89, LinuxError.ECANCELED },
|
|
|
|
|
{ 84, LinuxError.EOVERFLOW }
|
2022-01-12 19:31:08 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private static readonly Dictionary<BsdSocketOption, SocketOptionName> _soSocketOptionMap = new()
|
|
|
|
|
{
|
|
|
|
|
{ BsdSocketOption.SoDebug, SocketOptionName.Debug },
|
|
|
|
|
{ BsdSocketOption.SoReuseAddr, SocketOptionName.ReuseAddress },
|
|
|
|
|
{ BsdSocketOption.SoKeepAlive, SocketOptionName.KeepAlive },
|
|
|
|
|
{ BsdSocketOption.SoDontRoute, SocketOptionName.DontRoute },
|
|
|
|
|
{ BsdSocketOption.SoBroadcast, SocketOptionName.Broadcast },
|
|
|
|
|
{ BsdSocketOption.SoUseLoopBack, SocketOptionName.UseLoopback },
|
|
|
|
|
{ BsdSocketOption.SoLinger, SocketOptionName.Linger },
|
|
|
|
|
{ BsdSocketOption.SoOobInline, SocketOptionName.OutOfBandInline },
|
|
|
|
|
{ BsdSocketOption.SoReusePort, SocketOptionName.ReuseAddress },
|
|
|
|
|
{ BsdSocketOption.SoSndBuf, SocketOptionName.SendBuffer },
|
|
|
|
|
{ BsdSocketOption.SoRcvBuf, SocketOptionName.ReceiveBuffer },
|
|
|
|
|
{ BsdSocketOption.SoSndLoWat, SocketOptionName.SendLowWater },
|
|
|
|
|
{ BsdSocketOption.SoRcvLoWat, SocketOptionName.ReceiveLowWater },
|
|
|
|
|
{ BsdSocketOption.SoSndTimeo, SocketOptionName.SendTimeout },
|
|
|
|
|
{ BsdSocketOption.SoRcvTimeo, SocketOptionName.ReceiveTimeout },
|
|
|
|
|
{ BsdSocketOption.SoError, SocketOptionName.Error },
|
|
|
|
|
{ BsdSocketOption.SoType, SocketOptionName.Type }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private static readonly Dictionary<BsdSocketOption, SocketOptionName> _ipSocketOptionMap = new()
|
|
|
|
|
{
|
|
|
|
|
{ BsdSocketOption.IpOptions, SocketOptionName.IPOptions },
|
|
|
|
|
{ BsdSocketOption.IpHdrIncl, SocketOptionName.HeaderIncluded },
|
|
|
|
|
{ BsdSocketOption.IpTtl, SocketOptionName.IpTimeToLive },
|
|
|
|
|
{ BsdSocketOption.IpMulticastIf, SocketOptionName.MulticastInterface },
|
|
|
|
|
{ BsdSocketOption.IpMulticastTtl, SocketOptionName.MulticastTimeToLive },
|
|
|
|
|
{ BsdSocketOption.IpMulticastLoop, SocketOptionName.MulticastLoopback },
|
|
|
|
|
{ BsdSocketOption.IpAddMembership, SocketOptionName.AddMembership },
|
|
|
|
|
{ BsdSocketOption.IpDropMembership, SocketOptionName.DropMembership },
|
|
|
|
|
{ BsdSocketOption.IpDontFrag, SocketOptionName.DontFragment },
|
|
|
|
|
{ BsdSocketOption.IpAddSourceMembership, SocketOptionName.AddSourceMembership },
|
|
|
|
|
{ BsdSocketOption.IpDropSourceMembership, SocketOptionName.DropSourceMembership }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private static readonly Dictionary<BsdSocketOption, SocketOptionName> _tcpSocketOptionMap = new()
|
|
|
|
|
{
|
|
|
|
|
{ BsdSocketOption.TcpNoDelay, SocketOptionName.NoDelay },
|
|
|
|
|
{ BsdSocketOption.TcpKeepIdle, SocketOptionName.TcpKeepAliveTime },
|
|
|
|
|
{ BsdSocketOption.TcpKeepIntvl, SocketOptionName.TcpKeepAliveInterval },
|
|
|
|
|
{ BsdSocketOption.TcpKeepCnt, SocketOptionName.TcpKeepAliveRetryCount }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public static LinuxError ConvertError(WsaError errorCode)
|
|
|
|
|
{
|
2023-02-28 04:41:44 +01:00
|
|
|
|
if (OperatingSystem.IsMacOS())
|
|
|
|
|
{
|
|
|
|
|
if (_errorMapMacOs.TryGetValue((int)errorCode, out LinuxError errno))
|
|
|
|
|
{
|
|
|
|
|
return errno;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2022-01-12 19:31:08 +01:00
|
|
|
|
{
|
2023-02-28 04:41:44 +01:00
|
|
|
|
if (_errorMap.TryGetValue(errorCode, out LinuxError errno))
|
|
|
|
|
{
|
|
|
|
|
return errno;
|
|
|
|
|
}
|
2022-01-12 19:31:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-28 04:41:44 +01:00
|
|
|
|
return (LinuxError)errorCode;
|
2022-01-12 19:31:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool TryConvertSocketOption(BsdSocketOption option, SocketOptionLevel level, out SocketOptionName name)
|
|
|
|
|
{
|
|
|
|
|
var table = level switch
|
|
|
|
|
{
|
|
|
|
|
SocketOptionLevel.Socket => _soSocketOptionMap,
|
|
|
|
|
SocketOptionLevel.IP => _ipSocketOptionMap,
|
|
|
|
|
SocketOptionLevel.Tcp => _tcpSocketOptionMap,
|
|
|
|
|
_ => null
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (table == null)
|
|
|
|
|
{
|
|
|
|
|
name = default;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return table.TryGetValue(option, out name);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-12 14:59:31 +01:00
|
|
|
|
}
|