Anonymous user
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:
--------------------------------------------------------------------------------
--
-- 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
--------------------------------------------------------------------------------
local makeIPAddress, makeIPAddressFromRaw, makeSubnet, makeSubnetFromRaw▼
local IPAddress, Subnet = {}, {}▼
▲-- 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.
--------------------------------------------------------------------------------
do
Line 378:
-- 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 420:
checkSelf('getSubnet', self)
checkType('getSubnet', 1, bitLength, 'number')
return
end
function obj:getNextIP()
checkSelf('getNextIP', self)
return
end
function obj:getPreviousIP()
checkSelf('getPreviousIP', self)
return
end
Line 449:
end
makeIPAddress = function
checkType('IPAddress.new', 1, ip, 'string')▼
local rawIP = RawIP.newFromIP(ip)
if not rawIP then
error(string.format("'%s' is an invalid IP", ip),
end
return
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
-- Set up structure
local obj = setmetatable({}, mt)
Line 480 ⟶ 486:
function obj:getPrefix()
if not data.prefix then
data.prefix =
data.rawIP:getPrefix(data.bitLength)
)
Line 489 ⟶ 495:
function obj:getHighestIP()
if not data.highestIP then
data.highestIP =
data.rawIP:getHighestHost(data.bitLength)
)
Line 555 ⟶ 561:
end
makeSubnet = function
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
end
end
end
error(string.format("'%s' is an invalid CIDR string", cidr),
end
function Subnet.new(cidr)
▲ checkType('Subnet.new', 1, cidr, 'string')
return makeSubnet(cidr)
end
|