IResolver.cs 24 KB

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