libkernel: getsockname on an unbound socket fails with EINVAL on Windows (FreeBSD returns the wildcard address); fix branch ready #3943

Closed
opened 2026-10-11 19:30:29 +00:00 by husseinkad · 1 comment
husseinkad commented 2026-10-11 19:30:29 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-11T19:30:29Z.


On Windows, getsockname on a socket that has not been bound fails with EINVAL (Winsock WSAEINVAL). FreeBSD 11.4 succeeds: in_getsockaddr and in6_getsockaddr return the wildcard address with port 0 (length 16 and family 2 for AF_INET, length 28 and family 28 for AF_INET6). Linux already does this, so a guest that calls getsockname before bind or connect (for example to read an ephemeral port later) works on Linux and fails on Windows.

Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/kernel-socket-getsockname-unbound (one commit on current main, Socket/src/Udp.cpp and tests/GuestTcp.cpp). When Winsock reports WSAEINVAL the call now returns the zero address of the socket's family.

Checks:

  • The new guest_tcp case covers TCP and UDP, IPv4 and IPv6; it fails on main (assertion abort) and passes with the change; guest_(tcp|udp|poll|socket|dup) pass.
  • Probed through the guest exports on loopback against main: unbound sockets now return the zero address with the right length and family and port 0; short buffers get a truncated copy with the full length reported, as in every other bound case; bound, listening, connected and shut-down sockets and a UDP socket after connect are unchanged; getpeername on an unconnected socket still gives ENOTCONN (57).
  • Two independent reviews found nothing wrong. check_conventions.py is clean and pr_overlap.py finds no overlap.

Caveats: the new test needs an IPv6 socket to be creatable, so it would fail on a runner with IPv6 disabled (#2820 describes that setup). Only Windows was built and run; on Linux the code is not reached.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5804415961 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3943). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-11T19:30:29Z. --- On Windows, `getsockname` on a socket that has not been bound fails with `EINVAL` (Winsock `WSAEINVAL`). FreeBSD 11.4 succeeds: `in_getsockaddr` and `in6_getsockaddr` return the wildcard address with port 0 (length 16 and family 2 for AF_INET, length 28 and family 28 for AF_INET6). Linux already does this, so a guest that calls `getsockname` before `bind` or `connect` (for example to read an ephemeral port later) works on Linux and fails on Windows. Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/kernel-socket-getsockname-unbound (one commit on current main, `Socket/src/Udp.cpp` and `tests/GuestTcp.cpp`). When Winsock reports `WSAEINVAL` the call now returns the zero address of the socket's family. Checks: - The new `guest_tcp` case covers TCP and UDP, IPv4 and IPv6; it fails on main (assertion abort) and passes with the change; `guest_(tcp|udp|poll|socket|dup)` pass. - Probed through the guest exports on loopback against main: unbound sockets now return the zero address with the right length and family and port 0; short buffers get a truncated copy with the full length reported, as in every other bound case; bound, listening, connected and shut-down sockets and a UDP socket after `connect` are unchanged; `getpeername` on an unconnected socket still gives `ENOTCONN` (57). - Two independent reviews found nothing wrong. `check_conventions.py` is clean and `pr_overlap.py` finds no overlap. Caveats: the new test needs an IPv6 socket to be creatable, so it would fail on a runner with IPv6 disabled (#2820 describes that setup). Only Windows was built and run; on Linux the code is not reached. AI-assisted: yes
oneandonlydean commented 2026-10-13 07:37:50 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-13T07:37:50Z.


Closing: fixed by #3947. Reopen if getsockname on an unbound socket still fails on current main.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6140707210 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3943#issuecomment-6140707210). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-13T07:37:50Z. --- Closing: fixed by #3947. Reopen if `getsockname` on an unbound socket still fails on current main.
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#3943
No description provided.