add calc_dip function

This commit is contained in:
Pavel Patsey
2025-01-23 10:15:30 +03:00
parent 81f741ee4e
commit 474b6779d8
2 changed files with 15 additions and 0 deletions
+7
View File
@@ -2,6 +2,7 @@ import pytest
from vpn_manager.cidr4_merge.cidr4_merger import (
Cidr4MergerError,
calc_dip,
cidr4_to_node,
find_parent,
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)."
)
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
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:
lst = [str(ip >> (i << 3) & 0xFF) for i in reversed(range(4))]
ip_address = ".".join(lst)