From edccb1fc95d182358beea3b1b5de8d4ab91c9a69 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Fri, 8 Aug 2008 01:03:25 +0000 Subject: * luci/libs: added Hex() constructor to luci.ip --- libs/core/luasrc/ip.lua | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) (limited to 'libs') diff --git a/libs/core/luasrc/ip.lua b/libs/core/luasrc/ip.lua index ea243f950..15730dbac 100644 --- a/libs/core/luasrc/ip.lua +++ b/libs/core/luasrc/ip.lua @@ -46,6 +46,14 @@ local function __mask16(bits) ) end +local function __length(family) + if family == FAMILY_INET4 then + return 32 + else + return 128 + end +end + -- htons(), htonl(), ntohs(), ntohl() function htons(x) @@ -190,6 +198,35 @@ function IPv6(address, netmask) end end +function Hex( hex, prefix, family, swap ) + family = ( family ~= nil ) and family or FAMILY_INET4 + swap = ( swap == nil ) and true or swap + prefix = prefix or __length(family) + + local len = __length(family) + local tmp = "" + local data = { } + + for i = 1, (len/4) - #hex do tmp = tmp .. '0' end + + if swap and LITTLE_ENDIAN then + for i = #hex, 1, -2 do tmp = tmp .. hex:sub( i - 1, i ) end + end + + hex = tmp + + for i = 1, ( len / 4 ), 4 do + local n = tonumber( hex:sub( i, i+3 ), 16 ) + if n then + table.insert( data, n ) + else + return nil + end + end + + return __bless({ family, data, len }) +end + cidr = luci.util.class() @@ -296,11 +333,11 @@ function cidr.network( self ) table.insert( data, 0 ) end - return __bless({ self[1], data, self:is4() and 32 or 128 }) + return __bless({ self[1], data, __length(self[1]) }) end function cidr.host( self ) - return __bless({ self[1], data, self:is4() and 32 or 128 }) + return __bless({ self[1], data, __length(self[1]) }) end function cidr.mask( self, bits ) @@ -317,7 +354,7 @@ function cidr.mask( self, bits ) table.insert( data, 0 ) end - return __bless({ self[1], data, self:is4() and 32 or 128 }) + return __bless({ self[1], data, __length(self[1]) }) end function cidr.contains( self, addr ) -- cgit v1.2.3