diff --git a/core/libs/prx/libSceNet/CMakeLists.txt b/core/libs/prx/libSceNet/CMakeLists.txt index a61ce30b0b09..ddc39eef77a0 100644 --- a/core/libs/prx/libSceNet/CMakeLists.txt +++ b/core/libs/prx/libSceNet/CMakeLists.txt @@ -248,7 +248,7 @@ if(BUILD_TESTING) "PATH=path_list_prepend:$") endforeach() endif() - if(WIN32) + if(WIN32 OR CMAKE_SYSTEM_NAME STREQUAL "Linux") add_test_executable(net_empty_stream_tests ${LIBS_INCLUDE_DIR}/tests/NetEmptyStream.cpp) target_include_directories(net_empty_stream_tests PRIVATE ${LIBS_INCLUDE_DIR}) target_link_libraries(net_empty_stream_tests PRIVATE libSceNet libkernel libc) diff --git a/core/libs/prx/libSceNet/Export.cpp b/core/libs/prx/libSceNet/Export.cpp index c820455d083b..bc467d48f4f9 100644 --- a/core/libs/prx/libSceNet/Export.cpp +++ b/core/libs/prx/libSceNet/Export.cpp @@ -912,6 +912,37 @@ int native_message_flags(int flags) { return native; } +constexpr int RECEIVE_NOTHING = -1; + +#ifndef __APPLE__ +int empty_stream_receive(const Sock& socket) { + sockaddr_storage peer{}; + NativeLength length = sizeof(peer); + if (::getpeername(socket.native->value, reinterpret_cast(&peer), &length) != 0) { + const int error = native_error(); + const bool readShut = (socket.native->shutdownFlags.load() & KernelSocketPoll::ReceiveShut) != 0; + return !socket.connectAttempted && !socket.aborted && !readShut && error == NET_ENOTCONN ? error : 0; + } +#ifdef _WIN32 + WSAPOLLFD state{}; + state.fd = socket.native->value; + state.events = POLLRDNORM; + return WSAPoll(&state, 1, 0) >= 0 && (state.revents & (POLLERR | POLLHUP | POLLNVAL)) == 0 ? RECEIVE_NOTHING : 0; +#else + pollfd state{}; + state.fd = socket.native->value; + state.events = POLLIN; + return ::poll(&state, 1, 0) >= 0 && (state.revents & (POLLERR | POLLHUP | POLLNVAL)) == 0 ? RECEIVE_NOTHING : 0; +#endif +} +#endif + +#if !defined(_WIN32) && !defined(__APPLE__) +int wait_for_local_address(int, const Sock& socket, bool empty, int) { + return socket.type == NET_SOCK_STREAM && empty ? empty_stream_receive(socket) : 0; +} +#endif + #if defined(_WIN32) || defined(__APPLE__) #ifdef __APPLE__ bool has_local_address(NativeSocket native) { @@ -938,8 +969,6 @@ void note_local_address(int s, const Sock& socket) { g_cv.NotifyAll(); } -constexpr int RECEIVE_NOTHING = -1; - int wait_for_datagram(const Sock& socket, std::chrono::steady_clock::time_point deadline) { if (deadline == std::chrono::steady_clock::time_point::max()) return 0; const auto now = std::chrono::steady_clock::now(); @@ -961,18 +990,7 @@ int wait_for_datagram(const Sock& socket, std::chrono::steady_clock::time_point int wait_for_local_address(int s, const Sock& socket, bool empty, int flags) { if (socket.type == NET_SOCK_STREAM) { #ifdef _WIN32 - if (!empty) return 0; - sockaddr_storage peer{}; - NativeLength length = sizeof(peer); - if (::getpeername(socket.native->value, reinterpret_cast(&peer), &length) != 0) { - const int error = native_error(); - const bool readShut = (socket.native->shutdownFlags.load() & KernelSocketPoll::ReceiveShut) != 0; - return !socket.connectAttempted && !socket.aborted && !readShut && error == NET_ENOTCONN ? error : 0; - } - WSAPOLLFD state{}; - state.fd = socket.native->value; - state.events = POLLRDNORM; - return WSAPoll(&state, 1, 0) >= 0 && (state.revents & (POLLERR | POLLHUP | POLLNVAL)) == 0 ? RECEIVE_NOTHING : 0; + return empty ? empty_stream_receive(socket) : 0; #else return 0; #endif @@ -1834,9 +1852,7 @@ static int64_t ReceiveStream(int s, void* buf, size_t len, int flags) { if (it == g_socks.end()) return fail(NET_EBADF); socket = it->second; } -#if defined(_WIN32) || defined(__APPLE__) if (const int wait = wait_for_local_address(s, socket, len == 0, flags)) return wait == RECEIVE_NOTHING ? 0 : fail(wait); -#endif const GuestArena::HostWrite destination(len == 0 ? nullptr : buf, len); if (!destination.Open()) return fail(NET_EFAULT); const int native_flags = native_message_flags(flags); @@ -1874,14 +1890,12 @@ static int64_t ReceiveFrom(int s, void* buf, size_t len, int flags, void* from, std::uint32_t capacity = 0; if (from && (!GuestCopyIn_nid_no_patch(&capacity, fromlen, sizeof(capacity)) || !GuestCopyOut_nid_no_patch(fromlen, &capacity, sizeof(capacity)))) return fail(NET_EFAULT); -#if defined(_WIN32) || defined(__APPLE__) if (const int wait = wait_for_local_address(s, socket, len == 0, flags)) { if (wait != RECEIVE_NOTHING) return fail(wait); constexpr std::uint32_t empty = 0; if (from && !GuestCopyOut_nid_no_patch(fromlen, &empty, sizeof(empty))) return fail(NET_EFAULT); return 0; } -#endif const bool returnAddress = from != nullptr && socket.type == NET_SOCK_DGRAM; const auto addressBytes = returnAddress ? std::min(capacity, socket.family == NET_AF_INET ? 16u : 28u) : 0u; @@ -2074,7 +2088,6 @@ static int64_t ReceiveMessage(int s, NetMsghdr* msg, int flags) { if (!destinations.emplace_back(entry.base, static_cast(entry.length)).Open()) return fail(NET_EFAULT); } const bool datagram = socket.type != NET_SOCK_STREAM; -#if defined(_WIN32) || defined(__APPLE__) if (const int wait = wait_for_local_address(s, socket, total == 0, flags)) { if (wait != RECEIVE_NOTHING) return fail(wait); header.flags = 0; @@ -2083,7 +2096,6 @@ static int64_t ReceiveMessage(int s, NetMsghdr* msg, int flags) { *msg = header; return 0; } -#endif std::vector buffer; try { buffer.resize(static_cast(total) + (datagram ? 1u : 0u)); diff --git a/docs/dev/debt/changes/fix-libscenet-empty-stream-receive-linux.md b/docs/dev/debt/changes/fix-libscenet-empty-stream-receive-linux.md new file mode 100644 index 000000000000..3fad71be92e7 --- /dev/null +++ b/docs/dev/debt/changes/fix-libscenet-empty-stream-receive-linux.md @@ -0,0 +1,5 @@ +# libSceNet + +## change zero-length TCP receives while connecting + +- [sceNetRecv, sceNetRecvfrom and sceNetRecvmsg](../../../core/libs/prx/libSceNet/Export.cpp) (libSceNet) - on Windows and Linux, zero-length TCP receives while connecting, after disconnect, or with a pending host error still use the host receive path; on macOS every zero-length TCP receive does. Error and output precedence in those states is unmeasured on a console. The established, error-free case follows FreeBSD 11.4's [`soreceive_generic`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_socket.c#L1568-L1583).