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";
19 return setmetatable({ addr = ipStr, proto = proto }, ip_mt);
22 local function toBits(ip)
25 if ip.proto == "IPv4" then
28 ip = (ip.addr):upper();
29 ip:gsub("([^:]*):?", function (c) fields[#fields + 1] = c end);
30 if not ip:match(":$") then fields[#fields] = nil; end
31 for i, field in ipairs(fields) do
32 if field:len() == 0 and i ~= 1 and i ~= #fields then
33 for i = 1, 16 * (9 - #fields) do
34 result = result .. "0";
37 for i = 1, 4 - field:len() do
38 result = result .. "0000";
40 for i = 1, field:len() do
41 result = result .. hex2bits[field:sub(i,i)];
48 local function commonPrefixLength(ipA, ipB)
49 ipA, ipB = toBits(ipA), toBits(ipB);
51 if ipA:sub(i,i) ~= ipB:sub(i,i) then
58 local function v4scope(ip)
60 ip:gsub("([^.]*).?", function (c) fields[#fields + 1] = tonumber(c) end);
62 if fields[1] == 127 then
64 -- Link-local unicast:
65 elseif fields[1] == 169 and fields[2] == 254 then
67 -- Site-local unicast:
68 elseif (fields[1] == 10) or (fields[1] == 192 and fields[2] == 168) or (fields[1] == 172 and (fields[2] >= 16 and fields[2] < 32)) then
76 local function v6scope(ip)
78 if ip:match("^[0:]*1$") then
80 -- Link-local unicast:
81 elseif ip:match("^[Ff][Ee][89ABab]") then
83 -- Site-local unicast:
84 elseif ip:match("^[Ff][Ee][CcDdEeFf]") then
87 elseif ip:match("^[Ff][Ff]") then
88 return tonumber("0x"..ip:sub(4,4));
95 local function label(ip)
96 if commonPrefixLength(ip, new_ip("::1", "IPv6")) == 128 then
98 elseif commonPrefixLength(ip, new_ip("2002::", "IPv6")) >= 16 then
100 elseif commonPrefixLength(ip, new_ip("::", "IPv6")) >= 96 then
102 elseif commonPrefixLength(ip, new_ip("::ffff:0:0", "IPv6")) >= 96 then
109 local function precedence(ip)
110 if commonPrefixLength(ip, new_ip("::1", "IPv6")) == 128 then
112 elseif commonPrefixLength(ip, new_ip("2002::", "IPv6")) >= 16 then
114 elseif commonPrefixLength(ip, new_ip("::", "IPv6")) >= 96 then
116 elseif commonPrefixLength(ip, new_ip("::ffff:0:0", "IPv6")) >= 96 then
123 local function toV4mapped(ip)
125 local ret = "::ffff:";
126 ip:gsub("([^.]*).?", function (c) fields[#fields + 1] = tonumber(c) end);
127 ret = ret .. ("%02x"):format(fields[1]);
128 ret = ret .. ("%02x"):format(fields[2]);
130 ret = ret .. ("%02x"):format(fields[3]);
131 ret = ret .. ("%02x"):format(fields[4]);
132 return new_ip(ret, "IPv6");
135 function ip_methods:toV4mapped()
136 if self.proto ~= "IPv4" then return nil, "No IPv4 address" end
137 local value = toV4mapped(self.addr);
138 self.toV4mapped = value;
142 function ip_methods:label()
144 if self.proto == "IPv4" then
145 value = label(self.toV4mapped);
153 function ip_methods:precedence()
155 if self.proto == "IPv4" then
156 value = precedence(self.toV4mapped);
158 value = precedence(self);
160 self.precedence = value;
164 function ip_methods:scope()
166 if self.proto == "IPv4" then
167 value = v4scope(self.addr);
169 value = v6scope(self.addr);
175 return {new_ip = new_ip,
176 commonPrefixLength = commonPrefixLength};