mirror of
https://github.com/nmap/nmap.git
synced 2025-12-27 01:49:03 +00:00
New script broadcast-ospf2-discover. Closes #743
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
--
|
||||
-- @copyright Same as Nmap--See https://nmap.org/book/man-legal.html
|
||||
|
||||
local math = require "math"
|
||||
local stdnse = require "stdnse"
|
||||
local string = require "string"
|
||||
local table = require "table"
|
||||
@@ -674,6 +675,32 @@ hex_to_bin = function( hex )
|
||||
return status, result
|
||||
end
|
||||
|
||||
---
|
||||
-- Convert a CIDR subnet mask to dotted decimal notation.
|
||||
--
|
||||
-- @param subnet CIDR string representing the subnet mask.
|
||||
-- @usage
|
||||
-- local netmask = ipOps.cidr_to_subnet( "/16" )
|
||||
-- @return Dotted decimal representation of the suppliet subnet mask (e.g. "255.255.0.0")
|
||||
cidr_to_subnet = function( subnet )
|
||||
local bits = subnet:match("/(%d%d)$")
|
||||
if not bits then return nil end
|
||||
return fromdword((0xFFFFFFFF >> tonumber(bits)) ~ 0xFFFFFFFF)
|
||||
end
|
||||
|
||||
---
|
||||
-- Convert a dotted decimal subnet mask to CIDR notation.
|
||||
--
|
||||
-- @param subnet Dotted decimal string representing the subnet mask.
|
||||
-- @usage
|
||||
-- local cidr = ipOps.subnet_to_cidr( "255.255.0.0" )
|
||||
-- @return CIDR representation of the supplied subnet mask (e.g. "/16").
|
||||
subnet_to_cidr = function( subnet )
|
||||
local dword, err = todword(subnet)
|
||||
if not dword then return nil, err end
|
||||
return "/" .. tostring(32 - (math.tointeger(math.log((dword ~ 0xFFFFFFFF) + 1, 2))))
|
||||
end
|
||||
|
||||
--Ignore the rest if we are not testing.
|
||||
if not unittest.testing() then
|
||||
return _ENV
|
||||
@@ -801,5 +828,7 @@ do
|
||||
test_suite:add_test(unittest.is_nil(expand_ip("2001:db8::1", "ipv4")),
|
||||
"IPv6 to IPv4")
|
||||
end
|
||||
test_suite:add_test(unittest.equal(cidr_to_subnet("/16"), "255.255.0.0"), "cidr_to_subnet")
|
||||
test_suite:add_test(unittest.equal(subnet_to_cidr("255.255.0.0"), "/16"), "subnet_to_cidr")
|
||||
|
||||
return _ENV;
|
||||
|
||||
Reference in New Issue
Block a user