Sharpened exception handling for IPv6 support.

This commit is contained in:
Gregor Robinson 2013-07-22 20:10:55 +01:00
parent bf8177c148
commit ce921bf199
2 changed files with 11 additions and 3 deletions

View File

@ -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

View File

@ -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' + \