Module:IP: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
1,912 bytes added ,  7 years ago
implement Subnet (very rushed; will check and refactor!)
(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 Subnet (very rushed; will check and refactor!))
Line 94:
else
result[i] = 0
end
end
return result
end
 
local function setHostBits(parts, length)
-- Return a copy of IPv4 or IPv6 parts, with the least-significant bits
-- (host bits) set to 1.
-- The most-significant length bits identify the network.
local bits = parts.n * 16
local width
if length <= 0 then
width = bits
elseif length >= bits then
width = 0
else
width = bits - length
end
local result = { n = parts.n }
for i = parts.n, 1, -1 do
if width > 0 then
if width >= 16 then
result[i] = 0xffff
width = width - 16
else
result[i] = bit32.replace(parts[i], 0xffff, width - 1, width)
width = 0
end
else
result[i] = parts[i]
end
end
Line 231 ⟶ 261:
 
local IPAddress = {}
local uniqueId = {}
 
do
local uniqueId = {}
-- Initialize metatable
local mt = {}
Line 251 ⟶ 281:
function obj:getVersion()
return data.version
end
 
function obj:addIPsgetHighestIP(...bitLength)
return IPAddress.new(uniqueId, setHostBits(data.parts, bitLength))
end
 
function obj:addIPsFromStringgetPrefix(sbitLength)
return IPAddress.new(uniqueId, copyPrefix(data.parts, bitLength))
end
 
Line 265 ⟶ 303:
-- (ipFirst <= self and self <= ipLast)
if self:getVersion() == subnet:getVersion() then
local maskedprefix = IPAddress.newself:getPrefix(uniqueId,subnet:getBitLength())
copyPrefix(data.parts,return prefix == subnet:getBitLengthgetPrefix()))
return masked == subnet:getPrefix()
end
return false
Line 333 ⟶ 370:
return ipString(self:getIPParts())
end
end
 
local function makeSubnet(data, cidrStr)
-- If cidrStr is a valid IPv4 or IPv6 CIDR specification, store its parts
-- in data and return true. Otherwise, return false.
local lhs, rhs = cidrStr: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 then
local base = IPAddress.new(lhs)
returnlocal maskedprefix == subnetbase:getPrefix(n)
if base == prefix then
data.parts = base:getIPParts()
data.versionbitLength = V4n
data.prefix = prefix
data.highestIP = base:getHighestIP(n)
return true
end
end
end
return false
end
 
Line 347 ⟶ 406:
 
-- Constructor
function Subnet.new(optionscidr)
-- Set up structure
local obj = setmetatable({}, mt)
Line 353 ⟶ 412:
 
-- Public methods
function obj:addIPs(...)
end
 
function obj:addIPsFromString(s)
end
 
function obj:getPrefix()
return IPAddressdata.new('1.2.144.0')prefix
end
 
function obj:getHighestIP()
return IPAddressdata.new('1.2.159.255')highestIP
end
 
function obj:getBitLength()
return 20data.bitLength
end
 
Line 388 ⟶ 441:
 
function obj:containsIP(ip)
-- TODO See ip:isInSubnet(subnet); use this technique there?
if self:getVersion() == ip:getVersion() then
return self:getPrefix() <= ip and ip <= self:getHighestIP()
end
return false
end
 
function obj:overlapsSubnet(subnet)
if self:getVersion() == subnet:getVersion() then
return (
subnet:getHighestIP() >= self:getPrefix() and
subnet:getPrefix() <= self:getHighestIP()
)
end
return false
end
 
-- Set initial values
checkType('Subnet.new', 1, cidr, 'string')
data.version = V4
if not makeSubnet(data, cidr) then
error('invalid CIDR', 2)
end
data.version = data.parts.n == 2 and V4 or V6
 
return obj
Line 402 ⟶ 470:
-- Metamethods
function mt:__eq(obj)
return falseself:getCIDR() == obj:getCIDR()
end
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu