diff options
author | Steven Barth <steven@midlink.org> | 2008-08-07 15:09:29 +0000 |
---|---|---|
committer | Steven Barth <steven@midlink.org> | 2008-08-07 15:09:29 +0000 |
commit | 256ab6b57c4a80af98911a8ab299ef327febe628 (patch) | |
tree | e1edc4955ccb37415db7f390d44e239c689aba03 /libs | |
parent | 77ef326e77b9457a0f6586ad99a364b5c71e6fa2 (diff) |
libs/core: Taught luci.ip how to use netmasks and smaller syntax sugars
Diffstat (limited to 'libs')
-rw-r--r-- | libs/core/luasrc/ip.lua | 45 |
1 files changed, 38 insertions, 7 deletions
diff --git a/libs/core/luasrc/ip.lua b/libs/core/luasrc/ip.lua index 6a1a9e7cfd..391dced022 100644 --- a/libs/core/luasrc/ip.lua +++ b/libs/core/luasrc/ip.lua @@ -74,11 +74,15 @@ ntohs = htons ntohl = htonl -function IPv4(address) +function IPv4(address, netmask) + address = address or "0.0.0.0/0" + local data = {} local prefix = address:match("/(.+)") - if prefix then + if netmask then + prefix = IPv4():prefix(netmask) + elseif prefix then address = address:gsub("/.+","") prefix = tonumber(prefix) if not prefix or prefix < 0 or prefix > 32 then return nil end @@ -86,7 +90,7 @@ function IPv4(address) prefix = 32 end - local b1, b2, b3, b4 = address:match("(%d+)%.(%d+)%.(%d+)%.(%d+)") + local b1, b2, b3, b4 = address:match("^(%d+)%.(%d+)%.(%d+)%.(%d+)$") b1 = tonumber(b1) b2 = tonumber(b2) @@ -106,11 +110,15 @@ function IPv4(address) end end -function IPv6(address) +function IPv6(address, netmask) + address = address or "::/0" + local data = {} local prefix = address:match("/(.+)") - if prefix then + if netmask then + prefix = IPv6():prefix(netmask) + elseif prefix then address = address:gsub("/.+","") prefix = tonumber(prefix) if not prefix or prefix < 0 or prefix > 128 then return nil end @@ -238,8 +246,31 @@ function cidr.equal( self, addr ) return true end -function cidr.prefix( self ) - return self[3] +function cidr.prefix( self, mask ) + local prefix = self[3] + + if mask then + prefix = 0 + + local obj = self:is4() and IPv4(mask) or IPv6(mask) + if not obj then + return nil + end + + for i, block in ipairs(obj[2]) do + local pos = bit.lshift(1, 15) + for i=15, 0, -1 do + if bit.band(block, pos) == pos then + prefix = prefix + 1 + else + return prefix + end + pos = bit.rshift(pos, 1) + end + end + end + + return prefix end function cidr.network( self ) |