robyoung/tplinker

Detection not working with WSL

stanciuadrian opened this issue · 4 comments

I'm on Windows 10 with WSL2 and discover() does not always detect my HS110.

ipconfig enumerates the following connected adapters:

  • Ethernet adapter vEthernet (WSL) with the range 172.31.x.x
  • Ethernet adapter Ethernet with the range 192.168.x.x. My HS110 lives here.

A short investigation with the Microsoft Network Monitor shows that the broadcast is sent 3 times on either interface but not on both. No response is received when vEthernet is chosen.

The code seems fine. The socket is bound on all interfaces and data is sent to INADDR_BROADCAST. The binary is native and executed outside WSL.

  for (addr, device_data) in discover().unwrap() {
    let device = Device::from_data(addr, &device_data);
    let device_info = device_data.sysinfo();
    println!("IP:{} Alias:{} HwType:{}", addr, device_info.alias, device_info.hw_type);
  }

It seems this behavior is normal on Windows 10: https://serverfault.com/questions/72112/how-to-alter-the-global-broadcast-address-255-255-255-255-behavior-on-windows.

I can try to implement a #[cfg(windows)]-specific behavior by enumerating the network interfaces and then by sending the broadcast through all of them.

Interestingly, MSDN says that sendto should send broadcast packets on all interfaces:

To send a broadcast (on a SOCK_DGRAM only), the address pointed to by the to parameter can be constructed to contain the special IPv4 address INADDR_BROADCAST (defined in Winsock2.h), together with the intended port number. If the address pointed to by the to parameter contains the INADDR_BROADCAST address and intended port, then the broadcast will be sent out on all interfaces to that port.

(source)

But considering e.g. https://stackoverflow.com/a/683774, it might be a good idea to do that on every platform.

https://crates.io/crates/if-addrs looks like it should help with that.

I'm afraid I don't have a Windows machine I can test on but I'd be happy to take a PR on faith that it works for Windows 10.