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
5 changes: 3 additions & 2 deletions core/libs/prx/libSceNet/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ if(BUILD_TESTING)
target_link_libraries(guest_sce_net_tests PRIVATE libSceNet libSceNetCtl libkernel libc)
configure_windows_unwind(guest_sce_net_tests)
add_test(NAME guest_sce_net COMMAND guest_sce_net_tests)
set_tests_properties(guest_sce_net PROPERTIES TIMEOUT 20 SKIP_RETURN_CODE 77)
add_test(NAME guest_sce_net_peer_transports COMMAND guest_sce_net_tests --peer-only)
set_tests_properties(guest_sce_net guest_sce_net_peer_transports PROPERTIES TIMEOUT 20 SKIP_RETURN_CODE 77)
if(WIN32)
target_link_libraries(guest_sce_net_tests PRIVATE ws2_32)
set_tests_properties(guest_sce_net PROPERTIES ENVIRONMENT_MODIFICATION
set_tests_properties(guest_sce_net guest_sce_net_peer_transports PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceNet>")
endif()
endif()
16 changes: 11 additions & 5 deletions core/libs/prx/libSceNet/Export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ constexpr int NET_AF_INET6 = 28;
constexpr int NET_SOCK_STREAM = 1;
constexpr int NET_SOCK_DGRAM = 2;
constexpr int NET_SOCK_RAW = 3;
constexpr int NET_SOCK_DGRAM_P2P = 6;
constexpr int NET_SOCK_STREAM_P2P = 10;
constexpr int NET_EPOLL_CTL_ADD = 1;
constexpr int NET_EPOLL_CTL_MOD = 2;
constexpr int NET_EPOLL_CTL_DEL = 3;
Expand All @@ -87,6 +89,7 @@ constexpr int NET_SO_ERROR = 0x1007;
constexpr int NET_SO_NBIO = 0x1200;
constexpr int NET_SO_REUSEADDR = 0x4;
constexpr int NET_SO_BROADCAST = 0x20;
constexpr int NET_SO_BROADCAST_PS5 = 0x20000;
constexpr int NET_SO_SNDBUF = 0x1001;
constexpr int NET_SO_RCVBUF = 0x1002;
constexpr int NET_SO_SCE_SNDTIMEO = 0x1105;
Expand Down Expand Up @@ -629,11 +632,13 @@ int APS5_VABI sceNetSocket(const char* name, int family, int type, int protocol)
if (family != NET_AF_INET && family != NET_AF_INET6) {
return fail(NET_EAFNOSUPPORT);
}
if (type != NET_SOCK_STREAM && type != NET_SOCK_DGRAM && type != NET_SOCK_RAW) {
const int transportType = type == NET_SOCK_DGRAM_P2P ? NET_SOCK_DGRAM :
type == NET_SOCK_STREAM_P2P ? NET_SOCK_STREAM : type;
if (transportType != NET_SOCK_STREAM && transportType != NET_SOCK_DGRAM && transportType != NET_SOCK_RAW) {
return fail(NET_EPROTONOSUPPORT);
}
if (!initialize_sockets()) return fail(5);
const NativeSocket native = ::socket(family == NET_AF_INET ? AF_INET : AF_INET6, type, protocol);
const NativeSocket native = ::socket(family == NET_AF_INET ? AF_INET : AF_INET6, transportType, protocol);
if (native == INVALID_NATIVE_SOCKET) return fail(native_error());
const int ipv6_only = 1;
if (family == NET_AF_INET6 &&
Expand All @@ -654,7 +659,7 @@ int APS5_VABI sceNetSocket(const char* name, int family, int type, int protocol)
std::lock_guard lk(g_mutex);
Sock s;
s.family = family;
s.type = type;
s.type = transportType;
s.native = std::move(handle);
g_socks[fd] = s;
return fd;
Expand Down Expand Up @@ -1013,6 +1018,7 @@ int APS5_VABI sceNetSetsockopt(int s, int level, int optname, const void* optval
if (!optval || optlen < sizeof(int)) return fail(NET_EINVAL);
int value = 0;
std::memcpy(&value, optval, sizeof(value));
const bool broadcast = optname == NET_SO_BROADCAST || optname == NET_SO_BROADCAST_PS5;
if (optname == NET_SO_NBIO) {
#ifdef _WIN32
u_long enabled = value != 0;
Expand All @@ -1025,8 +1031,8 @@ int APS5_VABI sceNetSetsockopt(int s, int level, int optname, const void* optval
} else if (optname == NET_SO_RCVTIMEO || optname == NET_SO_SNDTIMEO) {
if (value < 0) return fail(NET_EINVAL);
if (set_native_timeout(socket.native->value, optname, value) != 0) return fail(native_error());
} else if (optname == NET_SO_REUSEADDR || optname == NET_SO_BROADCAST || optname == NET_SO_SNDBUF || optname == NET_SO_RCVBUF) {
const int native_option = optname == NET_SO_REUSEADDR ? SO_REUSEADDR : optname == NET_SO_BROADCAST ? SO_BROADCAST : optname == NET_SO_SNDBUF ? SO_SNDBUF : SO_RCVBUF;
} else if (optname == NET_SO_REUSEADDR || broadcast || optname == NET_SO_SNDBUF || optname == NET_SO_RCVBUF) {
const int native_option = optname == NET_SO_REUSEADDR ? SO_REUSEADDR : broadcast ? SO_BROADCAST : optname == NET_SO_SNDBUF ? SO_SNDBUF : SO_RCVBUF;
if (::setsockopt(socket.native->value, SOL_SOCKET, native_option, reinterpret_cast<const char*>(&value), sizeof(value)) != 0)
return fail(native_error());
} else {
Expand Down
39 changes: 38 additions & 1 deletion core/libs/tests/GuestSceNet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,42 @@ static void CheckDatagramDisconnect() {
for (const int descriptor : {peer, other, dissolved}) Require(sceNetSocketClose(descriptor) == 0);
}

int main() {
static void CheckPeerSocketTypes() {
std::array<std::uint8_t, 16> address{16, 2, 0, 0, 127, 0, 0, 1};
const int receiver = sceNetSocket("peer-udp-receiver", 2, 6, 0);
const int sender = sceNetSocket("peer-udp-sender", 2, 6, 0);
Require(receiver >= 0 && sender >= 0);
const int enabled = 1;
Require(sceNetSetsockopt(receiver, 0xffff, 0x20, &enabled, sizeof(enabled)) == 0);
Require(sceNetSetsockopt(receiver, 0xffff, 0x20000, &enabled, sizeof(enabled)) == 0);
Require(sceNetBind_nid_postfix(receiver, address.data(), address.size()) == 0);
std::uint32_t size = address.size();
Require(sceNetGetsockname(receiver, address.data(), &size) == 0);
const char payload[] = "peer socket transport";
char received[sizeof(payload)]{};
Require(sceNetSendto(sender, payload, sizeof(payload), 0, address.data(), size) == sizeof(payload));
Require(sceNetRecvfrom(receiver, received, sizeof(received), 0, nullptr, nullptr) == sizeof(received));
Require(std::strcmp(payload, received) == 0);
Require(sceNetSocketClose(sender) == 0 && sceNetSocketClose(receiver) == 0);

const int listener = sceNetSocket("peer-stream-listener", 2, 10, 0);
const int client = sceNetSocket("peer-stream-client", 2, 10, 0);
Require(listener >= 0 && client >= 0);
address = {16, 2, 0, 0, 127, 0, 0, 1};
Require(sceNetBind_nid_postfix(listener, address.data(), address.size()) == 0);
Require(sceNetListen(listener, 1) == 0);
size = address.size();
Require(sceNetGetsockname(listener, address.data(), &size) == 0);
Require(sceNetConnect(client, address.data(), size) == 0);
const int accepted = sceNetAccept(listener, nullptr, nullptr);
Require(accepted >= 0);
Require(sceNetSend(client, payload, sizeof(payload), 0) == sizeof(payload));
Require(sceNetRecv(accepted, received, sizeof(received), 0) == sizeof(received));
Require(std::strcmp(payload, received) == 0);
Require(sceNetSocketClose(accepted) == 0 && sceNetSocketClose(client) == 0 && sceNetSocketClose(listener) == 0);
}

int main(int argc, char** argv) {
const auto epoch = std::chrono::steady_clock::time_point{};
Require(KernelSocketSelect::DeadlineAfter(epoch, 2, 250000) == epoch + std::chrono::milliseconds(2250));
Require(KernelSocketSelect::DeadlineAfter(epoch, INT64_MAX, 0) == std::chrono::steady_clock::time_point::max());
Expand All @@ -818,6 +853,8 @@ int main() {
Require(in6addr_loopback_nid_postfix[i] == (i == 15 ? 1 : 0));
}
Require(sceNetInit_nid_postfix() == 0);
CheckPeerSocketTypes();
if (argc == 2 && std::strcmp(argv[1], "--peer-only") == 0) return 0;
CheckEpollWritable();
CheckPoolStats();
CheckNetstat();
Expand Down
1 change: 1 addition & 0 deletions docs/dev/debt/libSceNet.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

### Functional

- [sceNetSocket and sceNetSetsockopt](../../../core/libs/prx/libSceNet/Export.cpp) map peer socket types 6 and 10 to host UDP and TCP transports and accept the PS5 broadcast option `0x20000` as `SO_BROADCAST`. Loopback transport is tested; console peer negotiation and peer virtual-port routing are not implemented.
- [sceNetConnect to AF_UNSPEC](../../../core/libs/prx/libSceNet/Export.cpp) (libSceNet) dissolves a datagram association as FreeBSD 11.4's `udp_disconnect()` does, which resets the local address to the wildcard address and keeps the port. On Linux a socket bound to an explicit port keeps its local address as well (`127.0.0.1:<port>` stays `127.0.0.1:<port>`), because a bound socket cannot be bound again; one bound to port 0 is bound again to the wildcard address with the port it had
- [sceNetSetsockopt](../../../core/libs/prx/libSceNet/Export.cpp) (libSceNet) sets `SO_RCVTIMEO` and `SO_SNDTIMEO` as the host socket's timeouts. On Windows a receive or send on a stream socket that times out fails with `ETIMEDOUT`, where FreeBSD returns `EAGAIN` or the partial count: Winsock leaves the connection in an indeterminate state after a timeout and reports no partial count ([SOL_SOCKET options](https://learn.microsoft.com/en-us/windows/win32/winsock/sol-socket-socket-options)), so FreeBSD's behaviour needs the timeout waited for without the host option. A receive on a datagram socket that times out fails with `EAGAIN`
- [sceNetGetsockopt](../../../core/libs/prx/libSceNet/Export.cpp) (libSceNet) reads `SO_ERROR` from the host socket. Winsock does not clear a socket's pending error when it is read, so on Windows the library reports a pending error once and shows it again only after a new `sceNetConnect`; a second failure with the same code on the same socket without a `sceNetConnect` in between would be hidden.
Expand Down
Loading