mod_bosh: pcall() core_process_stanza per stanza, to bring in line with other listene...
[prosody.git] / util / ip.lua
index 5e2f46bb97b0b093a3388f43015a3365067f6ea8..856bf03498261860db5796afccac66258c33beae 100644 (file)
@@ -15,6 +15,13 @@ local function new_ip(ipStr, proto)
        if proto ~= "IPv4" and proto ~= "IPv6" then
                return nil, "invalid protocol";
        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)
+                       return (":%04X:%04X"):format(a*256+b,c*256+d);
+               end);
+               if changed ~= 1 then return nil, "invalid-address"; end
+       end
 
        return setmetatable({ addr = ipStr, proto = proto }, ip_mt);
 end
@@ -64,9 +71,6 @@ local function v4scope(ip)
        -- Link-local unicast:
        elseif fields[1] == 169 and fields[2] == 254 then
                return 0x2;
-       -- Site-local unicast:
-       elseif (fields[1] == 10) or (fields[1] == 192 and fields[2] == 168) or (fields[1] == 172 and fields[2] > 16) then
-               return 0x5;
        -- Global unicast:
        else
                return 0xE;
@@ -97,6 +101,14 @@ local function label(ip)
                return 0;
        elseif commonPrefixLength(ip, new_ip("2002::", "IPv6")) >= 16 then
                return 2;
+       elseif commonPrefixLength(ip, new_ip("2001::", "IPv6")) >= 32 then
+               return 5;
+       elseif commonPrefixLength(ip, new_ip("fc00::", "IPv6")) >= 7 then
+               return 13;
+       elseif commonPrefixLength(ip, new_ip("fec0::", "IPv6")) >= 10 then
+               return 11;
+       elseif commonPrefixLength(ip, new_ip("3ffe::", "IPv6")) >= 16 then
+               return 12;
        elseif commonPrefixLength(ip, new_ip("::", "IPv6")) >= 96 then
                return 3;
        elseif commonPrefixLength(ip, new_ip("::ffff:0:0", "IPv6")) >= 96 then
@@ -111,10 +123,18 @@ local function precedence(ip)
                return 50;
        elseif commonPrefixLength(ip, new_ip("2002::", "IPv6")) >= 16 then
                return 30;
+       elseif commonPrefixLength(ip, new_ip("2001::", "IPv6")) >= 32 then
+               return 5;
+       elseif commonPrefixLength(ip, new_ip("fc00::", "IPv6")) >= 7 then
+               return 3;
+       elseif commonPrefixLength(ip, new_ip("fec0::", "IPv6")) >= 10 then
+               return 1;
+       elseif commonPrefixLength(ip, new_ip("3ffe::", "IPv6")) >= 16 then
+               return 1;
        elseif commonPrefixLength(ip, new_ip("::", "IPv6")) >= 96 then
-               return 20;
+               return 1;
        elseif commonPrefixLength(ip, new_ip("::ffff:0:0", "IPv6")) >= 96 then
-               return 10;
+               return 35;
        else
                return 40;
        end