From e92fb277616ede376d81853c052bc399c198e15d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Sloth=20T=C3=B8nnesen?= Date: Thu, 18 Jul 2019 18:43:42 +0000 Subject: use and test mixed_networks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Asbjørn Sloth Tønnesen --- README.rst | 12 +++++++++++- lua/inet/core.lua | 8 +++++++- lua/inet/init.lua | 1 + 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index f243fab..274b7d3 100644 --- a/README.rst +++ b/README.rst @@ -305,7 +305,7 @@ Like ``tostring(foo)``, but always return the address in CIDR notation, as speci ``foo:ipstring()`` ~~~~~~~~~~~~~~~~~~ -Like ``tostring(foo)``, but always just return the IP address. +Like ``tostring(foo)``, but always returns the only the IP address, and not the mask. :: @@ -316,11 +316,21 @@ Like ``tostring(foo)``, but always just return the IP address. Like ``foo:ipstring()``, but always uses mixed notation. +:: + + inet('2001:db8::c000:218'):ipstring() -- returns '2001:db8::c000:218' + inet('2001:db8::c000:218'):ipstring4() -- returns '2001:db8::192.0.2.24' + ``foo:ipstring6()`` ~~~~~~~~~~~~~~~~~~~ Like ``tostring(foo)``, but never uses mixed notation. +:: + + inet('::ffff:192.0.2.24'):ipstring() -- returns '::ffff:192.0.2.24' + inet('::ffff:192.0.2.24'):ipstring6() -- returns '::ffff:c000:218' + Sets ---- diff --git a/lua/inet/core.lua b/lua/inet/core.lua index f9ff6e1..f3ae228 100644 --- a/lua/inet/core.lua +++ b/lua/inet/core.lua @@ -12,6 +12,8 @@ local bxor = bit32.bxor local get_mt = common.get_mt +local mixed_networks + local mt2fam = {} local inet = {} @@ -428,7 +430,7 @@ local function tostr6(self, withmask, embeddedipv4) local zeros = {} if embeddedipv4 == nil then - embeddedipv4 = false -- TODO check if well-known prefix + embeddedipv4 = mixed_networks:contains(self) end local ipv6pieces = 8 @@ -674,6 +676,10 @@ end local M = {} +function M.set_mixed_networks(mixed_set) + mixed_networks = mixed_set +end + M.is_inet4 = is_inet4 M.is_inet6 = is_inet6 M.is_inet = is_inet diff --git a/lua/inet/init.lua b/lua/inet/init.lua index 2dc7855..a504ddd 100644 --- a/lua/inet/init.lua +++ b/lua/inet/init.lua @@ -14,6 +14,7 @@ do local mixed_networks = set.new() mixed_networks:add(new_inet('::ffff:0.0.0.0/96')) M.mixed_networks = mixed_networks + core.set_mixed_networks(mixed_networks) end M.is4 = core.is_inet4 -- cgit v1.2.1