libSceNet: sceNetListen on an unbound TCP socket fails with EINVAL on Windows; FreeBSD binds the wildcard address and succeeds; fix branch ready #5339

Closed
opened 2026-10-13 17:28:18 +00:00 by husseinkad · 1 comment
husseinkad commented 2026-10-13 17:28:18 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-13T17:28:18Z.


sceNetListen on a TCP socket the guest never bound fails with EINVAL on Windows, because Winsock requires a bound socket before listen. FreeBSD 11.4 tcp_usr_listen calls in_pcbbind when the socket has no local port (inp_lport == 0), which binds the wildcard address with an ephemeral port, so the call succeeds. A server that calls listen first and then reads the port with getsockname (a common way to get a free port) works on the console and fails here.

Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/net-listen-autobind (two commits on a recent main: the change and a correction from review; libSceNet/Export.cpp and tests/GuestSceNet.cpp with CheckListenWithoutBind for IPv4 and IPv6 and CheckListenAfterRefusedConnect). sceNetListen now binds the wildcard address of the socket's family first, unless the socket was already bound or already has a local port. The first version bound unconditionally and broke a case that worked on main: after a refused TCP connect, Winsock keeps an automatic local port, the extra bind failed with WSAEINVAL, and listen returned EINVAL where main (and FreeBSD) return 0.

Checks:

  • The new tests fail on main (abort), and CheckListenAfterRefusedConnect fails on the first version of the branch; both pass on the final head, 6 of 6 runs; guest_sce_net and the datagram tests pass.
  • Probed through the guest exports against main, IPv4 and IPv6: a fresh TCP socket now listens (main EINVAL), 50 concurrent listeners each got a distinct nonzero ephemeral port, and connect and accept to the auto-bound port work. Matches FreeBSD: a second listen returns 0, listen on a UDP socket returns EOPNOTSUPP, listen after a failed bind (EADDRNOTAVAIL or EADDRINUSE) returns 0, and backlogs 0, -1, 1, 128, INT_MAX and INT_MIN all return 0. listen on a connected or accepted socket returns EINVAL (main returned 0 or EBADF).
  • Two independent reviews found nothing wrong in the final version. check_conventions.py is clean; pr_overlap.py finds only test-file overlap with #4669 and #4988 (same insertion points; their Export.cpp hunks are in sceNetConnect and sceNetGetsockopt).

Not covered: the Linux behaviour was read, not run (the host listen already auto-binds there). Only Windows was built and run.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5834769693 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5339). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-13T17:28:18Z. --- `sceNetListen` on a TCP socket the guest never bound fails with `EINVAL` on Windows, because Winsock requires a bound socket before `listen`. FreeBSD 11.4 `tcp_usr_listen` calls `in_pcbbind` when the socket has no local port (`inp_lport == 0`), which binds the wildcard address with an ephemeral port, so the call succeeds. A server that calls `listen` first and then reads the port with `getsockname` (a common way to get a free port) works on the console and fails here. Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/net-listen-autobind (two commits on a recent main: the change and a correction from review; `libSceNet/Export.cpp` and `tests/GuestSceNet.cpp` with `CheckListenWithoutBind` for IPv4 and IPv6 and `CheckListenAfterRefusedConnect`). `sceNetListen` now binds the wildcard address of the socket's family first, unless the socket was already bound or already has a local port. The first version bound unconditionally and broke a case that worked on main: after a refused TCP connect, Winsock keeps an automatic local port, the extra bind failed with `WSAEINVAL`, and `listen` returned `EINVAL` where main (and FreeBSD) return 0. Checks: - The new tests fail on main (abort), and `CheckListenAfterRefusedConnect` fails on the first version of the branch; both pass on the final head, 6 of 6 runs; `guest_sce_net` and the datagram tests pass. - Probed through the guest exports against main, IPv4 and IPv6: a fresh TCP socket now listens (main `EINVAL`), 50 concurrent listeners each got a distinct nonzero ephemeral port, and `connect` and `accept` to the auto-bound port work. Matches FreeBSD: a second `listen` returns 0, `listen` on a UDP socket returns `EOPNOTSUPP`, `listen` after a failed bind (`EADDRNOTAVAIL` or `EADDRINUSE`) returns 0, and backlogs 0, -1, 1, 128, `INT_MAX` and `INT_MIN` all return 0. `listen` on a connected or accepted socket returns `EINVAL` (main returned 0 or `EBADF`). - Two independent reviews found nothing wrong in the final version. `check_conventions.py` is clean; `pr_overlap.py` finds only test-file overlap with #4669 and #4988 (same insertion points; their `Export.cpp` hunks are in `sceNetConnect` and `sceNetGetsockopt`). Not covered: the Linux behaviour was read, not run (the host `listen` already auto-binds there). Only Windows was built and run. AI-assisted: yes
oneandonlydean commented 2026-10-14 06:54:52 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T06:54:52Z.


Closing: no title is known to reach this, and functions are only changed when a title needs them. If a title stops on or misbehaves because of it on current main, comment here with the title ID and the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6162719111 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5339#issuecomment-6162719111). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T06:54:52Z. --- Closing: no title is known to reach this, and functions are only changed when a title needs them. If a title stops on or misbehaves because of it on current main, comment here with the title ID and the log line.
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#5339
No description provided.