IResolver.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. string name = MemoryHelper.ReadAsciiString(context.Memory, inputBufferPosition, (int)inputBufferSize);
  180. // TODO: Use params.
  181. bool enableNsdResolve = (context.RequestData.ReadInt32() & 1) != 0;
  182. int timeOut = context.RequestData.ReadInt32();
  183. ulong pidPlaceholder = context.RequestData.ReadUInt64();
  184. if (optionsBufferSize > 0)
  185. {
  186. // TODO: Parse and use options.
  187. }
  188. IPHostEntry hostEntry = null;
  189. NetDbError netDbErrorCode = NetDbError.Success;
  190. GaiError errno = GaiError.Overflow;
  191. ulong serializedSize = 0;
  192. if (name.Length <= byte.MaxValue)
  193. {
  194. string targetHost = name;
  195. if (DnsBlacklist.IsHostBlocked(name))
  196. {
  197. Logger.Info?.Print(LogClass.ServiceSfdnsres, $"DNS Blocked: {name}");
  198. netDbErrorCode = NetDbError.HostNotFound;
  199. errno = GaiError.NoData;
  200. }
  201. else
  202. {
  203. Logger.Info?.Print(LogClass.ServiceSfdnsres, $"Trying to resolve: {name}");
  204. try
  205. {
  206. hostEntry = Dns.GetHostEntry(targetHost);
  207. }
  208. catch (SocketException exception)
  209. {
  210. netDbErrorCode = ConvertSocketErrorCodeToNetDbError(exception.ErrorCode);
  211. errno = ConvertSocketErrorCodeToGaiError(exception.ErrorCode, errno);
  212. }
  213. }
  214. }
  215. else
  216. {
  217. netDbErrorCode = NetDbError.HostNotFound;
  218. }
  219. if (hostEntry != null)
  220. {
  221. IEnumerable<IPAddress> addresses = GetIpv4Addresses(hostEntry);
  222. if (!addresses.Any())
  223. {
  224. errno = GaiError.NoData;
  225. netDbErrorCode = NetDbError.NoAddress;
  226. }
  227. else
  228. {
  229. errno = GaiError.Success;
  230. serializedSize = SerializeHostEntries(context, outputBufferPosition, outputBufferSize, hostEntry, addresses);
  231. }
  232. }
  233. context.ResponseData.Write((int)netDbErrorCode);
  234. context.ResponseData.Write((int)errno);
  235. context.ResponseData.Write(serializedSize);
  236. return ResultCode.Success;
  237. }
  238. private ResultCode GetHostByAddrRequestImpl(ServiceCtx context, ulong inputBufferPosition, ulong inputBufferSize, ulong outputBufferPosition, ulong outputBufferSize, ulong optionsBufferPosition, ulong optionsBufferSize)
  239. {
  240. byte[] rawIp = new byte[inputBufferSize];
  241. context.Memory.Read(inputBufferPosition, rawIp);
  242. // TODO: Use params.
  243. uint socketLength = context.RequestData.ReadUInt32();
  244. uint type = context.RequestData.ReadUInt32();
  245. int timeOut = context.RequestData.ReadInt32();
  246. ulong pidPlaceholder = context.RequestData.ReadUInt64();
  247. if (optionsBufferSize > 0)
  248. {
  249. // TODO: Parse and use options.
  250. }
  251. IPHostEntry hostEntry = null;
  252. NetDbError netDbErrorCode = NetDbError.Success;
  253. GaiError errno = GaiError.AddressFamily;
  254. ulong serializedSize = 0;
  255. if (rawIp.Length == 4)
  256. {
  257. try
  258. {
  259. IPAddress address = new IPAddress(rawIp);
  260. hostEntry = Dns.GetHostEntry(address);
  261. }
  262. catch (SocketException exception)
  263. {
  264. netDbErrorCode = ConvertSocketErrorCodeToNetDbError(exception.ErrorCode);
  265. errno = ConvertSocketErrorCodeToGaiError(exception.ErrorCode, errno);
  266. }
  267. }
  268. else
  269. {
  270. netDbErrorCode = NetDbError.NoAddress;
  271. }
  272. if (hostEntry != null)
  273. {
  274. errno = GaiError.Success;
  275. serializedSize = SerializeHostEntries(context, outputBufferPosition, outputBufferSize, hostEntry, GetIpv4Addresses(hostEntry));
  276. }
  277. context.ResponseData.Write((int)netDbErrorCode);
  278. context.ResponseData.Write((int)errno);
  279. context.ResponseData.Write(serializedSize);
  280. return ResultCode.Success;
  281. }
  282. private ulong SerializeHostEntries(ServiceCtx context, ulong outputBufferPosition, ulong outputBufferSize, IPHostEntry hostEntry, IEnumerable<IPAddress> addresses = null)
  283. {
  284. ulong originalBufferPosition = outputBufferPosition;
  285. ulong bufferPosition = originalBufferPosition;
  286. string hostName = hostEntry.HostName + '\0';
  287. // h_name
  288. context.Memory.Write(bufferPosition, Encoding.ASCII.GetBytes(hostName));
  289. bufferPosition += (ulong)hostName.Length;
  290. // h_aliases list size
  291. context.Memory.Write(bufferPosition, BinaryPrimitives.ReverseEndianness(hostEntry.Aliases.Length));
  292. bufferPosition += sizeof(int);
  293. // Actual aliases
  294. foreach (string alias in hostEntry.Aliases)
  295. {
  296. context.Memory.Write(bufferPosition, Encoding.ASCII.GetBytes(alias + '\0'));
  297. bufferPosition += (ulong)(alias.Length + 1);
  298. }
  299. // h_addrtype but it's a short (also only support IPv4)
  300. context.Memory.Write(bufferPosition, BinaryPrimitives.ReverseEndianness((short)AddressFamily.InterNetwork));
  301. bufferPosition += sizeof(short);
  302. // h_length but it's a short
  303. context.Memory.Write(bufferPosition, BinaryPrimitives.ReverseEndianness((short)4));
  304. bufferPosition += sizeof(short);
  305. // Ip address count, we can only support ipv4 (blame Nintendo)
  306. context.Memory.Write(bufferPosition, addresses != null ? BinaryPrimitives.ReverseEndianness(addresses.Count()) : 0);
  307. bufferPosition += sizeof(int);
  308. if (addresses != null)
  309. {
  310. foreach (IPAddress ip in addresses)
  311. {
  312. context.Memory.Write(bufferPosition, BinaryPrimitives.ReverseEndianness(BitConverter.ToInt32(ip.GetAddressBytes(), 0)));
  313. bufferPosition += sizeof(int);
  314. }
  315. }
  316. return bufferPosition - originalBufferPosition;
  317. }
  318. private ResultCode GetAddrInfoRequestImpl(ServiceCtx context, ulong responseBufferPosition, ulong responseBufferSize, ulong optionsBufferPosition, ulong optionsBufferSize)
  319. {
  320. bool enableNsdResolve = (context.RequestData.ReadInt32() & 1) != 0;
  321. uint cancelHandle = context.RequestData.ReadUInt32();
  322. string host = MemoryHelper.ReadAsciiString(context.Memory, context.Request.SendBuff[0].Position, (long)context.Request.SendBuff[0].Size);
  323. string service = MemoryHelper.ReadAsciiString(context.Memory, context.Request.SendBuff[1].Position, (long)context.Request.SendBuff[1].Size);
  324. // NOTE: We ignore hints for now.
  325. DeserializeAddrInfos(context.Memory, (ulong)context.Request.SendBuff[2].Position, (ulong)context.Request.SendBuff[2].Size);
  326. if (optionsBufferSize > 0)
  327. {
  328. // TODO: Find unknown, Parse and use options.
  329. uint unknown = context.RequestData.ReadUInt32();
  330. }
  331. ulong pidPlaceHolder = context.RequestData.ReadUInt64();
  332. Logger.Stub?.PrintStub(LogClass.ServiceSfdnsres, new { enableNsdResolve, cancelHandle, pidPlaceHolder, host, service });
  333. IPHostEntry hostEntry = null;
  334. NetDbError netDbErrorCode = NetDbError.Success;
  335. GaiError errno = GaiError.AddressFamily;
  336. ulong serializedSize = 0;
  337. if (host.Length <= byte.MaxValue)
  338. {
  339. string targetHost = host;
  340. if (DnsBlacklist.IsHostBlocked(host))
  341. {
  342. Logger.Info?.Print(LogClass.ServiceSfdnsres, $"DNS Blocked: {host}");
  343. netDbErrorCode = NetDbError.HostNotFound;
  344. errno = GaiError.NoData;
  345. }
  346. else
  347. {
  348. Logger.Info?.Print(LogClass.ServiceSfdnsres, $"Trying to resolve: {host}");
  349. try
  350. {
  351. hostEntry = Dns.GetHostEntry(targetHost);
  352. }
  353. catch (SocketException exception)
  354. {
  355. netDbErrorCode = ConvertSocketErrorCodeToNetDbError(exception.ErrorCode);
  356. errno = ConvertSocketErrorCodeToGaiError(exception.ErrorCode, errno);
  357. }
  358. }
  359. }
  360. else
  361. {
  362. netDbErrorCode = NetDbError.NoAddress;
  363. }
  364. if (hostEntry != null)
  365. {
  366. int.TryParse(service, out int port);
  367. errno = GaiError.Success;
  368. serializedSize = SerializeAddrInfos(context, responseBufferPosition, responseBufferSize, hostEntry, port);
  369. }
  370. context.ResponseData.Write((int)netDbErrorCode);
  371. context.ResponseData.Write((int)errno);
  372. context.ResponseData.Write(serializedSize);
  373. return ResultCode.Success;
  374. }
  375. private void DeserializeAddrInfos(IVirtualMemoryManager memory, ulong address, ulong size)
  376. {
  377. ulong endAddress = address + size;
  378. while (address < endAddress)
  379. {
  380. AddrInfoSerializedHeader header = memory.Read<AddrInfoSerializedHeader>(address);
  381. if (header.Magic != SfdnsresContants.AddrInfoMagic)
  382. {
  383. break;
  384. }
  385. address += (ulong)Unsafe.SizeOf<AddrInfoSerializedHeader>() + header.AddressLength;
  386. // ai_canonname
  387. string canonname = string.Empty;
  388. while (true)
  389. {
  390. byte chr = memory.Read<byte>(address++);
  391. if (chr == 0)
  392. {
  393. break;
  394. }
  395. canonname += (char)chr;
  396. }
  397. }
  398. }
  399. private ulong SerializeAddrInfos(ServiceCtx context, ulong responseBufferPosition, ulong responseBufferSize, IPHostEntry hostEntry, int port)
  400. {
  401. ulong originalBufferPosition = (ulong)responseBufferPosition;
  402. ulong bufferPosition = originalBufferPosition;
  403. string hostName = hostEntry.HostName + '\0';
  404. for (int i = 0; i < hostEntry.AddressList.Length; i++)
  405. {
  406. IPAddress ip = hostEntry.AddressList[i];
  407. if (ip.AddressFamily != AddressFamily.InterNetwork)
  408. {
  409. continue;
  410. }
  411. AddrInfoSerializedHeader header = new AddrInfoSerializedHeader(ip, 0);
  412. // NOTE: 0 = Any
  413. context.Memory.Write(bufferPosition, header);
  414. bufferPosition += (ulong)Unsafe.SizeOf<AddrInfoSerializedHeader>();
  415. // addrinfo_in
  416. context.Memory.Write(bufferPosition, new AddrInfo4(ip, (short)port));
  417. bufferPosition += header.AddressLength;
  418. // ai_canonname
  419. context.Memory.Write(bufferPosition, Encoding.ASCII.GetBytes(hostName));
  420. bufferPosition += (ulong)hostName.Length;
  421. }
  422. // Termination zero value.
  423. context.Memory.Write(bufferPosition, 0);
  424. bufferPosition += sizeof(int);
  425. return bufferPosition - originalBufferPosition;
  426. }
  427. private IEnumerable<IPAddress> GetIpv4Addresses(IPHostEntry hostEntry)
  428. {
  429. return hostEntry.AddressList.Where(x => x.AddressFamily == AddressFamily.InterNetwork);
  430. }
  431. private NetDbError ConvertSocketErrorCodeToNetDbError(int errorCode)
  432. {
  433. return errorCode switch
  434. {
  435. 11001 => NetDbError.HostNotFound,
  436. 11002 => NetDbError.TryAgain,
  437. 11003 => NetDbError.NoRecovery,
  438. 11004 => NetDbError.NoData,
  439. _ => NetDbError.Internal
  440. };
  441. }
  442. private GaiError ConvertSocketErrorCodeToGaiError(int errorCode, GaiError errno)
  443. {
  444. return errorCode switch
  445. {
  446. 11001 => GaiError.NoData,
  447. 10060 => GaiError.Again,
  448. _ => errno
  449. };
  450. }
  451. }
  452. }