Skip to content

Commit

Permalink
Merge branch 'dev-fix-server-ipv6'. Close #158.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanperez-keera committed Nov 29, 2023
2 parents a8208bb + fbf1a23 commit 45d7f1c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
33 changes: 33 additions & 0 deletions analyzer/python/ikos/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 on IPv6 when available.
In systems with dual stack, this also listens on IPv4.
This class tries to listen 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
4 changes: 2 additions & 2 deletions analyzer/python/ikos/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,8 +641,8 @@ def __init__(self):
try:
# try to start the http server on a random port
self.port = random.randint(8000, 9000)
self.httpd = http.HTTPServer(('', self.port),
ScanServerRequestHandler)
self.httpd = http.HTTPServerIPv6(('', self.port),
ScanServerRequestHandler)
except (OSError, IOError):
self.port = None # port already in use

Expand Down
4 changes: 2 additions & 2 deletions analyzer/python/ikos/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
from ikos import settings
from ikos.enums import Result, CheckKind
from ikos.highlight import CppLexer, HtmlFormatter, highlight
from ikos.http import HTTPServer, BaseHTTPRequestHandler
from ikos.http import HTTPServerIPv6, BaseHTTPRequestHandler
from ikos.log import printf
from ikos.output_db import OutputDatabase

Expand Down Expand Up @@ -337,7 +337,7 @@ def __init__(self, db, port=8080):
self.report = ViewReport(self.db)

try:
self.httpd = HTTPServer(('', self.port), RequestHandler)
self.httpd = HTTPServerIPv6(('', self.port), RequestHandler)
except (OSError, IOError) as e:
log.error("Could not start the HTTP server: %s" % e)
sys.exit(1)
Expand Down

0 comments on commit 45d7f1c

Please sign in to comment.