2018-10-20 15:08:58 -07:00
|
|
|
|
using Ryujinx.Common.Logging;
|
2018-08-16 16:47:36 -07:00
|
|
|
|
using Ryujinx.HLE.HOS.Ipc;
|
|
|
|
|
using Ryujinx.HLE.Utilities;
|
2018-02-27 19:31:52 -08:00
|
|
|
|
using System.Collections.Generic;
|
2018-03-11 18:05:39 -07:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Sockets;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
using System.Text;
|
2018-02-27 19:31:52 -08:00
|
|
|
|
|
2018-08-16 16:47:36 -07:00
|
|
|
|
namespace Ryujinx.HLE.HOS.Services.Bsd
|
2018-02-27 19:31:52 -08:00
|
|
|
|
{
|
2018-04-05 21:01:52 -07:00
|
|
|
|
class IClient : IpcService
|
2018-02-27 19:31:52 -08:00
|
|
|
|
{
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
private static Dictionary<WSAError, LinuxError> ErrorMap = new Dictionary<WSAError, LinuxError>
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
|
|
|
|
// WSAEINTR
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAEINTR, LinuxError.EINTR},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAEWOULDBLOCK
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAEWOULDBLOCK, LinuxError.EWOULDBLOCK},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAEINPROGRESS
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAEINPROGRESS, LinuxError.EINPROGRESS},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAEALREADY
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAEALREADY, LinuxError.EALREADY},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAENOTSOCK
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAENOTSOCK, LinuxError.ENOTSOCK},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAEDESTADDRREQ
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAEDESTADDRREQ, LinuxError.EDESTADDRREQ},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAEMSGSIZE
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAEMSGSIZE, LinuxError.EMSGSIZE},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAEPROTOTYPE
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAEPROTOTYPE, LinuxError.EPROTOTYPE},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAENOPROTOOPT
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAENOPROTOOPT, LinuxError.ENOPROTOOPT},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAEPROTONOSUPPORT
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAEPROTONOSUPPORT, LinuxError.EPROTONOSUPPORT},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAESOCKTNOSUPPORT
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAESOCKTNOSUPPORT, LinuxError.ESOCKTNOSUPPORT},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAEOPNOTSUPP
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAEOPNOTSUPP, LinuxError.EOPNOTSUPP},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAEPFNOSUPPORT
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAEPFNOSUPPORT, LinuxError.EPFNOSUPPORT},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAEAFNOSUPPORT
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAEAFNOSUPPORT, LinuxError.EAFNOSUPPORT},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAEADDRINUSE
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAEADDRINUSE, LinuxError.EADDRINUSE},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAEADDRNOTAVAIL
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAEADDRNOTAVAIL, LinuxError.EADDRNOTAVAIL},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAENETDOWN
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAENETDOWN, LinuxError.ENETDOWN},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAENETUNREACH
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAENETUNREACH, LinuxError.ENETUNREACH},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAENETRESET
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAENETRESET, LinuxError.ENETRESET},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAECONNABORTED
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAECONNABORTED, LinuxError.ECONNABORTED},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAECONNRESET
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAECONNRESET, LinuxError.ECONNRESET},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAENOBUFS
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAENOBUFS, LinuxError.ENOBUFS},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAEISCONN
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAEISCONN, LinuxError.EISCONN},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAENOTCONN
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAENOTCONN, LinuxError.ENOTCONN},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAESHUTDOWN
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAESHUTDOWN, LinuxError.ESHUTDOWN},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAETOOMANYREFS
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAETOOMANYREFS, LinuxError.ETOOMANYREFS},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAETIMEDOUT
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAETIMEDOUT, LinuxError.ETIMEDOUT},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAECONNREFUSED
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAECONNREFUSED, LinuxError.ECONNREFUSED},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAELOOP
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAELOOP, LinuxError.ELOOP},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAENAMETOOLONG
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAENAMETOOLONG, LinuxError.ENAMETOOLONG},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAEHOSTDOWN
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAEHOSTDOWN, LinuxError.EHOSTDOWN},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAEHOSTUNREACH
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAEHOSTUNREACH, LinuxError.EHOSTUNREACH},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAENOTEMPTY
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAENOTEMPTY, LinuxError.ENOTEMPTY},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAEUSERS
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAEUSERS, LinuxError.EUSERS},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAEDQUOT
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAEDQUOT, LinuxError.EDQUOT},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAESTALE
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAESTALE, LinuxError.ESTALE},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAEREMOTE
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAEREMOTE, LinuxError.EREMOTE},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAEINVAL
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAEINVAL, LinuxError.EINVAL},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// WSAEFAULT
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{WSAError.WSAEFAULT, LinuxError.EFAULT},
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// NOERROR
|
|
|
|
|
{0, 0}
|
|
|
|
|
};
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
private Dictionary<int, ServiceProcessRequest> m_Commands;
|
2018-02-27 19:31:52 -08:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
|
2018-02-27 19:31:52 -08:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
private bool IsPrivileged;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
private List<BsdSocket> Sockets = new List<BsdSocket>();
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
public IClient(bool IsPrivileged)
|
2018-02-27 19:31:52 -08:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
m_Commands = new Dictionary<int, ServiceProcessRequest>()
|
2018-02-27 19:31:52 -08:00
|
|
|
|
{
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{ 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 },
|
2018-12-04 16:52:39 -08:00
|
|
|
|
{ 27, DuplicateSocket },
|
2018-10-20 15:08:58 -07:00
|
|
|
|
};
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
this.IsPrivileged = IsPrivileged;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
private LinuxError ConvertError(WSAError ErrorCode)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
LinuxError Errno;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (!ErrorMap.TryGetValue(ErrorCode, out Errno))
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Errno = (LinuxError)ErrorCode;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return Errno;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
private long WriteWinSock2Error(ServiceCtx Context, WSAError ErrorCode)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return WriteBsdResult(Context, -1, ConvertError(ErrorCode));
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
private long WriteBsdResult(ServiceCtx Context, int Result, LinuxError ErrorCode = 0)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (ErrorCode != LinuxError.SUCCESS)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Result = -1;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Context.ResponseData.Write(Result);
|
|
|
|
|
Context.ResponseData.Write((int)ErrorCode);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
private BsdSocket RetrieveSocket(int SocketFd)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (SocketFd >= 0 && Sockets.Count > SocketFd)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return Sockets[SocketFd];
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
private LinuxError SetResultErrno(Socket Socket, int Result)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return Result == 0 && !Socket.Blocking ? LinuxError.EWOULDBLOCK : LinuxError.SUCCESS;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
private AddressFamily ConvertFromBsd(int Domain)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (Domain == 2)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
|
|
|
|
return AddressFamily.InterNetwork;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FIXME: AF_ROUTE ignored, is that really needed?
|
|
|
|
|
return AddressFamily.Unknown;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
private long SocketInternal(ServiceCtx Context, bool Exempt)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
AddressFamily Domain = (AddressFamily)Context.RequestData.ReadInt32();
|
|
|
|
|
SocketType Type = (SocketType)Context.RequestData.ReadInt32();
|
|
|
|
|
ProtocolType Protocol = (ProtocolType)Context.RequestData.ReadInt32();
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (Domain == AddressFamily.Unknown)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return WriteBsdResult(Context, -1, LinuxError.EPROTONOSUPPORT);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
2018-12-04 16:52:39 -08:00
|
|
|
|
else if ((Type == SocketType.Seqpacket || Type == SocketType.Raw) && !IsPrivileged)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (Domain != AddressFamily.InterNetwork || Type != SocketType.Raw || Protocol != ProtocolType.Icmp)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return WriteBsdResult(Context, -1, LinuxError.ENOENT);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
BsdSocket NewBsdSocket = new BsdSocket
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Family = (int)Domain,
|
|
|
|
|
Type = (int)Type,
|
|
|
|
|
Protocol = (int)Protocol,
|
|
|
|
|
Handle = new Socket(Domain, Type, Protocol)
|
2018-02-27 19:31:52 -08:00
|
|
|
|
};
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Sockets.Add(NewBsdSocket);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (Exempt)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
NewBsdSocket.Handle.Disconnect(true);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return WriteBsdResult(Context, Sockets.Count - 1);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
private IPEndPoint ParseSockAddr(ServiceCtx Context, long BufferPosition, long BufferSize)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
int Size = Context.Memory.ReadByte(BufferPosition);
|
|
|
|
|
int Family = Context.Memory.ReadByte(BufferPosition + 1);
|
|
|
|
|
int Port = EndianSwap.Swap16(Context.Memory.ReadUInt16(BufferPosition + 2));
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
byte[] RawIp = Context.Memory.ReadBytes(BufferPosition + 4, 4);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return new IPEndPoint(new IPAddress(RawIp), Port);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
private void WriteSockAddr(ServiceCtx Context, long BufferPosition, IPEndPoint EndPoint)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
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());
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
private void WriteSockAddr(ServiceCtx Context, long BufferPosition, BsdSocket Socket, bool IsRemote)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
IPEndPoint EndPoint = (IsRemote ? Socket.Handle.RemoteEndPoint : Socket.Handle.LocalEndPoint) as IPEndPoint;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
WriteSockAddr(Context, BufferPosition, EndPoint);
|
2018-02-27 19:31:52 -08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// Initialize(nn::socket::BsdBufferConfig config, u64 pid, u64 transferMemorySize, KObject<copy, transfer_memory>, pid) -> u32 bsd_errno
|
2018-12-04 16:52:39 -08:00
|
|
|
|
public long RegisterClient(ServiceCtx Context)
|
2018-02-27 19:31:52 -08:00
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
typedef struct {
|
|
|
|
|
u32 version; // Observed 1 on 2.0 LibAppletWeb, 2 on 3.0.
|
|
|
|
|
u32 tcp_tx_buf_size; // Size of the TCP transfer (send) buffer (initial or fixed).
|
|
|
|
|
u32 tcp_rx_buf_size; // Size of the TCP recieve buffer (initial or fixed).
|
|
|
|
|
u32 tcp_tx_buf_max_size; // Maximum size of the TCP transfer (send) buffer. If it is 0, the size of the buffer is fixed to its initial value.
|
|
|
|
|
u32 tcp_rx_buf_max_size; // Maximum size of the TCP receive buffer. If it is 0, the size of the buffer is fixed to its initial value.
|
|
|
|
|
u32 udp_tx_buf_size; // Size of the UDP transfer (send) buffer (typically 0x2400 bytes).
|
|
|
|
|
u32 udp_rx_buf_size; // Size of the UDP receive buffer (typically 0xA500 bytes).
|
|
|
|
|
u32 sb_efficiency; // Number of buffers for each socket (standard values range from 1 to 8).
|
|
|
|
|
} BsdBufferConfig;
|
|
|
|
|
*/
|
|
|
|
|
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// bsd_error
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Context.ResponseData.Write(0);
|
2018-02-27 19:31:52 -08:00
|
|
|
|
|
2018-10-20 15:08:58 -07:00
|
|
|
|
Logger.PrintStub(LogClass.ServiceBsd, "Stubbed.");
|
2018-02-27 19:31:52 -08:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// StartMonitoring(u64, pid)
|
2018-12-04 16:52:39 -08:00
|
|
|
|
public long StartMonitoring(ServiceCtx Context)
|
2018-02-27 19:31:52 -08:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
ulong Unknown0 = Context.RequestData.ReadUInt64();
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Logger.PrintStub(LogClass.ServiceBsd, $"Stubbed. Unknown0: {Unknown0}");
|
2018-02-27 19:31:52 -08:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2018-02-28 12:58:04 -08:00
|
|
|
|
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// Socket(u32 domain, u32 type, u32 protocol) -> (i32 ret, u32 bsd_errno)
|
2018-12-04 16:52:39 -08:00
|
|
|
|
public long Socket(ServiceCtx Context)
|
2018-02-28 12:58:04 -08:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return SocketInternal(Context, false);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// SocketExempt(u32 domain, u32 type, u32 protocol) -> (i32 ret, u32 bsd_errno)
|
2018-12-04 16:52:39 -08:00
|
|
|
|
public long SocketExempt(ServiceCtx Context)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return SocketInternal(Context, true);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// Open(u32 flags, array<unknown, 0x21> path) -> (i32 ret, u32 bsd_errno)
|
2018-12-04 16:52:39 -08:00
|
|
|
|
public long Open(ServiceCtx Context)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
(long BufferPosition, long BufferSize) = Context.Request.GetBufferType0x21();
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
int Flags = Context.RequestData.ReadInt32();
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
byte[] RawPath = Context.Memory.ReadBytes(BufferPosition, BufferSize);
|
|
|
|
|
string Path = Encoding.ASCII.GetString(RawPath);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
WriteBsdResult(Context, -1, LinuxError.EOPNOTSUPP);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Logger.PrintStub(LogClass.ServiceBsd, $"Stubbed. Path: {Path} - " +
|
|
|
|
|
$"Flags: {Flags}");
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// Select(u32 nfds, nn::socket::timeout timeout, buffer<nn::socket::fd_set, 0x21, 0> readfds_in, buffer<nn::socket::fd_set, 0x21, 0> writefds_in, buffer<nn::socket::fd_set, 0x21, 0> errorfds_in) -> (i32 ret, u32 bsd_errno, buffer<nn::socket::fd_set, 0x22, 0> readfds_out, buffer<nn::socket::fd_set, 0x22, 0> writefds_out, buffer<nn::socket::fd_set, 0x22, 0> errorfds_out)
|
2018-12-04 16:52:39 -08:00
|
|
|
|
public long Select(ServiceCtx Context)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
WriteBsdResult(Context, -1, LinuxError.EOPNOTSUPP);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
|
|
|
|
Logger.PrintStub(LogClass.ServiceBsd, $"Stubbed.");
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Poll(u32 nfds, u32 timeout, buffer<unknown, 0x21, 0> fds) -> (i32 ret, u32 bsd_errno, buffer<unknown, 0x22, 0>)
|
2018-12-04 16:52:39 -08:00
|
|
|
|
public long Poll(ServiceCtx Context)
|
2018-03-11 18:05:39 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
int FdsCount = Context.RequestData.ReadInt32();
|
|
|
|
|
int Timeout = Context.RequestData.ReadInt32();
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
(long BufferPosition, long BufferSize) = Context.Request.GetBufferType0x21();
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-04-05 21:01:52 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (Timeout < -1 || FdsCount < 0 || (FdsCount * 8) > BufferSize)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return WriteBsdResult(Context, -1, LinuxError.EINVAL);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
PollEvent[] Events = new PollEvent[FdsCount];
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
for (int i = 0; i < FdsCount; i++)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
int SocketFd = Context.Memory.ReadInt32(BufferPosition + i * 8);
|
2018-02-28 12:58:04 -08:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
BsdSocket Socket = RetrieveSocket(SocketFd);
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (Socket == null)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return WriteBsdResult(Context, -1, LinuxError.EBADF);
|
|
|
|
|
}
|
2018-04-05 21:01:52 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
PollEvent.EventTypeMask InputEvents = (PollEvent.EventTypeMask)Context.Memory.ReadInt16(BufferPosition + i * 8 + 4);
|
|
|
|
|
PollEvent.EventTypeMask OutputEvents = (PollEvent.EventTypeMask)Context.Memory.ReadInt16(BufferPosition + i * 8 + 6);
|
2018-08-24 10:20:42 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Events[i] = new PollEvent(SocketFd, Socket, InputEvents, OutputEvents);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
2018-04-05 21:01:52 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
List<Socket> ReadEvents = new List<Socket>();
|
|
|
|
|
List<Socket> WriteEvents = new List<Socket>();
|
|
|
|
|
List<Socket> ErrorEvents = new List<Socket>();
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
foreach (PollEvent Event in Events)
|
2018-03-11 18:05:39 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
bool IsValidEvent = false;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
|
|
|
|
if ((Event.InputEvents & PollEvent.EventTypeMask.Input) != 0)
|
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
ReadEvents.Add(Event.Socket.Handle);
|
|
|
|
|
ErrorEvents.Add(Event.Socket.Handle);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
IsValidEvent = true;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((Event.InputEvents & PollEvent.EventTypeMask.UrgentInput) != 0)
|
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
ReadEvents.Add(Event.Socket.Handle);
|
|
|
|
|
ErrorEvents.Add(Event.Socket.Handle);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
IsValidEvent = true;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((Event.InputEvents & PollEvent.EventTypeMask.Output) != 0)
|
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
WriteEvents.Add(Event.Socket.Handle);
|
|
|
|
|
ErrorEvents.Add(Event.Socket.Handle);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
IsValidEvent = true;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-10-20 15:08:58 -07:00
|
|
|
|
if ((Event.InputEvents & PollEvent.EventTypeMask.Error) != 0)
|
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
ErrorEvents.Add(Event.Socket.Handle);
|
|
|
|
|
IsValidEvent = true;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (!IsValidEvent)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
|
|
|
|
Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported Poll input event type: {Event.InputEvents}");
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return WriteBsdResult(Context, -1, LinuxError.EINVAL);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
System.Net.Sockets.Socket.Select(ReadEvents, WriteEvents, ErrorEvents, Timeout);
|
2018-03-11 18:05:39 -07:00
|
|
|
|
}
|
2018-12-04 16:52:39 -08:00
|
|
|
|
catch (SocketException Exception)
|
2018-03-11 18:05:39 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return WriteWinSock2Error(Context, (WSAError)Exception.ErrorCode);
|
2018-03-11 18:05:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
for (int i = 0; i < FdsCount; i++)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
PollEvent Event = Events[i];
|
|
|
|
|
Context.Memory.WriteInt32(BufferPosition + i * 8, Event.SocketFd);
|
|
|
|
|
Context.Memory.WriteInt16(BufferPosition + i * 8 + 4, (short)Event.InputEvents);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
PollEvent.EventTypeMask OutputEvents = 0;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Socket Socket = Event.Socket.Handle;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (ErrorEvents.Contains(Socket))
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
OutputEvents |= PollEvent.EventTypeMask.Error;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (!Socket.Connected || !Socket.IsBound)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
OutputEvents |= PollEvent.EventTypeMask.Disconnected;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (ReadEvents.Contains(Socket))
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
|
|
|
|
if ((Event.InputEvents & PollEvent.EventTypeMask.Input) != 0)
|
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
OutputEvents |= PollEvent.EventTypeMask.Input;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (WriteEvents.Contains(Socket))
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
OutputEvents |= PollEvent.EventTypeMask.Output;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Context.Memory.WriteInt16(BufferPosition + i * 8 + 6, (short)OutputEvents);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return WriteBsdResult(Context, ReadEvents.Count + WriteEvents.Count + ErrorEvents.Count, LinuxError.SUCCESS);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sysctl(buffer<unknown, 0x21, 0>, buffer<unknown, 0x21, 0>) -> (i32 ret, u32 bsd_errno, u32, buffer<unknown, 0x22, 0>)
|
2018-12-04 16:52:39 -08:00
|
|
|
|
public long Sysctl(ServiceCtx Context)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
WriteBsdResult(Context, -1, LinuxError.EOPNOTSUPP);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
|
|
|
|
Logger.PrintStub(LogClass.ServiceBsd, $"Stubbed.");
|
|
|
|
|
|
2018-03-11 18:05:39 -07:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// Recv(u32 socket, u32 flags) -> (i32 ret, u32 bsd_errno, array<i8, 0x22> message)
|
2018-12-04 16:52:39 -08:00
|
|
|
|
public long Recv(ServiceCtx Context)
|
2018-03-11 18:05:39 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
int SocketFd = Context.RequestData.ReadInt32();
|
|
|
|
|
SocketFlags SocketFlags = (SocketFlags)Context.RequestData.ReadInt32();
|
2018-04-05 21:01:52 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
(long ReceivePosition, long ReceiveLength) = Context.Request.GetBufferType0x22();
|
2018-08-24 10:20:42 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
LinuxError Errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket Socket = RetrieveSocket(SocketFd);
|
|
|
|
|
int Result = -1;
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (Socket != null)
|
2018-03-11 18:05:39 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (SocketFlags != SocketFlags.None && (SocketFlags & SocketFlags.OutOfBand) == 0
|
|
|
|
|
&& (SocketFlags & SocketFlags.Peek) == 0)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported Recv flags: {SocketFlags}");
|
|
|
|
|
return WriteBsdResult(Context, -1, LinuxError.EOPNOTSUPP);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
byte[] ReceivedBuffer = new byte[ReceiveLength];
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Result = Socket.Handle.Receive(ReceivedBuffer, SocketFlags);
|
|
|
|
|
Errno = SetResultErrno(Socket.Handle, Result);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Context.Memory.WriteBytes(ReceivePosition, ReceivedBuffer);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
2018-12-04 16:52:39 -08:00
|
|
|
|
catch (SocketException Exception)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Errno = ConvertError((WSAError)Exception.ErrorCode);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
2018-03-11 18:05:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return WriteBsdResult(Context, Result, Errno);
|
2018-03-11 18:05:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// RecvFrom(u32 sock, u32 flags) -> (i32 ret, u32 bsd_errno, u32 addrlen, buffer<i8, 0x22, 0> message, buffer<nn::socket::sockaddr_in, 0x22, 0x10>)
|
2018-12-04 16:52:39 -08:00
|
|
|
|
public long RecvFrom(ServiceCtx Context)
|
2018-03-11 18:05:39 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
int SocketFd = Context.RequestData.ReadInt32();
|
|
|
|
|
SocketFlags SocketFlags = (SocketFlags)Context.RequestData.ReadInt32();
|
2018-04-05 21:01:52 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
(long ReceivePosition, long ReceiveLength) = Context.Request.GetBufferType0x22();
|
|
|
|
|
(long SockAddrInPosition, long SockAddrInSize) = Context.Request.GetBufferType0x21();
|
|
|
|
|
(long SockAddrOutPosition, long SockAddrOutSize) = Context.Request.GetBufferType0x22(1);
|
2018-08-24 10:20:42 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
LinuxError Errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket Socket = RetrieveSocket(SocketFd);
|
|
|
|
|
int Result = -1;
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (Socket != null)
|
2018-03-11 18:05:39 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (SocketFlags != SocketFlags.None && (SocketFlags & SocketFlags.OutOfBand) == 0
|
|
|
|
|
&& (SocketFlags & SocketFlags.Peek) == 0)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported Recv flags: {SocketFlags}");
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return WriteBsdResult(Context, -1, LinuxError.EOPNOTSUPP);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
byte[] ReceivedBuffer = new byte[ReceiveLength];
|
|
|
|
|
EndPoint EndPoint = ParseSockAddr(Context, SockAddrInPosition, SockAddrInSize);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-03-11 18:05:39 -07:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Result = Socket.Handle.ReceiveFrom(ReceivedBuffer, ReceivedBuffer.Length, SocketFlags, ref EndPoint);
|
|
|
|
|
Errno = SetResultErrno(Socket.Handle, Result);
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Context.Memory.WriteBytes(ReceivePosition, ReceivedBuffer);
|
|
|
|
|
WriteSockAddr(Context, SockAddrOutPosition, (IPEndPoint)EndPoint);
|
2018-03-11 18:05:39 -07:00
|
|
|
|
}
|
2018-12-04 16:52:39 -08:00
|
|
|
|
catch (SocketException Exception)
|
2018-03-11 18:05:39 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Errno = ConvertError((WSAError)Exception.ErrorCode);
|
2018-03-11 18:05:39 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return WriteBsdResult(Context, Result, Errno);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// Send(u32 socket, u32 flags, buffer<i8, 0x21, 0>) -> (i32 ret, u32 bsd_errno)
|
2018-12-04 16:52:39 -08:00
|
|
|
|
public long Send(ServiceCtx Context)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
int SocketFd = Context.RequestData.ReadInt32();
|
|
|
|
|
SocketFlags SocketFlags = (SocketFlags)Context.RequestData.ReadInt32();
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
(long SendPosition, long SendSize) = Context.Request.GetBufferType0x21();
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
LinuxError Errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket Socket = RetrieveSocket(SocketFd);
|
|
|
|
|
int Result = -1;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (Socket != null)
|
2018-03-11 18:05:39 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (SocketFlags != SocketFlags.None && SocketFlags != SocketFlags.OutOfBand
|
|
|
|
|
&& SocketFlags != SocketFlags.Peek && SocketFlags != SocketFlags.DontRoute)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported Send flags: {SocketFlags}");
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return WriteBsdResult(Context, -1, LinuxError.EOPNOTSUPP);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
byte[] SendBuffer = Context.Memory.ReadBytes(SendPosition, SendSize);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Result = Socket.Handle.Send(SendBuffer, SocketFlags);
|
|
|
|
|
Errno = SetResultErrno(Socket.Handle, Result);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
2018-12-04 16:52:39 -08:00
|
|
|
|
catch (SocketException Exception)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Errno = ConvertError((WSAError)Exception.ErrorCode);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-11 18:05:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return WriteBsdResult(Context, Result, Errno);
|
2018-03-11 18:05:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// SendTo(u32 socket, u32 flags, buffer<i8, 0x21, 0>, buffer<nn::socket::sockaddr_in, 0x21, 0x10>) -> (i32 ret, u32 bsd_errno)
|
2018-12-04 16:52:39 -08:00
|
|
|
|
public long SendTo(ServiceCtx Context)
|
2018-03-11 18:05:39 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
int SocketFd = Context.RequestData.ReadInt32();
|
|
|
|
|
SocketFlags SocketFlags = (SocketFlags)Context.RequestData.ReadInt32();
|
2018-04-05 21:01:52 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
(long SendPosition, long SendSize) = Context.Request.GetBufferType0x21();
|
|
|
|
|
(long BufferPosition, long BufferSize) = Context.Request.GetBufferType0x21(1);
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
LinuxError Errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket Socket = RetrieveSocket(SocketFd);
|
|
|
|
|
int Result = -1;
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (Socket != null)
|
2018-03-11 18:05:39 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (SocketFlags != SocketFlags.None && SocketFlags != SocketFlags.OutOfBand
|
|
|
|
|
&& SocketFlags != SocketFlags.Peek && SocketFlags != SocketFlags.DontRoute)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported Send flags: {SocketFlags}");
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return WriteBsdResult(Context, -1, LinuxError.EOPNOTSUPP);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
byte[] SendBuffer = Context.Memory.ReadBytes(SendPosition, SendSize);
|
|
|
|
|
EndPoint EndPoint = ParseSockAddr(Context, BufferPosition, BufferSize);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-03-11 18:05:39 -07:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Result = Socket.Handle.SendTo(SendBuffer, SendBuffer.Length, SocketFlags, EndPoint);
|
|
|
|
|
Errno = SetResultErrno(Socket.Handle, Result);
|
2018-03-11 18:05:39 -07:00
|
|
|
|
}
|
2018-12-04 16:52:39 -08:00
|
|
|
|
catch (SocketException Exception)
|
2018-03-11 18:05:39 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Errno = ConvertError((WSAError)Exception.ErrorCode);
|
2018-03-11 18:05:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return WriteBsdResult(Context, Result, Errno);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Accept(u32 socket) -> (i32 ret, u32 bsd_errno, u32 addrlen, buffer<nn::socket::sockaddr_in, 0x22, 0x10> addr)
|
2018-12-04 16:52:39 -08:00
|
|
|
|
public long Accept(ServiceCtx Context)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
int SocketFd = Context.RequestData.ReadInt32();
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
(long BufferPos, long BufferSize) = Context.Request.GetBufferType0x22();
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
LinuxError Errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket Socket = RetrieveSocket(SocketFd);
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (Socket != null)
|
2018-03-11 18:05:39 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Errno = LinuxError.SUCCESS;
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Socket NewSocket = null;
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-10-20 15:08:58 -07:00
|
|
|
|
try
|
2018-03-11 18:05:39 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
NewSocket = Socket.Handle.Accept();
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
2018-12-04 16:52:39 -08:00
|
|
|
|
catch (SocketException Exception)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Errno = ConvertError((WSAError)Exception.ErrorCode);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (NewSocket == null && Errno == LinuxError.SUCCESS)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Errno = LinuxError.EWOULDBLOCK;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
2018-12-04 16:52:39 -08:00
|
|
|
|
else if (Errno == LinuxError.SUCCESS)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
BsdSocket NewBsdSocket = new BsdSocket
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Family = (int)NewSocket.AddressFamily,
|
|
|
|
|
Type = (int)NewSocket.SocketType,
|
|
|
|
|
Protocol = (int)NewSocket.ProtocolType,
|
|
|
|
|
Handle = NewSocket,
|
2018-10-20 15:08:58 -07:00
|
|
|
|
};
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Sockets.Add(NewBsdSocket);
|
2018-04-05 21:01:52 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
WriteSockAddr(Context, BufferPos, NewBsdSocket, true);
|
2018-04-05 21:01:52 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
WriteBsdResult(Context, Sockets.Count - 1, Errno);
|
2018-04-05 21:06:34 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Context.ResponseData.Write(0x10);
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-10-20 15:08:58 -07:00
|
|
|
|
return 0;
|
2018-03-11 18:05:39 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return WriteBsdResult(Context, -1, Errno);
|
2018-03-11 18:05:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// Bind(u32 socket, buffer<nn::socket::sockaddr_in, 0x21, 0x10> addr) -> (i32 ret, u32 bsd_errno)
|
2018-12-04 16:52:39 -08:00
|
|
|
|
public long Bind(ServiceCtx Context)
|
2018-03-11 18:05:39 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
int SocketFd = Context.RequestData.ReadInt32();
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
(long BufferPos, long BufferSize) = Context.Request.GetBufferType0x21();
|
2018-08-24 10:20:42 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
LinuxError Errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket Socket = RetrieveSocket(SocketFd);
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (Socket != null)
|
2018-03-11 18:05:39 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Errno = LinuxError.SUCCESS;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
IPEndPoint EndPoint = ParseSockAddr(Context, BufferPos, BufferSize);
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Socket.Handle.Bind(EndPoint);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
2018-12-04 16:52:39 -08:00
|
|
|
|
catch (SocketException Exception)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Errno = ConvertError((WSAError)Exception.ErrorCode);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
2018-03-11 18:05:39 -07:00
|
|
|
|
}
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return WriteBsdResult(Context, 0, Errno);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Connect(u32 socket, buffer<nn::socket::sockaddr_in, 0x21, 0x10>) -> (i32 ret, u32 bsd_errno)
|
2018-12-04 16:52:39 -08:00
|
|
|
|
public long Connect(ServiceCtx Context)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
int SocketFd = Context.RequestData.ReadInt32();
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
(long BufferPos, long BufferSize) = Context.Request.GetBufferType0x21();
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
LinuxError Errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket Socket = RetrieveSocket(SocketFd);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (Socket != null)
|
2018-03-11 18:05:39 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Errno = LinuxError.SUCCESS;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
IPEndPoint EndPoint = ParseSockAddr(Context, BufferPos, BufferSize);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Socket.Handle.Connect(EndPoint);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
2018-12-04 16:52:39 -08:00
|
|
|
|
catch (SocketException Exception)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Errno = ConvertError((WSAError)Exception.ErrorCode);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
2018-03-11 18:05:39 -07:00
|
|
|
|
}
|
2018-02-28 12:58:04 -08:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return WriteBsdResult(Context, 0, Errno);
|
2018-02-28 12:58:04 -08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// GetPeerName(u32 socket) -> (i32 ret, u32 bsd_errno, u32 addrlen, buffer<nn::socket::sockaddr_in, 0x22, 0x10> addr)
|
2018-12-04 16:52:39 -08:00
|
|
|
|
public long GetPeerName(ServiceCtx Context)
|
2018-02-28 12:58:04 -08:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
int SocketFd = Context.RequestData.ReadInt32();
|
2018-02-28 12:58:04 -08:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
(long BufferPos, long BufferSize) = Context.Request.GetBufferType0x22();
|
2018-08-24 10:20:42 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
LinuxError Errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket Socket = RetrieveSocket(SocketFd);
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (Socket != null)
|
2018-03-11 18:05:39 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Errno = LinuxError.SUCCESS;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
WriteSockAddr(Context, BufferPos, Socket, true);
|
|
|
|
|
WriteBsdResult(Context, 0, Errno);
|
|
|
|
|
Context.ResponseData.Write(0x10);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return WriteBsdResult(Context, 0, Errno);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetSockName(u32 socket) -> (i32 ret, u32 bsd_errno, u32 addrlen, buffer<nn::socket::sockaddr_in, 0x22, 0x10> addr)
|
2018-12-04 16:52:39 -08:00
|
|
|
|
public long GetSockName(ServiceCtx Context)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
int SocketFd = Context.RequestData.ReadInt32();
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
(long BufferPos, long BufferSize) = Context.Request.GetBufferType0x22();
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
LinuxError Errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket Socket = RetrieveSocket(SocketFd);
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (Socket != null)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Errno = LinuxError.SUCCESS;
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
WriteSockAddr(Context, BufferPos, Socket, false);
|
|
|
|
|
WriteBsdResult(Context, 0, Errno);
|
|
|
|
|
Context.ResponseData.Write(0x10);
|
2018-03-11 18:05:39 -07:00
|
|
|
|
}
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return WriteBsdResult(Context, 0, Errno);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetSockOpt(u32 socket, u32 level, u32 option_name) -> (i32 ret, u32 bsd_errno, u32, buffer<unknown, 0x22, 0>)
|
2018-12-04 16:52:39 -08:00
|
|
|
|
public long GetSockOpt(ServiceCtx Context)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
int SocketFd = Context.RequestData.ReadInt32();
|
|
|
|
|
int Level = Context.RequestData.ReadInt32();
|
|
|
|
|
int OptionName = Context.RequestData.ReadInt32();
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
(long BufferPosition, long BufferSize) = Context.Request.GetBufferType0x22();
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
LinuxError Errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket Socket = RetrieveSocket(SocketFd);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (Socket != null)
|
2018-03-11 18:05:39 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Errno = LinuxError.ENOPROTOOPT;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (Level == 0xFFFF)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Errno = HandleGetSocketOption(Context, Socket, (SocketOptionName)OptionName, BufferPosition, BufferSize);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported GetSockOpt Level: {(SocketOptionLevel)Level}");
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
2018-03-11 18:05:39 -07:00
|
|
|
|
}
|
2018-02-28 12:58:04 -08:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return WriteBsdResult(Context, 0, Errno);
|
2018-02-28 12:58:04 -08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// Listen(u32 socket, u32 backlog) -> (i32 ret, u32 bsd_errno)
|
2018-12-04 16:52:39 -08:00
|
|
|
|
public long Listen(ServiceCtx Context)
|
2018-02-28 12:58:04 -08:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
int SocketFd = Context.RequestData.ReadInt32();
|
|
|
|
|
int Backlog = Context.RequestData.ReadInt32();
|
2018-02-28 12:58:04 -08:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
LinuxError Errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket Socket = RetrieveSocket(SocketFd);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (Socket != null)
|
2018-03-11 18:05:39 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Errno = LinuxError.SUCCESS;
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-10-20 15:08:58 -07:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Socket.Handle.Listen(Backlog);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
2018-12-04 16:52:39 -08:00
|
|
|
|
catch (SocketException Exception)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Errno = ConvertError((WSAError)Exception.ErrorCode);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
2018-03-11 18:05:39 -07:00
|
|
|
|
}
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return WriteBsdResult(Context, 0, Errno);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ioctl(u32 fd, u32 request, u32 bufcount, buffer<unknown, 0x21, 0>, buffer<unknown, 0x21, 0>, buffer<unknown, 0x21, 0>, buffer<unknown, 0x21, 0>) -> (i32 ret, u32 bsd_errno, buffer<unknown, 0x22, 0>, buffer<unknown, 0x22, 0>, buffer<unknown, 0x22, 0>, buffer<unknown, 0x22, 0>)
|
2018-12-04 16:52:39 -08:00
|
|
|
|
public long Ioctl(ServiceCtx Context)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
int SocketFd = Context.RequestData.ReadInt32();
|
|
|
|
|
BsdIoctl Cmd = (BsdIoctl)Context.RequestData.ReadInt32();
|
|
|
|
|
int BufferCount = Context.RequestData.ReadInt32();
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
LinuxError Errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket Socket = RetrieveSocket(SocketFd);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (Socket != null)
|
2018-03-11 18:05:39 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
switch (Cmd)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
|
|
|
|
case BsdIoctl.AtMark:
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Errno = LinuxError.SUCCESS;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
(long BufferPosition, long BufferSize) = Context.Request.GetBufferType0x22();
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
|
|
|
|
// FIXME: OOB not implemented.
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Context.Memory.WriteInt32(BufferPosition, 0);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Errno = LinuxError.EOPNOTSUPP;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported Ioctl Cmd: {Cmd}");
|
2018-10-20 15:08:58 -07:00
|
|
|
|
break;
|
|
|
|
|
}
|
2018-03-11 18:05:39 -07:00
|
|
|
|
}
|
2018-02-28 12:58:04 -08:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return WriteBsdResult(Context, 0, Errno);
|
2018-02-28 12:58:04 -08:00
|
|
|
|
}
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// Fcntl(u32 socket, u32 cmd, u32 arg) -> (i32 ret, u32 bsd_errno)
|
2018-12-04 16:52:39 -08:00
|
|
|
|
public long Fcntl(ServiceCtx Context)
|
2018-03-11 18:05:39 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
int SocketFd = Context.RequestData.ReadInt32();
|
|
|
|
|
int Cmd = Context.RequestData.ReadInt32();
|
|
|
|
|
int Arg = Context.RequestData.ReadInt32();
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
int Result = 0;
|
|
|
|
|
LinuxError Errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket Socket = RetrieveSocket(SocketFd);
|
2018-04-05 21:01:52 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (Socket != null)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Errno = LinuxError.SUCCESS;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (Cmd == 0x3)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Result = !Socket.Handle.Blocking ? 0x800 : 0;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
2018-12-04 16:52:39 -08:00
|
|
|
|
else if (Cmd == 0x4 && Arg == 0x800)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Socket.Handle.Blocking = false;
|
|
|
|
|
Result = 0;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Errno = LinuxError.EOPNOTSUPP;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return WriteBsdResult(Context, Result, Errno);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
2018-04-05 21:01:52 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
private LinuxError HandleGetSocketOption(ServiceCtx Context, BsdSocket Socket, SocketOptionName OptionName, long OptionValuePosition, long OptionValueSize)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-03-11 18:05:39 -07:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
byte[] OptionValue = new byte[OptionValueSize];
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
switch (OptionName)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
|
|
|
|
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:
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Socket.Handle.GetSocketOption(SocketOptionLevel.Socket, OptionName, OptionValue);
|
|
|
|
|
Context.Memory.WriteBytes(OptionValuePosition, OptionValue);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
|
|
|
|
return LinuxError.SUCCESS;
|
|
|
|
|
|
|
|
|
|
case (SocketOptionName)0x200:
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Socket.Handle.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, OptionValue);
|
|
|
|
|
Context.Memory.WriteBytes(OptionValuePosition, OptionValue);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
|
|
|
|
return LinuxError.SUCCESS;
|
|
|
|
|
|
|
|
|
|
default:
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported SetSockOpt OptionName: {OptionName}");
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
|
|
|
|
return LinuxError.EOPNOTSUPP;
|
|
|
|
|
}
|
2018-03-11 18:05:39 -07:00
|
|
|
|
}
|
2018-12-04 16:52:39 -08:00
|
|
|
|
catch (SocketException Exception)
|
2018-03-11 18:05:39 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return ConvertError((WSAError)Exception.ErrorCode);
|
2018-03-11 18:05:39 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
private LinuxError HandleSetSocketOption(ServiceCtx Context, BsdSocket Socket, SocketOptionName OptionName, long OptionValuePosition, long OptionValueSize)
|
2018-08-24 10:20:42 -07:00
|
|
|
|
{
|
2018-10-20 15:08:58 -07:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
switch (OptionName)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
|
|
|
|
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:
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Socket.Handle.SetSocketOption(SocketOptionLevel.Socket, OptionName, Context.Memory.ReadInt32(OptionValuePosition));
|
2018-08-24 10:20:42 -07:00
|
|
|
|
|
2018-10-20 15:08:58 -07:00
|
|
|
|
return LinuxError.SUCCESS;
|
2018-08-24 10:20:42 -07:00
|
|
|
|
|
2018-10-20 15:08:58 -07:00
|
|
|
|
case (SocketOptionName)0x200:
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Socket.Handle.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, Context.Memory.ReadInt32(OptionValuePosition));
|
2018-08-24 10:20:42 -07:00
|
|
|
|
|
2018-10-20 15:08:58 -07:00
|
|
|
|
return LinuxError.SUCCESS;
|
2018-08-24 10:20:42 -07:00
|
|
|
|
|
2018-10-20 15:08:58 -07:00
|
|
|
|
case SocketOptionName.Linger:
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Socket.Handle.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger,
|
|
|
|
|
new LingerOption(Context.Memory.ReadInt32(OptionValuePosition) != 0, Context.Memory.ReadInt32(OptionValuePosition + 4)));
|
2018-08-24 10:20:42 -07:00
|
|
|
|
|
2018-10-20 15:08:58 -07:00
|
|
|
|
return LinuxError.SUCCESS;
|
|
|
|
|
|
|
|
|
|
default:
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported SetSockOpt OptionName: {OptionName}");
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
|
|
|
|
return LinuxError.EOPNOTSUPP;
|
|
|
|
|
}
|
2018-08-24 10:20:42 -07:00
|
|
|
|
}
|
2018-12-04 16:52:39 -08:00
|
|
|
|
catch (SocketException Exception)
|
2018-08-24 10:20:42 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return ConvertError((WSAError)Exception.ErrorCode);
|
2018-08-24 10:20:42 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// SetSockOpt(u32 socket, u32 level, u32 option_name, buffer<unknown, 0x21, 0> option_value) -> (i32 ret, u32 bsd_errno)
|
2018-12-04 16:52:39 -08:00
|
|
|
|
public long SetSockOpt(ServiceCtx Context)
|
2018-08-24 10:20:42 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
int SocketFd = Context.RequestData.ReadInt32();
|
|
|
|
|
int Level = Context.RequestData.ReadInt32();
|
|
|
|
|
int OptionName = Context.RequestData.ReadInt32();
|
2018-08-24 10:20:42 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
(long BufferPos, long BufferSize) = Context.Request.GetBufferType0x21();
|
2018-08-24 10:20:42 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
LinuxError Errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket Socket = RetrieveSocket(SocketFd);
|
2018-08-24 10:20:42 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (Socket != null)
|
2018-08-24 10:20:42 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Errno = LinuxError.ENOPROTOOPT;
|
2018-08-24 10:20:42 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (Level == 0xFFFF)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Errno = HandleSetSocketOption(Context, Socket, (SocketOptionName)OptionName, BufferPos, BufferSize);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported SetSockOpt Level: {(SocketOptionLevel)Level}");
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
2018-08-24 10:20:42 -07:00
|
|
|
|
}
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return WriteBsdResult(Context, 0, Errno);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Shutdown(u32 socket, u32 how) -> (i32 ret, u32 bsd_errno)
|
2018-12-04 16:52:39 -08:00
|
|
|
|
public long Shutdown(ServiceCtx Context)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
int SocketFd = Context.RequestData.ReadInt32();
|
|
|
|
|
int How = Context.RequestData.ReadInt32();
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
LinuxError Errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket Socket = RetrieveSocket(SocketFd);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (Socket != null)
|
2018-08-24 10:20:42 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Errno = LinuxError.EINVAL;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (How >= 0 && How <= 2)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Errno = LinuxError.SUCCESS;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Socket.Handle.Shutdown((SocketShutdown)How);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
2018-12-04 16:52:39 -08:00
|
|
|
|
catch (SocketException Exception)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Errno = ConvertError((WSAError)Exception.ErrorCode);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-24 10:20:42 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return WriteBsdResult(Context, 0, Errno);
|
2018-08-24 10:20:42 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// ShutdownAllSockets(u32 how) -> (i32 ret, u32 bsd_errno)
|
2018-12-04 16:52:39 -08:00
|
|
|
|
public long ShutdownAllSockets(ServiceCtx Context)
|
2018-03-11 18:05:39 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
int How = Context.RequestData.ReadInt32();
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
LinuxError Errno = LinuxError.EINVAL;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (How >= 0 && How <= 2)
|
2018-03-11 18:05:39 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Errno = LinuxError.SUCCESS;
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
foreach (BsdSocket Socket in Sockets)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (Socket != null)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Socket.Handle.Shutdown((SocketShutdown)How);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
2018-12-04 16:52:39 -08:00
|
|
|
|
catch (SocketException Exception)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Errno = ConvertError((WSAError)Exception.ErrorCode);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-11 18:05:39 -07:00
|
|
|
|
}
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return WriteBsdResult(Context, 0, Errno);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write(u32 socket, buffer<i8, 0x21, 0> message) -> (i32 ret, u32 bsd_errno)
|
2018-12-04 16:52:39 -08:00
|
|
|
|
public long Write(ServiceCtx Context)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
int SocketFd = Context.RequestData.ReadInt32();
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
(long SendPosition, long SendSize) = Context.Request.GetBufferType0x21();
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
LinuxError Errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket Socket = RetrieveSocket(SocketFd);
|
|
|
|
|
int Result = -1;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (Socket != null)
|
2018-03-11 18:05:39 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
byte[] SendBuffer = Context.Memory.ReadBytes(SendPosition, SendSize);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Result = Socket.Handle.Send(SendBuffer);
|
|
|
|
|
Errno = SetResultErrno(Socket.Handle, Result);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
2018-12-04 16:52:39 -08:00
|
|
|
|
catch (SocketException Exception)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Errno = ConvertError((WSAError)Exception.ErrorCode);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
2018-03-11 18:05:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return WriteBsdResult(Context, Result, Errno);
|
2018-03-11 18:05:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// Read(u32 socket) -> (i32 ret, u32 bsd_errno, buffer<i8, 0x22, 0> message)
|
2018-12-04 16:52:39 -08:00
|
|
|
|
public long Read(ServiceCtx Context)
|
2018-03-11 18:05:39 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
int SocketFd = Context.RequestData.ReadInt32();
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
(long ReceivePosition, long ReceiveLength) = Context.Request.GetBufferType0x22();
|
2018-04-05 21:01:52 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
LinuxError Errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket Socket = RetrieveSocket(SocketFd);
|
|
|
|
|
int Result = -1;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (Socket != null)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
byte[] ReceivedBuffer = new byte[ReceiveLength];
|
2018-03-11 18:05:39 -07:00
|
|
|
|
|
2018-10-20 15:08:58 -07:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Result = Socket.Handle.Receive(ReceivedBuffer);
|
|
|
|
|
Errno = SetResultErrno(Socket.Handle, Result);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
2018-12-04 16:52:39 -08:00
|
|
|
|
catch (SocketException Exception)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Errno = ConvertError((WSAError)Exception.ErrorCode);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
2018-03-11 18:05:39 -07:00
|
|
|
|
}
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return WriteBsdResult(Context, Result, Errno);
|
2018-03-11 18:05:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// Close(u32 socket) -> (i32 ret, u32 bsd_errno)
|
2018-12-04 16:52:39 -08:00
|
|
|
|
public long Close(ServiceCtx Context)
|
2018-03-11 18:05:39 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
int SocketFd = Context.RequestData.ReadInt32();
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
LinuxError Errno = LinuxError.EBADF;
|
|
|
|
|
BsdSocket Socket = RetrieveSocket(SocketFd);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (Socket != null)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Socket.Handle.Close();
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Sockets[SocketFd] = null;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Errno = LinuxError.SUCCESS;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return WriteBsdResult(Context, 0, Errno);
|
2018-03-11 18:05:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-20 15:08:58 -07:00
|
|
|
|
// DuplicateSocket(u32 socket, u64 reserved) -> (i32 ret, u32 bsd_errno)
|
2018-12-04 16:52:39 -08:00
|
|
|
|
public long DuplicateSocket(ServiceCtx Context)
|
2018-03-11 18:05:39 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
int SocketFd = Context.RequestData.ReadInt32();
|
|
|
|
|
ulong Reserved = Context.RequestData.ReadUInt64();
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
LinuxError Errno = LinuxError.ENOENT;
|
|
|
|
|
int NewSockFd = -1;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (IsPrivileged)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Errno = LinuxError.EBADF;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
BsdSocket OldSocket = RetrieveSocket(SocketFd);
|
2018-10-20 15:08:58 -07:00
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
if (OldSocket != null)
|
2018-10-20 15:08:58 -07:00
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
|
Sockets.Add(OldSocket);
|
|
|
|
|
NewSockFd = Sockets.Count - 1;
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
|
return WriteBsdResult(Context, NewSockFd, Errno);
|
2018-03-11 18:05:39 -07:00
|
|
|
|
}
|
2018-02-27 19:31:52 -08:00
|
|
|
|
}
|
2018-10-20 15:08:58 -07:00
|
|
|
|
}
|