libs/core: fallback to trunk version
[project/luci.git] / libs / core / luasrc / ip.lua
index 8ac017e..c5148c2 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
@@ -159,6 +160,7 @@ function IPv4(address, netmask)
        local data = {}
        local prefix = address:match("/(.+)")
        address = address:gsub("/.+","")
+       address = address:gsub("^%[(.*)%]$", "%1"):upper():gsub("^::FFFF:", "")
 
        if netmask then
                prefix = obj:prefix(netmask)
@@ -206,6 +208,7 @@ function IPv6(address, netmask)
        local data = {}
        local prefix = address:match("/(.+)")
        address = address:gsub("/.+","")
+       address = address:gsub("^%[(.*)%]$", "%1")
 
        if netmask then
                prefix = obj:prefix(netmask)
@@ -322,7 +325,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
@@ -331,6 +334,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
@@ -338,6 +361,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,