Skip to content

Linux Winsock: an unbound socket never gets all interfaces - #1

Open
x3ero0 wants to merge 1 commit into
spidercraft:linux-native-runtimefrom
x3ero0:linux-unbound-sockets
Open

x3ero0 wants to merge 1 commit into
spidercraft:linux-native-runtimefrom
x3ero0:linux-unbound-sockets

Conversation

@x3ero0

@x3ero0 x3ero0 commented Nov 24, 2026

Copy link
Copy Markdown

A fix for the native Linux runtime in Marvisak#526, on top of d46727f.

Problem

linux/ws2_32.rs keeps the title off the network by narrowing bind to loopback and refusing connects and sends to anything else. Two paths got around it, because Linux gives a socket without a local address one on all interfaces the first time it is used:

  • listen on an unbound socket listens on 0.0.0.0 and a port the kernel chooses, so other machines can connect in.
  • A first sendto from an unbound datagram socket binds it to 0.0.0.0, so other machines can send datagrams to it.

Both are easy to see outside VIctory:

$ python3 -c "import socket as s; a=s.socket(); a.listen(1); print(a.getsockname())"
('0.0.0.0', 32945)

Change

  • listen fails with WSAEINVAL unless the socket has a loopback address. Winsock returns the same error for an unbound socket, and bind only ever gives loopback addresses.
  • sendto binds an unbound datagram socket to loopback before the first send. An IPv6 socket sending to 127.0.0.1 gets the mapped address, so dual-stack sends keep working.
  • The module comment states the rule.

One file, about 100 lines including the test.

Testing

  • New test an_unbound_socket_never_gets_all_interfaces covers the unbound listen, the first datagram, a second datagram keeping the address, and the IPv6 case.
  • cargo test --workspace: 417 passed, 0 failed (Linux x86-64).
  • cargo xtask policy passes.
  • Not run with a title. The title's listener binds before it listens, so I expect no change there.

🤖 Generated with Claude Code

Linux gives a socket without a local address one on all interfaces the
first time it is used. Two calls reached that:

* listen on an unbound socket listened on 0.0.0.0 and a port the kernel
  chose, so the network could connect in. It now fails with WSAEINVAL
  unless the socket has a loopback address, which is what Winsock does
  for an unbound socket; bind only ever gives loopback addresses.
* A first sendto from an unbound datagram socket bound it to 0.0.0.0, so
  the network could send datagrams to it. It is now bound to loopback
  first (the mapped address for an IPv6 socket sending to 127.0.0.1).

Tested: new an_unbound_socket_never_gets_all_interfaces; cargo test
--workspace 417 passed; cargo xtask policy. Not run with a title.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
blockgameentity added a commit to blockgameentity/zafer that referenced this pull request Nov 25, 2026
The PR was 131 commits behind main; its conflicts are resolved here:
- calling convention: the PR's `extern "win64"` everywhere wins, since
  every function title code calls is win64 on both platforms (docs/linux.md).
- dxgi: keeps main's `crate::device::is_device` (every device interface
  level), which the PR's narrower `[device::IID]` broke for Device1..8.
- crash.rs: main's richer detail (memory_at, instruction_bytes, the C++
  throw) is fed into the PR's shared `crash_log::Report` format.
- thunks.rs: main's crash-log write for a trapped import, plus the PR's
  Linux ThunkBlock; both test modules kept.
- lower.rs: main's `nonuniform_handles`/`wave_ops` and the PR's
  `annotations` test fields coexist; both test bodies kept.
- files.rs/storage.rs/xuser.rs/lifecycle.rs: PR's Linux doc text and win64
  signatures, with main's test bodies which need Windows-only cfgs.

Three follow-up fixes from the PR's review comments are applied on top:
- ws2_32: an unbound socket never gets all interfaces (x3ero0's review,
  spidercraft/VIctory#1).
- GetProcAddress reads the exports of title DLLs we mapped, cmpxchg16b
  fixed registers, deterministic heap test (onpeek, #2).
- AMD-only Zen 2 instructions (clzero, SSE4a extrq/insertq) emulated in
  the SIGILL handler (toothpaste011, #3).

Also adds a Nix flake, a dev shell (default and windows-cross), a
shell.nix shim and .envrc, and allowlists .nix/.envrc in the asset policy.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant