Return common prefix length of IPv6 addresses a and b.
(a, b)
| 880 | |
| 881 | |
| 882 | def in6_get_common_plen(a, b): |
| 883 | # type: (str, str) -> int |
| 884 | """ |
| 885 | Return common prefix length of IPv6 addresses a and b. |
| 886 | """ |
| 887 | def matching_bits(byte1, byte2): |
| 888 | # type: (int, int) -> int |
| 889 | for i in range(8): |
| 890 | cur_mask = 0x80 >> i |
| 891 | if (byte1 & cur_mask) != (byte2 & cur_mask): |
| 892 | return i |
| 893 | return 8 |
| 894 | |
| 895 | tmpA = inet_pton(socket.AF_INET6, a) |
| 896 | tmpB = inet_pton(socket.AF_INET6, b) |
| 897 | for i in range(16): |
| 898 | mbits = matching_bits(orb(tmpA[i]), orb(tmpB[i])) |
| 899 | if mbits != 8: |
| 900 | return 8 * i + mbits |
| 901 | return 128 |
| 902 | |
| 903 | |
| 904 | def in6_isvalid(address): |
no test coverage detected