ankermake-m5-protocol
ankermake-m5-protocol copied to clipboard
[BUG] lan-search for printers does not work on Windows 11
During further investigation for PR #150 and Issue #142 I noticed, that ankerctl.py lan-search does not work as expected on Windows 11 in that no printer is found even though the host is on the same network as the printer and the printer is turned on.
From Linux computers, the printer could be found without issues.
To Reproduce Steps to reproduce the behavior:
- execute
ankerctl.py lan-searchon a Windows 11 computer - No printer will be detected.
Expected behavior The printer should be detected just the same as on a Linux machine.
Desktop (please complete the following information):
- OS: Windows 11
- Python 3.12
- Version 1.0
Initial Analysis The following observations where done:
- Wireshark on the Win 11 machine did not report any UDP broadcast packets being sent out, even with the Defender Firewall being disabled.
- A small test program just sending out UDP broadcasts to 255.255.255.255 port confirmed the issue:
#!/usr/bin/python
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
#sock.bind(("192.168.178.23", 0)) # with this the packet is actually sent on the desired interface
#sock.bind(("0.0.0.0", 0)) # does not work
#sock.bind(("", 0)) # does not work either
addr = ("255.255.255.255", 32108)
sock.sendto(b"hello", addr)
- This SO post lead me to the solution in the script above (see comment "# with this the packet is actually sent..."). Short story, on Windows 11 the broadcast does not seem to be sent out on all interfaces, just on the "first" on (whichever this may be).
So the solution for a Windows code path might be to determine all local network addresses and send out the broadcast individually from each of these addresses. I already found a library ifaddr which seems to do the job well according to initial tests.
I provided a fix in commit 2b6c402ed7887ef8d0e45ccd3f78204107ddb52a as part of PR #150.