Module:IP: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
use uniqueId table for helper functions to create an IPAddress; implement ip:isInSubnet(); functions like subnet:getPrefix() return an IPAddress not a string, I assume
(implement IPAddress getNextIP and getPreviousIP)
(use uniqueId table for helper functions to create an IPAddress; implement ip:isInSubnet(); functions like subnet:getPrefix() return an IPAddress not a string, I assume)
Line 3:
 
-- Load modules
local bit32 = require('bit32')
local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType
Line 29 ⟶ 30:
-- Instead, move the metamethods into IPAddress.new(ip) so they can
-- access the data upvalue?
-- Hmmm, that would work only for self, not for the other object.
-- Is there a way of keeping data private but accessing it from metamethods?
-- If getIPParts is kept, perhaps it should be changed to "getParts" or similar
-- as "IP" is redundant in an IPAddress class.
 
------------------------------------------------------------------------
-- Functions from Module:IPblock follow.
-- TODO Massage following for style consistent with this module.
--------------------------------------------------------------------------------
 
Line 45 ⟶ 50:
join = function (self, sep)
return table.concat(self, sep)
end,
remove = function (self, pos)
if self.n > 0 and (pos == nil or (0 < pos and pos <= self.n)) then
self.n = self.n - 1
return table.remove(self, pos)
end
end,
sort = function (self, comp)
Line 56 ⟶ 55:
end,
}
end
 
local function copyChanged(parts, down)
-- Return a copy of IPv4 or IPv6 parts, incremented or decremented.
-- Will wraparound:
-- increment 255.255.255.255 → 0.0.0.0
-- ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff → ::
-- decrement 0.0.0.0 → 255.255.255.255
-- :: → ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
local result = { n = parts.n }
local carry = down and 0xffff or 1
for i = parts.n, 1, -1 do
local sum = parts[i] + carry
if sum >= 0x10000 then
carry = down and 0x10000 or 1
sum = sum - 0x10000
else
carry = down and 0xffff or 0
end,
result[i] = sum
end
return result
end
 
local function copyPrefix(parts, length)
-- Return a copy of IPv4 or IPv6 parts, masked to length.
local result = { n = parts.n }
for i = 1, parts.n do
if length > 0 then
if length >= 16 then
result[i] = parts[i]
self.nlength = self.nlength - 116
else
result[i] = bit32.band(parts[i],
bit32.arshift(0xffff8000, length - 1))
length = 0
end
else
result[i] = 0
end
end
return result
end
 
Line 182 ⟶ 223:
end
return false
end
 
local function copyChanged(parts, down)
-- Return a copy of IPv4 or IPv6 parts, incremented or decremented.
-- Will wraparound:
-- increment 255.255.255.255 → 0.0.0.0
-- ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff → ::
-- decrement 0.0.0.0 → 255.255.255.255
-- :: → ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
local result = { n = parts.n }
local carry = down and 0xffff or 1
for i = parts.n, 1, -1 do
local sum = parts[i] + carry
if sum >= 0x10000 then
carry = down and 0x10000 or 1
sum = sum - 0x10000
else
carry = down and 0xffff or 0
end
result[i] = sum
end
return result
end
 
--------------------------------------------------------------------------------
-- IPAddress class
-- Represents a single ipv4IPv4 or ipv6IPv6 address.
--------------------------------------------------------------------------------
 
local IPAddress = {}
local uniqueId = {}
 
do
Line 242 ⟶ 262:
 
function obj:isInSubnet(subnet)
-- TODO Consider alternative of checking:
-- (ipFirst <= self and self <= ipLast)
if self:getVersion() == subnet:getVersion() then
local masked = IPAddress.new(uniqueId,
copyPrefix(data.parts, subnet:getBitLength()))
return masked == subnet:getPrefix()
end
return false
end
 
function obj:getNextIP()
return IPAddress.new(IPAddressuniqueId, copyChanged(data.parts))
end
 
function obj:getPreviousIP()
return IPAddress.new(IPAddressuniqueId, copyChanged(data.parts, true))
end
 
Line 259 ⟶ 286:
 
-- Set initial values
if ip == IPAddressuniqueId then
data.parts = parts
else
Line 310 ⟶ 337:
--------------------------------------------------------------------------------
-- Subnet class
-- Represents a block of ipv4IPv4 or ipv6IPv6 addresses.
--------------------------------------------------------------------------------
 
Line 333 ⟶ 360:
 
function obj:getPrefix()
return IPAddress.new('1.2.3144.0')
end
 
function obj:getHighestIP()
return IPAddress.new('1.2.3159.255')
end
 
function obj:getBitLength()
return 2420
end
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu