7

嘿,程序员/开发人员/网络人员/Devops/...

我在 WSL2(Windows 10 2004 版本)上下文中的mDNS/设置有问题DNS-SD

我在家里有一个非常简单的设置,有一个主服务器和一个 Raspberry Pi,我想激活 DNS 服务发现,从而让我有一种简单的方法来自动发现我的 Raspberry PI 上的服务器。

使用dnssd 之类的简单库,甚至自己广播正确的数据,我设法使其在不使用 WSL2 时轻松工作。但是我需要让它在 WSL2 上工作,这就是事情变得复杂的地方。

由于 WSL2 在其自己的子网上运行,因此广播不再起作用。在子网上使用 mDNS 仅适用于该子网。但是,Windows 已经重新路由主机和 WSL 之间的一些广播流量。

这很容易测试:Ping从我的服务器到依赖于 mDNS 的 Pi 的 Avahi 地址做一个简单的工作。

在此处输入图像描述

在屏幕的左侧,您可以看到 Wireshark 在主机网络接口上捕获的流量,在右侧,您可以看到 Wireshark 在 WSL 网络接口上捕获的流量。前 3 行是一个简单的 ping :它是在 WSL 的上下文中执行的,但是这里出现的 IP 地址 - 172.28.192.1- 不是 WSL 客户端的 IP 地址,它是 WSL 内部 DNS 服务器的 IP 地址。如右图所示,它在主机上完美地重新路由,带有 windows 主机的 IP 地址:192.168.0.39

但是,由脚本执行的第二个查询具有 WSL 源 IP ( 172.28.204.42),并且此查询不会在主机上重新路由。

我的网络知识非常有限,我不明白这是如何工作的,以及如何让 WSL 在主机上路由我自己的 mDNS 查询。一个疯狂的猜测是它与 iptables 有关,但就我而言。

如果有人知道它为什么在 DNS 服务器源地址上工作而不是当我自己执行它时,它会对我有很大帮助!

编辑 1:WSL 路由表 在此处输入图像描述

4

1 回答 1

3

The WSL2 Hyper-V network switch does not act as a multicast bridge. By default, the switch creates an internal network. Multicast packets are only delivered to systems connected to the internal network and not to anything beyond it. More information about Hyper-V network types can be found in this Nakivo blog post.

In your first case, the ping triggers a regular DNS lookup that goes to the resolver -- the Windows host. The Windows host then performs an mDNS lookup on both its external and internal networks. Your packet dump shows the internal lookup, but note that nothing responds to it. The response comes via the external network, and the ping gets its response via regular DNS. In your second case, you perform only an mDNS lookup. That lookup receives no response, because it only goes to the internal network. For proof that mDNS lookups work on the internal network, do a lookup for the Windows host's local address (MACHINE.local). It will work, because the Windows host is on the internal network and can respond.

The good news is you can change the WSL network type.

  1. Hit your Windows key and type "Hyper-V Manager"
  2. Right-click on the app and choose "Run as administrator"
  3. In the manager, find your machine under "Hyper-V Manager" and click on it
  4. In the Actions area, click on "Virtual Switch Manager..."
  5. Find the WSL switch and click on it
  6. Change the connection type to "External network"
  7. Click OK

Hyper-V Manager Screenshot

After doing this, restart WSL:

> wsl --shutdown
> wsl -t <distribution-name>
> wsl --distribution <distribution-name>

Once restarted, your guest's network will be broken. You will need to add an IP address and route from your external network using ip addr add and ip route or something similar. Your distribution is almost certainly going to try to setup its network for the default WSL switch every time it starts, so you may need to setup configurations to add this external network address in the future. As an example, Ubuntu 20.04 always adds a dynamic address on the internal network, regardless of the switch configuration.

Making the change to the network switch type will likely break other uses of WSL2 (e.g. Docker Desktop) for the same reason. Windows re-creates the Hyper-V network switch every time it reboots, so your change will only last as long as your system stays up.

于 2020-06-07T14:20:11.007 回答