Skip to content

Commit

Permalink
[analyzer] Add class for HTTP server defaulting to IPv6 (NASA-SW-VnV#158
Browse files Browse the repository at this point in the history
).

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.
  • Loading branch information
ivanperez-keera committed Nov 27, 2023
1 parent a8208bb commit a337bdd
Showing 1 changed file with 33 additions and 0 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

0 comments on commit a337bdd

Please sign in to comment.