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 dnmgmt.settings import TLD_NAMESERVERS
import ipaddress
@login_required
def asblockFreeAS(request):
@ -97,6 +100,22 @@ def freeSubnet(request):
# 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())