Module:IP: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
1,343 bytes added ,  7 years ago
need to use __index to have a unique key which is really private - if outside code gets uniqueKey with getmetatable then they can set it in another table's __eq metamethod, thus foiling isSubnetObject
(use libraryUtil for the checkSelf function)
(need to use __index to have a unique key which is really private - if outside code gets uniqueKey with getmetatable then they can set it in another table's __eq metamethod, thus foiling isSubnetObject)
 
(7 intermediate revisions by 2 users not shown)
Line 488:
-- uniqueKey is a unique, private key used to test whether a given object
-- is a Subnet object.
local uniqueKey = setmetatable({}, {__metatable = false})
 
-- Metatable
local mt = {
__eq = function (self, obj)
return self:getCIDR() == obj:getCIDR()
end,
__tostring = function (self)
return self:getCIDR()
end,
__metatable = uniqueKey
}
 
-- Private static methods
Line 513 ⟶ 502:
-- arg - the argument to be validated
validateSubnet = makeValidationFunction('Subnet', isSubnetObject)
 
-- Metamethods that don't need upvalues
local function subnetEquals(subnet1, subnet2)
return selfsubnet1:getCIDR() == objsubnet2:getCIDR()
end
 
local function concatenateSubnets(subnet1, subnet2)
return tostring(subnet1) .. tostring(subnet2)
end
 
local function subnetToString(subnet)
return selfsubnet:getCIDR()
end
 
-- Constructors
makeSubnetFromRaw = function (rawIP, bitLength)
-- Set up structure
local obj = setmetatable({}, mt)
local data = {
rawIP = rawIP,
bitLength = bitLength,
}
 
-- A function to check whether methods are called with a valid self
-- parameter.
local checkSelf = makeCheckSelfFunction(
'IP',
'subnet',
obj,
'Subnet object'
)
 
-- Public methods
function obj:getPrefix()
checkSelf(self, 'getPrefix')
if not data.prefix then
data.prefix = makeIPAddressFromRaw(
Line 534 ⟶ 546:
 
function obj:getHighestIP()
checkSelf(self, 'getHighestIP')
if not data.highestIP then
data.highestIP = makeIPAddressFromRaw(
Line 543 ⟶ 556:
 
function obj:getBitLength()
checkSelf(self, 'getBitLength')
return data.bitLength
end
 
function obj:getCIDR()
checkSelf(self, 'getCIDR')
return string.format(
'%s/%d',
Line 554 ⟶ 569:
 
function obj:getVersion()
checkSelf(self, 'getVersion')
return data.rawIP:getVersion()
end
 
function obj:isIPv4()
checkSelf(self, 'isIPv4')
return data.rawIP:isIPv4()
end
 
function obj:isIPv6()
checkSelf(self, 'isIPv6')
return data.rawIP:isIPv6()
end
 
function obj:containsIP(ip)
checkSelf(self, 'containsIP')
local mttp = {type(ip)
if tp == 'string' then
ip = makeIPAddress(ip)
elseif tp == 'table' then
validateIPAddress('containsIP', 1, ip)
else
checkTypeMulti('containsIP', 1, ip, {'string', 'table'})
end,
if self:getVersion() == ip:getVersion() then
return self:getPrefix() <= ip and ip <= self:getHighestIP()
Line 573 ⟶ 600:
 
function obj:overlapsSubnet(subnet)
checkSelf(self, 'overlapsSubnet')
local tp = type(subnet)
if tp == 'string' then
subnet = makeSubnet(subnet)
elseif tp == 'table' then
validateSubnet('overlapsSubnet', 1, subnet)
else
checkTypeMulti('overlapsSubnet', 1, subnet, {'string', 'table'})
end
if self:getVersion() == subnet:getVersion() then
return (
Line 583 ⟶ 619:
 
function obj:walk()
checkSelf(self, 'walk')
local started
local current = self:getPrefix()
Line 598 ⟶ 635:
end
 
return setmetatable(obj, {
__eq __index = function (self, objkey)
if key == uniqueKey then
return true
end
end,
__eq = subnetEquals,
__concat = concatenateSubnets,
__tostring = function (self)subnetToString,
__metatable = uniqueKeyfalse,
})
end
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu