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