sfdnsres.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <string_view>
  4. #include <utility>
  5. #include <vector>
  6. #include "common/string_util.h"
  7. #include "common/swap.h"
  8. #include "core/core.h"
  9. #include "core/hle/ipc_helpers.h"
  10. #include "core/hle/service/sockets/sfdnsres.h"
  11. #include "core/memory.h"
  12. #ifdef _WIN32
  13. #include <ws2tcpip.h>
  14. #elif YUZU_UNIX
  15. #include <arpa/inet.h>
  16. #include <netdb.h>
  17. #include <netinet/in.h>
  18. #include <sys/socket.h>
  19. #ifndef EAI_NODATA
  20. #define EAI_NODATA EAI_NONAME
  21. #endif
  22. #endif
  23. namespace Service::Sockets {
  24. SFDNSRES::SFDNSRES(Core::System& system_) : ServiceFramework{system_, "sfdnsres"} {
  25. static const FunctionInfo functions[] = {
  26. {0, nullptr, "SetDnsAddressesPrivateRequest"},
  27. {1, nullptr, "GetDnsAddressPrivateRequest"},
  28. {2, nullptr, "GetHostByNameRequest"},
  29. {3, nullptr, "GetHostByAddrRequest"},
  30. {4, nullptr, "GetHostStringErrorRequest"},
  31. {5, nullptr, "GetGaiStringErrorRequest"},
  32. {6, &SFDNSRES::GetAddrInfoRequest, "GetAddrInfoRequest"},
  33. {7, nullptr, "GetNameInfoRequest"},
  34. {8, nullptr, "RequestCancelHandleRequest"},
  35. {9, nullptr, "CancelRequest"},
  36. {10, nullptr, "GetHostByNameRequestWithOptions"},
  37. {11, nullptr, "GetHostByAddrRequestWithOptions"},
  38. {12, &SFDNSRES::GetAddrInfoRequestWithOptions, "GetAddrInfoRequestWithOptions"},
  39. {13, nullptr, "GetNameInfoRequestWithOptions"},
  40. {14, nullptr, "ResolverSetOptionRequest"},
  41. {15, nullptr, "ResolverGetOptionRequest"},
  42. };
  43. RegisterHandlers(functions);
  44. }
  45. SFDNSRES::~SFDNSRES() = default;
  46. enum class NetDbError : s32 {
  47. Internal = -1,
  48. Success = 0,
  49. HostNotFound = 1,
  50. TryAgain = 2,
  51. NoRecovery = 3,
  52. NoData = 4,
  53. };
  54. static NetDbError AddrInfoErrorToNetDbError(s32 result) {
  55. // Best effort guess to map errors
  56. switch (result) {
  57. case 0:
  58. return NetDbError::Success;
  59. case EAI_AGAIN:
  60. return NetDbError::TryAgain;
  61. case EAI_NODATA:
  62. return NetDbError::NoData;
  63. default:
  64. return NetDbError::HostNotFound;
  65. }
  66. }
  67. static std::vector<u8> SerializeAddrInfo(const addrinfo* addrinfo, s32 result_code,
  68. std::string_view host) {
  69. // Adapted from
  70. // https://github.com/switchbrew/libnx/blob/c5a9a909a91657a9818a3b7e18c9b91ff0cbb6e3/nx/source/runtime/resolver.c#L190
  71. std::vector<u8> data;
  72. auto* current = addrinfo;
  73. while (current != nullptr) {
  74. struct SerializedResponseHeader {
  75. u32 magic;
  76. s32 flags;
  77. s32 family;
  78. s32 socket_type;
  79. s32 protocol;
  80. u32 address_length;
  81. };
  82. static_assert(sizeof(SerializedResponseHeader) == 0x18,
  83. "Response header size must be 0x18 bytes");
  84. constexpr auto header_size = sizeof(SerializedResponseHeader);
  85. const auto addr_size =
  86. current->ai_addr && current->ai_addrlen > 0 ? current->ai_addrlen : 4;
  87. const auto canonname_size = current->ai_canonname ? strlen(current->ai_canonname) + 1 : 1;
  88. const auto last_size = data.size();
  89. data.resize(last_size + header_size + addr_size + canonname_size);
  90. // Header in network byte order
  91. SerializedResponseHeader header{};
  92. constexpr auto HEADER_MAGIC = 0xBEEFCAFE;
  93. header.magic = htonl(HEADER_MAGIC);
  94. header.family = htonl(current->ai_family);
  95. header.flags = htonl(current->ai_flags);
  96. header.socket_type = htonl(current->ai_socktype);
  97. header.protocol = htonl(current->ai_protocol);
  98. header.address_length = current->ai_addr ? htonl((u32)current->ai_addrlen) : 0;
  99. auto* header_ptr = data.data() + last_size;
  100. std::memcpy(header_ptr, &header, header_size);
  101. if (header.address_length == 0) {
  102. std::memset(header_ptr + header_size, 0, 4);
  103. } else {
  104. switch (current->ai_family) {
  105. case AF_INET: {
  106. struct SockAddrIn {
  107. s16 sin_family;
  108. u16 sin_port;
  109. u32 sin_addr;
  110. u8 sin_zero[8];
  111. };
  112. SockAddrIn serialized_addr{};
  113. const auto addr = *reinterpret_cast<sockaddr_in*>(current->ai_addr);
  114. serialized_addr.sin_port = htons(addr.sin_port);
  115. serialized_addr.sin_family = htons(addr.sin_family);
  116. serialized_addr.sin_addr = htonl(addr.sin_addr.s_addr);
  117. std::memcpy(header_ptr + header_size, &serialized_addr, sizeof(SockAddrIn));
  118. char addr_string_buf[64]{};
  119. inet_ntop(AF_INET, &addr.sin_addr, addr_string_buf, std::size(addr_string_buf));
  120. LOG_INFO(Service, "Resolved host '{}' to IPv4 address {}", host, addr_string_buf);
  121. break;
  122. }
  123. case AF_INET6: {
  124. struct SockAddrIn6 {
  125. s16 sin6_family;
  126. u16 sin6_port;
  127. u32 sin6_flowinfo;
  128. u8 sin6_addr[16];
  129. u32 sin6_scope_id;
  130. };
  131. SockAddrIn6 serialized_addr{};
  132. const auto addr = *reinterpret_cast<sockaddr_in6*>(current->ai_addr);
  133. serialized_addr.sin6_family = htons(addr.sin6_family);
  134. serialized_addr.sin6_port = htons(addr.sin6_port);
  135. serialized_addr.sin6_flowinfo = htonl(addr.sin6_flowinfo);
  136. serialized_addr.sin6_scope_id = htonl(addr.sin6_scope_id);
  137. std::memcpy(serialized_addr.sin6_addr, &addr.sin6_addr,
  138. sizeof(SockAddrIn6::sin6_addr));
  139. std::memcpy(header_ptr + header_size, &serialized_addr, sizeof(SockAddrIn6));
  140. char addr_string_buf[64]{};
  141. inet_ntop(AF_INET6, &addr.sin6_addr, addr_string_buf, std::size(addr_string_buf));
  142. LOG_INFO(Service, "Resolved host '{}' to IPv6 address {}", host, addr_string_buf);
  143. break;
  144. }
  145. default:
  146. std::memcpy(header_ptr + header_size, current->ai_addr, addr_size);
  147. break;
  148. }
  149. }
  150. if (current->ai_canonname) {
  151. std::memcpy(header_ptr + addr_size, current->ai_canonname, canonname_size);
  152. } else {
  153. *(header_ptr + header_size + addr_size) = 0;
  154. }
  155. current = current->ai_next;
  156. }
  157. // 4-byte sentinel value
  158. data.push_back(0);
  159. data.push_back(0);
  160. data.push_back(0);
  161. data.push_back(0);
  162. return data;
  163. }
  164. static std::pair<u32, s32> GetAddrInfoRequestImpl(Kernel::HLERequestContext& ctx) {
  165. struct Parameters {
  166. u8 use_nsd_resolve;
  167. u32 unknown;
  168. u64 process_id;
  169. };
  170. IPC::RequestParser rp{ctx};
  171. const auto parameters = rp.PopRaw<Parameters>();
  172. LOG_WARNING(Service,
  173. "called with ignored parameters: use_nsd_resolve={}, unknown={}, process_id={}",
  174. parameters.use_nsd_resolve, parameters.unknown, parameters.process_id);
  175. const auto host_buffer = ctx.ReadBuffer(0);
  176. const std::string host = Common::StringFromBuffer(host_buffer);
  177. const auto service_buffer = ctx.ReadBuffer(1);
  178. const std::string service = Common::StringFromBuffer(service_buffer);
  179. addrinfo* addrinfo;
  180. // Pass null for hints. Serialized hints are also passed in a buffer, but are ignored for now
  181. s32 result_code = getaddrinfo(host.c_str(), service.c_str(), nullptr, &addrinfo);
  182. u32 data_size = 0;
  183. if (result_code == 0 && addrinfo != nullptr) {
  184. const std::vector<u8>& data = SerializeAddrInfo(addrinfo, result_code, host);
  185. data_size = static_cast<u32>(data.size());
  186. freeaddrinfo(addrinfo);
  187. ctx.WriteBuffer(data, 0);
  188. }
  189. return std::make_pair(data_size, result_code);
  190. }
  191. void SFDNSRES::GetAddrInfoRequest(Kernel::HLERequestContext& ctx) {
  192. auto [data_size, result_code] = GetAddrInfoRequestImpl(ctx);
  193. IPC::ResponseBuilder rb{ctx, 4};
  194. rb.Push(ResultSuccess);
  195. rb.Push(static_cast<s32>(AddrInfoErrorToNetDbError(result_code))); // NetDBErrorCode
  196. rb.Push(result_code); // errno
  197. rb.Push(data_size); // serialized size
  198. }
  199. void SFDNSRES::GetAddrInfoRequestWithOptions(Kernel::HLERequestContext& ctx) {
  200. // Additional options are ignored
  201. auto [data_size, result_code] = GetAddrInfoRequestImpl(ctx);
  202. IPC::ResponseBuilder rb{ctx, 5};
  203. rb.Push(ResultSuccess);
  204. rb.Push(data_size); // serialized size
  205. rb.Push(result_code); // errno
  206. rb.Push(static_cast<s32>(AddrInfoErrorToNetDbError(result_code))); // NetDBErrorCode
  207. rb.Push(0);
  208. }
  209. } // namespace Service::Sockets