X-Git-Url: https://git.archive.openwrt.org/?p=project%2Fluci.git;a=blobdiff_plain;f=libs%2Fcore%2Fluasrc%2Fip.lua;h=60ca090135962eb24daf7c68ab3982475d2333b7;hp=02b4a66a68988262a904ad47970d7290444fc2cc;hb=481cd6feb7583e42fda746d369ab6744af705fbb;hpb=ba298a020bc033f526a0750caa24e9b098074674 diff --git a/libs/core/luasrc/ip.lua b/libs/core/luasrc/ip.lua index 02b4a66a6..60ca09013 100644 --- a/libs/core/luasrc/ip.lua +++ b/libs/core/luasrc/ip.lua @@ -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 @@ -65,7 +66,7 @@ local function __array16( x, family ) list = { unpack(x[2]) } elseif type(x) == "table" then - list = x + list = { unpack(x) } end assert( list, "Invalid operand" ) @@ -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) @@ -217,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 @@ -227,7 +230,7 @@ function IPv6(address, netmask) block = tonumber(address:sub(borderl, borderh - 1), 16) if block and block <= 0xFFFF then - table.insert(data, block) + data[#data+1] = block else if zeroh or borderh - borderl > 1 then return nil end zeroh = #data + 1 @@ -241,7 +244,7 @@ function IPv6(address, netmask) block = tonumber(chunk, 16) if not block or block > 0xFFFF then return nil end - table.insert(data, block) + data[#data+1] = block elseif #chunk > 4 then if #data == 7 or #chunk > 15 then return nil end borderl = 1 @@ -254,7 +257,7 @@ function IPv6(address, netmask) if not block or block > 255 then return nil end if i == 1 or i == 3 then - table.insert(data, block * 256) + data[#data+1] = block * 256 else data[#data] = data[#data] + block end @@ -294,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 @@ -308,13 +312,13 @@ function Hex( hex, prefix, family, swap ) for i = 1, ( len / 4 ), 4 do local n = tonumber( hex:sub( i, i+3 ), 16 ) if n then - table.insert( data, n ) + data[#data+1] = n else return nil end end - return __bless({ family, data, len }) + return __bless({ family, data, prefix }) end @@ -322,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 @@ -331,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 @@ -338,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, @@ -371,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] @@ -388,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] @@ -405,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 @@ -421,26 +457,25 @@ function cidr.prefix( self, mask ) if mask then prefix = 0 + local stop = false - local obj = self:is4() and IPv4(mask) or IPv6(mask) + local obj = type(mask) ~= "table" + and ( self:is4() and IPv4(mask) or IPv6(mask) ) or mask - if not obj then - return nil - end + 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 - if not stop then - prefix = prefix + 1 - else - return nil - end - else - stop = true + local _, word + for _, word in ipairs(obj[2]) do + if word == 0xFFFF then + prefix = prefix + 16 + else + local bitmask = bit.lshift(1, 15) + while bit.band(word, bitmask) == bitmask do + prefix = prefix + 1 + bitmask = bit.lshift(1, 15 - (prefix % 16)) end - pos = bit.rshift(pos, 1) + + break end end end @@ -459,15 +494,16 @@ function cidr.network( self, bits ) local data = { } bits = bits or self[3] + local i for i = 1, math.floor( bits / 16 ) do - table.insert( data, self[2][i] ) + data[#data+1] = self[2][i] end if #data < #self[2] then - table.insert( data, bit.band( self[2][1+#data], __mask16(bits) ) ) + data[#data+1] = bit.band( self[2][1+#data], __mask16(bits) ) for i = #data + 1, #self[2] do - table.insert( data, 0 ) + data[#data+1] = 0 end end @@ -481,7 +517,7 @@ end -- @see cidr.broadcast -- @see cidr.mask function cidr.host( self ) - return __bless({ self[1], data, __maxlen(self[1]) }) + return __bless({ self[1], self[2], __maxlen(self[1]) }) end --- Return a corresponding CIDR representing the netmask of this instance. @@ -495,14 +531,14 @@ function cidr.mask( self, bits ) bits = bits or self[3] for i = 1, math.floor( bits / 16 ) do - table.insert( data, 0xFFFF ) + data[#data+1] = 0xFFFF end if #data < #self[2] then - table.insert( data, __mask16(bits) ) + data[#data+1] = __mask16(bits) for i = #data + 1, #self[2] do - table.insert( data, 0 ) + data[#data+1] = 0 end end @@ -548,22 +584,21 @@ 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] ) - if shorts then - for pos = #data, 1, -1 do - local add = ( #shorts > 0 ) and table.remove( shorts, #shorts ) or 0 - if ( data[pos] + add ) > 0xFFFF then - data[pos] = ( data[pos] + add ) % 0xFFFF - if pos > 1 then - data[pos-1] = data[pos-1] + ( add - data[pos] ) - else - return nil - end + for pos = #data, 1, -1 do + local add = ( #shorts > 0 ) and table.remove( shorts, #shorts ) or 0 + if ( data[pos] + add ) > 0xFFFF then + data[pos] = ( data[pos] + add ) % 0xFFFF + if pos > 1 then + data[pos-1] = data[pos-1] + ( add - data[pos] ) else - data[pos] = data[pos] + add + return nil end + else + data[pos] = data[pos] + add end end @@ -581,22 +616,21 @@ 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] ) - if shorts then - for pos = #data, 1, -1 do - local sub = ( #shorts > 0 ) and table.remove( shorts, #shorts ) or 0 - if ( data[pos] - sub ) < 0 then - data[pos] = ( sub - data[pos] ) % 0xFFFF - if pos > 1 then - data[pos-1] = data[pos-1] - ( sub + data[pos] ) - else - return nil - end + for pos = #data, 1, -1 do + local sub = ( #shorts > 0 ) and table.remove( shorts, #shorts ) or 0 + if ( data[pos] - sub ) < 0 then + data[pos] = ( sub - data[pos] ) % 0xFFFF + if pos > 1 then + data[pos-1] = data[pos-1] - ( sub + data[pos] ) else - data[pos] = data[pos] - sub + return nil end + else + data[pos] = data[pos] - sub end end @@ -623,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