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";
28 if proto == "IPv6" and ipStr:find('.', 1, true) then
30 ipStr, changed = ipStr:gsub(":(%d+)%.(%d+)%.(%d+)%.(%d+)$", function(a,b,c,d)
31 return (":%04X:%04X"):format(a*256+b,c*256+d);
33 if changed ~= 1 then return nil, "invalid-address"; end
36 return setmetatable({ addr = ipStr, proto = proto }, ip_mt);
39 local function toBits(ip)
42 if ip.proto == "IPv4" then
45 ip = (ip.addr):upper();
46 ip:gsub("([^:]*):?", function (c) fields[#fields + 1] = c end);
47 if not ip:match(":$") then fields[#fields] = nil; end
48 for i, field in ipairs(fields) do
49 if field:len() == 0 and i ~= 1 and i ~= #fields then
50 for i = 1, 16 * (9 - #fields) do
51 result = result .. "0";
54 for i = 1, 4 - field:len() do
55 result = result .. "0000";
57 for i = 1, field:len() do
58 result = result .. hex2bits[field:sub(i,i)];
65 local function commonPrefixLength(ipA, ipB)
66 ipA, ipB = toBits(ipA), toBits(ipB);
68 if ipA:sub(i,i) ~= ipB:sub(i,i) then
75 local function v4scope(ip)
77 ip:gsub("([^.]*).?", function (c) fields[#fields + 1] = tonumber(c) end);
79 if fields[1] == 127 then
81 -- Link-local unicast:
82 elseif fields[1] == 169 and fields[2] == 254 then
90 local function v6scope(ip)
92 if ip:match("^[0:]*1$") then
94 -- Link-local unicast:
95 elseif ip:match("^[Ff][Ee][89ABab]") then
97 -- Site-local unicast:
98 elseif ip:match("^[Ff][Ee][CcDdEeFf]") then
101 elseif ip:match("^[Ff][Ff]") then
102 return tonumber("0x"..ip:sub(4,4));
109 local function label(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("2001::", "IPv6")) >= 32 then
116 elseif commonPrefixLength(ip, new_ip("fc00::", "IPv6")) >= 7 then
118 elseif commonPrefixLength(ip, new_ip("fec0::", "IPv6")) >= 10 then
120 elseif commonPrefixLength(ip, new_ip("3ffe::", "IPv6")) >= 16 then
122 elseif commonPrefixLength(ip, new_ip("::", "IPv6")) >= 96 then
124 elseif commonPrefixLength(ip, new_ip("::ffff:0:0", "IPv6")) >= 96 then
131 local function precedence(ip)
132 if commonPrefixLength(ip, new_ip("::1", "IPv6")) == 128 then
134 elseif commonPrefixLength(ip, new_ip("2002::", "IPv6")) >= 16 then
136 elseif commonPrefixLength(ip, new_ip("2001::", "IPv6")) >= 32 then
138 elseif commonPrefixLength(ip, new_ip("fc00::", "IPv6")) >= 7 then
140 elseif commonPrefixLength(ip, new_ip("fec0::", "IPv6")) >= 10 then
142 elseif commonPrefixLength(ip, new_ip("3ffe::", "IPv6")) >= 16 then
144 elseif commonPrefixLength(ip, new_ip("::", "IPv6")) >= 96 then
146 elseif commonPrefixLength(ip, new_ip("::ffff:0:0", "IPv6")) >= 96 then
153 local function toV4mapped(ip)
155 local ret = "::ffff:";
156 ip:gsub("([^.]*).?", function (c) fields[#fields + 1] = tonumber(c) end);
157 ret = ret .. ("%02x"):format(fields[1]);
158 ret = ret .. ("%02x"):format(fields[2]);
160 ret = ret .. ("%02x"):format(fields[3]);
161 ret = ret .. ("%02x"):format(fields[4]);
162 return new_ip(ret, "IPv6");
165 function ip_methods:toV4mapped()
166 if self.proto ~= "IPv4" then return nil, "No IPv4 address" end
167 local value = toV4mapped(self.addr);
168 self.toV4mapped = value;
172 function ip_methods:label()
174 if self.proto == "IPv4" then
175 value = label(self.toV4mapped);
183 function ip_methods:precedence()
185 if self.proto == "IPv4" then
186 value = precedence(self.toV4mapped);
188 value = precedence(self);
190 self.precedence = value;
194 function ip_methods:scope()
196 if self.proto == "IPv4" then
197 value = v4scope(self.addr);
199 value = v6scope(self.addr);
205 function ip_methods:private()
206 local private = self.scope ~= 0xE;
207 if not private and self.proto == "IPv4" then
208 local ip = self.addr;
210 ip:gsub("([^.]*).?", function (c) fields[#fields + 1] = tonumber(c) end);
211 if fields[1] == 127 or fields[1] == 10 or (fields[1] == 192 and fields[2] == 168)
212 or (fields[1] == 172 and (fields[2] >= 16 or fields[2] <= 32)) then
216 self.private = private;
220 local function parse_cidr(cidr)
222 local ip_len = cidr:find("/", 1, true);
224 bits = tonumber(cidr:sub(ip_len+1, -1));
225 cidr = cidr:sub(1, ip_len-1);
227 return new_ip(cidr), bits;
230 local function match(ipA, ipB, bits)
231 local common_bits = commonPrefixLength(ipA, ipB);
235 if bits and ipB.proto == "IPv4" then
236 common_bits = common_bits - 96; -- v6 mapped addresses always share these bits
238 return common_bits >= bits;
241 return {new_ip = new_ip,
242 commonPrefixLength = commonPrefixLength,
243 parse_cidr = parse_cidr,