Page MenuHomeDevCentral

D3077.id7853.diff
No OneTemporary

D3077.id7853.diff

diff --git a/_modules/network_utils.py b/_modules/network_utils.py
--- a/_modules/network_utils.py
+++ b/_modules/network_utils.py
@@ -8,6 +8,7 @@
# -------------------------------------------------------------
+import ipaddress
import re
@@ -44,3 +45,20 @@
#
# There is 24 "1" in this expression, that's 24 is our CIDR prefix.
return sum([bin(int(octet)).count("1") for octet in netmask.split(".")])
+
+
+# -------------------------------------------------------------
+# IPv6 prefixes
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+
+def _ipv6_address_to_prefix(address, prefixlen):
+ return ipaddress.IPv6Network((address, prefixlen), strict=False)
+
+
+def can_directly_be_discovered(gateway, address, prefixlen):
+ """Determines if the gateway belong to the same prefix than the address,
+ and so can be directly be discovered in NDP."""
+ return _ipv6_address_to_prefix(gateway, prefixlen) == _ipv6_address_to_prefix(
+ address, prefixlen
+ )
diff --git a/_tests/modules/test_network.py b/_tests/modules/test_network.py
--- a/_tests/modules/test_network.py
+++ b/_tests/modules/test_network.py
@@ -48,6 +48,28 @@
def test_is_valid_netmask_when_it_is_not(self, netmask):
self.assertFalse(network.is_valid_netmask(netmask))
+ def test_ipv6_address_to_prefix(self):
+ prefix = network._ipv6_address_to_prefix(
+ "2001:41d0:0303:d9ff:00ff:00ff:00ff:00ff", 64
+ )
+ self.assertEqual("2001:41d0:303:d9ff::", prefix.network_address.compressed)
+
+ def test_can_directly_be_discovered(self):
+ self.assertFalse(
+ network.can_directly_be_discovered(
+ "2001:41d0:0303:d9ff:00ff:00ff:00ff:00ff",
+ "2001:41d0:303:d971::517e:c0de",
+ 64,
+ )
+ )
+ self.assertTrue(
+ network.can_directly_be_discovered(
+ "2001:41d0:0303:d971:00ff:00ff:00ff:00ff",
+ "2001:41d0:303:d971::517e:c0de",
+ 64,
+ )
+ )
+
if __name__ == "__main__":
unittest.main()

File Metadata

Mime Type
text/plain
Expires
Mon, Nov 18, 01:26 (3 h, 26 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2249970
Default Alt Text
D3077.id7853.diff (2 KB)

Event Timeline