Skip to content

fix(mdns): pin IPv6 multicast interface index on multi-adapter hosts#6473

Open
ohrensessel wants to merge 1 commit into
libp2p:masterfrom
ohrensessel:fix/mdns-ipv6-multicast-interface-index
Open

fix(mdns): pin IPv6 multicast interface index on multi-adapter hosts#6473
ohrensessel wants to merge 1 commit into
libp2p:masterfrom
ohrensessel:fix/mdns-ipv6-multicast-interface-index

Conversation

@ohrensessel
Copy link
Copy Markdown

Disclaimer

Yes, this is AI generated. I stumbled upon the problem with mDNS on Windows in a small project I was doing. Hope this is of any use.

Problem

On hosts with multiple network adapters — common on Windows with Hyper-V, WSL,
or VPN virtual interfaces — IPv6 mDNS discovery silently fails. Both the
multicast group join and the outbound multicast socket use interface index 0
(the OS default), which may resolve to a virtual adapter rather than the
physical LAN interface. Linux tolerates this because it typically has fewer
adapters and a more predictable default, but Windows does not.

The IPv4 path is unaffected because it passes the real interface address to
join_multicast_v4. The IPv6 path has two separate issues that the existing
TODO comments already acknowledge:

  1. join_multicast_v6 is called with index 0 instead of the index of the
    interface that owns the bound address.
  2. The send socket is built from a plain UdpSocket without setting
    IPV6_MULTICAST_IF, so outbound multicast packets exit the wrong adapter
    regardless of which interface was joined.

Fix

Resolve the interface index by matching the bound IPv6 address against the
host's network interfaces via if-addrs, then use that index for both:

  • the multicast group join (join_multicast_v6)
  • the outbound multicast interface (set_multicast_if_v6 on the send socket,
    built via socket2 which is already a dependency)

Falls back to index 0 when the address cannot be matched, preserving the
existing behaviour on single-interface hosts.

This closes both TODO comments left in the code for this exact issue.

Testing

Verified on a Windows host with Hyper-V and WSL virtual adapters where IPv6
mDNS previously produced no discoveries. After the fix, peers on the same
physical LAN segment are discovered correctly.

On hosts with multiple network adapters — common on Windows with
Hyper-V, WSL, or VPN virtual interfaces — IPv6 mDNS discovery silently
fails because both the multicast group join and the outbound multicast
socket use interface index 0 (the OS-default), which may resolve to a
virtual adapter rather than the physical LAN interface.

The IPv4 path is unaffected because it passes the real interface address
to join_multicast_v4. The IPv6 path has two separate issues:

1. join_multicast_v6 is called with index 0 instead of the index of the
   interface that owns the bound address.
2. The send socket is built from a plain UdpSocket without setting
   IPV6_MULTICAST_IF, so outbound multicast packets exit the wrong
   adapter regardless of which interface we joined on.

Fix: resolve the interface index by matching the bound IPv6 address
against the host interfaces via if-addrs, then use that index for both
the multicast group join and IPV6_MULTICAST_IF on the send socket.
Falls back to index 0 when the address cannot be matched, preserving
the existing behaviour on single-interface hosts.

Closes the two TODO comments left in the code for this exact issue.
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