Skip to content

fix(macos): select the address slot by insi_vflag, not soi_family - #58

Merged
GyulyVGC merged 6 commits into
mainfrom
mapped-fix
Jul 25, 2026
Merged

fix(macos): select the address slot by insi_vflag, not soi_family#58
GyulyVGC merged 6 commits into
mainfrom
mapped-fix

Conversation

@GyulyVGC

Copy link
Copy Markdown
Owner

Continuation of #57

srid and others added 3 commits July 25, 2026 11:11
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
@GyulyVGC GyulyVGC self-assigned this Jul 25, 2026
@GyulyVGC GyulyVGC added bug Something isn't working macOS labels Jul 25, 2026

@GyulyVGC GyulyVGC left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

@GyulyVGC
GyulyVGC merged commit e870aed into main Jul 25, 2026
12 checks passed
@GyulyVGC
GyulyVGC deleted the mapped-fix branch July 25, 2026 20:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working macOS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants