aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAsbjørn Sloth Tønnesen <ast@2e8.dk>2019-07-18 21:35:33 +0000
committerAsbjørn Sloth Tønnesen <ast@2e8.dk>2019-07-18 21:35:33 +0000
commit1f2c6d833c579c8c3169e22ef8af00265a438406 (patch)
tree1ab27f5e85da78f1b67e001c22b9015260cbdfd1
parentcfec9c2570b9f185447025a17c735f44a7b9f525 (diff)
downloadlua-inet-1f2c6d833c579c8c3169e22ef8af00265a438406.tar.gz
lua-inet-1f2c6d833c579c8c3169e22ef8af00265a438406.tar.xz
lua-inet-1f2c6d833c579c8c3169e22ef8af00265a438406.zip
add :hostmask()
Signed-off-by: Asbjørn Sloth Tønnesen <ast@2e8.dk>
-rw-r--r--README.rst14
-rw-r--r--lua/inet/core.lua11
2 files changed, 25 insertions, 0 deletions
diff --git a/README.rst b/README.rst
index df955e2..186fca3 100644
--- a/README.rst
+++ b/README.rst
@@ -80,6 +80,7 @@ Operator Description
``:ipstring()`` ip as string without prefix
``:cidrstring()`` format CIDR notation
``:netmask()`` generate netmask as an address
+``:hostmask()`` generate hostmask as an address
``:flip()`` flip the least significant network bit
================= ======================================
@@ -239,6 +240,19 @@ Build an IP address mask with the netmask of ``foo``.
inet('2001:db8::/64'):netmask() -- returns inet('ffff:ffff:ffff:ffff::')
+``foo:hostmask()``
+~~~~~~~~~~~~~~~~~
+
+Build an IP address mask with the netmask of ``foo``.
+
+::
+
+ inet('192.0.2.0/24'):hostmask() -- returns inet('0.0.0.255')
+ inet('2001:db8::/64'):hostmask() -- returns inet('::ffff:ffff:ffff:ffff')
+ inet('2001:db8::/116'):hostmask() -- returns inet('::fff')
+ inet('2001:db8::/112'):hostmask() -- returns inet('::ffff')
+
+
``foo:flip()``
~~~~~~~~~~~~~~
diff --git a/lua/inet/core.lua b/lua/inet/core.lua
index 0b062e7..f8e17cc 100644
--- a/lua/inet/core.lua
+++ b/lua/inet/core.lua
@@ -385,6 +385,12 @@ function inet4:netmask()
return new_inet4(replace(0xffffffff, 0, 0, hostbits), 32)
end
+function inet4:hostmask()
+ local mask = self.mask
+ local hostbits = 32 - mask
+ return new_inet4(replace(0xffffffff, 0, hostbits, mask), 32)
+end
+
function inet4:flip()
-- find twin by flipping the last network bit
local mask = self.mask
@@ -716,6 +722,11 @@ function inet6:netmask()
return build_inet6_mask(0, mask, 128 - mask)
end
+function inet6:hostmask()
+ local mask = self.mask
+ return build_inet6_mask(mask, 128 - mask, 0)
+end
+
function inet6:flip()
-- find twin by flipping the last network bit
local mask = self.mask