Make addr2bin and dynip handle IP6.
This commit is contained in:
+2
-1
@@ -48,9 +48,10 @@ def is_dynip(host,addr):
|
|||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
if host.startswith('[') and host.endswith(']'):
|
if host.startswith('[') and host.endswith(']'):
|
||||||
return True
|
return True # no ptr
|
||||||
if addr:
|
if addr:
|
||||||
if host.find(addr) >= 0: return True
|
if host.find(addr) >= 0: return True
|
||||||
|
if addr.find(':') >= 0: return False # IP6
|
||||||
a = addr.split('.')
|
a = addr.split('.')
|
||||||
ia = map(int,a)
|
ia = map(int,a)
|
||||||
h = host
|
h = host
|
||||||
|
|||||||
+10
-5
@@ -28,16 +28,21 @@ ip6re = re.compile( '(?:%(hex4)s:){6}%(ls32)s$'
|
|||||||
}, re.IGNORECASE)
|
}, re.IGNORECASE)
|
||||||
|
|
||||||
# from spf.py
|
# from spf.py
|
||||||
def addr2bin(str):
|
def addr2bin(s):
|
||||||
"""Convert a string IPv4 address into an unsigned integer."""
|
"""Convert a string IPv4 address into an unsigned integer."""
|
||||||
|
if s.find(':') >= 0:
|
||||||
try:
|
try:
|
||||||
return struct.unpack("!L", socket.inet_aton(str))[0]
|
return bin2long6(inet_pton(s))
|
||||||
|
except:
|
||||||
|
raise socket.error("Invalid IP6 address: "+s)
|
||||||
|
try:
|
||||||
|
return struct.unpack("!L", socket.inet_aton(s))[0]
|
||||||
except socket.error:
|
except socket.error:
|
||||||
raise socket.error("Invalid IP4 address: "+str)
|
raise socket.error("Invalid IP4 address: "+s)
|
||||||
|
|
||||||
def bin2long6(str):
|
def bin2long6(s):
|
||||||
"""Convert binary IP6 address into an unsigned Python long integer."""
|
"""Convert binary IP6 address into an unsigned Python long integer."""
|
||||||
h, l = struct.unpack("!QQ", str)
|
h, l = struct.unpack("!QQ", s)
|
||||||
return h << 64 | l
|
return h << 64 | l
|
||||||
|
|
||||||
if hasattr(socket,'has_ipv6') and socket.has_ipv6:
|
if hasattr(socket,'has_ipv6') and socket.has_ipv6:
|
||||||
|
|||||||
Reference in New Issue
Block a user