From e234c6146e910135645390cb5f23ea7ca377755a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 31 Oct 2018 13:39:54 +0100 Subject: [PATCH] Better detection of ipv6 support Apparently socket.has_ipv6 doesn't actually do what it says in the docs (state whether the underlying platform supports ipv6). It returns True even when ipv6 has manually been disabled for whatever reason. So we will no longer trust this but instead attempt to actually create a v6 socket and only if that succeeds assume that v6 is indeed supported on the platform. --- src/octoprint/util/net.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/octoprint/util/net.py b/src/octoprint/util/net.py index 2ca5cfea3..ea935285d 100644 --- a/src/octoprint/util/net.py +++ b/src/octoprint/util/net.py @@ -9,7 +9,26 @@ import sys import netaddr import netifaces -HAS_V6 = socket.has_ipv6 +_cached_check_v6 = None +def check_v6(): + global _cached_check_v6 + + def f(): + if not socket.has_ipv6: + return False + + try: + socket.socket(socket.AF_INET6, socket.SOCK_STREAM) + except: + # "[Errno 97] Address family not supported by protocol" or anything else really... + return False + return True + + if _cached_check_v6 is None: + _cached_check_v6 = f() + return _cached_check_v6 + +HAS_V6 = check_v6() if hasattr(socket, "IPPROTO_IPV6") and hasattr(socket, "IPV6_V6ONLY"): # Dual stack support, hooray! @@ -21,7 +40,7 @@ else: IPPROTO_IPV6 = 41 IPV6_V6ONLY = 27 else: - # Whatever we are running on here, it we don't want to use V6 on here due to lack of dual stack support + # Whatever we are running on here, we don't want to use V6 on here due to lack of dual stack support HAS_V6 = False def is_lan_address(address, additional_private=None):