2022-01-12 13:31:08 -05:00
|
|
|
|
using Ryujinx.Common;
|
|
|
|
|
using Ryujinx.Common.Logging;
|
|
|
|
|
using Ryujinx.Memory;
|
2019-12-22 14:33:59 -05:00
|
|
|
|
using System;
|
2018-02-27 22:31:52 -05:00
|
|
|
|
using System.Collections.Generic;
|
2018-03-11 21:05:39 -04:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Sockets;
|
2022-01-12 13:31:08 -05:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2018-10-20 18:08:58 -04:00
|
|
|
|
using System.Text;
|
2018-02-27 22:31:52 -05:00
|
|
|
|
|
2019-09-18 20:45:11 -04:00
|
|
|
|
namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd
|
2018-02-27 22:31:52 -05:00
|
|
|
|
{
|
2019-07-10 11:59:54 -04:00
|
|
|
|
[Service("bsd:s", true)]
|
|
|
|
|
[Service("bsd:u", false)]
|
2018-04-06 00:01:52 -04:00
|
|
|
|
class IClient : IpcService
|
2018-02-27 22:31:52 -05:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
private static readonly List<IPollManager> _pollManagers = new List<IPollManager>
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
EventFileDescriptorPollManager.Instance,
|
|
|
|
|
ManagedSocketPollManager.Instance
|
2021-12-29 09:04:38 -05:00
|
|
|
|
};
|
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
private BsdContext _context;
|
2018-12-06 06:16:24 -05:00
|
|
|
|
private bool _isPrivileged;
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2020-12-02 07:14:44 -05:00
|
|
|
|
public IClient(ServiceCtx context, bool isPrivileged) : base(context.Device.System.BsdServer)
|
2018-02-27 22:31:52 -05:00
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
_isPrivileged = isPrivileged;
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
private ResultCode WriteBsdResult(ServiceCtx context, int result, LinuxError errorCode = LinuxError.SUCCESS)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
if (errorCode != LinuxError.SUCCESS)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
result = -1;
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
context.ResponseData.Write(result);
|
|
|
|
|
context.ResponseData.Write((int)errorCode);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2019-07-14 15:04:38 -04:00
|
|
|
|
return ResultCode.Success;
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
private static AddressFamily ConvertBsdAddressFamily(BsdAddressFamily family)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
switch (family)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
case BsdAddressFamily.Unspecified:
|
|
|
|
|
return AddressFamily.Unspecified;
|
|
|
|
|
case BsdAddressFamily.InterNetwork:
|
|
|
|
|
return AddressFamily.InterNetwork;
|
|
|
|
|
case BsdAddressFamily.InterNetworkV6:
|
|
|
|
|
return AddressFamily.InterNetworkV6;
|
|
|
|
|
case BsdAddressFamily.Unknown:
|
|
|
|
|
return AddressFamily.Unknown;
|
|
|
|
|
default:
|
|
|
|
|
throw new NotImplementedException(family.ToString());
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
private LinuxError SetResultErrno(IFileDescriptor socket, int result)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
return result == 0 && !socket.Blocking ? LinuxError.EWOULDBLOCK : LinuxError.SUCCESS;
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-14 15:04:38 -04:00
|
|
|
|
private ResultCode SocketInternal(ServiceCtx context, bool exempt)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
BsdAddressFamily domain = (BsdAddressFamily)context.RequestData.ReadInt32();
|
|
|
|
|
BsdSocketType type = (BsdSocketType)context.RequestData.ReadInt32();
|
|
|
|
|
ProtocolType protocol = (ProtocolType)context.RequestData.ReadInt32();
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
BsdSocketCreationFlags creationFlags = (BsdSocketCreationFlags)((int)type >> (int)BsdSocketCreationFlags.FlagsShift);
|
|
|
|
|
type &= BsdSocketType.TypeMask;
|
|
|
|
|
|
|
|
|
|
if (domain == BsdAddressFamily.Unknown)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
return WriteBsdResult(context, -1, LinuxError.EPROTONOSUPPORT);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
2022-01-12 13:31:08 -05:00
|
|
|
|
else if ((type == BsdSocketType.Seqpacket || type == BsdSocketType.Raw) && !_isPrivileged)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
if (domain != BsdAddressFamily.InterNetwork || type != BsdSocketType.Raw || protocol != ProtocolType.Icmp)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
return WriteBsdResult(context, -1, LinuxError.ENOENT);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
AddressFamily netDomain = ConvertBsdAddressFamily(domain);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
if (protocol == ProtocolType.IP)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
if (type == BsdSocketType.Stream)
|
|
|
|
|
{
|
|
|
|
|
protocol = ProtocolType.Tcp;
|
|
|
|
|
}
|
|
|
|
|
else if (type == BsdSocketType.Dgram)
|
|
|
|
|
{
|
|
|
|
|
protocol = ProtocolType.Udp;
|
|
|
|
|
}
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
ISocket newBsdSocket = new ManagedSocket(netDomain, (SocketType)type, protocol);
|
|
|
|
|
newBsdSocket.Blocking = !creationFlags.HasFlag(BsdSocketCreationFlags.NonBlocking);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
LinuxError errno = LinuxError.SUCCESS;
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
int newSockFd = _context.RegisterFileDescriptor(newBsdSocket);
|
2020-05-03 18:54:50 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
if (newSockFd == -1)
|
|
|
|
|
{
|
|
|
|
|
errno = LinuxError.EBADF;
|
|
|
|
|
}
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
if (exempt)
|
|
|
|
|
{
|
|
|
|
|
newBsdSocket.Disconnect();
|
|
|
|
|
}
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
return WriteBsdResult(context, newSockFd, errno);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
private void WriteSockAddr(ServiceCtx context, ulong bufferPosition, ISocket socket, bool isRemote)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
IPEndPoint endPoint = isRemote ? socket.RemoteEndPoint : socket.LocalEndPoint;
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
context.Memory.Write(bufferPosition, BsdSockAddr.FromIPEndPoint(endPoint));
|
2018-02-27 22:31:52 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 18:01:24 -04:00
|
|
|
|
[CommandHipc(0)]
|
2018-10-20 18:08:58 -04:00
|
|
|
|
// Initialize(nn::socket::BsdBufferConfig config, u64 pid, u64 transferMemorySize, KObject<copy, transfer_memory>, pid) -> u32 bsd_errno
|
2019-07-14 15:04:38 -04:00
|
|
|
|
public ResultCode RegisterClient(ServiceCtx context)
|
2018-02-27 22:31:52 -05:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
_context = BsdContext.GetOrRegister(context.Request.HandleDesc.PId);
|
|
|
|
|
|
2018-02-27 22:31:52 -05: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 18:08:58 -04:00
|
|
|
|
// bsd_error
|
2018-12-06 06:16:24 -05:00
|
|
|
|
context.ResponseData.Write(0);
|
2018-02-27 22:31:52 -05:00
|
|
|
|
|
2020-08-03 19:32:53 -04:00
|
|
|
|
Logger.Stub?.PrintStub(LogClass.ServiceBsd);
|
2018-02-27 22:31:52 -05:00
|
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
|
// Close transfer memory immediately as we don't use it.
|
|
|
|
|
context.Device.System.KernelContext.Syscall.CloseHandle(context.Request.HandleDesc.ToCopy[0]);
|
|
|
|
|
|
2019-07-14 15:04:38 -04:00
|
|
|
|
return ResultCode.Success;
|
2018-02-27 22:31:52 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 18:01:24 -04:00
|
|
|
|
[CommandHipc(1)]
|
2018-10-20 18:08:58 -04:00
|
|
|
|
// StartMonitoring(u64, pid)
|
2019-07-14 15:04:38 -04:00
|
|
|
|
public ResultCode StartMonitoring(ServiceCtx context)
|
2018-02-27 22:31:52 -05:00
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
ulong unknown0 = context.RequestData.ReadUInt64();
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2020-08-03 19:32:53 -04:00
|
|
|
|
Logger.Stub?.PrintStub(LogClass.ServiceBsd, new { unknown0 });
|
2018-02-27 22:31:52 -05:00
|
|
|
|
|
2019-07-14 15:04:38 -04:00
|
|
|
|
return ResultCode.Success;
|
2018-02-27 22:31:52 -05:00
|
|
|
|
}
|
2018-02-28 15:58:04 -05:00
|
|
|
|
|
2021-04-13 18:01:24 -04:00
|
|
|
|
[CommandHipc(2)]
|
2018-10-20 18:08:58 -04:00
|
|
|
|
// Socket(u32 domain, u32 type, u32 protocol) -> (i32 ret, u32 bsd_errno)
|
2019-07-14 15:04:38 -04:00
|
|
|
|
public ResultCode Socket(ServiceCtx context)
|
2018-02-28 15:58:04 -05:00
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
return SocketInternal(context, false);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
2018-03-11 21:05:39 -04:00
|
|
|
|
|
2021-04-13 18:01:24 -04:00
|
|
|
|
[CommandHipc(3)]
|
2018-10-20 18:08:58 -04:00
|
|
|
|
// SocketExempt(u32 domain, u32 type, u32 protocol) -> (i32 ret, u32 bsd_errno)
|
2019-07-14 15:04:38 -04:00
|
|
|
|
public ResultCode SocketExempt(ServiceCtx context)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
return SocketInternal(context, true);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
2018-03-11 21:05:39 -04:00
|
|
|
|
|
2021-04-13 18:01:24 -04:00
|
|
|
|
[CommandHipc(4)]
|
2018-10-20 18:08:58 -04:00
|
|
|
|
// Open(u32 flags, array<unknown, 0x21> path) -> (i32 ret, u32 bsd_errno)
|
2019-07-14 15:04:38 -04:00
|
|
|
|
public ResultCode Open(ServiceCtx context)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2021-04-24 06:16:01 -04:00
|
|
|
|
(ulong bufferPosition, ulong bufferSize) = context.Request.GetBufferType0x21();
|
2018-03-11 21:05:39 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
int flags = context.RequestData.ReadInt32();
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2020-05-03 18:54:50 -04:00
|
|
|
|
byte[] rawPath = new byte[bufferSize];
|
|
|
|
|
|
2021-04-24 06:16:01 -04:00
|
|
|
|
context.Memory.Read(bufferPosition, rawPath);
|
2020-05-03 18:54:50 -04:00
|
|
|
|
|
|
|
|
|
string path = Encoding.ASCII.GetString(rawPath);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
WriteBsdResult(context, -1, LinuxError.EOPNOTSUPP);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2020-08-03 19:32:53 -04:00
|
|
|
|
Logger.Stub?.PrintStub(LogClass.ServiceBsd, new { path, flags });
|
2018-03-11 21:05:39 -04:00
|
|
|
|
|
2019-07-14 15:04:38 -04:00
|
|
|
|
return ResultCode.Success;
|
2018-03-11 21:05:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 18:01:24 -04:00
|
|
|
|
[CommandHipc(5)]
|
2018-10-20 18:08:58 -04: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 15:04:38 -04:00
|
|
|
|
public ResultCode Select(ServiceCtx context)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
WriteBsdResult(context, -1, LinuxError.EOPNOTSUPP);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2020-08-03 19:32:53 -04:00
|
|
|
|
Logger.Stub?.PrintStub(LogClass.ServiceBsd);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2019-07-14 15:04:38 -04:00
|
|
|
|
return ResultCode.Success;
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 18:01:24 -04:00
|
|
|
|
[CommandHipc(6)]
|
2018-10-20 18:08:58 -04:00
|
|
|
|
// Poll(u32 nfds, u32 timeout, buffer<unknown, 0x21, 0> fds) -> (i32 ret, u32 bsd_errno, buffer<unknown, 0x22, 0>)
|
2019-07-14 15:04:38 -04:00
|
|
|
|
public ResultCode Poll(ServiceCtx context)
|
2018-03-11 21:05:39 -04:00
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
int fdsCount = context.RequestData.ReadInt32();
|
|
|
|
|
int timeout = context.RequestData.ReadInt32();
|
2018-03-11 21:05:39 -04:00
|
|
|
|
|
2022-08-26 12:10:45 -04:00
|
|
|
|
(ulong inputBufferPosition, ulong inputBufferSize) = context.Request.GetBufferType0x21();
|
|
|
|
|
(ulong outputBufferPosition, ulong outputBufferSize) = context.Request.GetBufferType0x22();
|
2018-03-11 21:05:39 -04:00
|
|
|
|
|
2022-08-26 12:10:45 -04:00
|
|
|
|
if (timeout < -1 || fdsCount < 0 || (ulong)(fdsCount * 8) > inputBufferSize)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
return WriteBsdResult(context, -1, LinuxError.EINVAL);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
2018-03-11 21:05:39 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
PollEvent[] events = new PollEvent[fdsCount];
|
2018-03-11 21:05:39 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
for (int i = 0; i < fdsCount; i++)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2022-08-26 12:10:45 -04:00
|
|
|
|
PollEventData pollEventData = context.Memory.Read<PollEventData>(inputBufferPosition + (ulong)(i * Unsafe.SizeOf<PollEventData>()));
|
2018-02-28 15:58:04 -05:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
IFileDescriptor fileDescriptor = _context.RetrieveFileDescriptor(pollEventData.SocketFd);
|
2018-03-11 21:05:39 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
if (fileDescriptor == null)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
return WriteBsdResult(context, -1, LinuxError.EBADF);
|
|
|
|
|
}
|
2018-08-24 13:20:42 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
events[i] = new PollEvent(pollEventData, fileDescriptor);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
2018-04-06 00:01:52 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
List<PollEvent> discoveredEvents = new List<PollEvent>();
|
|
|
|
|
List<PollEvent>[] eventsByPollManager = new List<PollEvent>[_pollManagers.Count];
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
for (int i = 0; i < eventsByPollManager.Length; i++)
|
2018-03-11 21:05:39 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
eventsByPollManager[i] = new List<PollEvent>();
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
foreach (PollEvent evnt in events)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
if (_pollManagers[i].IsCompatible(evnt))
|
|
|
|
|
{
|
|
|
|
|
eventsByPollManager[i].Add(evnt);
|
|
|
|
|
discoveredEvents.Add(evnt);
|
|
|
|
|
}
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
2022-01-12 13:31:08 -05:00
|
|
|
|
}
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
foreach (PollEvent evnt in events)
|
|
|
|
|
{
|
|
|
|
|
if (!discoveredEvents.Contains(evnt))
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
Logger.Error?.Print(LogClass.ServiceBsd, $"Poll operation is not supported for {evnt.FileDescriptor.GetType().Name}!");
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
return WriteBsdResult(context, -1, LinuxError.EBADF);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
2022-01-12 13:31:08 -05:00
|
|
|
|
}
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
int updateCount = 0;
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
LinuxError errno = LinuxError.SUCCESS;
|
2018-03-11 21:05:39 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
if (fdsCount != 0)
|
|
|
|
|
{
|
|
|
|
|
bool IsUnexpectedLinuxError(LinuxError error)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2022-08-26 12:10:45 -04:00
|
|
|
|
return error != LinuxError.SUCCESS && error != LinuxError.ETIMEDOUT;
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
2018-03-11 21:05:39 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
// Hybrid approach
|
|
|
|
|
long budgetLeftMilliseconds;
|
|
|
|
|
|
|
|
|
|
if (timeout == -1)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
budgetLeftMilliseconds = PerformanceCounter.ElapsedMilliseconds + uint.MaxValue;
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
2022-01-12 13:31:08 -05:00
|
|
|
|
else
|
2019-12-22 14:33:59 -05:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
budgetLeftMilliseconds = PerformanceCounter.ElapsedMilliseconds + timeout;
|
2019-12-22 14:33:59 -05:00
|
|
|
|
}
|
2022-01-12 13:31:08 -05:00
|
|
|
|
|
|
|
|
|
do
|
2019-12-22 14:33:59 -05:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
for (int i = 0; i < eventsByPollManager.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
if (eventsByPollManager[i].Count == 0)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
errno = _pollManagers[i].Poll(eventsByPollManager[i], 0, out updateCount);
|
|
|
|
|
|
|
|
|
|
if (IsUnexpectedLinuxError(errno))
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (updateCount > 0)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we are here, that mean nothing was availaible, sleep for 50ms
|
|
|
|
|
context.Device.System.KernelContext.Syscall.SleepThread(50 * 1000000);
|
2019-12-22 14:33:59 -05:00
|
|
|
|
}
|
2022-01-12 13:31:08 -05:00
|
|
|
|
while (PerformanceCounter.ElapsedMilliseconds < budgetLeftMilliseconds);
|
2019-12-22 14:33:59 -05:00
|
|
|
|
}
|
|
|
|
|
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-11 21:05:39 -04:00
|
|
|
|
}
|
2019-12-22 14:33:59 -05:00
|
|
|
|
else
|
2018-03-11 21:05:39 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
context.Device.System.KernelContext.Syscall.SleepThread(timeout);
|
2018-03-11 21:05:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
// TODO: Spanify
|
2018-12-06 06:16:24 -05:00
|
|
|
|
for (int i = 0; i < fdsCount; i++)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2022-08-26 12:10:45 -04:00
|
|
|
|
context.Memory.Write(outputBufferPosition + (ulong)(i * Unsafe.SizeOf<PollEventData>()), events[i].Data);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-01 14:12:10 -04:00
|
|
|
|
// In case of non blocking call timeout should not be returned.
|
|
|
|
|
if (timeout == 0 && errno == LinuxError.ETIMEDOUT)
|
|
|
|
|
{
|
|
|
|
|
errno = LinuxError.SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
return WriteBsdResult(context, updateCount, errno);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 18:01:24 -04:00
|
|
|
|
[CommandHipc(7)]
|
2018-10-20 18:08:58 -04:00
|
|
|
|
// Sysctl(buffer<unknown, 0x21, 0>, buffer<unknown, 0x21, 0>) -> (i32 ret, u32 bsd_errno, u32, buffer<unknown, 0x22, 0>)
|
2019-07-14 15:04:38 -04:00
|
|
|
|
public ResultCode Sysctl(ServiceCtx context)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
WriteBsdResult(context, -1, LinuxError.EOPNOTSUPP);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2020-08-03 19:32:53 -04:00
|
|
|
|
Logger.Stub?.PrintStub(LogClass.ServiceBsd);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2019-07-14 15:04:38 -04:00
|
|
|
|
return ResultCode.Success;
|
2018-03-11 21:05:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 18:01:24 -04:00
|
|
|
|
[CommandHipc(8)]
|
2018-10-20 18:08:58 -04:00
|
|
|
|
// Recv(u32 socket, u32 flags) -> (i32 ret, u32 bsd_errno, array<i8, 0x22> message)
|
2019-07-14 15:04:38 -04:00
|
|
|
|
public ResultCode Recv(ServiceCtx context)
|
2018-03-11 21:05:39 -04:00
|
|
|
|
{
|
2021-12-26 09:17:13 -05:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
|
|
|
|
BsdSocketFlags socketFlags = (BsdSocketFlags)context.RequestData.ReadInt32();
|
2018-04-06 00:01:52 -04:00
|
|
|
|
|
2021-04-24 06:16:01 -04:00
|
|
|
|
(ulong receivePosition, ulong receiveLength) = context.Request.GetBufferType0x22();
|
2018-08-24 13:20:42 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
WritableRegion receiveRegion = context.Memory.GetWritableRegion(receivePosition, (int)receiveLength);
|
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
2022-01-12 13:31:08 -05:00
|
|
|
|
ISocket socket = _context.RetrieveSocket(socketFd);
|
2018-12-06 06:16:24 -05:00
|
|
|
|
int result = -1;
|
2018-03-11 21:05:39 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
if (socket != null)
|
2018-03-11 21:05:39 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
errno = socket.Receive(out result, receiveRegion.Memory.Span, socketFlags);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
if (errno == LinuxError.SUCCESS)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
SetResultErrno(socket, result);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
receiveRegion.Dispose();
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
2018-03-11 21:05:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
return WriteBsdResult(context, result, errno);
|
2018-03-11 21:05:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 18:01:24 -04:00
|
|
|
|
[CommandHipc(9)]
|
2018-10-20 18:08:58 -04: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 15:04:38 -04:00
|
|
|
|
public ResultCode RecvFrom(ServiceCtx context)
|
2018-03-11 21:05:39 -04:00
|
|
|
|
{
|
2021-12-26 09:17:13 -05:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
|
|
|
|
BsdSocketFlags socketFlags = (BsdSocketFlags)context.RequestData.ReadInt32();
|
2018-04-06 00:01:52 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
(ulong receivePosition, ulong receiveLength) = context.Request.GetBufferType0x22(0);
|
2021-04-24 06:16:01 -04:00
|
|
|
|
(ulong sockAddrOutPosition, ulong sockAddrOutSize) = context.Request.GetBufferType0x22(1);
|
2018-08-24 13:20:42 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
WritableRegion receiveRegion = context.Memory.GetWritableRegion(receivePosition, (int)receiveLength);
|
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
2022-01-12 13:31:08 -05:00
|
|
|
|
ISocket socket = _context.RetrieveSocket(socketFd);
|
2018-12-06 06:16:24 -05:00
|
|
|
|
int result = -1;
|
2018-03-11 21:05:39 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
if (socket != null)
|
2018-03-11 21:05:39 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
errno = socket.ReceiveFrom(out result, receiveRegion.Memory.Span, receiveRegion.Memory.Span.Length, socketFlags, out IPEndPoint endPoint);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
if (errno == LinuxError.SUCCESS)
|
2018-03-11 21:05:39 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
SetResultErrno(socket, result);
|
2018-03-11 21:05:39 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
receiveRegion.Dispose();
|
|
|
|
|
|
|
|
|
|
context.Memory.Write(sockAddrOutPosition, BsdSockAddr.FromIPEndPoint(endPoint));
|
2018-03-11 21:05:39 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
return WriteBsdResult(context, result, errno);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
2018-03-11 21:05:39 -04:00
|
|
|
|
|
2021-04-13 18:01:24 -04:00
|
|
|
|
[CommandHipc(10)]
|
2018-10-20 18:08:58 -04:00
|
|
|
|
// Send(u32 socket, u32 flags, buffer<i8, 0x21, 0>) -> (i32 ret, u32 bsd_errno)
|
2019-07-14 15:04:38 -04:00
|
|
|
|
public ResultCode Send(ServiceCtx context)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2021-12-26 09:17:13 -05:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
|
|
|
|
BsdSocketFlags socketFlags = (BsdSocketFlags)context.RequestData.ReadInt32();
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2021-04-24 06:16:01 -04:00
|
|
|
|
(ulong sendPosition, ulong sendSize) = context.Request.GetBufferType0x21();
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
ReadOnlySpan<byte> sendBuffer = context.Memory.GetSpan(sendPosition, (int)sendSize);
|
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
2022-01-12 13:31:08 -05:00
|
|
|
|
ISocket socket = _context.RetrieveSocket(socketFd);
|
2018-12-06 06:16:24 -05:00
|
|
|
|
int result = -1;
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
if (socket != null)
|
2018-03-11 21:05:39 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
errno = socket.Send(out result, sendBuffer, socketFlags);
|
2020-05-03 18:54:50 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
if (errno == LinuxError.SUCCESS)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
SetResultErrno(socket, result);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
2018-03-11 21:05:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
return WriteBsdResult(context, result, errno);
|
2018-03-11 21:05:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 18:01:24 -04:00
|
|
|
|
[CommandHipc(11)]
|
2018-10-20 18:08:58 -04: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 15:04:38 -04:00
|
|
|
|
public ResultCode SendTo(ServiceCtx context)
|
2018-03-11 21:05:39 -04:00
|
|
|
|
{
|
2021-12-26 09:17:13 -05:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
|
|
|
|
BsdSocketFlags socketFlags = (BsdSocketFlags)context.RequestData.ReadInt32();
|
2018-04-06 00:01:52 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
(ulong sendPosition, ulong sendSize) = context.Request.GetBufferType0x21(0);
|
2021-04-24 06:16:01 -04:00
|
|
|
|
(ulong bufferPosition, ulong bufferSize) = context.Request.GetBufferType0x21(1);
|
2018-03-11 21:05:39 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
ReadOnlySpan<byte> sendBuffer = context.Memory.GetSpan(sendPosition, (int)sendSize);
|
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
2022-01-12 13:31:08 -05:00
|
|
|
|
ISocket socket = _context.RetrieveSocket(socketFd);
|
2018-12-06 06:16:24 -05:00
|
|
|
|
int result = -1;
|
2018-03-11 21:05:39 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
if (socket != null)
|
2018-03-11 21:05:39 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
IPEndPoint endPoint = context.Memory.Read<BsdSockAddr>(bufferPosition).ToIPEndPoint();
|
2020-05-03 18:54:50 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
errno = socket.SendTo(out result, sendBuffer, sendBuffer.Length, socketFlags, endPoint);
|
2020-05-03 18:54:50 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
if (errno == LinuxError.SUCCESS)
|
2018-03-11 21:05:39 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
SetResultErrno(socket, result);
|
2018-03-11 21:05:39 -04:00
|
|
|
|
}
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
return WriteBsdResult(context, result, errno);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 18:01:24 -04:00
|
|
|
|
[CommandHipc(12)]
|
2018-10-20 18:08:58 -04:00
|
|
|
|
// Accept(u32 socket) -> (i32 ret, u32 bsd_errno, u32 addrlen, buffer<nn::socket::sockaddr_in, 0x22, 0x10> addr)
|
2019-07-14 15:04:38 -04:00
|
|
|
|
public ResultCode Accept(ServiceCtx context)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2021-04-24 06:16:01 -04:00
|
|
|
|
(ulong bufferPos, ulong bufferSize) = context.Request.GetBufferType0x22();
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
2022-01-12 13:31:08 -05:00
|
|
|
|
ISocket socket = _context.RetrieveSocket(socketFd);
|
2018-03-11 21:05:39 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
if (socket != null)
|
2018-03-11 21:05:39 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
errno = socket.Accept(out ISocket newSocket);
|
2018-03-11 21:05:39 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
if (newSocket == null && errno == LinuxError.SUCCESS)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
errno = LinuxError.EWOULDBLOCK;
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
2018-12-06 06:16:24 -05:00
|
|
|
|
else if (errno == LinuxError.SUCCESS)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
int newSockFd = _context.RegisterFileDescriptor(newSocket);
|
2018-04-06 00:01:52 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
if (newSockFd == -1)
|
|
|
|
|
{
|
|
|
|
|
errno = LinuxError.EBADF;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
WriteSockAddr(context, bufferPos, newSocket, true);
|
|
|
|
|
}
|
2018-04-06 00:01:52 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
WriteBsdResult(context, newSockFd, errno);
|
2018-04-06 00:06:34 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
context.ResponseData.Write(0x10);
|
2018-03-11 21:05:39 -04:00
|
|
|
|
|
2019-07-14 15:04:38 -04:00
|
|
|
|
return ResultCode.Success;
|
2018-03-11 21:05:39 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
return WriteBsdResult(context, -1, errno);
|
2018-03-11 21:05:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 18:01:24 -04:00
|
|
|
|
[CommandHipc(13)]
|
2018-10-20 18:08:58 -04:00
|
|
|
|
// Bind(u32 socket, buffer<nn::socket::sockaddr_in, 0x21, 0x10> addr) -> (i32 ret, u32 bsd_errno)
|
2019-07-14 15:04:38 -04:00
|
|
|
|
public ResultCode Bind(ServiceCtx context)
|
2018-03-11 21:05:39 -04:00
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
2018-03-11 21:05:39 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
(ulong bufferPosition, ulong bufferSize) = context.Request.GetBufferType0x21();
|
2018-08-24 13:20:42 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
2022-01-12 13:31:08 -05:00
|
|
|
|
ISocket socket = _context.RetrieveSocket(socketFd);
|
2018-03-11 21:05:39 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
if (socket != null)
|
2018-03-11 21:05:39 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
IPEndPoint endPoint = context.Memory.Read<BsdSockAddr>(bufferPosition).ToIPEndPoint();
|
2018-03-11 21:05:39 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
errno = socket.Bind(endPoint);
|
2018-03-11 21:05:39 -04:00
|
|
|
|
}
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
return WriteBsdResult(context, 0, errno);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 18:01:24 -04:00
|
|
|
|
[CommandHipc(14)]
|
2018-10-20 18:08:58 -04:00
|
|
|
|
// Connect(u32 socket, buffer<nn::socket::sockaddr_in, 0x21, 0x10>) -> (i32 ret, u32 bsd_errno)
|
2019-07-14 15:04:38 -04:00
|
|
|
|
public ResultCode Connect(ServiceCtx context)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
(ulong bufferPosition, ulong bufferSize) = context.Request.GetBufferType0x21();
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
2022-01-12 13:31:08 -05:00
|
|
|
|
ISocket socket = _context.RetrieveSocket(socketFd);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
if (socket != null)
|
2018-03-11 21:05:39 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
IPEndPoint endPoint = context.Memory.Read<BsdSockAddr>(bufferPosition).ToIPEndPoint();
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
errno = socket.Connect(endPoint);
|
2018-03-11 21:05:39 -04:00
|
|
|
|
}
|
2018-02-28 15:58:04 -05:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
return WriteBsdResult(context, 0, errno);
|
2018-02-28 15:58:04 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 18:01:24 -04:00
|
|
|
|
[CommandHipc(15)]
|
2018-10-20 18:08:58 -04:00
|
|
|
|
// GetPeerName(u32 socket) -> (i32 ret, u32 bsd_errno, u32 addrlen, buffer<nn::socket::sockaddr_in, 0x22, 0x10> addr)
|
2019-07-14 15:04:38 -04:00
|
|
|
|
public ResultCode GetPeerName(ServiceCtx context)
|
2018-02-28 15:58:04 -05:00
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
2018-02-28 15:58:04 -05:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
(ulong bufferPosition, ulong bufferSize) = context.Request.GetBufferType0x22();
|
2018-08-24 13:20:42 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
|
|
|
|
ISocket socket = _context.RetrieveSocket(socketFd);
|
2018-12-06 06:16:24 -05:00
|
|
|
|
if (socket != null)
|
2018-03-11 21:05:39 -04:00
|
|
|
|
{
|
2022-09-01 18:04:01 -04:00
|
|
|
|
errno = LinuxError.ENOTCONN;
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2022-09-01 18:04:01 -04:00
|
|
|
|
if (socket.RemoteEndPoint != null)
|
|
|
|
|
{
|
|
|
|
|
errno = LinuxError.SUCCESS;
|
|
|
|
|
|
|
|
|
|
WriteSockAddr(context, bufferPosition, socket, true);
|
|
|
|
|
WriteBsdResult(context, 0, errno);
|
|
|
|
|
context.ResponseData.Write(Unsafe.SizeOf<BsdSockAddr>());
|
|
|
|
|
}
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
return WriteBsdResult(context, 0, errno);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 18:01:24 -04:00
|
|
|
|
[CommandHipc(16)]
|
2018-10-20 18:08:58 -04:00
|
|
|
|
// GetSockName(u32 socket) -> (i32 ret, u32 bsd_errno, u32 addrlen, buffer<nn::socket::sockaddr_in, 0x22, 0x10> addr)
|
2019-07-14 15:04:38 -04:00
|
|
|
|
public ResultCode GetSockName(ServiceCtx context)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2021-04-24 06:16:01 -04:00
|
|
|
|
(ulong bufferPos, ulong bufferSize) = context.Request.GetBufferType0x22();
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
2022-01-12 13:31:08 -05:00
|
|
|
|
ISocket socket = _context.RetrieveSocket(socketFd);
|
2018-03-11 21:05:39 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
if (socket != null)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
errno = LinuxError.SUCCESS;
|
2018-03-11 21:05:39 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
WriteSockAddr(context, bufferPos, socket, false);
|
|
|
|
|
WriteBsdResult(context, 0, errno);
|
2022-01-12 13:31:08 -05:00
|
|
|
|
context.ResponseData.Write(Unsafe.SizeOf<BsdSockAddr>());
|
2018-03-11 21:05:39 -04:00
|
|
|
|
}
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
return WriteBsdResult(context, 0, errno);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 18:01:24 -04:00
|
|
|
|
[CommandHipc(17)]
|
2018-10-20 18:08:58 -04:00
|
|
|
|
// GetSockOpt(u32 socket, u32 level, u32 option_name) -> (i32 ret, u32 bsd_errno, u32, buffer<unknown, 0x22, 0>)
|
2019-07-14 15:04:38 -04:00
|
|
|
|
public ResultCode GetSockOpt(ServiceCtx context)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2021-12-29 09:04:38 -05:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
|
|
|
|
SocketOptionLevel level = (SocketOptionLevel)context.RequestData.ReadInt32();
|
|
|
|
|
BsdSocketOption option = (BsdSocketOption)context.RequestData.ReadInt32();
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2021-04-24 06:16:01 -04:00
|
|
|
|
(ulong bufferPosition, ulong bufferSize) = context.Request.GetBufferType0x22();
|
2022-01-12 13:31:08 -05:00
|
|
|
|
WritableRegion optionValue = context.Memory.GetWritableRegion(bufferPosition, (int)bufferSize);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
2022-01-12 13:31:08 -05:00
|
|
|
|
ISocket socket = _context.RetrieveSocket(socketFd);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
if (socket != null)
|
2018-03-11 21:05:39 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
errno = socket.GetSocketOption(option, level, optionValue.Memory.Span);
|
|
|
|
|
|
|
|
|
|
if (errno == LinuxError.SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
optionValue.Dispose();
|
|
|
|
|
}
|
2018-03-11 21:05:39 -04:00
|
|
|
|
}
|
2018-02-28 15:58:04 -05:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
return WriteBsdResult(context, 0, errno);
|
2018-02-28 15:58:04 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 18:01:24 -04:00
|
|
|
|
[CommandHipc(18)]
|
2018-10-20 18:08:58 -04:00
|
|
|
|
// Listen(u32 socket, u32 backlog) -> (i32 ret, u32 bsd_errno)
|
2019-07-14 15:04:38 -04:00
|
|
|
|
public ResultCode Listen(ServiceCtx context)
|
2018-02-28 15:58:04 -05:00
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
|
|
|
|
int backlog = context.RequestData.ReadInt32();
|
2018-02-28 15:58:04 -05:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
2022-01-12 13:31:08 -05:00
|
|
|
|
ISocket socket = _context.RetrieveSocket(socketFd);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
if (socket != null)
|
2018-03-11 21:05:39 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
errno = socket.Listen(backlog);
|
2018-03-11 21:05:39 -04:00
|
|
|
|
}
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
return WriteBsdResult(context, 0, errno);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 18:01:24 -04:00
|
|
|
|
[CommandHipc(19)]
|
2018-10-20 18:08:58 -04: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 15:04:38 -04:00
|
|
|
|
public ResultCode Ioctl(ServiceCtx context)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
|
|
|
|
BsdIoctl cmd = (BsdIoctl)context.RequestData.ReadInt32();
|
|
|
|
|
int bufferCount = context.RequestData.ReadInt32();
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
2022-01-12 13:31:08 -05:00
|
|
|
|
ISocket socket = _context.RetrieveSocket(socketFd);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
if (socket != null)
|
2018-03-11 21:05:39 -04:00
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
switch (cmd)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
|
|
|
|
case BsdIoctl.AtMark:
|
2018-12-06 06:16:24 -05:00
|
|
|
|
errno = LinuxError.SUCCESS;
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2021-04-24 06:16:01 -04:00
|
|
|
|
(ulong bufferPosition, ulong bufferSize) = context.Request.GetBufferType0x22();
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
|
|
|
|
// FIXME: OOB not implemented.
|
2021-04-24 06:16:01 -04:00
|
|
|
|
context.Memory.Write(bufferPosition, 0);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
2018-12-06 06:16:24 -05:00
|
|
|
|
errno = LinuxError.EOPNOTSUPP;
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2020-08-03 19:32:53 -04:00
|
|
|
|
Logger.Warning?.Print(LogClass.ServiceBsd, $"Unsupported Ioctl Cmd: {cmd}");
|
2018-10-20 18:08:58 -04:00
|
|
|
|
break;
|
|
|
|
|
}
|
2018-03-11 21:05:39 -04:00
|
|
|
|
}
|
2018-02-28 15:58:04 -05:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
return WriteBsdResult(context, 0, errno);
|
2018-02-28 15:58:04 -05:00
|
|
|
|
}
|
2018-03-11 21:05:39 -04:00
|
|
|
|
|
2021-04-13 18:01:24 -04:00
|
|
|
|
[CommandHipc(20)]
|
2018-10-20 18:08:58 -04:00
|
|
|
|
// Fcntl(u32 socket, u32 cmd, u32 arg) -> (i32 ret, u32 bsd_errno)
|
2019-07-14 15:04:38 -04:00
|
|
|
|
public ResultCode Fcntl(ServiceCtx context)
|
2018-03-11 21:05:39 -04:00
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
|
|
|
|
int cmd = context.RequestData.ReadInt32();
|
|
|
|
|
int arg = context.RequestData.ReadInt32();
|
2018-03-11 21:05:39 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
int result = 0;
|
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
2022-01-12 13:31:08 -05:00
|
|
|
|
ISocket socket = _context.RetrieveSocket(socketFd);
|
2018-04-06 00:01:52 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
if (socket != null)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
errno = LinuxError.SUCCESS;
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
if (cmd == 0x3)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
result = !socket.Blocking ? 0x800 : 0;
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
2018-12-06 06:16:24 -05:00
|
|
|
|
else if (cmd == 0x4 && arg == 0x800)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
socket.Blocking = false;
|
2018-12-06 06:16:24 -05:00
|
|
|
|
result = 0;
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
errno = LinuxError.EOPNOTSUPP;
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-11 21:05:39 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
return WriteBsdResult(context, result, errno);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
2018-04-06 00:01:52 -04:00
|
|
|
|
|
2021-04-13 18:01:24 -04:00
|
|
|
|
[CommandHipc(21)]
|
2018-10-20 18:08:58 -04:00
|
|
|
|
// SetSockOpt(u32 socket, u32 level, u32 option_name, buffer<unknown, 0x21, 0> option_value) -> (i32 ret, u32 bsd_errno)
|
2019-07-14 15:04:38 -04:00
|
|
|
|
public ResultCode SetSockOpt(ServiceCtx context)
|
2018-08-24 13:20:42 -04:00
|
|
|
|
{
|
2021-12-29 09:04:38 -05:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
|
|
|
|
SocketOptionLevel level = (SocketOptionLevel)context.RequestData.ReadInt32();
|
|
|
|
|
BsdSocketOption option = (BsdSocketOption)context.RequestData.ReadInt32();
|
2018-08-24 13:20:42 -04:00
|
|
|
|
|
2021-04-24 06:16:01 -04:00
|
|
|
|
(ulong bufferPos, ulong bufferSize) = context.Request.GetBufferType0x21();
|
2018-08-24 13:20:42 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
ReadOnlySpan<byte> optionValue = context.Memory.GetSpan(bufferPos, (int)bufferSize);
|
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
2022-01-12 13:31:08 -05:00
|
|
|
|
ISocket socket = _context.RetrieveSocket(socketFd);
|
2018-08-24 13:20:42 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
if (socket != null)
|
2018-08-24 13:20:42 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
errno = socket.SetSocketOption(option, level, optionValue);
|
2018-08-24 13:20:42 -04:00
|
|
|
|
}
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
return WriteBsdResult(context, 0, errno);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 18:01:24 -04:00
|
|
|
|
[CommandHipc(22)]
|
2018-10-20 18:08:58 -04:00
|
|
|
|
// Shutdown(u32 socket, u32 how) -> (i32 ret, u32 bsd_errno)
|
2019-07-14 15:04:38 -04:00
|
|
|
|
public ResultCode Shutdown(ServiceCtx context)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
|
|
|
|
int how = context.RequestData.ReadInt32();
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
2022-01-12 13:31:08 -05:00
|
|
|
|
ISocket socket = _context.RetrieveSocket(socketFd);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
if (socket != null)
|
2018-08-24 13:20:42 -04:00
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
errno = LinuxError.EINVAL;
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
if (how >= 0 && how <= 2)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
errno = socket.Shutdown((BsdSocketShutdownFlags)how);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
2018-08-24 13:20:42 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
return WriteBsdResult(context, 0, errno);
|
2018-08-24 13:20:42 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 18:01:24 -04:00
|
|
|
|
[CommandHipc(23)]
|
2018-10-20 18:08:58 -04:00
|
|
|
|
// ShutdownAllSockets(u32 how) -> (i32 ret, u32 bsd_errno)
|
2019-07-14 15:04:38 -04:00
|
|
|
|
public ResultCode ShutdownAllSockets(ServiceCtx context)
|
2018-03-11 21:05:39 -04:00
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
int how = context.RequestData.ReadInt32();
|
2018-03-11 21:05:39 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
LinuxError errno = LinuxError.EINVAL;
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
if (how >= 0 && how <= 2)
|
2018-03-11 21:05:39 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
errno = _context.ShutdownAllSockets((BsdSocketShutdownFlags)how);
|
2018-03-11 21:05:39 -04:00
|
|
|
|
}
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
return WriteBsdResult(context, 0, errno);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 18:01:24 -04:00
|
|
|
|
[CommandHipc(24)]
|
2022-01-12 13:31:08 -05:00
|
|
|
|
// Write(u32 fd, buffer<i8, 0x21, 0> message) -> (i32 ret, u32 bsd_errno)
|
2019-07-14 15:04:38 -04:00
|
|
|
|
public ResultCode Write(ServiceCtx context)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
int fd = context.RequestData.ReadInt32();
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2021-04-24 06:16:01 -04:00
|
|
|
|
(ulong sendPosition, ulong sendSize) = context.Request.GetBufferType0x21();
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
ReadOnlySpan<byte> sendBuffer = context.Memory.GetSpan(sendPosition, (int)sendSize);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
|
|
|
|
IFileDescriptor file = _context.RetrieveFileDescriptor(fd);
|
|
|
|
|
int result = -1;
|
2020-05-03 18:54:50 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
if (file != null)
|
|
|
|
|
{
|
|
|
|
|
errno = file.Write(out result, sendBuffer);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
if (errno == LinuxError.SUCCESS)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
SetResultErrno(file, result);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
2018-03-11 21:05:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
return WriteBsdResult(context, result, errno);
|
2018-03-11 21:05:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 18:01:24 -04:00
|
|
|
|
[CommandHipc(25)]
|
2022-01-12 13:31:08 -05:00
|
|
|
|
// Read(u32 fd) -> (i32 ret, u32 bsd_errno, buffer<i8, 0x22, 0> message)
|
2019-07-14 15:04:38 -04:00
|
|
|
|
public ResultCode Read(ServiceCtx context)
|
2018-03-11 21:05:39 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
int fd = context.RequestData.ReadInt32();
|
2018-03-11 21:05:39 -04:00
|
|
|
|
|
2021-04-24 06:16:01 -04:00
|
|
|
|
(ulong receivePosition, ulong receiveLength) = context.Request.GetBufferType0x22();
|
2018-04-06 00:01:52 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
WritableRegion receiveRegion = context.Memory.GetWritableRegion(receivePosition, (int)receiveLength);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
|
|
|
|
IFileDescriptor file = _context.RetrieveFileDescriptor(fd);
|
|
|
|
|
int result = -1;
|
|
|
|
|
|
|
|
|
|
if (file != null)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
errno = file.Read(out result, receiveRegion.Memory.Span);
|
2018-03-11 21:05:39 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
if (errno == LinuxError.SUCCESS)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
SetResultErrno(file, result);
|
|
|
|
|
|
|
|
|
|
receiveRegion.Dispose();
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
2018-03-11 21:05:39 -04:00
|
|
|
|
}
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
return WriteBsdResult(context, result, errno);
|
2018-03-11 21:05:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 18:01:24 -04:00
|
|
|
|
[CommandHipc(26)]
|
2022-01-12 13:31:08 -05:00
|
|
|
|
// Close(u32 fd) -> (i32 ret, u32 bsd_errno)
|
2019-07-14 15:04:38 -04:00
|
|
|
|
public ResultCode Close(ServiceCtx context)
|
2018-03-11 21:05:39 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
int fd = context.RequestData.ReadInt32();
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
if (_context.CloseFileDescriptor(fd))
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
errno = LinuxError.SUCCESS;
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
return WriteBsdResult(context, 0, errno);
|
2018-03-11 21:05:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 18:01:24 -04:00
|
|
|
|
[CommandHipc(27)]
|
2022-01-12 13:31:08 -05:00
|
|
|
|
// DuplicateSocket(u32 fd, u64 reserved) -> (i32 ret, u32 bsd_errno)
|
2019-07-14 15:04:38 -04:00
|
|
|
|
public ResultCode DuplicateSocket(ServiceCtx context)
|
2018-03-11 21:05:39 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
int fd = context.RequestData.ReadInt32();
|
2018-12-06 06:16:24 -05:00
|
|
|
|
ulong reserved = context.RequestData.ReadUInt64();
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
LinuxError errno = LinuxError.ENOENT;
|
|
|
|
|
int newSockFd = -1;
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
if (_isPrivileged)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
errno = LinuxError.SUCCESS;
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
newSockFd = _context.DuplicateFileDescriptor(fd);
|
2018-10-20 18:08:58 -04:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
if (newSockFd == -1)
|
2018-10-20 18:08:58 -04:00
|
|
|
|
{
|
2022-01-12 13:31:08 -05:00
|
|
|
|
errno = LinuxError.EBADF;
|
2018-10-20 18:08:58 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
return WriteBsdResult(context, newSockFd, errno);
|
2018-03-11 21:05:39 -04:00
|
|
|
|
}
|
2022-01-12 13:31:08 -05:00
|
|
|
|
|
2022-09-07 16:37:15 -04:00
|
|
|
|
|
|
|
|
|
[CommandHipc(29)] // 7.0.0+
|
|
|
|
|
// RecvMMsg(u32 fd, u32 vlen, u32 flags, u32 reserved, nn::socket::TimeVal timeout) -> (i32 ret, u32 bsd_errno, buffer<bytes, 6> message);
|
|
|
|
|
public ResultCode RecvMMsg(ServiceCtx context)
|
|
|
|
|
{
|
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
|
|
|
|
int vlen = context.RequestData.ReadInt32();
|
|
|
|
|
BsdSocketFlags socketFlags = (BsdSocketFlags)context.RequestData.ReadInt32();
|
|
|
|
|
uint reserved = context.RequestData.ReadUInt32();
|
|
|
|
|
TimeVal timeout = context.RequestData.ReadStruct<TimeVal>();
|
|
|
|
|
|
|
|
|
|
ulong receivePosition = context.Request.ReceiveBuff[0].Position;
|
|
|
|
|
ulong receiveLength = context.Request.ReceiveBuff[0].Size;
|
|
|
|
|
|
|
|
|
|
WritableRegion receiveRegion = context.Memory.GetWritableRegion(receivePosition, (int)receiveLength);
|
|
|
|
|
|
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
|
|
|
|
ISocket socket = _context.RetrieveSocket(socketFd);
|
|
|
|
|
int result = -1;
|
|
|
|
|
|
|
|
|
|
if (socket != null)
|
|
|
|
|
{
|
|
|
|
|
errno = BsdMMsgHdr.Deserialize(out BsdMMsgHdr message, receiveRegion.Memory.Span, vlen);
|
|
|
|
|
|
|
|
|
|
if (errno == LinuxError.SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
errno = socket.RecvMMsg(out result, message, socketFlags, timeout);
|
|
|
|
|
|
|
|
|
|
if (errno == LinuxError.SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
errno = BsdMMsgHdr.Serialize(receiveRegion.Memory.Span, message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (errno == LinuxError.SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
SetResultErrno(socket, result);
|
|
|
|
|
receiveRegion.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return WriteBsdResult(context, result, errno);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[CommandHipc(30)] // 7.0.0+
|
|
|
|
|
// SendMMsg(u32 fd, u32 vlen, u32 flags) -> (i32 ret, u32 bsd_errno, buffer<bytes, 6> message);
|
|
|
|
|
public ResultCode SendMMsg(ServiceCtx context)
|
|
|
|
|
{
|
|
|
|
|
int socketFd = context.RequestData.ReadInt32();
|
|
|
|
|
int vlen = context.RequestData.ReadInt32();
|
|
|
|
|
BsdSocketFlags socketFlags = (BsdSocketFlags)context.RequestData.ReadInt32();
|
|
|
|
|
|
|
|
|
|
ulong receivePosition = context.Request.ReceiveBuff[0].Position;
|
|
|
|
|
ulong receiveLength = context.Request.ReceiveBuff[0].Size;
|
|
|
|
|
|
|
|
|
|
WritableRegion receiveRegion = context.Memory.GetWritableRegion(receivePosition, (int)receiveLength);
|
|
|
|
|
|
|
|
|
|
LinuxError errno = LinuxError.EBADF;
|
|
|
|
|
ISocket socket = _context.RetrieveSocket(socketFd);
|
|
|
|
|
int result = -1;
|
|
|
|
|
|
|
|
|
|
if (socket != null)
|
|
|
|
|
{
|
|
|
|
|
errno = BsdMMsgHdr.Deserialize(out BsdMMsgHdr message, receiveRegion.Memory.Span, vlen);
|
|
|
|
|
|
|
|
|
|
if (errno == LinuxError.SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
errno = socket.SendMMsg(out result, message, socketFlags);
|
|
|
|
|
|
|
|
|
|
if (errno == LinuxError.SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
errno = BsdMMsgHdr.Serialize(receiveRegion.Memory.Span, message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (errno == LinuxError.SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
SetResultErrno(socket, result);
|
|
|
|
|
receiveRegion.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return WriteBsdResult(context, result, errno);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
[CommandHipc(31)] // 7.0.0+
|
|
|
|
|
// EventFd(u64 initval, nn::socket::EventFdFlags flags) -> (i32 ret, u32 bsd_errno)
|
|
|
|
|
public ResultCode EventFd(ServiceCtx context)
|
|
|
|
|
{
|
|
|
|
|
ulong initialValue = context.RequestData.ReadUInt64();
|
|
|
|
|
EventFdFlags flags = (EventFdFlags)context.RequestData.ReadUInt32();
|
|
|
|
|
|
|
|
|
|
EventFileDescriptor newEventFile = new EventFileDescriptor(initialValue, flags);
|
|
|
|
|
|
|
|
|
|
LinuxError errno = LinuxError.SUCCESS;
|
|
|
|
|
|
|
|
|
|
int newSockFd = _context.RegisterFileDescriptor(newEventFile);
|
|
|
|
|
|
|
|
|
|
if (newSockFd == -1)
|
|
|
|
|
{
|
|
|
|
|
errno = LinuxError.EBADF;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return WriteBsdResult(context, newSockFd, errno);
|
|
|
|
|
}
|
2018-02-27 22:31:52 -05:00
|
|
|
|
}
|
2022-09-01 18:04:01 -04:00
|
|
|
|
}
|