util.ip: Convert IPv4 mapped addresses to hex.
authorKim Alvefur <zash@zash.se>
Sat, 18 May 2013 11:14:19 +0000 (13:14 +0200)
committerKim Alvefur <zash@zash.se>
Sat, 18 May 2013 11:14:19 +0000 (13:14 +0200)
util/ip.lua

index de287b163b90a1b48e827a57f18680954608b0b8..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