function cidr.is4( self )
return self[1] == FAMILY_INET4
end
--- Test whether the instance is a IPv6 address.
-- @return Boolean indicating a IPv6 address type
function cidr.is4( self )
return self[1] == FAMILY_INET4
end
--- Test whether the instance is a IPv6 address.
-- @return Boolean indicating a IPv6 address type
-- family than this instance.
-- @param addr A luci.ip.cidr instance to compare against
-- @return Boolean indicating whether this instance is lower
-- family than this instance.
-- @param addr A luci.ip.cidr instance to compare against
-- @return Boolean indicating whether this instance is lower
function cidr.lower( self, addr )
assert( self[1] == addr[1], "Can't compare IPv4 and IPv6 addresses" )
for i = 1, #self[2] do
function cidr.lower( self, addr )
assert( self[1] == addr[1], "Can't compare IPv4 and IPv6 addresses" )
for i = 1, #self[2] do
-- family than this instance.
-- @param addr A luci.ip.cidr instance to compare against
-- @return Boolean indicating whether this instance is higher
-- family than this instance.
-- @param addr A luci.ip.cidr instance to compare against
-- @return Boolean indicating whether this instance is higher
function cidr.higher( self, addr )
assert( self[1] == addr[1], "Can't compare IPv4 and IPv6 addresses" )
for i = 1, #self[2] do
function cidr.higher( self, addr )
assert( self[1] == addr[1], "Can't compare IPv4 and IPv6 addresses" )
for i = 1, #self[2] do
-- This function will throw an exception if the given address is a different
-- family than this instance.
-- @param addr A luci.ip.cidr instance to compare against
-- @return Boolean indicating whether this instance is equal
-- This function will throw an exception if the given address is a different
-- family than this instance.
-- @param addr A luci.ip.cidr instance to compare against
-- @return Boolean indicating whether this instance is equal
function cidr.equal( self, addr )
assert( self[1] == addr[1], "Can't compare IPv4 and IPv6 addresses" )
for i = 1, #self[2] do
function cidr.equal( self, addr )
assert( self[1] == addr[1], "Can't compare IPv4 and IPv6 addresses" )
for i = 1, #self[2] do
-- instance.
-- @param bits Override prefix length of this instance (optional)
-- @return CIDR instance containing the network address
-- instance.
-- @param bits Override prefix length of this instance (optional)
-- @return CIDR instance containing the network address
--- Return a corresponding CIDR representing the host address of this
-- instance. This is intended to extract the host address from larger subnet.
-- @return CIDR instance containing the network address
--- Return a corresponding CIDR representing the host address of this
-- instance. This is intended to extract the host address from larger subnet.
-- @return CIDR instance containing the network address
function cidr.host( self )
return __bless({ self[1], data, __maxlen(self[1]) })
end
function cidr.host( self )
return __bless({ self[1], data, __maxlen(self[1]) })
end
--- Return a corresponding CIDR representing the netmask of this instance.
-- @param bits Override prefix length of this instance (optional)
-- @return CIDR instance containing the netmask
--- Return a corresponding CIDR representing the netmask of this instance.
-- @param bits Override prefix length of this instance (optional)
-- @return CIDR instance containing the netmask
--- Return CIDR containing the broadcast address of this instance.
-- @return CIDR instance containing the netmask, always nil for IPv6
--- Return CIDR containing the broadcast address of this instance.
-- @return CIDR instance containing the netmask, always nil for IPv6
function cidr.broadcast( self )
-- IPv6 has no broadcast addresses (XXX: assert() instead?)
if self[1] == FAMILY_INET4 then
function cidr.broadcast( self )
-- IPv6 has no broadcast addresses (XXX: assert() instead?)
if self[1] == FAMILY_INET4 then
-- @param amount Number of hosts to add to this instance
-- @param inplace Boolen indicating whether to alter values inplace (optional)
-- @return CIDR representing the new address or nil on overflow error
-- @param amount Number of hosts to add to this instance
-- @param inplace Boolen indicating whether to alter values inplace (optional)
-- @return CIDR representing the new address or nil on overflow error
function cidr.add( self, amount, inplace )
local data = { unpack(self[2]) }
local shorts = __array16( amount, self[1] )
function cidr.add( self, amount, inplace )
local data = { unpack(self[2]) }
local shorts = __array16( amount, self[1] )
-- @param amount Number of hosts to substract from this instance
-- @param inplace Boolen indicating whether to alter values inplace (optional)
-- @return CIDR representing the new address or nil on underflow error
-- @param amount Number of hosts to substract from this instance
-- @param inplace Boolen indicating whether to alter values inplace (optional)
-- @return CIDR representing the new address or nil on underflow error
function cidr.sub( self, amount, inplace )
local data = { unpack(self[2]) }
local shorts = __array16( amount, self[1] )
function cidr.sub( self, amount, inplace )
local data = { unpack(self[2]) }
local shorts = __array16( amount, self[1] )
--- Return CIDR containing the lowest available host address within this subnet.
-- @return CIDR containing the host address, nil if subnet is too small
--- Return CIDR containing the lowest available host address within this subnet.
-- @return CIDR containing the host address, nil if subnet is too small
function cidr.minhost( self )
if self[3] <= __sublen(self[1]) then
-- 1st is Network Address in IPv4 and Subnet-Router Anycast Adresse in IPv6
function cidr.minhost( self )
if self[3] <= __sublen(self[1]) then
-- 1st is Network Address in IPv4 and Subnet-Router Anycast Adresse in IPv6
--- Return CIDR containing the highest available host address within the subnet.
-- @return CIDR containing the host address, nil if subnet is too small
--- Return CIDR containing the highest available host address within the subnet.
-- @return CIDR containing the host address, nil if subnet is too small
function cidr.maxhost( self )
if self[3] <= __sublen(self[1]) then
local data = { unpack(self[2]) }
function cidr.maxhost( self )
if self[3] <= __sublen(self[1]) then
local data = { unpack(self[2]) }