IResolver.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.Cpu;
  3. using Ryujinx.HLE.HOS.Services.Sockets.Nsd.Manager;
  4. using Ryujinx.HLE.HOS.Services.Sockets.Sfdnsres.Proxy;
  5. using Ryujinx.HLE.HOS.Services.Sockets.Sfdnsres.Types;
  6. using Ryujinx.Memory;
  7. using System;
  8. using System.Buffers.Binary;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Net;
  12. using System.Net.Sockets;
  13. using System.Runtime.InteropServices;
  14. using System.Text;
  15. namespace Ryujinx.HLE.HOS.Services.Sockets.Sfdnsres
  16. {
  17. [Service("sfdnsres")]
  18. class IResolver : IpcService
  19. {
  20. public IResolver(ServiceCtx context)
  21. {
  22. DnsMitmResolver.Instance.ReloadEntries(context);
  23. }
  24. [CommandHipc(0)]
  25. // SetDnsAddressesPrivateRequest(u32, buffer<unknown, 5, 0>)
  26. public ResultCode SetDnsAddressesPrivateRequest(ServiceCtx context)
  27. {
  28. uint cancelHandleRequest = context.RequestData.ReadUInt32();
  29. ulong bufferPosition = context.Request.SendBuff[0].Position;
  30. ulong bufferSize = context.Request.SendBuff[0].Size;
  31. // TODO: This is stubbed in 2.0.0+, reverse 1.0.0 version for the sake of completeness.
  32. Logger.Stub?.PrintStub(LogClass.ServiceSfdnsres, new { cancelHandleRequest });
  33. return ResultCode.NotAllocated;
  34. }
  35. [CommandHipc(1)]
  36. // GetDnsAddressPrivateRequest(u32) -> buffer<unknown, 6, 0>
  37. public ResultCode GetDnsAddressPrivateRequest(ServiceCtx context)
  38. {
  39. uint cancelHandleRequest = context.RequestData.ReadUInt32();
  40. ulong bufferPosition = context.Request.ReceiveBuff[0].Position;
  41. ulong bufferSize = context.Request.ReceiveBuff[0].Size;
  42. // TODO: This is stubbed in 2.0.0+, reverse 1.0.0 version for the sake of completeness.
  43. Logger.Stub?.PrintStub(LogClass.ServiceSfdnsres, new { cancelHandleRequest });
  44. return ResultCode.NotAllocated;
  45. }
  46. [CommandHipc(2)]
  47. // GetHostByNameRequest(u8, u32, u64, pid, buffer<unknown, 5, 0>) -> (u32, u32, u32, buffer<unknown, 6, 0>)
  48. public ResultCode GetHostByNameRequest(ServiceCtx context)
  49. {
  50. ulong inputBufferPosition = context.Request.SendBuff[0].Position;
  51. ulong inputBufferSize = context.Request.SendBuff[0].Size;
  52. ulong outputBufferPosition = context.Request.ReceiveBuff[0].Position;
  53. ulong outputBufferSize = context.Request.ReceiveBuff[0].Size;
  54. return GetHostByNameRequestImpl(context, inputBufferPosition, inputBufferSize, outputBufferPosition, outputBufferSize, false, 0, 0);
  55. }
  56. [CommandHipc(3)]
  57. // GetHostByAddrRequest(u32, u32, u32, u64, pid, buffer<unknown, 5, 0>) -> (u32, u32, u32, buffer<unknown, 6, 0>)
  58. public ResultCode GetHostByAddrRequest(ServiceCtx context)
  59. {
  60. ulong inputBufferPosition = context.Request.SendBuff[0].Position;
  61. ulong inputBufferSize = context.Request.SendBuff[0].Size;
  62. ulong outputBufferPosition = context.Request.ReceiveBuff[0].Position;
  63. ulong outputBufferSize = context.Request.ReceiveBuff[0].Size;
  64. return GetHostByAddrRequestImpl(context, inputBufferPosition, inputBufferSize, outputBufferPosition, outputBufferSize, false, 0, 0);
  65. }
  66. [CommandHipc(4)]
  67. // GetHostStringErrorRequest(u32) -> buffer<unknown, 6, 0>
  68. public ResultCode GetHostStringErrorRequest(ServiceCtx context)
  69. {
  70. ResultCode resultCode = ResultCode.NotAllocated;
  71. NetDbError errorCode = (NetDbError)context.RequestData.ReadInt32();
  72. string errorString = errorCode switch
  73. {
  74. NetDbError.Success => "Resolver Error 0 (no error)",
  75. NetDbError.HostNotFound => "Unknown host",
  76. NetDbError.TryAgain => "Host name lookup failure",
  77. NetDbError.NoRecovery => "Unknown server error",
  78. NetDbError.NoData => "No address associated with name",
  79. _ => (errorCode <= NetDbError.Internal) ? "Resolver internal error" : "Unknown resolver error"
  80. };
  81. ulong bufferPosition = context.Request.ReceiveBuff[0].Position;
  82. ulong bufferSize = context.Request.ReceiveBuff[0].Size;
  83. if ((ulong)(errorString.Length + 1) <= bufferSize)
  84. {
  85. context.Memory.Write(bufferPosition, Encoding.ASCII.GetBytes(errorString + '\0'));
  86. resultCode = ResultCode.Success;
  87. }
  88. return resultCode;
  89. }
  90. [CommandHipc(5)]
  91. // GetGaiStringErrorRequest(u32) -> buffer<byte, 6, 0>
  92. public ResultCode GetGaiStringErrorRequest(ServiceCtx context)
  93. {
  94. ResultCode resultCode = ResultCode.NotAllocated;
  95. GaiError errorCode = (GaiError)context.RequestData.ReadInt32();
  96. if (errorCode > GaiError.Max)
  97. {
  98. errorCode = GaiError.Max;
  99. }
  100. string errorString = errorCode switch
  101. {
  102. GaiError.AddressFamily => "Address family for hostname not supported",
  103. GaiError.Again => "Temporary failure in name resolution",
  104. GaiError.BadFlags => "Invalid value for ai_flags",
  105. GaiError.Fail => "Non-recoverable failure in name resolution",
  106. GaiError.Family => "ai_family not supported",
  107. GaiError.Memory => "Memory allocation failure",
  108. GaiError.NoData => "No address associated with hostname",
  109. GaiError.NoName => "hostname nor servname provided, or not known",
  110. GaiError.Service => "servname not supported for ai_socktype",
  111. GaiError.SocketType => "ai_socktype not supported",
  112. GaiError.System => "System error returned in errno",
  113. GaiError.BadHints => "Invalid value for hints",
  114. GaiError.Protocol => "Resolved protocol is unknown",
  115. GaiError.Overflow => "Argument buffer overflow",
  116. GaiError.Max => "Unknown error",
  117. _ => "Success"
  118. };
  119. ulong bufferPosition = context.Request.ReceiveBuff[0].Position;
  120. ulong bufferSize = context.Request.ReceiveBuff[0].Size;
  121. if ((ulong)(errorString.Length + 1) <= bufferSize)
  122. {
  123. context.Memory.Write(bufferPosition, Encoding.ASCII.GetBytes(errorString + '\0'));
  124. resultCode = ResultCode.Success;
  125. }
  126. return resultCode;
  127. }
  128. [CommandHipc(6)]
  129. // 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)
  130. public ResultCode GetAddrInfoRequest(ServiceCtx context)
  131. {
  132. ulong responseBufferPosition = context.Request.ReceiveBuff[0].Position;
  133. ulong responseBufferSize = context.Request.ReceiveBuff[0].Size;
  134. return GetAddrInfoRequestImpl(context, responseBufferPosition, responseBufferSize, false, 0, 0);
  135. }
  136. [CommandHipc(8)]
  137. // GetCancelHandleRequest(u64, pid) -> u32
  138. public ResultCode GetCancelHandleRequest(ServiceCtx context)
  139. {
  140. ulong pidPlaceHolder = context.RequestData.ReadUInt64();
  141. uint cancelHandleRequest = 0;
  142. context.ResponseData.Write(cancelHandleRequest);
  143. Logger.Stub?.PrintStub(LogClass.ServiceSfdnsres, new { cancelHandleRequest });
  144. return ResultCode.Success;
  145. }
  146. [CommandHipc(9)]
  147. // CancelRequest(u32, u64, pid)
  148. public ResultCode CancelRequest(ServiceCtx context)
  149. {
  150. uint cancelHandleRequest = context.RequestData.ReadUInt32();
  151. ulong pidPlaceHolder = context.RequestData.ReadUInt64();
  152. Logger.Stub?.PrintStub(LogClass.ServiceSfdnsres, new { cancelHandleRequest });
  153. return ResultCode.Success;
  154. }
  155. [CommandHipc(10)] // 5.0.0+
  156. // GetHostByNameRequestWithOptions(u8, u32, u64, pid, buffer<unknown, 21, 0>, buffer<unknown, 21, 0>) -> (u32, u32, u32, buffer<unknown, 22, 0>)
  157. public ResultCode GetHostByNameRequestWithOptions(ServiceCtx context)
  158. {
  159. (ulong inputBufferPosition, ulong inputBufferSize) = context.Request.GetBufferType0x21();
  160. (ulong outputBufferPosition, ulong outputBufferSize) = context.Request.GetBufferType0x22();
  161. (ulong optionsBufferPosition, ulong optionsBufferSize) = context.Request.GetBufferType0x21();
  162. return GetHostByNameRequestImpl(
  163. context,
  164. inputBufferPosition,
  165. inputBufferSize,
  166. outputBufferPosition,
  167. outputBufferSize,
  168. true,
  169. optionsBufferPosition,
  170. optionsBufferSize);
  171. }
  172. [CommandHipc(11)] // 5.0.0+
  173. // GetHostByAddrRequestWithOptions(u32, u32, u32, u64, pid, buffer<unknown, 21, 0>, buffer<unknown, 21, 0>) -> (u32, u32, u32, buffer<unknown, 22, 0>)
  174. public ResultCode GetHostByAddrRequestWithOptions(ServiceCtx context)
  175. {
  176. (ulong inputBufferPosition, ulong inputBufferSize) = context.Request.GetBufferType0x21();
  177. (ulong outputBufferPosition, ulong outputBufferSize) = context.Request.GetBufferType0x22();
  178. (ulong optionsBufferPosition, ulong optionsBufferSize) = context.Request.GetBufferType0x21();
  179. return GetHostByAddrRequestImpl(
  180. context,
  181. inputBufferPosition,
  182. inputBufferSize,
  183. outputBufferPosition,
  184. outputBufferSize,
  185. true,
  186. optionsBufferPosition,
  187. optionsBufferSize);
  188. }
  189. [CommandHipc(12)] // 5.0.0+
  190. // 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)
  191. public ResultCode GetAddrInfoRequestWithOptions(ServiceCtx context)
  192. {
  193. (ulong outputBufferPosition, ulong outputBufferSize) = context.Request.GetBufferType0x22();
  194. (ulong optionsBufferPosition, ulong optionsBufferSize) = context.Request.GetBufferType0x21();
  195. return GetAddrInfoRequestImpl(context, outputBufferPosition, outputBufferSize, true, optionsBufferPosition, optionsBufferSize);
  196. }
  197. [CommandHipc(14)] // 5.0.0+
  198. // ResolverSetOptionRequest(buffer<unknown, 5, 0>, u64 unknown, u64 pid_placeholder, pid) -> (i32 ret, u32 bsd_errno)
  199. public ResultCode ResolverSetOptionRequest(ServiceCtx context)
  200. {
  201. ulong bufferPosition = context.Request.SendBuff[0].Position;
  202. ulong bufferSize = context.Request.SendBuff[0].Size;
  203. ulong unknown = context.RequestData.ReadUInt64();
  204. byte[] buffer = new byte[bufferSize];
  205. context.Memory.Read(bufferPosition, buffer);
  206. // TODO: Parse and use options.
  207. Logger.Stub?.PrintStub(LogClass.ServiceSfdnsres, new { unknown });
  208. NetDbError netDbErrorCode = NetDbError.Success;
  209. GaiError errno = GaiError.Success;
  210. context.ResponseData.Write((int)errno);
  211. context.ResponseData.Write((int)netDbErrorCode);
  212. return ResultCode.Success;
  213. }
  214. // Atmosphère extension for dns_mitm
  215. [CommandHipc(65000)]
  216. // AtmosphereReloadHostsFile()
  217. public ResultCode AtmosphereReloadHostsFile(ServiceCtx context)
  218. {
  219. DnsMitmResolver.Instance.ReloadEntries(context);
  220. return ResultCode.Success;
  221. }
  222. private static ResultCode GetHostByNameRequestImpl(
  223. ServiceCtx context,
  224. ulong inputBufferPosition,
  225. ulong inputBufferSize,
  226. ulong outputBufferPosition,
  227. ulong outputBufferSize,
  228. bool withOptions,
  229. ulong optionsBufferPosition,
  230. ulong optionsBufferSize)
  231. {
  232. string host = MemoryHelper.ReadAsciiString(context.Memory, inputBufferPosition, (int)inputBufferSize);
  233. if (!context.Device.Configuration.EnableInternetAccess)
  234. {
  235. Logger.Info?.Print(LogClass.ServiceSfdnsres, $"Guest network access disabled, DNS Blocked: {host}");
  236. WriteResponse(context, withOptions, 0, GaiError.NoData, NetDbError.HostNotFound);
  237. return ResultCode.Success;
  238. }
  239. // TODO: Use params.
  240. bool enableNsdResolve = (context.RequestData.ReadInt32() & 1) != 0;
  241. int timeOut = context.RequestData.ReadInt32();
  242. ulong pidPlaceholder = context.RequestData.ReadUInt64();
  243. if (withOptions)
  244. {
  245. // TODO: Parse and use options.
  246. }
  247. IPHostEntry hostEntry = null;
  248. NetDbError netDbErrorCode = NetDbError.Success;
  249. GaiError errno = GaiError.Overflow;
  250. int serializedSize = 0;
  251. if (host.Length <= byte.MaxValue)
  252. {
  253. if (enableNsdResolve)
  254. {
  255. if (FqdnResolver.Resolve(host, out string newAddress) == Nsd.ResultCode.Success)
  256. {
  257. host = newAddress;
  258. }
  259. }
  260. string targetHost = host;
  261. if (DnsBlacklist.IsHostBlocked(host))
  262. {
  263. Logger.Info?.Print(LogClass.ServiceSfdnsres, $"DNS Blocked: {host}");
  264. netDbErrorCode = NetDbError.HostNotFound;
  265. errno = GaiError.NoData;
  266. }
  267. else
  268. {
  269. Logger.Info?.Print(LogClass.ServiceSfdnsres, $"Trying to resolve: {host}");
  270. try
  271. {
  272. hostEntry = DnsMitmResolver.Instance.ResolveAddress(targetHost);
  273. }
  274. catch (SocketException exception)
  275. {
  276. netDbErrorCode = ConvertSocketErrorCodeToNetDbError(exception.ErrorCode);
  277. errno = ConvertSocketErrorCodeToGaiError(exception.ErrorCode, errno);
  278. }
  279. }
  280. }
  281. else
  282. {
  283. netDbErrorCode = NetDbError.HostNotFound;
  284. }
  285. if (hostEntry != null)
  286. {
  287. IEnumerable<IPAddress> addresses = GetIpv4Addresses(hostEntry);
  288. if (!addresses.Any())
  289. {
  290. errno = GaiError.NoData;
  291. netDbErrorCode = NetDbError.NoAddress;
  292. }
  293. else
  294. {
  295. errno = GaiError.Success;
  296. serializedSize = SerializeHostEntries(context, outputBufferPosition, outputBufferSize, hostEntry, addresses);
  297. }
  298. }
  299. WriteResponse(context, withOptions, serializedSize, errno, netDbErrorCode);
  300. return ResultCode.Success;
  301. }
  302. private static ResultCode GetHostByAddrRequestImpl(
  303. ServiceCtx context,
  304. ulong inputBufferPosition,
  305. ulong inputBufferSize,
  306. ulong outputBufferPosition,
  307. ulong outputBufferSize,
  308. bool withOptions,
  309. ulong optionsBufferPosition,
  310. ulong optionsBufferSize)
  311. {
  312. if (!context.Device.Configuration.EnableInternetAccess)
  313. {
  314. Logger.Info?.Print(LogClass.ServiceSfdnsres, $"Guest network access disabled, DNS Blocked.");
  315. WriteResponse(context, withOptions, 0, GaiError.NoData, NetDbError.HostNotFound);
  316. return ResultCode.Success;
  317. }
  318. byte[] rawIp = new byte[inputBufferSize];
  319. context.Memory.Read(inputBufferPosition, rawIp);
  320. // TODO: Use params.
  321. uint socketLength = context.RequestData.ReadUInt32();
  322. uint type = context.RequestData.ReadUInt32();
  323. int timeOut = context.RequestData.ReadInt32();
  324. ulong pidPlaceholder = context.RequestData.ReadUInt64();
  325. if (withOptions)
  326. {
  327. // TODO: Parse and use options.
  328. }
  329. IPHostEntry hostEntry = null;
  330. NetDbError netDbErrorCode = NetDbError.Success;
  331. GaiError errno = GaiError.AddressFamily;
  332. int serializedSize = 0;
  333. if (rawIp.Length == 4)
  334. {
  335. try
  336. {
  337. IPAddress address = new IPAddress(rawIp);
  338. hostEntry = Dns.GetHostEntry(address);
  339. }
  340. catch (SocketException exception)
  341. {
  342. netDbErrorCode = ConvertSocketErrorCodeToNetDbError(exception.ErrorCode);
  343. errno = ConvertSocketErrorCodeToGaiError(exception.ErrorCode, errno);
  344. }
  345. }
  346. else
  347. {
  348. netDbErrorCode = NetDbError.NoAddress;
  349. }
  350. if (hostEntry != null)
  351. {
  352. errno = GaiError.Success;
  353. serializedSize = SerializeHostEntries(context, outputBufferPosition, outputBufferSize, hostEntry, GetIpv4Addresses(hostEntry));
  354. }
  355. WriteResponse(context, withOptions, serializedSize, errno, netDbErrorCode);
  356. return ResultCode.Success;
  357. }
  358. private static int SerializeHostEntries(ServiceCtx context, ulong outputBufferPosition, ulong outputBufferSize, IPHostEntry hostEntry, IEnumerable<IPAddress> addresses = null)
  359. {
  360. ulong originalBufferPosition = outputBufferPosition;
  361. ulong bufferPosition = originalBufferPosition;
  362. string hostName = hostEntry.HostName + '\0';
  363. // h_name
  364. context.Memory.Write(bufferPosition, Encoding.ASCII.GetBytes(hostName));
  365. bufferPosition += (ulong)hostName.Length;
  366. // h_aliases list size
  367. context.Memory.Write(bufferPosition, BinaryPrimitives.ReverseEndianness(hostEntry.Aliases.Length));
  368. bufferPosition += sizeof(int);
  369. // Actual aliases
  370. foreach (string alias in hostEntry.Aliases)
  371. {
  372. context.Memory.Write(bufferPosition, Encoding.ASCII.GetBytes(alias + '\0'));
  373. bufferPosition += (ulong)(alias.Length + 1);
  374. }
  375. // h_addrtype but it's a short (also only support IPv4)
  376. context.Memory.Write(bufferPosition, BinaryPrimitives.ReverseEndianness((short)AddressFamily.InterNetwork));
  377. bufferPosition += sizeof(short);
  378. // h_length but it's a short
  379. context.Memory.Write(bufferPosition, BinaryPrimitives.ReverseEndianness((short)4));
  380. bufferPosition += sizeof(short);
  381. // Ip address count, we can only support ipv4 (blame Nintendo)
  382. context.Memory.Write(bufferPosition, addresses != null ? BinaryPrimitives.ReverseEndianness(addresses.Count()) : 0);
  383. bufferPosition += sizeof(int);
  384. if (addresses != null)
  385. {
  386. foreach (IPAddress ip in addresses)
  387. {
  388. context.Memory.Write(bufferPosition, BinaryPrimitives.ReverseEndianness(BitConverter.ToInt32(ip.GetAddressBytes(), 0)));
  389. bufferPosition += sizeof(int);
  390. }
  391. }
  392. return (int)(bufferPosition - originalBufferPosition);
  393. }
  394. private static ResultCode GetAddrInfoRequestImpl(
  395. ServiceCtx context,
  396. ulong responseBufferPosition,
  397. ulong responseBufferSize,
  398. bool withOptions,
  399. ulong optionsBufferPosition,
  400. ulong optionsBufferSize)
  401. {
  402. bool enableNsdResolve = (context.RequestData.ReadInt32() & 1) != 0;
  403. uint cancelHandle = context.RequestData.ReadUInt32();
  404. string host = MemoryHelper.ReadAsciiString(context.Memory, context.Request.SendBuff[0].Position, (long)context.Request.SendBuff[0].Size);
  405. string service = MemoryHelper.ReadAsciiString(context.Memory, context.Request.SendBuff[1].Position, (long)context.Request.SendBuff[1].Size);
  406. if (!context.Device.Configuration.EnableInternetAccess)
  407. {
  408. Logger.Info?.Print(LogClass.ServiceSfdnsres, $"Guest network access disabled, DNS Blocked: {host}");
  409. WriteResponse(context, withOptions, 0, GaiError.NoData, NetDbError.HostNotFound);
  410. return ResultCode.Success;
  411. }
  412. // NOTE: We ignore hints for now.
  413. List<AddrInfoSerialized> hints = DeserializeAddrInfos(context.Memory, context.Request.SendBuff[2].Position, context.Request.SendBuff[2].Size);
  414. if (withOptions)
  415. {
  416. // TODO: Find unknown, Parse and use options.
  417. uint unknown = context.RequestData.ReadUInt32();
  418. }
  419. ulong pidPlaceHolder = context.RequestData.ReadUInt64();
  420. IPHostEntry hostEntry = null;
  421. NetDbError netDbErrorCode = NetDbError.Success;
  422. GaiError errno = GaiError.AddressFamily;
  423. int serializedSize = 0;
  424. if (host.Length <= byte.MaxValue)
  425. {
  426. if (enableNsdResolve)
  427. {
  428. if (FqdnResolver.Resolve(host, out string newAddress) == Nsd.ResultCode.Success)
  429. {
  430. host = newAddress;
  431. }
  432. }
  433. string targetHost = host;
  434. if (DnsBlacklist.IsHostBlocked(host))
  435. {
  436. Logger.Info?.Print(LogClass.ServiceSfdnsres, $"DNS Blocked: {host}");
  437. netDbErrorCode = NetDbError.HostNotFound;
  438. errno = GaiError.NoData;
  439. }
  440. else
  441. {
  442. Logger.Info?.Print(LogClass.ServiceSfdnsres, $"Trying to resolve: {host}");
  443. try
  444. {
  445. hostEntry = DnsMitmResolver.Instance.ResolveAddress(targetHost);
  446. }
  447. catch (SocketException exception)
  448. {
  449. netDbErrorCode = ConvertSocketErrorCodeToNetDbError(exception.ErrorCode);
  450. errno = ConvertSocketErrorCodeToGaiError(exception.ErrorCode, errno);
  451. }
  452. }
  453. }
  454. else
  455. {
  456. netDbErrorCode = NetDbError.NoAddress;
  457. }
  458. if (hostEntry != null)
  459. {
  460. int.TryParse(service, out int port);
  461. errno = GaiError.Success;
  462. serializedSize = SerializeAddrInfos(context, responseBufferPosition, responseBufferSize, hostEntry, port);
  463. }
  464. WriteResponse(context, withOptions, serializedSize, errno, netDbErrorCode);
  465. return ResultCode.Success;
  466. }
  467. private static List<AddrInfoSerialized> DeserializeAddrInfos(IVirtualMemoryManager memory, ulong address, ulong size)
  468. {
  469. List<AddrInfoSerialized> result = new();
  470. ReadOnlySpan<byte> data = memory.GetSpan(address, (int)size);
  471. while (!data.IsEmpty)
  472. {
  473. AddrInfoSerialized info = AddrInfoSerialized.Read(data, out data);
  474. if (info == null)
  475. {
  476. break;
  477. }
  478. result.Add(info);
  479. }
  480. return result;
  481. }
  482. private static int SerializeAddrInfos(ServiceCtx context, ulong responseBufferPosition, ulong responseBufferSize, IPHostEntry hostEntry, int port)
  483. {
  484. ulong originalBufferPosition = responseBufferPosition;
  485. ulong bufferPosition = originalBufferPosition;
  486. byte[] hostName = Encoding.ASCII.GetBytes(hostEntry.HostName + '\0');
  487. using (WritableRegion region = context.Memory.GetWritableRegion(responseBufferPosition, (int)responseBufferSize))
  488. {
  489. Span<byte> data = region.Memory.Span;
  490. for (int i = 0; i < hostEntry.AddressList.Length; i++)
  491. {
  492. IPAddress ip = hostEntry.AddressList[i];
  493. if (ip.AddressFamily != AddressFamily.InterNetwork)
  494. {
  495. continue;
  496. }
  497. // NOTE: 0 = Any
  498. AddrInfoSerializedHeader header = new(ip, 0);
  499. AddrInfo4 addr = new(ip, (short)port);
  500. AddrInfoSerialized info = new(header, addr, null, hostEntry.HostName);
  501. data = info.Write(data);
  502. }
  503. uint sentinel = 0;
  504. MemoryMarshal.Write(data, ref sentinel);
  505. data = data[sizeof(uint)..];
  506. return region.Memory.Span.Length - data.Length;
  507. }
  508. }
  509. private static void WriteResponse(
  510. ServiceCtx context,
  511. bool withOptions,
  512. int serializedSize,
  513. GaiError errno,
  514. NetDbError netDbErrorCode)
  515. {
  516. if (withOptions)
  517. {
  518. context.ResponseData.Write(serializedSize);
  519. context.ResponseData.Write((int)errno);
  520. context.ResponseData.Write((int)netDbErrorCode);
  521. context.ResponseData.Write(0);
  522. }
  523. else
  524. {
  525. context.ResponseData.Write((int)netDbErrorCode);
  526. context.ResponseData.Write((int)errno);
  527. context.ResponseData.Write(serializedSize);
  528. }
  529. }
  530. private static IEnumerable<IPAddress> GetIpv4Addresses(IPHostEntry hostEntry)
  531. {
  532. return hostEntry.AddressList.Where(x => x.AddressFamily == AddressFamily.InterNetwork);
  533. }
  534. private static NetDbError ConvertSocketErrorCodeToNetDbError(int errorCode)
  535. {
  536. return errorCode switch
  537. {
  538. 11001 => NetDbError.HostNotFound,
  539. 11002 => NetDbError.TryAgain,
  540. 11003 => NetDbError.NoRecovery,
  541. 11004 => NetDbError.NoData,
  542. _ => NetDbError.Internal
  543. };
  544. }
  545. private static GaiError ConvertSocketErrorCodeToGaiError(int errorCode, GaiError errno)
  546. {
  547. return errorCode switch
  548. {
  549. 11001 => GaiError.NoData,
  550. 10060 => GaiError.Again,
  551. _ => errno
  552. };
  553. }
  554. }
  555. }