Cidr4 merge algorithm #5

Merged
PavelPatsey merged 91 commits from CIDR4_merge_algorithm into main 2025-01-27 22:05:39 +03:00
2 changed files with 15 additions and 0 deletions
Showing only changes of commit 474b6779d8 - Show all commits
+7
View File
@@ -2,6 +2,7 @@ import pytest
from vpn_manager.cidr4_merge.cidr4_merger import ( from vpn_manager.cidr4_merge.cidr4_merger import (
Cidr4MergerError, Cidr4MergerError,
calc_dip,
cidr4_to_node, cidr4_to_node,
find_parent, find_parent,
make_cidr4, make_cidr4,
@@ -84,3 +85,9 @@ def test_find_parent__with_exception():
== "Error! Trying to find common parent of network and subnet! parent_node=(0, 0), a=(0, 0), b=(3221225472, 2)." == "Error! Trying to find common parent of network and subnet! parent_node=(0, 0), a=(0, 0), b=(3221225472, 2)."
) )
assert exc_info.type is Cidr4MergerError assert exc_info.type is Cidr4MergerError
def test_calc_dip():
assert calc_dip(26, 27) == 67108864
assert calc_dip(26, 28) == 201326592
assert calc_dip(26, 29) == 469762048
+8
View File
@@ -42,6 +42,14 @@ def find_parent(a: Node, b: Node) -> Node:
return parent_node return parent_node
def calc_dip(mask_len_a: int, mask_len_b: int) -> int:
dip = 0
while mask_len_a < mask_len_b:
dip += 1 << mask_len_a
mask_len_a += 1
return dip
def make_cidr4(ip, mask_len) -> str: def make_cidr4(ip, mask_len) -> str:
lst = [str(ip >> (i << 3) & 0xFF) for i in reversed(range(4))] lst = [str(ip >> (i << 3) & 0xFF) for i in reversed(range(4))]
ip_address = ".".join(lst) ip_address = ".".join(lst)