summaryrefslogtreecommitdiffhomepage
path: root/libs/core
diff options
context:
space:
mode:
authorJo-Philipp Wich <jow@openwrt.org>2008-08-08 12:38:21 +0000
committerJo-Philipp Wich <jow@openwrt.org>2008-08-08 12:38:21 +0000
commit069ac1115d4699f66996c04d6801d461ead1b189 (patch)
tree7a3f6dcdd3d4eaa8493a7705c8154bc855ca8189 /libs/core
parente080fcebd2396c138d8c916d1cf385a88e0db246 (diff)
* luci/libs: fixed contains() in luci.ip
Diffstat (limited to 'libs/core')
-rw-r--r--libs/core/luasrc/ip.lua28
1 files changed, 16 insertions, 12 deletions
diff --git a/libs/core/luasrc/ip.lua b/libs/core/luasrc/ip.lua
index 68ac384dc..e6de08f1b 100644
--- a/libs/core/luasrc/ip.lua
+++ b/libs/core/luasrc/ip.lua
@@ -322,17 +322,20 @@ function cidr.prefix( self, mask )
return prefix
end
-function cidr.network( self )
+function cidr.network( self, bits )
local data = { }
+ bits = bits or self[3]
- for i = 1, math.floor( self[3] / 16 ) do
+ for i = 1, math.floor( bits / 16 ) do
table.insert( data, self[2][i] )
end
- table.insert( data, bit.band( self[2][1+#data], __mask16(self[3]) ) )
+ if #data < #self[2] then
+ table.insert( data, bit.band( self[2][1+#data], __mask16(bits) ) )
- for i = #data, #self[2] do
- table.insert( data, 0 )
+ for i = #data, #self[2] do
+ table.insert( data, 0 )
+ end
end
return __bless({ self[1], data, __length(self[1]) })
@@ -350,10 +353,12 @@ function cidr.mask( self, bits )
table.insert( data, 0xFFFF )
end
- table.insert( data, __mask16(bits) )
+ if #data < #self[2] then
+ table.insert( data, __mask16(bits) )
- for i = #data + 1, #self[2] do
- table.insert( data, 0 )
+ for i = #data + 1, #self[2] do
+ table.insert( data, 0 )
+ end
end
return __bless({ self[1], data, __length(self[1]) })
@@ -361,10 +366,9 @@ end
function cidr.contains( self, addr )
assert( self[1] == addr[1], "Can't compare IPv4 and IPv6 addresses" )
- local mask1 = self:prefix()
- local mask2 = addr:prefix()
- if mask1 <= mask2 then
- return self:mask(addr:prefix()) == mask2
+
+ if self:prefix() <= addr:prefix() then
+ return self:network() == addr:network(self:prefix())
end
return false