2 -- Copyright (C) 2008-2011 Florian Zeitz
4 -- This project is MIT/X11 licensed. Please see the
5 -- COPYING file in the source package for more information.
9 local ip_mt = { __index = function (ip, key) return (ip_methods[key])(ip); end,
10 __tostring = function (ip) return ip.addr; end,
11 __eq = function (ipA, ipB) return ipA.addr == ipB.addr; end};
12 local hex2bits = { ["0"] = "0000", ["1"] = "0001", ["2"] = "0010", ["3"] = "0011", ["4"] = "0100", ["5"] = "0101", ["6"] = "0110", ["7"] = "0111", ["8"] = "1000", ["9"] = "1001", ["A"] = "1010", ["B"] = "1011", ["C"] = "1100", ["D"] = "1101", ["E"] = "1110", ["F"] = "1111" };
14 local function new_ip(ipStr, proto)
16 local sep = ipStr:match("^%x+(.)");
17 if sep == ":" or (not(sep) and ipStr:sub(1,1) == ":") then
19 elseif sep == "." then
23 return nil, "invalid address";
25 elseif proto ~= "IPv4" and proto ~= "IPv6" then
26 return nil, "invalid protocol";
29 if proto == "IPv6" and ipStr:find('%', 1, true) then
30 ipStr, zone = ipStr:match("^(.-)%%(.*)");
32 if proto == "IPv6" and ipStr:find('.', 1, true) then
34 ipStr, changed = ipStr:gsub(":(%d+)%.(%d+)%.(%d+)%.(%d+)$", function(a,b,c,d)
35 return (":%04X:%04X"):format(a*256+b,c*256+d);
37 if changed ~= 1 then return nil, "invalid-address"; end
40 return setmetatable({ addr = ipStr, proto = proto, zone = zone }, ip_mt);
43 local function toBits(ip)
46 if ip.proto == "IPv4" then
49 ip = (ip.addr):upper();
50 ip:gsub("([^:]*):?", function (c) fields[#fields + 1] = c end);
51 if not ip:match(":$") then fields[#fields] = nil; end
52 for i, field in ipairs(fields) do
53 if field:len() == 0 and i ~= 1 and i ~= #fields then
54 for i = 1, 16 * (9 - #fields) do
55 result = result .. "0";
58 for i = 1, 4 - field:len() do
59 result = result .. "0000";
61 for i = 1, field:len() do
62 result = result .. hex2bits[field:sub(i,i)];
69 local function commonPrefixLength(ipA, ipB)
70 ipA, ipB = toBits(ipA), toBits(ipB);
72 if ipA:sub(i,i) ~= ipB:sub(i,i) then
79 local function v4scope(ip)
81 ip:gsub("([^.]*).?", function (c) fields[#fields + 1] = tonumber(c) end);
83 if fields[1] == 127 then
85 -- Link-local unicast:
86 elseif fields[1] == 169 and fields[2] == 254 then
94 local function v6scope(ip)
96 if ip:match("^[0:]*1$") then
98 -- Link-local unicast:
99 elseif ip:match("^[Ff][Ee][89ABab]") then
101 -- Site-local unicast:
102 elseif ip:match("^[Ff][Ee][CcDdEeFf]") then
105 elseif ip:match("^[Ff][Ff]") then
106 return tonumber("0x"..ip:sub(4,4));
113 local function label(ip)
114 if commonPrefixLength(ip, new_ip("::1", "IPv6")) == 128 then
116 elseif commonPrefixLength(ip, new_ip("2002::", "IPv6")) >= 16 then
118 elseif commonPrefixLength(ip, new_ip("2001::", "IPv6")) >= 32 then
120 elseif commonPrefixLength(ip, new_ip("fc00::", "IPv6")) >= 7 then
122 elseif commonPrefixLength(ip, new_ip("fec0::", "IPv6")) >= 10 then
124 elseif commonPrefixLength(ip, new_ip("3ffe::", "IPv6")) >= 16 then
126 elseif commonPrefixLength(ip, new_ip("::", "IPv6")) >= 96 then
128 elseif commonPrefixLength(ip, new_ip("::ffff:0:0", "IPv6")) >= 96 then
135 local function precedence(ip)
136 if commonPrefixLength(ip, new_ip("::1", "IPv6")) == 128 then
138 elseif commonPrefixLength(ip, new_ip("2002::", "IPv6")) >= 16 then
140 elseif commonPrefixLength(ip, new_ip("2001::", "IPv6")) >= 32 then
142 elseif commonPrefixLength(ip, new_ip("fc00::", "IPv6")) >= 7 then
144 elseif commonPrefixLength(ip, new_ip("fec0::", "IPv6")) >= 10 then
146 elseif commonPrefixLength(ip, new_ip("3ffe::", "IPv6")) >= 16 then
148 elseif commonPrefixLength(ip, new_ip("::", "IPv6")) >= 96 then
150 elseif commonPrefixLength(ip, new_ip("::ffff:0:0", "IPv6")) >= 96 then
157 local function toV4mapped(ip)
159 local ret = "::ffff:";
160 ip:gsub("([^.]*).?", function (c) fields[#fields + 1] = tonumber(c) end);
161 ret = ret .. ("%02x"):format(fields[1]);
162 ret = ret .. ("%02x"):format(fields[2]);
164 ret = ret .. ("%02x"):format(fields[3]);
165 ret = ret .. ("%02x"):format(fields[4]);
166 return new_ip(ret, "IPv6");
169 function ip_methods:toV4mapped()
170 if self.proto ~= "IPv4" then return nil, "No IPv4 address" end
171 local value = toV4mapped(self.addr);
172 self.toV4mapped = value;
176 function ip_methods:label()
178 if self.proto == "IPv4" then
179 value = label(self.toV4mapped);
187 function ip_methods:precedence()
189 if self.proto == "IPv4" then
190 value = precedence(self.toV4mapped);
192 value = precedence(self);
194 self.precedence = value;
198 function ip_methods:scope()
200 if self.proto == "IPv4" then
201 value = v4scope(self.addr);
203 value = v6scope(self.addr);
209 return {new_ip = new_ip,
210 commonPrefixLength = commonPrefixLength};