Improved IPv4/IPv6 error handling during client testing.

This commit is contained in:
Joe Testa 2019-10-10 23:09:45 -04:00
parent e3a59a3e21
commit fd85e247e7

View File

@ -2004,13 +2004,18 @@ class SSH(object): # pylint: disable=too-few-public-methods
# auditing client connections).
def listen_and_accept(self):
try:
# Socket to listen on all IPv4 addresses.
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('0.0.0.0', self.__port))
s.listen()
self.__sock_map[s.fileno()] = s
except Exception as e:
print("Warning: failed to listen on any IPv4 interfaces.")
pass
try:
# Socket to listen on all IPv6 addresses.
s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
@ -2018,6 +2023,14 @@ class SSH(object): # pylint: disable=too-few-public-methods
s.bind(('::', self.__port))
s.listen()
self.__sock_map[s.fileno()] = s
except Exception as e:
print("Warning: failed to listen on any IPv6 interfaces.")
pass
# If we failed to listen on any interfaces, terminate.
if len(self.__sock_map.keys()) == 0:
print("Error: failed to listen on any IPv4 and IPv6 interfaces!")
exit(-1)
# Wait for a connection on either socket.
fds = select.select(self.__sock_map.keys(), [], [])