Move solution and projects to src

This commit is contained in:
TSR Berry
2023-04-08 01:22:00 +02:00
committed by Mary
parent cd124bda58
commit cee7121058
3466 changed files with 55 additions and 55 deletions

View File

@@ -0,0 +1,686 @@
using Ryujinx.Common.Logging;
using Ryujinx.Cpu;
using Ryujinx.HLE.HOS.Services.Sockets.Nsd.Manager;
using Ryujinx.HLE.HOS.Services.Sockets.Sfdnsres.Proxy;
using Ryujinx.HLE.HOS.Services.Sockets.Sfdnsres.Types;
using Ryujinx.Memory;
using System;
using System.Buffers.Binary;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
namespace Ryujinx.HLE.HOS.Services.Sockets.Sfdnsres
{
[Service("sfdnsres")]
class IResolver : IpcService
{
public IResolver(ServiceCtx context)
{
DnsMitmResolver.Instance.ReloadEntries(context);
}
[CommandCmif(0)]
// SetDnsAddressesPrivateRequest(u32, buffer<unknown, 5, 0>)
public ResultCode SetDnsAddressesPrivateRequest(ServiceCtx context)
{
uint cancelHandleRequest = context.RequestData.ReadUInt32();
ulong bufferPosition = context.Request.SendBuff[0].Position;
ulong bufferSize = context.Request.SendBuff[0].Size;
// 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 });
return ResultCode.NotAllocated;
}
[CommandCmif(1)]
// GetDnsAddressPrivateRequest(u32) -> buffer<unknown, 6, 0>
public ResultCode GetDnsAddressPrivateRequest(ServiceCtx context)
{
uint cancelHandleRequest = context.RequestData.ReadUInt32();
ulong bufferPosition = context.Request.ReceiveBuff[0].Position;
ulong bufferSize = context.Request.ReceiveBuff[0].Size;
// 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 });
return ResultCode.NotAllocated;
}
[CommandCmif(2)]
// GetHostByNameRequest(u8, u32, u64, pid, buffer<unknown, 5, 0>) -> (u32, u32, u32, buffer<unknown, 6, 0>)
public ResultCode GetHostByNameRequest(ServiceCtx context)
{
ulong inputBufferPosition = context.Request.SendBuff[0].Position;
ulong inputBufferSize = context.Request.SendBuff[0].Size;
ulong outputBufferPosition = context.Request.ReceiveBuff[0].Position;
ulong outputBufferSize = context.Request.ReceiveBuff[0].Size;
return GetHostByNameRequestImpl(context, inputBufferPosition, inputBufferSize, outputBufferPosition, outputBufferSize, false, 0, 0);
}
[CommandCmif(3)]
// GetHostByAddrRequest(u32, u32, u32, u64, pid, buffer<unknown, 5, 0>) -> (u32, u32, u32, buffer<unknown, 6, 0>)
public ResultCode GetHostByAddrRequest(ServiceCtx context)
{
ulong inputBufferPosition = context.Request.SendBuff[0].Position;
ulong inputBufferSize = context.Request.SendBuff[0].Size;
ulong outputBufferPosition = context.Request.ReceiveBuff[0].Position;
ulong outputBufferSize = context.Request.ReceiveBuff[0].Size;
return GetHostByAddrRequestImpl(context, inputBufferPosition, inputBufferSize, outputBufferPosition, outputBufferSize, false, 0, 0);
}
[CommandCmif(4)]
// 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
{
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"
};
ulong bufferPosition = context.Request.ReceiveBuff[0].Position;
ulong bufferSize = context.Request.ReceiveBuff[0].Size;
if ((ulong)(errorString.Length + 1) <= bufferSize)
{
context.Memory.Write(bufferPosition, Encoding.ASCII.GetBytes(errorString + '\0'));
resultCode = ResultCode.Success;
}
return resultCode;
}
[CommandCmif(5)]
// GetGaiStringErrorRequest(u32) -> buffer<byte, 6, 0>
public ResultCode GetGaiStringErrorRequest(ServiceCtx context)
{
ResultCode resultCode = ResultCode.NotAllocated;
GaiError errorCode = (GaiError)context.RequestData.ReadInt32();
if (errorCode > GaiError.Max)
{
errorCode = GaiError.Max;
}
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"
};
ulong bufferPosition = context.Request.ReceiveBuff[0].Position;
ulong bufferSize = context.Request.ReceiveBuff[0].Size;
if ((ulong)(errorString.Length + 1) <= bufferSize)
{
context.Memory.Write(bufferPosition, Encoding.ASCII.GetBytes(errorString + '\0'));
resultCode = ResultCode.Success;
}
return resultCode;
}
[CommandCmif(6)]
// 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)
{
ulong responseBufferPosition = context.Request.ReceiveBuff[0].Position;
ulong responseBufferSize = context.Request.ReceiveBuff[0].Size;
return GetAddrInfoRequestImpl(context, responseBufferPosition, responseBufferSize, false, 0, 0);
}
[CommandCmif(8)]
// GetCancelHandleRequest(u64, pid) -> u32
public ResultCode GetCancelHandleRequest(ServiceCtx context)
{
ulong pidPlaceHolder = context.RequestData.ReadUInt64();
uint cancelHandleRequest = 0;
context.ResponseData.Write(cancelHandleRequest);
Logger.Stub?.PrintStub(LogClass.ServiceSfdnsres, new { cancelHandleRequest });
return ResultCode.Success;
}
[CommandCmif(9)]
// CancelRequest(u32, u64, pid)
public ResultCode CancelRequest(ServiceCtx context)
{
uint cancelHandleRequest = context.RequestData.ReadUInt32();
ulong pidPlaceHolder = context.RequestData.ReadUInt64();
Logger.Stub?.PrintStub(LogClass.ServiceSfdnsres, new { cancelHandleRequest });
return ResultCode.Success;
}
[CommandCmif(10)] // 5.0.0+
// 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)
{
(ulong inputBufferPosition, ulong inputBufferSize) = context.Request.GetBufferType0x21();
(ulong outputBufferPosition, ulong outputBufferSize) = context.Request.GetBufferType0x22();
(ulong optionsBufferPosition, ulong optionsBufferSize) = context.Request.GetBufferType0x21();
return GetHostByNameRequestImpl(
context,
inputBufferPosition,
inputBufferSize,
outputBufferPosition,
outputBufferSize,
true,
optionsBufferPosition,
optionsBufferSize);
}
[CommandCmif(11)] // 5.0.0+
// 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)
{
(ulong inputBufferPosition, ulong inputBufferSize) = context.Request.GetBufferType0x21();
(ulong outputBufferPosition, ulong outputBufferSize) = context.Request.GetBufferType0x22();
(ulong optionsBufferPosition, ulong optionsBufferSize) = context.Request.GetBufferType0x21();
return GetHostByAddrRequestImpl(
context,
inputBufferPosition,
inputBufferSize,
outputBufferPosition,
outputBufferSize,
true,
optionsBufferPosition,
optionsBufferSize);
}
[CommandCmif(12)] // 5.0.0+
// 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)
{
(ulong outputBufferPosition, ulong outputBufferSize) = context.Request.GetBufferType0x22();
(ulong optionsBufferPosition, ulong optionsBufferSize) = context.Request.GetBufferType0x21();
return GetAddrInfoRequestImpl(context, outputBufferPosition, outputBufferSize, true, optionsBufferPosition, optionsBufferSize);
}
[CommandCmif(14)] // 5.0.0+
// ResolverSetOptionRequest(buffer<unknown, 5, 0>, u64 unknown, u64 pid_placeholder, pid) -> (i32 ret, u32 bsd_errno)
public ResultCode ResolverSetOptionRequest(ServiceCtx context)
{
ulong bufferPosition = context.Request.SendBuff[0].Position;
ulong bufferSize = context.Request.SendBuff[0].Size;
ulong unknown = context.RequestData.ReadUInt64();
byte[] buffer = new byte[bufferSize];
context.Memory.Read(bufferPosition, buffer);
// TODO: Parse and use options.
Logger.Stub?.PrintStub(LogClass.ServiceSfdnsres, new { unknown });
NetDbError netDbErrorCode = NetDbError.Success;
GaiError errno = GaiError.Success;
context.ResponseData.Write((int)errno);
context.ResponseData.Write((int)netDbErrorCode);
return ResultCode.Success;
}
// Atmosphère extension for dns_mitm
[CommandCmif(65000)]
// AtmosphereReloadHostsFile()
public ResultCode AtmosphereReloadHostsFile(ServiceCtx context)
{
DnsMitmResolver.Instance.ReloadEntries(context);
return ResultCode.Success;
}
private static ResultCode GetHostByNameRequestImpl(
ServiceCtx context,
ulong inputBufferPosition,
ulong inputBufferSize,
ulong outputBufferPosition,
ulong outputBufferSize,
bool withOptions,
ulong optionsBufferPosition,
ulong optionsBufferSize)
{
string host = MemoryHelper.ReadAsciiString(context.Memory, inputBufferPosition, (int)inputBufferSize);
if (!context.Device.Configuration.EnableInternetAccess)
{
Logger.Info?.Print(LogClass.ServiceSfdnsres, $"Guest network access disabled, DNS Blocked: {host}");
WriteResponse(context, withOptions, 0, GaiError.NoData, NetDbError.HostNotFound);
return ResultCode.Success;
}
// TODO: Use params.
bool enableNsdResolve = (context.RequestData.ReadInt32() & 1) != 0;
int timeOut = context.RequestData.ReadInt32();
ulong pidPlaceholder = context.RequestData.ReadUInt64();
if (withOptions)
{
// TODO: Parse and use options.
}
IPHostEntry hostEntry = null;
NetDbError netDbErrorCode = NetDbError.Success;
GaiError errno = GaiError.Overflow;
int serializedSize = 0;
if (host.Length <= byte.MaxValue)
{
if (enableNsdResolve)
{
if (FqdnResolver.Resolve(host, out string newAddress) == Nsd.ResultCode.Success)
{
host = newAddress;
}
}
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 = DnsMitmResolver.Instance.ResolveAddress(targetHost);
}
catch (SocketException exception)
{
netDbErrorCode = ConvertSocketErrorCodeToNetDbError(exception.ErrorCode);
errno = ConvertSocketErrorCodeToGaiError(exception.ErrorCode, errno);
}
}
}
else
{
netDbErrorCode = NetDbError.HostNotFound;
}
if (hostEntry != null)
{
IEnumerable<IPAddress> addresses = GetIpv4Addresses(hostEntry);
if (!addresses.Any())
{
errno = GaiError.NoData;
netDbErrorCode = NetDbError.NoAddress;
}
else
{
errno = GaiError.Success;
serializedSize = SerializeHostEntries(context, outputBufferPosition, outputBufferSize, hostEntry, addresses);
}
}
WriteResponse(context, withOptions, serializedSize, errno, netDbErrorCode);
return ResultCode.Success;
}
private static ResultCode GetHostByAddrRequestImpl(
ServiceCtx context,
ulong inputBufferPosition,
ulong inputBufferSize,
ulong outputBufferPosition,
ulong outputBufferSize,
bool withOptions,
ulong optionsBufferPosition,
ulong optionsBufferSize)
{
if (!context.Device.Configuration.EnableInternetAccess)
{
Logger.Info?.Print(LogClass.ServiceSfdnsres, $"Guest network access disabled, DNS Blocked.");
WriteResponse(context, withOptions, 0, GaiError.NoData, NetDbError.HostNotFound);
return ResultCode.Success;
}
byte[] rawIp = new byte[inputBufferSize];
context.Memory.Read(inputBufferPosition, rawIp);
// TODO: Use params.
uint socketLength = context.RequestData.ReadUInt32();
uint type = context.RequestData.ReadUInt32();
int timeOut = context.RequestData.ReadInt32();
ulong pidPlaceholder = context.RequestData.ReadUInt64();
if (withOptions)
{
// TODO: Parse and use options.
}
IPHostEntry hostEntry = null;
NetDbError netDbErrorCode = NetDbError.Success;
GaiError errno = GaiError.AddressFamily;
int serializedSize = 0;
if (rawIp.Length == 4)
{
try
{
IPAddress address = new IPAddress(rawIp);
hostEntry = Dns.GetHostEntry(address);
}
catch (SocketException exception)
{
netDbErrorCode = ConvertSocketErrorCodeToNetDbError(exception.ErrorCode);
errno = ConvertSocketErrorCodeToGaiError(exception.ErrorCode, errno);
}
}
else
{
netDbErrorCode = NetDbError.NoAddress;
}
if (hostEntry != null)
{
errno = GaiError.Success;
serializedSize = SerializeHostEntries(context, outputBufferPosition, outputBufferSize, hostEntry, GetIpv4Addresses(hostEntry));
}
WriteResponse(context, withOptions, serializedSize, errno, netDbErrorCode);
return ResultCode.Success;
}
private static int SerializeHostEntries(ServiceCtx context, ulong outputBufferPosition, ulong outputBufferSize, IPHostEntry hostEntry, IEnumerable<IPAddress> addresses = null)
{
ulong originalBufferPosition = outputBufferPosition;
ulong bufferPosition = originalBufferPosition;
string hostName = hostEntry.HostName + '\0';
// h_name
context.Memory.Write(bufferPosition, Encoding.ASCII.GetBytes(hostName));
bufferPosition += (ulong)hostName.Length;
// h_aliases list size
context.Memory.Write(bufferPosition, BinaryPrimitives.ReverseEndianness(hostEntry.Aliases.Length));
bufferPosition += sizeof(int);
// Actual aliases
foreach (string alias in hostEntry.Aliases)
{
context.Memory.Write(bufferPosition, Encoding.ASCII.GetBytes(alias + '\0'));
bufferPosition += (ulong)(alias.Length + 1);
}
// h_addrtype but it's a short (also only support IPv4)
context.Memory.Write(bufferPosition, BinaryPrimitives.ReverseEndianness((short)AddressFamily.InterNetwork));
bufferPosition += sizeof(short);
// h_length but it's a short
context.Memory.Write(bufferPosition, BinaryPrimitives.ReverseEndianness((short)4));
bufferPosition += sizeof(short);
// Ip address count, we can only support ipv4 (blame Nintendo)
context.Memory.Write(bufferPosition, addresses != null ? BinaryPrimitives.ReverseEndianness(addresses.Count()) : 0);
bufferPosition += sizeof(int);
if (addresses != null)
{
foreach (IPAddress ip in addresses)
{
context.Memory.Write(bufferPosition, BinaryPrimitives.ReverseEndianness(BitConverter.ToInt32(ip.GetAddressBytes(), 0)));
bufferPosition += sizeof(int);
}
}
return (int)(bufferPosition - originalBufferPosition);
}
private static ResultCode GetAddrInfoRequestImpl(
ServiceCtx context,
ulong responseBufferPosition,
ulong responseBufferSize,
bool withOptions,
ulong optionsBufferPosition,
ulong optionsBufferSize)
{
bool enableNsdResolve = (context.RequestData.ReadInt32() & 1) != 0;
uint cancelHandle = context.RequestData.ReadUInt32();
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);
if (!context.Device.Configuration.EnableInternetAccess)
{
Logger.Info?.Print(LogClass.ServiceSfdnsres, $"Guest network access disabled, DNS Blocked: {host}");
WriteResponse(context, withOptions, 0, GaiError.NoData, NetDbError.HostNotFound);
return ResultCode.Success;
}
// NOTE: We ignore hints for now.
List<AddrInfoSerialized> hints = DeserializeAddrInfos(context.Memory, context.Request.SendBuff[2].Position, context.Request.SendBuff[2].Size);
if (withOptions)
{
// TODO: Find unknown, Parse and use options.
uint unknown = context.RequestData.ReadUInt32();
}
ulong pidPlaceHolder = context.RequestData.ReadUInt64();
IPHostEntry hostEntry = null;
NetDbError netDbErrorCode = NetDbError.Success;
GaiError errno = GaiError.AddressFamily;
int serializedSize = 0;
if (host.Length <= byte.MaxValue)
{
if (enableNsdResolve)
{
if (FqdnResolver.Resolve(host, out string newAddress) == Nsd.ResultCode.Success)
{
host = newAddress;
}
}
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 = DnsMitmResolver.Instance.ResolveAddress(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);
}
WriteResponse(context, withOptions, serializedSize, errno, netDbErrorCode);
return ResultCode.Success;
}
private static List<AddrInfoSerialized> DeserializeAddrInfos(IVirtualMemoryManager memory, ulong address, ulong size)
{
List<AddrInfoSerialized> result = new();
ReadOnlySpan<byte> data = memory.GetSpan(address, (int)size);
while (!data.IsEmpty)
{
AddrInfoSerialized info = AddrInfoSerialized.Read(data, out data);
if (info == null)
{
break;
}
result.Add(info);
}
return result;
}
private static int SerializeAddrInfos(ServiceCtx context, ulong responseBufferPosition, ulong responseBufferSize, IPHostEntry hostEntry, int port)
{
ulong originalBufferPosition = responseBufferPosition;
ulong bufferPosition = originalBufferPosition;
byte[] hostName = Encoding.ASCII.GetBytes(hostEntry.HostName + '\0');
using (WritableRegion region = context.Memory.GetWritableRegion(responseBufferPosition, (int)responseBufferSize))
{
Span<byte> data = region.Memory.Span;
for (int i = 0; i < hostEntry.AddressList.Length; i++)
{
IPAddress ip = hostEntry.AddressList[i];
if (ip.AddressFamily != AddressFamily.InterNetwork)
{
continue;
}
// NOTE: 0 = Any
AddrInfoSerializedHeader header = new(ip, 0);
AddrInfo4 addr = new(ip, (short)port);
AddrInfoSerialized info = new(header, addr, null, hostEntry.HostName);
data = info.Write(data);
}
uint sentinel = 0;
MemoryMarshal.Write(data, ref sentinel);
data = data[sizeof(uint)..];
return region.Memory.Span.Length - data.Length;
}
}
private static void WriteResponse(
ServiceCtx context,
bool withOptions,
int serializedSize,
GaiError errno,
NetDbError netDbErrorCode)
{
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);
}
}
private static IEnumerable<IPAddress> GetIpv4Addresses(IPHostEntry hostEntry)
{
return hostEntry.AddressList.Where(x => x.AddressFamily == AddressFamily.InterNetwork);
}
private static NetDbError ConvertSocketErrorCodeToNetDbError(int errorCode)
{
return errorCode switch
{
11001 => NetDbError.HostNotFound,
11002 => NetDbError.TryAgain,
11003 => NetDbError.NoRecovery,
11004 => NetDbError.NoData,
_ => NetDbError.Internal
};
}
private static GaiError ConvertSocketErrorCodeToGaiError(int errorCode, GaiError errno)
{
return errorCode switch
{
11001 => GaiError.NoData,
10060 => GaiError.Again,
_ => errno
};
}
}
}

View File

@@ -0,0 +1,44 @@
using System.Text.RegularExpressions;
namespace Ryujinx.HLE.HOS.Services.Sockets.Sfdnsres.Proxy
{
static partial class DnsBlacklist
{
const RegexOptions RegexOpts = RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture;
[GeneratedRegex(@"^(.*)\-lp1\.(n|s)\.n\.srv\.nintendo\.net$", RegexOpts)]
private static partial Regex BlockedHost1();
[GeneratedRegex(@"^(.*)\-lp1\.lp1\.t\.npln\.srv\.nintendo\.net$", RegexOpts)]
private static partial Regex BlockedHost2();
[GeneratedRegex(@"^(.*)\-lp1\.(znc|p)\.srv\.nintendo\.net$", RegexOpts)]
private static partial Regex BlockedHost3();
[GeneratedRegex(@"^(.*)\-sb\-api\.accounts\.nintendo\.com$", RegexOpts)]
private static partial Regex BlockedHost4();
[GeneratedRegex(@"^(.*)\-sb\.accounts\.nintendo\.com$", RegexOpts)]
private static partial Regex BlockedHost5();
[GeneratedRegex(@"^accounts\.nintendo\.com$", RegexOpts)]
private static partial Regex BlockedHost6();
private static readonly Regex[] BlockedHosts = {
BlockedHost1(),
BlockedHost2(),
BlockedHost3(),
BlockedHost4(),
BlockedHost5(),
BlockedHost6()
};
public static bool IsHostBlocked(string host)
{
foreach (Regex regex in BlockedHosts)
{
if (regex.IsMatch(host))
{
return true;
}
}
return false;
}
}
}

View File

@@ -0,0 +1,106 @@
using Ryujinx.Common.Logging;
using Ryujinx.HLE.HOS.Services.Sockets.Nsd;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Enumeration;
using System.Net;
namespace Ryujinx.HLE.HOS.Services.Sockets.Sfdnsres.Proxy
{
class DnsMitmResolver
{
private const string HostsFilePath = "/atmosphere/hosts/default.txt";
private static DnsMitmResolver _instance;
public static DnsMitmResolver Instance => _instance ??= new DnsMitmResolver();
private readonly Dictionary<string, IPAddress> _mitmHostEntries = new();
public void ReloadEntries(ServiceCtx context)
{
string sdPath = context.Device.Configuration.VirtualFileSystem.GetSdCardPath();
string filePath = context.Device.Configuration.VirtualFileSystem.GetFullPath(sdPath, HostsFilePath);
_mitmHostEntries.Clear();
if (File.Exists(filePath))
{
using FileStream fileStream = File.Open(filePath, FileMode.Open, FileAccess.Read);
using StreamReader reader = new(fileStream);
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
if (line == null)
{
break;
}
// Ignore comments and empty lines
if (line.StartsWith("#") || line.Trim().Length == 0)
{
continue;
}
string[] entry = line.Split(new[] { ' ', '\t' }, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
// Hosts file example entry:
// 127.0.0.1 localhost loopback
// 0. Check the size of the array
if (entry.Length < 2)
{
Logger.Warning?.PrintMsg(LogClass.ServiceBsd, $"Invalid entry in hosts file: {line}");
continue;
}
// 1. Parse the address
if (!IPAddress.TryParse(entry[0], out IPAddress address))
{
Logger.Warning?.PrintMsg(LogClass.ServiceBsd, $"Failed to parse IP address in hosts file: {entry[0]}");
continue;
}
// 2. Check for AMS hosts file extension: "%"
for (int i = 1; i < entry.Length; i++)
{
entry[i] = entry[i].Replace("%", IManager.NsdSettings.Environment);
}
// 3. Add hostname to entry dictionary (updating duplicate entries)
foreach (string hostname in entry[1..])
{
_mitmHostEntries[hostname] = address;
}
}
}
}
public IPHostEntry ResolveAddress(string host)
{
foreach (var hostEntry in _mitmHostEntries)
{
// Check for AMS hosts file extension: "*"
// NOTE: MatchesSimpleExpression also allows "?" as a wildcard
if (FileSystemName.MatchesSimpleExpression(hostEntry.Key, host))
{
Logger.Info?.PrintMsg(LogClass.ServiceBsd, $"Redirecting '{host}' to: {hostEntry.Value}");
return new IPHostEntry
{
AddressList = new[] { hostEntry.Value },
HostName = hostEntry.Key,
Aliases = Array.Empty<string>()
};
}
}
// No match has been found, resolve the host using regular dns
return Dns.GetHostEntry(host);
}
}
}

View File

@@ -0,0 +1,51 @@
using Ryujinx.Common.Memory;
using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Ryujinx.HLE.HOS.Services.Sockets.Sfdnsres.Types
{
[StructLayout(LayoutKind.Sequential, Pack = 1, Size = 0x10)]
struct AddrInfo4
{
public byte Length;
public byte Family;
public short Port;
public Array4<byte> Address;
public Array8<byte> Padding;
public AddrInfo4(IPAddress address, short port)
{
Length = (byte)Unsafe.SizeOf<Array4<byte>>();
Family = (byte)AddressFamily.InterNetwork;
Port = IPAddress.HostToNetworkOrder(port);
Address = new Array4<byte>();
address.TryWriteBytes(Address.AsSpan(), out _);
}
public void ToNetworkOrder()
{
Port = IPAddress.HostToNetworkOrder(Port);
RawIpv4AddressNetworkEndianSwap(ref Address);
}
public void ToHostOrder()
{
Port = IPAddress.NetworkToHostOrder(Port);
RawIpv4AddressNetworkEndianSwap(ref Address);
}
public static void RawIpv4AddressNetworkEndianSwap(ref Array4<byte> address)
{
if (BitConverter.IsLittleEndian)
{
address.AsSpan().Reverse();
}
}
}
}

View File

@@ -0,0 +1,143 @@
using Ryujinx.Common.Memory;
using Ryujinx.HLE.Utilities;
using System;
using System.Diagnostics;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
namespace Ryujinx.HLE.HOS.Services.Sockets.Sfdnsres.Types
{
class AddrInfoSerialized
{
public AddrInfoSerializedHeader Header;
public AddrInfo4? SocketAddress;
public Array4<byte>? RawIPv4Address;
public string CanonicalName;
public AddrInfoSerialized(AddrInfoSerializedHeader header, AddrInfo4? address, Array4<byte>? rawIPv4Address, string canonicalName)
{
Header = header;
SocketAddress = address;
RawIPv4Address = rawIPv4Address;
CanonicalName = canonicalName;
}
public static AddrInfoSerialized Read(ReadOnlySpan<byte> buffer, out ReadOnlySpan<byte> rest)
{
if (!MemoryMarshal.TryRead(buffer, out AddrInfoSerializedHeader header))
{
rest = buffer;
return null;
}
AddrInfo4? socketAddress = null;
Array4<byte>? rawIPv4Address = null;
string canonicalName;
buffer = buffer[Unsafe.SizeOf<AddrInfoSerializedHeader>()..];
header.ToHostOrder();
if (header.Magic != SfdnsresContants.AddrInfoMagic)
{
rest = buffer;
return null;
}
Debug.Assert(header.Magic == SfdnsresContants.AddrInfoMagic);
if (header.AddressLength == 0)
{
rest = buffer;
return null;
}
if (header.Family == (int)AddressFamily.InterNetwork)
{
socketAddress = MemoryMarshal.Read<AddrInfo4>(buffer);
socketAddress.Value.ToHostOrder();
buffer = buffer[Unsafe.SizeOf<AddrInfo4>()..];
}
// AF_INET6
else if (header.Family == 28)
{
throw new NotImplementedException();
}
else
{
// Nintendo hardcode 4 bytes in that case here.
Array4<byte> address = MemoryMarshal.Read<Array4<byte>>(buffer);
AddrInfo4.RawIpv4AddressNetworkEndianSwap(ref address);
rawIPv4Address = address;
buffer = buffer[Unsafe.SizeOf<Array4<byte>>()..];
}
canonicalName = StringUtils.ReadUtf8String(buffer, out int dataRead);
buffer = buffer[dataRead..];
rest = buffer;
return new AddrInfoSerialized(header, socketAddress, rawIPv4Address, canonicalName);
}
public Span<byte> Write(Span<byte> buffer)
{
int familly = Header.Family;
Header.ToNetworkOrder();
MemoryMarshal.Write(buffer, ref Header);
buffer = buffer[Unsafe.SizeOf<AddrInfoSerializedHeader>()..];
if (familly == (int)AddressFamily.InterNetwork)
{
AddrInfo4 socketAddress = SocketAddress.Value;
socketAddress.ToNetworkOrder();
MemoryMarshal.Write(buffer, ref socketAddress);
buffer = buffer[Unsafe.SizeOf<AddrInfo4>()..];
}
// AF_INET6
else if (familly == 28)
{
throw new NotImplementedException();
}
else
{
Array4<byte> rawIPv4Address = RawIPv4Address.Value;
AddrInfo4.RawIpv4AddressNetworkEndianSwap(ref rawIPv4Address);
MemoryMarshal.Write(buffer, ref rawIPv4Address);
buffer = buffer[Unsafe.SizeOf<Array4<byte>>()..];
}
if (CanonicalName == null)
{
buffer[0] = 0;
buffer = buffer[1..];
}
else
{
byte[] canonicalName = Encoding.ASCII.GetBytes(CanonicalName + '\0');
canonicalName.CopyTo(buffer);
buffer = buffer[canonicalName.Length..];
}
return buffer;
}
}
}

View File

@@ -0,0 +1,57 @@
using Ryujinx.Common.Memory;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Ryujinx.HLE.HOS.Services.Sockets.Sfdnsres.Types
{
[StructLayout(LayoutKind.Sequential, Pack = 1, Size = 6 * sizeof(int))]
struct AddrInfoSerializedHeader
{
public uint Magic;
public int Flags;
public int Family;
public int SocketType;
public int Protocol;
public uint AddressLength;
public AddrInfoSerializedHeader(IPAddress address, SocketType socketType)
{
Magic = SfdnsresContants.AddrInfoMagic;
Flags = 0;
Family = (int)address.AddressFamily;
SocketType = (int)socketType;
Protocol = 0;
if (address.AddressFamily == AddressFamily.InterNetwork)
{
AddressLength = (uint)Unsafe.SizeOf<AddrInfo4>();
}
else
{
AddressLength = (uint)Unsafe.SizeOf<Array4<byte>>();
}
}
public void ToNetworkOrder()
{
Magic = (uint)IPAddress.HostToNetworkOrder((int)Magic);
Flags = IPAddress.HostToNetworkOrder(Flags);
Family = IPAddress.HostToNetworkOrder(Family);
SocketType = IPAddress.HostToNetworkOrder(SocketType);
Protocol = IPAddress.HostToNetworkOrder(Protocol);
AddressLength = (uint)IPAddress.HostToNetworkOrder((int)AddressLength);
}
public void ToHostOrder()
{
Magic = (uint)IPAddress.NetworkToHostOrder((int)Magic);
Flags = IPAddress.NetworkToHostOrder(Flags);
Family = IPAddress.NetworkToHostOrder(Family);
SocketType = IPAddress.NetworkToHostOrder(SocketType);
Protocol = IPAddress.NetworkToHostOrder(Protocol);
AddressLength = (uint)IPAddress.NetworkToHostOrder((int)AddressLength);
}
}
}

View File

@@ -0,0 +1,22 @@
namespace Ryujinx.HLE.HOS.Services.Sockets.Sfdnsres
{
enum GaiError
{
Success,
AddressFamily,
Again,
BadFlags,
Fail,
Family,
Memory,
NoData,
NoName,
Service,
SocketType,
System,
BadHints,
Protocol,
Overflow,
Max
}
}

View File

@@ -0,0 +1,13 @@
namespace Ryujinx.HLE.HOS.Services.Sockets.Sfdnsres
{
enum NetDbError
{
Internal = -1,
Success,
HostNotFound,
TryAgain,
NoRecovery,
NoData,
NoAddress = NoData
}
}

View File

@@ -0,0 +1,7 @@
namespace Ryujinx.HLE.HOS.Services.Sockets.Sfdnsres.Types
{
static class SfdnsresContants
{
public const uint AddrInfoMagic = 0xBEEFCAFE;
}
}