fix(macos): select the address slot by insi_vflag, not soi_family - #58
Merged
Conversation
An IPv4-mapped bind was reported as a different address. `get_local_addr` branched on `soi_family` alone, so any AF_INET6 socket had its 16-byte `insi_laddr.ina_6` slot read — but macOS keeps a v4 address in the 4-byte `i46a_addr4` slot and leaves `ina_6` holding 12 zero pad bytes followed by that address. Read as IPv6 those bytes are the deprecated IPv4-COMPATIBLE address, not the IPv4-MAPPED one that was bound. `soi_family` does not answer "which slot"; `insi_vflag` does, and the struct already carried it. ## Kernel evidence (macOS 27.0, build 26A5388g) Bind, then ask the kernel directly — `getsockname` plus the same `proc_pidfdinfo` view the crate reads. Every row measured, three independent binders (node, python3, a raw C `bind()`) agreeing: | bind | soi_family | insi_vflag | getsockname | v4 slot | v6 slot | |---|---|---|---|---|---| | `::ffff:127.0.0.1` | AF_INET6 | `0x01` (v4 only) | `…ffff7f000001` MAPPED | `7f000001` | `0000…00007f000001` | | `::` dual-stack | AF_INET6 | `0x03` (BOTH) | `::` | `00000000` | `::` | | `::1` | AF_INET6 | `0x02` (v6 only) | `::1` | `00000001` ← garbage | `::1` | | `127.0.0.1` | AF_INET | `0x01` | `7f000001` | `7f000001` | — | Before: `::ffff:127.0.0.1` → `::127.0.0.1`. `lsof` and a libproc C reader both said `127.0.0.1` for the same socket. ## Why the flag order is v6-first `::1` is why this cannot simply prefer the v4 slot: its v4 slot holds `00000001`, which would surface as `0.0.0.1`. And a DUAL-STACK `::` sets BOTH flags, so v6 must win there or the wildcard would be reported as `0.0.0.0` and the socket's IPv6 reach lost. Hence `INI_IPV6` first, `INI_IPV4` second, family as the last resort. ## Why the mapped form rather than a bare V4 A mapped bind now reports `::ffff:127.0.0.1`, matching what the Linux implementation already returns for the same bind (it reads the mapped form literally out of `/proc/net/tcp6`). A bare `IpAddr::V4` would be indistinguishable from an AF_INET socket; callers wanting that view have `Ipv6Addr::to_ipv4_mapped()`. ## Tests Two regression tests, and they were checked against the UNFIXED code rather than assumed: `test_tcp_ipv4_mapped_address_is_not_ipv4_compatible` FAILS before this change and passes after. `test_tcp_dual_stack_wildcard_reports_ipv6_unspecified` passes either way by design — it exists to pin the flag ORDER, which the first test cannot. Full suite on macOS: 15 passed, 1 failed — `test_consistency`, which fails IDENTICALLY with and without this change (different pids each run: `ssh` 95174 vs 94936, then `sshd-session` 14448 vs 11527). It looks like the flake tracked upstream as #36, and the cause is visible here: `lsof` shows several ports on this box held by MORE THAN ONE process (`*:5000`, `*:64673`, `*:7000` — fork-inherited or SO_REUSEPORT listeners), so asserting `get_process_by_port(port) == a_specific_listener.process` is unsound when a port has multiple holders. Not addressed here — out of scope for an address-decoding fix, and noted only because it is adjacent.
fix(macos): select the address slot by insi_vflag, not soi_family
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Continuation of #57