aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAsbjørn Sloth Tønnesen <ast@2e8.dk>2019-07-16 19:19:20 +0000
committerAsbjørn Sloth Tønnesen <ast@2e8.dk>2019-07-16 19:19:20 +0000
commit930296cfaadb49a8205231098502eaf20af2121a (patch)
tree3b72d5e54732afe414fd4c8bc4f4da8a750d415c
parentabadb6492b65b18bb54968f8f86e5ca770443335 (diff)
downloadlua-inet-930296cfaadb49a8205231098502eaf20af2121a.tar.gz
lua-inet-930296cfaadb49a8205231098502eaf20af2121a.tar.xz
lua-inet-930296cfaadb49a8205231098502eaf20af2121a.zip
inet6: fix text representation to comply with RFC 5952
Signed-off-by: Asbjørn Sloth Tønnesen <ast@2e8.dk>
-rw-r--r--lua/inet/init.lua41
1 files changed, 34 insertions, 7 deletions
diff --git a/lua/inet/init.lua b/lua/inet/init.lua
index 328032f..b91f845 100644
--- a/lua/inet/init.lua
+++ b/lua/inet/init.lua
@@ -417,15 +417,29 @@ function inet6:balance(quick)
return self
end
-local function tostr6(self, withmask)
+local function tohex(n)
+ if n == nil then return nil end
+ return format('%x', n)
+end
+
+local function tostr6(self, withmask, embeddedipv4)
-- return human readable
local pcs = self.pcs
local zeros = {}
+ if embeddedipv4 == nil then
+ embeddedipv4 = false -- TODO check if well-known prefix
+ end
+
+ local ipv6pieces = 8
+ if embeddedipv4 then
+ ipv6pieces = 6
+ end
+
-- count zero clusters
local first_zero = 0
local prev_was_zero = false
- for i=1,#pcs do
+ for i=1,ipv6pieces do
if pcs[i] == 0 then
if prev_was_zero then
zeros[first_zero] = zeros[first_zero] + 1
@@ -439,10 +453,11 @@ local function tostr6(self, withmask)
end
end
- -- find the largest zero cluster
+ -- find the first largest zero cluster
local zeros_begin = nil
- local zeros_cnt = 0
- for begin,cnt in pairs(zeros) do
+ local zeros_cnt = 1
+ for begin=1,ipv6pieces do
+ local cnt = zeros[begin] or 0
if cnt > zeros_cnt then
zeros_begin = begin
zeros_cnt = cnt
@@ -452,7 +467,7 @@ local function tostr6(self, withmask)
-- format ipv6 address
local out = ''
local i = 1
- while i <= 8 do
+ while i <= ipv6pieces do
if i == zeros_begin then
if i > 1 then
out = out .. ':'
@@ -462,7 +477,7 @@ local function tostr6(self, withmask)
i = i + zeros_cnt
else
local p = pcs[i]
- local hexdigits = string.format('%x', p)
+ local hexdigits = tohex(p)
out = out .. hexdigits
if i ~= 8 then
out = out .. ':'
@@ -471,6 +486,10 @@ local function tostr6(self, withmask)
end
end
+ if embeddedipv4 then
+ out = out .. new_inet4(lshift(pcs[7], 16) + pcs[8]):ipstring()
+ end
+
local mask = self.mask
if (mask == nil or mask == 128 or withmask == false) and withmask ~= true then
return out
@@ -487,6 +506,14 @@ function inet6:ipstring()
return tostr6(self, false)
end
+function inet6:ipstring4()
+ return tostr6(self, false, true)
+end
+
+function inet6:ipstring6()
+ return tostr6(self, false, false)
+end
+
function inet6:cidrstring()
return tostr6(self, true)
end