Ryujinx/Ryujinx.HLE/HOS/Services/Bsd/IClient.cs

1194 lines
44 KiB
C#
Raw Normal View History

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
{
2018-12-01 21:01:59 +01:00
private static Dictionary<WsaError, LinuxError> _errorMap = new Dictionary<WsaError, LinuxError>
{
// WSAEINTR
2018-12-01 18:15:17 +01:00
{WsaError.Interrupted, LinuxError.Intr},
// WSAEWOULDBLOCK
2018-12-01 18:15:17 +01:00
{WsaError.WouldBlock, LinuxError.WouldBlock},
// WSAEINPROGRESS
2018-12-01 18:15:17 +01:00
{WsaError.InProgress, LinuxError.InProgress},
// WSAEALREADY
2018-12-01 18:15:17 +01:00
{WsaError.AlreadyInProgress, LinuxError.Already},
// WSAENOTSOCK
2018-12-01 18:15:17 +01:00
{WsaError.NotSocket, LinuxError.NotSock},
// WSAEDESTADDRREQ
2018-12-01 18:15:17 +01:00
{WsaError.DestinationAddressRequired, LinuxError.DestAddrReq},
// WSAEMSGSIZE
2018-12-01 18:15:17 +01:00
{WsaError.MessageSize, LinuxError.MsgSize},
// WSAEPROTOTYPE
2018-12-01 18:15:17 +01:00
{WsaError.ProtocolType, LinuxError.ProtoType},
// WSAENOPROTOOPT
2018-12-01 18:15:17 +01:00
{WsaError.ProtocolOption, LinuxError.NoProtoOpt},
// WSAEPROTONOSUPPORT
2018-12-01 18:15:17 +01:00
{WsaError.ProtocolNotSupported, LinuxError.ProtoNoSupport},
// WSAESOCKTNOSUPPORT
2018-12-01 18:15:17 +01:00
{WsaError.SocketNotSupported, LinuxError.SocktNoSupport},
// WSAEOPNOTSUPP
2018-12-01 18:15:17 +01:00
{WsaError.OperationNotSupported, LinuxError.OpNotSupp},
// WSAEPFNOSUPPORT
2018-12-01 18:15:17 +01:00
{WsaError.ProtocolFamilyNotSupported, LinuxError.PfNoSupport},
// WSAEAFNOSUPPORT
2018-12-01 18:15:17 +01:00
{WsaError.AddressFamilyNotSupported, LinuxError.AfNoSupport},
// WSAEADDRINUSE
2018-12-01 18:15:17 +01:00
{WsaError.AddressAlreadyInUse, LinuxError.AddrInUse},
// WSAEADDRNOTAVAIL
2018-12-01 18:15:17 +01:00
{WsaError.AddressNotAvailable, LinuxError.AddrNotAvail},
// WSAENETDOWN
2018-12-01 18:15:17 +01:00
{WsaError.NetworkDown, LinuxError.NetDown},
// WSAENETUNREACH
2018-12-01 18:15:17 +01:00
{WsaError.NetworkUnreachable, LinuxError.NetUnReach},
// WSAENETRESET
2018-12-01 18:15:17 +01:00
{WsaError.NetworkReset, LinuxError.NetReset},
// WSAECONNABORTED
2018-12-01 18:15:17 +01:00
{WsaError.ConnectionAborted, LinuxError.ConnAborted},
// WSAECONNRESET
2018-12-01 18:15:17 +01:00
{WsaError.ConnectionReset, LinuxError.ConnReset},
// WSAENOBUFS
2018-12-01 18:15:17 +01:00
{WsaError.NoBufferSpaceAvailable, LinuxError.NoBufs},
// WSAEISCONN
2018-12-01 18:15:17 +01:00
{WsaError.IsConnected, LinuxError.IsConn},
// WSAENOTCONN
2018-12-01 18:15:17 +01:00
{WsaError.NotConnected, LinuxError.NotConn},
// WSAESHUTDOWN
2018-12-01 18:15:17 +01:00
{WsaError.Shutdown, LinuxError.Shutdown},
// WSAETOOMANYREFS
2018-12-01 18:15:17 +01:00
{WsaError.TooManyReferences, LinuxError.TooManyRefs},
// WSAETIMEDOUT
2018-12-01 18:15:17 +01:00
{WsaError.TimedOut, LinuxError.TimedOut},
// WSAECONNREFUSED
2018-12-01 18:15:17 +01:00
{WsaError.ConnectionRefused, LinuxError.ConnRefused},
// WSAELOOP
2018-12-01 18:15:17 +01:00
{WsaError.Loop, LinuxError.Loop},
// WSAENAMETOOLONG
2018-12-01 18:15:17 +01:00
{WsaError.NameTooLong, LinuxError.NameTooLong},
// WSAEHOSTDOWN
2018-12-01 18:15:17 +01:00
{WsaError.HostDown, LinuxError.HostDown},
// WSAEHOSTUNREACH
2018-12-01 18:15:17 +01:00
{WsaError.HostUnreachable, LinuxError.HostUnReach},
// WSAENOTEMPTY
2018-12-01 18:15:17 +01:00
{WsaError.NotEmpty, LinuxError.NotEmpty},
// WSAEUSERS
2018-12-01 18:15:17 +01:00
{WsaError.UserQuota, LinuxError.Users},
// WSAEDQUOT
2018-12-01 18:15:17 +01:00
{WsaError.DiskQuota, LinuxError.DQuot},
// WSAESTALE
2018-12-01 18:15:17 +01:00
{WsaError.Stale, LinuxError.Stale},
// WSAEREMOTE
2018-12-01 18:15:17 +01:00
{WsaError.Remote, LinuxError.Remote},
// WSAEINVAL
2018-12-01 18:15:17 +01:00
{WsaError.InvalidArgument, LinuxError.InVal},
// WSAEFAULT
2018-12-01 18:15:17 +01:00
{WsaError.Fault, LinuxError.Fault},
// NOERROR
{0, 0}
};
2018-12-01 18:15:17 +01:00
private Dictionary<int, ServiceProcessRequest> _commands;
2018-12-01 18:15:17 +01:00
public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => _commands;
2018-12-01 21:01:59 +01:00
private bool _isPrivileged;
2018-12-01 21:01:59 +01:00
private List<BsdSocket> _sockets = new List<BsdSocket>();
2018-12-01 21:01:59 +01:00
public IClient(bool isPrivileged)
{
2018-12-01 18:15:17 +01:00
_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 },
};
2018-12-01 21:01:59 +01:00
this._isPrivileged = isPrivileged;
}
2018-12-01 21:01:59 +01:00
private LinuxError ConvertError(WsaError errorCode)
{
2018-12-01 21:01:59 +01:00
LinuxError errno;
2018-12-01 21:01:59 +01:00
if (!_errorMap.TryGetValue(errorCode, out errno))
{
2018-12-01 21:01:59 +01:00
errno = (LinuxError)errorCode;
}
2018-12-01 21:01:59 +01:00
return errno;
}
2018-12-01 21:01:59 +01:00
private long WriteWinSock2Error(ServiceCtx context, WsaError errorCode)
{
2018-12-01 21:01:59 +01:00
return WriteBsdResult(context, -1, ConvertError(errorCode));
}
2018-12-01 21:01:59 +01:00
private long WriteBsdResult(ServiceCtx context, int result, LinuxError errorCode = 0)
{
2018-12-01 21:01:59 +01:00
if (errorCode != LinuxError.Success)
{
2018-12-01 21:01:59 +01:00
result = -1;
}
2018-12-01 21:01:59 +01:00
context.ResponseData.Write(result);
context.ResponseData.Write((int)errorCode);
return 0;
}
2018-12-01 21:01:59 +01:00
private BsdSocket RetrieveSocket(int socketFd)
{
2018-12-01 21:01:59 +01:00
if (socketFd >= 0 && _sockets.Count > socketFd)
{
2018-12-01 21:01:59 +01:00
return _sockets[socketFd];
}
return null;
}
2018-12-01 21:01:59 +01:00
private LinuxError SetResultErrno(Socket socket, int result)
{
2018-12-01 21:01:59 +01:00
return result == 0 && !socket.Blocking ? LinuxError.WouldBlock : LinuxError.Success;
}
2018-12-01 21:01:59 +01:00
private AddressFamily ConvertFromBsd(int domain)
{
2018-12-01 21:01:59 +01:00
if (domain == 2)
{
return AddressFamily.InterNetwork;
}
// FIXME: AF_ROUTE ignored, is that really needed?
return AddressFamily.Unknown;
}
2018-12-01 21:01:59 +01:00
private long SocketInternal(ServiceCtx context, bool exempt)
{
2018-12-01 21:01:59 +01:00
AddressFamily domain = (AddressFamily)context.RequestData.ReadInt32();
SocketType type = (SocketType)context.RequestData.ReadInt32();
ProtocolType protocol = (ProtocolType)context.RequestData.ReadInt32();
2018-12-01 21:01:59 +01:00
if (domain == AddressFamily.Unknown)
{
2018-12-01 21:01:59 +01:00
return WriteBsdResult(context, -1, LinuxError.ProtoNoSupport);
}
2018-12-01 21:01:59 +01:00
else if ((type == SocketType.Seqpacket || type == SocketType.Raw) && !_isPrivileged)
{
2018-12-01 21:01:59 +01:00
if (domain != AddressFamily.InterNetwork || type != SocketType.Raw || protocol != ProtocolType.Icmp)
{
2018-12-01 21:01:59 +01:00
return WriteBsdResult(context, -1, LinuxError.NoEnt);
}
}
2018-12-01 21:01:59 +01:00
BsdSocket newBsdSocket = new BsdSocket
{
2018-12-01 21:01:59 +01:00
Family = (int)domain,
Type = (int)type,
Protocol = (int)protocol,
Handle = new Socket(domain, type, protocol)
};
2018-12-01 21:01:59 +01:00
_sockets.Add(newBsdSocket);
2018-12-01 21:01:59 +01:00
if (exempt)
{
2018-12-01 21:01:59 +01:00
newBsdSocket.Handle.Disconnect(true);
}
2018-12-01 21:01:59 +01:00
return WriteBsdResult(context, _sockets.Count - 1);
}
2018-12-01 21:01:59 +01:00
private IPEndPoint ParseSockAddr(ServiceCtx context, long bufferPosition, long bufferSize)
{
2018-12-01 21:01:59 +01:00
int size = context.Memory.ReadByte(bufferPosition);
int family = context.Memory.ReadByte(bufferPosition + 1);
int port = EndianSwap.Swap16(context.Memory.ReadUInt16(bufferPosition + 2));
2018-12-01 21:01:59 +01:00
byte[] rawIp = context.Memory.ReadBytes(bufferPosition + 4, 4);
2018-12-01 21:01:59 +01:00
return new IPEndPoint(new IPAddress(rawIp), port);
}
2018-12-01 21:01:59 +01:00
private void WriteSockAddr(ServiceCtx context, long bufferPosition, IPEndPoint endPoint)
{
2018-12-01 21:01:59 +01: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-12-01 21:01:59 +01:00
private void WriteSockAddr(ServiceCtx context, long bufferPosition, BsdSocket socket, bool isRemote)
{
2018-12-01 21:01:59 +01:00
IPEndPoint endPoint = (isRemote ? socket.Handle.RemoteEndPoint : socket.Handle.LocalEndPoint) as IPEndPoint;
2018-12-01 21:01:59 +01:00
WriteSockAddr(context, bufferPosition, endPoint);
}
// Initialize(nn::socket::BsdBufferConfig config, u64 pid, u64 transferMemorySize, KObject<copy, transfer_memory>, pid) -> u32 bsd_errno
2018-12-01 21:01:59 +01:00
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
2018-12-01 21:01:59 +01:00
context.ResponseData.Write(0);
Logger.PrintStub(LogClass.ServiceBsd, "Stubbed.");
return 0;
}
// StartMonitoring(u64, pid)
2018-12-01 21:01:59 +01:00
public long StartMonitoring(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
ulong unknown0 = context.RequestData.ReadUInt64();
2018-12-01 21:01:59 +01:00
Logger.PrintStub(LogClass.ServiceBsd, $"Stubbed. Unknown0: {unknown0}");
return 0;
}
// Socket(u32 domain, u32 type, u32 protocol) -> (i32 ret, u32 bsd_errno)
2018-12-01 21:01:59 +01:00
public long Socket(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
return SocketInternal(context, false);
}
// SocketExempt(u32 domain, u32 type, u32 protocol) -> (i32 ret, u32 bsd_errno)
2018-12-01 21:01:59 +01:00
public long SocketExempt(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
return SocketInternal(context, true);
}
// Open(u32 flags, array<unknown, 0x21> path) -> (i32 ret, u32 bsd_errno)
2018-12-01 21:01:59 +01:00
public long Open(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
(long bufferPosition, long bufferSize) = context.Request.GetBufferType0x21();
2018-12-01 21:01:59 +01:00
int flags = context.RequestData.ReadInt32();
2018-12-01 21:01:59 +01:00
byte[] rawPath = context.Memory.ReadBytes(bufferPosition, bufferSize);
string path = Encoding.ASCII.GetString(rawPath);
2018-12-01 21:01:59 +01:00
WriteBsdResult(context, -1, LinuxError.OpNotSupp);
2018-12-01 21:01:59 +01:00
Logger.PrintStub(LogClass.ServiceBsd, $"Stubbed. Path: {path} - " +
$"Flags: {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)
2018-12-01 21:01:59 +01:00
public long Select(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
WriteBsdResult(context, -1, LinuxError.OpNotSupp);
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-01 21:01:59 +01:00
public long Poll(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
int fdsCount = context.RequestData.ReadInt32();
int timeout = context.RequestData.ReadInt32();
2018-12-01 21:01:59 +01:00
(long bufferPosition, long bufferSize) = context.Request.GetBufferType0x21();
2018-12-01 21:01:59 +01:00
if (timeout < -1 || fdsCount < 0 || (fdsCount * 8) > bufferSize)
{
2018-12-01 21:01:59 +01:00
return WriteBsdResult(context, -1, LinuxError.InVal);
}
2018-12-01 21:01:59 +01:00
PollEvent[] events = new PollEvent[fdsCount];
2018-12-01 21:01:59 +01:00
for (int i = 0; i < fdsCount; i++)
{
2018-12-01 21:01:59 +01:00
int socketFd = context.Memory.ReadInt32(bufferPosition + i * 8);
2018-12-01 21:01:59 +01:00
BsdSocket socket = RetrieveSocket(socketFd);
2018-12-01 21:01:59 +01:00
if (socket == null)
{
2018-12-01 21:01:59 +01:00
return WriteBsdResult(context, -1, LinuxError.BadF);
}
2018-12-01 21:01:59 +01: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-12-01 21:01:59 +01:00
events[i] = new PollEvent(socketFd, socket, inputEvents, outputEvents);
}
2018-12-01 21:01:59 +01:00
List<Socket> readEvents = new List<Socket>();
List<Socket> writeEvents = new List<Socket>();
List<Socket> errorEvents = new List<Socket>();
2018-12-01 21:01:59 +01:00
foreach (PollEvent Event in events)
{
2018-12-01 21:01:59 +01:00
bool isValidEvent = false;
if ((Event.InputEvents & PollEvent.EventTypeMask.Input) != 0)
{
2018-12-01 21:01:59 +01:00
readEvents.Add(Event.Socket.Handle);
errorEvents.Add(Event.Socket.Handle);
2018-12-01 21:01:59 +01:00
isValidEvent = true;
}
if ((Event.InputEvents & PollEvent.EventTypeMask.UrgentInput) != 0)
{
2018-12-01 21:01:59 +01:00
readEvents.Add(Event.Socket.Handle);
errorEvents.Add(Event.Socket.Handle);
2018-12-01 21:01:59 +01:00
isValidEvent = true;
}
if ((Event.InputEvents & PollEvent.EventTypeMask.Output) != 0)
{
2018-12-01 21:01:59 +01:00
writeEvents.Add(Event.Socket.Handle);
errorEvents.Add(Event.Socket.Handle);
2018-12-01 21:01:59 +01:00
isValidEvent = true;
}
if ((Event.InputEvents & PollEvent.EventTypeMask.Error) != 0)
{
2018-12-01 21:01:59 +01:00
errorEvents.Add(Event.Socket.Handle);
isValidEvent = true;
}
2018-12-01 21:01:59 +01:00
if (!isValidEvent)
{
Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported Poll input event type: {Event.InputEvents}");
2018-12-01 21:01:59 +01:00
return WriteBsdResult(context, -1, LinuxError.InVal);
}
}
try
{
2018-12-01 21:01:59 +01:00
System.Net.Sockets.Socket.Select(readEvents, writeEvents, errorEvents, timeout);
}
2018-12-01 21:01:59 +01:00
catch (SocketException exception)
{
2018-12-01 21:01:59 +01:00
return WriteWinSock2Error(context, (WsaError)exception.ErrorCode);
}
2018-12-01 21:01:59 +01:00
for (int i = 0; i < fdsCount; i++)
{
2018-12-01 21:01:59 +01:00
PollEvent Event = events[i];
context.Memory.WriteInt32(bufferPosition + i * 8, Event.SocketFd);
context.Memory.WriteInt16(bufferPosition + i * 8 + 4, (short)Event.InputEvents);
2018-12-01 21:01:59 +01:00
PollEvent.EventTypeMask outputEvents = 0;
2018-12-01 21:01:59 +01:00
Socket socket = Event.Socket.Handle;
2018-12-01 21:01:59 +01:00
if (errorEvents.Contains(socket))
{
2018-12-01 21:01:59 +01:00
outputEvents |= PollEvent.EventTypeMask.Error;
2018-12-01 21:01:59 +01:00
if (!socket.Connected || !socket.IsBound)
{
2018-12-01 21:01:59 +01:00
outputEvents |= PollEvent.EventTypeMask.Disconnected;
}
}
2018-12-01 21:01:59 +01:00
if (readEvents.Contains(socket))
{
if ((Event.InputEvents & PollEvent.EventTypeMask.Input) != 0)
{
2018-12-01 21:01:59 +01:00
outputEvents |= PollEvent.EventTypeMask.Input;
}
}
2018-12-01 21:01:59 +01:00
if (writeEvents.Contains(socket))
{
2018-12-01 21:01:59 +01:00
outputEvents |= PollEvent.EventTypeMask.Output;
}
2018-12-01 21:01:59 +01:00
context.Memory.WriteInt16(bufferPosition + i * 8 + 6, (short)outputEvents);
}
2018-12-01 21:01:59 +01:00
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>)
2018-12-01 21:01:59 +01:00
public long Sysctl(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
WriteBsdResult(context, -1, LinuxError.OpNotSupp);
Logger.PrintStub(LogClass.ServiceBsd, $"Stubbed.");
return 0;
}
// Recv(u32 socket, u32 flags) -> (i32 ret, u32 bsd_errno, array<i8, 0x22> message)
2018-12-01 21:01:59 +01:00
public long Recv(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
int socketFd = context.RequestData.ReadInt32();
SocketFlags socketFlags = (SocketFlags)context.RequestData.ReadInt32();
2018-12-01 21:01:59 +01:00
(long receivePosition, long receiveLength) = context.Request.GetBufferType0x22();
2018-12-01 21:01:59 +01:00
LinuxError errno = LinuxError.BadF;
BsdSocket socket = RetrieveSocket(socketFd);
int result = -1;
2018-12-01 21:01:59 +01:00
if (socket != null)
{
2018-12-01 21:01:59 +01:00
if (socketFlags != SocketFlags.None && (socketFlags & SocketFlags.OutOfBand) == 0
&& (socketFlags & SocketFlags.Peek) == 0)
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported Recv flags: {socketFlags}");
return WriteBsdResult(context, -1, LinuxError.OpNotSupp);
}
2018-12-01 21:01:59 +01:00
byte[] receivedBuffer = new byte[receiveLength];
try
{
2018-12-01 21:01:59 +01:00
result = socket.Handle.Receive(receivedBuffer, socketFlags);
errno = SetResultErrno(socket.Handle, result);
2018-12-01 21:01:59 +01:00
context.Memory.WriteBytes(receivePosition, receivedBuffer);
}
2018-12-01 21:01:59 +01:00
catch (SocketException exception)
{
2018-12-01 21:01:59 +01:00
errno = ConvertError((WsaError)exception.ErrorCode);
}
}
2018-12-01 21:01:59 +01:00
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>)
2018-12-01 21:01:59 +01:00
public long RecvFrom(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
int socketFd = context.RequestData.ReadInt32();
SocketFlags socketFlags = (SocketFlags)context.RequestData.ReadInt32();
2018-12-01 21:01:59 +01:00
(long receivePosition, long receiveLength) = context.Request.GetBufferType0x22();
(long sockAddrInPosition, long sockAddrInSize) = context.Request.GetBufferType0x21();
(long sockAddrOutPosition, long sockAddrOutSize) = context.Request.GetBufferType0x22(1);
2018-12-01 21:01:59 +01:00
LinuxError errno = LinuxError.BadF;
BsdSocket socket = RetrieveSocket(socketFd);
int result = -1;
2018-12-01 21:01:59 +01:00
if (socket != null)
{
2018-12-01 21:01:59 +01:00
if (socketFlags != SocketFlags.None && (socketFlags & SocketFlags.OutOfBand) == 0
&& (socketFlags & SocketFlags.Peek) == 0)
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported Recv flags: {socketFlags}");
2018-12-01 21:01:59 +01:00
return WriteBsdResult(context, -1, LinuxError.OpNotSupp);
}
2018-12-01 21:01:59 +01:00
byte[] receivedBuffer = new byte[receiveLength];
EndPoint endPoint = ParseSockAddr(context, sockAddrInPosition, sockAddrInSize);
try
{
2018-12-01 21:01:59 +01:00
result = socket.Handle.ReceiveFrom(receivedBuffer, receivedBuffer.Length, socketFlags, ref endPoint);
errno = SetResultErrno(socket.Handle, result);
2018-12-01 21:01:59 +01:00
context.Memory.WriteBytes(receivePosition, receivedBuffer);
WriteSockAddr(context, sockAddrOutPosition, (IPEndPoint)endPoint);
}
2018-12-01 21:01:59 +01:00
catch (SocketException exception)
{
2018-12-01 21:01:59 +01:00
errno = ConvertError((WsaError)exception.ErrorCode);
}
}
2018-12-01 21:01:59 +01:00
return WriteBsdResult(context, result, errno);
}
// Send(u32 socket, u32 flags, buffer<i8, 0x21, 0>) -> (i32 ret, u32 bsd_errno)
2018-12-01 21:01:59 +01:00
public long Send(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
int socketFd = context.RequestData.ReadInt32();
SocketFlags socketFlags = (SocketFlags)context.RequestData.ReadInt32();
2018-12-01 21:01:59 +01:00
(long sendPosition, long sendSize) = context.Request.GetBufferType0x21();
2018-12-01 21:01:59 +01:00
LinuxError errno = LinuxError.BadF;
BsdSocket socket = RetrieveSocket(socketFd);
int result = -1;
2018-12-01 21:01:59 +01:00
if (socket != null)
{
2018-12-01 21:01:59 +01:00
if (socketFlags != SocketFlags.None && socketFlags != SocketFlags.OutOfBand
&& socketFlags != SocketFlags.Peek && socketFlags != SocketFlags.DontRoute)
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported Send flags: {socketFlags}");
2018-12-01 21:01:59 +01:00
return WriteBsdResult(context, -1, LinuxError.OpNotSupp);
}
2018-12-01 21:01:59 +01:00
byte[] sendBuffer = context.Memory.ReadBytes(sendPosition, sendSize);
try
{
2018-12-01 21:01:59 +01:00
result = socket.Handle.Send(sendBuffer, socketFlags);
errno = SetResultErrno(socket.Handle, result);
}
2018-12-01 21:01:59 +01:00
catch (SocketException exception)
{
2018-12-01 21:01:59 +01:00
errno = ConvertError((WsaError)exception.ErrorCode);
}
}
2018-12-01 21:01:59 +01:00
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)
2018-12-01 21:01:59 +01:00
public long SendTo(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
int socketFd = context.RequestData.ReadInt32();
SocketFlags socketFlags = (SocketFlags)context.RequestData.ReadInt32();
2018-12-01 21:01:59 +01:00
(long sendPosition, long sendSize) = context.Request.GetBufferType0x21();
(long bufferPosition, long bufferSize) = context.Request.GetBufferType0x21(1);
2018-12-01 21:01:59 +01:00
LinuxError errno = LinuxError.BadF;
BsdSocket socket = RetrieveSocket(socketFd);
int result = -1;
2018-12-01 21:01:59 +01:00
if (socket != null)
{
2018-12-01 21:01:59 +01:00
if (socketFlags != SocketFlags.None && socketFlags != SocketFlags.OutOfBand
&& socketFlags != SocketFlags.Peek && socketFlags != SocketFlags.DontRoute)
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported Send flags: {socketFlags}");
2018-12-01 21:01:59 +01:00
return WriteBsdResult(context, -1, LinuxError.OpNotSupp);
}
2018-12-01 21:01:59 +01:00
byte[] sendBuffer = context.Memory.ReadBytes(sendPosition, sendSize);
EndPoint endPoint = ParseSockAddr(context, bufferPosition, bufferSize);
try
{
2018-12-01 21:01:59 +01:00
result = socket.Handle.SendTo(sendBuffer, sendBuffer.Length, socketFlags, endPoint);
errno = SetResultErrno(socket.Handle, result);
}
2018-12-01 21:01:59 +01:00
catch (SocketException exception)
{
2018-12-01 21:01:59 +01:00
errno = ConvertError((WsaError)exception.ErrorCode);
}
}
2018-12-01 21:01:59 +01:00
return WriteBsdResult(context, result, errno);
}
// Accept(u32 socket) -> (i32 ret, u32 bsd_errno, u32 addrlen, buffer<nn::socket::sockaddr_in, 0x22, 0x10> addr)
2018-12-01 21:01:59 +01:00
public long Accept(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
int socketFd = context.RequestData.ReadInt32();
2018-12-01 21:01:59 +01:00
(long bufferPos, long bufferSize) = context.Request.GetBufferType0x22();
2018-12-01 21:01:59 +01:00
LinuxError errno = LinuxError.BadF;
BsdSocket socket = RetrieveSocket(socketFd);
2018-12-01 21:01:59 +01:00
if (socket != null)
{
2018-12-01 21:01:59 +01:00
errno = LinuxError.Success;
2018-12-01 21:01:59 +01:00
Socket newSocket = null;
try
{
2018-12-01 21:01:59 +01:00
newSocket = socket.Handle.Accept();
}
2018-12-01 21:01:59 +01:00
catch (SocketException exception)
{
2018-12-01 21:01:59 +01:00
errno = ConvertError((WsaError)exception.ErrorCode);
}
2018-12-01 21:01:59 +01:00
if (newSocket == null && errno == LinuxError.Success)
{
2018-12-01 21:01:59 +01:00
errno = LinuxError.WouldBlock;
}
2018-12-01 21:01:59 +01:00
else if (errno == LinuxError.Success)
{
2018-12-01 21:01:59 +01:00
BsdSocket newBsdSocket = new BsdSocket
{
2018-12-01 21:01:59 +01:00
Family = (int)newSocket.AddressFamily,
Type = (int)newSocket.SocketType,
Protocol = (int)newSocket.ProtocolType,
Handle = newSocket,
};
2018-12-01 21:01:59 +01:00
_sockets.Add(newBsdSocket);
2018-12-01 21:01:59 +01:00
WriteSockAddr(context, bufferPos, newBsdSocket, true);
2018-12-01 21:01:59 +01:00
WriteBsdResult(context, _sockets.Count - 1, errno);
2018-04-06 06:06:34 +02:00
2018-12-01 21:01:59 +01:00
context.ResponseData.Write(0x10);
return 0;
}
}
2018-12-01 21:01:59 +01:00
return WriteBsdResult(context, -1, errno);
}
// Bind(u32 socket, buffer<nn::socket::sockaddr_in, 0x21, 0x10> addr) -> (i32 ret, u32 bsd_errno)
2018-12-01 21:01:59 +01:00
public long Bind(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
int socketFd = context.RequestData.ReadInt32();
2018-12-01 21:01:59 +01:00
(long bufferPos, long bufferSize) = context.Request.GetBufferType0x21();
2018-12-01 21:01:59 +01:00
LinuxError errno = LinuxError.BadF;
BsdSocket socket = RetrieveSocket(socketFd);
2018-12-01 21:01:59 +01:00
if (socket != null)
{
2018-12-01 21:01:59 +01:00
errno = LinuxError.Success;
try
{
2018-12-01 21:01:59 +01:00
IPEndPoint endPoint = ParseSockAddr(context, bufferPos, bufferSize);
2018-12-01 21:01:59 +01:00
socket.Handle.Bind(endPoint);
}
2018-12-01 21:01:59 +01:00
catch (SocketException exception)
{
2018-12-01 21:01:59 +01:00
errno = ConvertError((WsaError)exception.ErrorCode);
}
}
2018-12-01 21:01:59 +01:00
return WriteBsdResult(context, 0, errno);
}
// Connect(u32 socket, buffer<nn::socket::sockaddr_in, 0x21, 0x10>) -> (i32 ret, u32 bsd_errno)
2018-12-01 21:01:59 +01:00
public long Connect(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
int socketFd = context.RequestData.ReadInt32();
2018-12-01 21:01:59 +01:00
(long bufferPos, long bufferSize) = context.Request.GetBufferType0x21();
2018-12-01 21:01:59 +01:00
LinuxError errno = LinuxError.BadF;
BsdSocket socket = RetrieveSocket(socketFd);
2018-12-01 21:01:59 +01:00
if (socket != null)
{
2018-12-01 21:01:59 +01:00
errno = LinuxError.Success;
try
{
2018-12-01 21:01:59 +01:00
IPEndPoint endPoint = ParseSockAddr(context, bufferPos, bufferSize);
2018-12-01 21:01:59 +01:00
socket.Handle.Connect(endPoint);
}
2018-12-01 21:01:59 +01:00
catch (SocketException exception)
{
2018-12-01 21:01:59 +01:00
errno = ConvertError((WsaError)exception.ErrorCode);
}
}
2018-12-01 21:01:59 +01:00
return WriteBsdResult(context, 0, errno);
}
// GetPeerName(u32 socket) -> (i32 ret, u32 bsd_errno, u32 addrlen, buffer<nn::socket::sockaddr_in, 0x22, 0x10> addr)
2018-12-01 21:01:59 +01:00
public long GetPeerName(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
int socketFd = context.RequestData.ReadInt32();
2018-12-01 21:01:59 +01:00
(long bufferPos, long bufferSize) = context.Request.GetBufferType0x22();
2018-12-01 21:01:59 +01:00
LinuxError errno = LinuxError.BadF;
BsdSocket socket = RetrieveSocket(socketFd);
2018-12-01 21:01:59 +01:00
if (socket != null)
{
2018-12-01 21:01:59 +01:00
errno = LinuxError.Success;
2018-12-01 21:01:59 +01:00
WriteSockAddr(context, bufferPos, socket, true);
WriteBsdResult(context, 0, errno);
context.ResponseData.Write(0x10);
}
2018-12-01 21:01:59 +01:00
return WriteBsdResult(context, 0, errno);
}
// GetSockName(u32 socket) -> (i32 ret, u32 bsd_errno, u32 addrlen, buffer<nn::socket::sockaddr_in, 0x22, 0x10> addr)
2018-12-01 21:01:59 +01:00
public long GetSockName(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
int socketFd = context.RequestData.ReadInt32();
2018-12-01 21:01:59 +01:00
(long bufferPos, long bufferSize) = context.Request.GetBufferType0x22();
2018-12-01 21:01:59 +01:00
LinuxError errno = LinuxError.BadF;
BsdSocket socket = RetrieveSocket(socketFd);
2018-12-01 21:01:59 +01:00
if (socket != null)
{
2018-12-01 21:01:59 +01:00
errno = LinuxError.Success;
2018-12-01 21:01:59 +01:00
WriteSockAddr(context, bufferPos, socket, false);
WriteBsdResult(context, 0, errno);
context.ResponseData.Write(0x10);
}
2018-12-01 21:01:59 +01:00
return WriteBsdResult(context, 0, errno);
}
// GetSockOpt(u32 socket, u32 level, u32 option_name) -> (i32 ret, u32 bsd_errno, u32, buffer<unknown, 0x22, 0>)
2018-12-01 21:01:59 +01:00
public long GetSockOpt(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
int socketFd = context.RequestData.ReadInt32();
int level = context.RequestData.ReadInt32();
int optionName = context.RequestData.ReadInt32();
2018-12-01 21:01:59 +01:00
(long bufferPosition, long bufferSize) = context.Request.GetBufferType0x22();
2018-12-01 21:01:59 +01:00
LinuxError errno = LinuxError.BadF;
BsdSocket socket = RetrieveSocket(socketFd);
2018-12-01 21:01:59 +01:00
if (socket != null)
{
2018-12-01 21:01:59 +01:00
errno = LinuxError.NoProtoOpt;
2018-12-01 21:01:59 +01:00
if (level == 0xFFFF)
{
2018-12-01 21:01:59 +01:00
errno = HandleGetSocketOption(context, socket, (SocketOptionName)optionName, bufferPosition, bufferSize);
}
else
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported GetSockOpt Level: {(SocketOptionLevel)level}");
}
}
2018-12-01 21:01:59 +01:00
return WriteBsdResult(context, 0, errno);
}
// Listen(u32 socket, u32 backlog) -> (i32 ret, u32 bsd_errno)
2018-12-01 21:01:59 +01:00
public long Listen(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
int socketFd = context.RequestData.ReadInt32();
int backlog = context.RequestData.ReadInt32();
2018-12-01 21:01:59 +01:00
LinuxError errno = LinuxError.BadF;
BsdSocket socket = RetrieveSocket(socketFd);
2018-12-01 21:01:59 +01:00
if (socket != null)
{
2018-12-01 21:01:59 +01:00
errno = LinuxError.Success;
try
{
2018-12-01 21:01:59 +01:00
socket.Handle.Listen(backlog);
}
2018-12-01 21:01:59 +01:00
catch (SocketException exception)
{
2018-12-01 21:01:59 +01:00
errno = ConvertError((WsaError)exception.ErrorCode);
}
}
2018-12-01 21:01:59 +01:00
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>)
2018-12-01 21:01:59 +01:00
public long Ioctl(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
int socketFd = context.RequestData.ReadInt32();
BsdIoctl cmd = (BsdIoctl)context.RequestData.ReadInt32();
int bufferCount = context.RequestData.ReadInt32();
2018-12-01 21:01:59 +01:00
LinuxError errno = LinuxError.BadF;
BsdSocket socket = RetrieveSocket(socketFd);
2018-12-01 21:01:59 +01:00
if (socket != null)
{
2018-12-01 21:01:59 +01:00
switch (cmd)
{
case BsdIoctl.AtMark:
2018-12-01 21:01:59 +01:00
errno = LinuxError.Success;
2018-12-01 21:01:59 +01:00
(long bufferPosition, long bufferSize) = context.Request.GetBufferType0x22();
// FIXME: OOB not implemented.
2018-12-01 21:01:59 +01:00
context.Memory.WriteInt32(bufferPosition, 0);
break;
default:
2018-12-01 21:01:59 +01:00
errno = LinuxError.OpNotSupp;
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported Ioctl Cmd: {cmd}");
break;
}
}
2018-12-01 21:01:59 +01:00
return WriteBsdResult(context, 0, errno);
}
// Fcntl(u32 socket, u32 cmd, u32 arg) -> (i32 ret, u32 bsd_errno)
2018-12-01 21:01:59 +01:00
public long Fcntl(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
int socketFd = context.RequestData.ReadInt32();
int cmd = context.RequestData.ReadInt32();
int arg = context.RequestData.ReadInt32();
2018-12-01 21:01:59 +01:00
int result = 0;
LinuxError errno = LinuxError.BadF;
BsdSocket socket = RetrieveSocket(socketFd);
2018-12-01 21:01:59 +01:00
if (socket != null)
{
2018-12-01 21:01:59 +01:00
errno = LinuxError.Success;
2018-12-01 21:01:59 +01:00
if (cmd == 0x3)
{
2018-12-01 21:01:59 +01:00
result = !socket.Handle.Blocking ? 0x800 : 0;
}
2018-12-01 21:01:59 +01:00
else if (cmd == 0x4 && arg == 0x800)
{
2018-12-01 21:01:59 +01:00
socket.Handle.Blocking = false;
result = 0;
}
else
{
2018-12-01 21:01:59 +01:00
errno = LinuxError.OpNotSupp;
}
}
2018-12-01 21:01:59 +01:00
return WriteBsdResult(context, result, errno);
}
2018-12-01 21:01:59 +01:00
private LinuxError HandleGetSocketOption(ServiceCtx context, BsdSocket socket, SocketOptionName optionName, long optionValuePosition, long optionValueSize)
{
try
{
2018-12-01 21:01:59 +01:00
byte[] optionValue = new byte[optionValueSize];
2018-12-01 21:01:59 +01:00
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:
2018-12-01 21:01:59 +01:00
socket.Handle.GetSocketOption(SocketOptionLevel.Socket, optionName, optionValue);
context.Memory.WriteBytes(optionValuePosition, optionValue);
2018-12-01 18:15:17 +01:00
return LinuxError.Success;
case (SocketOptionName)0x200:
2018-12-01 21:01:59 +01:00
socket.Handle.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, optionValue);
context.Memory.WriteBytes(optionValuePosition, optionValue);
2018-12-01 18:15:17 +01:00
return LinuxError.Success;
default:
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported SetSockOpt OptionName: {optionName}");
2018-12-01 18:15:17 +01:00
return LinuxError.OpNotSupp;
}
}
2018-12-01 21:01:59 +01:00
catch (SocketException exception)
{
2018-12-01 21:01:59 +01:00
return ConvertError((WsaError)exception.ErrorCode);
}
}
2018-12-01 21:01:59 +01:00
private LinuxError HandleSetSocketOption(ServiceCtx context, BsdSocket socket, SocketOptionName optionName, long optionValuePosition, long optionValueSize)
{
try
{
2018-12-01 21:01:59 +01:00
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:
2018-12-01 21:01:59 +01:00
socket.Handle.SetSocketOption(SocketOptionLevel.Socket, optionName, context.Memory.ReadInt32(optionValuePosition));
2018-12-01 18:15:17 +01:00
return LinuxError.Success;
case (SocketOptionName)0x200:
2018-12-01 21:01:59 +01:00
socket.Handle.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, context.Memory.ReadInt32(optionValuePosition));
2018-12-01 18:15:17 +01:00
return LinuxError.Success;
case SocketOptionName.Linger:
2018-12-01 21:01:59 +01:00
socket.Handle.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger,
new LingerOption(context.Memory.ReadInt32(optionValuePosition) != 0, context.Memory.ReadInt32(optionValuePosition + 4)));
2018-12-01 18:15:17 +01:00
return LinuxError.Success;
default:
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported SetSockOpt OptionName: {optionName}");
2018-12-01 18:15:17 +01:00
return LinuxError.OpNotSupp;
}
}
2018-12-01 21:01:59 +01:00
catch (SocketException exception)
{
2018-12-01 21:01:59 +01:00
return ConvertError((WsaError)exception.ErrorCode);
}
}
// SetSockOpt(u32 socket, u32 level, u32 option_name, buffer<unknown, 0x21, 0> option_value) -> (i32 ret, u32 bsd_errno)
2018-12-01 21:01:59 +01:00
public long SetSockOpt(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
int socketFd = context.RequestData.ReadInt32();
int level = context.RequestData.ReadInt32();
int optionName = context.RequestData.ReadInt32();
2018-12-01 21:01:59 +01:00
(long bufferPos, long bufferSize) = context.Request.GetBufferType0x21();
2018-12-01 21:01:59 +01:00
LinuxError errno = LinuxError.BadF;
BsdSocket socket = RetrieveSocket(socketFd);
2018-12-01 21:01:59 +01:00
if (socket != null)
{
2018-12-01 21:01:59 +01:00
errno = LinuxError.NoProtoOpt;
2018-12-01 21:01:59 +01:00
if (level == 0xFFFF)
{
2018-12-01 21:01:59 +01:00
errno = HandleSetSocketOption(context, socket, (SocketOptionName)optionName, bufferPos, bufferSize);
}
else
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported SetSockOpt Level: {(SocketOptionLevel)level}");
}
}
2018-12-01 21:01:59 +01:00
return WriteBsdResult(context, 0, errno);
}
// Shutdown(u32 socket, u32 how) -> (i32 ret, u32 bsd_errno)
2018-12-01 21:01:59 +01:00
public long Shutdown(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
int socketFd = context.RequestData.ReadInt32();
int how = context.RequestData.ReadInt32();
2018-12-01 21:01:59 +01:00
LinuxError errno = LinuxError.BadF;
BsdSocket socket = RetrieveSocket(socketFd);
2018-12-01 21:01:59 +01:00
if (socket != null)
{
2018-12-01 21:01:59 +01:00
errno = LinuxError.InVal;
2018-12-01 21:01:59 +01:00
if (how >= 0 && how <= 2)
{
2018-12-01 21:01:59 +01:00
errno = LinuxError.Success;
try
{
2018-12-01 21:01:59 +01:00
socket.Handle.Shutdown((SocketShutdown)how);
}
2018-12-01 21:01:59 +01:00
catch (SocketException exception)
{
2018-12-01 21:01:59 +01:00
errno = ConvertError((WsaError)exception.ErrorCode);
}
}
}
2018-12-01 21:01:59 +01:00
return WriteBsdResult(context, 0, errno);
}
// ShutdownAllSockets(u32 how) -> (i32 ret, u32 bsd_errno)
2018-12-01 21:01:59 +01:00
public long ShutdownAllSockets(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
int how = context.RequestData.ReadInt32();
2018-12-01 21:01:59 +01:00
LinuxError errno = LinuxError.InVal;
2018-12-01 21:01:59 +01:00
if (how >= 0 && how <= 2)
{
2018-12-01 21:01:59 +01:00
errno = LinuxError.Success;
2018-12-01 21:01:59 +01:00
foreach (BsdSocket socket in _sockets)
{
2018-12-01 21:01:59 +01:00
if (socket != null)
{
try
{
2018-12-01 21:01:59 +01:00
socket.Handle.Shutdown((SocketShutdown)how);
}
2018-12-01 21:01:59 +01:00
catch (SocketException exception)
{
2018-12-01 21:01:59 +01:00
errno = ConvertError((WsaError)exception.ErrorCode);
break;
}
}
}
}
2018-12-01 21:01:59 +01:00
return WriteBsdResult(context, 0, errno);
}
// Write(u32 socket, buffer<i8, 0x21, 0> message) -> (i32 ret, u32 bsd_errno)
2018-12-01 21:01:59 +01:00
public long Write(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
int socketFd = context.RequestData.ReadInt32();
2018-12-01 21:01:59 +01:00
(long sendPosition, long sendSize) = context.Request.GetBufferType0x21();
2018-12-01 21:01:59 +01:00
LinuxError errno = LinuxError.BadF;
BsdSocket socket = RetrieveSocket(socketFd);
int result = -1;
2018-12-01 21:01:59 +01:00
if (socket != null)
{
2018-12-01 21:01:59 +01:00
byte[] sendBuffer = context.Memory.ReadBytes(sendPosition, sendSize);
try
{
2018-12-01 21:01:59 +01:00
result = socket.Handle.Send(sendBuffer);
errno = SetResultErrno(socket.Handle, result);
}
2018-12-01 21:01:59 +01:00
catch (SocketException exception)
{
2018-12-01 21:01:59 +01:00
errno = ConvertError((WsaError)exception.ErrorCode);
}
}
2018-12-01 21:01:59 +01:00
return WriteBsdResult(context, result, errno);
}
// Read(u32 socket) -> (i32 ret, u32 bsd_errno, buffer<i8, 0x22, 0> message)
2018-12-01 21:01:59 +01:00
public long Read(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
int socketFd = context.RequestData.ReadInt32();
2018-12-01 21:01:59 +01:00
(long receivePosition, long receiveLength) = context.Request.GetBufferType0x22();
2018-12-01 21:01:59 +01:00
LinuxError errno = LinuxError.BadF;
BsdSocket socket = RetrieveSocket(socketFd);
int result = -1;
2018-12-01 21:01:59 +01:00
if (socket != null)
{
2018-12-01 21:01:59 +01:00
byte[] receivedBuffer = new byte[receiveLength];
try
{
2018-12-01 21:01:59 +01:00
result = socket.Handle.Receive(receivedBuffer);
errno = SetResultErrno(socket.Handle, result);
}
2018-12-01 21:01:59 +01:00
catch (SocketException exception)
{
2018-12-01 21:01:59 +01:00
errno = ConvertError((WsaError)exception.ErrorCode);
}
}
2018-12-01 21:01:59 +01:00
return WriteBsdResult(context, result, errno);
}
// Close(u32 socket) -> (i32 ret, u32 bsd_errno)
2018-12-01 21:01:59 +01:00
public long Close(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
int socketFd = context.RequestData.ReadInt32();
2018-12-01 21:01:59 +01:00
LinuxError errno = LinuxError.BadF;
BsdSocket socket = RetrieveSocket(socketFd);
2018-12-01 21:01:59 +01:00
if (socket != null)
{
2018-12-01 21:01:59 +01:00
socket.Handle.Close();
2018-12-01 21:01:59 +01:00
_sockets[socketFd] = null;
2018-12-01 21:01:59 +01:00
errno = LinuxError.Success;
}
2018-12-01 21:01:59 +01:00
return WriteBsdResult(context, 0, errno);
}
// DuplicateSocket(u32 socket, u64 reserved) -> (i32 ret, u32 bsd_errno)
2018-12-01 21:01:59 +01:00
public long DuplicateSocket(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
int socketFd = context.RequestData.ReadInt32();
ulong reserved = context.RequestData.ReadUInt64();
2018-12-01 21:01:59 +01:00
LinuxError errno = LinuxError.NoEnt;
int newSockFd = -1;
2018-12-01 21:01:59 +01:00
if (_isPrivileged)
{
2018-12-01 21:01:59 +01:00
errno = LinuxError.BadF;
2018-12-01 21:01:59 +01:00
BsdSocket oldSocket = RetrieveSocket(socketFd);
2018-12-01 21:01:59 +01:00
if (oldSocket != null)
{
2018-12-01 21:01:59 +01:00
_sockets.Add(oldSocket);
newSockFd = _sockets.Count - 1;
}
}
2018-12-01 21:01:59 +01:00
return WriteBsdResult(context, newSockFd, errno);
}
}
}