Anonymous user
Module:IP: Difference between revisions
From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
add more methods to RawIP and rework IPAddress and Subnet to use them
m (comment and variable tweaks) |
(add more methods to RawIP and rework IPAddress and Subnet to use them) |
||
Line 3:
-- Load modules
require('Module:No globals')
local bit32 = require('bit32')
local libraryUtil = require('libraryUtil')
Line 45 ⟶ 46:
-- Constructors
function RawIP.newFromIPv4(ipStr)
--
--
-- This representation is for compatibility with IPv6 addresses.
local octets = Collection()
Line 100 ⟶ 101:
parts[i] = num
else
return
end
end
Line 107 ⟶ 108:
end
return nil
end
function RawIP.newFromIP(ipStr)
-- Return a new RawIP object from either an IPv4 string or an IPv6
-- string. If ipStr is not a valid IPv4 or IPv6 string, then return
-- nil.
return RawIP.newFromIPv4(ipStr) or RawIP.newFromIPv6(ipStr)
end
-- Methods
function RawIP:getVersion()
return self.n == 2 and V4 or V6
end
function RawIP:isIPv4()
return self.n == 2
end
function RawIP:isIPv6()
return self.n == 8
end
function RawIP:getAdjacent(previous)
-- Return a RawIP object for an adjacent IP address. If previous is true
Line 185 ⟶ 205:
end
end
return setmetatable(result,
end
Line 280 ⟶ 300:
--------------------------------------------------------------------------------
-- Set up classes to be exported
--------------------------------------------------------------------------------
-- The IPAddress class and the Subnet class should be accessible from each
-- other, but should not be accessible from the RawIP class, so initialize them
-- here.
local IPAddress, Subnet = {}, {}
-- IPAddress needs to create Subnet objects using its RawIP data, and Subnet
--
-- other modules shouldn't have to worry about RawIP data, so the constructors
-- need to be private. Hence the variables holding the constructor functions
-- need to be initialized here as well.
local makeIPAddress, makeSubnet
--------------------------------------------------------------------------------
-- IPAddress class
-- Represents a single IPv4 or IPv6 address.
--------------------------------------------------------------------------------
do
Line 311 ⟶ 341:
-- Constructors
-- Constructs a new IPAddress object from a rawIP object. This function
-- is for internal use; it is called by IPAddress.new and from other
Line 320 ⟶ 350:
-- Set up structure
local obj = {}
local data = {}
-- Public methods
Line 331 ⟶ 359:
function obj:getVersion()
return data.
end
function obj:isIPv4()
return data.
end
function obj:isIPv6()
return data.
end
function obj:isInSubnet(subnet)
return subnet:containsIP(self)
end
function obj:getNextIP()
return
end
function obj:getPreviousIP()
return
end
Line 384 ⟶ 398:
function IPAddress.new(ip)
checkType('IPAddress.new', 1, ip, 'string')
local rawIP = RawIP.
if not rawIP then
error(string.format("'%s' is an invalid IP
end
return
end
end
Line 417 ⟶ 410:
-- Represents a block of IPv4 or IPv6 addresses.
--------------------------------------------------------------------------------
do
Line 424 ⟶ 415:
local mt = {}
--
makeSubnet = function
-- Set up structure
local obj = setmetatable({}, mt)
local data = {
rawIP = rawIP,
bitLength = bitLength,
}
-- Public methods
function obj:getPrefix()
if not data.prefix then
data.prefix = makeIPAddress(
data.rawIP:getPrefix(data.bitLength)
)
end
return data.prefix
end
function obj:getHighestIP()
if not data.highestIP then
data.highestIP = makeIPAddress(
data.rawIP:getHighestHost(data.bitLength)
)
end
return data.highestIP
end
Line 444 ⟶ 448:
function obj:getCIDR()
return string.format(
'%s/%d',
tostring(self:getPrefix()), self:getBitLength()
)
end
function obj:getVersion()
return data.
end
function obj:isIPv4()
return data.
end
function obj:isIPv6()
return data.
end
function obj:containsIP(ip)
if self:getVersion() == ip:getVersion() then
return self:getPrefix() <= ip and ip <= self:getHighestIP()
Line 477 ⟶ 483:
end
return obj
end
function Subnet.new(cidr)
checkType('Subnet.new', 1, cidr, 'string')
local lhs, rhs = cidr:match('^%s*(.-)/(%d+)%s*$')
if
local bits = lhs:find(':', 1, true) and 128 or 32
local n = tonumber(rhs)
if n and n <= bits then
local base = RawIP.newFromIP(lhs)
local prefix = base:getPrefix(n)
if base == prefix then
return makeSubnet(prefix, n)
end
end
end
error(string.format("'%s' is an invalid CIDR string", cidr), 2)
end
|