From 897919cf922e4c739924f88ba8eb3a93f3f9d387 Mon Sep 17 00:00:00 2001 From: Ivan Perez Date: Sun, 29 Oct 2023 15:25:42 +0000 Subject: [PATCH] [analyzer] Add class for HTTP server defaulting to IPv6 (#158). This commit adds a new class HTTPServerIPv6 that listens on IPv6 by default when available. In systems where IPv6 is not available, the server attemps to use IPv4. In systems with dual stack (e.g., default on linux), opening a server in IPv6 will also listen in IPv4. --- analyzer/python/ikos/http.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/analyzer/python/ikos/http.py b/analyzer/python/ikos/http.py index 3b7d6117..44202ce2 100644 --- a/analyzer/python/ikos/http.py +++ b/analyzer/python/ikos/http.py @@ -50,3 +50,36 @@ from urlparse import parse_qs from urllib import urlencode from urllib2 import urlopen + +from errno import EAFNOSUPPORT +from socket import AF_INET, AF_INET6 +import os + +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. + super().__init__(server_address, RequestHandlerClass, bind_and_activate) + + except OSError as e: + if e.errno == EAFNOSUPPORT: + # If the exception is due to IPv6 not being supported, we + # select IPv4 only and try to re-open the server again. + self.address_family = AF_INET + super().__init__(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