Module:IP: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
make private constructors for IPAddress and Subnet strings as well as for RawIPs
(add type checks to IPAddress)
(make private constructors for IPAddress and Subnet strings as well as for RawIPs)
Line 320:
 
--------------------------------------------------------------------------------
-- need to beInitialize private. Hence the variables holding the constructor functions
-- Set up classes to be exported
--
-- Both IPAddress and Subnet need access to each others' private constructor
-- functions. IPAddress must be able to make Subnet objects from CIDR strings
-- and from RawIP objects, and Subnet must be able to make IPAddress objects
-- from IP strings and from RawIP objects. These constructors must all be
-- private to ensure correct error levels and to stop other modules from having
-- to worry about RawIP objects. Because they are private, they must be
-- need to be initialized here as well.
--------------------------------------------------------------------------------
 
local makeIPAddress, makeIPAddressFromRaw, makeSubnet, makeSubnetFromRaw
-- 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
-- needs to create IPAddress objects with its RawIP data as well. However,
-- 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
 
--------------------------------------------------------------------------------
Line 339 ⟶ 337:
-- Represents a single IPv4 or IPv6 address.
--------------------------------------------------------------------------------
 
local IPAddress, Subnet = {}, {}
 
do
Line 378:
 
-- Constructors
makeIPAddressmakeIPAddressFromRaw = function (rawIP)
-- 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 420:
checkSelf('getSubnet', self)
checkType('getSubnet', 1, bitLength, 'number')
return makeSubnetmakeSubnetFromRaw(data.rawIP, bitLength)
end
 
function obj:getNextIP()
checkSelf('getNextIP', self)
return makeIPAddressmakeIPAddressFromRaw(data.rawIP:getAdjacent())
end
 
function obj:getPreviousIP()
checkSelf('getPreviousIP', self)
return makeIPAddressmakeIPAddressFromRaw(data.rawIP:getAdjacent(true))
end
 
Line 449:
end
 
makeIPAddress = function IPAddress.new(ip)
checkType('IPAddress.new', 1, ip, 'string')
local rawIP = RawIP.newFromIP(ip)
if not rawIP then
error(string.format("'%s' is an invalid IP", ip), 23)
end
return makeIPAddressmakeIPAddressFromRaw(rawIP)
end
 
function IPAddress.new(ip)
checkType('IPAddress.new', 1, ip, 'string')
return makeIPAddress(ip)
end
end
Line 463 ⟶ 467:
-- Represents a block of IPv4 or IPv6 addresses.
--------------------------------------------------------------------------------
 
local Subnet = {}
 
do
Line 469 ⟶ 475:
 
-- Constructors
makeSubnetmakeSubnetFromRaw = function (rawIP, bitLength)
-- Set up structure
local obj = setmetatable({}, mt)
Line 480 ⟶ 486:
function obj:getPrefix()
if not data.prefix then
data.prefix = makeIPAddressmakeIPAddressFromRaw(
data.rawIP:getPrefix(data.bitLength)
)
Line 489 ⟶ 495:
function obj:getHighestIP()
if not data.highestIP then
data.highestIP = makeIPAddressmakeIPAddressFromRaw(
data.rawIP:getHighestHost(data.bitLength)
)
Line 555 ⟶ 561:
end
 
makeSubnet = function Subnet.new(cidr)
checkType('Subnet.new', 1, cidr, 'string')
local lhs, rhs = cidr:match('^%s*(.-)/(%d+)%s*$')
if lhs then
Line 565 ⟶ 570:
local prefix = base:getPrefix(n)
if base == prefix then
return makeSubnetmakeSubnetFromRaw(prefix, n)
end
end
end
error(string.format("'%s' is an invalid CIDR string", cidr), 23)
end
 
function Subnet.new(cidr)
checkType('Subnet.new', 1, cidr, 'string')
return makeSubnet(cidr)
end
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu