| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191 |
- using Ryujinx.Common.Logging;
- using Ryujinx.HLE.HOS.Ipc;
- using Ryujinx.HLE.Utilities;
- using System.Collections.Generic;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- namespace Ryujinx.HLE.HOS.Services.Bsd
- {
- class IClient : IpcService
- {
- private static Dictionary<WsaError, LinuxError> _errorMap = new Dictionary<WsaError, LinuxError>
- {
- // WSAEINTR
- {WsaError.WSAEINTR, LinuxError.EINTR},
- // WSAEWOULDBLOCK
- {WsaError.WSAEWOULDBLOCK, LinuxError.EWOULDBLOCK},
- // WSAEINPROGRESS
- {WsaError.WSAEINPROGRESS, LinuxError.EINPROGRESS},
- // WSAEALREADY
- {WsaError.WSAEALREADY, LinuxError.EALREADY},
- // WSAENOTSOCK
- {WsaError.WSAENOTSOCK, LinuxError.ENOTSOCK},
- // WSAEDESTADDRREQ
- {WsaError.WSAEDESTADDRREQ, LinuxError.EDESTADDRREQ},
- // WSAEMSGSIZE
- {WsaError.WSAEMSGSIZE, LinuxError.EMSGSIZE},
- // WSAEPROTOTYPE
- {WsaError.WSAEPROTOTYPE, LinuxError.EPROTOTYPE},
- // WSAENOPROTOOPT
- {WsaError.WSAENOPROTOOPT, LinuxError.ENOPROTOOPT},
- // WSAEPROTONOSUPPORT
- {WsaError.WSAEPROTONOSUPPORT, LinuxError.EPROTONOSUPPORT},
- // WSAESOCKTNOSUPPORT
- {WsaError.WSAESOCKTNOSUPPORT, LinuxError.ESOCKTNOSUPPORT},
- // WSAEOPNOTSUPP
- {WsaError.WSAEOPNOTSUPP, LinuxError.EOPNOTSUPP},
- // WSAEPFNOSUPPORT
- {WsaError.WSAEPFNOSUPPORT, LinuxError.EPFNOSUPPORT},
- // WSAEAFNOSUPPORT
- {WsaError.WSAEAFNOSUPPORT, LinuxError.EAFNOSUPPORT},
- // WSAEADDRINUSE
- {WsaError.WSAEADDRINUSE, LinuxError.EADDRINUSE},
- // WSAEADDRNOTAVAIL
- {WsaError.WSAEADDRNOTAVAIL, LinuxError.EADDRNOTAVAIL},
- // WSAENETDOWN
- {WsaError.WSAENETDOWN, LinuxError.ENETDOWN},
- // WSAENETUNREACH
- {WsaError.WSAENETUNREACH, LinuxError.ENETUNREACH},
- // WSAENETRESET
- {WsaError.WSAENETRESET, LinuxError.ENETRESET},
- // WSAECONNABORTED
- {WsaError.WSAECONNABORTED, LinuxError.ECONNABORTED},
- // WSAECONNRESET
- {WsaError.WSAECONNRESET, LinuxError.ECONNRESET},
- // WSAENOBUFS
- {WsaError.WSAENOBUFS, LinuxError.ENOBUFS},
- // WSAEISCONN
- {WsaError.WSAEISCONN, LinuxError.EISCONN},
- // WSAENOTCONN
- {WsaError.WSAENOTCONN, LinuxError.ENOTCONN},
- // WSAESHUTDOWN
- {WsaError.WSAESHUTDOWN, LinuxError.ESHUTDOWN},
- // WSAETOOMANYREFS
- {WsaError.WSAETOOMANYREFS, LinuxError.ETOOMANYREFS},
- // WSAETIMEDOUT
- {WsaError.WSAETIMEDOUT, LinuxError.ETIMEDOUT},
- // WSAECONNREFUSED
- {WsaError.WSAECONNREFUSED, LinuxError.ECONNREFUSED},
- // WSAELOOP
- {WsaError.WSAELOOP, LinuxError.ELOOP},
- // WSAENAMETOOLONG
- {WsaError.WSAENAMETOOLONG, LinuxError.ENAMETOOLONG},
- // WSAEHOSTDOWN
- {WsaError.WSAEHOSTDOWN, LinuxError.EHOSTDOWN},
- // WSAEHOSTUNREACH
- {WsaError.WSAEHOSTUNREACH, LinuxError.EHOSTUNREACH},
- // WSAENOTEMPTY
- {WsaError.WSAENOTEMPTY, LinuxError.ENOTEMPTY},
- // WSAEUSERS
- {WsaError.WSAEUSERS, LinuxError.EUSERS},
- // WSAEDQUOT
- {WsaError.WSAEDQUOT, LinuxError.EDQUOT},
- // WSAESTALE
- {WsaError.WSAESTALE, LinuxError.ESTALE},
- // WSAEREMOTE
- {WsaError.WSAEREMOTE, LinuxError.EREMOTE},
- // WSAEINVAL
- {WsaError.WSAEINVAL, LinuxError.EINVAL},
- // WSAEFAULT
- {WsaError.WSAEFAULT, LinuxError.EFAULT},
- // NOERROR
- {0, 0}
- };
- private Dictionary<int, ServiceProcessRequest> _commands;
- public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => _commands;
- private bool _isPrivileged;
- private List<BsdSocket> _sockets = new List<BsdSocket>();
- public IClient(bool isPrivileged)
- {
- _commands = new Dictionary<int, ServiceProcessRequest>
- {
- { 0, RegisterClient },
- { 1, StartMonitoring },
- { 2, Socket },
- { 3, SocketExempt },
- { 4, Open },
- { 5, Select },
- { 6, Poll },
- { 7, Sysctl },
- { 8, Recv },
- { 9, RecvFrom },
- { 10, Send },
- { 11, SendTo },
- { 12, Accept },
- { 13, Bind },
- { 14, Connect },
- { 15, GetPeerName },
- { 16, GetSockName },
- { 17, GetSockOpt },
- { 18, Listen },
- { 19, Ioctl },
- { 20, Fcntl },
- { 21, SetSockOpt },
- { 22, Shutdown },
- { 23, ShutdownAllSockets },
- { 24, Write },
- { 25, Read },
- { 26, Close },
- { 27, DuplicateSocket }
- };
- _isPrivileged = isPrivileged;
- }
- private LinuxError ConvertError(WsaError errorCode)
- {
- LinuxError errno;
- if (!_errorMap.TryGetValue(errorCode, out errno))
- {
- errno = (LinuxError)errorCode;
- }
- return errno;
- }
- private long WriteWinSock2Error(ServiceCtx context, WsaError errorCode)
- {
- return WriteBsdResult(context, -1, ConvertError(errorCode));
- }
- private long WriteBsdResult(ServiceCtx context, int result, LinuxError errorCode = 0)
- {
- if (errorCode != LinuxError.SUCCESS)
- {
- result = -1;
- }
- context.ResponseData.Write(result);
- context.ResponseData.Write((int)errorCode);
- return 0;
- }
- private BsdSocket RetrieveSocket(int socketFd)
- {
- if (socketFd >= 0 && _sockets.Count > socketFd)
- {
- return _sockets[socketFd];
- }
- return null;
- }
- private LinuxError SetResultErrno(Socket socket, int result)
- {
- return result == 0 && !socket.Blocking ? LinuxError.EWOULDBLOCK : LinuxError.SUCCESS;
- }
- private AddressFamily ConvertFromBsd(int domain)
- {
- if (domain == 2)
- {
- return AddressFamily.InterNetwork;
- }
- // FIXME: AF_ROUTE ignored, is that really needed?
- return AddressFamily.Unknown;
- }
- private long 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);
- }
- private IPEndPoint ParseSockAddr(ServiceCtx context, long bufferPosition, long bufferSize)
- {
- int size = context.Memory.ReadByte(bufferPosition);
- int family = context.Memory.ReadByte(bufferPosition + 1);
- int port = EndianSwap.Swap16(context.Memory.ReadUInt16(bufferPosition + 2));
- byte[] rawIp = context.Memory.ReadBytes(bufferPosition + 4, 4);
- return new IPEndPoint(new IPAddress(rawIp), port);
- }
- private void WriteSockAddr(ServiceCtx context, long bufferPosition, IPEndPoint endPoint)
- {
- context.Memory.WriteByte(bufferPosition, 0);
- context.Memory.WriteByte(bufferPosition + 1, (byte)endPoint.AddressFamily);
- context.Memory.WriteUInt16(bufferPosition + 2, EndianSwap.Swap16((ushort)endPoint.Port));
- context.Memory.WriteBytes(bufferPosition + 4, endPoint.Address.GetAddressBytes());
- }
- 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);
- }
- // Initialize(nn::socket::BsdBufferConfig config, u64 pid, u64 transferMemorySize, KObject<copy, transfer_memory>, pid) -> u32 bsd_errno
- public long RegisterClient(ServiceCtx context)
- {
- /*
- 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.PrintStub(LogClass.ServiceBsd);
- return 0;
- }
- // StartMonitoring(u64, pid)
- public long StartMonitoring(ServiceCtx context)
- {
- ulong unknown0 = context.RequestData.ReadUInt64();
- Logger.PrintStub(LogClass.ServiceBsd, new { unknown0 });
- return 0;
- }
- // Socket(u32 domain, u32 type, u32 protocol) -> (i32 ret, u32 bsd_errno)
- public long Socket(ServiceCtx context)
- {
- return SocketInternal(context, false);
- }
- // SocketExempt(u32 domain, u32 type, u32 protocol) -> (i32 ret, u32 bsd_errno)
- public long SocketExempt(ServiceCtx context)
- {
- return SocketInternal(context, true);
- }
- // Open(u32 flags, array<unknown, 0x21> path) -> (i32 ret, u32 bsd_errno)
- public long Open(ServiceCtx context)
- {
- (long bufferPosition, long bufferSize) = context.Request.GetBufferType0x21();
- int flags = context.RequestData.ReadInt32();
- byte[] rawPath = context.Memory.ReadBytes(bufferPosition, bufferSize);
- string path = Encoding.ASCII.GetString(rawPath);
- WriteBsdResult(context, -1, LinuxError.EOPNOTSUPP);
- Logger.PrintStub(LogClass.ServiceBsd, new { path, flags });
- return 0;
- }
- // Select(u32 nfds, nn::socket::timeout timeout, buffer<nn::socket::fd_set, 0x21, 0> readfds_in, buffer<nn::socket::fd_set, 0x21, 0> writefds_in, buffer<nn::socket::fd_set, 0x21, 0> errorfds_in) -> (i32 ret, u32 bsd_errno, buffer<nn::socket::fd_set, 0x22, 0> readfds_out, buffer<nn::socket::fd_set, 0x22, 0> writefds_out, buffer<nn::socket::fd_set, 0x22, 0> errorfds_out)
- public long Select(ServiceCtx context)
- {
- WriteBsdResult(context, -1, LinuxError.EOPNOTSUPP);
- Logger.PrintStub(LogClass.ServiceBsd);
- return 0;
- }
- // Poll(u32 nfds, u32 timeout, buffer<unknown, 0x21, 0> fds) -> (i32 ret, u32 bsd_errno, buffer<unknown, 0x22, 0>)
- public long 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);
- }
- // Sysctl(buffer<unknown, 0x21, 0>, buffer<unknown, 0x21, 0>) -> (i32 ret, u32 bsd_errno, u32, buffer<unknown, 0x22, 0>)
- public long Sysctl(ServiceCtx context)
- {
- WriteBsdResult(context, -1, LinuxError.EOPNOTSUPP);
- Logger.PrintStub(LogClass.ServiceBsd);
- return 0;
- }
- // Recv(u32 socket, u32 flags) -> (i32 ret, u32 bsd_errno, array<i8, 0x22> message)
- public long Recv(ServiceCtx context)
- {
- int socketFd = context.RequestData.ReadInt32();
- SocketFlags socketFlags = (SocketFlags)context.RequestData.ReadInt32();
- (long receivePosition, long receiveLength) = context.Request.GetBufferType0x22();
- 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];
- try
- {
- result = socket.Handle.Receive(receivedBuffer, socketFlags);
- errno = SetResultErrno(socket.Handle, result);
- context.Memory.WriteBytes(receivePosition, receivedBuffer);
- }
- catch (SocketException exception)
- {
- errno = ConvertError((WsaError)exception.ErrorCode);
- }
- }
- return WriteBsdResult(context, result, errno);
- }
- // 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 long RecvFrom(ServiceCtx context)
- {
- int socketFd = context.RequestData.ReadInt32();
- SocketFlags socketFlags = (SocketFlags)context.RequestData.ReadInt32();
- (long receivePosition, long receiveLength) = context.Request.GetBufferType0x22();
- (long sockAddrInPosition, long sockAddrInSize) = context.Request.GetBufferType0x21();
- (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 = ParseSockAddr(context, sockAddrInPosition, sockAddrInSize);
- 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);
- }
- // Send(u32 socket, u32 flags, buffer<i8, 0x21, 0>) -> (i32 ret, u32 bsd_errno)
- public long Send(ServiceCtx context)
- {
- int socketFd = context.RequestData.ReadInt32();
- SocketFlags socketFlags = (SocketFlags)context.RequestData.ReadInt32();
- (long sendPosition, long sendSize) = context.Request.GetBufferType0x21();
- 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.PrintWarning(LogClass.ServiceBsd, $"Unsupported Send flags: {socketFlags}");
- return WriteBsdResult(context, -1, LinuxError.EOPNOTSUPP);
- }
- byte[] sendBuffer = context.Memory.ReadBytes(sendPosition, sendSize);
- try
- {
- result = socket.Handle.Send(sendBuffer, socketFlags);
- errno = SetResultErrno(socket.Handle, result);
- }
- catch (SocketException exception)
- {
- errno = ConvertError((WsaError)exception.ErrorCode);
- }
- }
- return WriteBsdResult(context, result, errno);
- }
- // SendTo(u32 socket, u32 flags, buffer<i8, 0x21, 0>, buffer<nn::socket::sockaddr_in, 0x21, 0x10>) -> (i32 ret, u32 bsd_errno)
- public long SendTo(ServiceCtx context)
- {
- int socketFd = context.RequestData.ReadInt32();
- SocketFlags socketFlags = (SocketFlags)context.RequestData.ReadInt32();
- (long sendPosition, long sendSize) = context.Request.GetBufferType0x21();
- (long bufferPosition, long 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.PrintWarning(LogClass.ServiceBsd, $"Unsupported Send flags: {socketFlags}");
- return WriteBsdResult(context, -1, LinuxError.EOPNOTSUPP);
- }
- byte[] sendBuffer = context.Memory.ReadBytes(sendPosition, sendSize);
- 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);
- }
- // Accept(u32 socket) -> (i32 ret, u32 bsd_errno, u32 addrlen, buffer<nn::socket::sockaddr_in, 0x22, 0x10> addr)
- public long 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 0;
- }
- }
- return WriteBsdResult(context, -1, errno);
- }
- // Bind(u32 socket, buffer<nn::socket::sockaddr_in, 0x21, 0x10> addr) -> (i32 ret, u32 bsd_errno)
- public long Bind(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.Bind(endPoint);
- }
- catch (SocketException exception)
- {
- errno = ConvertError((WsaError)exception.ErrorCode);
- }
- }
- return WriteBsdResult(context, 0, errno);
- }
- // Connect(u32 socket, buffer<nn::socket::sockaddr_in, 0x21, 0x10>) -> (i32 ret, u32 bsd_errno)
- public long 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);
- }
- // GetPeerName(u32 socket) -> (i32 ret, u32 bsd_errno, u32 addrlen, buffer<nn::socket::sockaddr_in, 0x22, 0x10> addr)
- public long GetPeerName(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, true);
- WriteBsdResult(context, 0, errno);
- context.ResponseData.Write(0x10);
- }
- return WriteBsdResult(context, 0, errno);
- }
- // GetSockName(u32 socket) -> (i32 ret, u32 bsd_errno, u32 addrlen, buffer<nn::socket::sockaddr_in, 0x22, 0x10> addr)
- public long 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);
- }
- // GetSockOpt(u32 socket, u32 level, u32 option_name) -> (i32 ret, u32 bsd_errno, u32, buffer<unknown, 0x22, 0>)
- public long GetSockOpt(ServiceCtx context)
- {
- int socketFd = context.RequestData.ReadInt32();
- int level = context.RequestData.ReadInt32();
- int optionName = context.RequestData.ReadInt32();
- (long bufferPosition, long bufferSize) = context.Request.GetBufferType0x22();
- LinuxError errno = LinuxError.EBADF;
- BsdSocket socket = RetrieveSocket(socketFd);
- if (socket != null)
- {
- errno = LinuxError.ENOPROTOOPT;
- if (level == 0xFFFF)
- {
- errno = HandleGetSocketOption(context, socket, (SocketOptionName)optionName, bufferPosition, bufferSize);
- }
- else
- {
- Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported GetSockOpt Level: {(SocketOptionLevel)level}");
- }
- }
- return WriteBsdResult(context, 0, errno);
- }
- // Listen(u32 socket, u32 backlog) -> (i32 ret, u32 bsd_errno)
- public long 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);
- }
- // 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 long 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);
- }
- // Fcntl(u32 socket, u32 cmd, u32 arg) -> (i32 ret, u32 bsd_errno)
- public long 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);
- }
- private LinuxError HandleGetSocketOption(ServiceCtx context, BsdSocket socket, SocketOptionName optionName, long optionValuePosition, long optionValueSize)
- {
- try
- {
- byte[] optionValue = new byte[optionValueSize];
- 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.Linger:
- socket.Handle.GetSocketOption(SocketOptionLevel.Socket, optionName, optionValue);
- context.Memory.WriteBytes(optionValuePosition, optionValue);
- return LinuxError.SUCCESS;
- case (SocketOptionName)0x200:
- socket.Handle.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, optionValue);
- context.Memory.WriteBytes(optionValuePosition, optionValue);
- return LinuxError.SUCCESS;
- default:
- Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported SetSockOpt OptionName: {optionName}");
- return LinuxError.EOPNOTSUPP;
- }
- }
- catch (SocketException exception)
- {
- return ConvertError((WsaError)exception.ErrorCode);
- }
- }
- 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);
- }
- }
- // SetSockOpt(u32 socket, u32 level, u32 option_name, buffer<unknown, 0x21, 0> option_value) -> (i32 ret, u32 bsd_errno)
- public long 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);
- }
- // Shutdown(u32 socket, u32 how) -> (i32 ret, u32 bsd_errno)
- public long 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);
- }
- // ShutdownAllSockets(u32 how) -> (i32 ret, u32 bsd_errno)
- public long ShutdownAllSockets(ServiceCtx context)
- {
- int how = context.RequestData.ReadInt32();
- LinuxError errno = LinuxError.EINVAL;
- if (how >= 0 && how <= 2)
- {
- errno = LinuxError.SUCCESS;
- foreach (BsdSocket socket in _sockets)
- {
- if (socket != null)
- {
- try
- {
- socket.Handle.Shutdown((SocketShutdown)how);
- }
- catch (SocketException exception)
- {
- errno = ConvertError((WsaError)exception.ErrorCode);
- break;
- }
- }
- }
- }
- return WriteBsdResult(context, 0, errno);
- }
- // Write(u32 socket, buffer<i8, 0x21, 0> message) -> (i32 ret, u32 bsd_errno)
- public long 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);
- }
- // Read(u32 socket) -> (i32 ret, u32 bsd_errno, buffer<i8, 0x22, 0> message)
- public long 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);
- }
- // Close(u32 socket) -> (i32 ret, u32 bsd_errno)
- public long 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);
- }
- // DuplicateSocket(u32 socket, u64 reserved) -> (i32 ret, u32 bsd_errno)
- public long 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);
- }
- }
- }
|