2022-04-23 08:59:50 +00:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-01-30 06:29:47 +00:00
|
|
|
|
2022-04-07 21:07:12 +00:00
|
|
|
#include <string_view>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "common/string_util.h"
|
|
|
|
#include "common/swap.h"
|
|
|
|
#include "core/core.h"
|
2023-02-19 19:42:12 +00:00
|
|
|
#include "core/hle/service/ipc_helpers.h"
|
2018-01-30 06:29:47 +00:00
|
|
|
#include "core/hle/service/sockets/sfdnsres.h"
|
2022-04-07 21:07:12 +00:00
|
|
|
#include "core/memory.h"
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#elif YUZU_UNIX
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netdb.h>
|
2022-04-11 22:26:32 +00:00
|
|
|
#include <netinet/in.h>
|
2022-04-07 21:07:12 +00:00
|
|
|
#include <sys/socket.h>
|
2022-04-11 22:26:32 +00:00
|
|
|
#ifndef EAI_NODATA
|
|
|
|
#define EAI_NODATA EAI_NONAME
|
|
|
|
#endif
|
2022-04-07 21:07:12 +00:00
|
|
|
#endif
|
2018-01-30 06:29:47 +00:00
|
|
|
|
2018-04-20 01:41:44 +00:00
|
|
|
namespace Service::Sockets {
|
2018-01-30 06:29:47 +00:00
|
|
|
|
2020-11-26 20:19:08 +00:00
|
|
|
SFDNSRES::SFDNSRES(Core::System& system_) : ServiceFramework{system_, "sfdnsres"} {
|
2018-03-25 09:41:00 +00:00
|
|
|
static const FunctionInfo functions[] = {
|
2021-04-08 20:19:46 +00:00
|
|
|
{0, nullptr, "SetDnsAddressesPrivateRequest"},
|
|
|
|
{1, nullptr, "GetDnsAddressPrivateRequest"},
|
2020-06-29 02:01:34 +00:00
|
|
|
{2, nullptr, "GetHostByNameRequest"},
|
|
|
|
{3, nullptr, "GetHostByAddrRequest"},
|
|
|
|
{4, nullptr, "GetHostStringErrorRequest"},
|
|
|
|
{5, nullptr, "GetGaiStringErrorRequest"},
|
|
|
|
{6, &SFDNSRES::GetAddrInfoRequest, "GetAddrInfoRequest"},
|
|
|
|
{7, nullptr, "GetNameInfoRequest"},
|
|
|
|
{8, nullptr, "RequestCancelHandleRequest"},
|
|
|
|
{9, nullptr, "CancelRequest"},
|
|
|
|
{10, nullptr, "GetHostByNameRequestWithOptions"},
|
|
|
|
{11, nullptr, "GetHostByAddrRequestWithOptions"},
|
2022-04-07 21:07:12 +00:00
|
|
|
{12, &SFDNSRES::GetAddrInfoRequestWithOptions, "GetAddrInfoRequestWithOptions"},
|
2020-06-29 02:01:34 +00:00
|
|
|
{13, nullptr, "GetNameInfoRequestWithOptions"},
|
|
|
|
{14, nullptr, "ResolverSetOptionRequest"},
|
|
|
|
{15, nullptr, "ResolverGetOptionRequest"},
|
2018-03-25 09:41:00 +00:00
|
|
|
};
|
2018-01-30 06:29:47 +00:00
|
|
|
RegisterHandlers(functions);
|
|
|
|
}
|
|
|
|
|
hle/service: Default constructors and destructors in the cpp file where applicable
When a destructor isn't defaulted into a cpp file, it can cause the use
of forward declarations to seemingly fail to compile for non-obvious
reasons. It also allows inlining of the construction/destruction logic
all over the place where a constructor or destructor is invoked, which
can lead to code bloat. This isn't so much a worry here, given the
services won't be created and destroyed frequently.
The cause of the above mentioned non-obvious errors can be demonstrated
as follows:
------- Demonstrative example, if you know how the described error happens, skip forwards -------
Assume we have the following in the header, which we'll call "thing.h":
\#include <memory>
// Forward declaration. For example purposes, assume the definition
// of Object is in some header named "object.h"
class Object;
class Thing {
public:
// assume no constructors or destructors are specified here,
// or the constructors/destructors are defined as:
//
// Thing() = default;
// ~Thing() = default;
//
// ... Some interface member functions would be defined here
private:
std::shared_ptr<Object> obj;
};
If this header is included in a cpp file, (which we'll call "main.cpp"),
this will result in a compilation error, because even though no
destructor is specified, the destructor will still need to be generated by
the compiler because std::shared_ptr's destructor is *not* trivial (in
other words, it does something other than nothing), as std::shared_ptr's
destructor needs to do two things:
1. Decrement the shared reference count of the object being pointed to,
and if the reference count decrements to zero,
2. Free the Object instance's memory (aka deallocate the memory it's
pointing to).
And so the compiler generates the code for the destructor doing this inside main.cpp.
Now, keep in mind, the Object forward declaration is not a complete type. All it
does is tell the compiler "a type named Object exists" and allows us to
use the name in certain situations to avoid a header dependency. So the
compiler needs to generate destruction code for Object, but the compiler
doesn't know *how* to destruct it. A forward declaration doesn't tell
the compiler anything about Object's constructor or destructor. So, the
compiler will issue an error in this case because it's undefined
behavior to try and deallocate (or construct) an incomplete type and
std::shared_ptr and std::unique_ptr make sure this isn't the case
internally.
Now, if we had defaulted the destructor in "thing.cpp", where we also
include "object.h", this would never be an issue, as the destructor
would only have its code generated in one place, and it would be in a
place where the full class definition of Object would be visible to the
compiler.
---------------------- End example ----------------------------
Given these service classes are more than certainly going to change in
the future, this defaults the constructors and destructors into the
relevant cpp files to make the construction and destruction of all of
the services consistent and unlikely to run into cases where forward
declarations are indirectly causing compilation errors. It also has the
plus of avoiding the need to rebuild several services if destruction
logic changes, since it would only be necessary to recompile the single
cpp file.
2018-09-11 01:20:52 +00:00
|
|
|
SFDNSRES::~SFDNSRES() = default;
|
|
|
|
|
2022-04-07 21:07:12 +00:00
|
|
|
enum class NetDbError : s32 {
|
|
|
|
Internal = -1,
|
|
|
|
Success = 0,
|
|
|
|
HostNotFound = 1,
|
|
|
|
TryAgain = 2,
|
|
|
|
NoRecovery = 3,
|
|
|
|
NoData = 4,
|
|
|
|
};
|
|
|
|
|
|
|
|
static NetDbError AddrInfoErrorToNetDbError(s32 result) {
|
|
|
|
// Best effort guess to map errors
|
|
|
|
switch (result) {
|
|
|
|
case 0:
|
|
|
|
return NetDbError::Success;
|
|
|
|
case EAI_AGAIN:
|
|
|
|
return NetDbError::TryAgain;
|
|
|
|
case EAI_NODATA:
|
|
|
|
return NetDbError::NoData;
|
|
|
|
default:
|
|
|
|
return NetDbError::HostNotFound;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::vector<u8> SerializeAddrInfo(const addrinfo* addrinfo, s32 result_code,
|
|
|
|
std::string_view host) {
|
|
|
|
// Adapted from
|
|
|
|
// https://github.com/switchbrew/libnx/blob/c5a9a909a91657a9818a3b7e18c9b91ff0cbb6e3/nx/source/runtime/resolver.c#L190
|
|
|
|
std::vector<u8> data;
|
|
|
|
|
|
|
|
auto* current = addrinfo;
|
|
|
|
while (current != nullptr) {
|
|
|
|
struct SerializedResponseHeader {
|
|
|
|
u32 magic;
|
|
|
|
s32 flags;
|
|
|
|
s32 family;
|
|
|
|
s32 socket_type;
|
|
|
|
s32 protocol;
|
|
|
|
u32 address_length;
|
|
|
|
};
|
|
|
|
static_assert(sizeof(SerializedResponseHeader) == 0x18,
|
|
|
|
"Response header size must be 0x18 bytes");
|
|
|
|
|
|
|
|
constexpr auto header_size = sizeof(SerializedResponseHeader);
|
|
|
|
const auto addr_size =
|
|
|
|
current->ai_addr && current->ai_addrlen > 0 ? current->ai_addrlen : 4;
|
|
|
|
const auto canonname_size = current->ai_canonname ? strlen(current->ai_canonname) + 1 : 1;
|
|
|
|
|
|
|
|
const auto last_size = data.size();
|
|
|
|
data.resize(last_size + header_size + addr_size + canonname_size);
|
|
|
|
|
|
|
|
// Header in network byte order
|
|
|
|
SerializedResponseHeader header{};
|
|
|
|
|
|
|
|
constexpr auto HEADER_MAGIC = 0xBEEFCAFE;
|
|
|
|
header.magic = htonl(HEADER_MAGIC);
|
|
|
|
header.family = htonl(current->ai_family);
|
|
|
|
header.flags = htonl(current->ai_flags);
|
|
|
|
header.socket_type = htonl(current->ai_socktype);
|
|
|
|
header.protocol = htonl(current->ai_protocol);
|
|
|
|
header.address_length = current->ai_addr ? htonl((u32)current->ai_addrlen) : 0;
|
|
|
|
|
|
|
|
auto* header_ptr = data.data() + last_size;
|
|
|
|
std::memcpy(header_ptr, &header, header_size);
|
|
|
|
|
|
|
|
if (header.address_length == 0) {
|
|
|
|
std::memset(header_ptr + header_size, 0, 4);
|
|
|
|
} else {
|
|
|
|
switch (current->ai_family) {
|
|
|
|
case AF_INET: {
|
|
|
|
struct SockAddrIn {
|
|
|
|
s16 sin_family;
|
|
|
|
u16 sin_port;
|
|
|
|
u32 sin_addr;
|
|
|
|
u8 sin_zero[8];
|
|
|
|
};
|
|
|
|
|
|
|
|
SockAddrIn serialized_addr{};
|
|
|
|
const auto addr = *reinterpret_cast<sockaddr_in*>(current->ai_addr);
|
|
|
|
serialized_addr.sin_port = htons(addr.sin_port);
|
|
|
|
serialized_addr.sin_family = htons(addr.sin_family);
|
|
|
|
serialized_addr.sin_addr = htonl(addr.sin_addr.s_addr);
|
|
|
|
std::memcpy(header_ptr + header_size, &serialized_addr, sizeof(SockAddrIn));
|
|
|
|
|
|
|
|
char addr_string_buf[64]{};
|
|
|
|
inet_ntop(AF_INET, &addr.sin_addr, addr_string_buf, std::size(addr_string_buf));
|
|
|
|
LOG_INFO(Service, "Resolved host '{}' to IPv4 address {}", host, addr_string_buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case AF_INET6: {
|
|
|
|
struct SockAddrIn6 {
|
|
|
|
s16 sin6_family;
|
|
|
|
u16 sin6_port;
|
|
|
|
u32 sin6_flowinfo;
|
|
|
|
u8 sin6_addr[16];
|
|
|
|
u32 sin6_scope_id;
|
|
|
|
};
|
|
|
|
|
|
|
|
SockAddrIn6 serialized_addr{};
|
|
|
|
const auto addr = *reinterpret_cast<sockaddr_in6*>(current->ai_addr);
|
|
|
|
serialized_addr.sin6_family = htons(addr.sin6_family);
|
|
|
|
serialized_addr.sin6_port = htons(addr.sin6_port);
|
|
|
|
serialized_addr.sin6_flowinfo = htonl(addr.sin6_flowinfo);
|
|
|
|
serialized_addr.sin6_scope_id = htonl(addr.sin6_scope_id);
|
|
|
|
std::memcpy(serialized_addr.sin6_addr, &addr.sin6_addr,
|
|
|
|
sizeof(SockAddrIn6::sin6_addr));
|
|
|
|
std::memcpy(header_ptr + header_size, &serialized_addr, sizeof(SockAddrIn6));
|
|
|
|
|
|
|
|
char addr_string_buf[64]{};
|
|
|
|
inet_ntop(AF_INET6, &addr.sin6_addr, addr_string_buf, std::size(addr_string_buf));
|
|
|
|
LOG_INFO(Service, "Resolved host '{}' to IPv6 address {}", host, addr_string_buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
std::memcpy(header_ptr + header_size, current->ai_addr, addr_size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (current->ai_canonname) {
|
|
|
|
std::memcpy(header_ptr + addr_size, current->ai_canonname, canonname_size);
|
|
|
|
} else {
|
|
|
|
*(header_ptr + header_size + addr_size) = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
current = current->ai_next;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 4-byte sentinel value
|
|
|
|
data.push_back(0);
|
|
|
|
data.push_back(0);
|
|
|
|
data.push_back(0);
|
|
|
|
data.push_back(0);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2023-02-19 19:42:12 +00:00
|
|
|
static std::pair<u32, s32> GetAddrInfoRequestImpl(HLERequestContext& ctx) {
|
2020-11-26 20:19:08 +00:00
|
|
|
struct Parameters {
|
|
|
|
u8 use_nsd_resolve;
|
|
|
|
u32 unknown;
|
|
|
|
u64 process_id;
|
|
|
|
};
|
|
|
|
|
|
|
|
IPC::RequestParser rp{ctx};
|
|
|
|
const auto parameters = rp.PopRaw<Parameters>();
|
|
|
|
|
|
|
|
LOG_WARNING(Service,
|
2022-04-07 21:07:12 +00:00
|
|
|
"called with ignored parameters: use_nsd_resolve={}, unknown={}, process_id={}",
|
2020-11-26 20:19:08 +00:00
|
|
|
parameters.use_nsd_resolve, parameters.unknown, parameters.process_id);
|
|
|
|
|
2022-12-25 19:31:53 +00:00
|
|
|
const auto host_buffer = ctx.ReadBuffer(0);
|
2022-04-07 21:07:12 +00:00
|
|
|
const std::string host = Common::StringFromBuffer(host_buffer);
|
|
|
|
|
2022-12-25 19:31:53 +00:00
|
|
|
const auto service_buffer = ctx.ReadBuffer(1);
|
2022-04-07 21:07:12 +00:00
|
|
|
const std::string service = Common::StringFromBuffer(service_buffer);
|
|
|
|
|
|
|
|
addrinfo* addrinfo;
|
|
|
|
// Pass null for hints. Serialized hints are also passed in a buffer, but are ignored for now
|
|
|
|
s32 result_code = getaddrinfo(host.c_str(), service.c_str(), nullptr, &addrinfo);
|
|
|
|
|
|
|
|
u32 data_size = 0;
|
|
|
|
if (result_code == 0 && addrinfo != nullptr) {
|
|
|
|
const std::vector<u8>& data = SerializeAddrInfo(addrinfo, result_code, host);
|
|
|
|
data_size = static_cast<u32>(data.size());
|
|
|
|
freeaddrinfo(addrinfo);
|
|
|
|
|
|
|
|
ctx.WriteBuffer(data, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::make_pair(data_size, result_code);
|
|
|
|
}
|
|
|
|
|
2023-02-19 19:42:12 +00:00
|
|
|
void SFDNSRES::GetAddrInfoRequest(HLERequestContext& ctx) {
|
2022-04-07 21:07:12 +00:00
|
|
|
auto [data_size, result_code] = GetAddrInfoRequestImpl(ctx);
|
|
|
|
|
|
|
|
IPC::ResponseBuilder rb{ctx, 4};
|
|
|
|
rb.Push(ResultSuccess);
|
|
|
|
rb.Push(static_cast<s32>(AddrInfoErrorToNetDbError(result_code))); // NetDBErrorCode
|
|
|
|
rb.Push(result_code); // errno
|
|
|
|
rb.Push(data_size); // serialized size
|
|
|
|
}
|
|
|
|
|
2023-02-19 19:42:12 +00:00
|
|
|
void SFDNSRES::GetAddrInfoRequestWithOptions(HLERequestContext& ctx) {
|
2022-04-07 21:07:12 +00:00
|
|
|
// Additional options are ignored
|
|
|
|
auto [data_size, result_code] = GetAddrInfoRequestImpl(ctx);
|
|
|
|
|
|
|
|
IPC::ResponseBuilder rb{ctx, 5};
|
2021-05-21 05:05:04 +00:00
|
|
|
rb.Push(ResultSuccess);
|
2022-04-07 21:07:12 +00:00
|
|
|
rb.Push(data_size); // serialized size
|
|
|
|
rb.Push(result_code); // errno
|
|
|
|
rb.Push(static_cast<s32>(AddrInfoErrorToNetDbError(result_code))); // NetDBErrorCode
|
|
|
|
rb.Push(0);
|
2020-11-26 20:19:08 +00:00
|
|
|
}
|
|
|
|
|
2023-02-03 05:08:45 +00:00
|
|
|
} // namespace Service::Sockets
|