From f8ed94d72589ff24f176b3f6f6515c4601600dfc Mon Sep 17 00:00:00 2001 From: Sebastian Lohff Date: Thu, 13 Apr 2017 00:58:18 +0200 Subject: [PATCH] API: Better free range finding algorithm --- api/views.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/api/views.py b/api/views.py index 7b6a891..9a72336 100644 --- a/api/views.py +++ b/api/views.py @@ -8,6 +8,9 @@ from whoisdb.models import ASBlock, ASNumber, InetNum from domains.models import Domain, ReverseZone from dnmgmt.settings import TLD_NAMESERVERS +import ipaddress + + @login_required def asblockFreeAS(request): @@ -91,12 +94,28 @@ def freeSubnet(request): raise ValidationError("PrefixLen is not a number") usableNet = None - # FIXME: use first biggest usable netblock... + # FIXME: use first biggest usable netblock... #biggestNet = parentRange.inetnum_set.order_by("-address") #if biggestNet - # candidateNet = + # candidateNet = # and (biggestNet.getNetwork().broadcast_address.: + # try using next network in range + # ordering does not work, as order_by sorts alphabetically + # ==> the set is short, we can iterate through all. no sense in unpacking the SQL magic + #biggestNets = parentRange.inetnum_set.order_by("-address") + biggestNet = None + for ipRange in parentRange.inetnum_set.all(): + if not biggestNet or ipRange.getNetwork() > biggestNet: + biggestNet = ipRange.getNetwork() + + if biggestNet: + candidateNet = ipaddress.ip_network("%s/%s" % (biggestNet.broadcast_address + 1, biggestNet.prefixlen)) + print("biggest net", biggestNet, "candidate", candidateNet) + if candidateNet.network_address in parentRange.getNetwork(): + usableNet = candidateNet + + # check if there are still networks left in range if not usableNet: # search for free network nets = list(parentRange.getNetwork().subnets())