From 513479380c2ae8d0080db4e76acb321fd43fb466 Mon Sep 17 00:00:00 2001 From: Raj Date: Thu, 8 Oct 2026 12:22:49 +0530 Subject: [PATCH 1/2] fix(libSceNet): report EINPROGRESS for a non-blocking connect A non-blocking sceNetConnect that cannot complete at once must fail with EINPROGRESS (36, 0x80410124), as FreeBSD's connect does, and a repeated call while it is pending with EALREADY (37). On Windows, Winsock reports the first case as WSAEWOULDBLOCK, which was translated to EAGAIN (0x80410123); on Linux, EINPROGRESS and EALREADY had no translation and became EIO (0x80410105). Code that waits for the connection only when connect reports EINPROGRESS treated both as a failed connection. guest_sce_net connects a non-blocking socket to the loopback listener and accepts 0 or EINPROGRESS; before this change it received EAGAIN on Windows. --- core/libs/prx/libSceNet/Export.cpp | 13 +++++++++++-- core/libs/tests/GuestSceNet.cpp | 6 ++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/core/libs/prx/libSceNet/Export.cpp b/core/libs/prx/libSceNet/Export.cpp index a67af3b25..b35d9c6ce 100644 --- a/core/libs/prx/libSceNet/Export.cpp +++ b/core/libs/prx/libSceNet/Export.cpp @@ -42,6 +42,8 @@ constexpr int NET_EFAULT = 14; constexpr int NET_EINVAL = 22; constexpr int NET_ENOSPC = 28; constexpr int NET_EAGAIN = 35; +constexpr int NET_EINPROGRESS = 36; +constexpr int NET_EALREADY = 37; constexpr int NET_ENOTSOCK = 38; constexpr int NET_EOPNOTSUPP = 45; constexpr int NET_EPROTONOSUPPORT = 43; @@ -84,6 +86,8 @@ void close_socket(NativeSocket socket) { closesocket(socket); } int native_error() { switch (WSAGetLastError()) { case WSAEWOULDBLOCK: return NET_EAGAIN; + case WSAEINPROGRESS: return NET_EINPROGRESS; + case WSAEALREADY: return NET_EALREADY; case WSAEADDRINUSE: return NET_EADDRINUSE; case WSAEAFNOSUPPORT: return NET_EAFNOSUPPORT; case WSAENETUNREACH: return NET_ENETUNREACH; @@ -112,6 +116,8 @@ int native_error() { #if EWOULDBLOCK != EAGAIN case EWOULDBLOCK: return NET_EAGAIN; #endif + case EINPROGRESS: return NET_EINPROGRESS; + case EALREADY: return NET_EALREADY; case EADDRINUSE: return NET_EADDRINUSE; case EAFNOSUPPORT: return NET_EAFNOSUPPORT; case ENETUNREACH: return NET_ENETUNREACH; @@ -480,8 +486,11 @@ int APS5_VABI sceNetConnect(int s, const void* addr, uint32_t addrlen) { NativeLength native_length = 0; if (!guest_to_native_address(addr, addrlen, native, native_length)) return fail(NET_EINVAL); if (native.ss_family != (socket.family == NET_AF_INET ? AF_INET : AF_INET6)) return fail(NET_EAFNOSUPPORT); - return ::connect(socket.native->value, reinterpret_cast(&native), native_length) == 0 - ? 0 : fail(native_error()); + if (::connect(socket.native->value, reinterpret_cast(&native), native_length) == 0) return 0; +#ifdef _WIN32 + if (WSAGetLastError() == WSAEWOULDBLOCK) return fail(NET_EINPROGRESS); +#endif + return fail(native_error()); } int64_t APS5_VABI sceNetRecv(int s, void* buf, size_t len, int flags) { diff --git a/core/libs/tests/GuestSceNet.cpp b/core/libs/tests/GuestSceNet.cpp index f20e1c315..b10536311 100644 --- a/core/libs/tests/GuestSceNet.cpp +++ b/core/libs/tests/GuestSceNet.cpp @@ -211,6 +211,12 @@ int main() { Require(sceNetSetsockopt(accepted, 0xffff, 0x1200, &nonblocking, sizeof(nonblocking)) == 0); char pending = 0; Require(sceNetRecv(accepted, &pending, sizeof(pending), 0) == static_cast(0x80410123) && *sceNetErrnoLoc() == 35); + const int connecting = sceNetSocket(nullptr, 2, 1, 6); + Require(connecting >= 0); + Require(sceNetSetsockopt(connecting, 0xffff, 0x1200, &nonblocking, sizeof(nonblocking)) == 0); + const int started = sceNetConnect(connecting, address.data(), address.size()); + Require(started == 0 || (started == static_cast(0x80410124) && *sceNetErrnoLoc() == 36)); + Require(sceNetSocketClose(connecting) == 0); const int epoll = sceNetEpollCreate("guest-sce-net", 0); Require(epoll >= 0); From d3900b240f1a9bafa5ff8ed0d65fb385b70d2032 Mon Sep 17 00:00:00 2001 From: Raj Date: Thu, 8 Oct 2026 12:23:36 +0530 Subject: [PATCH 2/2] fix(libSceNet): translate EHOSTUNREACH and the socket errors libkernel translates The host's EHOSTUNREACH was reported as ENETUNREACH (51); FreeBSD's EHOSTUNREACH is 65, which libkernel's sockets already report. EADDRNOTAVAIL, ENOBUFS, EISCONN, ENOTCONN and EACCES had no translation and became EIO; they now map to 49, 55, 56, 57 and 13, as in libkernel/Socket/src/Udp.cpp. guest_sce_net connects the connected client again and expects EISCONN (0x80410138); before this change it received EIO. --- core/libs/prx/libSceNet/Export.cpp | 20 ++++++++++++++++++-- core/libs/tests/GuestSceNet.cpp | 1 + 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/core/libs/prx/libSceNet/Export.cpp b/core/libs/prx/libSceNet/Export.cpp index b35d9c6ce..a5cce9f8b 100644 --- a/core/libs/prx/libSceNet/Export.cpp +++ b/core/libs/prx/libSceNet/Export.cpp @@ -38,6 +38,7 @@ namespace { // FreeBSD errno numbers as reported through sceNetErrnoLoc (SCE_NET_ERROR_* is 0x80410100 + errno). constexpr int NET_ENOENT = 2; constexpr int NET_EBADF = 9; +constexpr int NET_EACCES = 13; constexpr int NET_EFAULT = 14; constexpr int NET_EINVAL = 22; constexpr int NET_ENOSPC = 28; @@ -49,11 +50,16 @@ constexpr int NET_EOPNOTSUPP = 45; constexpr int NET_EPROTONOSUPPORT = 43; constexpr int NET_EAFNOSUPPORT = 47; constexpr int NET_EADDRINUSE = 48; +constexpr int NET_EADDRNOTAVAIL = 49; constexpr int NET_ENETUNREACH = 51; constexpr int NET_ECONNABORTED = 53; +constexpr int NET_ENOBUFS = 55; +constexpr int NET_EISCONN = 56; +constexpr int NET_ENOTCONN = 57; constexpr int NET_EMSGSIZE = 40; constexpr int NET_ETIMEDOUT = 60; constexpr int NET_ECONNREFUSED = 61; +constexpr int NET_EHOSTUNREACH = 65; constexpr int NET_ERROR_BASE = static_cast(0x80410100u); constexpr int NET_ERROR_RESOLVER_ENODNS = static_cast(0x804101E1u); @@ -89,10 +95,15 @@ int native_error() { case WSAEINPROGRESS: return NET_EINPROGRESS; case WSAEALREADY: return NET_EALREADY; case WSAEADDRINUSE: return NET_EADDRINUSE; + case WSAEADDRNOTAVAIL: return NET_EADDRNOTAVAIL; case WSAEAFNOSUPPORT: return NET_EAFNOSUPPORT; case WSAENETUNREACH: return NET_ENETUNREACH; - case WSAEHOSTUNREACH: return NET_ENETUNREACH; + case WSAEHOSTUNREACH: return NET_EHOSTUNREACH; case WSAECONNABORTED: return NET_ECONNABORTED; + case WSAENOBUFS: return NET_ENOBUFS; + case WSAEISCONN: return NET_EISCONN; + case WSAENOTCONN: return NET_ENOTCONN; + case WSAEACCES: return NET_EACCES; case WSAECONNRESET: return 54; case WSAETIMEDOUT: return NET_ETIMEDOUT; case WSAECONNREFUSED: return NET_ECONNREFUSED; @@ -119,10 +130,15 @@ int native_error() { case EINPROGRESS: return NET_EINPROGRESS; case EALREADY: return NET_EALREADY; case EADDRINUSE: return NET_EADDRINUSE; + case EADDRNOTAVAIL: return NET_EADDRNOTAVAIL; case EAFNOSUPPORT: return NET_EAFNOSUPPORT; case ENETUNREACH: return NET_ENETUNREACH; - case EHOSTUNREACH: return NET_ENETUNREACH; + case EHOSTUNREACH: return NET_EHOSTUNREACH; case ECONNABORTED: return NET_ECONNABORTED; + case ENOBUFS: return NET_ENOBUFS; + case EISCONN: return NET_EISCONN; + case ENOTCONN: return NET_ENOTCONN; + case EACCES: return NET_EACCES; case ECONNRESET: return 54; case ETIMEDOUT: return NET_ETIMEDOUT; case ECONNREFUSED: return NET_ECONNREFUSED; diff --git a/core/libs/tests/GuestSceNet.cpp b/core/libs/tests/GuestSceNet.cpp index b10536311..6a23a6f14 100644 --- a/core/libs/tests/GuestSceNet.cpp +++ b/core/libs/tests/GuestSceNet.cpp @@ -207,6 +207,7 @@ int main() { address_size = peer.size(); const int accepted = sceNetAccept(listener, peer.data(), &address_size); Require(accepted >= 0 && address_size == 16); + Require(sceNetConnect(client, address.data(), address.size()) == static_cast(0x80410138) && *sceNetErrnoLoc() == 56); const int nonblocking = 1; Require(sceNetSetsockopt(accepted, 0xffff, 0x1200, &nonblocking, sizeof(nonblocking)) == 0); char pending = 0;