From a84d3c46c8d9006c5ead37c63984e1bfc8cbea3d Mon Sep 17 00:00:00 2001 From: Queuze <58829992+lucaslima430@users.noreply.github.com> Date: Wed, 21 Oct 2026 23:02:48 -0300 Subject: [PATCH 1/2] feat(net): expose stream and datagram protocol --- core/libs/prx/libSceNet/CMakeLists.txt | 10 +++ core/libs/prx/libSceNet/Export.cpp | 12 ++++ core/libs/tests/GuestNetProtocol.cpp | 84 ++++++++++++++++++++++++++ docs/dev/debt/libSceNet.md | 2 + 4 files changed, 108 insertions(+) create mode 100644 core/libs/tests/GuestNetProtocol.cpp diff --git a/core/libs/prx/libSceNet/CMakeLists.txt b/core/libs/prx/libSceNet/CMakeLists.txt index 95faf4584b3..435d5bb9797 100644 --- a/core/libs/prx/libSceNet/CMakeLists.txt +++ b/core/libs/prx/libSceNet/CMakeLists.txt @@ -4,6 +4,16 @@ target_link_libraries(libSceNet PRIVATE libSceNetCtl) configure_windows_unwind(libSceNet) if(BUILD_TESTING) + add_test_executable(guest_net_protocol_tests ${LIBS_INCLUDE_DIR}/tests/GuestNetProtocol.cpp) + target_include_directories(guest_net_protocol_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_net_protocol_tests PRIVATE libSceNet libkernel libc) + configure_windows_unwind(guest_net_protocol_tests) + add_test(NAME guest_net_protocol COMMAND guest_net_protocol_tests) + set_tests_properties(guest_net_protocol PROPERTIES TIMEOUT 20) + if(WIN32) + set_tests_properties(guest_net_protocol PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() add_test_executable(net_dontwait_tests ${LIBS_INCLUDE_DIR}/tests/NetDontWait.cpp) target_include_directories(net_dontwait_tests PRIVATE ${LIBS_INCLUDE_DIR}) target_link_libraries(net_dontwait_tests PRIVATE libSceNet libkernel libc) diff --git a/core/libs/prx/libSceNet/Export.cpp b/core/libs/prx/libSceNet/Export.cpp index 808cdc73643..8d1778fe51d 100644 --- a/core/libs/prx/libSceNet/Export.cpp +++ b/core/libs/prx/libSceNet/Export.cpp @@ -273,6 +273,7 @@ constexpr int NET_TF_NODELAY = 4; constexpr int NET_SO_SNDTIMEO = 0x1005; constexpr int NET_SO_RCVTIMEO = 0x1006; constexpr int NET_SO_ERROR = 0x1007; +constexpr int NET_SO_PROTOCOL = 0x1016; constexpr int NET_SO_NBIO = 0x1200; constexpr int NET_SO_REUSEADDR = 0x4; constexpr int NET_SO_BROADCAST = 0x20; @@ -390,6 +391,7 @@ bool native_to_guest_address(const sockaddr_storage& native, void* address, std: struct Sock { int family = 0; int type = 0; + int protocol = 0; std::shared_ptr native; bool bound = false; bool listening = false; @@ -1006,6 +1008,9 @@ int APS5_VABI sceNetSocket(const char* name, int family, int type, int protocol) Sock s; s.family = family; s.type = type; + s.protocol = protocol; + if (protocol == 0 && type == NET_SOCK_STREAM) s.protocol = IPPROTO_TCP; + if (protocol == 0 && type == NET_SOCK_DGRAM) s.protocol = IPPROTO_UDP; s.native = std::move(handle); g_socks[fd] = s; return fd; @@ -1129,6 +1134,7 @@ int APS5_VABI sceNetAccept(int s, void* addr, uint32_t* addrlen) { Sock connection; connection.family = listener.family; connection.type = NET_SOCK_STREAM; + connection.protocol = listener.protocol; connection.native = std::move(handle); #ifdef _WIN32 connection.native->receiveTimeout = listener.native->receiveTimeout.load(); @@ -1536,6 +1542,12 @@ int APS5_VABI sceNetGetsockopt(int s, int level, int optname, void* optval, uint socket = it->second; } if (optval == nullptr || optlen == nullptr) return fail(NET_EINVAL); + if (level == NET_SOL_SOCKET && optname == NET_SO_PROTOCOL) { + if (socket.type == NET_SOCK_RAW) NotImplemented_nid_no_patch(__func__); + *optlen = std::min(*optlen, static_cast(sizeof(socket.protocol))); + std::memcpy(optval, &socket.protocol, *optlen); + return 0; + } if (level == NET_SOL_SOCKET && (optname == NET_SO_RCVTIMEO || optname == NET_SO_SNDTIMEO)) { const int microseconds = optname == NET_SO_RCVTIMEO ? socket.native->receiveTimeout.load() : socket.native->sendTimeout.load(); const KernelTimeval timeout{microseconds / 1000000, microseconds % 1000000}; diff --git a/core/libs/tests/GuestNetProtocol.cpp b/core/libs/tests/GuestNetProtocol.cpp new file mode 100644 index 00000000000..01f247b3e08 --- /dev/null +++ b/core/libs/tests/GuestNetProtocol.cpp @@ -0,0 +1,84 @@ +#include +#include +#include +#include +#include +#include +#include +#include "prx/libc/include/general/VabiMacros.hpp" + +extern "C" { +int APS5_VABI sceNetInit_nid_postfix(); +int APS5_VABI sceNetSocket(const char*, int, int, int); +int APS5_VABI sceNetSocketClose(int); +int APS5_VABI sceNetGetsockopt(int, int, int, void*, std::uint32_t*); +int APS5_VABI sceNetSetsockopt(int, int, int, const void*, std::uint32_t); +int APS5_VABI sceNetBind_nid_postfix(int, const void*, std::uint32_t); +int APS5_VABI sceNetGetsockname(int, void*, std::uint32_t*); +int APS5_VABI sceNetListen(int, int); +int APS5_VABI sceNetConnect(int, const void*, std::uint32_t); +int APS5_VABI sceNetAccept(int, void*, std::uint32_t*); +int* APS5_VABI sceNetErrnoLoc(); +} + +static void Require(bool condition, std::source_location where = std::source_location::current()) { + if (!condition) { + std::fprintf(stderr, "check failed at line %u\n", where.line()); + std::abort(); + } +} + +static void CheckProtocol(int socket, int expected) { + for (const std::uint32_t capacity : {0u, 1u, 3u, 4u, 8u}) { + std::array bytes; + bytes.fill(0xa5); + auto size = capacity; + Require(sceNetGetsockopt(socket, 0xffff, 0x1016, bytes.data(), &size) == 0); + Require(size == std::min(capacity, 4u)); + Require(std::memcmp(bytes.data(), &expected, size) == 0); + Require(std::all_of(bytes.begin() + size, bytes.end(), [](auto value) { return value == 0xa5; })); + } +} + +int main() { + Require(sceNetInit_nid_postfix() == 0); + for (const int family : {2, 28}) { + for (const int type : {1, 2}) { + const int expected = type == 1 ? 6 : 17; + for (const int protocol : {0, expected}) { + const int socket = sceNetSocket("protocol", family, type, protocol); + Require(socket >= 0); + CheckProtocol(socket, expected); + std::uint32_t size = 4; + int value = 99; + Require(sceNetGetsockopt(socket, 0xffff, 0x1016, nullptr, &size) < 0 && *sceNetErrnoLoc() == 22); + Require(sceNetGetsockopt(socket, 0xffff, 0x1016, &value, nullptr) < 0 && *sceNetErrnoLoc() == 22); + Require(value == 99 && size == 4); + Require(sceNetSetsockopt(socket, 0xffff, 0x1016, &value, sizeof(value)) < 0); + CheckProtocol(socket, expected); + Require(sceNetSocketClose(socket) == 0); + Require(sceNetGetsockopt(socket, 0xffff, 0x1016, &value, &size) < 0 && *sceNetErrnoLoc() == 9); + Require(value == 99 && size == 4); + } + } + const int listener = sceNetSocket("protocol-listener", family, 1, 0); + const int client = sceNetSocket("protocol-client", family, 1, 6); + Require(listener >= 0 && client >= 0); + std::array address{}; + std::uint32_t size = family == 2 ? 16 : 28; + address[0] = size; + address[1] = family; + if (family == 2) { address[4] = 127; address[7] = 1; } + else address[23] = 1; + Require(sceNetBind_nid_postfix(listener, address.data(), size) == 0); + Require(sceNetListen(listener, 1) == 0); + CheckProtocol(listener, 6); + Require(sceNetGetsockname(listener, address.data(), &size) == 0); + Require(sceNetConnect(client, address.data(), size) == 0); + const int accepted = sceNetAccept(listener, nullptr, nullptr); + Require(accepted >= 0); + CheckProtocol(accepted, 6); + CheckProtocol(client, 6); + for (const int socket : {accepted, client, listener}) Require(sceNetSocketClose(socket) == 0); + } +} diff --git a/docs/dev/debt/libSceNet.md b/docs/dev/debt/libSceNet.md index f10d1eb8737..e6430e89fe3 100644 --- a/docs/dev/debt/libSceNet.md +++ b/docs/dev/debt/libSceNet.md @@ -7,6 +7,8 @@ ### Unknown function info +- [sceNetGetsockopt](../../../core/libs/prx/libSceNet/Export.cpp) - `SO_PROTOCOL` on raw sockets throws: FreeBSD's raw protocol-switch selection has not been mapped to host raw sockets. For stream and datagram sockets, the effective creation protocol and accepted-socket inheritance follow FreeBSD 11.4's [protocol tables](https://github.com/freebsd/freebsd-src/blob/releng/11.4/sys/netinet/in_proto.c) and [sogetopt](https://github.com/freebsd/freebsd-src/blob/releng/11.4/sys/kern/uipc_socket.c); the `0x1016` option and truncated integer output are not measured on PS5. Existing null-pointer validation still returns `EINVAL`. + - [sceNetConfigAddIfaddr](../../../core/libs/prx/libSceNet/Export.cpp) (libSceNet) - disassembly confirms the same interface/address/broadcast/netmask arguments as SetIfaddr, entering its shared handler with add mode selected. GetIfaddr copies up to two 16-byte interface-address records. GetRouteInfo uses a family, record buffer, signed capacity and byte flags; internal statistics output is 16 bytes. The address-record and internal-statistics fields remain incomplete. - [Resolver-connect, thread creation, interface and diagnostic queries, WLAN configuration and DHCP queries](../../../core/libs/prx/libSceNet/Export.cpp) retain unsupported implementations. Their restored argument lists still reference incomplete connection, thread, interface, route, WLAN and statistics types. Kernel ioctl payloads need reconstruction from their handlers or consumers; buffer lengths alone do not establish field types. From 05eec3e725dd216e55a8da49318c24cd1e2c780e Mon Sep 17 00:00:00 2001 From: Queuze <58829992+lucaslima430@users.noreply.github.com> Date: Sun, 25 Oct 2026 07:26:07 -0300 Subject: [PATCH 2/2] test(net): use canonical public declarations --- core/libs/tests/GuestNetProtocol.cpp | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/core/libs/tests/GuestNetProtocol.cpp b/core/libs/tests/GuestNetProtocol.cpp index 01f247b3e08..038c59f77dc 100644 --- a/core/libs/tests/GuestNetProtocol.cpp +++ b/core/libs/tests/GuestNetProtocol.cpp @@ -5,21 +5,8 @@ #include #include #include -#include "prx/libc/include/general/VabiMacros.hpp" +#include "prx/libSceNet/libSceNet.hpp" -extern "C" { -int APS5_VABI sceNetInit_nid_postfix(); -int APS5_VABI sceNetSocket(const char*, int, int, int); -int APS5_VABI sceNetSocketClose(int); -int APS5_VABI sceNetGetsockopt(int, int, int, void*, std::uint32_t*); -int APS5_VABI sceNetSetsockopt(int, int, int, const void*, std::uint32_t); -int APS5_VABI sceNetBind_nid_postfix(int, const void*, std::uint32_t); -int APS5_VABI sceNetGetsockname(int, void*, std::uint32_t*); -int APS5_VABI sceNetListen(int, int); -int APS5_VABI sceNetConnect(int, const void*, std::uint32_t); -int APS5_VABI sceNetAccept(int, void*, std::uint32_t*); -int* APS5_VABI sceNetErrnoLoc(); -} static void Require(bool condition, std::source_location where = std::source_location::current()) { if (!condition) {