From 57c1de58d7a20f3264ac7ebf04e33da3554c1423 Mon Sep 17 00:00:00 2001 From: Ana Date: Tue, 20 Aug 2024 22:46:49 +0530 Subject: [PATCH] Fix: Handle IPv6 Stack Unavailability in Tracer This commit addresses an issue where the Tracer class in connvitals/traceroute.py would throw an exception if the host machine did not have an IPv6 stack available. The issue stemmed from the __init__ method attempting to create an IPv6 socket regardless of the system's capability. This commit resolves the problem by adding a check for socket.has_ipv6 before creating the socket. If an IPv6 stack is not available, an EnvironmentError is raised with a descriptive message, preventing the exception and providing clearer feedback to the user. --- connvitals/traceroute.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/connvitals/traceroute.py b/connvitals/traceroute.py index 35f2b0c..7a7324c 100644 --- a/connvitals/traceroute.py +++ b/connvitals/traceroute.py @@ -39,17 +39,20 @@ def __init__(self, host:utils.Host, ID:int, maxHops:int): self.maxHops = maxHops # A bunch of stuff needs to be tweaked if we're using IPv6 - if host.family is socket.AF_INET6: - # BTW, this doesn't actually work. The RFCs for IPv6 don't define - # the behaviour of raw sockets - which are heavily utilized by - # `connvitals`. One of these days, I'll have to implement it using - # raw ethernet frames ... - - self.receiver = socket.socket(family=host.family, - type=socket.SOCK_RAW, - proto=socket.IPPROTO_ICMPV6) - self.setTTL = self.setIPv6TTL - self.isMyTraceResponse = self.isMyIPv6TraceResponse + if host.family is socket.AF_INET6: + if socket.has_ipv6: + # BTW, this doesn't actually work. The RFCs for IPv6 don't define + # the behaviour of raw sockets - which are heavily utilized by + # `connvitals`. One of these days, I'll have to implement it using + # raw ethernet frames ... + + self.receiver = socket.socket(family=host.family, + type=socket.SOCK_RAW, + proto=socket.IPPROTO_ICMPV6) + self.setTTL = self.setIPv6TTL + self.isMyTraceResponse = self.isMyIPv6TraceResponse + else: + raise EnvironmentError("IPv6 stack is not available on this system.") else: self.receiver = socket.socket(family=host.family,