fix(libkernel): connect and send to INADDR_ANY through 127.0.0.1 on Windows as FreeBSD reaches the local host #15782

Merged
Ragbie merged 3 commits from upstream-pr/15782 into main 2026-11-07 23:41:18 +00:00
Ragbie commented 2026-11-04 14:41:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ragbie on 2026-11-04T14:41:53Z.


What

On Windows, connect, sendto and sendmsg to INADDR_ANY on an AF_INET socket, or to ::ffff:0.0.0.0 on a dual-stack AF_INET6 socket, now reach the local host through 127.0.0.1. Before, they went to the host unchanged and failed with EADDRNOTAVAIL, as the libkernel.md debt entry "connect to the unspecified address" recorded. #14876 did the AF_INET6 :: half of connect; this is the IPv4 half.

  • FreeBSD 11.4: in_pcbconnect_setup fails port 0 with EADDRNOTAVAIL, then replaces an INADDR_ANY destination with the first interface address (the jail's address in a jail). tcp_connect, udp_connect and udp_output all go through it; the v4-mapped form reaches it through tcp6_usr_connect, udp6_connect and udp6_output. A connect or unconnected send to 0.0.0.0 therefore reaches the local machine.
  • Which local address: which one the console's "first interface address" is was not measured. 127.0.0.1 is the address that always reaches the local host, matches ::1 for the IPv6 half, and is what Linux uses for an unbound socket. The debt entry keeps the remaining difference: the peer a guest reads back can differ from the console's.
  • Windows only: Linux and macOS keep their kernels' answer. Linux uses the socket's bound address or the loopback address; macOS uses BSD's first-interface rule.

The code (Udp.cpp):

  • LoopbackForUnspecifiedInet: rewrites an AF_INET INADDR_ANY, or an AF_INET6 ::ffff:0.0.0.0, to the loopback address.
  • connect: the mapping runs after the multicast check and ZeroPortConnect, as FreeBSD rejects multicast and port 0 first. RefuseClosedLoopbackAtOnce moved to the same point, so a port-0 refusal no longer leaves the socket's no-SYN-retransmission setting behind.
  • sendto: the mapping runs after ZeroPortSend, because udp_output's port-0 rule depends on whether the destination is INADDR_ANY. GuestUdp's 0.0.0.0:0 case still gets EADDRNOTAVAIL. sendmsg on an IP socket goes through sendto_nid_postfix.
  • Debt entry: narrowed to the IPv6 sends, which are unchanged, and the IPv4 address choice. It now also names sceNetSendto as unchanged.

Tested

guest_socket_port_zero, new CheckUnspecifiedInet (stream and datagram):

  • A server on 127.0.0.1. connect to 0.0.0.0 on the server's port returns 0, getpeername reports 127.0.0.1 on that port, and data flows.
  • For datagrams: a fresh socket's sendto to 0.0.0.0 reaches the server. A dual-stack AF_INET6 socket with IPV6_V6ONLY cleared reaches it with sendto and with connect to ::ffff:0.0.0.0.

Windows 11 Pro, Intel Core i5-14600KF, NVIDIA GPU, WinLibs GCC 15.2.0, full Release build with libs:

  • Without the fix: connect to INADDR_ANY must reach 127.0.0.1 on the same port (errno 49).
  • With a first version that mapped only AF_INET: sendto ::ffff:0.0.0.0 must reach 127.0.0.1 (errno 49).
  • With the fix: guest_socket_port_zero passes with --repeat until-fail:3. The 159 tests matching socket|net|udp|tcp|connect|sendto|poll|select|kqueue|epoll pass, except select_tests_MissingCommandTests, which fails with WinError 1314 because this user cannot create symbolic links.
  • Full ctest: 99% tests passed, 6 tests failed out of 4005. All six fail on main on this machine too:
    • test_registration and select_tests_MissingCommandTests fail with WinError 1314.
    • agc_driver_direct_write_completion, agc_driver_image_plane_address_on_line, agc_driver_image_sample_lod_clamp and agc_driver_image_sample_lod_clamp_shader_lane are GPU tests on an NVIDIA card.
  • check_skips.py reports one skip, guest_fopen_errors, which skips itself here because this user cannot create symbolic links.
  • After the full run, the two adjacent #ifdef _WIN32 blocks in sendto were joined. The socket tests above were run again on that state.

Linux (Ubuntu 24.04 container, GCC 13.3.0): the code is unchanged there, and the new test passes, since the host kernel reaches the loopback address. The 121 tests matching the same pattern pass.

macOS: not tested; the code is unchanged there. The macOS CI jobs do not run this test.

Overlaps: a trial merge with #14043 and #11223 (mine, both in Udp.cpp) has no conflicts (git merge-tree).

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 under Silent stubs in their library's technical debt file
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5156176037 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15782). Originally posted by [Ragbie](https://github.com/Ragbie) on 2026-11-04T14:41:53Z. --- ### What On Windows, `connect`, `sendto` and `sendmsg` to `INADDR_ANY` on an `AF_INET` socket, or to `::ffff:0.0.0.0` on a dual-stack `AF_INET6` socket, now reach the local host through `127.0.0.1`. Before, they went to the host unchanged and failed with `EADDRNOTAVAIL`, as the `libkernel.md` debt entry "connect to the unspecified address" recorded. #14876 did the `AF_INET6` `::` half of `connect`; this is the IPv4 half. - **FreeBSD 11.4:** [`in_pcbconnect_setup`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/netinet/in_pcb.c#L1040-L1060) fails port 0 with `EADDRNOTAVAIL`, then replaces an `INADDR_ANY` destination with the first interface address (the jail's address in a jail). `tcp_connect`, `udp_connect` and `udp_output` all go through it; the v4-mapped form reaches it through `tcp6_usr_connect`, `udp6_connect` and `udp6_output`. A `connect` or unconnected send to 0.0.0.0 therefore reaches the local machine. - **Which local address:** which one the console's "first interface address" is was not measured. `127.0.0.1` is the address that always reaches the local host, matches `::1` for the IPv6 half, and is what Linux uses for an unbound socket. The debt entry keeps the remaining difference: the peer a guest reads back can differ from the console's. - **Windows only:** Linux and macOS keep their kernels' answer. Linux uses the socket's bound address or the loopback address; macOS uses BSD's first-interface rule. The code (`Udp.cpp`): - **`LoopbackForUnspecifiedInet`:** rewrites an `AF_INET` `INADDR_ANY`, or an `AF_INET6` `::ffff:0.0.0.0`, to the loopback address. - **`connect`:** the mapping runs after the multicast check and `ZeroPortConnect`, as FreeBSD rejects multicast and port 0 first. `RefuseClosedLoopbackAtOnce` moved to the same point, so a port-0 refusal no longer leaves the socket's no-SYN-retransmission setting behind. - **`sendto`:** the mapping runs after `ZeroPortSend`, because `udp_output`'s port-0 rule depends on whether the destination is `INADDR_ANY`. `GuestUdp`'s 0.0.0.0:0 case still gets `EADDRNOTAVAIL`. `sendmsg` on an IP socket goes through `sendto_nid_postfix`. - **Debt entry:** narrowed to the IPv6 sends, which are unchanged, and the IPv4 address choice. It now also names `sceNetSendto` as unchanged. ### Tested `guest_socket_port_zero`, new `CheckUnspecifiedInet` (stream and datagram): - A server on `127.0.0.1`. `connect` to `0.0.0.0` on the server's port returns 0, `getpeername` reports `127.0.0.1` on that port, and data flows. - For datagrams: a fresh socket's `sendto` to `0.0.0.0` reaches the server. A dual-stack `AF_INET6` socket with `IPV6_V6ONLY` cleared reaches it with `sendto` and with `connect` to `::ffff:0.0.0.0`. Windows 11 Pro, Intel Core i5-14600KF, NVIDIA GPU, WinLibs GCC 15.2.0, full Release build with `libs`: - **Without the fix:** `connect to INADDR_ANY must reach 127.0.0.1 on the same port (errno 49)`. - **With a first version that mapped only `AF_INET`:** `sendto ::ffff:0.0.0.0 must reach 127.0.0.1 (errno 49)`. - **With the fix:** `guest_socket_port_zero` passes with `--repeat until-fail:3`. The 159 tests matching `socket|net|udp|tcp|connect|sendto|poll|select|kqueue|epoll` pass, except `select_tests_MissingCommandTests`, which fails with `WinError 1314` because this user cannot create symbolic links. - Full `ctest`: `99% tests passed, 6 tests failed out of 4005`. All six fail on `main` on this machine too: - `test_registration` and `select_tests_MissingCommandTests` fail with `WinError 1314`. - `agc_driver_direct_write_completion`, `agc_driver_image_plane_address_on_line`, `agc_driver_image_sample_lod_clamp` and `agc_driver_image_sample_lod_clamp_shader_lane` are GPU tests on an NVIDIA card. - `check_skips.py` reports one skip, `guest_fopen_errors`, which skips itself here because this user cannot create symbolic links. - After the full run, the two adjacent `#ifdef _WIN32` blocks in `sendto` were joined. The socket tests above were run again on that state. Linux (Ubuntu 24.04 container, GCC 13.3.0): the code is unchanged there, and the new test passes, since the host kernel reaches the loopback address. The 121 tests matching the same pattern pass. macOS: not tested; the code is unchanged there. The macOS CI jobs do not run this test. **Overlaps:** a trial merge with #14043 and #11223 (mine, both in `Udp.cpp`) has no conflicts (`git merge-tree`). ### 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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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: <!-- #PR, or none --> none - [x] Fixes: <!-- issue it closes as Fixes #N, or none --> none - [x] Unblocks: <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> none - [x] GPUs tested: <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> n/a - [x] AI-assisted: <!-- yes / no --> 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!15782
No description provided.