IResolver.cs 23 KB

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