libkernel: connect() to port 0 on an unconnected UDP socket succeeds where FreeBSD returns EADDRNOTAVAIL; fix branch ready #6880

Closed
opened 2026-10-16 15:45:29 +00:00 by husseinkad · 0 comments
husseinkad commented 2026-10-16 15:45:29 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-16T15:45:29Z.


connect() to a destination with port 0 succeeds on an unconnected UDP socket and leaves a peer on port 0, so later sends go to a port that nothing can listen on. FreeBSD 11.4 in_pcbconnect_setup (and in6_pcbconnect for IPv6) fails with EADDRNOTAVAIL (49) when the destination port is 0, and the socket stays unconnected. TCP already returns 49 here.

Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/kernel-socket-connect-port-zero (two commits on a recent main; it merges cleanly into current main: libkernel/Socket/src/Udp.cpp with a new UnsetPort() helper, the new test tests/GuestSocketPortZero.cpp registered in the libkernel CMakeLists.txt, and a debt entry in docs/dev/debt/libkernel.md). connect now returns EADDRNOTAVAIL for a port-0 destination on an unconnected datagram socket, for IPv4 and IPv6. The first version also changed sendto/sendmsg; a review found it wrongly changed TCP results, so that part is gone and TCP behaves as on main.

Checks:

  • The new test fails on main (abort: "connect to port 0 must fail with EADDRNOTAVAIL (49)") and passes with the change; it covers UDP over IPv4 and IPv6, and the TCP cases that must keep main's results (unconnected connect to port 0 gives 49, a connected socket gives EISCONN (56), and a connected sendto to port 0 returns the byte count). The 27-test socket group passes.
  • About 4,490 matrix cases plus 5,000 fuzz cases per variant against main, IPv4 and IPv6, UDP and TCP: no TCP result changed from main; connect to port 0 on an unconnected, bound or disconnected UDP socket now returns 49 and leaves the socket unconnected (main returned 0 and left a peer on port 0); bind to port 0 is unchanged; the 0.0.0.0 and :: port-0 cases still give 49 as on main.
  • Three independent reviews. The first two found the TCP regressions in the first version, now removed; the last found the narrowed head clean. check_conventions.py is clean and pr_overlap.py found no overlap; it also merges with the sendto fix filed as #5374, and the two do not interact.

Not fixed here (listed in the debt entry): connect to port 0 on an already connected UDP socket still returns 0 and keeps the peer (FreeBSD disconnects, fails with 49, and getpeername then gives 57); sendto to port 0 on an unconnected UDP socket still succeeds (FreeBSD returns 49, but it can succeed from a socket bound to a specific local address, so I left it); an address longer than 255 bytes with port 0 would return 49 instead of ENAMETOOLONG if #4920 lands. Only Windows was built and run.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5887018348 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/6880). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-16T15:45:29Z. --- `connect()` to a destination with port 0 succeeds on an unconnected UDP socket and leaves a peer on port 0, so later sends go to a port that nothing can listen on. FreeBSD 11.4 `in_pcbconnect_setup` (and `in6_pcbconnect` for IPv6) fails with `EADDRNOTAVAIL` (49) when the destination port is 0, and the socket stays unconnected. TCP already returns 49 here. Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/kernel-socket-connect-port-zero (two commits on a recent main; it merges cleanly into current main: `libkernel/Socket/src/Udp.cpp` with a new `UnsetPort()` helper, the new test `tests/GuestSocketPortZero.cpp` registered in the libkernel `CMakeLists.txt`, and a debt entry in `docs/dev/debt/libkernel.md`). `connect` now returns `EADDRNOTAVAIL` for a port-0 destination on an unconnected datagram socket, for IPv4 and IPv6. The first version also changed `sendto`/`sendmsg`; a review found it wrongly changed TCP results, so that part is gone and TCP behaves as on main. Checks: - The new test fails on main (abort: "connect to port 0 must fail with EADDRNOTAVAIL (49)") and passes with the change; it covers UDP over IPv4 and IPv6, and the TCP cases that must keep main's results (unconnected `connect` to port 0 gives 49, a connected socket gives `EISCONN` (56), and a connected `sendto` to port 0 returns the byte count). The 27-test socket group passes. - About 4,490 matrix cases plus 5,000 fuzz cases per variant against main, IPv4 and IPv6, UDP and TCP: no TCP result changed from main; `connect` to port 0 on an unconnected, bound or disconnected UDP socket now returns 49 and leaves the socket unconnected (main returned 0 and left a peer on port 0); `bind` to port 0 is unchanged; the `0.0.0.0` and `::` port-0 cases still give 49 as on main. - Three independent reviews. The first two found the TCP regressions in the first version, now removed; the last found the narrowed head clean. `check_conventions.py` is clean and `pr_overlap.py` found no overlap; it also merges with the `sendto` fix filed as #5374, and the two do not interact. Not fixed here (listed in the debt entry): `connect` to port 0 on an already connected UDP socket still returns 0 and keeps the peer (FreeBSD disconnects, fails with 49, and `getpeername` then gives 57); `sendto` to port 0 on an unconnected UDP socket still succeeds (FreeBSD returns 49, but it can succeed from a socket bound to a specific local address, so I left it); an address longer than 255 bytes with port 0 would return 49 instead of `ENAMETOOLONG` if #4920 lands. Only Windows was built and run. AI-assisted: yes
Sign in to join this conversation.
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#6880
No description provided.