Skip to content
Closed
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
4 changes: 4 additions & 0 deletions core/libs/prx/libkernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,10 @@ if(BUILD_TESTING)
set_tests_properties(guest_poll PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
endif()
add_test_executable(guest_connect_error_tests ${LIBS_INCLUDE_DIR}/tests/GuestConnectError.cpp)
target_include_directories(guest_connect_error_tests PRIVATE ${LIBS_INCLUDE_DIR})
configure_windows_unwind(guest_connect_error_tests)
add_test(NAME guest_connect_error COMMAND guest_connect_error_tests)
add_test_executable(guest_tcp_tests ${LIBS_INCLUDE_DIR}/tests/GuestTcp.cpp)
target_include_directories(guest_tcp_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_tcp_tests PRIVATE libkernel libc)
Expand Down
18 changes: 18 additions & 0 deletions core/libs/prx/libkernel/Socket/include/SocketError.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once
#ifdef _WIN32
#include <winsock2.h>
#else
#include <cerrno>
#endif
namespace GuestSockets {
inline int PendingConnectError(int nativeError) {
#ifdef _WIN32
if (nativeError == WSAEWOULDBLOCK) return 36;
if (nativeError == WSAEALREADY) return 37;
#else
if (nativeError == EINPROGRESS) return 36;
if (nativeError == EALREADY) return 37;
#endif
return -1;
}
}
13 changes: 12 additions & 1 deletion core/libs/prx/libkernel/Socket/src/Udp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#endif
#include "prx/libc/include/general/VabiMacros.hpp"
#include "prx/libkernel/Socket/include/SocketRuntime.hpp"
#include "prx/libkernel/Socket/include/SocketError.hpp"
#include <algorithm>
#include <chrono>
#include <climits>
Expand Down Expand Up @@ -91,6 +92,16 @@ int NativeError() {
}
#endif
}

int ConnectError() {
#ifdef _WIN32
const int nativeError = WSAGetLastError();
#else
const int nativeError = errno;
#endif
const int guestError = GuestSockets::PendingConnectError(nativeError);
return guestError == -1 ? NativeError() : guestError;
}
struct Socket {
NativeSocket value;
int family;
Expand Down Expand Up @@ -293,7 +304,7 @@ int APS5_VABI connect_nid_postfix(int descriptor, const void* address, std::uint
socklen_t size;
if (!Address(address, length, native, size)) return -1;
if (native.ss_family != (socket->family == 2 ? AF_INET : AF_INET6)) return Fail(47);
return ::connect(socket->value, reinterpret_cast<sockaddr*>(&native), size) ? Fail(NativeError()) : 0;
return ::connect(socket->value, reinterpret_cast<sockaddr*>(&native), size) ? Fail(ConnectError()) : 0;
}
int APS5_VABI listen_nid_postfix(int descriptor, int backlog) {
const auto socket = Lookup(descriptor);
Expand Down
18 changes: 18 additions & 0 deletions core/libs/tests/GuestConnectError.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "prx/libkernel/Socket/include/SocketError.hpp"
#include <cstdlib>

static void Require(bool condition) {
if (!condition) std::abort();
}

int main() {
#ifdef _WIN32
Require(GuestSockets::PendingConnectError(WSAEWOULDBLOCK) == 36);
Require(GuestSockets::PendingConnectError(WSAEALREADY) == 37);
Require(GuestSockets::PendingConnectError(WSAECONNREFUSED) == -1);
#else
Require(GuestSockets::PendingConnectError(EINPROGRESS) == 36);
Require(GuestSockets::PendingConnectError(EALREADY) == 37);
Require(GuestSockets::PendingConnectError(ECONNREFUSED) == -1);
#endif
}
37 changes: 35 additions & 2 deletions core/libs/tests/GuestTcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#include <array>
#include <cstdint>
#include <cstdlib>
#include <cstdio>
#include <cstring>
struct PollDescriptor { int descriptor; short events; short revents; };

extern "C" {
int APS5_VABI socket_nid_postfix(int, int, int);
Expand All @@ -11,16 +13,19 @@ int APS5_VABI listen_nid_postfix(int, int);
int APS5_VABI getsockname_nid_postfix(int, void*, std::uint32_t*);
int APS5_VABI connect_nid_postfix(int, const void*, std::uint32_t);
int APS5_VABI accept_nid_postfix(int, void*, std::uint32_t*);
int APS5_VABI fcntl_nid_postfix(int, int, ...);
int APS5_VABI poll_nid_postfix(PollDescriptor*, std::uint32_t, int);
std::int64_t APS5_VABI send_nid_postfix(int, const void*, std::uint64_t, int);
std::int64_t APS5_VABI recv_nid_postfix(int, void*, std::uint64_t, int);
int APS5_VABI getpeername_nid_postfix(int, void*, std::uint32_t*);
int APS5_VABI close_nid_postfix(int);
int* APS5_VABI __error_nid_postfix();
}

static void Require(bool condition) {
if (!condition) std::abort();
static void Check(bool condition, int line) {
if (!condition) { std::fprintf(stderr, "TCP check failed at line %d\n", line); std::abort(); }
}
#define Require(value) Check((value), __LINE__)

int main() {
const int listener = socket_nid_postfix(2, 1, 6);
Expand Down Expand Up @@ -54,6 +59,34 @@ int main() {
Require(recv_nid_postfix(accepted, received, sizeof(received), 0) == sizeof(received));
Require(std::strcmp(request, received) == 0);

const int pending_listener = socket_nid_postfix(2, 1, 6);
Require(pending_listener >= 0);
std::array<std::uint8_t, 16> pending_address{16, 2, 0, 0, 127, 0, 0, 1};
Require(bind_nid_postfix(pending_listener, pending_address.data(), pending_address.size()) == 0);
Require(listen_nid_postfix(pending_listener, 4) == 0);
address_size = pending_address.size();
Require(getsockname_nid_postfix(pending_listener, pending_address.data(), &address_size) == 0);
const int pending_client = socket_nid_postfix(2, 1, 0);
Require(pending_client >= 0 && fcntl_nid_postfix(pending_client, 4, 6) == 0);
const int pending_connect = connect_nid_postfix(pending_client, pending_address.data(), pending_address.size());
Require(pending_connect == 0 || (pending_connect == -1 && *__error_nid_postfix() == 36));
PollDescriptor pending_client_ready{pending_client, 4, 0};
Require(poll_nid_postfix(&pending_client_ready, 1, 1000) == 1);
Require((pending_client_ready.revents & 4) != 0);
PollDescriptor pending_listener_ready{pending_listener, 1, 0};
Require(poll_nid_postfix(&pending_listener_ready, 1, 1000) == 1);
Require((pending_listener_ready.revents & 1) != 0);
const int pending_accepted = accept_nid_postfix(pending_listener, nullptr, nullptr);
Require(pending_accepted >= 0);
const char pending_message[] = "pending connect";
char pending_received[sizeof(pending_message)]{};
Require(send_nid_postfix(pending_client, pending_message, sizeof(pending_message), 0) == sizeof(pending_message));
Require(recv_nid_postfix(pending_accepted, pending_received, sizeof(pending_received), 0) == sizeof(pending_received));
Require(std::strcmp(pending_message, pending_received) == 0);
Require(close_nid_postfix(pending_accepted) == 0);
Require(close_nid_postfix(pending_client) == 0);
Require(close_nid_postfix(pending_listener) == 0);

Require(close_nid_postfix(accepted) == 0);
#ifndef _WIN32
std::int64_t sent = 0;
Expand Down
Loading