2019-12-22 19:33:59 +00:00
|
|
|
|
using Ryujinx.Common.Logging;
|
2018-08-16 23:47:36 +00:00
|
|
|
|
using Ryujinx.HLE.Utilities;
|
2019-12-22 19:33:59 +00:00
|
|
|
|
using System;
|
2019-11-23 02:15:15 +00:00
|
|
|
|
using System.Buffers.Binary;
|
2018-02-28 03:31:52 +00:00
|
|
|
|
using System.Collections.Generic;
|
2018-03-12 01:05:39 +00:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Sockets;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
using System.Text;
|
2019-12-22 19:33:59 +00:00
|
|
|
|
using System.Threading;
|
2018-02-28 03:31:52 +00:00
|
|
|
|
|
2019-09-19 00:45:11 +00:00
|
|
|
|
namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd
|
2018-02-28 03:31:52 +00:00
|
|
|
|
{
|
2019-07-10 15:59:54 +00:00
|
|
|
|
[Service("bsd:s", true)]
|
|
|
|
|
[Service("bsd:u", false)]
|
2018-04-06 04:01:52 +00:00
|
|
|
|
class IClient : IpcService
|
2018-02-28 03:31:52 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
private static Dictionary<WsaError, LinuxError> _errorMap = new Dictionary<WsaError, LinuxError>
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
|
|
|
|
// WSAEINTR
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAEINTR, LinuxError.EINTR},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAEWOULDBLOCK
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAEWOULDBLOCK, LinuxError.EWOULDBLOCK},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAEINPROGRESS
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAEINPROGRESS, LinuxError.EINPROGRESS},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAEALREADY
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAEALREADY, LinuxError.EALREADY},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAENOTSOCK
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAENOTSOCK, LinuxError.ENOTSOCK},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAEDESTADDRREQ
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAEDESTADDRREQ, LinuxError.EDESTADDRREQ},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAEMSGSIZE
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAEMSGSIZE, LinuxError.EMSGSIZE},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAEPROTOTYPE
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAEPROTOTYPE, LinuxError.EPROTOTYPE},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAENOPROTOOPT
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAENOPROTOOPT, LinuxError.ENOPROTOOPT},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAEPROTONOSUPPORT
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAEPROTONOSUPPORT, LinuxError.EPROTONOSUPPORT},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAESOCKTNOSUPPORT
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAESOCKTNOSUPPORT, LinuxError.ESOCKTNOSUPPORT},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAEOPNOTSUPP
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAEOPNOTSUPP, LinuxError.EOPNOTSUPP},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAEPFNOSUPPORT
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAEPFNOSUPPORT, LinuxError.EPFNOSUPPORT},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAEAFNOSUPPORT
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAEAFNOSUPPORT, LinuxError.EAFNOSUPPORT},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAEADDRINUSE
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAEADDRINUSE, LinuxError.EADDRINUSE},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAEADDRNOTAVAIL
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAEADDRNOTAVAIL, LinuxError.EADDRNOTAVAIL},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAENETDOWN
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAENETDOWN, LinuxError.ENETDOWN},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAENETUNREACH
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAENETUNREACH, LinuxError.ENETUNREACH},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAENETRESET
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAENETRESET, LinuxError.ENETRESET},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAECONNABORTED
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAECONNABORTED, LinuxError.ECONNABORTED},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAECONNRESET
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAECONNRESET, LinuxError.ECONNRESET},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAENOBUFS
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAENOBUFS, LinuxError.ENOBUFS},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAEISCONN
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAEISCONN, LinuxError.EISCONN},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAENOTCONN
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAENOTCONN, LinuxError.ENOTCONN},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAESHUTDOWN
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAESHUTDOWN, LinuxError.ESHUTDOWN},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAETOOMANYREFS
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAETOOMANYREFS, LinuxError.ETOOMANYREFS},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAETIMEDOUT
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAETIMEDOUT, LinuxError.ETIMEDOUT},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAECONNREFUSED
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAECONNREFUSED, LinuxError.ECONNREFUSED},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAELOOP
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAELOOP, LinuxError.ELOOP},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAENAMETOOLONG
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAENAMETOOLONG, LinuxError.ENAMETOOLONG},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAEHOSTDOWN
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAEHOSTDOWN, LinuxError.EHOSTDOWN},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAEHOSTUNREACH
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAEHOSTUNREACH, LinuxError.EHOSTUNREACH},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAENOTEMPTY
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAENOTEMPTY, LinuxError.ENOTEMPTY},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAEUSERS
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAEUSERS, LinuxError.EUSERS},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAEDQUOT
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAEDQUOT, LinuxError.EDQUOT},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAESTALE
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAESTALE, LinuxError.ESTALE},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAEREMOTE
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAEREMOTE, LinuxError.EREMOTE},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAEINVAL
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAEINVAL, LinuxError.EINVAL},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// WSAEFAULT
|
2018-12-06 11:16:24 +00:00
|
|
|
|
{WsaError.WSAEFAULT, LinuxError.EFAULT},
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// NOERROR
|
|
|
|
|
{0, 0}
|
|
|
|
|
};
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
private bool _isPrivileged;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
private List<BsdSocket> _sockets = new List<BsdSocket>();
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2019-07-10 15:59:54 +00:00
|
|
|
|
public IClient(ServiceCtx context, bool isPrivileged)
|
2018-02-28 03:31:52 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
_isPrivileged = isPrivileged;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
private LinuxError ConvertError(WsaError errorCode)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2019-07-12 01:13:43 +00:00
|
|
|
|
if (!_errorMap.TryGetValue(errorCode, out LinuxError errno))
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errno = (LinuxError)errorCode;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return errno;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-14 19:04:38 +00:00
|
|
|
|
private ResultCode WriteWinSock2Error(ServiceCtx context, WsaError errorCode)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return WriteBsdResult(context, -1, ConvertError(errorCode));
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-14 19:04:38 +00:00
|
|
|
|
private ResultCode WriteBsdResult(ServiceCtx context, int result, LinuxError errorCode = 0)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (errorCode != LinuxError.SUCCESS)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
result = -1;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
context.ResponseData.Write(result);
|
|
|
|
|
context.ResponseData.Write((int)errorCode);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2019-07-14 19:04:38 +00:00
|
|
|
|
return ResultCode.Success;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
private BsdSocket RetrieveSocket(int socketFd)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (socketFd >= 0 && _sockets.Count > socketFd)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return _sockets[socketFd];
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
private LinuxError SetResultErrno(Socket socket, int result)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return result == 0 && !socket.Blocking ? LinuxError.EWOULDBLOCK : LinuxError.SUCCESS;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
private AddressFamily ConvertFromBsd(int domain)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (domain == 2)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
|
|
|
|
return AddressFamily.InterNetwork;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FIXME: AF_ROUTE ignored, is that really needed?
|
|
|
|
|
return AddressFamily.Unknown;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-14 19:04:38 +00:00
|
|
|
|
private ResultCode SocketInternal(ServiceCtx context, bool exempt)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
AddressFamily domain = (AddressFamily)context.RequestData.ReadInt32();
|
|
|
|
|
SocketType type = (SocketType)context.RequestData.ReadInt32();
|
|
|
|
|
ProtocolType protocol = (ProtocolType)context.RequestData.ReadInt32();
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (domain == AddressFamily.Unknown)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return WriteBsdResult(context, -1, LinuxError.EPROTONOSUPPORT);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
2018-12-06 11:16:24 +00:00
|
|
|
|
else if ((type == SocketType.Seqpacket || type == SocketType.Raw) && !_isPrivileged)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (domain != AddressFamily.InterNetwork || type != SocketType.Raw || protocol != ProtocolType.Icmp)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return WriteBsdResult(context, -1, LinuxError.ENOENT);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
BsdSocket newBsdSocket = new BsdSocket
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
Family = (int)domain,
|
|
|
|
|
Type = (int)type,
|
|
|
|
|
Protocol = (int)protocol,
|
|
|
|
|
Handle = new Socket(domain, type, protocol)
|
2018-02-28 03:31:52 +00:00
|
|
|
|
};
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
_sockets.Add(newBsdSocket);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (exempt)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
newBsdSocket.Handle.Disconnect(true);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return WriteBsdResult(context, _sockets.Count - 1);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
private IPEndPoint ParseSockAddr(ServiceCtx context, long bufferPosition, long bufferSize)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2020-05-03 22:54:50 +00:00
|
|
|
|
int size = context.Memory.Read<byte>((ulong)bufferPosition);
|
|
|
|
|
int family = context.Memory.Read<byte>((ulong)bufferPosition + 1);
|
|
|
|
|
int port = BinaryPrimitives.ReverseEndianness(context.Memory.Read<ushort>((ulong)bufferPosition + 2));
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2020-05-03 22:54:50 +00:00
|
|
|
|
byte[] rawIp = new byte[4];
|
|
|
|
|
|
|
|
|
|
context.Memory.Read((ulong)bufferPosition + 4, rawIp);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return new IPEndPoint(new IPAddress(rawIp), port);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
private void WriteSockAddr(ServiceCtx context, long bufferPosition, IPEndPoint endPoint)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2020-05-03 22:54:50 +00:00
|
|
|
|
context.Memory.Write((ulong)bufferPosition, (byte)0);
|
|
|
|
|
context.Memory.Write((ulong)bufferPosition + 1, (byte)endPoint.AddressFamily);
|
|
|
|
|
context.Memory.Write((ulong)bufferPosition + 2, BinaryPrimitives.ReverseEndianness((ushort)endPoint.Port));
|
|
|
|
|
context.Memory.Write((ulong)bufferPosition + 4, endPoint.Address.GetAddressBytes());
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
private void WriteSockAddr(ServiceCtx context, long bufferPosition, BsdSocket socket, bool isRemote)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
IPEndPoint endPoint = (isRemote ? socket.Handle.RemoteEndPoint : socket.Handle.LocalEndPoint) as IPEndPoint;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
WriteSockAddr(context, bufferPosition, endPoint);
|
2018-02-28 03:31:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 01:13:43 +00:00
|
|
|
|
[Command(0)]
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// Initialize(nn::socket::BsdBufferConfig config, u64 pid, u64 transferMemorySize, KObject<copy, transfer_memory>, pid) -> u32 bsd_errno
|
2019-07-14 19:04:38 +00:00
|
|
|
|
public ResultCode RegisterClient(ServiceCtx context)
|
2018-02-28 03:31:52 +00:00
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
typedef struct {
|
|
|
|
|
u32 version; // Observed 1 on 2.0 LibAppletWeb, 2 on 3.0.
|
|
|
|
|
u32 tcp_tx_buf_size; // Size of the TCP transfer (send) buffer (initial or fixed).
|
|
|
|
|
u32 tcp_rx_buf_size; // Size of the TCP recieve buffer (initial or fixed).
|
|
|
|
|
u32 tcp_tx_buf_max_size; // Maximum size of the TCP transfer (send) buffer. If it is 0, the size of the buffer is fixed to its initial value.
|
|
|
|
|
u32 tcp_rx_buf_max_size; // Maximum size of the TCP receive buffer. If it is 0, the size of the buffer is fixed to its initial value.
|
|
|
|
|
u32 udp_tx_buf_size; // Size of the UDP transfer (send) buffer (typically 0x2400 bytes).
|
|
|
|
|
u32 udp_rx_buf_size; // Size of the UDP receive buffer (typically 0xA500 bytes).
|
|
|
|
|
u32 sb_efficiency; // Number of buffers for each socket (standard values range from 1 to 8).
|
|
|
|
|
} BsdBufferConfig;
|
|
|
|
|
*/
|
|
|
|
|
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// bsd_error
|
2018-12-06 11:16:24 +00:00
|
|
|
|
context.ResponseData.Write(0);
|
2018-02-28 03:31:52 +00:00
|
|
|
|
|
2019-01-11 00:11:46 +00:00
|
|
|
|
Logger.PrintStub(LogClass.ServiceBsd);
|
2018-02-28 03:31:52 +00:00
|
|
|
|
|
2019-07-14 19:04:38 +00:00
|
|
|
|
return ResultCode.Success;
|
2018-02-28 03:31:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 01:13:43 +00:00
|
|
|
|
[Command(1)]
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// StartMonitoring(u64, pid)
|
2019-07-14 19:04:38 +00:00
|
|
|
|
public ResultCode StartMonitoring(ServiceCtx context)
|
2018-02-28 03:31:52 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
ulong unknown0 = context.RequestData.ReadUInt64();
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2019-01-11 00:11:46 +00:00
|
|
|
|
Logger.PrintStub(LogClass.ServiceBsd, new { unknown0 });
|
2018-02-28 03:31:52 +00:00
|
|
|
|
|
2019-07-14 19:04:38 +00:00
|
|
|
|
return ResultCode.Success;
|
2018-02-28 03:31:52 +00:00
|
|
|
|
}
|
2018-02-28 20:58:04 +00:00
|
|
|
|
|
2019-07-12 01:13:43 +00:00
|
|
|
|
[Command(2)]
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// Socket(u32 domain, u32 type, u32 protocol) -> (i32 ret, u32 bsd_errno)
|
2019-07-14 19:04:38 +00:00
|
|
|
|
public ResultCode Socket(ServiceCtx context)
|
2018-02-28 20:58:04 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return SocketInternal(context, false);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2019-07-12 01:13:43 +00:00
|
|
|
|
[Command(3)]
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// SocketExempt(u32 domain, u32 type, u32 protocol) -> (i32 ret, u32 bsd_errno)
|
2019-07-14 19:04:38 +00:00
|
|
|
|
public ResultCode SocketExempt(ServiceCtx context)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return SocketInternal(context, true);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2019-07-12 01:13:43 +00:00
|
|
|
|
[Command(4)]
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// Open(u32 flags, array<unknown, 0x21> path) -> (i32 ret, u32 bsd_errno)
|
2019-07-14 19:04:38 +00:00
|
|
|
|
public ResultCode Open(ServiceCtx context)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
(long bufferPosition, long bufferSize) = context.Request.GetBufferType0x21();
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
int flags = context.RequestData.ReadInt32();
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2020-05-03 22:54:50 +00:00
|
|
|
|
byte[] rawPath = new byte[bufferSize];
|
|
|
|
|
|
|
|
|
|
context.Memory.Read((ulong)bufferPosition, rawPath);
|
|
|
|
|
|
|
|
|
|
string path = Encoding.ASCII.GetString(rawPath);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
WriteBsdResult(context, -1, LinuxError.EOPNOTSUPP);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2019-01-11 00:11:46 +00:00
|
|
|
|
Logger.PrintStub(LogClass.ServiceBsd, new { path, flags });
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2019-07-14 19:04:38 +00:00
|
|
|
|
return ResultCode.Success;
|
2018-03-12 01:05:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 01:13:43 +00:00
|
|
|
|
[Command(5)]
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// Select(u32 nfds, nn::socket::timeout timeout, buffer<nn::socket::fd_set, 0x21, 0> readfds_in, buffer<nn::socket::fd_set, 0x21, 0> writefds_in, buffer<nn::socket::fd_set, 0x21, 0> errorfds_in) -> (i32 ret, u32 bsd_errno, buffer<nn::socket::fd_set, 0x22, 0> readfds_out, buffer<nn::socket::fd_set, 0x22, 0> writefds_out, buffer<nn::socket::fd_set, 0x22, 0> errorfds_out)
|
2019-07-14 19:04:38 +00:00
|
|
|
|
public ResultCode Select(ServiceCtx context)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
WriteBsdResult(context, -1, LinuxError.EOPNOTSUPP);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2019-01-11 00:11:46 +00:00
|
|
|
|
Logger.PrintStub(LogClass.ServiceBsd);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2019-07-14 19:04:38 +00:00
|
|
|
|
return ResultCode.Success;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 01:13:43 +00:00
|
|
|
|
[Command(6)]
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// Poll(u32 nfds, u32 timeout, buffer<unknown, 0x21, 0> fds) -> (i32 ret, u32 bsd_errno, buffer<unknown, 0x22, 0>)
|
2019-07-14 19:04:38 +00:00
|
|
|
|
public ResultCode Poll(ServiceCtx context)
|
2018-03-12 01:05:39 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
int fdsCount = context.RequestData.ReadInt32();
|
|
|
|
|
int timeout = context.RequestData.ReadInt32();
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
(long bufferPosition, long bufferSize) = context.Request.GetBufferType0x21();
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2018-04-06 04:01:52 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (timeout < -1 || fdsCount < 0 || (fdsCount * 8) > bufferSize)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return WriteBsdResult(context, -1, LinuxError.EINVAL);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
PollEvent[] events = new PollEvent[fdsCount];
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
for (int i = 0; i < fdsCount; i++)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2020-05-03 22:54:50 +00:00
|
|
|
|
int socketFd = context.Memory.Read<int>((ulong)(bufferPosition + i * 8));
|
2018-02-28 20:58:04 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
BsdSocket socket = RetrieveSocket(socketFd);
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (socket == null)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return WriteBsdResult(context, -1, LinuxError.EBADF);}
|
2018-04-06 04:01:52 +00:00
|
|
|
|
|
2020-05-03 22:54:50 +00:00
|
|
|
|
PollEvent.EventTypeMask inputEvents = (PollEvent.EventTypeMask)context.Memory.Read<short>((ulong)(bufferPosition + i * 8 + 4));
|
|
|
|
|
PollEvent.EventTypeMask outputEvents = (PollEvent.EventTypeMask)context.Memory.Read<short>((ulong)(bufferPosition + i * 8 + 6));
|
2018-08-24 17:20:42 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
events[i] = new PollEvent(socketFd, socket, inputEvents, outputEvents);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
2018-04-06 04:01:52 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
List<Socket> readEvents = new List<Socket>();
|
|
|
|
|
List<Socket> writeEvents = new List<Socket>();
|
|
|
|
|
List<Socket> errorEvents = new List<Socket>();
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
foreach (PollEvent Event in events)
|
2018-03-12 01:05:39 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
bool isValidEvent = false;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
|
|
|
|
if ((Event.InputEvents & PollEvent.EventTypeMask.Input) != 0)
|
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
readEvents.Add(Event.Socket.Handle);
|
|
|
|
|
errorEvents.Add(Event.Socket.Handle);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
isValidEvent = true;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((Event.InputEvents & PollEvent.EventTypeMask.UrgentInput) != 0)
|
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
readEvents.Add(Event.Socket.Handle);
|
|
|
|
|
errorEvents.Add(Event.Socket.Handle);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
isValidEvent = true;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((Event.InputEvents & PollEvent.EventTypeMask.Output) != 0)
|
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
writeEvents.Add(Event.Socket.Handle);
|
|
|
|
|
errorEvents.Add(Event.Socket.Handle);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
isValidEvent = true;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2018-10-20 22:08:58 +00:00
|
|
|
|
if ((Event.InputEvents & PollEvent.EventTypeMask.Error) != 0)
|
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errorEvents.Add(Event.Socket.Handle);
|
|
|
|
|
isValidEvent = true;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (!isValidEvent)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
|
|
|
|
Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported Poll input event type: {Event.InputEvents}");
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return WriteBsdResult(context, -1, LinuxError.EINVAL);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-22 19:33:59 +00:00
|
|
|
|
if (fdsCount != 0)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2019-12-22 19:33:59 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
System.Net.Sockets.Socket.Select(readEvents, writeEvents, errorEvents, timeout);
|
|
|
|
|
}
|
|
|
|
|
catch (SocketException exception)
|
|
|
|
|
{
|
|
|
|
|
return WriteWinSock2Error(context, (WsaError)exception.ErrorCode);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (timeout == -1)
|
|
|
|
|
{
|
|
|
|
|
// FIXME: If we get a timeout of -1 and there is no fds to wait on, this should kill the KProces. (need to check that with re)
|
|
|
|
|
throw new InvalidOperationException();
|
2018-03-12 01:05:39 +00:00
|
|
|
|
}
|
2019-12-22 19:33:59 +00:00
|
|
|
|
else
|
2018-03-12 01:05:39 +00:00
|
|
|
|
{
|
2019-12-22 19:33:59 +00:00
|
|
|
|
// FIXME: We should make the KThread sleep but we can't do much about it yet.
|
|
|
|
|
Thread.Sleep(timeout);
|
2018-03-12 01:05:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
for (int i = 0; i < fdsCount; i++)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
PollEvent Event = events[i];
|
2020-05-03 22:54:50 +00:00
|
|
|
|
context.Memory.Write((ulong)(bufferPosition + i * 8), Event.SocketFd);
|
|
|
|
|
context.Memory.Write((ulong)(bufferPosition + i * 8 + 4), (short)Event.InputEvents);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
PollEvent.EventTypeMask outputEvents = 0;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
Socket socket = Event.Socket.Handle;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (errorEvents.Contains(socket))
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
outputEvents |= PollEvent.EventTypeMask.Error;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (!socket.Connected || !socket.IsBound)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
outputEvents |= PollEvent.EventTypeMask.Disconnected;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (readEvents.Contains(socket))
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
|
|
|
|
if ((Event.InputEvents & PollEvent.EventTypeMask.Input) != 0)
|
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
outputEvents |= PollEvent.EventTypeMask.Input;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (writeEvents.Contains(socket))
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
outputEvents |= PollEvent.EventTypeMask.Output;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-03 22:54:50 +00:00
|
|
|
|
context.Memory.Write((ulong)(bufferPosition + i * 8 + 6), (short)outputEvents);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return WriteBsdResult(context, readEvents.Count + writeEvents.Count + errorEvents.Count, LinuxError.SUCCESS);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 01:13:43 +00:00
|
|
|
|
[Command(7)]
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// Sysctl(buffer<unknown, 0x21, 0>, buffer<unknown, 0x21, 0>) -> (i32 ret, u32 bsd_errno, u32, buffer<unknown, 0x22, 0>)
|
2019-07-14 19:04:38 +00:00
|
|
|
|
public ResultCode Sysctl(ServiceCtx context)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
WriteBsdResult(context, -1, LinuxError.EOPNOTSUPP);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2019-01-11 00:11:46 +00:00
|
|
|
|
Logger.PrintStub(LogClass.ServiceBsd);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2019-07-14 19:04:38 +00:00
|
|
|
|
return ResultCode.Success;
|
2018-03-12 01:05:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 01:13:43 +00:00
|
|
|
|
[Command(8)]
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// Recv(u32 socket, u32 flags) -> (i32 ret, u32 bsd_errno, array<i8, 0x22> message)
|
2019-07-14 19:04:38 +00:00
|
|
|
|
public ResultCode Recv(ServiceCtx context)
|
2018-03-12 01:05:39 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
|
|
|
|
SocketFlags socketFlags = (SocketFlags)context.RequestData.ReadInt32();
|
2018-04-06 04:01:52 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
(long receivePosition, long receiveLength) = context.Request.GetBufferType0x22();
|
2018-08-24 17:20:42 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket socket = RetrieveSocket(socketFd);
|
|
|
|
|
int result = -1;
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (socket != null)
|
2018-03-12 01:05:39 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (socketFlags != SocketFlags.None && (socketFlags & SocketFlags.OutOfBand) == 0
|
|
|
|
|
&& (socketFlags & SocketFlags.Peek) == 0)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported Recv flags: {socketFlags}");
|
|
|
|
|
return WriteBsdResult(context, -1, LinuxError.EOPNOTSUPP);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
byte[] receivedBuffer = new byte[receiveLength];
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
result = socket.Handle.Receive(receivedBuffer, socketFlags);
|
|
|
|
|
errno = SetResultErrno(socket.Handle, result);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2020-05-03 22:54:50 +00:00
|
|
|
|
context.Memory.Write((ulong)receivePosition, receivedBuffer);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
2018-12-06 11:16:24 +00:00
|
|
|
|
catch (SocketException exception)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errno = ConvertError((WsaError)exception.ErrorCode);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
2018-03-12 01:05:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return WriteBsdResult(context, result, errno);
|
2018-03-12 01:05:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 01:13:43 +00:00
|
|
|
|
[Command(9)]
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// RecvFrom(u32 sock, u32 flags) -> (i32 ret, u32 bsd_errno, u32 addrlen, buffer<i8, 0x22, 0> message, buffer<nn::socket::sockaddr_in, 0x22, 0x10>)
|
2019-07-14 19:04:38 +00:00
|
|
|
|
public ResultCode RecvFrom(ServiceCtx context)
|
2018-03-12 01:05:39 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
|
|
|
|
SocketFlags socketFlags = (SocketFlags)context.RequestData.ReadInt32();
|
2018-04-06 04:01:52 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
(long receivePosition, long receiveLength) = context.Request.GetBufferType0x22();
|
|
|
|
|
(long sockAddrOutPosition, long sockAddrOutSize) = context.Request.GetBufferType0x22(1);
|
2018-08-24 17:20:42 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket socket = RetrieveSocket(socketFd);
|
|
|
|
|
int result = -1;
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (socket != null)
|
2018-03-12 01:05:39 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (socketFlags != SocketFlags.None && (socketFlags & SocketFlags.OutOfBand) == 0
|
|
|
|
|
&& (socketFlags & SocketFlags.Peek) == 0)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported Recv flags: {socketFlags}");
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return WriteBsdResult(context, -1, LinuxError.EOPNOTSUPP);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
byte[] receivedBuffer = new byte[receiveLength];
|
2019-07-13 23:47:18 +00:00
|
|
|
|
EndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-03-12 01:05:39 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
result = socket.Handle.ReceiveFrom(receivedBuffer, receivedBuffer.Length, socketFlags, ref endPoint);
|
|
|
|
|
errno = SetResultErrno(socket.Handle, result);
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2020-05-03 22:54:50 +00:00
|
|
|
|
context.Memory.Write((ulong)receivePosition, receivedBuffer);
|
2018-12-06 11:16:24 +00:00
|
|
|
|
WriteSockAddr(context, sockAddrOutPosition, (IPEndPoint)endPoint);
|
2018-03-12 01:05:39 +00:00
|
|
|
|
}
|
2018-12-06 11:16:24 +00:00
|
|
|
|
catch (SocketException exception)
|
2018-03-12 01:05:39 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errno = ConvertError((WsaError)exception.ErrorCode);
|
2018-03-12 01:05:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return WriteBsdResult(context, result, errno);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2019-07-12 01:13:43 +00:00
|
|
|
|
[Command(10)]
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// Send(u32 socket, u32 flags, buffer<i8, 0x21, 0>) -> (i32 ret, u32 bsd_errno)
|
2019-07-14 19:04:38 +00:00
|
|
|
|
public ResultCode Send(ServiceCtx context)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
|
|
|
|
SocketFlags socketFlags = (SocketFlags)context.RequestData.ReadInt32();
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
(long sendPosition, long sendSize) = context.Request.GetBufferType0x21();
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket socket = RetrieveSocket(socketFd);
|
|
|
|
|
int result = -1;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (socket != null)
|
2018-03-12 01:05:39 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (socketFlags != SocketFlags.None && socketFlags != SocketFlags.OutOfBand
|
|
|
|
|
&& socketFlags != SocketFlags.Peek && socketFlags != SocketFlags.DontRoute)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported Send flags: {socketFlags}");
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return WriteBsdResult(context, -1, LinuxError.EOPNOTSUPP);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-03 22:54:50 +00:00
|
|
|
|
byte[] sendBuffer = new byte[sendSize];
|
|
|
|
|
|
|
|
|
|
context.Memory.Read((ulong)sendPosition, sendBuffer);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
result = socket.Handle.Send(sendBuffer, socketFlags);
|
|
|
|
|
errno = SetResultErrno(socket.Handle, result);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
2018-12-06 11:16:24 +00:00
|
|
|
|
catch (SocketException exception)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errno = ConvertError((WsaError)exception.ErrorCode);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-12 01:05:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return WriteBsdResult(context, result, errno);
|
2018-03-12 01:05:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 01:13:43 +00:00
|
|
|
|
[Command(11)]
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// SendTo(u32 socket, u32 flags, buffer<i8, 0x21, 0>, buffer<nn::socket::sockaddr_in, 0x21, 0x10>) -> (i32 ret, u32 bsd_errno)
|
2019-07-14 19:04:38 +00:00
|
|
|
|
public ResultCode SendTo(ServiceCtx context)
|
2018-03-12 01:05:39 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
|
|
|
|
SocketFlags socketFlags = (SocketFlags)context.RequestData.ReadInt32();
|
2018-04-06 04:01:52 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
(long sendPosition, long sendSize) = context.Request.GetBufferType0x21();
|
|
|
|
|
(long bufferPosition, long bufferSize) = context.Request.GetBufferType0x21(1);
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket socket = RetrieveSocket(socketFd);
|
|
|
|
|
int result = -1;
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (socket != null)
|
2018-03-12 01:05:39 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (socketFlags != SocketFlags.None && socketFlags != SocketFlags.OutOfBand
|
|
|
|
|
&& socketFlags != SocketFlags.Peek && socketFlags != SocketFlags.DontRoute)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported Send flags: {socketFlags}");
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return WriteBsdResult(context, -1, LinuxError.EOPNOTSUPP);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-03 22:54:50 +00:00
|
|
|
|
byte[] sendBuffer = new byte[sendSize];
|
|
|
|
|
|
|
|
|
|
context.Memory.Read((ulong)sendPosition, sendBuffer);
|
|
|
|
|
|
|
|
|
|
EndPoint endPoint = ParseSockAddr(context, bufferPosition, bufferSize);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-03-12 01:05:39 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
result = socket.Handle.SendTo(sendBuffer, sendBuffer.Length, socketFlags, endPoint);
|
|
|
|
|
errno = SetResultErrno(socket.Handle, result);
|
2018-03-12 01:05:39 +00:00
|
|
|
|
}
|
2018-12-06 11:16:24 +00:00
|
|
|
|
catch (SocketException exception)
|
2018-03-12 01:05:39 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errno = ConvertError((WsaError)exception.ErrorCode);
|
2018-03-12 01:05:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return WriteBsdResult(context, result, errno);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 01:13:43 +00:00
|
|
|
|
[Command(12)]
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// Accept(u32 socket) -> (i32 ret, u32 bsd_errno, u32 addrlen, buffer<nn::socket::sockaddr_in, 0x22, 0x10> addr)
|
2019-07-14 19:04:38 +00:00
|
|
|
|
public ResultCode Accept(ServiceCtx context)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
(long bufferPos, long bufferSize) = context.Request.GetBufferType0x22();
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket socket = RetrieveSocket(socketFd);
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (socket != null)
|
2018-03-12 01:05:39 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errno = LinuxError.SUCCESS;
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
Socket newSocket = null;
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2018-10-20 22:08:58 +00:00
|
|
|
|
try
|
2018-03-12 01:05:39 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
newSocket = socket.Handle.Accept();
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
2018-12-06 11:16:24 +00:00
|
|
|
|
catch (SocketException exception)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errno = ConvertError((WsaError)exception.ErrorCode);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (newSocket == null && errno == LinuxError.SUCCESS)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errno = LinuxError.EWOULDBLOCK;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
2018-12-06 11:16:24 +00:00
|
|
|
|
else if (errno == LinuxError.SUCCESS)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
BsdSocket newBsdSocket = new BsdSocket
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
Family = (int)newSocket.AddressFamily,
|
|
|
|
|
Type = (int)newSocket.SocketType,
|
|
|
|
|
Protocol = (int)newSocket.ProtocolType,
|
|
|
|
|
Handle = newSocket
|
2018-10-20 22:08:58 +00:00
|
|
|
|
};
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
_sockets.Add(newBsdSocket);
|
2018-04-06 04:01:52 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
WriteSockAddr(context, bufferPos, newBsdSocket, true);
|
2018-04-06 04:01:52 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
WriteBsdResult(context, _sockets.Count - 1, errno);
|
2018-04-06 04:06:34 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
context.ResponseData.Write(0x10);
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2019-07-14 19:04:38 +00:00
|
|
|
|
return ResultCode.Success;
|
2018-03-12 01:05:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return WriteBsdResult(context, -1, errno);
|
2018-03-12 01:05:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 01:13:43 +00:00
|
|
|
|
[Command(13)]
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// Bind(u32 socket, buffer<nn::socket::sockaddr_in, 0x21, 0x10> addr) -> (i32 ret, u32 bsd_errno)
|
2019-07-14 19:04:38 +00:00
|
|
|
|
public ResultCode Bind(ServiceCtx context)
|
2018-03-12 01:05:39 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
(long bufferPos, long bufferSize) = context.Request.GetBufferType0x21();
|
2018-08-24 17:20:42 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket socket = RetrieveSocket(socketFd);
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (socket != null)
|
2018-03-12 01:05:39 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errno = LinuxError.SUCCESS;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
IPEndPoint endPoint = ParseSockAddr(context, bufferPos, bufferSize);
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
socket.Handle.Bind(endPoint);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
2018-12-06 11:16:24 +00:00
|
|
|
|
catch (SocketException exception)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errno = ConvertError((WsaError)exception.ErrorCode);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
2018-03-12 01:05:39 +00:00
|
|
|
|
}
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return WriteBsdResult(context, 0, errno);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 01:13:43 +00:00
|
|
|
|
[Command(14)]
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// Connect(u32 socket, buffer<nn::socket::sockaddr_in, 0x21, 0x10>) -> (i32 ret, u32 bsd_errno)
|
2019-07-14 19:04:38 +00:00
|
|
|
|
public ResultCode Connect(ServiceCtx context)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
(long bufferPos, long bufferSize) = context.Request.GetBufferType0x21();
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket socket = RetrieveSocket(socketFd);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (socket != null)
|
2018-03-12 01:05:39 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errno = LinuxError.SUCCESS;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
IPEndPoint endPoint = ParseSockAddr(context, bufferPos, bufferSize);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
socket.Handle.Connect(endPoint);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
2018-12-06 11:16:24 +00:00
|
|
|
|
catch (SocketException exception)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errno = ConvertError((WsaError)exception.ErrorCode);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
2018-03-12 01:05:39 +00:00
|
|
|
|
}
|
2018-02-28 20:58:04 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return WriteBsdResult(context, 0, errno);
|
2018-02-28 20:58:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 01:13:43 +00:00
|
|
|
|
[Command(15)]
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// GetPeerName(u32 socket) -> (i32 ret, u32 bsd_errno, u32 addrlen, buffer<nn::socket::sockaddr_in, 0x22, 0x10> addr)
|
2019-07-14 19:04:38 +00:00
|
|
|
|
public ResultCode GetPeerName(ServiceCtx context)
|
2018-02-28 20:58:04 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
2018-02-28 20:58:04 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
(long bufferPos, long bufferSize) = context.Request.GetBufferType0x22();
|
2018-08-24 17:20:42 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket socket = RetrieveSocket(socketFd);
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (socket != null)
|
2018-03-12 01:05:39 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errno = LinuxError.SUCCESS;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
WriteSockAddr(context, bufferPos, socket, true);
|
|
|
|
|
WriteBsdResult(context, 0, errno);
|
|
|
|
|
context.ResponseData.Write(0x10);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return WriteBsdResult(context, 0, errno);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 01:13:43 +00:00
|
|
|
|
[Command(16)]
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// GetSockName(u32 socket) -> (i32 ret, u32 bsd_errno, u32 addrlen, buffer<nn::socket::sockaddr_in, 0x22, 0x10> addr)
|
2019-07-14 19:04:38 +00:00
|
|
|
|
public ResultCode GetSockName(ServiceCtx context)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
(long bufferPos, long bufferSize) = context.Request.GetBufferType0x22();
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket socket = RetrieveSocket(socketFd);
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (socket != null)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errno = LinuxError.SUCCESS;
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
WriteSockAddr(context, bufferPos, socket, false);
|
|
|
|
|
WriteBsdResult(context, 0, errno);
|
|
|
|
|
context.ResponseData.Write(0x10);
|
2018-03-12 01:05:39 +00:00
|
|
|
|
}
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return WriteBsdResult(context, 0, errno);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 01:13:43 +00:00
|
|
|
|
[Command(17)]
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// GetSockOpt(u32 socket, u32 level, u32 option_name) -> (i32 ret, u32 bsd_errno, u32, buffer<unknown, 0x22, 0>)
|
2019-07-14 19:04:38 +00:00
|
|
|
|
public ResultCode GetSockOpt(ServiceCtx context)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
|
|
|
|
int level = context.RequestData.ReadInt32();
|
|
|
|
|
int optionName = context.RequestData.ReadInt32();
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
(long bufferPosition, long bufferSize) = context.Request.GetBufferType0x22();
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket socket = RetrieveSocket(socketFd);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (socket != null)
|
2018-03-12 01:05:39 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errno = LinuxError.ENOPROTOOPT;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (level == 0xFFFF)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errno = HandleGetSocketOption(context, socket, (SocketOptionName)optionName, bufferPosition, bufferSize);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported GetSockOpt Level: {(SocketOptionLevel)level}");
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
2018-03-12 01:05:39 +00:00
|
|
|
|
}
|
2018-02-28 20:58:04 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return WriteBsdResult(context, 0, errno);
|
2018-02-28 20:58:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 01:13:43 +00:00
|
|
|
|
[Command(18)]
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// Listen(u32 socket, u32 backlog) -> (i32 ret, u32 bsd_errno)
|
2019-07-14 19:04:38 +00:00
|
|
|
|
public ResultCode Listen(ServiceCtx context)
|
2018-02-28 20:58:04 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
|
|
|
|
int backlog = context.RequestData.ReadInt32();
|
2018-02-28 20:58:04 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket socket = RetrieveSocket(socketFd);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (socket != null)
|
2018-03-12 01:05:39 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errno = LinuxError.SUCCESS;
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2018-10-20 22:08:58 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
socket.Handle.Listen(backlog);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
2018-12-06 11:16:24 +00:00
|
|
|
|
catch (SocketException exception)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errno = ConvertError((WsaError)exception.ErrorCode);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
2018-03-12 01:05:39 +00:00
|
|
|
|
}
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return WriteBsdResult(context, 0, errno);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 01:13:43 +00:00
|
|
|
|
[Command(19)]
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// Ioctl(u32 fd, u32 request, u32 bufcount, buffer<unknown, 0x21, 0>, buffer<unknown, 0x21, 0>, buffer<unknown, 0x21, 0>, buffer<unknown, 0x21, 0>) -> (i32 ret, u32 bsd_errno, buffer<unknown, 0x22, 0>, buffer<unknown, 0x22, 0>, buffer<unknown, 0x22, 0>, buffer<unknown, 0x22, 0>)
|
2019-07-14 19:04:38 +00:00
|
|
|
|
public ResultCode Ioctl(ServiceCtx context)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
|
|
|
|
BsdIoctl cmd = (BsdIoctl)context.RequestData.ReadInt32();
|
|
|
|
|
int bufferCount = context.RequestData.ReadInt32();
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket socket = RetrieveSocket(socketFd);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (socket != null)
|
2018-03-12 01:05:39 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
switch (cmd)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
|
|
|
|
case BsdIoctl.AtMark:
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errno = LinuxError.SUCCESS;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
(long bufferPosition, long bufferSize) = context.Request.GetBufferType0x22();
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
|
|
|
|
// FIXME: OOB not implemented.
|
2020-05-03 22:54:50 +00:00
|
|
|
|
context.Memory.Write((ulong)bufferPosition, 0);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errno = LinuxError.EOPNOTSUPP;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported Ioctl Cmd: {cmd}");
|
2018-10-20 22:08:58 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2018-03-12 01:05:39 +00:00
|
|
|
|
}
|
2018-02-28 20:58:04 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return WriteBsdResult(context, 0, errno);
|
2018-02-28 20:58:04 +00:00
|
|
|
|
}
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2019-07-12 01:13:43 +00:00
|
|
|
|
[Command(20)]
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// Fcntl(u32 socket, u32 cmd, u32 arg) -> (i32 ret, u32 bsd_errno)
|
2019-07-14 19:04:38 +00:00
|
|
|
|
public ResultCode Fcntl(ServiceCtx context)
|
2018-03-12 01:05:39 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
|
|
|
|
int cmd = context.RequestData.ReadInt32();
|
|
|
|
|
int arg = context.RequestData.ReadInt32();
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
int result = 0;
|
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket socket = RetrieveSocket(socketFd);
|
2018-04-06 04:01:52 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (socket != null)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errno = LinuxError.SUCCESS;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (cmd == 0x3)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
result = !socket.Handle.Blocking ? 0x800 : 0;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
2018-12-06 11:16:24 +00:00
|
|
|
|
else if (cmd == 0x4 && arg == 0x800)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
socket.Handle.Blocking = false;
|
|
|
|
|
result = 0;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errno = LinuxError.EOPNOTSUPP;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return WriteBsdResult(context, result, errno);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
2018-04-06 04:01:52 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
private LinuxError HandleGetSocketOption(ServiceCtx context, BsdSocket socket, SocketOptionName optionName, long optionValuePosition, long optionValueSize)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-03-12 01:05:39 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
byte[] optionValue = new byte[optionValueSize];
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
switch (optionName)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
|
|
|
|
case SocketOptionName.Broadcast:
|
|
|
|
|
case SocketOptionName.DontLinger:
|
|
|
|
|
case SocketOptionName.Debug:
|
|
|
|
|
case SocketOptionName.Error:
|
|
|
|
|
case SocketOptionName.KeepAlive:
|
|
|
|
|
case SocketOptionName.OutOfBandInline:
|
|
|
|
|
case SocketOptionName.ReceiveBuffer:
|
|
|
|
|
case SocketOptionName.ReceiveTimeout:
|
|
|
|
|
case SocketOptionName.SendBuffer:
|
|
|
|
|
case SocketOptionName.SendTimeout:
|
|
|
|
|
case SocketOptionName.Type:
|
|
|
|
|
case SocketOptionName.Linger:
|
2018-12-06 11:16:24 +00:00
|
|
|
|
socket.Handle.GetSocketOption(SocketOptionLevel.Socket, optionName, optionValue);
|
2020-05-03 22:54:50 +00:00
|
|
|
|
context.Memory.Write((ulong)optionValuePosition, optionValue);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
|
|
|
|
return LinuxError.SUCCESS;
|
|
|
|
|
|
|
|
|
|
case (SocketOptionName)0x200:
|
2018-12-06 11:16:24 +00:00
|
|
|
|
socket.Handle.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, optionValue);
|
2020-05-03 22:54:50 +00:00
|
|
|
|
context.Memory.Write((ulong)optionValuePosition, optionValue);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
|
|
|
|
return LinuxError.SUCCESS;
|
|
|
|
|
|
|
|
|
|
default:
|
2018-12-06 11:16:24 +00:00
|
|
|
|
Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported SetSockOpt OptionName: {optionName}");
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
|
|
|
|
return LinuxError.EOPNOTSUPP;
|
|
|
|
|
}
|
2018-03-12 01:05:39 +00:00
|
|
|
|
}
|
2018-12-06 11:16:24 +00:00
|
|
|
|
catch (SocketException exception)
|
2018-03-12 01:05:39 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return ConvertError((WsaError)exception.ErrorCode);
|
2018-03-12 01:05:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
private LinuxError HandleSetSocketOption(ServiceCtx context, BsdSocket socket, SocketOptionName optionName, long optionValuePosition, long optionValueSize)
|
2018-08-24 17:20:42 +00:00
|
|
|
|
{
|
2018-10-20 22:08:58 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
switch (optionName)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
|
|
|
|
case SocketOptionName.Broadcast:
|
|
|
|
|
case SocketOptionName.DontLinger:
|
|
|
|
|
case SocketOptionName.Debug:
|
|
|
|
|
case SocketOptionName.Error:
|
|
|
|
|
case SocketOptionName.KeepAlive:
|
|
|
|
|
case SocketOptionName.OutOfBandInline:
|
|
|
|
|
case SocketOptionName.ReceiveBuffer:
|
|
|
|
|
case SocketOptionName.ReceiveTimeout:
|
|
|
|
|
case SocketOptionName.SendBuffer:
|
|
|
|
|
case SocketOptionName.SendTimeout:
|
|
|
|
|
case SocketOptionName.Type:
|
|
|
|
|
case SocketOptionName.ReuseAddress:
|
2020-05-03 22:54:50 +00:00
|
|
|
|
socket.Handle.SetSocketOption(SocketOptionLevel.Socket, optionName, context.Memory.Read<int>((ulong)optionValuePosition));
|
2018-08-24 17:20:42 +00:00
|
|
|
|
|
2018-10-20 22:08:58 +00:00
|
|
|
|
return LinuxError.SUCCESS;
|
2018-08-24 17:20:42 +00:00
|
|
|
|
|
2018-10-20 22:08:58 +00:00
|
|
|
|
case (SocketOptionName)0x200:
|
2020-05-03 22:54:50 +00:00
|
|
|
|
socket.Handle.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, context.Memory.Read<int>((ulong)optionValuePosition));
|
2018-08-24 17:20:42 +00:00
|
|
|
|
|
2018-10-20 22:08:58 +00:00
|
|
|
|
return LinuxError.SUCCESS;
|
2018-08-24 17:20:42 +00:00
|
|
|
|
|
2018-10-20 22:08:58 +00:00
|
|
|
|
case SocketOptionName.Linger:
|
2018-12-06 11:16:24 +00:00
|
|
|
|
socket.Handle.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger,
|
2020-05-03 22:54:50 +00:00
|
|
|
|
new LingerOption(context.Memory.Read<int>((ulong)optionValuePosition) != 0, context.Memory.Read<int>((ulong)optionValuePosition + 4)));
|
2018-08-24 17:20:42 +00:00
|
|
|
|
|
2018-10-20 22:08:58 +00:00
|
|
|
|
return LinuxError.SUCCESS;
|
|
|
|
|
|
|
|
|
|
default:
|
2018-12-06 11:16:24 +00:00
|
|
|
|
Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported SetSockOpt OptionName: {optionName}");
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
|
|
|
|
return LinuxError.EOPNOTSUPP;
|
|
|
|
|
}
|
2018-08-24 17:20:42 +00:00
|
|
|
|
}
|
2018-12-06 11:16:24 +00:00
|
|
|
|
catch (SocketException exception)
|
2018-08-24 17:20:42 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return ConvertError((WsaError)exception.ErrorCode);
|
2018-08-24 17:20:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 01:13:43 +00:00
|
|
|
|
[Command(21)]
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// SetSockOpt(u32 socket, u32 level, u32 option_name, buffer<unknown, 0x21, 0> option_value) -> (i32 ret, u32 bsd_errno)
|
2019-07-14 19:04:38 +00:00
|
|
|
|
public ResultCode SetSockOpt(ServiceCtx context)
|
2018-08-24 17:20:42 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
|
|
|
|
int level = context.RequestData.ReadInt32();
|
|
|
|
|
int optionName = context.RequestData.ReadInt32();
|
2018-08-24 17:20:42 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
(long bufferPos, long bufferSize) = context.Request.GetBufferType0x21();
|
2018-08-24 17:20:42 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket socket = RetrieveSocket(socketFd);
|
2018-08-24 17:20:42 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (socket != null)
|
2018-08-24 17:20:42 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errno = LinuxError.ENOPROTOOPT;
|
2018-08-24 17:20:42 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (level == 0xFFFF)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errno = HandleSetSocketOption(context, socket, (SocketOptionName)optionName, bufferPos, bufferSize);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported SetSockOpt Level: {(SocketOptionLevel)level}");
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
2018-08-24 17:20:42 +00:00
|
|
|
|
}
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return WriteBsdResult(context, 0, errno);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 01:13:43 +00:00
|
|
|
|
[Command(22)]
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// Shutdown(u32 socket, u32 how) -> (i32 ret, u32 bsd_errno)
|
2019-07-14 19:04:38 +00:00
|
|
|
|
public ResultCode Shutdown(ServiceCtx context)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
|
|
|
|
int how = context.RequestData.ReadInt32();
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket socket = RetrieveSocket(socketFd);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (socket != null)
|
2018-08-24 17:20:42 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errno = LinuxError.EINVAL;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (how >= 0 && how <= 2)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errno = LinuxError.SUCCESS;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
socket.Handle.Shutdown((SocketShutdown)how);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
2018-12-06 11:16:24 +00:00
|
|
|
|
catch (SocketException exception)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errno = ConvertError((WsaError)exception.ErrorCode);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-24 17:20:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return WriteBsdResult(context, 0, errno);
|
2018-08-24 17:20:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 01:13:43 +00:00
|
|
|
|
[Command(23)]
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// ShutdownAllSockets(u32 how) -> (i32 ret, u32 bsd_errno)
|
2019-07-14 19:04:38 +00:00
|
|
|
|
public ResultCode ShutdownAllSockets(ServiceCtx context)
|
2018-03-12 01:05:39 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
int how = context.RequestData.ReadInt32();
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
LinuxError errno = LinuxError.EINVAL;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (how >= 0 && how <= 2)
|
2018-03-12 01:05:39 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errno = LinuxError.SUCCESS;
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
foreach (BsdSocket socket in _sockets)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (socket != null)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
socket.Handle.Shutdown((SocketShutdown)how);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
2018-12-06 11:16:24 +00:00
|
|
|
|
catch (SocketException exception)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errno = ConvertError((WsaError)exception.ErrorCode);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-12 01:05:39 +00:00
|
|
|
|
}
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return WriteBsdResult(context, 0, errno);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 01:13:43 +00:00
|
|
|
|
[Command(24)]
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// Write(u32 socket, buffer<i8, 0x21, 0> message) -> (i32 ret, u32 bsd_errno)
|
2019-07-14 19:04:38 +00:00
|
|
|
|
public ResultCode Write(ServiceCtx context)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
(long sendPosition, long sendSize) = context.Request.GetBufferType0x21();
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket socket = RetrieveSocket(socketFd);
|
|
|
|
|
int result = -1;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (socket != null)
|
2018-03-12 01:05:39 +00:00
|
|
|
|
{
|
2020-05-03 22:54:50 +00:00
|
|
|
|
byte[] sendBuffer = new byte[sendSize];
|
|
|
|
|
|
|
|
|
|
context.Memory.Read((ulong)sendPosition, sendBuffer);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
result = socket.Handle.Send(sendBuffer);
|
|
|
|
|
errno = SetResultErrno(socket.Handle, result);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
2018-12-06 11:16:24 +00:00
|
|
|
|
catch (SocketException exception)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errno = ConvertError((WsaError)exception.ErrorCode);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
2018-03-12 01:05:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return WriteBsdResult(context, result, errno);
|
2018-03-12 01:05:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 01:13:43 +00:00
|
|
|
|
[Command(25)]
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// Read(u32 socket) -> (i32 ret, u32 bsd_errno, buffer<i8, 0x22, 0> message)
|
2019-07-14 19:04:38 +00:00
|
|
|
|
public ResultCode Read(ServiceCtx context)
|
2018-03-12 01:05:39 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
(long receivePosition, long receiveLength) = context.Request.GetBufferType0x22();
|
2018-04-06 04:01:52 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket socket = RetrieveSocket(socketFd);
|
|
|
|
|
int result = -1;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (socket != null)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
byte[] receivedBuffer = new byte[receiveLength];
|
2018-03-12 01:05:39 +00:00
|
|
|
|
|
2018-10-20 22:08:58 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
result = socket.Handle.Receive(receivedBuffer);
|
|
|
|
|
errno = SetResultErrno(socket.Handle, result);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
2018-12-06 11:16:24 +00:00
|
|
|
|
catch (SocketException exception)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errno = ConvertError((WsaError)exception.ErrorCode);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
2018-03-12 01:05:39 +00:00
|
|
|
|
}
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return WriteBsdResult(context, result, errno);
|
2018-03-12 01:05:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 01:13:43 +00:00
|
|
|
|
[Command(26)]
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// Close(u32 socket) -> (i32 ret, u32 bsd_errno)
|
2019-07-14 19:04:38 +00:00
|
|
|
|
public ResultCode Close(ServiceCtx context)
|
2018-03-12 01:05:39 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket socket = RetrieveSocket(socketFd);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (socket != null)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
socket.Handle.Close();
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
_sockets[socketFd] = null;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errno = LinuxError.SUCCESS;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return WriteBsdResult(context, 0, errno);
|
2018-03-12 01:05:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 01:13:43 +00:00
|
|
|
|
[Command(27)]
|
2018-10-20 22:08:58 +00:00
|
|
|
|
// DuplicateSocket(u32 socket, u64 reserved) -> (i32 ret, u32 bsd_errno)
|
2019-07-14 19:04:38 +00:00
|
|
|
|
public ResultCode DuplicateSocket(ServiceCtx context)
|
2018-03-12 01:05:39 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
|
|
|
|
ulong reserved = context.RequestData.ReadUInt64();
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
LinuxError errno = LinuxError.ENOENT;
|
|
|
|
|
int newSockFd = -1;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (_isPrivileged)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
errno = LinuxError.EBADF;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
BsdSocket oldSocket = RetrieveSocket(socketFd);
|
2018-10-20 22:08:58 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (oldSocket != null)
|
2018-10-20 22:08:58 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
_sockets.Add(oldSocket);
|
|
|
|
|
newSockFd = _sockets.Count - 1;
|
2018-10-20 22:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return WriteBsdResult(context, newSockFd, errno);
|
2018-03-12 01:05:39 +00:00
|
|
|
|
}
|
2018-02-28 03:31:52 +00:00
|
|
|
|
}
|
2019-07-12 01:13:43 +00:00
|
|
|
|
}
|