fix: pass interface to multicast receive socket on Windows multi-NIC …#149
Open
koenvelle wants to merge 1 commit intohashicorp:mainfrom
Open
fix: pass interface to multicast receive socket on Windows multi-NIC …#149koenvelle wants to merge 1 commit intohashicorp:mainfrom
koenvelle wants to merge 1 commit intohashicorp:mainfrom
Conversation
…hosts On Windows with multiple network interfaces, newClient creates the multicast receive socket with net.ListenMulticastUDP(udp4, nil, ipv4Addr). The nil interface argument causes IP_ADD_MEMBERSHIP to be issued with INADDR_ANY, which lets Windows choose the multicast group membership interface based on the lowest route metric. On a multi-NIC machine this is often not the interface passed via params.Interface, so the receive socket never sees incoming packets even though queries are sent on the correct interface via setInterface/IP_MULTICAST_IF. The fix passes iface (from params.Interface) through newClient to both ListenMulticastUDP calls so that IP_ADD_MEMBERSHIP is issued for the correct interface from the start. When params.Interface is nil, iface is nil and behaviour is identical to before. Verified on Windows 11 with Ethernet + vEthernet (WSL Hyper-V) adapters: before the fix ReadFromUDP was never called during a 15-second query; after the fix the target board is discovered within 15 ms. Fixes hashicorp#80
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.
On Windows with multiple network interfaces,
newClientcreates the multicastreceive socket with
net.ListenMulticastUDP("udp4", nil, ipv4Addr). Thenilinterface argument causes
IP_ADD_MEMBERSHIPto be issued withINADDR_ANY,which lets Windows choose the multicast group membership interface based on the
lowest route metric. On a multi-NIC machine this is often not the interface
passed via
params.Interface, so the receive socket never sees incoming packetseven though queries are sent on the correct interface via
setInterface/IP_MULTICAST_IF.The fix passes
iface(fromparams.Interface) throughnewClientto bothListenMulticastUDPcalls so thatIP_ADD_MEMBERSHIPis issued for the correctinterface from the start. When
params.Interfaceisnil,ifaceisnilandbehaviour is identical to before.
Verified on Windows 11 with Ethernet + vEthernet (WSL Hyper-V) adapters: before
the fix
ReadFromUDPwas never called during a 15-second query; after the fixthe target board is discovered within 15 ms.
Fixes #80
Description
hashicorp/mdnsis used byarduino/mdns-discovery(Arduino IDE's pluggablenetwork port discovery tool). On Windows machines with more than one network
adapter — common with WSL, VirtualBox, VPN clients, etc. — mDNS discovery
silently returns no results even when the target device is actively responding
on the wire.
The root cause is that
newClientpassesnilas the interface tonet.ListenMulticastUDP. On Windows,nilresolves toIP_ADD_MEMBERSHIPwithINADDR_ANY, and the OS picks whichever interface has the lowest routing metric.This is often a virtual adapter (WSL, VPN), not the physical NIC where the mDNS
device lives. The send path was already correct (
setInterfacecallsIP_MULTICAST_IF), but receive group membership was never updated to match.This PR fixes the receive path by forwarding
params.InterfaceintonewClientand passing it to both
ListenMulticastUDPcalls.Related Issue
Fixes #80 ("No results (Windows)")
Also resolves the underlying cause of:
How Has This Been Tested?
Tested manually on Windows 11 with two active network interfaces:
Ethernet 2at10.0.0.17(route metric 281) — physical NIC, Arduino device presentvEthernet (WSL Hyper-V firewall)at172.17.112.1(route metric 5256) — virtual adapterBefore the fix:
ReadFromUDPwas never invoked during the full 15-secondquery window. The Arduino device at
10.0.0.38was confirmed responding on thewire (Wireshark showed its mDNS reply within 15 ms), but the socket never
received it because multicast membership was joined on the WSL adapter.
After the fix: The device is discovered and an
addevent is emitted within15 ms of the query starting. All other mDNS devices on the same network are also
discovered correctly.
Backward compatibility: when
params.Interfaceisnil(the default),ifaceisnilandnet.ListenMulticastUDPbehaves identically to before —no regression on single-NIC or Linux/macOS hosts.