From ce921bf1996c3b433271dc092905be2b21da88c0 Mon Sep 17 00:00:00 2001 From: Gregor Robinson Date: Mon, 22 Jul 2013 20:10:55 +0100 Subject: [PATCH] Sharpened exception handling for IPv6 support. --- src/message_parsers.py | 9 +++++++-- src/shared.py | 5 ++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/message_parsers.py b/src/message_parsers.py index 069b8ea9..4964d045 100644 --- a/src/message_parsers.py +++ b/src/message_parsers.py @@ -107,7 +107,8 @@ class AddressMessageParser: return port # Consume one entry in the addr_list. - # Returns (timestamp, stream, services, host, port) tuple on success. + # Returns (timestamp, stream, services, host, port) tuple on success, + # and None on failure. def __consumeAddress(self): past_position = self.position try: @@ -116,9 +117,13 @@ class AddressMessageParser: services = self.__consumeServices() host = self.__consumeHost() port = self.__consumePort() + addressDetails = (timestamp, stream, services, host, port) + except Exception as err: + logger.warning('Could not read address in addr message. Err: %s' % (err)) + addressDetails = None finally: if self.remoteProtocolVersion == 1: self.position = past_position + 34 elif self.remoteProtocolVersion == 2: self.position = past_position + 38 - return (timestamp, stream, services, host, port) + return addressDetails diff --git a/src/shared.py b/src/shared.py index 33f4197a..0b2ba7b0 100644 --- a/src/shared.py +++ b/src/shared.py @@ -77,6 +77,9 @@ def isInSqlInventory(hash): else: return True +class IPv6NotSupportedException(Exception): + pass + def packNetworkAddress(address): # For windows, we need to avoid socket.inet_pton() if sys.platform == 'win32': @@ -89,7 +92,7 @@ def packNetworkAddress(address): # Chop off the IPv4-mapped IPv6 prefix from the standard-form address. address = address[7:] else: - raise Exception('IPv6 not supported by packNetworkAddress on Windows.') + raise IPv6NotSupportedException('IPv6 not supported by packNetworkAddress on Windows.') # Pack the standard-form IPv4 address and add prefix to make packed IPv4-mapped IPv6. return '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF' + \