From 61ebe3fabb8fecd5806d878ae2c3585ef5a67eea Mon Sep 17 00:00:00 2001 From: Clauz Date: Sat, 16 Nov 2013 13:57:31 +0000 Subject: [PATCH] read_topology_hna.py: better isinsubnet() function --- nodeshot/scripts/read_topology_hna.py | 55 +++++++++------------------ 1 file changed, 19 insertions(+), 36 deletions(-) diff --git a/nodeshot/scripts/read_topology_hna.py b/nodeshot/scripts/read_topology_hna.py index 005258d4..9721d5e2 100755 --- a/nodeshot/scripts/read_topology_hna.py +++ b/nodeshot/scripts/read_topology_hna.py @@ -180,44 +180,27 @@ def isinsubnet(ip, subnet, mask_threshold): If the subnet mask is less than "mask_threshold" then return False. """ - # take the IPv4 address and transform it into binary - try: - ibs = ip.split(".") - except: + # validation + if ip == None or subnet == None or len(ip) < 7 or len(subnet) < 9: return False - bip = 0 - for ib in ibs: - bip |= int(ib) - bip = bip << 8 - bip = bip >> 8 - - # the parsed IP address is now in bip - try: - net = subnet.split("/")[0] - mask = subnet.split("/")[1] - nbs = net.split(".") - except: + # TODO: more validation + # separate the net from the netmask + net = subnet.split("/")[0] + mask = int(subnet.split("/")[1]) + # check if the mask is below mask_threshold + if mask < mask_threshold: return False - bnet = 0 - for nb in nbs: - bnet |= int(nb) - bnet = bnet << 8 - bnet = bnet >> 8 - - intmask = int(mask) - if intmask < mask_threshold: - return False - - bmask = 0 - for i in range(32): - if i < intmask: - bmask |= 1 - bmask = bmask << 1 - bmask = bmask >> 1 - - #print "%s %s %s" % (hex(bip), hex(bnet), hex(bmask)) - - return bip & bmask == bnet & bmask + # convert the net into an int + netb = [int(b) for b in net.split(".")] + intnet = sum([octect << lshift for (octect, lshift) in zip(netb, range(24, -1, -8))]) + # convert the netmask into an int + maskbits = [1] * mask + intmask = sum([octect << lshift for (octect, lshift) in zip(maskbits, range(31, 31 - mask, -1))]) + # convert the IPv4 address into an int + ipb = [int(b) for b in ip.split(".")] + intip = sum([octect << lshift for (octect, lshift) in zip(ipb, range(24, -1, -8))]) + + return intip & intmask == intnet & intmask #OLSR