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