lib/core: luci.ip: Added more sanity checks and optimizations
This commit is contained in:
parent
256ab6b57c
commit
3546ed9bbe
1 changed files with 23 additions and 13 deletions
|
@ -77,11 +77,13 @@ ntohl = htonl
|
||||||
function IPv4(address, netmask)
|
function IPv4(address, netmask)
|
||||||
address = address or "0.0.0.0/0"
|
address = address or "0.0.0.0/0"
|
||||||
|
|
||||||
|
local obj = __bless({ FAMILY_INET4 })
|
||||||
|
|
||||||
local data = {}
|
local data = {}
|
||||||
local prefix = address:match("/(.+)")
|
local prefix = address:match("/(.+)")
|
||||||
|
|
||||||
if netmask then
|
if netmask then
|
||||||
prefix = IPv4():prefix(netmask)
|
prefix = obj:prefix(netmask)
|
||||||
elseif prefix then
|
elseif prefix then
|
||||||
address = address:gsub("/.+","")
|
address = address:gsub("/.+","")
|
||||||
prefix = tonumber(prefix)
|
prefix = tonumber(prefix)
|
||||||
|
@ -100,24 +102,25 @@ function IPv4(address, netmask)
|
||||||
if b1 and b1 <= 255 and
|
if b1 and b1 <= 255 and
|
||||||
b2 and b2 <= 255 and
|
b2 and b2 <= 255 and
|
||||||
b3 and b3 <= 255 and
|
b3 and b3 <= 255 and
|
||||||
b4 and b4 <= 255
|
b4 and b4 <= 255 and
|
||||||
|
prefix
|
||||||
then
|
then
|
||||||
return __bless({
|
table.insert(obj, { b1 * 256 + b2, b3 * 256 + b4 })
|
||||||
FAMILY_INET4,
|
table.insert(obj, prefix)
|
||||||
{ b1 * 256 + b2, b3 * 256 + b4 },
|
return obj
|
||||||
prefix
|
|
||||||
})
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function IPv6(address, netmask)
|
function IPv6(address, netmask)
|
||||||
address = address or "::/0"
|
address = address or "::/0"
|
||||||
|
|
||||||
|
local obj = __bless({ FAMILY_INET6 })
|
||||||
|
|
||||||
local data = {}
|
local data = {}
|
||||||
local prefix = address:match("/(.+)")
|
local prefix = address:match("/(.+)")
|
||||||
|
|
||||||
if netmask then
|
if netmask then
|
||||||
prefix = IPv6():prefix(netmask)
|
prefix = obj:prefix(netmask)
|
||||||
elseif prefix then
|
elseif prefix then
|
||||||
address = address:gsub("/.+","")
|
address = address:gsub("/.+","")
|
||||||
prefix = tonumber(prefix)
|
prefix = tonumber(prefix)
|
||||||
|
@ -180,8 +183,10 @@ function IPv6(address, netmask)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if #data == 8 then
|
if #data == 8 and prefix then
|
||||||
return __bless({ FAMILY_INET6, data, prefix })
|
table.insert(obj, data)
|
||||||
|
table.insert(obj, prefix)
|
||||||
|
return obj
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -251,8 +256,9 @@ function cidr.prefix( self, mask )
|
||||||
|
|
||||||
if mask then
|
if mask then
|
||||||
prefix = 0
|
prefix = 0
|
||||||
|
local stop = false
|
||||||
local obj = self:is4() and IPv4(mask) or IPv6(mask)
|
local obj = self:is4() and IPv4(mask) or IPv6(mask)
|
||||||
|
|
||||||
if not obj then
|
if not obj then
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
@ -261,9 +267,13 @@ function cidr.prefix( self, mask )
|
||||||
local pos = bit.lshift(1, 15)
|
local pos = bit.lshift(1, 15)
|
||||||
for i=15, 0, -1 do
|
for i=15, 0, -1 do
|
||||||
if bit.band(block, pos) == pos then
|
if bit.band(block, pos) == pos then
|
||||||
prefix = prefix + 1
|
if not stop then
|
||||||
|
prefix = prefix + 1
|
||||||
|
else
|
||||||
|
return nil
|
||||||
|
end
|
||||||
else
|
else
|
||||||
return prefix
|
stop = true
|
||||||
end
|
end
|
||||||
pos = bit.rshift(pos, 1)
|
pos = bit.rshift(pos, 1)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue