2018-10-17 17:15:50 +00:00
|
|
|
using Ryujinx.Common.Logging;
|
2021-03-24 17:43:23 +00:00
|
|
|
using Ryujinx.Cpu;
|
|
|
|
using Ryujinx.HLE.HOS.Services.Sockets.Sfdnsres.Proxy;
|
|
|
|
using Ryujinx.HLE.HOS.Services.Sockets.Sfdnsres.Types;
|
|
|
|
using Ryujinx.Memory;
|
2018-10-09 23:15:07 +00:00
|
|
|
using System;
|
2021-03-24 17:43:23 +00:00
|
|
|
using System.Buffers.Binary;
|
2018-02-28 20:58:04 +00:00
|
|
|
using System.Collections.Generic;
|
2021-03-24 17:43:23 +00:00
|
|
|
using System.Linq;
|
2018-10-09 23:15:07 +00:00
|
|
|
using System.Net;
|
|
|
|
using System.Net.Sockets;
|
2021-03-24 17:43:23 +00:00
|
|
|
using System.Runtime.CompilerServices;
|
2018-10-09 23:15:07 +00:00
|
|
|
using System.Text;
|
|
|
|
|
2019-09-19 00:45:11 +00:00
|
|
|
namespace Ryujinx.HLE.HOS.Services.Sockets.Sfdnsres
|
2018-02-28 20:58:04 +00:00
|
|
|
{
|
2019-07-10 15:59:54 +00:00
|
|
|
[Service("sfdnsres")]
|
2018-04-06 04:01:52 +00:00
|
|
|
class IResolver : IpcService
|
2018-02-28 20:58:04 +00:00
|
|
|
{
|
2019-07-12 01:13:43 +00:00
|
|
|
public IResolver(ServiceCtx context) { }
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2021-04-13 22:01:24 +00:00
|
|
|
[CommandHipc(0)]
|
2021-03-24 17:43:23 +00:00
|
|
|
// SetDnsAddressesPrivateRequest(u32, buffer<unknown, 5, 0>)
|
|
|
|
public ResultCode SetDnsAddressesPrivateRequest(ServiceCtx context)
|
2018-10-09 23:15:07 +00:00
|
|
|
{
|
2021-03-24 17:43:23 +00:00
|
|
|
uint cancelHandleRequest = context.RequestData.ReadUInt32();
|
2021-04-24 10:16:01 +00:00
|
|
|
ulong bufferPosition = context.Request.SendBuff[0].Position;
|
|
|
|
ulong bufferSize = context.Request.SendBuff[0].Size;
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2021-03-24 17:43:23 +00:00
|
|
|
// TODO: This is stubbed in 2.0.0+, reverse 1.0.0 version for the sake of completeness.
|
|
|
|
Logger.Stub?.PrintStub(LogClass.ServiceSfdnsres, new { cancelHandleRequest });
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2021-03-24 17:43:23 +00:00
|
|
|
return ResultCode.NotAllocated;
|
|
|
|
}
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2021-04-13 22:01:24 +00:00
|
|
|
[CommandHipc(1)]
|
2021-03-24 17:43:23 +00:00
|
|
|
// GetDnsAddressPrivateRequest(u32) -> buffer<unknown, 6, 0>
|
|
|
|
public ResultCode GetDnsAddressPrivateRequest(ServiceCtx context)
|
|
|
|
{
|
|
|
|
uint cancelHandleRequest = context.RequestData.ReadUInt32();
|
2021-04-24 10:16:01 +00:00
|
|
|
ulong bufferPosition = context.Request.ReceiveBuff[0].Position;
|
|
|
|
ulong bufferSize = context.Request.ReceiveBuff[0].Size;
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2021-03-24 17:43:23 +00:00
|
|
|
// TODO: This is stubbed in 2.0.0+, reverse 1.0.0 version for the sake of completeness.
|
|
|
|
Logger.Stub?.PrintStub(LogClass.ServiceSfdnsres, new { cancelHandleRequest });
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2021-03-24 17:43:23 +00:00
|
|
|
return ResultCode.NotAllocated;
|
|
|
|
}
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2021-04-13 22:01:24 +00:00
|
|
|
[CommandHipc(2)]
|
2021-03-24 17:43:23 +00:00
|
|
|
// GetHostByNameRequest(u8, u32, u64, pid, buffer<unknown, 5, 0>) -> (u32, u32, u32, buffer<unknown, 6, 0>)
|
|
|
|
public ResultCode GetHostByNameRequest(ServiceCtx context)
|
|
|
|
{
|
2021-04-24 10:16:01 +00:00
|
|
|
ulong inputBufferPosition = context.Request.SendBuff[0].Position;
|
|
|
|
ulong inputBufferSize = context.Request.SendBuff[0].Size;
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2021-04-24 10:16:01 +00:00
|
|
|
ulong outputBufferPosition = context.Request.ReceiveBuff[0].Position;
|
|
|
|
ulong outputBufferSize = context.Request.ReceiveBuff[0].Size;
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2021-03-24 17:43:23 +00:00
|
|
|
return GetHostByNameRequestImpl(context, inputBufferPosition, inputBufferSize, outputBufferPosition, outputBufferSize, 0, 0);
|
|
|
|
}
|
|
|
|
|
2021-04-13 22:01:24 +00:00
|
|
|
[CommandHipc(3)]
|
2021-03-24 17:43:23 +00:00
|
|
|
// GetHostByAddrRequest(u32, u32, u32, u64, pid, buffer<unknown, 5, 0>) -> (u32, u32, u32, buffer<unknown, 6, 0>)
|
|
|
|
public ResultCode GetHostByAddrRequest(ServiceCtx context)
|
|
|
|
{
|
2021-04-24 10:16:01 +00:00
|
|
|
ulong inputBufferPosition = context.Request.SendBuff[0].Position;
|
|
|
|
ulong inputBufferSize = context.Request.SendBuff[0].Size;
|
2021-03-24 17:43:23 +00:00
|
|
|
|
2021-04-24 10:16:01 +00:00
|
|
|
ulong outputBufferPosition = context.Request.ReceiveBuff[0].Position;
|
|
|
|
ulong outputBufferSize = context.Request.ReceiveBuff[0].Size;
|
2021-03-24 17:43:23 +00:00
|
|
|
|
|
|
|
return GetHostByAddrRequestImpl(context, inputBufferPosition, inputBufferSize, outputBufferPosition, outputBufferSize, 0, 0);
|
|
|
|
}
|
|
|
|
|
2021-04-13 22:01:24 +00:00
|
|
|
[CommandHipc(4)]
|
2021-03-24 17:43:23 +00:00
|
|
|
// GetHostStringErrorRequest(u32) -> buffer<unknown, 6, 0>
|
|
|
|
public ResultCode GetHostStringErrorRequest(ServiceCtx context)
|
|
|
|
{
|
|
|
|
ResultCode resultCode = ResultCode.NotAllocated;
|
|
|
|
NetDbError errorCode = (NetDbError)context.RequestData.ReadInt32();
|
|
|
|
|
|
|
|
string errorString = errorCode switch
|
2018-10-09 23:15:07 +00:00
|
|
|
{
|
2021-03-24 17:43:23 +00:00
|
|
|
NetDbError.Success => "Resolver Error 0 (no error)",
|
|
|
|
NetDbError.HostNotFound => "Unknown host",
|
|
|
|
NetDbError.TryAgain => "Host name lookup failure",
|
|
|
|
NetDbError.NoRecovery => "Unknown server error",
|
|
|
|
NetDbError.NoData => "No address associated with name",
|
|
|
|
_ => (errorCode <= NetDbError.Internal) ? "Resolver internal error" : "Unknown resolver error"
|
|
|
|
};
|
|
|
|
|
2021-04-24 10:16:01 +00:00
|
|
|
ulong bufferPosition = context.Request.ReceiveBuff[0].Position;
|
|
|
|
ulong bufferSize = context.Request.ReceiveBuff[0].Size;
|
2021-03-24 17:43:23 +00:00
|
|
|
|
2021-04-24 10:16:01 +00:00
|
|
|
if ((ulong)(errorString.Length + 1) <= bufferSize)
|
2021-03-24 17:43:23 +00:00
|
|
|
{
|
2021-04-24 10:16:01 +00:00
|
|
|
context.Memory.Write(bufferPosition, Encoding.ASCII.GetBytes(errorString + '\0'));
|
2021-03-24 17:43:23 +00:00
|
|
|
|
|
|
|
resultCode = ResultCode.Success;
|
2018-10-09 23:15:07 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 17:43:23 +00:00
|
|
|
return resultCode;
|
2018-10-09 23:15:07 +00:00
|
|
|
}
|
|
|
|
|
2021-04-13 22:01:24 +00:00
|
|
|
[CommandHipc(5)]
|
2021-03-24 17:43:23 +00:00
|
|
|
// GetGaiStringErrorRequest(u32) -> buffer<byte, 6, 0>
|
|
|
|
public ResultCode GetGaiStringErrorRequest(ServiceCtx context)
|
2018-10-09 23:15:07 +00:00
|
|
|
{
|
2021-03-24 17:43:23 +00:00
|
|
|
ResultCode resultCode = ResultCode.NotAllocated;
|
|
|
|
GaiError errorCode = (GaiError)context.RequestData.ReadInt32();
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (errorCode > GaiError.Max)
|
2018-10-09 23:15:07 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
errorCode = GaiError.Max;
|
2018-10-09 23:15:07 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 17:43:23 +00:00
|
|
|
string errorString = errorCode switch
|
|
|
|
{
|
|
|
|
GaiError.AddressFamily => "Address family for hostname not supported",
|
|
|
|
GaiError.Again => "Temporary failure in name resolution",
|
|
|
|
GaiError.BadFlags => "Invalid value for ai_flags",
|
|
|
|
GaiError.Fail => "Non-recoverable failure in name resolution",
|
|
|
|
GaiError.Family => "ai_family not supported",
|
|
|
|
GaiError.Memory => "Memory allocation failure",
|
|
|
|
GaiError.NoData => "No address associated with hostname",
|
|
|
|
GaiError.NoName => "hostname nor servname provided, or not known",
|
|
|
|
GaiError.Service => "servname not supported for ai_socktype",
|
|
|
|
GaiError.SocketType => "ai_socktype not supported",
|
|
|
|
GaiError.System => "System error returned in errno",
|
|
|
|
GaiError.BadHints => "Invalid value for hints",
|
|
|
|
GaiError.Protocol => "Resolved protocol is unknown",
|
|
|
|
GaiError.Overflow => "Argument buffer overflow",
|
|
|
|
GaiError.Max => "Unknown error",
|
|
|
|
_ => "Success"
|
|
|
|
};
|
|
|
|
|
2021-04-24 10:16:01 +00:00
|
|
|
ulong bufferPosition = context.Request.ReceiveBuff[0].Position;
|
|
|
|
ulong bufferSize = context.Request.ReceiveBuff[0].Size;
|
2021-03-24 17:43:23 +00:00
|
|
|
|
2021-04-24 10:16:01 +00:00
|
|
|
if ((ulong)(errorString.Length + 1) <= bufferSize)
|
2018-10-09 23:15:07 +00:00
|
|
|
{
|
2021-04-24 10:16:01 +00:00
|
|
|
context.Memory.Write(bufferPosition, Encoding.ASCII.GetBytes(errorString + '\0'));
|
2021-03-24 17:43:23 +00:00
|
|
|
|
|
|
|
resultCode = ResultCode.Success;
|
2018-10-09 23:15:07 +00:00
|
|
|
}
|
2021-03-24 17:43:23 +00:00
|
|
|
|
|
|
|
return resultCode;
|
2018-10-09 23:15:07 +00:00
|
|
|
}
|
|
|
|
|
2021-04-13 22:01:24 +00:00
|
|
|
[CommandHipc(6)]
|
2021-03-24 17:43:23 +00:00
|
|
|
// GetAddrInfoRequest(bool enable_nsd_resolve, u32, u64 pid_placeholder, pid, buffer<i8, 5, 0> host, buffer<i8, 5, 0> service, buffer<packed_addrinfo, 5, 0> hints) -> (i32 ret, u32 bsd_errno, u32 packed_addrinfo_size, buffer<packed_addrinfo, 6, 0> response)
|
|
|
|
public ResultCode GetAddrInfoRequest(ServiceCtx context)
|
2018-10-09 23:15:07 +00:00
|
|
|
{
|
2021-04-24 10:16:01 +00:00
|
|
|
ulong responseBufferPosition = context.Request.ReceiveBuff[0].Position;
|
|
|
|
ulong responseBufferSize = context.Request.ReceiveBuff[0].Size;
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2021-12-26 14:17:13 +00:00
|
|
|
return GetAddrInfoRequestImpl(context, responseBufferPosition, responseBufferSize, false, 0, 0);
|
2018-10-09 23:15:07 +00:00
|
|
|
}
|
|
|
|
|
2021-04-13 22:01:24 +00:00
|
|
|
[CommandHipc(8)]
|
2021-03-24 17:43:23 +00:00
|
|
|
// GetCancelHandleRequest(u64, pid) -> u32
|
|
|
|
public ResultCode GetCancelHandleRequest(ServiceCtx context)
|
2018-10-09 23:15:07 +00:00
|
|
|
{
|
2021-03-24 17:43:23 +00:00
|
|
|
ulong pidPlaceHolder = context.RequestData.ReadUInt64();
|
|
|
|
uint cancelHandleRequest = 0;
|
|
|
|
|
|
|
|
context.ResponseData.Write(cancelHandleRequest);
|
|
|
|
|
|
|
|
Logger.Stub?.PrintStub(LogClass.ServiceSfdnsres, new { cancelHandleRequest });
|
|
|
|
|
|
|
|
return ResultCode.Success;
|
2018-10-09 23:15:07 +00:00
|
|
|
}
|
|
|
|
|
2021-04-13 22:01:24 +00:00
|
|
|
[CommandHipc(9)]
|
2021-03-24 17:43:23 +00:00
|
|
|
// CancelRequest(u32, u64, pid)
|
|
|
|
public ResultCode CancelRequest(ServiceCtx context)
|
2018-10-09 23:15:07 +00:00
|
|
|
{
|
2021-03-24 17:43:23 +00:00
|
|
|
uint cancelHandleRequest = context.RequestData.ReadUInt32();
|
|
|
|
ulong pidPlaceHolder = context.RequestData.ReadUInt64();
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2021-03-24 17:43:23 +00:00
|
|
|
Logger.Stub?.PrintStub(LogClass.ServiceSfdnsres, new { cancelHandleRequest });
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2021-03-24 17:43:23 +00:00
|
|
|
return ResultCode.Success;
|
2018-10-09 23:15:07 +00:00
|
|
|
}
|
|
|
|
|
2021-04-13 22:01:24 +00:00
|
|
|
[CommandHipc(10)] // 5.0.0+
|
2021-03-24 17:43:23 +00:00
|
|
|
// GetHostByNameRequestWithOptions(u8, u32, u64, pid, buffer<unknown, 21, 0>, buffer<unknown, 21, 0>) -> (u32, u32, u32, buffer<unknown, 22, 0>)
|
|
|
|
public ResultCode GetHostByNameRequestWithOptions(ServiceCtx context)
|
2018-10-09 23:15:07 +00:00
|
|
|
{
|
2021-04-24 10:16:01 +00:00
|
|
|
(ulong inputBufferPosition, ulong inputBufferSize) = context.Request.GetBufferType0x21();
|
|
|
|
(ulong outputBufferPosition, ulong outputBufferSize) = context.Request.GetBufferType0x22();
|
|
|
|
(ulong optionsBufferPosition, ulong optionsBufferSize) = context.Request.GetBufferType0x21();
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2021-03-24 17:43:23 +00:00
|
|
|
return GetHostByNameRequestImpl(context, inputBufferPosition, inputBufferSize, outputBufferPosition, outputBufferSize, optionsBufferPosition, optionsBufferSize);
|
|
|
|
}
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2021-04-13 22:01:24 +00:00
|
|
|
[CommandHipc(11)] // 5.0.0+
|
2021-03-24 17:43:23 +00:00
|
|
|
// GetHostByAddrRequestWithOptions(u32, u32, u32, u64, pid, buffer<unknown, 21, 0>, buffer<unknown, 21, 0>) -> (u32, u32, u32, buffer<unknown, 22, 0>)
|
|
|
|
public ResultCode GetHostByAddrRequestWithOptions(ServiceCtx context)
|
|
|
|
{
|
2021-04-24 10:16:01 +00:00
|
|
|
(ulong inputBufferPosition, ulong inputBufferSize) = context.Request.GetBufferType0x21();
|
|
|
|
(ulong outputBufferPosition, ulong outputBufferSize) = context.Request.GetBufferType0x22();
|
|
|
|
(ulong optionsBufferPosition, ulong optionsBufferSize) = context.Request.GetBufferType0x21();
|
2021-03-24 17:43:23 +00:00
|
|
|
|
|
|
|
return GetHostByAddrRequestImpl(context, inputBufferPosition, inputBufferSize, outputBufferPosition, outputBufferSize, optionsBufferPosition, optionsBufferSize);
|
2018-10-09 23:15:07 +00:00
|
|
|
}
|
|
|
|
|
2021-04-13 22:01:24 +00:00
|
|
|
[CommandHipc(12)] // 5.0.0+
|
2021-03-24 17:43:23 +00:00
|
|
|
// GetAddrInfoRequestWithOptions(bool enable_nsd_resolve, u32, u64 pid_placeholder, pid, buffer<i8, 5, 0> host, buffer<i8, 5, 0> service, buffer<packed_addrinfo, 5, 0> hints, buffer<unknown, 21, 0>) -> (i32 ret, u32 bsd_errno, u32 unknown, u32 packed_addrinfo_size, buffer<packed_addrinfo, 22, 0> response)
|
|
|
|
public ResultCode GetAddrInfoRequestWithOptions(ServiceCtx context)
|
|
|
|
{
|
2021-04-24 10:16:01 +00:00
|
|
|
(ulong responseBufferPosition, ulong responseBufferSize) = context.Request.GetBufferType0x22();
|
|
|
|
(ulong optionsBufferPosition, ulong optionsBufferSize) = context.Request.GetBufferType0x21();
|
2021-03-24 17:43:23 +00:00
|
|
|
|
2021-12-26 14:17:13 +00:00
|
|
|
return GetAddrInfoRequestImpl(context, responseBufferPosition, responseBufferSize, true, optionsBufferPosition, optionsBufferSize);
|
2021-03-24 17:43:23 +00:00
|
|
|
}
|
|
|
|
|
2021-04-24 10:16:01 +00:00
|
|
|
private ResultCode GetHostByNameRequestImpl(ServiceCtx context, ulong inputBufferPosition, ulong inputBufferSize, ulong outputBufferPosition, ulong outputBufferSize, ulong optionsBufferPosition, ulong optionsBufferSize)
|
2018-10-09 23:15:07 +00:00
|
|
|
{
|
2021-10-24 22:13:20 +00:00
|
|
|
string name = MemoryHelper.ReadAsciiString(context.Memory, inputBufferPosition, (int)inputBufferSize);
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2021-03-24 17:43:23 +00:00
|
|
|
// TODO: Use params.
|
|
|
|
bool enableNsdResolve = (context.RequestData.ReadInt32() & 1) != 0;
|
2018-12-06 11:16:24 +00:00
|
|
|
int timeOut = context.RequestData.ReadInt32();
|
|
|
|
ulong pidPlaceholder = context.RequestData.ReadUInt64();
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2021-03-24 17:43:23 +00:00
|
|
|
if (optionsBufferSize > 0)
|
|
|
|
{
|
|
|
|
// TODO: Parse and use options.
|
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
IPHostEntry hostEntry = null;
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
NetDbError netDbErrorCode = NetDbError.Success;
|
|
|
|
GaiError errno = GaiError.Overflow;
|
2021-04-24 10:16:01 +00:00
|
|
|
ulong serializedSize = 0;
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2021-03-24 17:43:23 +00:00
|
|
|
if (name.Length <= byte.MaxValue)
|
2018-10-09 23:15:07 +00:00
|
|
|
{
|
2021-03-24 17:43:23 +00:00
|
|
|
string targetHost = name;
|
|
|
|
|
|
|
|
if (DnsBlacklist.IsHostBlocked(name))
|
2018-10-09 23:15:07 +00:00
|
|
|
{
|
2021-03-24 17:43:23 +00:00
|
|
|
Logger.Info?.Print(LogClass.ServiceSfdnsres, $"DNS Blocked: {name}");
|
|
|
|
|
|
|
|
netDbErrorCode = NetDbError.HostNotFound;
|
|
|
|
errno = GaiError.NoData;
|
2018-10-09 23:15:07 +00:00
|
|
|
}
|
2021-03-24 17:43:23 +00:00
|
|
|
else
|
2018-10-09 23:15:07 +00:00
|
|
|
{
|
2021-03-24 17:43:23 +00:00
|
|
|
Logger.Info?.Print(LogClass.ServiceSfdnsres, $"Trying to resolve: {name}");
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2021-03-24 17:43:23 +00:00
|
|
|
try
|
2018-10-09 23:15:07 +00:00
|
|
|
{
|
2021-03-24 17:43:23 +00:00
|
|
|
hostEntry = Dns.GetHostEntry(targetHost);
|
2018-10-09 23:15:07 +00:00
|
|
|
}
|
2021-03-24 17:43:23 +00:00
|
|
|
catch (SocketException exception)
|
2018-10-09 23:15:07 +00:00
|
|
|
{
|
2021-03-24 17:43:23 +00:00
|
|
|
netDbErrorCode = ConvertSocketErrorCodeToNetDbError(exception.ErrorCode);
|
|
|
|
errno = ConvertSocketErrorCodeToGaiError(exception.ErrorCode, errno);
|
2018-10-09 23:15:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
netDbErrorCode = NetDbError.HostNotFound;
|
2018-10-09 23:15:07 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (hostEntry != null)
|
2018-10-09 23:15:07 +00:00
|
|
|
{
|
2021-03-24 17:43:23 +00:00
|
|
|
IEnumerable<IPAddress> addresses = GetIpv4Addresses(hostEntry);
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2021-03-24 17:43:23 +00:00
|
|
|
if (!addresses.Any())
|
2018-10-09 23:15:07 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
errno = GaiError.NoData;
|
|
|
|
netDbErrorCode = NetDbError.NoAddress;
|
2018-10-09 23:15:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-24 17:43:23 +00:00
|
|
|
errno = GaiError.Success;
|
|
|
|
serializedSize = SerializeHostEntries(context, outputBufferPosition, outputBufferSize, hostEntry, addresses);
|
2018-10-09 23:15:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
context.ResponseData.Write((int)netDbErrorCode);
|
|
|
|
context.ResponseData.Write((int)errno);
|
|
|
|
context.ResponseData.Write(serializedSize);
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2019-07-14 19:04:38 +00:00
|
|
|
return ResultCode.Success;
|
2018-10-09 23:15:07 +00:00
|
|
|
}
|
|
|
|
|
2021-04-24 10:16:01 +00:00
|
|
|
private ResultCode GetHostByAddrRequestImpl(ServiceCtx context, ulong inputBufferPosition, ulong inputBufferSize, ulong outputBufferPosition, ulong outputBufferSize, ulong optionsBufferPosition, ulong optionsBufferSize)
|
2018-10-09 23:15:07 +00:00
|
|
|
{
|
2021-03-24 17:43:23 +00:00
|
|
|
byte[] rawIp = new byte[inputBufferSize];
|
2020-05-03 22:54:50 +00:00
|
|
|
|
2021-04-24 10:16:01 +00:00
|
|
|
context.Memory.Read(inputBufferPosition, rawIp);
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2021-03-24 17:43:23 +00:00
|
|
|
// TODO: Use params.
|
2018-12-06 11:16:24 +00:00
|
|
|
uint socketLength = context.RequestData.ReadUInt32();
|
|
|
|
uint type = context.RequestData.ReadUInt32();
|
|
|
|
int timeOut = context.RequestData.ReadInt32();
|
|
|
|
ulong pidPlaceholder = context.RequestData.ReadUInt64();
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2021-03-24 17:43:23 +00:00
|
|
|
if (optionsBufferSize > 0)
|
|
|
|
{
|
|
|
|
// TODO: Parse and use options.
|
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
IPHostEntry hostEntry = null;
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
NetDbError netDbErrorCode = NetDbError.Success;
|
|
|
|
GaiError errno = GaiError.AddressFamily;
|
2021-04-24 10:16:01 +00:00
|
|
|
ulong serializedSize = 0;
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (rawIp.Length == 4)
|
2018-10-09 23:15:07 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
IPAddress address = new IPAddress(rawIp);
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
hostEntry = Dns.GetHostEntry(address);
|
2018-10-09 23:15:07 +00:00
|
|
|
}
|
2018-12-06 11:16:24 +00:00
|
|
|
catch (SocketException exception)
|
2018-10-09 23:15:07 +00:00
|
|
|
{
|
2021-03-24 17:43:23 +00:00
|
|
|
netDbErrorCode = ConvertSocketErrorCodeToNetDbError(exception.ErrorCode);
|
|
|
|
errno = ConvertSocketErrorCodeToGaiError(exception.ErrorCode, errno);
|
2018-10-09 23:15:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
netDbErrorCode = NetDbError.NoAddress;
|
2018-10-09 23:15:07 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (hostEntry != null)
|
2018-10-09 23:15:07 +00:00
|
|
|
{
|
2021-03-24 17:43:23 +00:00
|
|
|
errno = GaiError.Success;
|
|
|
|
serializedSize = SerializeHostEntries(context, outputBufferPosition, outputBufferSize, hostEntry, GetIpv4Addresses(hostEntry));
|
2018-10-09 23:15:07 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
context.ResponseData.Write((int)netDbErrorCode);
|
|
|
|
context.ResponseData.Write((int)errno);
|
|
|
|
context.ResponseData.Write(serializedSize);
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2019-07-14 19:04:38 +00:00
|
|
|
return ResultCode.Success;
|
2018-10-09 23:15:07 +00:00
|
|
|
}
|
|
|
|
|
2021-04-24 10:16:01 +00:00
|
|
|
private ulong SerializeHostEntries(ServiceCtx context, ulong outputBufferPosition, ulong outputBufferSize, IPHostEntry hostEntry, IEnumerable<IPAddress> addresses = null)
|
2018-10-09 23:15:07 +00:00
|
|
|
{
|
2021-04-24 10:16:01 +00:00
|
|
|
ulong originalBufferPosition = outputBufferPosition;
|
|
|
|
ulong bufferPosition = originalBufferPosition;
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2021-03-24 17:43:23 +00:00
|
|
|
string hostName = hostEntry.HostName + '\0';
|
|
|
|
|
|
|
|
// h_name
|
2021-04-24 10:16:01 +00:00
|
|
|
context.Memory.Write(bufferPosition, Encoding.ASCII.GetBytes(hostName));
|
|
|
|
bufferPosition += (ulong)hostName.Length;
|
2021-03-24 17:43:23 +00:00
|
|
|
|
|
|
|
// h_aliases list size
|
2021-04-24 10:16:01 +00:00
|
|
|
context.Memory.Write(bufferPosition, BinaryPrimitives.ReverseEndianness(hostEntry.Aliases.Length));
|
|
|
|
bufferPosition += sizeof(int);
|
2021-03-24 17:43:23 +00:00
|
|
|
|
|
|
|
// Actual aliases
|
|
|
|
foreach (string alias in hostEntry.Aliases)
|
2018-10-09 23:15:07 +00:00
|
|
|
{
|
2021-04-24 10:16:01 +00:00
|
|
|
context.Memory.Write(bufferPosition, Encoding.ASCII.GetBytes(alias + '\0'));
|
|
|
|
bufferPosition += (ulong)(alias.Length + 1);
|
2018-10-09 23:15:07 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 17:43:23 +00:00
|
|
|
// h_addrtype but it's a short (also only support IPv4)
|
2021-04-24 10:16:01 +00:00
|
|
|
context.Memory.Write(bufferPosition, BinaryPrimitives.ReverseEndianness((short)AddressFamily.InterNetwork));
|
|
|
|
bufferPosition += sizeof(short);
|
2021-03-24 17:43:23 +00:00
|
|
|
|
|
|
|
// h_length but it's a short
|
2021-04-24 10:16:01 +00:00
|
|
|
context.Memory.Write(bufferPosition, BinaryPrimitives.ReverseEndianness((short)4));
|
|
|
|
bufferPosition += sizeof(short);
|
2021-03-24 17:43:23 +00:00
|
|
|
|
|
|
|
// Ip address count, we can only support ipv4 (blame Nintendo)
|
2021-04-24 10:16:01 +00:00
|
|
|
context.Memory.Write(bufferPosition, addresses != null ? BinaryPrimitives.ReverseEndianness(addresses.Count()) : 0);
|
|
|
|
bufferPosition += sizeof(int);
|
2021-03-24 17:43:23 +00:00
|
|
|
|
|
|
|
if (addresses != null)
|
|
|
|
{
|
|
|
|
foreach (IPAddress ip in addresses)
|
|
|
|
{
|
2021-04-24 10:16:01 +00:00
|
|
|
context.Memory.Write(bufferPosition, BinaryPrimitives.ReverseEndianness(BitConverter.ToInt32(ip.GetAddressBytes(), 0)));
|
|
|
|
bufferPosition += sizeof(int);
|
2021-03-24 17:43:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bufferPosition - originalBufferPosition;
|
2018-10-09 23:15:07 +00:00
|
|
|
}
|
|
|
|
|
2021-12-26 14:17:13 +00:00
|
|
|
private ResultCode GetAddrInfoRequestImpl(
|
|
|
|
ServiceCtx context,
|
|
|
|
ulong responseBufferPosition,
|
|
|
|
ulong responseBufferSize,
|
|
|
|
bool withOptions,
|
|
|
|
ulong optionsBufferPosition,
|
|
|
|
ulong optionsBufferSize)
|
2018-10-09 23:15:07 +00:00
|
|
|
{
|
2021-03-24 17:43:23 +00:00
|
|
|
bool enableNsdResolve = (context.RequestData.ReadInt32() & 1) != 0;
|
|
|
|
uint cancelHandle = context.RequestData.ReadUInt32();
|
|
|
|
|
2021-04-24 10:16:01 +00:00
|
|
|
string host = MemoryHelper.ReadAsciiString(context.Memory, context.Request.SendBuff[0].Position, (long)context.Request.SendBuff[0].Size);
|
|
|
|
string service = MemoryHelper.ReadAsciiString(context.Memory, context.Request.SendBuff[1].Position, (long)context.Request.SendBuff[1].Size);
|
2021-03-24 17:43:23 +00:00
|
|
|
|
|
|
|
// NOTE: We ignore hints for now.
|
|
|
|
DeserializeAddrInfos(context.Memory, (ulong)context.Request.SendBuff[2].Position, (ulong)context.Request.SendBuff[2].Size);
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2021-12-26 14:17:13 +00:00
|
|
|
if (withOptions)
|
2018-10-09 23:15:07 +00:00
|
|
|
{
|
2021-03-24 17:43:23 +00:00
|
|
|
// TODO: Find unknown, Parse and use options.
|
|
|
|
uint unknown = context.RequestData.ReadUInt32();
|
2018-10-09 23:15:07 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 17:43:23 +00:00
|
|
|
ulong pidPlaceHolder = context.RequestData.ReadUInt64();
|
|
|
|
|
|
|
|
Logger.Stub?.PrintStub(LogClass.ServiceSfdnsres, new { enableNsdResolve, cancelHandle, pidPlaceHolder, host, service });
|
|
|
|
|
|
|
|
IPHostEntry hostEntry = null;
|
|
|
|
|
|
|
|
NetDbError netDbErrorCode = NetDbError.Success;
|
|
|
|
GaiError errno = GaiError.AddressFamily;
|
|
|
|
ulong serializedSize = 0;
|
|
|
|
|
|
|
|
if (host.Length <= byte.MaxValue)
|
|
|
|
{
|
|
|
|
string targetHost = host;
|
|
|
|
|
|
|
|
if (DnsBlacklist.IsHostBlocked(host))
|
|
|
|
{
|
|
|
|
Logger.Info?.Print(LogClass.ServiceSfdnsres, $"DNS Blocked: {host}");
|
|
|
|
|
|
|
|
netDbErrorCode = NetDbError.HostNotFound;
|
|
|
|
errno = GaiError.NoData;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Logger.Info?.Print(LogClass.ServiceSfdnsres, $"Trying to resolve: {host}");
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
hostEntry = Dns.GetHostEntry(targetHost);
|
|
|
|
}
|
|
|
|
catch (SocketException exception)
|
|
|
|
{
|
|
|
|
netDbErrorCode = ConvertSocketErrorCodeToNetDbError(exception.ErrorCode);
|
|
|
|
errno = ConvertSocketErrorCodeToGaiError(exception.ErrorCode, errno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
netDbErrorCode = NetDbError.NoAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hostEntry != null)
|
|
|
|
{
|
|
|
|
int.TryParse(service, out int port);
|
|
|
|
|
|
|
|
errno = GaiError.Success;
|
|
|
|
serializedSize = SerializeAddrInfos(context, responseBufferPosition, responseBufferSize, hostEntry, port);
|
|
|
|
}
|
|
|
|
|
2021-12-26 14:17:13 +00:00
|
|
|
if (withOptions)
|
|
|
|
{
|
|
|
|
context.ResponseData.Write(serializedSize);
|
|
|
|
context.ResponseData.Write((int)errno);
|
|
|
|
context.ResponseData.Write((int)netDbErrorCode);
|
|
|
|
context.ResponseData.Write(0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
context.ResponseData.Write((int)netDbErrorCode);
|
|
|
|
context.ResponseData.Write((int)errno);
|
|
|
|
context.ResponseData.Write(serializedSize);
|
|
|
|
}
|
2021-03-24 17:43:23 +00:00
|
|
|
|
|
|
|
return ResultCode.Success;
|
2018-10-09 23:15:07 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 17:43:23 +00:00
|
|
|
private void DeserializeAddrInfos(IVirtualMemoryManager memory, ulong address, ulong size)
|
2018-10-09 23:15:07 +00:00
|
|
|
{
|
2021-03-24 17:43:23 +00:00
|
|
|
ulong endAddress = address + size;
|
|
|
|
|
|
|
|
while (address < endAddress)
|
|
|
|
{
|
|
|
|
AddrInfoSerializedHeader header = memory.Read<AddrInfoSerializedHeader>(address);
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2021-03-24 17:43:23 +00:00
|
|
|
if (header.Magic != SfdnsresContants.AddrInfoMagic)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2021-03-24 17:43:23 +00:00
|
|
|
address += (ulong)Unsafe.SizeOf<AddrInfoSerializedHeader>() + header.AddressLength;
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2021-03-24 17:43:23 +00:00
|
|
|
// ai_canonname
|
|
|
|
string canonname = string.Empty;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
byte chr = memory.Read<byte>(address++);
|
|
|
|
|
|
|
|
if (chr == 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
canonname += (char)chr;
|
|
|
|
}
|
|
|
|
}
|
2018-10-09 23:15:07 +00:00
|
|
|
}
|
|
|
|
|
2021-04-24 10:16:01 +00:00
|
|
|
private ulong SerializeAddrInfos(ServiceCtx context, ulong responseBufferPosition, ulong responseBufferSize, IPHostEntry hostEntry, int port)
|
2018-10-09 23:15:07 +00:00
|
|
|
{
|
2021-03-24 17:43:23 +00:00
|
|
|
ulong originalBufferPosition = (ulong)responseBufferPosition;
|
|
|
|
ulong bufferPosition = originalBufferPosition;
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2021-03-24 17:43:23 +00:00
|
|
|
string hostName = hostEntry.HostName + '\0';
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2021-03-24 17:43:23 +00:00
|
|
|
for (int i = 0; i < hostEntry.AddressList.Length; i++)
|
|
|
|
{
|
|
|
|
IPAddress ip = hostEntry.AddressList[i];
|
|
|
|
|
|
|
|
if (ip.AddressFamily != AddressFamily.InterNetwork)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
AddrInfoSerializedHeader header = new AddrInfoSerializedHeader(ip, 0);
|
|
|
|
|
|
|
|
// NOTE: 0 = Any
|
|
|
|
context.Memory.Write(bufferPosition, header);
|
|
|
|
bufferPosition += (ulong)Unsafe.SizeOf<AddrInfoSerializedHeader>();
|
|
|
|
|
|
|
|
// addrinfo_in
|
|
|
|
context.Memory.Write(bufferPosition, new AddrInfo4(ip, (short)port));
|
|
|
|
bufferPosition += header.AddressLength;
|
|
|
|
|
|
|
|
// ai_canonname
|
|
|
|
context.Memory.Write(bufferPosition, Encoding.ASCII.GetBytes(hostName));
|
|
|
|
bufferPosition += (ulong)hostName.Length;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Termination zero value.
|
|
|
|
context.Memory.Write(bufferPosition, 0);
|
2021-04-24 10:16:01 +00:00
|
|
|
bufferPosition += sizeof(int);
|
2021-03-24 17:43:23 +00:00
|
|
|
|
|
|
|
return bufferPosition - originalBufferPosition;
|
2018-10-09 23:15:07 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 17:43:23 +00:00
|
|
|
private IEnumerable<IPAddress> GetIpv4Addresses(IPHostEntry hostEntry)
|
2018-10-09 23:15:07 +00:00
|
|
|
{
|
2021-03-24 17:43:23 +00:00
|
|
|
return hostEntry.AddressList.Where(x => x.AddressFamily == AddressFamily.InterNetwork);
|
|
|
|
}
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2021-03-24 17:43:23 +00:00
|
|
|
private NetDbError ConvertSocketErrorCodeToNetDbError(int errorCode)
|
|
|
|
{
|
|
|
|
return errorCode switch
|
|
|
|
{
|
|
|
|
11001 => NetDbError.HostNotFound,
|
|
|
|
11002 => NetDbError.TryAgain,
|
|
|
|
11003 => NetDbError.NoRecovery,
|
|
|
|
11004 => NetDbError.NoData,
|
|
|
|
_ => NetDbError.Internal
|
|
|
|
};
|
|
|
|
}
|
2018-10-09 23:15:07 +00:00
|
|
|
|
2021-03-24 17:43:23 +00:00
|
|
|
private GaiError ConvertSocketErrorCodeToGaiError(int errorCode, GaiError errno)
|
|
|
|
{
|
|
|
|
return errorCode switch
|
|
|
|
{
|
|
|
|
11001 => GaiError.NoData,
|
|
|
|
10060 => GaiError.Again,
|
|
|
|
_ => errno
|
|
|
|
};
|
2018-10-09 23:15:07 +00:00
|
|
|
}
|
2018-02-28 20:58:04 +00:00
|
|
|
}
|
2019-07-12 01:13:43 +00:00
|
|
|
}
|