Exemplo n.º 1
0
        // Write(u32 socket, buffer<i8, 0x21, 0> message) -> (i32 ret, u32 bsd_errno)
        public ResultCode Write(ServiceCtx context)
        {
            int socketFd = context.RequestData.ReadInt32();

            (long sendPosition, long sendSize) = context.Request.GetBufferType0x21();

            LinuxError errno  = LinuxError.EBADF;
            BsdSocket  socket = RetrieveSocket(socketFd);
            int        result = -1;

            if (socket != null)
            {
                byte[] sendBuffer = context.Memory.ReadBytes(sendPosition, sendSize);

                try
                {
                    result = socket.Handle.Send(sendBuffer);
                    errno  = SetResultErrno(socket.Handle, result);
                }
                catch (SocketException exception)
                {
                    errno = ConvertError((WsaError)exception.ErrorCode);
                }
            }

            return(WriteBsdResult(context, result, errno));
        }
Exemplo n.º 2
0
        // Read(u32 socket) -> (i32 ret, u32 bsd_errno, buffer<i8, 0x22, 0> message)
        public ResultCode Read(ServiceCtx context)
        {
            int socketFd = context.RequestData.ReadInt32();

            (long receivePosition, long receiveLength) = context.Request.GetBufferType0x22();

            LinuxError errno  = LinuxError.EBADF;
            BsdSocket  socket = RetrieveSocket(socketFd);
            int        result = -1;

            if (socket != null)
            {
                byte[] receivedBuffer = new byte[receiveLength];

                try
                {
                    result = socket.Handle.Receive(receivedBuffer);
                    errno  = SetResultErrno(socket.Handle, result);
                }
                catch (SocketException exception)
                {
                    errno = ConvertError((WsaError)exception.ErrorCode);
                }
            }

            return(WriteBsdResult(context, result, errno));
        }
Exemplo n.º 3
0
        // 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;
            BsdSocket  socket = RetrieveSocket(socketFd);

            if (socket != null)
            {
                errno = LinuxError.EINVAL;

                if (how >= 0 && how <= 2)
                {
                    errno = LinuxError.SUCCESS;

                    try
                    {
                        socket.Handle.Shutdown((SocketShutdown)how);
                    }
                    catch (SocketException exception)
                    {
                        errno = ConvertError((WsaError)exception.ErrorCode);
                    }
                }
            }

            return(WriteBsdResult(context, 0, errno));
        }
Exemplo n.º 4
0
        // 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;
            BsdSocket  socket = RetrieveSocket(socketFd);

            if (socket != null)
            {
                errno = LinuxError.SUCCESS;

                if (cmd == 0x3)
                {
                    result = !socket.Handle.Blocking ? 0x800 : 0;
                }
                else if (cmd == 0x4 && arg == 0x800)
                {
                    socket.Handle.Blocking = false;
                    result = 0;
                }
                else
                {
                    errno = LinuxError.EOPNOTSUPP;
                }
            }

            return(WriteBsdResult(context, result, errno));
        }
Exemplo n.º 5
0
        // 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();
            int level      = context.RequestData.ReadInt32();
            int optionName = context.RequestData.ReadInt32();

            (long bufferPos, long bufferSize) = context.Request.GetBufferType0x21();

            LinuxError errno  = LinuxError.EBADF;
            BsdSocket  socket = RetrieveSocket(socketFd);

            if (socket != null)
            {
                errno = LinuxError.ENOPROTOOPT;

                if (level == 0xFFFF)
                {
                    errno = HandleSetSocketOption(context, socket, (SocketOptionName)optionName, bufferPos, bufferSize);
                }
                else
                {
                    Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported SetSockOpt Level: {(SocketOptionLevel)level}");
                }
            }

            return(WriteBsdResult(context, 0, errno));
        }
Exemplo n.º 6
0
        // 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();

            (long bufferPos, long bufferSize) = context.Request.GetBufferType0x21();

            LinuxError errno  = LinuxError.EBADF;
            BsdSocket  socket = RetrieveSocket(socketFd);

            if (socket != null)
            {
                errno = LinuxError.SUCCESS;
                try
                {
                    IPEndPoint endPoint = ParseSockAddr(context, bufferPos, bufferSize);

                    socket.Handle.Connect(endPoint);
                }
                catch (SocketException exception)
                {
                    errno = ConvertError((WsaError)exception.ErrorCode);
                }
            }

            return(WriteBsdResult(context, 0, errno));
        }
Exemplo n.º 7
0
        // 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;
            BsdSocket  socket = RetrieveSocket(socketFd);

            if (socket != null)
            {
                switch (cmd)
                {
                case BsdIoctl.AtMark:
                    errno = LinuxError.SUCCESS;

                    (long bufferPosition, long bufferSize) = context.Request.GetBufferType0x22();

                    // FIXME: OOB not implemented.
                    context.Memory.WriteInt32(bufferPosition, 0);
                    break;

                default:
                    errno = LinuxError.EOPNOTSUPP;

                    Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported Ioctl Cmd: {cmd}");
                    break;
                }
            }

            return(WriteBsdResult(context, 0, errno));
        }
Exemplo n.º 8
0
        private ResultCode SocketInternal(ServiceCtx context, bool exempt)
        {
            AddressFamily domain   = (AddressFamily)context.RequestData.ReadInt32();
            SocketType    type     = (SocketType)context.RequestData.ReadInt32();
            ProtocolType  protocol = (ProtocolType)context.RequestData.ReadInt32();

            if (domain == AddressFamily.Unknown)
            {
                return(WriteBsdResult(context, -1, LinuxError.EPROTONOSUPPORT));
            }
            else if ((type == SocketType.Seqpacket || type == SocketType.Raw) && !_isPrivileged)
            {
                if (domain != AddressFamily.InterNetwork || type != SocketType.Raw || protocol != ProtocolType.Icmp)
                {
                    return(WriteBsdResult(context, -1, LinuxError.ENOENT));
                }
            }

            BsdSocket newBsdSocket = new BsdSocket
            {
                Family   = (int)domain,
                Type     = (int)type,
                Protocol = (int)protocol,
                Handle   = new Socket(domain, type, protocol)
            };

            _sockets.Add(newBsdSocket);

            if (exempt)
            {
                newBsdSocket.Handle.Disconnect(true);
            }

            return(WriteBsdResult(context, _sockets.Count - 1));
        }
Exemplo n.º 9
0
        // 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();

            (long bufferPos, long bufferSize) = context.Request.GetBufferType0x22();

            LinuxError errno  = LinuxError.EBADF;
            BsdSocket  socket = RetrieveSocket(socketFd);

            if (socket != null)
            {
                errno = LinuxError.SUCCESS;

                Socket newSocket = null;

                try
                {
                    newSocket = socket.Handle.Accept();
                }
                catch (SocketException exception)
                {
                    errno = ConvertError((WsaError)exception.ErrorCode);
                }

                if (newSocket == null && errno == LinuxError.SUCCESS)
                {
                    errno = LinuxError.EWOULDBLOCK;
                }
                else if (errno == LinuxError.SUCCESS)
                {
                    BsdSocket newBsdSocket = new BsdSocket
                    {
                        Family   = (int)newSocket.AddressFamily,
                        Type     = (int)newSocket.SocketType,
                        Protocol = (int)newSocket.ProtocolType,
                        Handle   = newSocket
                    };

                    _sockets.Add(newBsdSocket);

                    WriteSockAddr(context, bufferPos, newBsdSocket, true);

                    WriteBsdResult(context, _sockets.Count - 1, errno);

                    context.ResponseData.Write(0x10);

                    return(ResultCode.Success);
                }
            }

            return(WriteBsdResult(context, -1, errno));
        }
Exemplo n.º 10
0
        // Close(u32 socket) -> (i32 ret, u32 bsd_errno)
        public ResultCode Close(ServiceCtx context)
        {
            int socketFd = context.RequestData.ReadInt32();

            LinuxError errno  = LinuxError.EBADF;
            BsdSocket  socket = RetrieveSocket(socketFd);

            if (socket != null)
            {
                socket.Handle.Close();

                _sockets[socketFd] = null;

                errno = LinuxError.SUCCESS;
            }

            return(WriteBsdResult(context, 0, errno));
        }
Exemplo n.º 11
0
        private LinuxError HandleSetSocketOption(ServiceCtx context, BsdSocket socket, SocketOptionName optionName, long optionValuePosition, long optionValueSize)
        {
            try
            {
                switch (optionName)
                {
                case SocketOptionName.Broadcast:
                case SocketOptionName.DontLinger:
                case SocketOptionName.Debug:
                case SocketOptionName.Error:
                case SocketOptionName.KeepAlive:
                case SocketOptionName.OutOfBandInline:
                case SocketOptionName.ReceiveBuffer:
                case SocketOptionName.ReceiveTimeout:
                case SocketOptionName.SendBuffer:
                case SocketOptionName.SendTimeout:
                case SocketOptionName.Type:
                case SocketOptionName.ReuseAddress:
                    socket.Handle.SetSocketOption(SocketOptionLevel.Socket, optionName, context.Memory.ReadInt32(optionValuePosition));

                    return(LinuxError.SUCCESS);

                case (SocketOptionName)0x200:
                    socket.Handle.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, context.Memory.ReadInt32(optionValuePosition));

                    return(LinuxError.SUCCESS);

                case SocketOptionName.Linger:
                    socket.Handle.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger,
                                                  new LingerOption(context.Memory.ReadInt32(optionValuePosition) != 0, context.Memory.ReadInt32(optionValuePosition + 4)));

                    return(LinuxError.SUCCESS);

                default:
                    Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported SetSockOpt OptionName: {optionName}");

                    return(LinuxError.EOPNOTSUPP);
                }
            }
            catch (SocketException exception)
            {
                return(ConvertError((WsaError)exception.ErrorCode));
            }
        }
Exemplo n.º 12
0
        // 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();

            (long bufferPos, long bufferSize) = context.Request.GetBufferType0x22();

            LinuxError errno  = LinuxError.EBADF;
            BsdSocket  socket = RetrieveSocket(socketFd);

            if (socket != null)
            {
                errno = LinuxError.SUCCESS;

                WriteSockAddr(context, bufferPos, socket, false);
                WriteBsdResult(context, 0, errno);
                context.ResponseData.Write(0x10);
            }

            return(WriteBsdResult(context, 0, errno));
        }
Exemplo n.º 13
0
        // 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();
            SocketFlags socketFlags = (SocketFlags)context.RequestData.ReadInt32();

            (long receivePosition, long receiveLength)       = context.Request.GetBufferType0x22();
            (long sockAddrOutPosition, long sockAddrOutSize) = context.Request.GetBufferType0x22(1);

            LinuxError errno  = LinuxError.EBADF;
            BsdSocket  socket = RetrieveSocket(socketFd);
            int        result = -1;

            if (socket != null)
            {
                if (socketFlags != SocketFlags.None && (socketFlags & SocketFlags.OutOfBand) == 0 &&
                    (socketFlags & SocketFlags.Peek) == 0)
                {
                    Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported Recv flags: {socketFlags}");

                    return(WriteBsdResult(context, -1, LinuxError.EOPNOTSUPP));
                }

                byte[]   receivedBuffer = new byte[receiveLength];
                EndPoint endPoint       = new IPEndPoint(IPAddress.Any, 0);

                try
                {
                    result = socket.Handle.ReceiveFrom(receivedBuffer, receivedBuffer.Length, socketFlags, ref endPoint);
                    errno  = SetResultErrno(socket.Handle, result);

                    context.Memory.WriteBytes(receivePosition, receivedBuffer);
                    WriteSockAddr(context, sockAddrOutPosition, (IPEndPoint)endPoint);
                }
                catch (SocketException exception)
                {
                    errno = ConvertError((WsaError)exception.ErrorCode);
                }
            }

            return(WriteBsdResult(context, result, errno));
        }
Exemplo n.º 14
0
        // 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();
            SocketFlags socketFlags = (SocketFlags)context.RequestData.ReadInt32();

            (ulong sendPosition, ulong sendSize)     = context.Request.GetBufferType0x21();
            (ulong bufferPosition, ulong bufferSize) = context.Request.GetBufferType0x21(1);

            LinuxError errno  = LinuxError.EBADF;
            BsdSocket  socket = RetrieveSocket(socketFd);
            int        result = -1;

            if (socket != null)
            {
                if (socketFlags != SocketFlags.None && socketFlags != SocketFlags.OutOfBand &&
                    socketFlags != SocketFlags.Peek && socketFlags != SocketFlags.DontRoute)
                {
                    Logger.Warning?.Print(LogClass.ServiceBsd, $"Unsupported Send flags: {socketFlags}");

                    return(WriteBsdResult(context, -1, LinuxError.EOPNOTSUPP));
                }

                byte[] sendBuffer = new byte[sendSize];

                context.Memory.Read(sendPosition, sendBuffer);

                EndPoint endPoint = ParseSockAddr(context, bufferPosition, bufferSize);

                try
                {
                    result = socket.Handle.SendTo(sendBuffer, sendBuffer.Length, socketFlags, endPoint);
                    errno  = SetResultErrno(socket.Handle, result);
                }
                catch (SocketException exception)
                {
                    errno = ConvertError((WsaError)exception.ErrorCode);
                }
            }

            return(WriteBsdResult(context, result, errno));
        }
Exemplo n.º 15
0
        // DuplicateSocket(u32 socket, u64 reserved) -> (i32 ret, u32 bsd_errno)
        public ResultCode DuplicateSocket(ServiceCtx context)
        {
            int   socketFd = context.RequestData.ReadInt32();
            ulong reserved = context.RequestData.ReadUInt64();

            LinuxError errno     = LinuxError.ENOENT;
            int        newSockFd = -1;

            if (_isPrivileged)
            {
                errno = LinuxError.EBADF;

                BsdSocket oldSocket = RetrieveSocket(socketFd);

                if (oldSocket != null)
                {
                    _sockets.Add(oldSocket);
                    newSockFd = _sockets.Count - 1;
                }
            }

            return(WriteBsdResult(context, newSockFd, errno));
        }
Exemplo n.º 16
0
        // 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;
            BsdSocket  socket = RetrieveSocket(socketFd);

            if (socket != null)
            {
                errno = LinuxError.SUCCESS;

                try
                {
                    socket.Handle.Listen(backlog);
                }
                catch (SocketException exception)
                {
                    errno = ConvertError((WsaError)exception.ErrorCode);
                }
            }

            return(WriteBsdResult(context, 0, errno));
        }
Exemplo n.º 17
0
        private void WriteSockAddr(ServiceCtx context, long bufferPosition, BsdSocket socket, bool isRemote)
        {
            IPEndPoint endPoint = (isRemote ? socket.Handle.RemoteEndPoint : socket.Handle.LocalEndPoint) as IPEndPoint;

            WriteSockAddr(context, bufferPosition, endPoint);
        }
Exemplo n.º 18
0
        // 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();

            (long bufferPosition, long bufferSize) = context.Request.GetBufferType0x21();


            if (timeout < -1 || fdsCount < 0 || (fdsCount * 8) > bufferSize)
            {
                return(WriteBsdResult(context, -1, LinuxError.EINVAL));
            }

            PollEvent[] events = new PollEvent[fdsCount];

            for (int i = 0; i < fdsCount; i++)
            {
                int socketFd = context.Memory.ReadInt32(bufferPosition + i * 8);

                BsdSocket socket = RetrieveSocket(socketFd);

                if (socket == null)
                {
                    return(WriteBsdResult(context, -1, LinuxError.EBADF));
                }

                PollEvent.EventTypeMask inputEvents  = (PollEvent.EventTypeMask)context.Memory.ReadInt16(bufferPosition + i * 8 + 4);
                PollEvent.EventTypeMask outputEvents = (PollEvent.EventTypeMask)context.Memory.ReadInt16(bufferPosition + i * 8 + 6);

                events[i] = new PollEvent(socketFd, socket, inputEvents, outputEvents);
            }

            List <Socket> readEvents  = new List <Socket>();
            List <Socket> writeEvents = new List <Socket>();
            List <Socket> errorEvents = new List <Socket>();

            foreach (PollEvent Event in events)
            {
                bool isValidEvent = false;

                if ((Event.InputEvents & PollEvent.EventTypeMask.Input) != 0)
                {
                    readEvents.Add(Event.Socket.Handle);
                    errorEvents.Add(Event.Socket.Handle);

                    isValidEvent = true;
                }

                if ((Event.InputEvents & PollEvent.EventTypeMask.UrgentInput) != 0)
                {
                    readEvents.Add(Event.Socket.Handle);
                    errorEvents.Add(Event.Socket.Handle);

                    isValidEvent = true;
                }

                if ((Event.InputEvents & PollEvent.EventTypeMask.Output) != 0)
                {
                    writeEvents.Add(Event.Socket.Handle);
                    errorEvents.Add(Event.Socket.Handle);

                    isValidEvent = true;
                }

                if ((Event.InputEvents & PollEvent.EventTypeMask.Error) != 0)
                {
                    errorEvents.Add(Event.Socket.Handle);
                    isValidEvent = true;
                }

                if (!isValidEvent)
                {
                    Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported Poll input event type: {Event.InputEvents}");
                    return(WriteBsdResult(context, -1, LinuxError.EINVAL));
                }
            }

            try
            {
                System.Net.Sockets.Socket.Select(readEvents, writeEvents, errorEvents, timeout);
            }
            catch (SocketException exception)
            {
                return(WriteWinSock2Error(context, (WsaError)exception.ErrorCode));
            }

            for (int i = 0; i < fdsCount; i++)
            {
                PollEvent Event = events[i];
                context.Memory.WriteInt32(bufferPosition + i * 8, Event.SocketFd);
                context.Memory.WriteInt16(bufferPosition + i * 8 + 4, (short)Event.InputEvents);

                PollEvent.EventTypeMask outputEvents = 0;

                Socket socket = Event.Socket.Handle;

                if (errorEvents.Contains(socket))
                {
                    outputEvents |= PollEvent.EventTypeMask.Error;

                    if (!socket.Connected || !socket.IsBound)
                    {
                        outputEvents |= PollEvent.EventTypeMask.Disconnected;
                    }
                }

                if (readEvents.Contains(socket))
                {
                    if ((Event.InputEvents & PollEvent.EventTypeMask.Input) != 0)
                    {
                        outputEvents |= PollEvent.EventTypeMask.Input;
                    }
                }

                if (writeEvents.Contains(socket))
                {
                    outputEvents |= PollEvent.EventTypeMask.Output;
                }

                context.Memory.WriteInt16(bufferPosition + i * 8 + 6, (short)outputEvents);
            }

            return(WriteBsdResult(context, readEvents.Count + writeEvents.Count + errorEvents.Count, LinuxError.SUCCESS));
        }
Exemplo n.º 19
0
        // 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 bufferPosition, ulong bufferSize) = context.Request.GetBufferType0x21();


            if (timeout < -1 || fdsCount < 0 || (ulong)(fdsCount * 8) > bufferSize)
            {
                return(WriteBsdResult(context, -1, LinuxError.EINVAL));
            }

            PollEvent[] events = new PollEvent[fdsCount];

            for (int i = 0; i < fdsCount; i++)
            {
                int socketFd = context.Memory.Read <int>(bufferPosition + (ulong)i * 8);

                BsdSocket socket = RetrieveSocket(socketFd);

                if (socket == null)
                {
                    return(WriteBsdResult(context, -1, LinuxError.EBADF));
                }

                PollEvent.EventTypeMask inputEvents  = (PollEvent.EventTypeMask)context.Memory.Read <short>(bufferPosition + (ulong)i * 8 + 4);
                PollEvent.EventTypeMask outputEvents = (PollEvent.EventTypeMask)context.Memory.Read <short>(bufferPosition + (ulong)i * 8 + 6);

                events[i] = new PollEvent(socketFd, socket, inputEvents, outputEvents);
            }

            List <Socket> readEvents  = new List <Socket>();
            List <Socket> writeEvents = new List <Socket>();
            List <Socket> errorEvents = new List <Socket>();

            foreach (PollEvent Event in events)
            {
                bool isValidEvent = false;

                if ((Event.InputEvents & PollEvent.EventTypeMask.Input) != 0)
                {
                    readEvents.Add(Event.Socket.Handle);
                    errorEvents.Add(Event.Socket.Handle);

                    isValidEvent = true;
                }

                if ((Event.InputEvents & PollEvent.EventTypeMask.UrgentInput) != 0)
                {
                    readEvents.Add(Event.Socket.Handle);
                    errorEvents.Add(Event.Socket.Handle);

                    isValidEvent = true;
                }

                if ((Event.InputEvents & PollEvent.EventTypeMask.Output) != 0)
                {
                    writeEvents.Add(Event.Socket.Handle);
                    errorEvents.Add(Event.Socket.Handle);

                    isValidEvent = true;
                }

                if ((Event.InputEvents & PollEvent.EventTypeMask.Error) != 0)
                {
                    errorEvents.Add(Event.Socket.Handle);
                    isValidEvent = true;
                }

                if (!isValidEvent)
                {
                    Logger.Warning?.Print(LogClass.ServiceBsd, $"Unsupported Poll input event type: {Event.InputEvents}");
                    return(WriteBsdResult(context, -1, LinuxError.EINVAL));
                }
            }

            if (fdsCount != 0)
            {
                try
                {
                    System.Net.Sockets.Socket.Select(readEvents, writeEvents, errorEvents, timeout);
                }
                catch (SocketException exception)
                {
                    return(WriteWinSock2Error(context, (WsaError)exception.ErrorCode));
                }
            }
            else if (timeout == -1)
            {
                // FIXME: If we get a timeout of -1 and there is no fds to wait on, this should kill the KProces. (need to check that with re)
                throw new InvalidOperationException();
            }
            else
            {
                // FIXME: We should make the KThread sleep but we can't do much about it yet.
                Thread.Sleep(timeout);
            }

            for (int i = 0; i < fdsCount; i++)
            {
                PollEvent Event = events[i];
                context.Memory.Write(bufferPosition + (ulong)i * 8, Event.SocketFd);
                context.Memory.Write(bufferPosition + (ulong)i * 8 + 4, (short)Event.InputEvents);

                PollEvent.EventTypeMask outputEvents = 0;

                Socket socket = Event.Socket.Handle;

                if (errorEvents.Contains(socket))
                {
                    outputEvents |= PollEvent.EventTypeMask.Error;

                    if (!socket.Connected || !socket.IsBound)
                    {
                        outputEvents |= PollEvent.EventTypeMask.Disconnected;
                    }
                }

                if (readEvents.Contains(socket))
                {
                    if ((Event.InputEvents & PollEvent.EventTypeMask.Input) != 0)
                    {
                        outputEvents |= PollEvent.EventTypeMask.Input;
                    }
                }

                if (writeEvents.Contains(socket))
                {
                    outputEvents |= PollEvent.EventTypeMask.Output;
                }

                context.Memory.Write(bufferPosition + (ulong)i * 8 + 6, (short)outputEvents);
            }

            return(WriteBsdResult(context, readEvents.Count + writeEvents.Count + errorEvents.Count, LinuxError.SUCCESS));
        }