Skip to content

Commit

Permalink
[analyzer] Default to IPv4 if IPv6 is not available (#158).
Browse files Browse the repository at this point in the history
This commit re-defines the IPv6 HTTP server class to attempt to open a
server listing only on IPv4 if IPv6 is not available.
  • Loading branch information
ivanperez-keera committed Nov 27, 2023
1 parent bd7a46d commit 012ec2e
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions analyzer/python/ikos/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,47 @@
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):
'''
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

0 comments on commit 012ec2e

Please sign in to comment.