fix(libSceNet): dissolve a datagram association on connect to AF_UNSPEC #4669
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4669"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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 isAF_UNSPEC(a null address) withEINVALbefore 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():udp_disconnect()(sys/netinet/udp_usrreq.c) clears the peer and keeps the local port, andconnect(2)documents it: "Datagram sockets may dissolve the association by connecting to an invalid address, such as a null address." #4498 makes libkernel'sconnectdo 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_UNSPECon Linux, a zeroed address of the socket's family on Windows), and the call then fails withEINVALas 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
mainedddd6a1.guest_sce_netgainedCheckDatagramDisconnect: a UDP socket bound to127.0.0.1:0is connected to a peer, then connected to{16, AF_UNSPEC}; the call must fail withEINVAL, thensceNetGetpeernamemust fail withENOTCONN, the local port must be kept with the wildcard address (on Linux and macOS;udp_disconnect()resetsinp_laddrtoINADDR_ANY,udp6_disconnect()resetsin6p_laddrtoin6addr_any), and a datagram from an unrelated socket must arrive.main'sExport.cppthe test aborts (sceNetGetpeernamestill returns the old peer).guest_sce_netpasses, also in 30 repeated runs; allguest_*tests exceptguest_video_out_*pass (306/306).Windows: @johnmaia ran it on Windows 11 with WinLibs GCC 15.2.0, merged into
main3faa9cb40.guest_sce_netpasses, includingCheckDatagramDisconnect(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
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-12T19:25:50Z.
Code review location:
core/libs/prx/libSceNet/Export.cpp; line 699When Linux drops the port, this fallback rebinds every previously bound socket to
0.0.0.0/::, even ifgetsocknamebefore disconnect returned a specific local address (for example127.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 reproducedbind(127.0.0.1:0) → connect → AF_UNSPECreturning127.0.0.1:0; rebinding the saved port to wildcard changesgetsocknameto0.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?Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-12T19:29:49Z.
Code review location:
core/libs/prx/libSceNet/Export.cpp; line 699Thanks 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,and
udp6_disconnect()(sys/netinet6/udp6_usrreq.c) does the same withinp->in6p_laddr = in6addr_any;. So after a disconnect FreeBSD reports0.0.0.0:<port>(or[::]:<port>) even for a socket bound to127.0.0.1, which is what the rebind gives once Linux has dropped the port. Keeping the original address would leave127.0.0.1:0, matching neither.You're right that the test should pin it, so it now also checks that
getsocknamereports 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 reports127.0.0.1:<port>where FreeBSD reports0.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
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-12T19:31:08Z.
Code review location:
core/libs/prx/libSceNet/Export.cpp; line 699Thanks 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.
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-12T19:33:55Z.
Code review location:
core/libs/prx/libSceNet/Export.cpp; line 699Added: the explicit-port case is now an entry in
docs/dev/debt/libSceNet.md(a6de318bf), and the same one indocs/dev/debt/libkernel.mdfor #4498 (b8d71fc7f). Thanks for the review.AI-assisted: yes
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 intomain3faa9cb40.guest_sce_netpasses, includingCheckDatagramDisconnect. On Windows that covers everything except the zeroed local address bytes, which are#ifndef _WIN32.Not tested: a title, and a negative control on Windows.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T14:56:29Z.
Checked on Linux: scoped build and tests green,
CheckDatagramDisconnectfails onmain'sExport.cppand passes with the change; @johnmaia's Windows run covers the_WIN32branch.One fix added as our own commit on your branch: after the disconnect FreeBSD 11.4 fails
connectwithEAFNOSUPPORT, notEINVAL(in_pcbconnect_setup()insys/netinet/in_pcb.c:if (sin->sin_family != AF_INET) return (EAFNOSUPPORT);, reached oncesodisconnect()succeeded).sceNetConnectnow returnsNET_EAFNOSUPPORTthere and the test expects 47.Rebased onto current
main; merging once the full build is green.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 withEAFNOSUPPORT. I had kept the oldEINVALthere by mistake.AI-assisted: yes
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,
CheckDatagramDisconnectfails onmain'sExport.cppand passes with the change; @johnmaia's Windows run covers the_WIN32branch. TheEAFNOSUPPORTfix is already on the branch; merging once the full build is green.Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-14T02:59:10Z.
Rebased onto
main486420f91, keeping both commits and their authors. The only conflict was inGuestSceNet.cpp, where the newCheckSocketErrorfrom #4988 sits next toCheckDatagramDisconnect; both are kept. After the rebase: the build is clean,guest_sce_netpasses 5/5 and all 357guest_*tests pass (Ubuntu 24.04, GCC 13.3, Release). A MinGW-w64 GCC 13-fsyntax-onlycompile ofExport.cpphas no errors.AI-assisted: yes