From 1e1f7c80c20e53bee3106bffb1d5d4d7a9ce25f0 Mon Sep 17 00:00:00 2001 From: nnposter Date: Sun, 5 Aug 2018 21:29:37 +0000 Subject: [PATCH] Simplifies code; possibly ipOps candidate --- scripts/asn-query.nse | 17 ++++++----------- scripts/whois-ip.nse | 17 ++++++----------- 2 files changed, 12 insertions(+), 22 deletions(-) diff --git a/scripts/asn-query.nse b/scripts/asn-query.nse index 082499c82..db6fd6d8c 100644 --- a/scripts/asn-query.nse +++ b/scripts/asn-query.nse @@ -366,21 +366,16 @@ function get_prefix_length( range ) local first, last, err = ipOps.get_ips_from_range( range ) if err then return nil end - first = ipOps.ip_to_bin( first ):reverse() - last = ipOps.ip_to_bin( last ):reverse() + first = ipOps.ip_to_bin(first) + last = ipOps.ip_to_bin(last) - local hostbits = 0 - for pos = 1, # first , 1 do - - if first:sub( pos, pos ) == "0" and last:sub( pos, pos ) == "1" then - hostbits = hostbits + 1 - else - break + for pos = 1, #first do + if first:sub(pos, pos) ~= last:sub(pos, pos) then + return pos - 1 end - end - return ( # first - hostbits ) + return #first end diff --git a/scripts/whois-ip.nse b/scripts/whois-ip.nse index fc7ac7465..e5afb9cf4 100644 --- a/scripts/whois-ip.nse +++ b/scripts/whois-ip.nse @@ -434,21 +434,16 @@ function get_prefix_length( range ) local first, last, err = ipOps.get_ips_from_range( range ) if err then return nil end - first = ipOps.ip_to_bin( first ):reverse() - last = ipOps.ip_to_bin( last ):reverse() + first = ipOps.ip_to_bin(first) + last = ipOps.ip_to_bin(last) - local hostbits = 0 - for pos = 1, string.len( first ), 1 do - - if first:sub( pos, pos ) == "0" and last:sub( pos, pos ) == "1" then - hostbits = hostbits + 1 - else - break + for pos = 1, #first do + if first:sub(pos, pos) ~= last:sub(pos, pos) then + return pos - 1 end - end - return ( string.len( first ) - hostbits ) + return #first end