Module:IP: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
implement IPAddress getNextIP and getPreviousIP
(add IPAddress __concat; minor style changes)
(implement IPAddress getNextIP and getPreviousIP)
Line 182:
end
return false
end
 
local function copyChanged(parts, down)
-- Return a copy of IPv4 or IPv6 parts, incremented or decremented.
-- Will wraparound:
-- increment 255.255.255.255 → 0.0.0.0
-- ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff → ::
-- decrement 0.0.0.0 → 255.255.255.255
-- :: → ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
local result = { n = parts.n }
local carry = down and 0xffff or 1
for i = parts.n, 1, -1 do
local sum = parts[i] + carry
if sum >= 0x10000 then
carry = down and 0x10000 or 1
sum = sum - 0x10000
else
carry = down and 0xffff or 0
end
result[i] = sum
end
return result
end
 
Line 196 ⟶ 218:
 
-- Constructor
function IPAddress.new(ip, parts)
checkType('IPAddress.new', 1, ip, 'string')
 
-- Set up structure
Line 205 ⟶ 226:
-- Public methods
function obj:getIP()
return ipString(data.ipparts)
end
 
Line 225 ⟶ 246:
 
function obj:getNextIP()
return IPAddress.new(IPAddress, copyChanged(data.parts))
end
 
function obj:getPreviousIP()
return IPAddress.new(IPAddress, copyChanged(data.parts, true))
end
 
Line 236 ⟶ 259:
 
-- Set initial values
data.if ip == ipIPAddress then
if ipv4_address( data,.parts ip)= thenparts
data.version = V4
elseif ipv6_address(data, ip) then
data.version = V6
else
checkType('IPAddress.new', 1, ip, 'string')
error('invalid IP', 2)
if not (ipv4Address(data, ip) or ipv6Address(data, ip)) then
error('invalid IP', 2)
end
end
data.version = data.parts.n == 2 and V4 or V6
 
return obj
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu