diff options
| author | 2022-04-07 23:07:12 +0200 | |
|---|---|---|
| committer | 2022-04-08 21:28:03 +0200 | |
| commit | 82d46a974ad46956169f161257ebc8f67dc80f26 (patch) | |
| tree | 285ba988e505e1d017424ca0df9615b2040c432b /src/core/hle/service/sockets | |
| parent | service: bsd: Add keepalive socket option (diff) | |
| download | yuzu-82d46a974ad46956169f161257ebc8f67dc80f26.tar.gz yuzu-82d46a974ad46956169f161257ebc8f67dc80f26.tar.xz yuzu-82d46a974ad46956169f161257ebc8f67dc80f26.zip | |
service: sfdnsres: Implement DNS address resolution
Diffstat (limited to 'src/core/hle/service/sockets')
| -rw-r--r-- | src/core/hle/service/sockets/sfdnsres.cpp | 201 | ||||
| -rw-r--r-- | src/core/hle/service/sockets/sfdnsres.h | 1 |
2 files changed, 197 insertions, 5 deletions
diff --git a/src/core/hle/service/sockets/sfdnsres.cpp b/src/core/hle/service/sockets/sfdnsres.cpp index fb6142c49..2e5666ca8 100644 --- a/src/core/hle/service/sockets/sfdnsres.cpp +++ b/src/core/hle/service/sockets/sfdnsres.cpp | |||
| @@ -2,8 +2,24 @@ | |||
| 2 | // Licensed under GPLv2 or any later version | 2 | // Licensed under GPLv2 or any later version |
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <string_view> | ||
| 6 | #include <utility> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "common/string_util.h" | ||
| 10 | #include "common/swap.h" | ||
| 11 | #include "core/core.h" | ||
| 5 | #include "core/hle/ipc_helpers.h" | 12 | #include "core/hle/ipc_helpers.h" |
| 6 | #include "core/hle/service/sockets/sfdnsres.h" | 13 | #include "core/hle/service/sockets/sfdnsres.h" |
| 14 | #include "core/memory.h" | ||
| 15 | |||
| 16 | #ifdef _WIN32 | ||
| 17 | #include <ws2tcpip.h> | ||
| 18 | #elif YUZU_UNIX | ||
| 19 | #include <arpa/inet.h> | ||
| 20 | #include <netdb.h> | ||
| 21 | #include <sys/socket.h> | ||
| 22 | #endif | ||
| 7 | 23 | ||
| 8 | namespace Service::Sockets { | 24 | namespace Service::Sockets { |
| 9 | 25 | ||
| @@ -21,7 +37,7 @@ SFDNSRES::SFDNSRES(Core::System& system_) : ServiceFramework{system_, "sfdnsres" | |||
| 21 | {9, nullptr, "CancelRequest"}, | 37 | {9, nullptr, "CancelRequest"}, |
| 22 | {10, nullptr, "GetHostByNameRequestWithOptions"}, | 38 | {10, nullptr, "GetHostByNameRequestWithOptions"}, |
| 23 | {11, nullptr, "GetHostByAddrRequestWithOptions"}, | 39 | {11, nullptr, "GetHostByAddrRequestWithOptions"}, |
| 24 | {12, nullptr, "GetAddrInfoRequestWithOptions"}, | 40 | {12, &SFDNSRES::GetAddrInfoRequestWithOptions, "GetAddrInfoRequestWithOptions"}, |
| 25 | {13, nullptr, "GetNameInfoRequestWithOptions"}, | 41 | {13, nullptr, "GetNameInfoRequestWithOptions"}, |
| 26 | {14, nullptr, "ResolverSetOptionRequest"}, | 42 | {14, nullptr, "ResolverSetOptionRequest"}, |
| 27 | {15, nullptr, "ResolverGetOptionRequest"}, | 43 | {15, nullptr, "ResolverGetOptionRequest"}, |
| @@ -31,7 +47,142 @@ SFDNSRES::SFDNSRES(Core::System& system_) : ServiceFramework{system_, "sfdnsres" | |||
| 31 | 47 | ||
| 32 | SFDNSRES::~SFDNSRES() = default; | 48 | SFDNSRES::~SFDNSRES() = default; |
| 33 | 49 | ||
| 34 | void SFDNSRES::GetAddrInfoRequest(Kernel::HLERequestContext& ctx) { | 50 | enum class NetDbError : s32 { |
| 51 | Internal = -1, | ||
| 52 | Success = 0, | ||
| 53 | HostNotFound = 1, | ||
| 54 | TryAgain = 2, | ||
| 55 | NoRecovery = 3, | ||
| 56 | NoData = 4, | ||
| 57 | }; | ||
| 58 | |||
| 59 | static NetDbError AddrInfoErrorToNetDbError(s32 result) { | ||
| 60 | // Best effort guess to map errors | ||
| 61 | switch (result) { | ||
| 62 | case 0: | ||
| 63 | return NetDbError::Success; | ||
| 64 | case EAI_AGAIN: | ||
| 65 | return NetDbError::TryAgain; | ||
| 66 | case EAI_NODATA: | ||
| 67 | return NetDbError::NoData; | ||
| 68 | default: | ||
| 69 | return NetDbError::HostNotFound; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | static std::vector<u8> SerializeAddrInfo(const addrinfo* addrinfo, s32 result_code, | ||
| 74 | std::string_view host) { | ||
| 75 | // Adapted from | ||
| 76 | // https://github.com/switchbrew/libnx/blob/c5a9a909a91657a9818a3b7e18c9b91ff0cbb6e3/nx/source/runtime/resolver.c#L190 | ||
| 77 | std::vector<u8> data; | ||
| 78 | |||
| 79 | auto* current = addrinfo; | ||
| 80 | while (current != nullptr) { | ||
| 81 | struct SerializedResponseHeader { | ||
| 82 | u32 magic; | ||
| 83 | s32 flags; | ||
| 84 | s32 family; | ||
| 85 | s32 socket_type; | ||
| 86 | s32 protocol; | ||
| 87 | u32 address_length; | ||
| 88 | }; | ||
| 89 | static_assert(sizeof(SerializedResponseHeader) == 0x18, | ||
| 90 | "Response header size must be 0x18 bytes"); | ||
| 91 | |||
| 92 | constexpr auto header_size = sizeof(SerializedResponseHeader); | ||
| 93 | const auto addr_size = | ||
| 94 | current->ai_addr && current->ai_addrlen > 0 ? current->ai_addrlen : 4; | ||
| 95 | const auto canonname_size = current->ai_canonname ? strlen(current->ai_canonname) + 1 : 1; | ||
| 96 | |||
| 97 | const auto last_size = data.size(); | ||
| 98 | data.resize(last_size + header_size + addr_size + canonname_size); | ||
| 99 | |||
| 100 | // Header in network byte order | ||
| 101 | SerializedResponseHeader header{}; | ||
| 102 | |||
| 103 | constexpr auto HEADER_MAGIC = 0xBEEFCAFE; | ||
| 104 | header.magic = htonl(HEADER_MAGIC); | ||
| 105 | header.family = htonl(current->ai_family); | ||
| 106 | header.flags = htonl(current->ai_flags); | ||
| 107 | header.socket_type = htonl(current->ai_socktype); | ||
| 108 | header.protocol = htonl(current->ai_protocol); | ||
| 109 | header.address_length = current->ai_addr ? htonl((u32)current->ai_addrlen) : 0; | ||
| 110 | |||
| 111 | auto* header_ptr = data.data() + last_size; | ||
| 112 | std::memcpy(header_ptr, &header, header_size); | ||
| 113 | |||
| 114 | if (header.address_length == 0) { | ||
| 115 | std::memset(header_ptr + header_size, 0, 4); | ||
| 116 | } else { | ||
| 117 | switch (current->ai_family) { | ||
| 118 | case AF_INET: { | ||
| 119 | struct SockAddrIn { | ||
| 120 | s16 sin_family; | ||
| 121 | u16 sin_port; | ||
| 122 | u32 sin_addr; | ||
| 123 | u8 sin_zero[8]; | ||
| 124 | }; | ||
| 125 | |||
| 126 | SockAddrIn serialized_addr{}; | ||
| 127 | const auto addr = *reinterpret_cast<sockaddr_in*>(current->ai_addr); | ||
| 128 | serialized_addr.sin_port = htons(addr.sin_port); | ||
| 129 | serialized_addr.sin_family = htons(addr.sin_family); | ||
| 130 | serialized_addr.sin_addr = htonl(addr.sin_addr.s_addr); | ||
| 131 | std::memcpy(header_ptr + header_size, &serialized_addr, sizeof(SockAddrIn)); | ||
| 132 | |||
| 133 | char addr_string_buf[64]{}; | ||
| 134 | inet_ntop(AF_INET, &addr.sin_addr, addr_string_buf, std::size(addr_string_buf)); | ||
| 135 | LOG_INFO(Service, "Resolved host '{}' to IPv4 address {}", host, addr_string_buf); | ||
| 136 | break; | ||
| 137 | } | ||
| 138 | case AF_INET6: { | ||
| 139 | struct SockAddrIn6 { | ||
| 140 | s16 sin6_family; | ||
| 141 | u16 sin6_port; | ||
| 142 | u32 sin6_flowinfo; | ||
| 143 | u8 sin6_addr[16]; | ||
| 144 | u32 sin6_scope_id; | ||
| 145 | }; | ||
| 146 | |||
| 147 | SockAddrIn6 serialized_addr{}; | ||
| 148 | const auto addr = *reinterpret_cast<sockaddr_in6*>(current->ai_addr); | ||
| 149 | serialized_addr.sin6_family = htons(addr.sin6_family); | ||
| 150 | serialized_addr.sin6_port = htons(addr.sin6_port); | ||
| 151 | serialized_addr.sin6_flowinfo = htonl(addr.sin6_flowinfo); | ||
| 152 | serialized_addr.sin6_scope_id = htonl(addr.sin6_scope_id); | ||
| 153 | std::memcpy(serialized_addr.sin6_addr, &addr.sin6_addr, | ||
| 154 | sizeof(SockAddrIn6::sin6_addr)); | ||
| 155 | std::memcpy(header_ptr + header_size, &serialized_addr, sizeof(SockAddrIn6)); | ||
| 156 | |||
| 157 | char addr_string_buf[64]{}; | ||
| 158 | inet_ntop(AF_INET6, &addr.sin6_addr, addr_string_buf, std::size(addr_string_buf)); | ||
| 159 | LOG_INFO(Service, "Resolved host '{}' to IPv6 address {}", host, addr_string_buf); | ||
| 160 | break; | ||
| 161 | } | ||
| 162 | default: | ||
| 163 | std::memcpy(header_ptr + header_size, current->ai_addr, addr_size); | ||
| 164 | break; | ||
| 165 | } | ||
| 166 | } | ||
| 167 | if (current->ai_canonname) { | ||
| 168 | std::memcpy(header_ptr + addr_size, current->ai_canonname, canonname_size); | ||
| 169 | } else { | ||
| 170 | *(header_ptr + header_size + addr_size) = 0; | ||
| 171 | } | ||
| 172 | |||
| 173 | current = current->ai_next; | ||
| 174 | } | ||
| 175 | |||
| 176 | // 4-byte sentinel value | ||
| 177 | data.push_back(0); | ||
| 178 | data.push_back(0); | ||
| 179 | data.push_back(0); | ||
| 180 | data.push_back(0); | ||
| 181 | |||
| 182 | return data; | ||
| 183 | } | ||
| 184 | |||
| 185 | static std::pair<u32, s32> GetAddrInfoRequestImpl(Kernel::HLERequestContext& ctx) { | ||
| 35 | struct Parameters { | 186 | struct Parameters { |
| 36 | u8 use_nsd_resolve; | 187 | u8 use_nsd_resolve; |
| 37 | u32 unknown; | 188 | u32 unknown; |
| @@ -42,11 +193,51 @@ void SFDNSRES::GetAddrInfoRequest(Kernel::HLERequestContext& ctx) { | |||
| 42 | const auto parameters = rp.PopRaw<Parameters>(); | 193 | const auto parameters = rp.PopRaw<Parameters>(); |
| 43 | 194 | ||
| 44 | LOG_WARNING(Service, | 195 | LOG_WARNING(Service, |
| 45 | "(STUBBED) called. use_nsd_resolve={}, unknown=0x{:08X}, process_id=0x{:016X}", | 196 | "called with ignored parameters: use_nsd_resolve={}, unknown={}, process_id={}", |
| 46 | parameters.use_nsd_resolve, parameters.unknown, parameters.process_id); | 197 | parameters.use_nsd_resolve, parameters.unknown, parameters.process_id); |
| 47 | 198 | ||
| 48 | IPC::ResponseBuilder rb{ctx, 2}; | 199 | const auto host_buffer = ctx.ReadBuffer(0); |
| 200 | const std::string host = Common::StringFromBuffer(host_buffer); | ||
| 201 | |||
| 202 | const auto service_buffer = ctx.ReadBuffer(1); | ||
| 203 | const std::string service = Common::StringFromBuffer(service_buffer); | ||
| 204 | |||
| 205 | addrinfo* addrinfo; | ||
| 206 | // Pass null for hints. Serialized hints are also passed in a buffer, but are ignored for now | ||
| 207 | s32 result_code = getaddrinfo(host.c_str(), service.c_str(), nullptr, &addrinfo); | ||
| 208 | |||
| 209 | u32 data_size = 0; | ||
| 210 | if (result_code == 0 && addrinfo != nullptr) { | ||
| 211 | const std::vector<u8>& data = SerializeAddrInfo(addrinfo, result_code, host); | ||
| 212 | data_size = static_cast<u32>(data.size()); | ||
| 213 | freeaddrinfo(addrinfo); | ||
| 214 | |||
| 215 | ctx.WriteBuffer(data, 0); | ||
| 216 | } | ||
| 217 | |||
| 218 | return std::make_pair(data_size, result_code); | ||
| 219 | } | ||
| 220 | |||
| 221 | void SFDNSRES::GetAddrInfoRequest(Kernel::HLERequestContext& ctx) { | ||
| 222 | auto [data_size, result_code] = GetAddrInfoRequestImpl(ctx); | ||
| 223 | |||
| 224 | IPC::ResponseBuilder rb{ctx, 4}; | ||
| 225 | rb.Push(ResultSuccess); | ||
| 226 | rb.Push(static_cast<s32>(AddrInfoErrorToNetDbError(result_code))); // NetDBErrorCode | ||
| 227 | rb.Push(result_code); // errno | ||
| 228 | rb.Push(data_size); // serialized size | ||
| 229 | } | ||
| 230 | |||
| 231 | void SFDNSRES::GetAddrInfoRequestWithOptions(Kernel::HLERequestContext& ctx) { | ||
| 232 | // Additional options are ignored | ||
| 233 | auto [data_size, result_code] = GetAddrInfoRequestImpl(ctx); | ||
| 234 | |||
| 235 | IPC::ResponseBuilder rb{ctx, 5}; | ||
| 49 | rb.Push(ResultSuccess); | 236 | rb.Push(ResultSuccess); |
| 237 | rb.Push(data_size); // serialized size | ||
| 238 | rb.Push(result_code); // errno | ||
| 239 | rb.Push(static_cast<s32>(AddrInfoErrorToNetDbError(result_code))); // NetDBErrorCode | ||
| 240 | rb.Push(0); | ||
| 50 | } | 241 | } |
| 51 | 242 | ||
| 52 | } // namespace Service::Sockets | 243 | } // namespace Service::Sockets \ No newline at end of file |
diff --git a/src/core/hle/service/sockets/sfdnsres.h b/src/core/hle/service/sockets/sfdnsres.h index 5d3b4dc2d..f0c57377d 100644 --- a/src/core/hle/service/sockets/sfdnsres.h +++ b/src/core/hle/service/sockets/sfdnsres.h | |||
| @@ -19,6 +19,7 @@ public: | |||
| 19 | 19 | ||
| 20 | private: | 20 | private: |
| 21 | void GetAddrInfoRequest(Kernel::HLERequestContext& ctx); | 21 | void GetAddrInfoRequest(Kernel::HLERequestContext& ctx); |
| 22 | void GetAddrInfoRequestWithOptions(Kernel::HLERequestContext& ctx); | ||
| 22 | }; | 23 | }; |
| 23 | 24 | ||
| 24 | } // namespace Service::Sockets | 25 | } // namespace Service::Sockets |