From 34cf0990dd596a1e4f1a9d14fb5a752176c4a50b Mon Sep 17 00:00:00 2001 From: Ivan Perez Date: Mon, 27 Nov 2023 01:44:40 +0000 Subject: [PATCH] [analyzer] Default to IPv4 if IPv6 is not available (#158). This commit re-defines the IPv6 HTTP server class to attempt to open a server listing only on IPv4 if IPv6 is not available. [ci skip] --- analyzer/python/ikos/http.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/analyzer/python/ikos/http.py b/analyzer/python/ikos/http.py index 16c51173..abf2f2b3 100644 --- a/analyzer/python/ikos/http.py +++ b/analyzer/python/ikos/http.py @@ -42,16 +42,20 @@ try: # Python 3 from http.server import HTTPServer, BaseHTTPRequestHandler + from errno import EAFNOSUPPORT from urllib.parse import parse_qs, urlencode from urllib.request import urlopen from socket import AF_INET6 + import os # OSError except ImportError: # Python 2 from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler + from errno import EAFNOSUPPORT from urlparse import parse_qs from urllib import urlencode from urllib2 import urlopen from socket import AF_INET6 + import os # OSError class HTTPServerIPv6(HTTPServer): @@ -59,6 +63,26 @@ class HTTPServerIPv6(HTTPServer): HTTP server that listens in IPv6. In systems with dual stack, this also listens in IPv4. + + This method tries to listed on IPv6 first (and IPv4 if available). If that + is not supported, then it tries IPv4 only. ''' address_family = AF_INET6 + + def __init__(self, server_address, RequestHandlerClass, bind_and_activate=True): + try: + # Initially try to open the server with IPv6. This will also select + # IPv4 on systems with dual stack. + HTTPServer(server_address, RequestHandlerClass, bind_and_activate) + + except OSError as e: + if e.errno == errno.EAFNOSUPPORT: + # If the exception is due to IPv6 not being supported, we + # select IPv4 only and try to re-open the server again. + address_family = AF_INET + HTTPServer(server_address, RequestHandlerClass, bind_and_activate) + else: + # If the exception is for any other reason other than IPv6 not + # being supported, then we cannot do anything about it. + raise e