Comparison

util/ip.lua @ 7055:23de70d19e77

util.ip: Support zone id syntax in IPv6 addresses
author Kim Alvefur <zash@zash.se>
date Mon, 04 Jan 2016 17:47:40 +0100
parent 7053:3049137d14b6
child 7061:eda0feeaf759
comparison
equal deleted inserted replaced
7054:94d5e2f33a10 7055:23de70d19e77
23 return nil, "invalid address"; 23 return nil, "invalid address";
24 end 24 end
25 elseif proto ~= "IPv4" and proto ~= "IPv6" then 25 elseif proto ~= "IPv4" and proto ~= "IPv6" then
26 return nil, "invalid protocol"; 26 return nil, "invalid protocol";
27 end 27 end
28 local zone;
29 if proto == "IPv6" and ipStr:find('%', 1, true) then
30 ipStr, zone = ipStr:match("^(.-)%%(.*)");
31 end
28 if proto == "IPv6" and ipStr:find('.', 1, true) then 32 if proto == "IPv6" and ipStr:find('.', 1, true) then
29 local changed; 33 local changed;
30 ipStr, changed = ipStr:gsub(":(%d+)%.(%d+)%.(%d+)%.(%d+)$", function(a,b,c,d) 34 ipStr, changed = ipStr:gsub(":(%d+)%.(%d+)%.(%d+)%.(%d+)$", function(a,b,c,d)
31 return (":%04X:%04X"):format(a*256+b,c*256+d); 35 return (":%04X:%04X"):format(a*256+b,c*256+d);
32 end); 36 end);
33 if changed ~= 1 then return nil, "invalid-address"; end 37 if changed ~= 1 then return nil, "invalid-address"; end
34 end 38 end
35 39
36 return setmetatable({ addr = ipStr, proto = proto }, ip_mt); 40 return setmetatable({ addr = ipStr, proto = proto, zone = zone }, ip_mt);
37 end 41 end
38 42
39 local function toBits(ip) 43 local function toBits(ip)
40 local result = ""; 44 local result = "";
41 local fields = {}; 45 local fields = {};