fix(libSceNet): dissolve a datagram association on connect to AF_UNSPEC #4669

Merged
milescoviello merged 2 commits from upstream-pr/4669 into main 2026-10-14 04:25:40 +00:00
milescoviello commented 2026-10-12 18:24:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-12T18:24:24Z.


What

sceNetConnect() on a connected datagram socket rejected an address whose family is AF_UNSPEC (a null address) with EINVAL before touching the socket, so the socket stayed connected: sceNetGetpeername() still returned the old peer and datagrams from any other sender never arrived.

FreeBSD 11.4 disconnects a connected datagram socket first and only then looks at the address. sys/kern/uipc_socket.c, soconnectat():

	 * If protocol is connection-based, can only connect once.
	 * Otherwise, if connected, try to disconnect first.  This allows
	 * user to disconnect by connecting to, e.g., a null address.
	 */
	if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
	    ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
	    (error = sodisconnect(so)))) {

udp_disconnect() (sys/netinet/udp_usrreq.c) clears the peer and keeps the local port, and connect(2) documents it: "Datagram sockets may dissolve the association by connecting to an invalid address, such as a null address." #4498 makes libkernel's connect do this; this is the same change for libSceNet's sockets, which games use more often.

Now a connected datagram socket given a family byte of 0 is disconnected on the host first (AF_UNSPEC on Linux, a zeroed address of the socket's family on Windows), and the call then fails with EINVAL as before; I left that error alone. Linux also drops an ephemeral local port on that disconnect, so the port is bound again on the wildcard address, which is what FreeBSD leaves. A host failure during the disconnect is returned as the call's error. Stream sockets keep the previous path.

Tested

Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags, on main edddd6a1. guest_sce_net gained CheckDatagramDisconnect: a UDP socket bound to 127.0.0.1:0 is connected to a peer, then connected to {16, AF_UNSPEC}; the call must fail with EINVAL, then sceNetGetpeername must fail with ENOTCONN, the local port must be kept with the wildcard address (on Linux and macOS; udp_disconnect() resets inp_laddr to INADDR_ANY, udp6_disconnect() resets in6p_laddr to in6addr_any), and a datagram from an unrelated socket must arrive.

  • Red: with main's Export.cpp the test aborts (sceNetGetpeername still returns the old peer).
  • Green: guest_sce_net passes, also in 30 repeated runs; all guest_* tests except guest_video_out_* pass (306/306).

Windows: @johnmaia ran it on Windows 11 with WinLibs GCC 15.2.0, merged into main 3faa9cb40. guest_sce_net passes, including CheckDatagramDisconnect (the zeroed local address check is #ifndef _WIN32), and the full suite passes 1074 of 1074. Merges cleanly with #4498, #4370, #4371 and #4394, which touch the same files.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4833405340 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4669). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-12T18:24:24Z. --- ### What `sceNetConnect()` on a connected datagram socket rejected an address whose family is `AF_UNSPEC` (a null address) with `EINVAL` before touching the socket, so the socket stayed connected: `sceNetGetpeername()` still returned the old peer and datagrams from any other sender never arrived. FreeBSD 11.4 disconnects a connected datagram socket first and only then looks at the address. `sys/kern/uipc_socket.c`, `soconnectat()`: ```c * If protocol is connection-based, can only connect once. * Otherwise, if connected, try to disconnect first. This allows * user to disconnect by connecting to, e.g., a null address. */ if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) && ((so->so_proto->pr_flags & PR_CONNREQUIRED) || (error = sodisconnect(so)))) { ``` `udp_disconnect()` (`sys/netinet/udp_usrreq.c`) clears the peer and keeps the local port, and `connect(2)` documents it: "Datagram sockets may dissolve the association by connecting to an invalid address, such as a null address." #4498 makes libkernel's `connect` do this; this is the same change for libSceNet's sockets, which games use more often. Now a connected datagram socket given a family byte of 0 is disconnected on the host first (`AF_UNSPEC` on Linux, a zeroed address of the socket's family on Windows), and the call then fails with `EINVAL` as before; I left that error alone. Linux also drops an ephemeral local port on that disconnect, so the port is bound again on the wildcard address, which is what FreeBSD leaves. A host failure during the disconnect is returned as the call's error. Stream sockets keep the previous path. ### Tested Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags, on `main` edddd6a1. `guest_sce_net` gained `CheckDatagramDisconnect`: a UDP socket bound to `127.0.0.1:0` is connected to a peer, then connected to `{16, AF_UNSPEC}`; the call must fail with `EINVAL`, then `sceNetGetpeername` must fail with `ENOTCONN`, the local port must be kept with the wildcard address (on Linux and macOS; `udp_disconnect()` resets `inp_laddr` to `INADDR_ANY`, `udp6_disconnect()` resets `in6p_laddr` to `in6addr_any`), and a datagram from an unrelated socket must arrive. - Red: with `main`'s `Export.cpp` the test aborts (`sceNetGetpeername` still returns the old peer). - Green: `guest_sce_net` passes, also in 30 repeated runs; all `guest_*` tests except `guest_video_out_*` pass (306/306). Windows: @johnmaia [ran it](https://github.com/boykopovar/AnyPS5/pull/4669#issuecomment-6146813966) on Windows 11 with WinLibs GCC 15.2.0, merged into `main` 3faa9cb40. `guest_sce_net` passes, including `CheckDatagramDisconnect` (the zeroed local address check is `#ifndef _WIN32`), and the full suite passes 1074 of 1074. Merges cleanly with #4498, #4370, #4371 and #4394, which touch the same files. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-12 19:25:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-12T19:25:50Z.

Code review location: core/libs/prx/libSceNet/Export.cpp; line 699

@@ -671,6 +671,38 @@ int APS5_VABI sceNetAccept(int s, void* addr, uint32_t* addrlen) {
     return descriptor;
 }
 
+static std::uint16_t& native_port(sockaddr_storage& address) {
+    return address.ss_family == AF_INET ? reinterpret_cast<sockaddr_in&>(address).sin_port : reinterpret_cast<sockaddr_in6&>(address).sin6_port;
+}
+
+static int disconnect_datagram(const Sock& socket) {
+    const auto value = socket.native->value;
+    sockaddr_storage peer{};
+    NativeLength peer_length = sizeof(peer);
+    if (::getpeername(value, reinterpret_cast<sockaddr*>(&peer), &peer_length) != 0) return 0;
+    sockaddr_storage local{};
+    NativeLength local_length = sizeof(local);
+    if (::getsockname(value, reinterpret_cast<sockaddr*>(&local), &local_length) != 0) return fail(native_error());
+#ifdef _WIN32
+    sockaddr_storage unspecified{};
+    unspecified.ss_family = local.ss_family;
+    if (::connect(value, reinterpret_cast<const sockaddr*>(&unspecified), local_length) != 0) return fail(native_error());
+#else
+    sockaddr unspecified{};
+    unspecified.sa_family = AF_UNSPEC;
+    if (::connect(value, &unspecified, sizeof(unspecified)) != 0) return fail(native_error());
+#endif
+    sockaddr_storage current{};
+    NativeLength current_length = sizeof(current);
+    if (::getsockname(value, reinterpret_cast<sockaddr*>(&current), &current_length) != 0) return fail(native_error());
+    if (native_port(current) != 0) return 0;
+    sockaddr_storage wildcard{};

When Linux drops the port, this fallback rebinds every previously bound socket to 0.0.0.0/::, even if getsockname before disconnect returned a specific local address (for example 127.0.0.1). FreeBSD’s disconnect preserves the local binding; after this branch, a socket bound to loopback can receive packets addressed to other local interfaces and may send from a different source address. On Linux I reproduced bind(127.0.0.1:0) → connect → AF_UNSPEC returning 127.0.0.1:0; rebinding the saved port to wildcard changes getsockname to 0.0.0.0:<port>. Could the fallback preserve the original local address and IPv6 scope while restoring the port, and assert that address in the regression test?

<!-- greyfinch-sync:boykopovar/AnyPS5:review-comment:4251602131 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4669#discussion_r4251602131). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-12T19:25:50Z. **Code review location:** `core/libs/prx/libSceNet/Export.cpp`; line 699 ~~~~diff @@ -671,6 +671,38 @@ int APS5_VABI sceNetAccept(int s, void* addr, uint32_t* addrlen) { return descriptor; } +static std::uint16_t& native_port(sockaddr_storage& address) { + return address.ss_family == AF_INET ? reinterpret_cast<sockaddr_in&>(address).sin_port : reinterpret_cast<sockaddr_in6&>(address).sin6_port; +} + +static int disconnect_datagram(const Sock& socket) { + const auto value = socket.native->value; + sockaddr_storage peer{}; + NativeLength peer_length = sizeof(peer); + if (::getpeername(value, reinterpret_cast<sockaddr*>(&peer), &peer_length) != 0) return 0; + sockaddr_storage local{}; + NativeLength local_length = sizeof(local); + if (::getsockname(value, reinterpret_cast<sockaddr*>(&local), &local_length) != 0) return fail(native_error()); +#ifdef _WIN32 + sockaddr_storage unspecified{}; + unspecified.ss_family = local.ss_family; + if (::connect(value, reinterpret_cast<const sockaddr*>(&unspecified), local_length) != 0) return fail(native_error()); +#else + sockaddr unspecified{}; + unspecified.sa_family = AF_UNSPEC; + if (::connect(value, &unspecified, sizeof(unspecified)) != 0) return fail(native_error()); +#endif + sockaddr_storage current{}; + NativeLength current_length = sizeof(current); + if (::getsockname(value, reinterpret_cast<sockaddr*>(&current), &current_length) != 0) return fail(native_error()); + if (native_port(current) != 0) return 0; + sockaddr_storage wildcard{}; ~~~~ --- When Linux drops the port, this fallback rebinds every previously bound socket to `0.0.0.0`/`::`, even if `getsockname` before disconnect returned a specific local address (for example `127.0.0.1`). FreeBSD’s disconnect preserves the local binding; after this branch, a socket bound to loopback can receive packets addressed to other local interfaces and may send from a different source address. On Linux I reproduced `bind(127.0.0.1:0) → connect → AF_UNSPEC` returning `127.0.0.1:0`; rebinding the saved port to wildcard changes `getsockname` to `0.0.0.0:<port>`. Could the fallback preserve the original local address and IPv6 scope while restoring the port, and assert that address in the regression test?
milescoviello commented 2026-10-12 19:29:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-12T19:29:49Z.

Code review location: core/libs/prx/libSceNet/Export.cpp; line 699

@@ -671,6 +671,38 @@ int APS5_VABI sceNetAccept(int s, void* addr, uint32_t* addrlen) {
     return descriptor;
 }
 
+static std::uint16_t& native_port(sockaddr_storage& address) {
+    return address.ss_family == AF_INET ? reinterpret_cast<sockaddr_in&>(address).sin_port : reinterpret_cast<sockaddr_in6&>(address).sin6_port;
+}
+
+static int disconnect_datagram(const Sock& socket) {
+    const auto value = socket.native->value;
+    sockaddr_storage peer{};
+    NativeLength peer_length = sizeof(peer);
+    if (::getpeername(value, reinterpret_cast<sockaddr*>(&peer), &peer_length) != 0) return 0;
+    sockaddr_storage local{};
+    NativeLength local_length = sizeof(local);
+    if (::getsockname(value, reinterpret_cast<sockaddr*>(&local), &local_length) != 0) return fail(native_error());
+#ifdef _WIN32
+    sockaddr_storage unspecified{};
+    unspecified.ss_family = local.ss_family;
+    if (::connect(value, reinterpret_cast<const sockaddr*>(&unspecified), local_length) != 0) return fail(native_error());
+#else
+    sockaddr unspecified{};
+    unspecified.sa_family = AF_UNSPEC;
+    if (::connect(value, &unspecified, sizeof(unspecified)) != 0) return fail(native_error());
+#endif
+    sockaddr_storage current{};
+    NativeLength current_length = sizeof(current);
+    if (::getsockname(value, reinterpret_cast<sockaddr*>(&current), &current_length) != 0) return fail(native_error());
+    if (native_port(current) != 0) return 0;
+    sockaddr_storage wildcard{};

Thanks for running it. I checked the local address against FreeBSD 11.4 again: udp_disconnect() (sys/netinet/udp_usrreq.c) clears it right after the disconnect and keeps the port,

	in_pcbdisconnect(inp);
	inp->inp_laddr.s_addr = INADDR_ANY;

and udp6_disconnect() (sys/netinet6/udp6_usrreq.c) does the same with inp->in6p_laddr = in6addr_any;. So after a disconnect FreeBSD reports 0.0.0.0:<port> (or [::]:<port>) even for a socket bound to 127.0.0.1, which is what the rebind gives once Linux has dropped the port. Keeping the original address would leave 127.0.0.1:0, matching neither.

You're right that the test should pin it, so it now also checks that getsockname reports the wildcard address after the disconnect, on Linux and macOS (94f4d759d here, and the same in #4498, c11d4793f). One case still differs from FreeBSD: with an explicitly bound port Linux keeps both the address and the port, and a bound socket can't be rebound, so it reports 127.0.0.1:<port> where FreeBSD reports 0.0.0.0:<port>. I can record that in the libSceNet and libkernel debt files if you think it's worth it.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:review-comment:4251631193 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4669#discussion_r4251631193). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-12T19:29:49Z. **Code review location:** `core/libs/prx/libSceNet/Export.cpp`; line 699 ~~~~diff @@ -671,6 +671,38 @@ int APS5_VABI sceNetAccept(int s, void* addr, uint32_t* addrlen) { return descriptor; } +static std::uint16_t& native_port(sockaddr_storage& address) { + return address.ss_family == AF_INET ? reinterpret_cast<sockaddr_in&>(address).sin_port : reinterpret_cast<sockaddr_in6&>(address).sin6_port; +} + +static int disconnect_datagram(const Sock& socket) { + const auto value = socket.native->value; + sockaddr_storage peer{}; + NativeLength peer_length = sizeof(peer); + if (::getpeername(value, reinterpret_cast<sockaddr*>(&peer), &peer_length) != 0) return 0; + sockaddr_storage local{}; + NativeLength local_length = sizeof(local); + if (::getsockname(value, reinterpret_cast<sockaddr*>(&local), &local_length) != 0) return fail(native_error()); +#ifdef _WIN32 + sockaddr_storage unspecified{}; + unspecified.ss_family = local.ss_family; + if (::connect(value, reinterpret_cast<const sockaddr*>(&unspecified), local_length) != 0) return fail(native_error()); +#else + sockaddr unspecified{}; + unspecified.sa_family = AF_UNSPEC; + if (::connect(value, &unspecified, sizeof(unspecified)) != 0) return fail(native_error()); +#endif + sockaddr_storage current{}; + NativeLength current_length = sizeof(current); + if (::getsockname(value, reinterpret_cast<sockaddr*>(&current), &current_length) != 0) return fail(native_error()); + if (native_port(current) != 0) return 0; + sockaddr_storage wildcard{}; ~~~~ --- Thanks for running it. I checked the local address against FreeBSD 11.4 again: `udp_disconnect()` (`sys/netinet/udp_usrreq.c`) clears it right after the disconnect and keeps the port, ```c in_pcbdisconnect(inp); inp->inp_laddr.s_addr = INADDR_ANY; ``` and `udp6_disconnect()` (`sys/netinet6/udp6_usrreq.c`) does the same with `inp->in6p_laddr = in6addr_any;`. So after a disconnect FreeBSD reports `0.0.0.0:<port>` (or `[::]:<port>`) even for a socket bound to `127.0.0.1`, which is what the rebind gives once Linux has dropped the port. Keeping the original address would leave `127.0.0.1:0`, matching neither. You're right that the test should pin it, so it now also checks that `getsockname` reports the wildcard address after the disconnect, on Linux and macOS (94f4d759d here, and the same in #4498, c11d4793f). One case still differs from FreeBSD: with an explicitly bound port Linux keeps both the address and the port, and a bound socket can't be rebound, so it reports `127.0.0.1:<port>` where FreeBSD reports `0.0.0.0:<port>`. I can record that in the libSceNet and libkernel debt files if you think it's worth it. AI-assisted: yes
MarcZX10 commented 2026-10-12 19:31:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-12T19:31:08Z.

Code review location: core/libs/prx/libSceNet/Export.cpp; line 699

@@ -671,6 +671,38 @@ int APS5_VABI sceNetAccept(int s, void* addr, uint32_t* addrlen) {
     return descriptor;
 }
 
+static std::uint16_t& native_port(sockaddr_storage& address) {
+    return address.ss_family == AF_INET ? reinterpret_cast<sockaddr_in&>(address).sin_port : reinterpret_cast<sockaddr_in6&>(address).sin6_port;
+}
+
+static int disconnect_datagram(const Sock& socket) {
+    const auto value = socket.native->value;
+    sockaddr_storage peer{};
+    NativeLength peer_length = sizeof(peer);
+    if (::getpeername(value, reinterpret_cast<sockaddr*>(&peer), &peer_length) != 0) return 0;
+    sockaddr_storage local{};
+    NativeLength local_length = sizeof(local);
+    if (::getsockname(value, reinterpret_cast<sockaddr*>(&local), &local_length) != 0) return fail(native_error());
+#ifdef _WIN32
+    sockaddr_storage unspecified{};
+    unspecified.ss_family = local.ss_family;
+    if (::connect(value, reinterpret_cast<const sockaddr*>(&unspecified), local_length) != 0) return fail(native_error());
+#else
+    sockaddr unspecified{};
+    unspecified.sa_family = AF_UNSPEC;
+    if (::connect(value, &unspecified, sizeof(unspecified)) != 0) return fail(native_error());
+#endif
+    sockaddr_storage current{};
+    NativeLength current_length = sizeof(current);
+    if (::getsockname(value, reinterpret_cast<sockaddr*>(&current), &current_length) != 0) return fail(native_error());
+    if (native_port(current) != 0) return 0;
+    sockaddr_storage wildcard{};

Thanks for checking the FreeBSD source and adding the wildcard-address assertion. That corrects my original finding: for the port-zero case, wildcard is the expected post-disconnect address.

The explicit-port case you mention is still a real Linux/FreeBSD difference, but it sounds like this PR cannot close it with a rebind while retaining the descriptor and bound port. I would record that limitation in the libSceNet technical-debt entry rather than hold this PR for it; the tested port-zero path looks covered now.

<!-- greyfinch-sync:boykopovar/AnyPS5:review-comment:4251640513 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4669#discussion_r4251640513). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-12T19:31:08Z. **Code review location:** `core/libs/prx/libSceNet/Export.cpp`; line 699 ~~~~diff @@ -671,6 +671,38 @@ int APS5_VABI sceNetAccept(int s, void* addr, uint32_t* addrlen) { return descriptor; } +static std::uint16_t& native_port(sockaddr_storage& address) { + return address.ss_family == AF_INET ? reinterpret_cast<sockaddr_in&>(address).sin_port : reinterpret_cast<sockaddr_in6&>(address).sin6_port; +} + +static int disconnect_datagram(const Sock& socket) { + const auto value = socket.native->value; + sockaddr_storage peer{}; + NativeLength peer_length = sizeof(peer); + if (::getpeername(value, reinterpret_cast<sockaddr*>(&peer), &peer_length) != 0) return 0; + sockaddr_storage local{}; + NativeLength local_length = sizeof(local); + if (::getsockname(value, reinterpret_cast<sockaddr*>(&local), &local_length) != 0) return fail(native_error()); +#ifdef _WIN32 + sockaddr_storage unspecified{}; + unspecified.ss_family = local.ss_family; + if (::connect(value, reinterpret_cast<const sockaddr*>(&unspecified), local_length) != 0) return fail(native_error()); +#else + sockaddr unspecified{}; + unspecified.sa_family = AF_UNSPEC; + if (::connect(value, &unspecified, sizeof(unspecified)) != 0) return fail(native_error()); +#endif + sockaddr_storage current{}; + NativeLength current_length = sizeof(current); + if (::getsockname(value, reinterpret_cast<sockaddr*>(&current), &current_length) != 0) return fail(native_error()); + if (native_port(current) != 0) return 0; + sockaddr_storage wildcard{}; ~~~~ --- Thanks for checking the FreeBSD source and adding the wildcard-address assertion. That corrects my original finding: for the port-zero case, wildcard is the expected post-disconnect address. The explicit-port case you mention is still a real Linux/FreeBSD difference, but it sounds like this PR cannot close it with a rebind while retaining the descriptor and bound port. I would record that limitation in the libSceNet technical-debt entry rather than hold this PR for it; the tested port-zero path looks covered now.
milescoviello commented 2026-10-12 19:33:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-12T19:33:55Z.

Code review location: core/libs/prx/libSceNet/Export.cpp; line 699

@@ -671,6 +671,38 @@ int APS5_VABI sceNetAccept(int s, void* addr, uint32_t* addrlen) {
     return descriptor;
 }
 
+static std::uint16_t& native_port(sockaddr_storage& address) {
+    return address.ss_family == AF_INET ? reinterpret_cast<sockaddr_in&>(address).sin_port : reinterpret_cast<sockaddr_in6&>(address).sin6_port;
+}
+
+static int disconnect_datagram(const Sock& socket) {
+    const auto value = socket.native->value;
+    sockaddr_storage peer{};
+    NativeLength peer_length = sizeof(peer);
+    if (::getpeername(value, reinterpret_cast<sockaddr*>(&peer), &peer_length) != 0) return 0;
+    sockaddr_storage local{};
+    NativeLength local_length = sizeof(local);
+    if (::getsockname(value, reinterpret_cast<sockaddr*>(&local), &local_length) != 0) return fail(native_error());
+#ifdef _WIN32
+    sockaddr_storage unspecified{};
+    unspecified.ss_family = local.ss_family;
+    if (::connect(value, reinterpret_cast<const sockaddr*>(&unspecified), local_length) != 0) return fail(native_error());
+#else
+    sockaddr unspecified{};
+    unspecified.sa_family = AF_UNSPEC;
+    if (::connect(value, &unspecified, sizeof(unspecified)) != 0) return fail(native_error());
+#endif
+    sockaddr_storage current{};
+    NativeLength current_length = sizeof(current);
+    if (::getsockname(value, reinterpret_cast<sockaddr*>(&current), &current_length) != 0) return fail(native_error());
+    if (native_port(current) != 0) return 0;
+    sockaddr_storage wildcard{};

Added: the explicit-port case is now an entry in docs/dev/debt/libSceNet.md (a6de318bf), and the same one in docs/dev/debt/libkernel.md for #4498 (b8d71fc7f). Thanks for the review.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:review-comment:4251661097 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4669#discussion_r4251661097). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-12T19:33:55Z. **Code review location:** `core/libs/prx/libSceNet/Export.cpp`; line 699 ~~~~diff @@ -671,6 +671,38 @@ int APS5_VABI sceNetAccept(int s, void* addr, uint32_t* addrlen) { return descriptor; } +static std::uint16_t& native_port(sockaddr_storage& address) { + return address.ss_family == AF_INET ? reinterpret_cast<sockaddr_in&>(address).sin_port : reinterpret_cast<sockaddr_in6&>(address).sin6_port; +} + +static int disconnect_datagram(const Sock& socket) { + const auto value = socket.native->value; + sockaddr_storage peer{}; + NativeLength peer_length = sizeof(peer); + if (::getpeername(value, reinterpret_cast<sockaddr*>(&peer), &peer_length) != 0) return 0; + sockaddr_storage local{}; + NativeLength local_length = sizeof(local); + if (::getsockname(value, reinterpret_cast<sockaddr*>(&local), &local_length) != 0) return fail(native_error()); +#ifdef _WIN32 + sockaddr_storage unspecified{}; + unspecified.ss_family = local.ss_family; + if (::connect(value, reinterpret_cast<const sockaddr*>(&unspecified), local_length) != 0) return fail(native_error()); +#else + sockaddr unspecified{}; + unspecified.sa_family = AF_UNSPEC; + if (::connect(value, &unspecified, sizeof(unspecified)) != 0) return fail(native_error()); +#endif + sockaddr_storage current{}; + NativeLength current_length = sizeof(current); + if (::getsockname(value, reinterpret_cast<sockaddr*>(&current), &current_length) != 0) return fail(native_error()); + if (native_port(current) != 0) return 0; + sockaddr_storage wildcard{}; ~~~~ --- Added: the explicit-port case is now an entry in `docs/dev/debt/libSceNet.md` (a6de318bf), and the same one in `docs/dev/debt/libkernel.md` for #4498 (b8d71fc7f). Thanks for the review. AI-assisted: yes
johnmaia commented 2026-10-13 13:36:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by johnmaia on 2026-10-13T13:36:54Z.


Windows run, since the description says Windows is untested. Setup: Windows 11 (26300), WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON, ctest -j4 --timeout 300, RTX 4080. Head a6de318bf merged into main 3faa9cb40.

  • guest_sce_net passes, including CheckDatagramDisconnect. On Windows that covers everything except the zeroed local address bytes, which are #ifndef _WIN32.
  • The full suite passes 1074 of 1074.

Not tested: a title, and a negative control on Windows.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6146813966 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4669#issuecomment-6146813966). Originally posted by [johnmaia](https://github.com/johnmaia) on 2026-10-13T13:36:54Z. --- Windows run, since the description says Windows is untested. Setup: Windows 11 (26300), WinLibs GCC 15.2.0 r7, Release, `BUILD_TESTING=ON`, `ctest -j4 --timeout 300`, RTX 4080. Head a6de318bf merged into `main` 3faa9cb40. - `guest_sce_net` passes, including `CheckDatagramDisconnect`. On Windows that covers everything except the zeroed local address bytes, which are `#ifndef _WIN32`. - The full suite passes 1074 of 1074. Not tested: a title, and a negative control on Windows. AI-assisted: yes (Claude Code)
adriadam10 commented 2026-10-13 14:56:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T14:56:29Z.


Checked on Linux: scoped build and tests green, CheckDatagramDisconnect fails on main's Export.cpp and passes with the change; @johnmaia's Windows run covers the _WIN32 branch.

One fix added as our own commit on your branch: after the disconnect FreeBSD 11.4 fails connect with EAFNOSUPPORT, not EINVAL (in_pcbconnect_setup() in sys/netinet/in_pcb.c: if (sin->sin_family != AF_INET) return (EAFNOSUPPORT);, reached once sodisconnect() succeeded). sceNetConnect now returns NET_EAFNOSUPPORT there and the test expects 47.

Rebased onto current main; merging once the full build is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6148432900 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4669#issuecomment-6148432900). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-13T14:56:29Z. --- Checked on Linux: scoped build and tests green, `CheckDatagramDisconnect` fails on `main`'s `Export.cpp` and passes with the change; @johnmaia's Windows run covers the `_WIN32` branch. One fix added as our own commit on your branch: after the disconnect FreeBSD 11.4 fails `connect` with `EAFNOSUPPORT`, not `EINVAL` (`in_pcbconnect_setup()` in `sys/netinet/in_pcb.c`: `if (sin->sin_family != AF_INET) return (EAFNOSUPPORT);`, reached once `sodisconnect()` succeeded). `sceNetConnect` now returns `NET_EAFNOSUPPORT` there and the test expects 47. Rebased onto current `main`; merging once the full build is green.
milescoviello commented 2026-10-13 14:58:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-13T14:58:24Z.


Thanks, @adriadam10. You're right: once sodisconnect() succeeds, in_pcbconnect_setup() (in_pcb.c:1031-1032 on releng/11.4) rejects the family with EAFNOSUPPORT. I had kept the old EINVAL there by mistake.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6148471225 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4669#issuecomment-6148471225). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-13T14:58:24Z. --- Thanks, @adriadam10. You're right: once `sodisconnect()` succeeds, `in_pcbconnect_setup()` (in_pcb.c:1031-1032 on releng/11.4) rejects the family with `EAFNOSUPPORT`. I had kept the old `EINVAL` there by mistake. AI-assisted: yes
adriadam10 commented 2026-10-13 15:34:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T15:34:27Z.


Checked on Linux: scoped build and tests green on the current head, CheckDatagramDisconnect fails on main's Export.cpp and passes with the change; @johnmaia's Windows run covers the _WIN32 branch. The EAFNOSUPPORT fix is already on the branch; merging once the full build is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6149194932 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4669#issuecomment-6149194932). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-13T15:34:27Z. --- Checked on Linux: scoped build and tests green on the current head, `CheckDatagramDisconnect` fails on `main`'s `Export.cpp` and passes with the change; @johnmaia's Windows run covers the `_WIN32` branch. The `EAFNOSUPPORT` fix is already on the branch; merging once the full build is green.
milescoviello commented 2026-10-14 02:59:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-14T02:59:10Z.


Rebased onto main 486420f91, keeping both commits and their authors. The only conflict was in GuestSceNet.cpp, where the new CheckSocketError from #4988 sits next to CheckDatagramDisconnect; both are kept. After the rebase: the build is clean, guest_sce_net passes 5/5 and all 357 guest_* tests pass (Ubuntu 24.04, GCC 13.3, Release). A MinGW-w64 GCC 13 -fsyntax-only compile of Export.cpp has no errors.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159885573 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4669#issuecomment-6159885573). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-14T02:59:10Z. --- Rebased onto `main` 486420f91, keeping both commits and their authors. The only conflict was in `GuestSceNet.cpp`, where the new `CheckSocketError` from #4988 sits next to `CheckDatagramDisconnect`; both are kept. After the rebase: the build is clean, `guest_sce_net` passes 5/5 and all 357 `guest_*` tests pass (Ubuntu 24.04, GCC 13.3, Release). A MinGW-w64 GCC 13 `-fsyntax-only` compile of `Export.cpp` has no errors. AI-assisted: yes
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!4669
No description provided.