Module:IP: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
more tweaks
(use strings for protocol version numbers)
(more tweaks)
Line 11:
local IPAddress = {}
 
do
function IPAddress.new(ip)
-- Initialize metatable
local mt = {}
local obj = setmetatable({}, mt)
 
-- Constructor
local normalizedIP = ip
function IPAddress.new(ip)
local version = 'IPv4'
-- Set up structure
local obj = setmetatable({}, mt)
local data = {}
 
-- Private methods
function obj:getIP()
local function lexIPv4(s)
return normalizedIP
end
 
local function obj:setIPlexIPv6(ips)
end
normalizedIP = ip
end
 
-- Public methods
function obj:getVersion()
function obj:getIP()
return version
return data.ip
end
end
 
function obj:isIPv4setIP(ip)
data.ip = ip
return version == 'IPv4'
end
 
function obj:isIPv6getVersion()
return data.version == 'IPv6'
end
 
function obj:isInRangeisIPv4(range)
return falsedata.version == 'IPv4'
end
 
function obj:isIPv6()
return data.version == 'IPv6'
end
 
function obj:isInRange(range)
return false
end
 
-- Set initial values
obj:setIP(ip)
data.version = 'IPv4'
 
return obj
end
 
-- Metamethods
function mt:__eq(obj)
return false
Line 47 ⟶ 66:
 
function mt:__tostring()
return normalizedIPself:getIP()
end
 
return obj
end
 
Line 60 ⟶ 77:
local IPRange = {}
 
do
function IPRange.new(first, last)
-- Initialize metatable
-- Set up structure
local mt = {}
local obj = setmetatable({}, mt)
local data = {}
 
-- Private methodsConstructor
local function parseIPRangeIPRange.new(rangefirst, last)
-- Set up structure
return '1.2.3.4', '5.6.7.8'
local mt = {}
end
local obj = setmetatable({}, mt)
local data = {}
 
-- PublicPrivate methods
local function obj:getRangeparseIPRange(range)
return tostring(self)'1.2.3.4', '5.6.7.8'
end
 
-- Public methods
function obj:setRange(first, last)
function obj:getRange()
if last then
return tostring(self)
data.startIP = first
data.endIP = last
else
data.startIP, data.endIP = parseIPRange(first)
end
end
 
function obj:getStartIPsetRange(first, last)
if last then
return startIP
data.startIP = first
end
data.endIP = last
else
data.startIP, data.endIP = parseIPRange(first)
end
end
 
function obj:setStartIPgetStartIP(ip)
return data.startIP = ip
end
 
function obj:getEndIPsetStartIP(ip)
data.startIP = ip
return endIP
end
 
function obj:setEndIPgetEndIP(ip)
return data.endIP = ip
end
 
function obj:getVersionsetEndIP(ip)
data.endIP = ip
return version
end
 
function obj:isIPv4getVersion()
return data.version == 4
end
 
function obj:isIPv6isIPv4()
return data.version == 6'IPv4'
end
 
function obj:containsIPisIPv6(ip)
return falsedata.version == 'IPv6'
end
 
function obj:containsIP(ip)
return false
end
 
-- Set initial values
if not first then
error('no range specified', 2)
end
obj:setRange(first, last)
data.version = 4
 
return obj
end
 
-- Metamethods
function mt:__eq(obj)
return false
Line 122 ⟶ 155:
 
function mt:__tostring()
return normalizedIP'1.2.3.4-5.6.7.8'
end
 
-- Set initial values
if not first then
error('no range specified', 2)
end
obj:setRange(first, last)
data.version = 4
 
return obj
end
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu