Module:IP: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
114 bytes removed ,  7 years ago
don't allow setting a new IP address in IPAddress objects - this doesn't make much sense
(fix metatable error)
(don't allow setting a new IP address in IPAddress objects - this doesn't make much sense)
Line 4:
-- Load modules
local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType
 
-- Constants
Line 22 ⟶ 23:
-- Constructor
function IPAddress.new(ip)
checkType('IPAddress.new', 1, ip, 'string')
 
-- Set up structure
local obj = setmetatable({}, mt)
local data = {}
 
-- Private methods
local function lexIPv4(s)
end
 
local function lexIPv6(s)
end
 
-- Public methods
function obj:getIP()
return data.ip
end
 
function obj:setIP(ip)
if type(ip) ~= 'string' then
return nil
end
data.ip = ip
if ip:find('^%x?%x?%x?%x?:') then
data.version = V6
elseif ip:find('^%d') then
data.version = V4
else
error('invalid IP', 2)
end
end
 
Line 69 ⟶ 51:
 
-- Set initial values
obj:setIP(data.ip = ip)
if ip:find('^%x?%x?%x?%x?:') then
data.ipversion = ipV6
elseif ip:find('^%d') then
data.version = V6V4
else
error('invalid IP', 2)
end
 
return obj
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu