Skip to content

Commit

Permalink
Merge pull request #252 from treviza153/bugfix/CheckPrefix
Browse files Browse the repository at this point in the history
Checking prefix
  • Loading branch information
treviza153 authored Jun 2, 2020
2 parents f04e3e4 + bb93df4 commit 155bd38
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions networkapi/ambiente/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1929,6 +1929,19 @@ def check_cidr(self, environment, network):

return False

def check_prefix(self, network_address, subnet_mask):
"""
check if subnet mask is correct, based on network mask.
:param network_address: environment cidr
:param subnet_mask: environment cidr subnet mask
:return: boolean
"""

network = ipaddr.IPNetwork(network_address)

return True if int(network.prefixlen) <= int(subnet_mask) else False

def check_duplicated_cidr(self, environment, network):
"""
check if the network overlaps another cidr from another environment.
Expand Down Expand Up @@ -2020,6 +2033,9 @@ def post(self, env_cidr):
self.ip_version = env_cidr.get('ip_version')
self.subnet_mask = env_cidr.get('subnet_mask')

if not self.check_prefix(self.network, self.subnet_mask):
raise CIDRErrorV3("The prefix %s is not valid for the network %s" % (self.subnet_mask, self.network))

objects = EnvCIDR.objects.filter(id_env=int(env_cidr.get('environment')))
for obj in objects:
if ipaddr.IPNetwork(obj.network).overlaps(ipaddr.IPNetwork(self.network)):
Expand Down

0 comments on commit 155bd38

Please sign in to comment.