util.encodings: Spell out all IDNA 2008 options ICU has
[prosody.git] / util / ip.lua
blobd180822570fccc1d5a305a5ad512edbbf227c0ba
1 -- Prosody IM
2 -- Copyright (C) 2008-2011 Florian Zeitz
3 --
4 -- This project is MIT/X11 licensed. Please see the
5 -- COPYING file in the source package for more information.
6 --
8 local net = require "util.net";
9 local hex = require "util.hex";
11 local ip_methods = {};
13 local ip_mt = {
14 __index = function (ip, key)
15 local method = ip_methods[key];
16 if not method then return nil; end
17 local ret = method(ip);
18 ip[key] = ret;
19 return ret;
20 end,
21 __tostring = function (ip) return ip.addr; end,
22 __eq = function (ipA, ipB) return ipA.packed == ipB.packed; end
25 local hex2bits = {
26 ["0"] = "0000", ["1"] = "0001", ["2"] = "0010", ["3"] = "0011",
27 ["4"] = "0100", ["5"] = "0101", ["6"] = "0110", ["7"] = "0111",
28 ["8"] = "1000", ["9"] = "1001", ["A"] = "1010", ["B"] = "1011",
29 ["C"] = "1100", ["D"] = "1101", ["E"] = "1110", ["F"] = "1111",
32 local function new_ip(ipStr, proto)
33 local zone;
34 if (not proto or proto == "IPv6") and ipStr:find('%', 1, true) then
35 ipStr, zone = ipStr:match("^(.-)%%(.*)");
36 end
38 local packed, err = net.pton(ipStr);
39 if not packed then return packed, err end
40 if proto == "IPv6" and #packed ~= 16 then
41 return nil, "invalid-ipv6";
42 elseif proto == "IPv4" and #packed ~= 4 then
43 return nil, "invalid-ipv4";
44 elseif not proto then
45 if #packed == 16 then
46 proto = "IPv6";
47 elseif #packed == 4 then
48 proto = "IPv4";
49 else
50 return nil, "unknown protocol";
51 end
52 elseif proto ~= "IPv6" and proto ~= "IPv4" then
53 return nil, "invalid protocol";
54 end
56 return setmetatable({ addr = ipStr, packed = packed, proto = proto, zone = zone }, ip_mt);
57 end
59 function ip_methods:normal()
60 return net.ntop(self.packed);
61 end
63 function ip_methods.bits(ip)
64 return hex.to(ip.packed):upper():gsub(".", hex2bits);
65 end
67 function ip_methods.bits_full(ip)
68 if ip.proto == "IPv4" then
69 ip = ip.toV4mapped;
70 end
71 return ip.bits;
72 end
74 local match;
76 local function commonPrefixLength(ipA, ipB)
77 ipA, ipB = ipA.bits_full, ipB.bits_full;
78 for i = 1, 128 do
79 if ipA:sub(i,i) ~= ipB:sub(i,i) then
80 return i-1;
81 end
82 end
83 return 128;
84 end
86 -- Instantiate once
87 local loopback = new_ip("::1");
88 local loopback4 = new_ip("127.0.0.0");
89 local sixtofour = new_ip("2002::");
90 local teredo = new_ip("2001::");
91 local linklocal = new_ip("fe80::");
92 local linklocal4 = new_ip("169.254.0.0");
93 local uniquelocal = new_ip("fc00::");
94 local sitelocal = new_ip("fec0::");
95 local sixbone = new_ip("3ffe::");
96 local defaultunicast = new_ip("::");
97 local multicast = new_ip("ff00::");
98 local ipv6mapped = new_ip("::ffff:0:0");
100 local function v4scope(ip)
101 if match(ip, loopback4, 8) then
102 return 0x2;
103 elseif match(ip, linklocal4) then
104 return 0x2;
105 else -- Global unicast
106 return 0xE;
110 local function v6scope(ip)
111 if ip == loopback then
112 return 0x2;
113 elseif match(ip, linklocal, 10) then
114 return 0x2;
115 elseif match(ip, sitelocal, 10) then
116 return 0x5;
117 elseif match(ip, multicast, 10) then
118 return ip.packed:byte(2) % 0x10;
119 else -- Global unicast
120 return 0xE;
124 local function label(ip)
125 if ip == loopback then
126 return 0;
127 elseif match(ip, sixtofour, 16) then
128 return 2;
129 elseif match(ip, teredo, 32) then
130 return 5;
131 elseif match(ip, uniquelocal, 7) then
132 return 13;
133 elseif match(ip, sitelocal, 10) then
134 return 11;
135 elseif match(ip, sixbone, 16) then
136 return 12;
137 elseif match(ip, defaultunicast, 96) then
138 return 3;
139 elseif match(ip, ipv6mapped, 96) then
140 return 4;
141 else
142 return 1;
146 local function precedence(ip)
147 if ip == loopback then
148 return 50;
149 elseif match(ip, sixtofour, 16) then
150 return 30;
151 elseif match(ip, teredo, 32) then
152 return 5;
153 elseif match(ip, uniquelocal, 7) then
154 return 3;
155 elseif match(ip, sitelocal, 10) then
156 return 1;
157 elseif match(ip, sixbone, 16) then
158 return 1;
159 elseif match(ip, defaultunicast, 96) then
160 return 1;
161 elseif match(ip, ipv6mapped, 96) then
162 return 35;
163 else
164 return 40;
168 function ip_methods:toV4mapped()
169 if self.proto ~= "IPv4" then return nil, "No IPv4 address" end
170 local value = new_ip("::ffff:" .. self.normal);
171 return value;
174 function ip_methods:label()
175 if self.proto == "IPv4" then
176 return label(self.toV4mapped);
177 else
178 return label(self);
182 function ip_methods:precedence()
183 if self.proto == "IPv4" then
184 return precedence(self.toV4mapped);
185 else
186 return precedence(self);
190 function ip_methods:scope()
191 if self.proto == "IPv4" then
192 return v4scope(self);
193 else
194 return v6scope(self);
198 local rfc1918_8 = new_ip("10.0.0.0");
199 local rfc1918_12 = new_ip("172.16.0.0");
200 local rfc1918_16 = new_ip("192.168.0.0");
201 local rfc6598 = new_ip("100.64.0.0");
203 function ip_methods:private()
204 local private = self.scope ~= 0xE;
205 if not private and self.proto == "IPv4" then
206 return match(self, rfc1918_8, 8) or match(self, rfc1918_12, 12) or match(self, rfc1918_16, 16) or match(self, rfc6598, 10);
208 return private;
211 local function parse_cidr(cidr)
212 local bits;
213 local ip_len = cidr:find("/", 1, true);
214 if ip_len then
215 bits = tonumber(cidr:sub(ip_len+1, -1));
216 cidr = cidr:sub(1, ip_len-1);
218 return new_ip(cidr), bits;
221 function match(ipA, ipB, bits)
222 if not bits or bits >= 128 or ipB.proto == "IPv4" and bits >= 32 then
223 return ipA == ipB;
224 elseif bits < 1 then
225 return true;
227 if ipA.proto ~= ipB.proto then
228 if ipA.proto == "IPv4" then
229 ipA = ipA.toV4mapped;
230 elseif ipB.proto == "IPv4" then
231 ipB = ipB.toV4mapped;
232 bits = bits + (128 - 32);
235 return ipA.bits:sub(1, bits) == ipB.bits:sub(1, bits);
238 return {
239 new_ip = new_ip,
240 commonPrefixLength = commonPrefixLength,
241 parse_cidr = parse_cidr,
242 match = match,