X-Git-Url: https://git.enpas.org/?a=blobdiff_plain;f=util%2Fip.lua;h=81a98ef7dcec640c429c356baa0f8f845fba0a5e;hb=2518a9b0cd8e43f84c98e26580b931c510d2944b;hp=d0ae07eb8aa3c57cd18e178b20ff10c391d0ed4b;hpb=ac8c3154923599cf1226ae4d7ea5a6ab0d473ea5;p=prosody.git diff --git a/util/ip.lua b/util/ip.lua index d0ae07eb..81a98ef7 100644 --- a/util/ip.lua +++ b/util/ip.lua @@ -25,6 +25,10 @@ local function new_ip(ipStr, proto) elseif proto ~= "IPv4" and proto ~= "IPv6" then return nil, "invalid protocol"; end + local zone; + if proto == "IPv6" and ipStr:find('%', 1, true) then + ipStr, zone = ipStr:match("^(.-)%%(.*)"); + end if proto == "IPv6" and ipStr:find('.', 1, true) then local changed; ipStr, changed = ipStr:gsub(":(%d+)%.(%d+)%.(%d+)%.(%d+)$", function(a,b,c,d) @@ -33,7 +37,7 @@ local function new_ip(ipStr, proto) if changed ~= 1 then return nil, "invalid-address"; end end - return setmetatable({ addr = ipStr, proto = proto }, ip_mt); + return setmetatable({ addr = ipStr, proto = proto, zone = zone }, ip_mt); end local function toBits(ip) @@ -47,15 +51,15 @@ local function toBits(ip) if not ip:match(":$") then fields[#fields] = nil; end for i, field in ipairs(fields) do if field:len() == 0 and i ~= 1 and i ~= #fields then - for i = 1, 16 * (9 - #fields) do + for _ = 1, 16 * (9 - #fields) do result = result .. "0"; end else - for i = 1, 4 - field:len() do + for _ = 1, 4 - field:len() do result = result .. "0000"; end - for i = 1, field:len() do - result = result .. hex2bits[field:sub(i,i)]; + for j = 1, field:len() do + result = result .. hex2bits[field:sub(j, j)]; end end end @@ -229,13 +233,10 @@ end local function match(ipA, ipB, bits) local common_bits = commonPrefixLength(ipA, ipB); - if not bits then - return ipA == ipB; - end if bits and ipB.proto == "IPv4" then common_bits = common_bits - 96; -- v6 mapped addresses always share these bits end - return common_bits >= bits; + return common_bits >= (bits or 128); end return {new_ip = new_ip,