util.ip: Support zone id syntax in IPv6 addresses
authorKim Alvefur <zash@zash.se>
Mon, 4 Jan 2016 16:47:40 +0000 (17:47 +0100)
committerKim Alvefur <zash@zash.se>
Mon, 4 Jan 2016 16:47:40 +0000 (17:47 +0100)
util/ip.lua

index 043303eebb311ce6a1d07bcc23b9bdf97db4e620..acfd7f247c3c7e0c056544763cb4350edaacbff4 100644 (file)
@@ -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)