Object Instance luci.ip.cidr
IP CIDR Object. Represents an IPv4 or IPv6 address range.
Functions
cidr:is4 () | Checks whether the CIDR instance is an IPv4 address range |
cidr:is4rfc1918 () | Checks whether the CIDR instance is within the private RFC1918 address space |
cidr:is4linklocal () | Checks whether the CIDR instance is an IPv4 link local (Zeroconf) address |
cidr:is6 () | Checks whether the CIDR instance is an IPv6 address range |
cidr:is6linklocal () | Checks whether the CIDR instance is an IPv6 link local address |
cidr:is6mapped4 () | Checks whether the CIDR instance is an IPv6 mapped IPv4 address |
cidr:lower (addr) | Checks whether this CIDR instance is lower than the given argument. |
cidr:higher (addr) | Checks whether this CIDR instance is higher than the given argument. |
cidr:equal (addr) | Checks whether this CIDR instance is equal to the given argument. |
cidr:prefix (mask) | Get or set prefix size of CIDR instance. |
cidr:network (mask) | Derive network address of CIDR instance. |
cidr:host () | Derive host address of CIDR instance. |
cidr:mask (mask) | Derive netmask of CIDR instance. |
cidr:broadcast (mask) | Derive broadcast address of CIDR instance. |
cidr:mapped4 () | Derive mapped IPv4 address of CIDR instance. |
cidr:contains (addr) | Test whether CIDR contains given range. |
cidr:add (amount, inplace) | Add given amount to CIDR instance. |
cidr:sub (amount, inplace) | Substract given amount from CIDR instance. |
cidr:minhost () | Calculate the lowest possible host address within this CIDR instance. |
cidr:maxhost () | Calculate the highest possible host address within this CIDR instance. |
cidr:string () | Convert CIDR instance into string representation. |
Functions
- cidr:is4 ()
-
Checks whether the CIDR instance is an IPv4 address range
Return value:
true
if the CIDR is an IPv4 range, elsefalse
See also:
- cidr:is4rfc1918 ()
-
Checks whether the CIDR instance is within the private RFC1918 address space
Usage:
local addr = luci.ip.new("192.168.45.2/24") if addr:is4rfc1918() then print("Is a private address") end
Return value:
true
if the entire range of this CIDR lies within one of the ranges10.0.0.0-10.255.255.255
,172.16.0.0-172.31.0.0
or192.168.0.0-192.168.255.255
, elsefalse
. - cidr:is4linklocal ()
-
Checks whether the CIDR instance is an IPv4 link local (Zeroconf) address
Usage:
local addr = luci.ip.new("169.254.34.125") if addr:is4linklocal() then print("Is a zeroconf address") end
Return value:
true
if the entire range of this CIDR lies within the range the range169.254.0.0-169.254.255.255
, elsefalse
. - cidr:is6 ()
-
Checks whether the CIDR instance is an IPv6 address range
Return value:
true
if the CIDR is an IPv6 range, elsefalse
See also:
- cidr:is6linklocal ()
-
Checks whether the CIDR instance is an IPv6 link local address
Usage:
local addr = luci.ip.new("fe92:53a:3216:af01:221:63ff:fe75:aa17/64") if addr:is6linklocal() then print("Is a linklocal address") end
Return value:
true
if the entire range of this CIDR lies within the range thefe80::/10
range, elsefalse
. - cidr:is6mapped4 ()
-
Checks whether the CIDR instance is an IPv6 mapped IPv4 address
Usage:
local addr = luci.ip.new("::ffff:192.168.1.1") if addr:is6mapped4() then print("Is a mapped IPv4 address") end
Return value:
true
if the address is an IPv6 mapped IPv4 address in the form::ffff:1.2.3.4
. - cidr:lower (addr)
-
Checks whether this CIDR instance is lower than the given argument.
The comparisation follows these rules:
- An IPv4 address is always lower than an IPv6 address
- Prefix sizes are ignored
Parameters
-
addr: A
luci.ip.cidr
instance or a string convertable byluci.ip.new()
to compare against.
Usage:
local addr = luci.ip.new("192.168.1.1") print(addr:lower(addr)) -- false print(addr:lower("10.10.10.10/24")) -- false print(addr:lower(luci.ip.new("::1"))) -- true print(addr:lower(luci.ip.new("192.168.200.1"))) -- true
Return value:
true
if this CIDR is lower than the given address, elsefalse
.See also:
- cidr:higher (addr)
-
Checks whether this CIDR instance is higher than the given argument.
The comparisation follows these rules:
- An IPv4 address is always lower than an IPv6 address
- Prefix sizes are ignored
Parameters
-
addr: A
luci.ip.cidr
instance or a string convertable byluci.ip.new()
to compare against.
Usage:
local addr = luci.ip.new("192.168.1.1") print(addr:higher(addr)) -- false print(addr:higher("10.10.10.10/24")) -- true print(addr:higher(luci.ip.new("::1"))) -- false print(addr:higher(luci.ip.new("192.168.200.1"))) -- false
Return value:
true
if this CIDR is higher than the given address, elsefalse
.See also:
- cidr:equal (addr)
-
Checks whether this CIDR instance is equal to the given argument.
Parameters
-
addr: A
luci.ip.cidr
instance or a string convertable byluci.ip.new()
to compare against.
Usage:
local addr = luci.ip.new("192.168.1.1") print(addr:equal(addr)) -- true print(addr:equal("192.168.1.1")) -- true print(addr:equal(luci.ip.new("::1"))) -- false local addr6 = luci.ip.new("::1") print(addr6:equal("0:0:0:0:0:0:0:1/64")) -- true print(addr6:equal(luci.ip.new("fe80::221:63ff:fe75:aa17"))) -- false
Return value:
true
if this CIDR is equal to the given address, elsefalse
.See also:
-
addr: A
- cidr:prefix (mask)
-
Get or set prefix size of CIDR instance.
If the optional mask parameter is given, the prefix size of this CIDR is altered
else the current prefix size is returned.
Parameters
-
mask: Either a number containing the number of bits (
0..32
for IPv4,0..128
for IPv6) or a string containing a valid netmask (optional)
Usage:
local range = luci.ip.new("192.168.1.1/255.255.255.0") print(range:prefix()) -- 24 range:prefix(16) print(range:prefix()) -- 16 range:prefix("255.255.255.255") print(range:prefix()) -- 32
Return value:
Bit count of the current prefix size -
mask: Either a number containing the number of bits (
- cidr:network (mask)
-
Derive network address of CIDR instance.
Returns a new CIDR instance representing the network address of this instance
with all host parts masked out. The used prefix size can be overridden by the
optional mask parameter.
Parameters
-
mask: Either a number containing the number of bits (
0..32
for IPv4,0..128
for IPv6) or a string containing a valid netmask (optional)
Usage:
local range = luci.ip.new("192.168.62.243/255.255.0.0") print(range:network()) -- "192.168.0.0" print(range:network(24)) -- "192.168.62.0" print(range:network("255.255.255.0")) -- "192.168.62.0" local range6 = luci.ip.new("fd9b:62b3:9cc5:0:221:63ff:fe75:aa17/64") print(range6:network()) -- "fd9b:62b3:9cc5::"
Return value:
CIDR instance representing the network address -
mask: Either a number containing the number of bits (
- cidr:host ()
-
Derive host address of CIDR instance.
This function essentially constructs a copy of this CIDR with the prefix size
set to
32
for IPv4 and128
for IPv6.Usage:
local range = luci.ip.new("172.19.37.45/16") print(range) -- "172.19.37.45/16" print(range:host()) -- "172.19.37.45"
Return value:
CIDR instance representing the host address - cidr:mask (mask)
-
Derive netmask of CIDR instance.
Constructs a CIDR instance representing the netmask of this instance. The used
prefix size can be overridden by the optional mask parameter.
Parameters
-
mask: Either a number containing the number of bits (
0..32
for IPv4,0..128
for IPv6) or a string containing a valid netmask (optional)
Usage:
local range = luci.ip.new("172.19.37.45/16") print(range:mask()) -- "255.255.0.0" print(range:mask(24)) -- "255.255.255.0" print(range:mask("255.0.0.0")) -- "255.0.0.0"
Return value:
CIDR instance representing the netmask -
mask: Either a number containing the number of bits (
- cidr:broadcast (mask)
-
Derive broadcast address of CIDR instance.
Constructs a CIDR instance representing the broadcast address of this instance.
The used prefix size can be overridden by the optional mask parameter.
This function has no effect on IPv6 instances, it will return nothing in this
case.
Parameters
-
mask: Either a number containing the number of bits (
0..32
for IPv4,0..128
for IPv6) or a string containing a valid netmask (optional)
Usage:
local range = luci.ip.new("172.19.37.45/16") print(range:broadcast()) -- "172.19.255.255" print(range:broadcast(24)) -- "172.19.37.255" print(range:broadcast("255.0.0.0")) -- "172.255.255.255"
Return value:
Return a new CIDR instance representing the broadcast address if this instance is an IPv4 range, else return nothing. -
mask: Either a number containing the number of bits (
- cidr:mapped4 ()
-
Derive mapped IPv4 address of CIDR instance.
Constructs a CIDR instance representing the IPv4 address of the IPv6 mapped
IPv4 address in this instance.
This function has no effect on IPv4 instances or IPv6 instances which are not a
mapped address, it will return nothing in this case.
Usage:
local addr = luci.ip.new("::ffff:172.16.19.1") print(addr:mapped4()) -- "172.16.19.1"
Return value:
Return a new CIDR instance representing the IPv4 address if this instance is an IPv6 mapped IPv4 address, else return nothing. - cidr:contains (addr)
-
Test whether CIDR contains given range.
Parameters
-
addr: A
luci.ip.cidr
instance or a string convertable byluci.ip.new()
to test.
Usage:
local range = luci.ip.new("10.24.0.0/255.255.0.0") print(range:contains("10.24.5.1")) -- true print(range:contains("::1")) -- false print(range:contains("10.0.0.0/8")) -- false local range6 = luci.ip.new("fe80::/10") print(range6:contains("fe80::221:63f:fe75:aa17/64")) -- true print(range6:contains("fd9b:6b3:c5:0:221:63f:fe75:aa17/64")) -- false
Return value:
true
if this instance fully contains the given address elsefalse
. -
addr: A
- cidr:add (amount, inplace)
-
Add given amount to CIDR instance. If the result would overflow the maximum
address space, the result is set to the highest possible address.
Parameters
-
amount: A numeric value between 0 and 0xFFFFFFFF, a
luci.ip.cidr
instance or a string convertable byluci.ip.new()
. -
inplace: If
true
, modify this instance instead of returning a new derived CIDR instance.
Usage:
local addr = luci.ip.new("192.168.1.1/24") print(addr:add(250)) -- "192.168.1.251/24" print(addr:add("0.0.99.0")) -- "192.168.100.1/24" addr:add(256, true) -- true print(addr) -- "192.168.2.1/24 addr:add("255.0.0.0", true) -- false (overflow) print(addr) -- "255.255.255.255/24 local addr6 = luci.ip.new("fe80::221:63f:fe75:aa17/64") print(addr6:add(256)) -- "fe80::221:63f:fe75:ab17/64" print(addr6:add("::ffff:0")) -- "fe80::221:640:fe74:aa17/64" addr:add(256, true) -- true print(addr) -- "fe80::221:63f:fe75:ab17/64 addr:add("ffff::", true) -- false (overflow) print(addr) -- "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/64"
Return value:
- When adding inplace: Return
true
if the addition succeded orfalse
when the addition overflowed. - When deriving new CIDR: Return new instance representing the value of this instance plus the added amount or the highest possible address if the addition overflowed the available address space.
-
amount: A numeric value between 0 and 0xFFFFFFFF, a
- cidr:sub (amount, inplace)
-
Substract given amount from CIDR instance. If the result would under, the lowest
possible address is returned.
Parameters
-
amount: A numeric value between 0 and 0xFFFFFFFF, a
luci.ip.cidr
instance or a string convertable byluci.ip.new()
. -
inplace: If
true
, modify this instance instead of returning a new derived CIDR instance.
Usage:
local addr = luci.ip.new("192.168.1.1/24") print(addr:sub(256)) -- "192.168.0.1/24" print(addr:sub("0.168.0.0")) -- "192.0.1.1/24" addr:sub(256, true) -- true print(addr) -- "192.168.0.1/24 addr:sub("255.0.0.0", true) -- false (underflow) print(addr) -- "0.0.0.0/24 local addr6 = luci.ip.new("fe80::221:63f:fe75:aa17/64") print(addr6:sub(256)) -- "fe80::221:63f:fe75:a917/64" print(addr6:sub("::ffff:0")) -- "fe80::221:63e:fe76:aa17/64" addr:sub(256, true) -- true print(addr) -- "fe80::221:63f:fe75:a917/64" addr:sub("ffff::", true) -- false (underflow) print(addr) -- "::/64"
Return value:
- When substracting inplace: Return
true
if the substraction succeded orfalse
when the substraction underflowed. - When deriving new CIDR: Return new instance representing the value of this instance minus the substracted amount or the lowest address if the substraction underflowed.
-
amount: A numeric value between 0 and 0xFFFFFFFF, a
- cidr:minhost ()
-
Calculate the lowest possible host address within this CIDR instance.
Usage:
local addr = luci.ip.new("192.168.123.56/24") print(addr:minhost()) -- "192.168.123.1" local addr6 = luci.ip.new("fd9b:62b3:9cc5:0:221:63ff:fe75:aa17/64") print(addr6:minhost()) -- "fd9b:62b3:9cc5::1"
Return value:
Returns a new CIDR instance representing the lowest host address within this range. - cidr:maxhost ()
-
Calculate the highest possible host address within this CIDR instance.
Usage:
local addr = luci.ip.new("192.168.123.56/24") print(addr:maxhost()) -- "192.168.123.254" (.255 is broadcast) local addr6 = luci.ip.new("fd9b:62b3:9cc5:0:221:63ff:fe75:aa17/64") print(addr6:maxhost()) -- "fd9b:62b3:9cc5:0:ffff:ffff:ffff:ffff"
Return value:
Returns a new CIDR instance representing the highest host address within this range. - cidr:string ()
-
Convert CIDR instance into string representation.
If the prefix size of instance is less than 32 for IPv4 or 128 for IPv6, the
address is returned in the form "address/prefix" otherwise just "address".
It is usually not required to call this function directly as CIDR objects
define it as __tostring function in the associated metatable.
Return value:
Returns a string representing the range or address of this CIDR instance