title: Send UDP datagrams to multiple interfaces with multicast address
body: <p>I have a Windows 11 machine that has two network interfaces:</p>
  <ol>
  <li><code>192.168.1.16/24</code> (Wi-Fi: physical Ethernet connected to external router)</li>
  <li><code>172.22.112.1/20</code> (vEthernet (Default Switch): internal virtual switch from Hyper-V)</li>
  </ol>
  <p>I am writing an application that should send multicast messages via both interfaces.</p>
  <p>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 :) :</p>
  <pre><code>s = socket(AF_INET, SOCK_DGRAM, ...)
  bind(s, INADDR_ANY,...)
  sendto(s, buf, ..., 239.255.0.1, ...)
  </code></pre>
  <p>The messages arrives unfortunately only on the Wi-Fi interface. This interface is determined by the routing table, I guess.</p>
  <pre><code>PS&gt;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
  ...
  </code></pre>
  <p>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.</p>
  <pre><code>PS&gt; 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
  </code></pre>
  <p>One solution I found, is to send the packet twice like so:</p>
  <pre><code>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, ...)
  </code></pre>
  <p>Is there a way to achieve the sending of the multicast message via all interfaces without the <code>setsockopt IP_MULTICAST_IF</code> loop? Maybe I missed some socket options?</p>
  <p>Or if not possible in code, is there a way to configure Windows to forward the packets to all interfaces?</p>
  <p>Originally I thought multicast would go to all joining interfaces, is Windows handling this correctly?</p>