2018-03-12 02:05:39 +01:00
|
|
|
using ChocolArm64.Memory;
|
2018-02-28 04:31:52 +01:00
|
|
|
using Ryujinx.Core.OsHle.Ipc;
|
2018-03-12 02:05:39 +01:00
|
|
|
using Ryujinx.Core.OsHle.Utilities;
|
2018-02-28 04:31:52 +01:00
|
|
|
using System.Collections.Generic;
|
2018-03-12 02:05:39 +01:00
|
|
|
using System.IO;
|
|
|
|
using System.Net;
|
|
|
|
using System.Net.Sockets;
|
|
|
|
using System.Threading.Tasks;
|
2018-02-28 04:31:52 +01:00
|
|
|
|
2018-03-20 21:00:00 +01:00
|
|
|
namespace Ryujinx.Core.OsHle.Services.Bsd
|
2018-02-28 04:31:52 +01:00
|
|
|
{
|
2018-04-06 06:01:52 +02:00
|
|
|
class IClient : IpcService
|
2018-02-28 04:31:52 +01:00
|
|
|
{
|
|
|
|
private Dictionary<int, ServiceProcessRequest> m_Commands;
|
|
|
|
|
2018-03-19 19:58:46 +01:00
|
|
|
public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
|
2018-02-28 04:31:52 +01:00
|
|
|
|
2018-04-06 06:01:52 +02:00
|
|
|
private List<BsdSocket> Sockets = new List<BsdSocket>();
|
2018-03-12 02:05:39 +01:00
|
|
|
|
2018-04-06 06:01:52 +02:00
|
|
|
public IClient()
|
2018-02-28 04:31:52 +01:00
|
|
|
{
|
|
|
|
m_Commands = new Dictionary<int, ServiceProcessRequest>()
|
|
|
|
{
|
2018-04-24 20:57:39 +02:00
|
|
|
{ 0, Initialize },
|
|
|
|
{ 1, StartMonitoring },
|
|
|
|
{ 2, Socket },
|
|
|
|
{ 6, Poll },
|
|
|
|
{ 8, Recv },
|
2018-02-28 21:58:04 +01:00
|
|
|
{ 10, Send },
|
2018-03-12 02:05:39 +01:00
|
|
|
{ 11, SendTo },
|
|
|
|
{ 12, Accept },
|
|
|
|
{ 13, Bind },
|
|
|
|
{ 14, Connect },
|
|
|
|
{ 18, Listen },
|
|
|
|
{ 21, SetSockOpt },
|
|
|
|
{ 26, Close }
|
2018-02-28 04:31:52 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-12 02:05:39 +01:00
|
|
|
//(u32, u32, u32, u32, u32, u32, u32, u32, u64 pid, u64 transferMemorySize, pid, KObject) -> u32 bsd_errno
|
2018-02-28 04:31:52 +01:00
|
|
|
public long Initialize(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;
|
|
|
|
*/
|
|
|
|
|
|
|
|
Context.ResponseData.Write(0);
|
|
|
|
|
|
|
|
//Todo: Stub
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-12 02:05:39 +01:00
|
|
|
//(u64, pid)
|
2018-02-28 04:31:52 +01:00
|
|
|
public long StartMonitoring(ServiceCtx Context)
|
|
|
|
{
|
|
|
|
//Todo: Stub
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2018-02-28 21:58:04 +01:00
|
|
|
|
2018-03-12 02:05:39 +01:00
|
|
|
//(u32 domain, u32 type, u32 protocol) -> (i32 ret, u32 bsd_errno)
|
2018-02-28 21:58:04 +01:00
|
|
|
public long Socket(ServiceCtx Context)
|
|
|
|
{
|
2018-04-06 06:01:52 +02:00
|
|
|
BsdSocket NewBsdSocket = new BsdSocket
|
2018-03-12 02:05:39 +01:00
|
|
|
{
|
|
|
|
Family = Context.RequestData.ReadInt32(),
|
|
|
|
Type = Context.RequestData.ReadInt32(),
|
|
|
|
Protocol = Context.RequestData.ReadInt32()
|
|
|
|
};
|
|
|
|
|
2018-04-06 06:01:52 +02:00
|
|
|
Sockets.Add(NewBsdSocket);
|
2018-03-12 02:05:39 +01:00
|
|
|
|
2018-04-06 06:01:52 +02:00
|
|
|
NewBsdSocket.Handle = new Socket((AddressFamily)NewBsdSocket.Family,
|
|
|
|
(SocketType)NewBsdSocket.Type,
|
|
|
|
(ProtocolType)NewBsdSocket.Protocol);
|
2018-03-12 02:05:39 +01:00
|
|
|
|
|
|
|
Context.ResponseData.Write(Sockets.Count - 1);
|
2018-02-28 21:58:04 +01:00
|
|
|
Context.ResponseData.Write(0);
|
2018-03-12 02:05:39 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//(u32, u32, buffer<unknown, 0x21, 0>) -> (i32 ret, u32 bsd_errno, buffer<unknown, 0x22, 0>)
|
|
|
|
public long Poll(ServiceCtx Context)
|
|
|
|
{
|
|
|
|
int PollCount = Context.RequestData.ReadInt32();
|
|
|
|
int TimeOut = Context.RequestData.ReadInt32();
|
|
|
|
|
|
|
|
//https://github.com/torvalds/linux/blob/master/include/uapi/asm-generic/poll.h
|
|
|
|
//https://msdn.microsoft.com/fr-fr/library/system.net.sockets.socket.poll(v=vs.110).aspx
|
|
|
|
//https://github.com/switchbrew/libnx/blob/e0457c4534b3c37426d83e1a620f82cb28c3b528/nx/source/services/bsd.c#L343
|
|
|
|
//https://github.com/TuxSH/ftpd/blob/switch_pr/source/ftp.c#L1634
|
|
|
|
//https://linux.die.net/man/2/poll
|
|
|
|
|
2018-06-09 02:15:02 +02:00
|
|
|
byte[] SentBuffer = Context.Memory.ReadBytes(Context.Request.SendBuff[0].Position,
|
|
|
|
Context.Request.SendBuff[0].Size);
|
2018-04-06 06:01:52 +02:00
|
|
|
|
|
|
|
int SocketId = Get32(SentBuffer, 0);
|
|
|
|
int RequestedEvents = Get16(SentBuffer, 4);
|
|
|
|
int ReturnedEvents = Get16(SentBuffer, 6);
|
2018-03-12 02:05:39 +01:00
|
|
|
|
|
|
|
//Todo: Stub - Need to implemented the Type-22 buffer.
|
|
|
|
|
|
|
|
Context.ResponseData.Write(1);
|
2018-02-28 21:58:04 +01:00
|
|
|
Context.ResponseData.Write(0);
|
|
|
|
|
2018-03-12 02:05:39 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//(u32 socket, u32 flags) -> (i32 ret, u32 bsd_errno, buffer<i8, 0x22, 0> message)
|
|
|
|
public long Recv(ServiceCtx Context)
|
|
|
|
{
|
2018-04-06 06:01:52 +02:00
|
|
|
int SocketId = Context.RequestData.ReadInt32();
|
|
|
|
int SocketFlags = Context.RequestData.ReadInt32();
|
|
|
|
|
|
|
|
byte[] ReceivedBuffer = new byte[Context.Request.ReceiveBuff[0].Size];
|
|
|
|
|
2018-03-12 02:05:39 +01:00
|
|
|
try
|
|
|
|
{
|
2018-04-06 06:01:52 +02:00
|
|
|
int BytesRead = Sockets[SocketId].Handle.Receive(ReceivedBuffer);
|
2018-03-12 02:05:39 +01:00
|
|
|
|
|
|
|
//Logging.Debug("Received Buffer:" + Environment.NewLine + Logging.HexDump(ReceivedBuffer));
|
|
|
|
|
|
|
|
AMemoryHelper.WriteBytes(Context.Memory, Context.Request.ReceiveBuff[0].Position, ReceivedBuffer);
|
|
|
|
|
2018-04-06 06:01:52 +02:00
|
|
|
Context.ResponseData.Write(BytesRead);
|
2018-03-12 02:05:39 +01:00
|
|
|
Context.ResponseData.Write(0);
|
|
|
|
}
|
|
|
|
catch (SocketException Ex)
|
|
|
|
{
|
|
|
|
Context.ResponseData.Write(-1);
|
|
|
|
Context.ResponseData.Write(Ex.ErrorCode - 10000);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//(u32 socket, u32 flags, buffer<i8, 0x21, 0>) -> (i32 ret, u32 bsd_errno)
|
|
|
|
public long Send(ServiceCtx Context)
|
|
|
|
{
|
2018-04-06 06:01:52 +02:00
|
|
|
int SocketId = Context.RequestData.ReadInt32();
|
|
|
|
int SocketFlags = Context.RequestData.ReadInt32();
|
|
|
|
|
2018-06-09 02:15:02 +02:00
|
|
|
byte[] SentBuffer = Context.Memory.ReadBytes(Context.Request.SendBuff[0].Position,
|
|
|
|
Context.Request.SendBuff[0].Size);
|
2018-03-12 02:05:39 +01:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2018-04-06 06:01:52 +02:00
|
|
|
//Logging.Debug("Sent Buffer:" + Environment.NewLine + Logging.HexDump(SentBuffer));
|
2018-03-12 02:05:39 +01:00
|
|
|
|
|
|
|
int BytesSent = Sockets[SocketId].Handle.Send(SentBuffer);
|
|
|
|
|
|
|
|
Context.ResponseData.Write(BytesSent);
|
|
|
|
Context.ResponseData.Write(0);
|
|
|
|
}
|
|
|
|
catch (SocketException Ex)
|
|
|
|
{
|
|
|
|
Context.ResponseData.Write(-1);
|
|
|
|
Context.ResponseData.Write(Ex.ErrorCode - 10000);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//(u32 socket, u32 flags, buffer<i8, 0x21, 0>, buffer<sockaddr, 0x21, 0>) -> (i32 ret, u32 bsd_errno)
|
|
|
|
public long SendTo(ServiceCtx Context)
|
|
|
|
{
|
2018-04-06 06:01:52 +02:00
|
|
|
int SocketId = Context.RequestData.ReadInt32();
|
|
|
|
int SocketFlags = Context.RequestData.ReadInt32();
|
|
|
|
|
2018-06-09 02:15:02 +02:00
|
|
|
byte[] SentBuffer = Context.Memory.ReadBytes(Context.Request.SendBuff[0].Position,
|
|
|
|
Context.Request.SendBuff[0].Size);
|
2018-04-06 06:01:52 +02:00
|
|
|
|
2018-06-09 02:15:02 +02:00
|
|
|
byte[] AddressBuffer = Context.Memory.ReadBytes(Context.Request.SendBuff[1].Position,
|
|
|
|
Context.Request.SendBuff[1].Size);
|
2018-03-12 02:05:39 +01:00
|
|
|
|
|
|
|
if (!Sockets[SocketId].Handle.Connected)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
ParseAddrBuffer(SocketId, AddressBuffer);
|
|
|
|
|
|
|
|
Sockets[SocketId].Handle.Connect(Sockets[SocketId].RemoteEP);
|
|
|
|
}
|
|
|
|
catch (SocketException Ex)
|
|
|
|
{
|
|
|
|
Context.ResponseData.Write(-1);
|
|
|
|
Context.ResponseData.Write(Ex.ErrorCode - 10000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2018-04-06 06:01:52 +02:00
|
|
|
//Logging.Debug("Sent Buffer:" + Environment.NewLine + Logging.HexDump(SentBuffer));
|
2018-03-12 02:05:39 +01:00
|
|
|
|
|
|
|
int BytesSent = Sockets[SocketId].Handle.Send(SentBuffer);
|
|
|
|
|
|
|
|
Context.ResponseData.Write(BytesSent);
|
|
|
|
Context.ResponseData.Write(0);
|
|
|
|
}
|
|
|
|
catch (SocketException Ex)
|
|
|
|
{
|
|
|
|
Context.ResponseData.Write(-1);
|
|
|
|
Context.ResponseData.Write(Ex.ErrorCode - 10000);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//(u32 socket) -> (i32 ret, u32 bsd_errno, u32 addrlen, buffer<sockaddr, 0x22, 0> addr)
|
|
|
|
public long Accept(ServiceCtx Context)
|
|
|
|
{
|
2018-04-06 06:01:52 +02:00
|
|
|
int SocketId = Context.RequestData.ReadInt32();
|
|
|
|
|
2018-03-12 02:05:39 +01:00
|
|
|
long AddrBufferPtr = Context.Request.ReceiveBuff[0].Position;
|
|
|
|
|
|
|
|
Socket HandleAccept = null;
|
|
|
|
|
2018-04-06 06:01:52 +02:00
|
|
|
Task TimeOut = Task.Factory.StartNew(() =>
|
2018-03-12 02:05:39 +01:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
HandleAccept = Sockets[SocketId].Handle.Accept();
|
|
|
|
}
|
|
|
|
catch (SocketException Ex)
|
|
|
|
{
|
|
|
|
Context.ResponseData.Write(-1);
|
|
|
|
Context.ResponseData.Write(Ex.ErrorCode - 10000);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
TimeOut.Wait(10000);
|
|
|
|
|
|
|
|
if (HandleAccept != null)
|
|
|
|
{
|
2018-04-06 06:01:52 +02:00
|
|
|
BsdSocket NewBsdSocket = new BsdSocket
|
2018-03-12 02:05:39 +01:00
|
|
|
{
|
|
|
|
IpAddress = ((IPEndPoint)Sockets[SocketId].Handle.LocalEndPoint).Address,
|
|
|
|
RemoteEP = ((IPEndPoint)Sockets[SocketId].Handle.LocalEndPoint),
|
|
|
|
Handle = HandleAccept
|
|
|
|
};
|
|
|
|
|
2018-04-06 06:01:52 +02:00
|
|
|
Sockets.Add(NewBsdSocket);
|
2018-03-12 02:05:39 +01:00
|
|
|
|
|
|
|
using (MemoryStream MS = new MemoryStream())
|
|
|
|
{
|
|
|
|
BinaryWriter Writer = new BinaryWriter(MS);
|
|
|
|
|
|
|
|
Writer.Write((byte)0);
|
|
|
|
|
2018-04-06 06:01:52 +02:00
|
|
|
Writer.Write((byte)NewBsdSocket.Handle.AddressFamily);
|
2018-03-12 02:05:39 +01:00
|
|
|
|
2018-04-06 06:01:52 +02:00
|
|
|
Writer.Write((short)((IPEndPoint)NewBsdSocket.Handle.LocalEndPoint).Port);
|
|
|
|
|
2018-04-06 06:07:51 +02:00
|
|
|
byte[] IpAddress = NewBsdSocket.IpAddress.GetAddressBytes();
|
2018-04-06 06:01:52 +02:00
|
|
|
|
2018-04-06 06:07:51 +02:00
|
|
|
Writer.Write(IpAddress);
|
2018-04-06 06:06:34 +02:00
|
|
|
|
|
|
|
AMemoryHelper.WriteBytes(Context.Memory, AddrBufferPtr, MS.ToArray());
|
2018-03-12 02:05:39 +01:00
|
|
|
|
|
|
|
Context.ResponseData.Write(Sockets.Count - 1);
|
|
|
|
Context.ResponseData.Write(0);
|
2018-04-06 06:06:34 +02:00
|
|
|
Context.ResponseData.Write(MS.Length);
|
2018-03-12 02:05:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Context.ResponseData.Write(-1);
|
2018-04-06 06:01:52 +02:00
|
|
|
Context.ResponseData.Write((int)BsdError.Timeout);
|
2018-03-12 02:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//(u32 socket, buffer<sockaddr, 0x21, 0>) -> (i32 ret, u32 bsd_errno)
|
|
|
|
public long Bind(ServiceCtx Context)
|
|
|
|
{
|
|
|
|
int SocketId = Context.RequestData.ReadInt32();
|
|
|
|
|
2018-06-09 02:15:02 +02:00
|
|
|
byte[] AddressBuffer = Context.Memory.ReadBytes(Context.Request.SendBuff[0].Position,
|
|
|
|
Context.Request.SendBuff[0].Size);
|
2018-03-12 02:05:39 +01:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
ParseAddrBuffer(SocketId, AddressBuffer);
|
|
|
|
|
|
|
|
Context.ResponseData.Write(0);
|
|
|
|
Context.ResponseData.Write(0);
|
|
|
|
}
|
|
|
|
catch (SocketException Ex)
|
|
|
|
{
|
|
|
|
Context.ResponseData.Write(-1);
|
|
|
|
Context.ResponseData.Write(Ex.ErrorCode - 10000);
|
|
|
|
}
|
2018-02-28 21:58:04 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-12 02:05:39 +01:00
|
|
|
//(u32 socket, buffer<sockaddr, 0x21, 0>) -> (i32 ret, u32 bsd_errno)
|
2018-02-28 21:58:04 +01:00
|
|
|
public long Connect(ServiceCtx Context)
|
|
|
|
{
|
2018-03-12 02:05:39 +01:00
|
|
|
int SocketId = Context.RequestData.ReadInt32();
|
2018-02-28 21:58:04 +01:00
|
|
|
|
2018-06-09 02:15:02 +02:00
|
|
|
byte[] AddressBuffer = Context.Memory.ReadBytes(Context.Request.SendBuff[0].Position,
|
|
|
|
Context.Request.SendBuff[0].Size);
|
2018-03-12 02:05:39 +01:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
ParseAddrBuffer(SocketId, AddressBuffer);
|
|
|
|
|
|
|
|
Sockets[SocketId].Handle.Connect(Sockets[SocketId].RemoteEP);
|
|
|
|
|
|
|
|
Context.ResponseData.Write(0);
|
|
|
|
Context.ResponseData.Write(0);
|
|
|
|
}
|
|
|
|
catch (SocketException Ex)
|
|
|
|
{
|
|
|
|
Context.ResponseData.Write(-1);
|
|
|
|
Context.ResponseData.Write(Ex.ErrorCode - 10000);
|
|
|
|
}
|
2018-02-28 21:58:04 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-12 02:05:39 +01:00
|
|
|
//(u32 socket, u32 backlog) -> (i32 ret, u32 bsd_errno)
|
|
|
|
public long Listen(ServiceCtx Context)
|
2018-02-28 21:58:04 +01:00
|
|
|
{
|
2018-03-12 02:05:39 +01:00
|
|
|
int SocketId = Context.RequestData.ReadInt32();
|
|
|
|
int BackLog = Context.RequestData.ReadInt32();
|
2018-02-28 21:58:04 +01:00
|
|
|
|
2018-03-12 02:05:39 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
Sockets[SocketId].Handle.Bind(Sockets[SocketId].RemoteEP);
|
|
|
|
Sockets[SocketId].Handle.Listen(BackLog);
|
|
|
|
|
|
|
|
Context.ResponseData.Write(0);
|
|
|
|
Context.ResponseData.Write(0);
|
|
|
|
}
|
|
|
|
catch (SocketException Ex)
|
|
|
|
{
|
|
|
|
Context.ResponseData.Write(-1);
|
|
|
|
Context.ResponseData.Write(Ex.ErrorCode - 10000);
|
|
|
|
}
|
2018-02-28 21:58:04 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2018-03-12 02:05:39 +01:00
|
|
|
|
|
|
|
//(u32 socket, u32 level, u32 option_name, buffer<unknown, 0x21, 0>) -> (i32 ret, u32 bsd_errno)
|
|
|
|
public long SetSockOpt(ServiceCtx Context)
|
|
|
|
{
|
2018-04-06 06:01:52 +02:00
|
|
|
int SocketId = Context.RequestData.ReadInt32();
|
2018-03-12 02:05:39 +01:00
|
|
|
|
2018-04-06 06:01:52 +02:00
|
|
|
SocketOptionLevel SocketLevel = (SocketOptionLevel)Context.RequestData.ReadInt32();
|
|
|
|
SocketOptionName SocketOptionName = (SocketOptionName)Context.RequestData.ReadInt32();
|
|
|
|
|
2018-06-09 02:15:02 +02:00
|
|
|
byte[] SocketOptionValue = Context.Memory.ReadBytes(Context.Request.PtrBuff[0].Position,
|
|
|
|
Context.Request.PtrBuff[0].Size);
|
2018-03-12 02:05:39 +01:00
|
|
|
|
2018-04-06 06:01:52 +02:00
|
|
|
int OptionValue = Get32(SocketOptionValue, 0);
|
|
|
|
|
2018-03-12 02:05:39 +01:00
|
|
|
try
|
|
|
|
{
|
2018-04-06 06:01:52 +02:00
|
|
|
Sockets[SocketId].Handle.SetSocketOption(SocketLevel, SocketOptionName, OptionValue);
|
2018-03-12 02:05:39 +01:00
|
|
|
|
|
|
|
Context.ResponseData.Write(0);
|
|
|
|
Context.ResponseData.Write(0);
|
|
|
|
}
|
|
|
|
catch (SocketException Ex)
|
|
|
|
{
|
|
|
|
Context.ResponseData.Write(-1);
|
|
|
|
Context.ResponseData.Write(Ex.ErrorCode - 10000);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//(u32 socket) -> (i32 ret, u32 bsd_errno)
|
|
|
|
public long Close(ServiceCtx Context)
|
|
|
|
{
|
|
|
|
int SocketId = Context.RequestData.ReadInt32();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Sockets[SocketId].Handle.Close();
|
|
|
|
Sockets[SocketId] = null;
|
|
|
|
|
|
|
|
Context.ResponseData.Write(0);
|
|
|
|
Context.ResponseData.Write(0);
|
|
|
|
}
|
|
|
|
catch (SocketException Ex)
|
|
|
|
{
|
|
|
|
Context.ResponseData.Write(-1);
|
|
|
|
Context.ResponseData.Write(Ex.ErrorCode - 10000);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ParseAddrBuffer(int SocketId, byte[] AddrBuffer)
|
|
|
|
{
|
|
|
|
using (MemoryStream MS = new MemoryStream(AddrBuffer))
|
|
|
|
{
|
|
|
|
BinaryReader Reader = new BinaryReader(MS);
|
|
|
|
|
|
|
|
int Size = Reader.ReadByte();
|
|
|
|
int Family = Reader.ReadByte();
|
|
|
|
int Port = EndianSwap.Swap16(Reader.ReadInt16());
|
2018-04-06 06:01:52 +02:00
|
|
|
|
|
|
|
string IpAddress = Reader.ReadByte().ToString() + "." +
|
|
|
|
Reader.ReadByte().ToString() + "." +
|
|
|
|
Reader.ReadByte().ToString() + "." +
|
|
|
|
Reader.ReadByte().ToString();
|
2018-03-12 02:05:39 +01:00
|
|
|
|
|
|
|
Sockets[SocketId].IpAddress = IPAddress.Parse(IpAddress);
|
2018-04-24 20:57:39 +02:00
|
|
|
|
2018-03-12 02:05:39 +01:00
|
|
|
Sockets[SocketId].RemoteEP = new IPEndPoint(Sockets[SocketId].IpAddress, Port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private int Get16(byte[] Data, int Address)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
Data[Address + 0] << 0 |
|
|
|
|
Data[Address + 1] << 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int Get32(byte[] Data, int Address)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
Data[Address + 0] << 0 |
|
|
|
|
Data[Address + 1] << 8 |
|
|
|
|
Data[Address + 2] << 16 |
|
|
|
|
Data[Address + 3] << 24;
|
|
|
|
}
|
2018-02-28 04:31:52 +01:00
|
|
|
}
|
|
|
|
}
|