Module:IP: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
2,680 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
(allow strings to isInSubnet)
(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)
 
(15 intermediate revisions by 2 users not shown)
Line 8:
local checkType = libraryUtil.checkType
local checkTypeMulti = libraryUtil.checkTypeMulti
local makeCheckSelfFunction = libraryUtil.makeCheckSelfFunction
 
-- Constants
Line 17 ⟶ 18:
--------------------------------------------------------------------------------
 
local function makeCheckSelfFuncmakeValidationFunction(className, objectName, isSelfFuncisObjectFunc)
-- MakesMake a function for classesvalidating toa check that their methods have aspecific validobject.
return function (methodName, argIdx, arg)
-- self parameter.
if not isObjectFunc(arg) then
return function (methodName, selfObject)
if not isSelfFunc(selfObject) then
error(string.format(
"bad argument #%d to '%s' (not a valid %s object)",
'invalid %s object. Did you call %s with a dot instead of a colon, i.e. %s.%s() instead of %s:%s()?',
classNameargIdx, methodName, objectName, methodName, objectName, methodNameclassName
), 3)
end
Line 320:
 
--------------------------------------------------------------------------------
-- Initialize private constructormethods functionsavailable to IPAddress and Subnet
--------------------------------------------------------------------------------
--
 
-- Both IPAddress and Subnet need access to each others' private constructor
-- functions. IPAddress must be able to make Subnet objects from CIDR strings
Line 329 ⟶ 330:
-- to worry about RawIP objects. Because they are private, they must be
-- initialized here.
--------------------------------------------------------------------------------
 
local makeIPAddress, makeIPAddressFromRaw, makeSubnet, makeSubnetFromRaw
 
-- IPAddress and Subnet also need to be able to validate Subnet and IPAddress
-- objects that they are passed as input, so initialize those functions here
-- as well.
local validateIPAddress, validateSubnet
 
--------------------------------------------------------------------------------
Line 352 ⟶ 356:
end
 
validateIPAddress = makeValidationFunction('IPAddress', isIPAddressObject)
-- A function to check whether methods are called with a valid self
-- parameter.
local checkSelf = makeCheckSelfFunc(
'IPAddress',
'ipAddress',
isIPAddressObject
)
 
-- Metamethods that don't need upvalues
Line 389 ⟶ 387:
local data = {}
data.rawIP = rawIP
 
-- A function to check whether methods are called with a valid self
-- parameter.
local checkSelf = makeCheckSelfFunction(
'IP',
'ipAddress',
obj,
'IPAddress object'
)
 
-- Public methods
function obj:getIP()
checkSelf(self, 'getIP', self)
return tostring(data.rawIP)
end
 
function obj:getVersion()
checkSelf(self, 'getVersion', self)
return data.rawIP:getVersion()
end
 
function obj:isIPv4()
checkSelf(self, 'isIPv4', self)
return data.rawIP:isIPv4()
end
 
function obj:isIPv6()
checkSelf(self, 'isIPv6', self)
return data.rawIP:isIPv6()
end
 
function obj:isInSubnet(subnet)
checkSelf(self, 'isInSubnet', self)
local tp = type(subnet)
if tp == 'string' then
subnet = makeSubnet(subnet)
elseif tp == 'table' then
validateSubnet('isInSubnet', 1, subnet)
-- TODO: check if Subnet object
else
checkTypeMulti('isInSubnet', 1, subnet, {'string', 'table'})
Line 425 ⟶ 432:
 
function obj:getSubnet(bitLength)
checkSelf(self, 'getSubnet', self)
checkType('getSubnet', 1, bitLength, 'number')
return makeSubnetFromRaw(data.rawIP, bitLength)
Line 431 ⟶ 438:
 
function obj:getNextIP()
checkSelf(self, 'getNextIP', self)
return makeIPAddressFromRaw(data.rawIP:getAdjacent())
end
 
function obj:getPreviousIP()
checkSelf(self, 'getPreviousIP', self)
return makeIPAddressFromRaw(data.rawIP:getAdjacent(true))
end
Line 453 ⟶ 460:
end
end,
__metatable = false, -- Don't allow access to the metatable
})
end
Line 459 ⟶ 467:
local rawIP = RawIP.newFromIP(ip)
if not rawIP then
error(string.format("'%s' is an invalid IP address", ip), 3)
end
return makeIPAddressFromRaw(rawIP)
Line 478 ⟶ 486:
 
do
-- uniqueKey is a unique, private key used to test whether a given object
-- Initialize metatable
-- is a Subnet object.
local mt = {}
local uniqueKey = setmetatable({}, {__metatable = false})
 
-- Private static methods
local function isSubnetObject(val)
-- Return true if val is a Subnet object, and false otherwise.
return getmetatable(val) == uniqueKey
end
 
-- Function to validate subnet objects.
-- Params:
-- methodName (string) - the name of the method being validated
-- argIdx (number) - the position of the argument in the argument list
-- arg - the argument to be validated
validateSubnet = makeValidationFunction('Subnet', isSubnetObject)
 
-- Metamethods that don't need upvalues
local function subnetEquals(subnet1, subnet2)
return subnet1:getCIDR() == subnet2:getCIDR()
end
 
local function concatenateSubnets(subnet1, subnet2)
return tostring(subnet1) .. tostring(subnet2)
end
 
local function subnetToString(subnet)
return subnet: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 501 ⟶ 546:
 
function obj:getHighestIP()
checkSelf(self, 'getHighestIP')
if not data.highestIP then
data.highestIP = makeIPAddressFromRaw(
Line 510 ⟶ 556:
 
function obj:getBitLength()
checkSelf(self, 'getBitLength')
return data.bitLength
end
 
function obj:getCIDR()
checkSelf(self, 'getCIDR')
return string.format(
'%s/%d',
Line 521 ⟶ 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 tp = 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 540 ⟶ 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 550 ⟶ 619:
 
function obj:walk()
checkSelf(self, 'walk')
local started
local current = self:getPrefix()
Line 565 ⟶ 635:
end
 
return setmetatable(obj, {
__index = function (self, key)
if key == uniqueKey then
return true
end
end,
__eq = subnetEquals,
__concat = concatenateSubnets,
__tostring = subnetToString,
__metatable = false,
})
end
 
makeSubnet = function (cidr)
-- Return a Subnet object from a CIDR string. If the CIDR string is
-- invalid, throw an error.
local lhs, rhs = cidr:match('^%s*(.-)/(%d+)%s*$')
if lhs then
local bits = lhs:find(':', 1, true) and 128 or 32
local n = tonumber(rhs)
if n and n <= bits and (n == 0 or not rhs:find('^0')) then
-- The right-hand side is a number between 0 and 32 (for IPv4)
-- or 0 and 128 (for IPv6) and doesn't have any leading zeroes.
local base = RawIP.newFromIP(lhs)
if base then
local prefix = base:getPrefix(n)
-- The left-hand side is a valid IP address.
if base == prefix then
returnlocal makeSubnetFromRaw(prefix, = base:getPrefix(n)
if base == prefix then
-- The left-hand side is the lowest IP in the subnet.
return makeSubnetFromRaw(prefix, n)
end
end
end
Line 587 ⟶ 675:
checkType('Subnet.new', 1, cidr, 'string')
return makeSubnet(cidr)
end
 
-- Metamethods
function mt:__eq(obj)
return self:getCIDR() == obj:getCIDR()
end
 
function mt:__tostring()
return self:getCIDR()
end
end
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu