aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2017-12-01 04:37:33 +0100
committerKim Alvefur <zash@zash.se>2017-12-01 04:37:33 +0100
commite9e827557b7c6c6351a49aa7a1c1e868b1723b62 (patch)
tree52f7eb2df3098a3911812b66af6c6a1d936282d3
parent0152e8b7c6820e288e9498ea4f6519984dd06b38 (diff)
downloadprosody-e9e827557b7c6c6351a49aa7a1c1e868b1723b62.tar.gz
prosody-e9e827557b7c6c6351a49aa7a1c1e868b1723b62.zip
util.ip: Make bit string function into a method
-rw-r--r--util/ip.lua16
1 files changed, 9 insertions, 7 deletions
diff --git a/util/ip.lua b/util/ip.lua
index 0e01c067..5cf612f8 100644
--- a/util/ip.lua
+++ b/util/ip.lua
@@ -51,12 +51,7 @@ local function new_ip(ipStr, proto)
return setmetatable({ addr = ipStr, packed = packed, proto = proto, zone = zone }, ip_mt);
end
-local function toBits(ip)
- local result = "";
- local fields = {};
- if ip.proto == "IPv4" then
- ip = ip.toV4mapped;
- end
+function ip_methods.bits(ip)
ip = (ip.addr):upper();
ip:gsub("([^:]*):?", function (c) fields[#fields + 1] = c end);
if not ip:match(":$") then fields[#fields] = nil; end
@@ -77,8 +72,15 @@ local function toBits(ip)
return result;
end
+function ip_methods.bits_full(ip)
+ if ip.proto == "IPv4" then
+ ip = ip.toV4mapped;
+ end
+ return ip.bits;
+end
+
local function commonPrefixLength(ipA, ipB)
- ipA, ipB = toBits(ipA), toBits(ipB);
+ ipA, ipB = ipA.bits_full, ipB.bits_full;
for i = 1, 128 do
if ipA:sub(i,i) ~= ipB:sub(i,i) then
return i-1;