mclarkk / lifxlan

Python library for accessing LIFX devices locally using the official LIFX LAN protocol.
MIT License
503 stars 115 forks source link

get_devices() throws a "OSError: [WinError 10051] A socket operation was attempted to an unreachable network" if a Hyper-V Virtual Ethernet is active #164

Closed samclane closed 1 year ago

samclane commented 3 years ago

Problem: Running lifxLAN.get_devices() with a Hyper-V Virtual Ethernet Adapter enabled causes Python to throw:

OSError: [WinError 10051] A socket operation was attempted to an unreachable network

Traceback:

...
  File "C:/Users/samcl/PycharmProjects/LIFX-Control-Panel/lifx_control_panel/__main__.pyw", line 165, in scan_for_lights
    device_list: List[lifxlan.Device] = self.lifx.get_devices()
  File "C:\Users\samcl\PycharmProjects\LIFX-Control-Panel\venv\lib\site-packages\lifxlan\lifxlan.py", line 38, in get_devices
    self.discover_devices()
  File "C:\Users\samcl\PycharmProjects\LIFX-Control-Panel\venv\lib\site-packages\lifxlan\lifxlan.py", line 50, in discover_devices
    responses = self.broadcast_with_resp(GetService, StateService,)
  File "C:\Users\samcl\PycharmProjects\LIFX-Control-Panel\venv\lib\site-packages\lifxlan\lifxlan.py", line 246, in broadcast_with_resp
    self.sock.sendto(msg.packed_message, (ip_addr, UDP_BROADCAST_PORT))
OSError: [WinError 10051] A socket operation was attempted to an unreachable network

Process finished with exit code 1

Workaround: Disable the Hyper-V adapter in your Control Panel

System Info:

OS: Windows 10 Python Version: 3.8.3 (venv) Pip List:

Package                   Version
------------------------- ---------
altgraph                  0.17
appdirs                   1.4.4
attrs                     21.2.0
bitstring                 3.1.7
black                     19.10b0
bleach                    3.3.0
certifi                   2020.12.5
cfgv                      3.3.0
chardet                   3.0.4
click                     8.0.1
colorama                  0.4.4
Desktopmagic              14.3.11
distlib                   0.3.1
docutils                  0.17.1
filelock                  3.0.12
future                    0.18.2
identify                  2.2.6
idna                      2.10
ifaddr                    0.1.7
importlib-metadata        4.3.0
keyboard                  0.13.5
keyring                   23.0.1
lifxlan                   1.2.7
mouse                     0.7.1
mss                       6.1.0
netifaces                 0.10.9
nodeenv                   1.6.0
numexpr                   2.7.3
numpy                     1.20.3
packaging                 20.9
pathspec                  0.8.1
pefile                    2021.5.24
Pillow                    8.2.0
pip                       21.1.2
pkginfo                   1.7.0
pre-commit                2.13.0
PyAudio                   0.2.11
Pygments                  2.9.0
PyHook3                   1.6.1
pyinstaller               4.3
pyinstaller-hooks-contrib 2021.1
pyparsing                 2.4.7
pystray                   0.17.3
pywin32                   300
pywin32-ctypes            0.2.0
PyYAML                    5.4.1
readme-renderer           29.0
regex                     2021.4.4
requests                  2.25.1
requests-toolbelt         0.9.1
rfc3986                   1.5.0
setuptools                57.0.0
six                       1.16.0
toml                      0.10.2
tqdm                      4.61.0
twine                     3.4.1
typed-ast                 1.4.3
urllib3                   1.26.5
virtualenv                20.4.7
webencodings              0.5.1
zipp                      3.4.1

I've tried updating all my packages, including the latest src release of lifxlan, with no changes.

Additional Files: image

stuntguy3000 commented 3 years ago

Can confirm, same problem here. Would love a way to select the network adapter.

pauldapps commented 2 years ago

Potential breaking change, but you could add an additional filter to this line to exclude Hyper-v adapters.

It's not clear at this time if all Hyper-V interfaces cause this error or just specific types. I see WSL is in play on your machine. However, some Hyper-V interface types could be how the host actually does access the LAN.

Edit: Not just Hyper-V adapters cause this. In order to solve the issue, I added a continue filter at the adapter level on L43.

Here's what I ultimately ended up doing to filter out the [I assume] bad interface types and allow for manual selection of an interface to use. In device.py:

"""added global default variable IFINDEX"""
DEFAULT_TIMEOUT = 1  # second
DEFAULT_ATTEMPTS = 1
IFINDEX = 24
VERBOSE = False
#
#...lines omitted...
#
"""added logic to override the list of all adapters if global IFINDEX is passed to function."""
"""added regex filter based on interface nice_name for a few values that I felt like shouldn't be included in all_adapters"""
def get_broadcast_addrs(use_index: int = None):
    broadcast_addrs = []
    all_adapters = ifaddr.get_adapters()
    if use_index is not None:
        to_process = [a for a in all_adapters if a.index == use_index]
    else:
        to_process = all_adapters
    for iface in to_process:
        if re.search(
            "VPN|TAP|Bluetooth|Loopback|Virtual Adapter|Hyper-V", iface.nice_name
        ):
            continue
#
#...lines omitted...
#
"""added check to see if ifindex != none"""
if IFINDEX:
    UDP_BROADCAST_IP_ADDRS = get_broadcast_addrs(use_index=IFINDEX)
else:
    UDP_BROADCAST_IP_ADDRS = get_broadcast_addrs()
UDP_BROADCAST_PORT = 56700
exking commented 1 year ago

165 is now merged, please re-open if issue still present

Aiddy81 commented 5 months ago

I've downloaded the latest version of this library and have both hyper-v and vmware virtual apdaters on my computer and am getting this OSError: [WinError 10051] A socket operation was attempted to an unreachable network error.

These are what the adapters look like on my system,

image