fix(macos): select the address slot by insi_vflag, not soi_family - #57
Merged
GyulyVGC merged 1 commit intoJul 25, 2026
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 GyulyVGC#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.
Owner
|
I had to change the base because I wasn't allowed to make edits to your branch since it's owned by an org. |
Owner
|
I merged. @srid please let me know if you guys are planning for other fixes / enhancements or otherwise I'll soon publish version 0.6.1 so you can use the library with the fix right away |
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.
Fixes #56.
On macOS,
get_local_addrselects the address slot fromsoi_familyalone. That does not answer which slot of theinsi_laddrunion holds the address —insi_vflagdoes. For an IPv4-mapped bind, macOS reportssoi_family = AF_INET6while keeping the address in the 4-bytei46a_addr4slot; the 16-byteina_6slot then holds 12 zero pad bytes followed by the v4 address, which read as IPv6 is::127.0.0.1— the deprecated IPv4-compatible address, not the mapped one that was bound.Evidence
Each row measured by binding the address, then reading the kernel directly —
getsockname()plus the sameproc_pidfdinfo(PROC_PIDFDSOCKETINFO)view the crate uses. macOS 27.0 (build 26A5388g), reproduced with three independent binders (Node, Python 3, a raw Cbind()).soi_familyinsi_vflaggetsocknamei46a_addr4ina_6::ffff:127.0.0.10x01— INI_IPV4 only…ffff7f000001(mapped)7f0000010000000000000000000000007f000001::(dual-stack)0x03— both::00000000::::10x02— INI_IPV6 only::100000001— not an address::1127.0.0.10x017f0000017f000001Why this ordering
The table rules out the two simpler patches:
INI_IPV6is checked first, because a dual-stack socket sets both flags. CheckingINI_IPV4first would report a::bind as0.0.0.0and lose its IPv6 reach.INI_IPV4. For::1that slot holds00000001, which would surface as0.0.0.1.soi_familyremains the fallback when neither flag is set.Reported representation
A mapped bind now reports
::ffff:127.0.0.1, which matches what the Linux implementation already returns for the same bind (it reads the mapped form literally from/proc/net/tcp6), so the two platforms agree. Returning a bareIpAddr::V4would have been indistinguishable from anAF_INETsocket; callers wanting that view can useIpv6Addr::to_ipv4_mapped().Tests
Two tests in
tests/integration.rs:test_tcp_ipv4_mapped_address_is_not_ipv4_compatible— checked against the unfixed code: it 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 ordering, which the first test cannot.Full suite: macOS 15 passed / 1 failed, Linux 15 passed / 1 failed. Both failures are
test_consistency, and neither is introduced here:ssh95174 vs 94936, thensshd-session14448 vs 11527).platform/macos— and it still fails there, with a different symptom:get_process_by_portreturningErr("No listener found on port")for a port thatget_all()had just reported.Both look like #36, and the macOS run suggests a mechanism: on that host
lsofshowed several ports held by more than one process (*:5000,*:64673,*:7000), andget_process_by_port(port) == a_specific_listener.processis not well-defined when a port has multiple holders — while the Linux symptom is the same assertion losing a race against a socket closing. Not addressed in this PR.Not tested on Windows — no machine available.