util.ip: Convert IPv4 mapped addresses to hex.
authorKim Alvefur <zash@zash.se>
Sat, 18 May 2013 13:14:19 +0200
changeset 5597 6fe09707c73b
parent 5596 73fea1a87afd
child 5598 3bb8aefd8ce0
util.ip: Convert IPv4 mapped addresses to hex.
util/ip.lua
--- a/util/ip.lua	Sat May 18 13:11:44 2013 +0200
+++ b/util/ip.lua	Sat May 18 13:14:19 2013 +0200
@@ -15,6 +15,13 @@
 	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