简体   繁体   English

向具有多播地址的多个接口发送 UDP 数据报

[英]Send UDP datagrams to multiple interfaces with multicast address

I have a Windows 11 machine that has two.network interfaces:我有一台 Windows 11 机器,它有两个网络接口:

  1. 192.168.1.16/24 (Wi-Fi: physical Ethe.net connected to external router) 192.168.1.16/24 :物理 Ethe.net 连接到外部路由器)
  2. 172.22.112.1/20 (vEthe.net (Default Switch): internal virtual switch from Hyper-V) 172.22.112.1/20 (vEthe.net(默认交换机):来自 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:):理想情况下,我只想从应用程序发送一次数据,并且数据将在两个接口上结束,而无需配置 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.不幸的是,消息仅通过 Wi-Fi 接口到达。 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.在这种情况下,它只转到接口 192.168.1.16,因为度量是匹配路由的最小值。 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?有没有办法在没有setsockopt IP_MULTICAST_IF循环的情况下通过所有接口发送多播消息? 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?或者如果在代码中不可能,有没有办法配置 Windows 将数据包转发到所有接口?

Originally I thought multicast would go to all joining interfaces, is Windows handling this correctly?最初我认为多播会将 go 发送到所有加入的接口,Windows 是否正确处理了这个问题?

Packets will only ever go out one interface, whether they are unicast, multicast, or broadcast.数据包只会从一个接口发出 go,无论它们是单播、多播还是广播。

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.处理此问题的正确方法正如您所发现的那样,即在发送数据包之前在套接字上设置IP_MULTICAST_IF选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM