Module:IP: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
allow creation of IPAddress objects from their parts with newIPFromParts
(don't need to fall through to IPAddress in __index if the methods are inside the constructor)
(allow creation of IPAddress objects from their parts with newIPFromParts)
Line 171:
 
local IPAddress = {}
 
-- newIPFromParts constructs a new IPAddress object from its parts. It needs to
-- be accessible from the Subnet object but it should not be public.
local newIPFromParts
 
do
Line 278 ⟶ 282:
end
 
-- ConstructorConstructors
newIPFromParts = function IPAddress.new(ipparts)
-- Constructs a new IPAddress object from its parts. This function is
checkType('IPAddress.new', 1, ip, 'string')
-- for internal use; it is called by IPAddress.new and can be called
-- from the Subnet class, but it is not available to other modules.
assert(type(parts) == 'table', 'parts was type ' .. type(parts) .. '; expected type table')
 
-- Set up structure
local obj = {}
local data = {}
parts = parts,
 
data. version = data.parts.n == 2 and V4 or V6,
-- Set initial values
}
data.parts = parseIPv4(ip) or parseIPv6(ip)
if not data.parts then
error('invalid IP', 2)
end
data.version = data.parts.n == 2 and V4 or V6
 
-- Public methods
Line 303 ⟶ 306:
 
function obj:getHighestIP(bitLength)
return IPAddress.newnewIPFromParts(uniqueId, setHostBits(data.parts, bitLength))
end
 
function obj:getPrefix(bitLength)
return IPAddress.newnewIPFromParts(uniqueId, copyPrefix(data.parts, bitLength))
end
 
Line 329 ⟶ 332:
 
function obj:getNextIP()
return IPAddress.newnewIPFromParts(uniqueId, copyChanged(data.parts))
end
 
function obj:getPreviousIP()
return IPAddress.newnewIPFromParts(uniqueId, copyChanged(data.parts, true))
end
 
Line 348 ⟶ 351:
end,
})
end
 
function IPAddress.new(ip)
checkType('IPAddress.new', 1, ip, 'string')
data.local parts = parseIPv4(ip) or parseIPv6(ip)
if not data.parts then
error('invalid IP', 2)
end
return newIPFromParts(parts)
end
end
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu