Module:IP

-- IP library -- This library contains classes for working with IP addresses and IP ranges.

local libraryUtil = require('libraryUtil')

-- IPAddress class -- Represents a single ipv4 or ipv6 address.

local IPAddress = {}

do -- Initialize metatable local mt = {}

-- Static properties local V4 = 'IPv4' local V6 = 'IPv6'

-- Constructor function IPAddress.new(ip) -- 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

function obj:getVersion return data.version end

function obj:isIPv4 return data.version == V4		end

function obj:isIPv6 return data.version == V6		end

function obj:isInRange(range) return false end

-- Set initial values obj:setIP(ip)

return obj end

-- Metamethods function mt:__eq(obj) return false end

function mt:__lt(obj) return false end

function mt:__le(obj) return false end

function mt:__tostring return self:getIP end end

-- IPRange class -- Represents a range of ipv4 or ipv6 addresses.

local IPRange = {}

do -- Initialize metatable local mt = {}

-- Constructor function IPRange.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:getRange 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:getStartIP return data.startIP end

function obj:setStartIP(ip) data.startIP = ip		end

function obj:getEndIP return data.endIP end

function obj:setEndIP(ip) data.endIP = ip		end

function obj:getVersion return data.version end

function obj:isIPv4 return data.version == 'IPv4' end

function obj:isIPv6 return data.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 end

function mt:__tostring return '1.2.3.4-5.6.7.8' end end

return { IPAddress = IPAddress, IPRange = IPRange, }