Windows-compatible IPv6-minded cleanup of recaddr.
* Made shared.{,un}packNetworkAddress() Windows-compatible. * Removes superfluous break statement.
This commit is contained in:
parent
90771f377c
commit
bf8177c148
|
@ -31,7 +31,6 @@ class AddressMessageParser:
|
||||||
break
|
break
|
||||||
if not hostDetails:
|
if not hostDetails:
|
||||||
continue
|
continue
|
||||||
break
|
|
||||||
|
|
||||||
timestamp, stream, services, host, port = hostDetails
|
timestamp, stream, services, host, port = hostDetails
|
||||||
yield hostDetails
|
yield hostDetails
|
||||||
|
|
|
@ -78,6 +78,27 @@ def isInSqlInventory(hash):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def packNetworkAddress(address):
|
def packNetworkAddress(address):
|
||||||
|
# For windows, we need to avoid socket.inet_pton()
|
||||||
|
if sys.platform == 'win32':
|
||||||
|
try:
|
||||||
|
# Matches IPV4-style address.
|
||||||
|
if ':' not in address and address.count('.') == 3:
|
||||||
|
# Already in IPv6 format; no address curtailing needed.
|
||||||
|
pass
|
||||||
|
elif address.lower().startswith('::ffff:'):
|
||||||
|
# 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.')
|
||||||
|
|
||||||
|
# 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' + \
|
||||||
|
socket.inet_aton(address)
|
||||||
|
except Exception as err:
|
||||||
|
logger.error('Failed to pack address "%s". Err: %s' % (address, err))
|
||||||
|
raise
|
||||||
|
# Works on most modern posix distros.
|
||||||
|
else:
|
||||||
try:
|
try:
|
||||||
# Matches IPV4-style address.
|
# Matches IPV4-style address.
|
||||||
if ':' not in address and address.count('.') == 3:
|
if ':' not in address and address.count('.') == 3:
|
||||||
|
@ -85,14 +106,26 @@ def packNetworkAddress(address):
|
||||||
# Matches IPV4-mapped IPV6 and plain IPV6.
|
# Matches IPV4-mapped IPV6 and plain IPV6.
|
||||||
else:
|
else:
|
||||||
return socket.inet_pton(socket.AF_INET6, address)
|
return socket.inet_pton(socket.AF_INET6, address)
|
||||||
except OSError:
|
except Exception as err:
|
||||||
logger.error('Failed to pack address "%s".' % (address))
|
logger.error('Failed to pack address "%s". Err: %s' % (address, err))
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
# Take packed IPv4-mapped IPv6 address and return unpacked IPv4-mapped IPv6 string in standard
|
||||||
|
# notation (e.g. ::ffff:127.0.0.1).
|
||||||
def unpackNetworkAddress(packedAddress):
|
def unpackNetworkAddress(packedAddress):
|
||||||
|
# For windows, we need to avoid socket.inet_ntop()
|
||||||
|
if sys.platform == 'win32':
|
||||||
try:
|
try:
|
||||||
address = socket.inet_ntop(socket.AF_INET6, packedAddress)
|
if not packedAddress.startswith('\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF'):
|
||||||
return address
|
raise Exception('IPv6 not supported by unpackNetworkAddress on Windows.')
|
||||||
|
return '::ffff:' + socket.inet_ntoa(packedAddress[12:])
|
||||||
|
except:
|
||||||
|
logger.error('Failed to unpack address %s.' % repr(packedAddress))
|
||||||
|
raise
|
||||||
|
# Works on most modern posix distros.
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
return socket.inet_ntop(socket.AF_INET6, packedAddress)
|
||||||
except:
|
except:
|
||||||
logger.error('Failed to unpack address %s.' % repr(packedAddress))
|
logger.error('Failed to unpack address %s.' % repr(packedAddress))
|
||||||
raise
|
raise
|
||||||
|
|
Reference in New Issue
Block a user