Skip to content
Snippets Groups Projects
Commit 0190c6f5 authored by Arnaud Ebalard's avatar Arnaud Ebalard
Browse files

Added in6_get_common_plen()

parent a1c5a258
No related branches found
No related tags found
No related merge requests found
......@@ -700,3 +700,22 @@ def in6_getscope(addr):
else:
scope = -1
return scope
def in6_get_common_plen(a, b):
"""
Return common prefix length of IPv6 addresses a and b.
"""
def matching_bits(byte1, byte2):
for i in range(8):
cur_mask = 0x80 >> i
if (byte1 & cur_mask) != (byte2 & cur_mask):
return i
return 8
tmpA = inet_pton(socket.AF_INET6, a)
tmpB = inet_pton(socket.AF_INET6, b)
for i in range(16):
mbits = matching_bits(ord(tmpA[i]), ord(tmpB[i]))
if mbits != 8:
return 8*i + mbits
return 128
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment