feat(kernel): implement tracked UDP sockets and ioctl

This commit is contained in:
SamG-Coder
2026-09-25 06:23:44 +10:00
parent b42c33ca6c
commit 9cbefb7e04
8 changed files with 364 additions and 62 deletions
+10
View File
@@ -104,6 +104,16 @@ endforeach()
add_custom_target(libs DEPENDS ${PATCHED_OUTPUTS})
if(BUILD_TESTING)
add_executable(guest_udp_tests tests/GuestUdp.cpp)
target_include_directories(guest_udp_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(guest_udp_tests PRIVATE libkernel libc)
configure_windows_unwind(guest_udp_tests)
add_test(NAME guest_udp COMMAND guest_udp_tests)
set_tests_properties(guest_udp PROPERTIES TIMEOUT 20)
if(WIN32)
set_tests_properties(guest_udp PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
endif()
add_executable(guest_address_tests tests/GuestAddressConversion.cpp)
target_include_directories(guest_address_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(guest_address_tests PRIVATE libkernel libc)
+1
View File
@@ -25,6 +25,7 @@ add_library(libkernel SHARED EXCLUDE_FROM_ALL
${CMAKE_CURRENT_SOURCE_DIR}/Pthread/Posix/Tsd.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Pthread/Posix/Sem.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Socket/src/Socket.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Socket/src/Udp.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Socket/src/AddressConversion.cpp
${CMAKE_CURRENT_SOURCE_DIR}/EventFlag/src/EventFlag.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Module/src/Module.cpp
+12 -3
View File
@@ -2,6 +2,12 @@
#include <cstddef>
#include "SceTypes.hpp"
#include "prx/libc/include/General.hpp"
#include "prx/libkernel/Socket/include/SocketRuntime.hpp"
#ifdef _WIN32
#include <io.h>
#else
#include <unistd.h>
#endif
extern "C" {
@@ -13,9 +19,12 @@ int APS5_VABI chmod_nid_postfix(const char* path, int mode) {
}
int APS5_VABI close_nid_postfix(int d) {
(void)d;
NotImplemented_nid_no_patch(__func__);
return 0;
if (d >= GuestSockets::FirstDescriptor) return GuestSockets::Close(d);
#ifdef _WIN32
return _close(d);
#else
return ::close(d);
#endif
}
int APS5_VABI flock_nid_postfix(int d, int operation) {
@@ -0,0 +1,5 @@
#pragma once
namespace GuestSockets {
constexpr int FirstDescriptor = 0x10000000;
int Close(int descriptor);
}
@@ -13,13 +13,6 @@ int APS5_VABI accept_nid_postfix(int s, void* addr, uint32_t* addrlen) {
return 0;
}
int APS5_VABI bind_nid_postfix(int s, const void* addr, uint32_t addrlen) {
(void)s;
(void)addr;
(void)addrlen;
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI connect_nid_postfix(int s, const void* addr, uint32_t addrlen) {
(void)s;
@@ -36,41 +29,9 @@ int APS5_VABI listen_nid_postfix(int s, int backlog) {
return 0;
}
int APS5_VABI socket_nid_postfix(int family, int type, int protocol) {
(void)family;
(void)type;
(void)protocol;
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI getsockname_nid_postfix(int s, void* addr, uint32_t* addrlen) {
(void)s;
(void)addr;
(void)addrlen;
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI getsockopt_nid_postfix(int s, int level, int optname, void* optval, uint32_t* optlen) {
(void)s;
(void)level;
(void)optname;
(void)optval;
(void)optlen;
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI setsockopt_nid_postfix(int s, int level, int optname, const void* optval, uint32_t optlen) {
(void)s;
(void)level;
(void)optname;
(void)optval;
(void)optlen;
NotImplemented_nid_no_patch(__func__);
return 0;
}
int64_t APS5_VABI send_nid_postfix(int s, const void* buf, uint64_t len, int flags) {
(void)s;
@@ -81,16 +42,6 @@ int64_t APS5_VABI send_nid_postfix(int s, const void* buf, uint64_t len, int fla
return 0;
}
int64_t APS5_VABI sendto_nid_postfix(int s, const void* buf, uint64_t len, int flags, const void* addr, uint32_t addrlen) {
(void)s;
(void)buf;
(void)len;
(void)flags;
(void)addr;
(void)addrlen;
NotImplemented_nid_no_patch(__func__);
return 0;
}
int64_t APS5_VABI recv_nid_postfix(int s, void* buf, uint64_t len, int flags) {
(void)s;
@@ -101,16 +52,6 @@ int64_t APS5_VABI recv_nid_postfix(int s, void* buf, uint64_t len, int flags) {
return 0;
}
int64_t APS5_VABI recvfrom_nid_postfix(int s, void* buf, uint64_t len, int flags, void* addr, uint32_t* addrlen) {
(void)s;
(void)buf;
(void)len;
(void)flags;
(void)addr;
(void)addrlen;
NotImplemented_nid_no_patch(__func__);
return 0;
}
const char* APS5_VABI inet_ntop_nid_postfix(int af, const void* src, char* dst, uint32_t size) {
(void)af;
+254
View File
@@ -0,0 +1,254 @@
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <unistd.h>
#include <cerrno>
#endif
#include "prx/libc/include/general/VabiMacros.hpp"
#include "prx/libkernel/Socket/include/SocketRuntime.hpp"
#include <algorithm>
#include <climits>
#include <cstdint>
#include <cstring>
#include <map>
#include <memory>
#include <mutex>
extern "C" int* APS5_VABI __error_nid_postfix();
namespace {
#ifdef _WIN32
using NativeSocket = SOCKET;
constexpr auto Invalid = INVALID_SOCKET;
#else
using NativeSocket = int;
constexpr auto Invalid = -1;
#endif
int Fail(int error) { *__error_nid_postfix() = error; return -1; }
int NativeError() {
#ifdef _WIN32
switch (WSAGetLastError()) {
case WSAEWOULDBLOCK: return 35;
case WSAEADDRINUSE: return 48;
case WSAEADDRNOTAVAIL: return 49;
case WSAEACCES: return 13;
case WSAEMSGSIZE: return 40;
case WSAENETUNREACH: return 51;
case WSAECONNRESET: return 54;
case WSAENOBUFS: return 55;
case WSAETIMEDOUT: return 60;
case WSAECONNREFUSED: return 61;
case WSAEINTR: return 4;
case WSAEINVAL: return 22;
default: return 5;
}
#else
switch (errno) {
case EAGAIN: return 35;
case EADDRINUSE: return 48;
case EADDRNOTAVAIL: return 49;
case EMSGSIZE: return 40;
case ENETUNREACH: return 51;
case ECONNREFUSED: return 61;
default: return 5;
}
#endif
}
struct Socket {
NativeSocket value;
explicit Socket(NativeSocket value) : value(value) {}
~Socket() {
if (value == Invalid) return;
#ifdef _WIN32
closesocket(value);
#else
::close(value);
#endif
}
};
std::mutex socketsMutex;
std::map<int, std::shared_ptr<Socket>> sockets;
int nextDescriptor = GuestSockets::FirstDescriptor;
std::shared_ptr<Socket> Lookup(int descriptor) {
std::lock_guard lock(socketsMutex);
const auto found = sockets.find(descriptor);
if (found != sockets.end()) return found->second;
Fail(9);
return {};
}
int Option(int guest) {
switch (guest) {
case 0x4: return SO_REUSEADDR;
case 0x20: return SO_BROADCAST;
case 0x1001: return SO_SNDBUF;
case 0x1002: return SO_RCVBUF;
default: return -1;
}
}
bool Address(const void* input, std::uint32_t length, sockaddr_storage& native, socklen_t& size) {
if (!input || length < 2) { Fail(14); return false; }
const auto* bytes = static_cast<const unsigned char*>(input);
if (bytes[1] == 2 && length >= 16 && bytes[0] == 16) {
auto& v4 = reinterpret_cast<sockaddr_in&>(native);
v4.sin_family = AF_INET;
std::memcpy(&v4.sin_port, bytes + 2, 2);
std::memcpy(&v4.sin_addr, bytes + 4, 4);
size = sizeof(v4);
return true;
}
if (bytes[1] == 28 && length >= 28 && bytes[0] == 28) {
auto& v6 = reinterpret_cast<sockaddr_in6&>(native);
v6.sin6_family = AF_INET6;
std::memcpy(&v6.sin6_port, bytes + 2, 2);
std::memcpy(&v6.sin6_flowinfo, bytes + 4, 4);
std::memcpy(&v6.sin6_addr, bytes + 8, 16);
std::memcpy(&v6.sin6_scope_id, bytes + 24, 4);
size = sizeof(v6);
return true;
}
Fail(47);
return false;
}
void GuestAddress(const sockaddr_storage& native, void* output, std::uint32_t* length) {
unsigned char bytes[28]{};
bytes[0] = native.ss_family == AF_INET ? 16 : 28;
bytes[1] = native.ss_family == AF_INET ? 2 : 28;
if (native.ss_family == AF_INET) {
const auto& v4 = reinterpret_cast<const sockaddr_in&>(native);
std::memcpy(bytes + 2, &v4.sin_port, 2);
std::memcpy(bytes + 4, &v4.sin_addr, 4);
} else {
const auto& v6 = reinterpret_cast<const sockaddr_in6&>(native);
std::memcpy(bytes + 2, &v6.sin6_port, 2);
std::memcpy(bytes + 4, &v6.sin6_flowinfo, 4);
std::memcpy(bytes + 8, &v6.sin6_addr, 16);
std::memcpy(bytes + 24, &v6.sin6_scope_id, 4);
}
std::memcpy(output, bytes, std::min<std::uint32_t>(*length, bytes[0]));
*length = bytes[0];
}
}
int GuestSockets::Close(int descriptor) {
std::lock_guard lock(socketsMutex);
return sockets.erase(descriptor) ? 0 : Fail(9);
}
extern "C" {
int APS5_VABI setsockopt_nid_postfix(int descriptor, int level, int option,
const void* value, std::uint32_t length) {
const auto socket = Lookup(descriptor);
if (!socket) return -1;
const int nativeOption = Option(option);
if (level != 0xffff || nativeOption == -1) return Fail(42);
if (!value || length != sizeof(int)) return Fail(22);
return ::setsockopt(socket->value, SOL_SOCKET, nativeOption,
static_cast<const char*>(value), sizeof(int)) ? Fail(NativeError()) : 0;
}
int APS5_VABI getsockopt_nid_postfix(int descriptor, int level, int option,
void* value, std::uint32_t* length) {
const auto socket = Lookup(descriptor);
if (!socket) return -1;
const int nativeOption = Option(option);
if (level != 0xffff || nativeOption == -1) return Fail(42);
if (!value || !length || *length < sizeof(int)) return Fail(22);
int result = 0;
socklen_t size = sizeof(result);
if (::getsockopt(socket->value, SOL_SOCKET, nativeOption, reinterpret_cast<char*>(&result), &size))
return Fail(NativeError());
std::memcpy(value, &result, sizeof(result));
*length = sizeof(result);
return 0;
}
int APS5_VABI socket_nid_postfix(int family, int type, int protocol) {
if (family != 2 && family != 28) return Fail(47);
if (type != 2 || (protocol != 0 && protocol != 17)) return Fail(43);
#ifdef _WIN32
static const int startup = [] { WSADATA data{}; return WSAStartup(MAKEWORD(2, 2), &data); }();
if (startup) return Fail(5);
#endif
const auto native = ::socket(family == 2 ? AF_INET : AF_INET6, SOCK_DGRAM, protocol);
if (native == Invalid) return Fail(NativeError());
Socket guard(native);
try {
auto socket = std::make_shared<Socket>(native);
guard.value = Invalid;
std::lock_guard lock(socketsMutex);
if (nextDescriptor == INT_MAX) return Fail(24);
const int descriptor = nextDescriptor++;
sockets.emplace(descriptor, std::move(socket));
return descriptor;
} catch (const std::bad_alloc&) {
return Fail(12);
}
}
int APS5_VABI bind_nid_postfix(int descriptor, const void* address, std::uint32_t length) {
const auto socket = Lookup(descriptor);
if (!socket) return -1;
sockaddr_storage native{};
socklen_t size;
if (!Address(address, length, native, size)) return -1;
return ::bind(socket->value, reinterpret_cast<sockaddr*>(&native), size) ? Fail(NativeError()) : 0;
}
int APS5_VABI getsockname_nid_postfix(int descriptor, void* address, std::uint32_t* length) {
const auto socket = Lookup(descriptor);
if (!socket) return -1;
if (!address || !length) return Fail(14);
sockaddr_storage native{};
socklen_t size = sizeof(native);
if (::getsockname(socket->value, reinterpret_cast<sockaddr*>(&native), &size)) return Fail(NativeError());
GuestAddress(native, address, length);
return 0;
}
int APS5_VABI ioctl_nid_postfix(int descriptor, std::uint64_t request, void* argument) {
const auto socket = Lookup(descriptor);
if (!socket) return -1;
if (!argument) return Fail(14);
if (request != 0x8004667e && request != 0x4004667f) return Fail(25);
unsigned long value = 0;
if (request == 0x8004667e) value = *static_cast<int*>(argument) != 0;
#ifdef _WIN32
const auto result = ioctlsocket(socket->value, request == 0x8004667e ? FIONBIO : FIONREAD, &value);
#else
int nativeValue = static_cast<int>(value);
const auto result = ::ioctl(socket->value, request == 0x8004667e ? FIONBIO : FIONREAD, &nativeValue);
value = static_cast<unsigned long>(nativeValue);
#endif
if (result) return Fail(NativeError());
if (request == 0x4004667f) *static_cast<int*>(argument) = static_cast<int>(value);
return 0;
}
std::int64_t APS5_VABI sendto_nid_postfix(int descriptor, const void* buffer, std::uint64_t length,
int flags, const void* address, std::uint32_t addressLength) {
const auto socket = Lookup(descriptor);
if (!socket) return -1;
if (flags != 0) return Fail(45);
if (length > INT_MAX) return Fail(40);
if (!buffer && length) return Fail(14);
sockaddr_storage native{};
socklen_t size;
if (!Address(address, addressLength, native, size)) return -1;
const auto result = ::sendto(socket->value, static_cast<const char*>(buffer), static_cast<int>(length), 0,
reinterpret_cast<sockaddr*>(&native), size);
return result < 0 ? Fail(NativeError()) : result;
}
std::int64_t APS5_VABI recvfrom_nid_postfix(int descriptor, void* buffer, std::uint64_t length,
int flags, void* address, std::uint32_t* addressLength) {
const auto socket = Lookup(descriptor);
if (!socket) return -1;
if ((flags & ~2) != 0) return Fail(45);
if (length > INT_MAX) return Fail(40);
if ((!buffer && length) || (address && !addressLength)) return Fail(14);
sockaddr_storage native{};
socklen_t size = sizeof(native);
const auto result = ::recvfrom(socket->value, static_cast<char*>(buffer), static_cast<int>(length),
flags & 2 ? MSG_PEEK : 0, reinterpret_cast<sockaddr*>(&native), &size);
if (result < 0) return Fail(NativeError());
if (address) GuestAddress(native, address, addressLength);
return result;
}
}
+61
View File
@@ -0,0 +1,61 @@
#include "prx/libc/include/general/VabiMacros.hpp"
#include <array>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <thread>
#include <chrono>
extern "C" {
int APS5_VABI socket_nid_postfix(int, int, int);
int APS5_VABI setsockopt_nid_postfix(int, int, int, const void*, std::uint32_t);
int APS5_VABI getsockopt_nid_postfix(int, int, int, void*, std::uint32_t*);
int APS5_VABI bind_nid_postfix(int, const void*, std::uint32_t);
int APS5_VABI getsockname_nid_postfix(int, void*, std::uint32_t*);
int APS5_VABI ioctl_nid_postfix(int, std::uint64_t, void*);
std::int64_t APS5_VABI sendto_nid_postfix(int, const void*, std::uint64_t, int, const void*, std::uint32_t);
std::int64_t APS5_VABI recvfrom_nid_postfix(int, void*, std::uint64_t, int, void*, std::uint32_t*);
int APS5_VABI close_nid_postfix(int);
int* APS5_VABI __error_nid_postfix();
}
static void Require(bool value) { if (!value) std::abort(); }
int main() {
const int receiver = socket_nid_postfix(2, 2, 0);
const int sender = socket_nid_postfix(2, 2, 17);
Require(receiver >= 0 && sender >= 0 && receiver != sender);
std::array<unsigned char, 16> destination{16, 2, 0, 0, 127, 0, 0, 1};
Require(bind_nid_postfix(receiver, destination.data(), destination.size()) == 0);
std::uint32_t size = destination.size();
Require(getsockname_nid_postfix(receiver, destination.data(), &size) == 0 && size == 16);
Require(destination[2] || destination[3]);
int enabled = 1;
Require(setsockopt_nid_postfix(sender, 0xffff, 0x20, &enabled, sizeof(enabled)) == 0);
int option = 0;
std::uint32_t optionSize = sizeof(option);
Require(getsockopt_nid_postfix(sender, 0xffff, 0x20, &option, &optionSize) == 0 && option != 0);
Require(setsockopt_nid_postfix(sender, 0xffff, 12345, &enabled, sizeof(enabled)) == -1);
Require(*__error_nid_postfix() == 42);
Require(ioctl_nid_postfix(receiver, 0x8004667e, &enabled) == 0);
char buffer[64]{};
Require(recvfrom_nid_postfix(receiver, buffer, sizeof(buffer), 0, nullptr, nullptr) == -1);
Require(*__error_nid_postfix() == 35);
const char message[] = "guest UDP loopback";
Require(sendto_nid_postfix(sender, message, sizeof(message), 0, destination.data(), destination.size()) == sizeof(message));
int queued = 0;
for (int i = 0; i < 100 && queued == 0; ++i) {
Require(ioctl_nid_postfix(receiver, 0x4004667f, &queued) == 0);
if (!queued) std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
Require(queued == sizeof(message));
Require(recvfrom_nid_postfix(receiver, buffer, sizeof(buffer), 2, nullptr, nullptr) == sizeof(message));
std::array<unsigned char, 16> source{};
size = source.size();
Require(recvfrom_nid_postfix(receiver, buffer, sizeof(buffer), 0, source.data(), &size) == sizeof(message));
Require(std::strcmp(buffer, message) == 0 && source[1] == 2 && source[4] == 127);
Require(ioctl_nid_postfix(receiver, 0x4004667f, &queued) == 0 && queued == 0);
Require(ioctl_nid_postfix(receiver, 123, &queued) == -1 && *__error_nid_postfix() == 25);
Require(close_nid_postfix(receiver) == 0);
Require(close_nid_postfix(receiver) == -1 && *__error_nid_postfix() == 9);
Require(ioctl_nid_postfix(receiver, 0x4004667f, &queued) == -1);
Require(close_nid_postfix(sender) == 0);
Require(socket_nid_postfix(2, 1, 0) == -1); // unsupported TCP must not appear to work
}
+21
View File
@@ -159,3 +159,24 @@ Latest startup stops at `PfccT7qURYE` (`ioctl`). The existing libkernel socket
operations include unimplemented placeholders, so the socket lifecycle needs
review before networking behavior can be considered supported. No gameplay
or guest entry-point execution is established by resolving these imports.
## Follow-up: tracked UDP sockets
Replaced the UDP-path placeholders with tracked guest descriptors, IPv4/IPv6
address conversion, bind, getsockname, sendto, recvfrom, and close. Added guest
FIONBIO/FIONREAD ioctl translation and integer socket options for reuse-address,
broadcast, send-buffer size, and receive-buffer size. Descriptors occupy a
separate range from host CRT file descriptors and are not reused. In-flight
operations retain native socket ownership while guest close removes the handle.
Allocation failure closes the native socket and reports guest ENOMEM.
The new loopback test sends and receives a real IPv4 UDP datagram through guest
exports, checks nonblocking EAGAIN, queued-byte counts, MSG_PEEK, broadcast
options, and closed-descriptor rejection. All eleven suites pass. IPv6 socket
address translation is implemented but this UDP test exercises IPv4 only.
TCP, select, connected send/recv, and unimplemented options remain unsupported;
online multiplayer is not validated. ioctl currently accepts tracked sockets
only. POSIX close also closes ordinary host CRT descriptors.
The unchanged game resolves ioctl and now stops at `mkawd0NA9ts` (`sysconf`),
before guest entry-point execution. No rendered frame has been observed.