mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
Merge pull request #1838 from LeandroLP23/feat/net-resolver-multiple-records-ex
feat(libSceNet): implement sceNetResolverStartNtoaMultipleRecordsEx
This commit is contained in:
@@ -1074,11 +1074,7 @@ int APS5_VABI sceNetResolverDestroy(int rid) {
|
||||
return g_resolvers.erase(rid) != 0 ? 0 : fail(NET_EBADF);
|
||||
}
|
||||
|
||||
int APS5_VABI sceNetResolverStartNtoa(int rid, const char* hostname, void* addr, int timeout, int retry, int flags) {
|
||||
(void)timeout;
|
||||
(void)retry;
|
||||
(void)flags;
|
||||
if (!hostname || !addr) return fail(NET_EINVAL);
|
||||
int lookup_ipv4(int rid, const char* hostname, const char* func, std::vector<std::uint32_t>& addresses) {
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(g_mutex);
|
||||
if (g_resolvers.count(rid) == 0) return fail(NET_EBADF);
|
||||
@@ -1090,17 +1086,64 @@ int APS5_VABI sceNetResolverStartNtoa(int rid, const char* hostname, void* addr,
|
||||
const int result = ::getaddrinfo(hostname, nullptr, &hints, &results);
|
||||
if (result != 0) {
|
||||
*errno_slot() = result == EAI_AGAIN ? NET_ETIMEDOUT : NET_ENOENT;
|
||||
log_soft(__func__, "host DNS lookup failed");
|
||||
log_soft(func, "host DNS lookup failed");
|
||||
set_resolver_error(rid, NET_ERROR_RESOLVER_ENODNS);
|
||||
return NET_ERROR_RESOLVER_ENODNS;
|
||||
}
|
||||
const auto* address = reinterpret_cast<const sockaddr_in*>(results->ai_addr);
|
||||
std::memcpy(addr, &address->sin_addr, sizeof(address->sin_addr));
|
||||
for (const addrinfo* entry = results; entry != nullptr; entry = entry->ai_next) {
|
||||
const auto address = reinterpret_cast<const sockaddr_in*>(entry->ai_addr)->sin_addr.s_addr;
|
||||
if (std::find(addresses.begin(), addresses.end(), address) == addresses.end()) addresses.push_back(address);
|
||||
}
|
||||
::freeaddrinfo(results);
|
||||
set_resolver_error(rid, 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int APS5_VABI sceNetResolverStartNtoa(int rid, const char* hostname, void* addr, int timeout, int retry, int flags) {
|
||||
(void)timeout;
|
||||
(void)retry;
|
||||
(void)flags;
|
||||
if (!hostname || !addr) return fail(NET_EINVAL);
|
||||
std::vector<std::uint32_t> addresses;
|
||||
if (const int error = lookup_ipv4(rid, hostname, __func__, addresses)) return error;
|
||||
std::memcpy(addr, addresses.data(), sizeof(addresses[0]));
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct NetResolverRecord {
|
||||
std::uint32_t address;
|
||||
std::uint8_t address6[12];
|
||||
std::int32_t family;
|
||||
std::int32_t reserved[3];
|
||||
};
|
||||
|
||||
struct NetResolverInfo {
|
||||
NetResolverRecord records[10];
|
||||
std::int32_t count;
|
||||
std::int32_t count4;
|
||||
std::int32_t reserved[14];
|
||||
};
|
||||
static_assert(sizeof(NetResolverRecord) == 32 && offsetof(NetResolverRecord, family) == 16);
|
||||
static_assert(sizeof(NetResolverInfo) == 384 && offsetof(NetResolverInfo, count) == 320 && offsetof(NetResolverInfo, count4) == 324);
|
||||
|
||||
int APS5_VABI sceNetResolverStartNtoaMultipleRecordsEx(int rid, const char* hostname, NetResolverInfo* info, int timeout,
|
||||
int retry, int flags) {
|
||||
(void)timeout;
|
||||
(void)retry;
|
||||
if (flags != 0) throw std::runtime_error("sceNetResolverStartNtoaMultipleRecordsEx: flags " + std::to_string(flags) + " are not supported");
|
||||
if (!hostname || !info) return fail(NET_EINVAL);
|
||||
std::vector<std::uint32_t> addresses;
|
||||
if (const int error = lookup_ipv4(rid, hostname, __func__, addresses)) return error;
|
||||
*info = {};
|
||||
info->count = static_cast<std::int32_t>(std::min<std::size_t>(addresses.size(), std::size(info->records)));
|
||||
info->count4 = info->count;
|
||||
for (std::int32_t index = 0; index < info->count; ++index) {
|
||||
info->records[index].address = addresses[index];
|
||||
info->records[index].family = NET_AF_INET;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int APS5_VABI sceNetResolverStartAton(int rid, const void* addr, char* hostname, int len, int timeout, int retry, int flags) {
|
||||
(void)timeout;
|
||||
(void)retry;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "prx/libc/include/general/VabiMacros.hpp"
|
||||
#include "SceTypes.hpp"
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
@@ -32,6 +33,7 @@ int APS5_VABI sceNetEpollDestroy(int);
|
||||
extern const std::uint32_t sce_net_in6addr_any[4];
|
||||
int APS5_VABI sceNetResolverCreate(const char*, int, int);
|
||||
int APS5_VABI sceNetResolverStartNtoa(int, const char*, void*, int, int, int);
|
||||
int APS5_VABI sceNetResolverStartNtoaMultipleRecordsEx(int, const char*, void*, int, int, int);
|
||||
int APS5_VABI sceNetResolverDestroy(int);
|
||||
int APS5_VABI sceNetResolverGetError(int, int*);
|
||||
int APS5_VABI sceNetCtlGetState(int*);
|
||||
@@ -306,11 +308,38 @@ int main() {
|
||||
Require(sceNetResolverStartNtoa(resolver, "localhost", ipv4.data(), 5000000, 1, 0) == 0);
|
||||
Require(ipv4[0] == 127);
|
||||
Require(sceNetResolverGetError(resolver, &resolver_error) == 0 && resolver_error == 0);
|
||||
std::array<std::uint8_t, 512> records{};
|
||||
records.fill(0xA5);
|
||||
Require(sceNetResolverStartNtoaMultipleRecordsEx(resolver, "localhost", records.data(), 5000000, 1, 0) == 0);
|
||||
std::int32_t record_family = 0;
|
||||
std::int32_t record_count = 0;
|
||||
std::int32_t record_count4 = 0;
|
||||
std::memcpy(&record_family, records.data() + 16, sizeof(record_family));
|
||||
std::memcpy(&record_count, records.data() + 320, sizeof(record_count));
|
||||
std::memcpy(&record_count4, records.data() + 324, sizeof(record_count4));
|
||||
Require(records[0] == 127 && record_family == 2 && record_count >= 1 && record_count <= 10 && record_count4 == record_count);
|
||||
Require(std::all_of(records.begin() + 32 * record_count, records.begin() + 320, [](std::uint8_t byte) { return byte == 0; }));
|
||||
Require(std::all_of(records.begin() + 328, records.begin() + 384, [](std::uint8_t byte) { return byte == 0; }));
|
||||
Require(std::all_of(records.begin() + 384, records.end(), [](std::uint8_t byte) { return byte == 0xA5; }));
|
||||
for (int first = 0; first < record_count; ++first) {
|
||||
for (int second = first + 1; second < record_count; ++second) {
|
||||
Require(std::memcmp(records.data() + 32 * first, records.data() + 32 * second, 4) != 0);
|
||||
}
|
||||
}
|
||||
const auto resolved = records;
|
||||
Require(sceNetResolverStartNtoaMultipleRecordsEx(resolver, nullptr, records.data(), 5000000, 1, 0) ==
|
||||
static_cast<int>(0x80410116) && *sceNetErrnoLoc() == 22 && records == resolved);
|
||||
Require(sceNetResolverStartNtoaMultipleRecordsEx(resolver, "localhost", nullptr, 5000000, 1, 0) ==
|
||||
static_cast<int>(0x80410116) && *sceNetErrnoLoc() == 22);
|
||||
Require(sceNetResolverStartNtoaMultipleRecordsEx(resolver, "guest-sce-net.invalid", records.data(), 5000000, 1, 0) ==
|
||||
static_cast<int>(0x804101E1) && records == resolved);
|
||||
Require(sceNetResolverGetError(resolver, nullptr) == static_cast<int>(0x80410116) && *sceNetErrnoLoc() == 22);
|
||||
Require(sceNetResolverDestroy(resolver) == 0);
|
||||
resolver_error = -1;
|
||||
Require(sceNetResolverGetError(resolver, &resolver_error) == static_cast<int>(0x80410109) &&
|
||||
*sceNetErrnoLoc() == 9 && resolver_error == -1);
|
||||
Require(sceNetResolverStartNtoaMultipleRecordsEx(resolver, "localhost", records.data(), 5000000, 1, 0) ==
|
||||
static_cast<int>(0x80410109) && *sceNetErrnoLoc() == 9 && records == resolved);
|
||||
|
||||
std::array<std::uint8_t, 16> ipv6{};
|
||||
Require(sceNetInetPton(28, "::1", ipv6.data()) == 1);
|
||||
|
||||
Reference in New Issue
Block a user