libs/core: remove various uses of global vars in luci.ip
[project/luci.git] / libs / core / luasrc / ip.lua
index 0a66804..60ca090 100644 (file)
@@ -17,11 +17,12 @@ $Id$
 --- LuCI IP calculation library.
 module( "luci.ip", package.seeall )
 
-require("bit")
-require("luci.util")
+require "nixio"
+local bit  = nixio.bit
+local util = require "luci.util"
 
 --- Boolean; true if system is little endian
-LITTLE_ENDIAN = not luci.util.bigendian()
+LITTLE_ENDIAN = not util.bigendian()
 
 --- Boolean; true if system is big endian
 BIG_ENDIAN    = not LITTLE_ENDIAN
@@ -219,7 +220,7 @@ function IPv6(address, netmask)
        end
 
        local borderl = address:sub(1, 1) == ":" and 2 or 1
-       local borderh, zeroh, chunk, block
+       local borderh, zeroh, chunk, block, i
 
        if #address > 45 then return nil end
 
@@ -296,6 +297,7 @@ function Hex( hex, prefix, family, swap )
        local len  = __maxlen(family)
        local tmp  = ""
        local data = { }
+       local i
 
        for i = 1, (len/4) - #hex do tmp = tmp .. '0' end
 
@@ -324,7 +326,7 @@ end
 -- @class      module
 -- @cstyle     instance
 -- @name       luci.ip.cidr
-cidr = luci.util.class()
+cidr = util.class()
 
 --- Test whether the instance is a IPv4 address.
 -- @return     Boolean indicating a IPv4 address type
@@ -333,6 +335,26 @@ function cidr.is4( self )
        return self[1] == FAMILY_INET4
 end
 
+--- Test whether this instance is an IPv4 RFC1918 private address
+-- @return     Boolean indicating whether this instance is an RFC1918 address
+function cidr.is4rfc1918( self )
+       if self[1] == FAMILY_INET4 then
+               return ((self[2][1] >= 0x0A00) and (self[2][1] <= 0x0AFF)) or
+                      ((self[2][1] >= 0xAC10) and (self[2][1] <= 0xAC1F)) or
+                       (self[2][1] == 0xC0A8)
+       end
+       return false
+end
+
+--- Test whether this instance is an IPv4 link-local address (Zeroconf)
+-- @return     Boolean indicating whether this instance is IPv4 link-local
+function cidr.is4linklocal( self )
+       if self[1] == FAMILY_INET4 then
+               return (self[2][1] == 0xA9FE)
+       end
+       return false
+end
+
 --- Test whether the instance is a IPv6 address.
 -- @return     Boolean indicating a IPv6 address type
 -- @see                cidr.is4
@@ -340,6 +362,15 @@ function cidr.is6( self )
        return self[1] == FAMILY_INET6
 end
 
+--- Test whether this instance is an IPv6 link-local address
+-- @return     Boolean indicating whether this instance is IPv6 link-local
+function cidr.is6linklocal( self )
+       if self[1] == FAMILY_INET6 then
+               return (self[2][1] >= 0xFE80) and (self[2][1] <= 0xFEBF)
+       end
+       return false
+end
+
 --- Return a corresponding string representation of the instance.
 -- If the prefix length is lower then the maximum possible prefix length for the
 -- corresponding address type then the address is returned in CIDR notation,
@@ -373,6 +404,7 @@ end
 -- @see                        cidr.equal
 function cidr.lower( self, addr )
        assert( self[1] == addr[1], "Can't compare IPv4 and IPv6 addresses" )
+       local i
        for i = 1, #self[2] do
                if self[2][i] ~= addr[2][i] then
                        return self[2][i] < addr[2][i]
@@ -390,6 +422,7 @@ end
 -- @see                        cidr.equal
 function cidr.higher( self, addr )
        assert( self[1] == addr[1], "Can't compare IPv4 and IPv6 addresses" )
+       local i
        for i = 1, #self[2] do
                if self[2][i] ~= addr[2][i] then
                        return self[2][i] > addr[2][i]
@@ -407,6 +440,7 @@ end
 -- @see                        cidr.higher
 function cidr.equal( self, addr )
        assert( self[1] == addr[1], "Can't compare IPv4 and IPv6 addresses" )
+       local i
        for i = 1, #self[2] do
                if self[2][i] ~= addr[2][i] then
                        return false
@@ -430,6 +464,7 @@ function cidr.prefix( self, mask )
 
                if not obj then return nil end
 
+               local _, word
                for _, word in ipairs(obj[2]) do
                        if word == 0xFFFF then
                                prefix = prefix + 16
@@ -459,6 +494,7 @@ function cidr.network( self, bits )
        local data = { }
        bits = bits or self[3]
 
+       local i
        for i = 1, math.floor( bits / 16 ) do
                data[#data+1] = self[2][i]
        end
@@ -548,6 +584,7 @@ end
 -- @return                     CIDR representing the new address or nil on overflow error
 -- @see                                cidr.sub
 function cidr.add( self, amount, inplace )
+       local pos
        local data   = { unpack(self[2]) }
        local shorts = __array16( amount, self[1] )
 
@@ -579,6 +616,7 @@ end
 -- @return                     CIDR representing the new address or nil on underflow error
 -- @see                                cidr.add
 function cidr.sub( self, amount, inplace )
+       local pos
        local data   = { unpack(self[2]) }
        local shorts = __array16( amount, self[1] )
 
@@ -619,6 +657,7 @@ end
 -- @see                        cidr.minhost
 function cidr.maxhost( self )
        if self[3] <= __sublen(self[1]) then
+               local i
                local data   = { unpack(self[2]) }
                local offset = math.floor( self[3] / 16 ) + 1