API: Better free range finding algorithm

This commit is contained in:
Sebastian Lohff 2017-04-13 00:58:18 +02:00
parent c0a65be9d0
commit f8ed94d725
1 changed files with 21 additions and 2 deletions

View File

@ -8,6 +8,9 @@ from whoisdb.models import ASBlock, ASNumber, InetNum
from domains.models import Domain, ReverseZone from domains.models import Domain, ReverseZone
from dnmgmt.settings import TLD_NAMESERVERS from dnmgmt.settings import TLD_NAMESERVERS
import ipaddress
@login_required @login_required
def asblockFreeAS(request): def asblockFreeAS(request):
@ -91,12 +94,28 @@ def freeSubnet(request):
raise ValidationError("PrefixLen is not a number") raise ValidationError("PrefixLen is not a number")
usableNet = None usableNet = None
# FIXME: use first biggest usable netblock... # FIXME: use first biggest usable netblock...
#biggestNet = parentRange.inetnum_set.order_by("-address") #biggestNet = parentRange.inetnum_set.order_by("-address")
#if biggestNet #if biggestNet
# candidateNet = # candidateNet =
# and (biggestNet.getNetwork().broadcast_address.: # 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: if not usableNet:
# search for free network # search for free network
nets = list(parentRange.getNetwork().subnets()) nets = list(parentRange.getNetwork().subnets())