I have a Windows 11 machine that has two network interfaces:
192.168.1.16/24
(Wi-Fi: physical Ethernet connected to external router)172.22.112.1/20
(vEthernet (Default Switch): internal virtual switch from Hyper-V)
I am writing an application that should send multicast messages via both interfaces.
Ideally I would like to send the data only once from the application and the data would end up on both interfaces without configuring Windows :) :
s = socket(AF_INET, SOCK_DGRAM, ...)
bind(s, INADDR_ANY,...)
sendto(s, buf, ..., 239.255.0.1, ...)
The messages arrives unfortunately only on the Wi-Fi interface. This interface is determined by the routing table, I guess.
PS>netstat -rn
Active Routes:
Network Destination Netmask Gateway Interface Metric
0.0.0.0 0.0.0.0 192.168.1.1 192.168.1.16 50
...
224.0.0.0 240.0.0.0 On-link 172.22.112.1 5256
224.0.0.0 240.0.0.0 On-link 192.168.1.16 306
...
In this case it goes only to interface 192.168.1.16 because the metric is the minimum of the matching route. This also happens if both interfaces have joined the multicast group.
PS> netsh int ip show joins
Interface 1: Wi-Fi
Scope References Last Address
---------- ---------- ---- ---------------------------------
...
0 1 Yes 239.255.0.1
0 3 Yes 239.255.255.250
...
Interface 2: vEthernet (Default Switch)
Scope References Last Address
---------- ---------- ---- ---------------------------------
...
0 1 Yes 239.255.0.1
0 4 Yes 239.255.255.250
One solution I found, is to send the packet twice like so:
setsockopt (socket, IPPROTO_IP, IP_MULTICAST_IF, 192.168.1.16, ...)
sendto(s, buf, ..., 239.255.0.1, ...)
setsockopt (socket, IPPROTO_IP, IP_MULTICAST_IF, 172.22.112.1, ...)
sendto(s, buf, ..., 239.255.0.1, ...)
Is there a way to achieve the sending of the multicast message via all interfaces without the setsockopt IP_MULTICAST_IF
loop? Maybe I missed some socket options?
Or if not possible in code, is there a way to configure Windows to forward the packets to all interfaces?
Originally I thought multicast would go to all joining interfaces, is Windows handling this correctly?
CodePudding user response:
Packets will only ever go out one interface, whether they are unicast, multicast, or broadcast.
The correct way to handle this is as you've discovered, namely to set the IP_MULTICAST_IF
option on the socket before sending the packet.