X-Git-Url: https://git.enpas.org/?a=blobdiff_plain;f=util%2Fip.lua;h=ec3b4d7e56ba4c8342d11e5af00a044d25fe418e;hb=76e441c85feb7186cb9b1dcfb5b89fe66bb709f7;hp=62649c9b8567d6afe1aacc85d0aa91b7948245db;hpb=ecd2e6962f8993af96ffa869659f7949e64054ae;p=prosody.git diff --git a/util/ip.lua b/util/ip.lua index 62649c9b..ec3b4d7e 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) @@ -92,7 +96,7 @@ local function v6scope(ip) if ip:match("^[0:]*1$") then return 0x2; -- Link-local unicast: - elseif ip:match("^[Ff][Ee][89ABab]") then + elseif ip:match("^[Ff][Ee][89ABab]") then return 0x2; -- Site-local unicast: elseif ip:match("^[Ff][Ee][CcDdEeFf]") then @@ -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,