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.
This commit is contained in:
Mouhand-Kaddo
2026-10-08 23:21:55 +04:00
parent e0b0d087cc
commit d14f036122
3 changed files with 85 additions and 0 deletions
@@ -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:$<TARGET_FILE_DIR:libScePosixForWebKit>")
endif()
@@ -11,9 +11,24 @@
#include <cstdint>
#include <cstring>
#include <cstdlib>
#include <cerrno>
#include <new>
#include <string>
#include <vector>
#include "GuestResolver.hpp"
extern "C" int* APS5_VABI sceNetErrnoLoc();
namespace {
struct HostLookup {
GuestResolver::HostEntry entry{};
std::string name;
std::vector<std::string> aliasNames;
std::vector<char*> aliases;
std::vector<std::string> addressBytes;
std::vector<char*> 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<char> 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;
+22
View File
@@ -1,3 +1,9 @@
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#else
#include <netdb.h>
#endif
#include "prx/libc/include/general/VabiMacros.hpp"
#include "prx/libScePosixForWebKit/GuestResolver.hpp"
#include <array>
@@ -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");