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)
15 if proto ~= "IPv4" and proto ~= "IPv6" then
16 return nil, "invalid protocol";
18 if proto == "IPv6" and ipStr:find('.', 1, true) then
20 ipStr, changed = ipStr:gsub(":(%d+)%.(%d+)%.(%d+)%.(%d+)$", function(a,b,c,d)
21 return (":%04X:%04X"):format(a*256+b,c*256+d);
23 if changed ~= 1 then return nil, "invalid-address"; end
26 return setmetatable({ addr = ipStr, proto = proto }, ip_mt);
29 local function toBits(ip)
32 if ip.proto == "IPv4" then
35 ip = (ip.addr):upper();
36 ip:gsub("([^:]*):?", function (c) fields[#fields + 1] = c end);
37 if not ip:match(":$") then fields[#fields] = nil; end
38 for i, field in ipairs(fields) do
39 if field:len() == 0 and i ~= 1 and i ~= #fields then
40 for i = 1, 16 * (9 - #fields) do
41 result = result .. "0";
44 for i = 1, 4 - field:len() do
45 result = result .. "0000";
47 for i = 1, field:len() do
48 result = result .. hex2bits[field:sub(i,i)];
55 local function commonPrefixLength(ipA, ipB)
56 ipA, ipB = toBits(ipA), toBits(ipB);
58 if ipA:sub(i,i) ~= ipB:sub(i,i) then
65 local function v4scope(ip)
67 ip:gsub("([^.]*).?", function (c) fields[#fields + 1] = tonumber(c) end);
69 if fields[1] == 127 then
71 -- Link-local unicast:
72 elseif fields[1] == 169 and fields[2] == 254 then
80 local function v6scope(ip)
82 if ip:match("^[0:]*1$") then
84 -- Link-local unicast:
85 elseif ip:match("^[Ff][Ee][89ABab]") then
87 -- Site-local unicast:
88 elseif ip:match("^[Ff][Ee][CcDdEeFf]") then
91 elseif ip:match("^[Ff][Ff]") then
92 return tonumber("0x"..ip:sub(4,4));
99 local function label(ip)
100 if commonPrefixLength(ip, new_ip("::1", "IPv6")) == 128 then
102 elseif commonPrefixLength(ip, new_ip("2002::", "IPv6")) >= 16 then
104 elseif commonPrefixLength(ip, new_ip("2001::", "IPv6")) >= 32 then
106 elseif commonPrefixLength(ip, new_ip("fc00::", "IPv6")) >= 7 then
108 elseif commonPrefixLength(ip, new_ip("fec0::", "IPv6")) >= 10 then
110 elseif commonPrefixLength(ip, new_ip("3ffe::", "IPv6")) >= 16 then
112 elseif commonPrefixLength(ip, new_ip("::", "IPv6")) >= 96 then
114 elseif commonPrefixLength(ip, new_ip("::ffff:0:0", "IPv6")) >= 96 then
121 local function precedence(ip)
122 if commonPrefixLength(ip, new_ip("::1", "IPv6")) == 128 then
124 elseif commonPrefixLength(ip, new_ip("2002::", "IPv6")) >= 16 then
126 elseif commonPrefixLength(ip, new_ip("2001::", "IPv6")) >= 32 then
128 elseif commonPrefixLength(ip, new_ip("fc00::", "IPv6")) >= 7 then
130 elseif commonPrefixLength(ip, new_ip("fec0::", "IPv6")) >= 10 then
132 elseif commonPrefixLength(ip, new_ip("3ffe::", "IPv6")) >= 16 then
134 elseif commonPrefixLength(ip, new_ip("::", "IPv6")) >= 96 then
136 elseif commonPrefixLength(ip, new_ip("::ffff:0:0", "IPv6")) >= 96 then
143 local function toV4mapped(ip)
145 local ret = "::ffff:";
146 ip:gsub("([^.]*).?", function (c) fields[#fields + 1] = tonumber(c) end);
147 ret = ret .. ("%02x"):format(fields[1]);
148 ret = ret .. ("%02x"):format(fields[2]);
150 ret = ret .. ("%02x"):format(fields[3]);
151 ret = ret .. ("%02x"):format(fields[4]);
152 return new_ip(ret, "IPv6");
155 function ip_methods:toV4mapped()
156 if self.proto ~= "IPv4" then return nil, "No IPv4 address" end
157 local value = toV4mapped(self.addr);
158 self.toV4mapped = value;
162 function ip_methods:label()
164 if self.proto == "IPv4" then
165 value = label(self.toV4mapped);
173 function ip_methods:precedence()
175 if self.proto == "IPv4" then
176 value = precedence(self.toV4mapped);
178 value = precedence(self);
180 self.precedence = value;
184 function ip_methods:scope()
186 if self.proto == "IPv4" then
187 value = v4scope(self.addr);
189 value = v6scope(self.addr);
195 return {new_ip = new_ip,
196 commonPrefixLength = commonPrefixLength};