Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/libs/prx/libSceNet/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ if(BUILD_TESTING)
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceNet>")
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)
Expand Down
52 changes: 32 additions & 20 deletions core/libs/prx/libSceNet/Export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<sockaddr*>(&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) {
Expand All @@ -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();
Expand All @@ -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<sockaddr*>(&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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<std::uint32_t>(capacity, socket.family == NET_AF_INET ? 16u : 28u) : 0u;
Expand Down Expand Up @@ -2074,7 +2088,6 @@ static int64_t ReceiveMessage(int s, NetMsghdr* msg, int flags) {
if (!destinations.emplace_back(entry.base, static_cast<std::size_t>(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;
Expand All @@ -2083,7 +2096,6 @@ static int64_t ReceiveMessage(int s, NetMsghdr* msg, int flags) {
*msg = header;
return 0;
}
#endif
std::vector<char> buffer;
try {
buffer.resize(static_cast<std::size_t>(total) + (datagram ? 1u : 0u));
Expand Down
Original file line number Diff line number Diff line change
@@ -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).
Loading