IResolver.cs 27 KB

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