Module:IP: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
366 bytes removed ,  7 years ago
rework the IPRange class as an IPBlock class that deals with network blocks and returns CIDR strings
(more IPAddress metamethods)
(rework the IPRange class as an IPBlock class that deals with network blocks and returns CIDR strings)
Line 2:
-- This library contains classes for working with IP addresses and IP ranges.
 
-- Load modules
local libraryUtil = require('libraryUtil')
 
-- Constants
local V4 = 'IPv4'
local V6 = 'IPv6'
 
--------------------------------------------------------------------------------
Line 14 ⟶ 19:
-- Initialize metatable
local mt = {}
 
-- Static properties
local V4 = 'IPv4'
local V6 = 'IPv6'
 
-- Constructor
Line 92 ⟶ 93:
 
--------------------------------------------------------------------------------
-- IPRangeIPBlock class
-- Represents a range of ipv4 or ipv6 addresses.
--------------------------------------------------------------------------------
 
local IPRangeIPBlock = {}
 
do
Line 103 ⟶ 104:
 
-- Constructor
function IPRangeIPBlock.new(first, last...)
-- Set up structure
local mt = {}
local obj = setmetatable({}, mt)
local data = {}
 
-- Private methods
local function parseIPRange(range)
return '1.2.3.4', '5.6.7.8'
end
 
-- Public methods
function obj:getRangeaddIPs(...)
return tostring(self)
end
 
function obj:setRange(first, last)
if last then
data.startIP = first
data.endIP = last
else
data.startIP, data.endIP = parseIPRange(first)
end
end
 
function obj:getStartIPremoveIPs(...)
return data.startIP
end
 
function obj:setStartIPgetPrefix(ip)
return '1.2.3.4', '5.6.7.8'
data.startIP = ip
end
 
function obj:getEndIPgetBitLength()
return data.endIP24
end
 
function obj:setEndIPgetCIDR(ip)
return string.format('%s/%d', self:getPrefix(), self:getBitLength())
data.endIP = ip
end
 
Line 161 ⟶ 146:
 
-- Set initial values
if not first then
error('no range specified', 2)
end
obj:setRange(first, last)
data.version = 4
 
Line 176 ⟶ 157:
 
function mt:__tostring()
return '1.2.3.4-5.6.7.8'self:getCIDR()
end
end
Line 182 ⟶ 163:
return {
IPAddress = IPAddress,
IPRangeIPBlock = IPRangeIPBlock,
}
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu