1 from resources
.net
import Net
4 lock
= threading
.Lock()
7 __default_subnet_size
= 256
8 __default_subnet_bitmask
= 24
9 __first_ip
= Net
.ip_2_int('10.0.0.0')
10 __last_ip
= Net
.ip_2_int('10.255.255.255')
11 __current_ip
= __first_ip
14 def get_new_cidr(uuid
):
16 Calculates a unused cidr for a subnet.
18 :param uuid: The UUID of the subnet - Thus it can store which subnet gets which CIDR
20 :return: Returns None if all available CIDR are used. Otherwise returns a valid CIDR.
27 while __first_ip
<= __current_ip
< __last_ip
and __current_ip
in __issued_ips
:
28 __current_ip
+= __default_subnet_size
30 if __current_ip
>= __last_ip
or __current_ip
< __first_ip
or __current_ip
in __issued_ips
:
33 __issued_ips
[__current_ip
] = uuid
36 return Net
.int_2_ip(__current_ip
) + '/' + str(__default_subnet_bitmask
)
39 def free_cidr(cidr
, uuid
):
41 Frees a issued CIDR thus it can be reused.
43 :param cidr: The currently used CIDR.
45 :param uuid: The UUID of the Subnet, which uses this CIDR.
47 :return: Returns False if the CIDR is None or the UUID did not correspond tho the used CIDR. Else it returns True.
54 int_ip
= Net
.cidr_2_int(cidr
)
59 if int_ip
in __issued_ips
and __issued_ips
[int_ip
] == uuid
:
60 del __issued_ips
[int_ip
]
61 if int_ip
< __current_ip
:
69 def is_cidr_issued(cidr
):
71 Returns True if the CIDR is used.
73 :param cidr: The requested CIDR.
75 :return: Returns True if the CIDR is used, else False.
81 int_ip
= Net
.cidr_2_int(cidr
)
83 if int_ip
in __issued_ips
:
88 def is_my_cidr(cidr
, uuid
):
90 Checks if the UUID and the used CIDR are related.
92 :param cidr: The issued CIDR.
94 :param uuid: The Subnet UUID.
96 :return: Returns False if the CIDR is None or if the CIDR is not issued. Else returns True.
102 int_ip
= Net
.cidr_2_int(cidr
)
104 if not int_ip
in __issued_ips
:
107 if __issued_ips
[int_ip
] == uuid
:
112 def assign_cidr(cidr
, uuid
):
114 Allows a subnet to request a specific CIDR.
116 :param cidr: The requested CIDR.
118 :param uuid: The Subnet UUID.
120 :return: Returns False if the CIDR is None or if the CIDR is already issued. Returns True if the CIDR could be
121 assigned to the UUID.
126 int_ip
= Net
.cidr_2_int(cidr
)
128 if int_ip
in __issued_ips
:
133 __issued_ips
[int_ip
] = uuid