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