IResolver.cs 27 KB

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