From 0190c6f516b9bd9e917f32329853b512b51eb4a2 Mon Sep 17 00:00:00 2001 From: Arnaud Ebalard <arno@natisbad.org> Date: Mon, 2 Nov 2009 21:03:29 +0100 Subject: [PATCH] Added in6_get_common_plen() --- scapy/utils6.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/scapy/utils6.py b/scapy/utils6.py index 1b7a8c0d..17c94b4c 100644 --- a/scapy/utils6.py +++ b/scapy/utils6.py @@ -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 -- GitLab