From d14f036122a8c8dc7cfa101119f74fe7d83adbd4 Mon Sep 17 00:00:00 2001 From: Mouhand-Kaddo Date: Thu, 8 Oct 2026 23:21:55 +0400 Subject: [PATCH] feat(posix): implement gethostbyname Resolve the missing EVO-PLAYER import reported in #1578. Convert native host entries into the guest ABI with owned per-thread storage, and use the SDK network error location for failures. --- .../prx/libScePosixForWebKit/CMakeLists.txt | 1 + .../prx/libScePosixForWebKit/Resolver.cpp | 62 +++++++++++++++++++ core/libs/tests/GuestResolver.cpp | 22 +++++++ 3 files changed, 85 insertions(+) diff --git a/core/libs/prx/libScePosixForWebKit/CMakeLists.txt b/core/libs/prx/libScePosixForWebKit/CMakeLists.txt index 956b7aca7..ce2e4cac7 100644 --- a/core/libs/prx/libScePosixForWebKit/CMakeLists.txt +++ b/core/libs/prx/libScePosixForWebKit/CMakeLists.txt @@ -12,6 +12,7 @@ if(BUILD_TESTING) target_link_libraries(guest_resolver_tests PRIVATE libScePosixForWebKit libSceNet) add_test(NAME guest_resolver COMMAND guest_resolver_tests) if(WIN32) + target_link_libraries(guest_resolver_tests PRIVATE ws2_32) set_tests_properties(guest_resolver PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() diff --git a/core/libs/prx/libScePosixForWebKit/Resolver.cpp b/core/libs/prx/libScePosixForWebKit/Resolver.cpp index 5952e6276..c6f34c02f 100644 --- a/core/libs/prx/libScePosixForWebKit/Resolver.cpp +++ b/core/libs/prx/libScePosixForWebKit/Resolver.cpp @@ -11,9 +11,24 @@ #include #include #include +#include +#include +#include +#include #include "GuestResolver.hpp" +extern "C" int* APS5_VABI sceNetErrnoLoc(); + namespace { +struct HostLookup { + GuestResolver::HostEntry entry{}; + std::string name; + std::vector aliasNames; + std::vector aliases; + std::vector addressBytes; + std::vector addresses; +}; + bool Ready() { #ifdef _WIN32 static const int status = [] { WSADATA data{}; return WSAStartup(MAKEWORD(2, 2), &data); }(); @@ -41,6 +56,53 @@ int GuestError(int error) { } extern "C" { +GuestResolver::HostEntry* APS5_VABI gethostbyname_nid_postfix(const char* name) { + if (!name || !Ready()) { *sceNetErrnoLoc() = 3; return nullptr; } + try { +#ifdef _WIN32 + auto* native = ::gethostbyname(name); + if (!native) { + const auto error = WSAGetLastError(); + *sceNetErrnoLoc() = error == WSAHOST_NOT_FOUND ? 1 : error == WSATRY_AGAIN ? 2 : error == WSANO_DATA ? 4 : 3; + return nullptr; + } +#else + hostent nativeEntry{}; + hostent* native = nullptr; + std::vector buffer(1024); + int hostError = 0; + int error; + while ((error = ::gethostbyname_r(name, &nativeEntry, buffer.data(), buffer.size(), &native, &hostError)) == ERANGE) + buffer.resize(buffer.size() * 2); + if (error != 0 || !native) { + *sceNetErrnoLoc() = hostError != 0 ? hostError : 3; + if (hostError == -1) errno = error == EAGAIN ? 35 : error; + return nullptr; + } +#endif + static thread_local HostLookup lookup; + lookup.name = native->h_name; + lookup.aliasNames.clear(); + lookup.aliases.clear(); + lookup.addressBytes.clear(); + lookup.addresses.clear(); + for (auto** alias = native->h_aliases; *alias; ++alias) lookup.aliasNames.emplace_back(*alias); + for (auto& alias : lookup.aliasNames) lookup.aliases.push_back(alias.data()); + lookup.aliases.push_back(nullptr); + for (auto** address = native->h_addr_list; *address; ++address) + lookup.addressBytes.emplace_back(*address, native->h_length); + for (auto& address : lookup.addressBytes) lookup.addresses.push_back(address.data()); + lookup.addresses.push_back(nullptr); + lookup.entry = {lookup.name.data(), lookup.aliases.data(), native->h_addrtype == AF_INET6 ? 28 : 2, + native->h_length, lookup.addresses.data()}; + return &lookup.entry; + } catch (const std::bad_alloc&) { + *sceNetErrnoLoc() = -1; + errno = ENOMEM; + return nullptr; + } +} + void APS5_VABI freeaddrinfo_nid_postfix(GuestResolver::AddressInfo* first) { while (first) { auto* next = first->next; diff --git a/core/libs/tests/GuestResolver.cpp b/core/libs/tests/GuestResolver.cpp index 9441b01eb..8e9f8ae8c 100644 --- a/core/libs/tests/GuestResolver.cpp +++ b/core/libs/tests/GuestResolver.cpp @@ -1,3 +1,9 @@ +#ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN +#include +#else +#include +#endif #include "prx/libc/include/general/VabiMacros.hpp" #include "prx/libScePosixForWebKit/GuestResolver.hpp" #include @@ -32,6 +38,22 @@ int main() { Require(legacy->addresses && legacy->addresses[0]); Require(std::memcmp(legacy->addresses[0], loopback.data(), loopback.size()) == 0); Require(legacy->addresses[1] == nullptr && legacy->aliases && legacy->aliases[0] == nullptr); + legacy = gethostbyname_nid_postfix("localhost"); + const auto* native = ::gethostbyname("localhost"); + Require(legacy && native && std::strcmp(legacy->name, native->h_name) == 0); + Require(legacy->family == 2 && legacy->length == native->h_length); + std::size_t index = 0; + while (native->h_aliases[index]) { + Require(legacy->aliases[index] && std::strcmp(legacy->aliases[index], native->h_aliases[index]) == 0); + ++index; + } + Require(legacy->aliases[index] == nullptr); + index = 0; + while (native->h_addr_list[index]) { + Require(legacy->addresses[index] && std::memcmp(legacy->addresses[index], native->h_addr_list[index], legacy->length) == 0); + ++index; + } + Require(legacy->addresses[index] == nullptr); std::barrier ready(2); std::thread worker([&] { auto* entry = gethostbyname_nid_postfix("127.0.0.2");