util.ip: rename variable (i is already defined) [luacheck]
[prosody.git] / util / ip.lua
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 --
7
8 local ip_methods = {};
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" };
13
14 local function new_ip(ipStr, proto)
15         if not proto then
16                 local sep = ipStr:match("^%x+(.)");
17                 if sep == ":" or (not(sep) and ipStr:sub(1,1) == ":") then
18                         proto = "IPv6"
19                 elseif sep == "." then
20                         proto = "IPv4"
21                 end
22                 if not proto then
23                         return nil, "invalid address";
24                 end
25         elseif proto ~= "IPv4" and proto ~= "IPv6" then
26                 return nil, "invalid protocol";
27         end
28         local zone;
29         if proto == "IPv6" and ipStr:find('%', 1, true) then
30                 ipStr, zone = ipStr:match("^(.-)%%(.*)");
31         end
32         if proto == "IPv6" and ipStr:find('.', 1, true) then
33                 local changed;
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);
36                 end);
37                 if changed ~= 1 then return nil, "invalid-address"; end
38         end
39
40         return setmetatable({ addr = ipStr, proto = proto, zone = zone }, ip_mt);
41 end
42
43 local function toBits(ip)
44         local result = "";
45         local fields = {};
46         if ip.proto == "IPv4" then
47                 ip = ip.toV4mapped;
48         end
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 _ = 1, 16 * (9 - #fields) do
55                                 result = result .. "0";
56                         end
57                 else
58                         for _ = 1, 4 - field:len() do
59                                 result = result .. "0000";
60                         end
61                         for j = 1, field:len() do
62                                 result = result .. hex2bits[field:sub(j, j)];
63                         end
64                 end
65         end
66         return result;
67 end
68
69 local function commonPrefixLength(ipA, ipB)
70         ipA, ipB = toBits(ipA), toBits(ipB);
71         for i = 1, 128 do
72                 if ipA:sub(i,i) ~= ipB:sub(i,i) then
73                         return i-1;
74                 end
75         end
76         return 128;
77 end
78
79 local function v4scope(ip)
80         local fields = {};
81         ip:gsub("([^.]*).?", function (c) fields[#fields + 1] = tonumber(c) end);
82         -- Loopback:
83         if fields[1] == 127 then
84                 return 0x2;
85         -- Link-local unicast:
86         elseif fields[1] == 169 and fields[2] == 254 then
87                 return 0x2;
88         -- Global unicast:
89         else
90                 return 0xE;
91         end
92 end
93
94 local function v6scope(ip)
95         -- Loopback:
96         if ip:match("^[0:]*1$") then
97                 return 0x2;
98         -- Link-local unicast:
99         elseif ip:match("^[Ff][Ee][89ABab]") then
100                 return 0x2;
101         -- Site-local unicast:
102         elseif ip:match("^[Ff][Ee][CcDdEeFf]") then
103                 return 0x5;
104         -- Multicast:
105         elseif ip:match("^[Ff][Ff]") then
106                 return tonumber("0x"..ip:sub(4,4));
107         -- Global unicast:
108         else
109                 return 0xE;
110         end
111 end
112
113 local function label(ip)
114         if commonPrefixLength(ip, new_ip("::1", "IPv6")) == 128 then
115                 return 0;
116         elseif commonPrefixLength(ip, new_ip("2002::", "IPv6")) >= 16 then
117                 return 2;
118         elseif commonPrefixLength(ip, new_ip("2001::", "IPv6")) >= 32 then
119                 return 5;
120         elseif commonPrefixLength(ip, new_ip("fc00::", "IPv6")) >= 7 then
121                 return 13;
122         elseif commonPrefixLength(ip, new_ip("fec0::", "IPv6")) >= 10 then
123                 return 11;
124         elseif commonPrefixLength(ip, new_ip("3ffe::", "IPv6")) >= 16 then
125                 return 12;
126         elseif commonPrefixLength(ip, new_ip("::", "IPv6")) >= 96 then
127                 return 3;
128         elseif commonPrefixLength(ip, new_ip("::ffff:0:0", "IPv6")) >= 96 then
129                 return 4;
130         else
131                 return 1;
132         end
133 end
134
135 local function precedence(ip)
136         if commonPrefixLength(ip, new_ip("::1", "IPv6")) == 128 then
137                 return 50;
138         elseif commonPrefixLength(ip, new_ip("2002::", "IPv6")) >= 16 then
139                 return 30;
140         elseif commonPrefixLength(ip, new_ip("2001::", "IPv6")) >= 32 then
141                 return 5;
142         elseif commonPrefixLength(ip, new_ip("fc00::", "IPv6")) >= 7 then
143                 return 3;
144         elseif commonPrefixLength(ip, new_ip("fec0::", "IPv6")) >= 10 then
145                 return 1;
146         elseif commonPrefixLength(ip, new_ip("3ffe::", "IPv6")) >= 16 then
147                 return 1;
148         elseif commonPrefixLength(ip, new_ip("::", "IPv6")) >= 96 then
149                 return 1;
150         elseif commonPrefixLength(ip, new_ip("::ffff:0:0", "IPv6")) >= 96 then
151                 return 35;
152         else
153                 return 40;
154         end
155 end
156
157 local function toV4mapped(ip)
158         local fields = {};
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]);
163         ret = ret .. ":"
164         ret = ret .. ("%02x"):format(fields[3]);
165         ret = ret .. ("%02x"):format(fields[4]);
166         return new_ip(ret, "IPv6");
167 end
168
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;
173         return value;
174 end
175
176 function ip_methods:label()
177         local value;
178         if self.proto == "IPv4" then
179                 value = label(self.toV4mapped);
180         else
181                 value = label(self);
182         end
183         self.label = value;
184         return value;
185 end
186
187 function ip_methods:precedence()
188         local value;
189         if self.proto == "IPv4" then
190                 value = precedence(self.toV4mapped);
191         else
192                 value = precedence(self);
193         end
194         self.precedence = value;
195         return value;
196 end
197
198 function ip_methods:scope()
199         local value;
200         if self.proto == "IPv4" then
201                 value = v4scope(self.addr);
202         else
203                 value = v6scope(self.addr);
204         end
205         self.scope = value;
206         return value;
207 end
208
209 function ip_methods:private()
210         local private = self.scope ~= 0xE;
211         if not private and self.proto == "IPv4" then
212                 local ip = self.addr;
213                 local fields = {};
214                 ip:gsub("([^.]*).?", function (c) fields[#fields + 1] = tonumber(c) end);
215                 if fields[1] == 127 or fields[1] == 10 or (fields[1] == 192 and fields[2] == 168)
216                 or (fields[1] == 172 and (fields[2] >= 16 or fields[2] <= 32)) then
217                         private = true;
218                 end
219         end
220         self.private = private;
221         return private;
222 end
223
224 local function parse_cidr(cidr)
225         local bits;
226         local ip_len = cidr:find("/", 1, true);
227         if ip_len then
228                 bits = tonumber(cidr:sub(ip_len+1, -1));
229                 cidr = cidr:sub(1, ip_len-1);
230         end
231         return new_ip(cidr), bits;
232 end
233
234 local function match(ipA, ipB, bits)
235         local common_bits = commonPrefixLength(ipA, ipB);
236         if bits and ipB.proto == "IPv4" then
237                 common_bits = common_bits - 96; -- v6 mapped addresses always share these bits
238         end
239         return common_bits >= (bits or 128);
240 end
241
242 return {new_ip = new_ip,
243         commonPrefixLength = commonPrefixLength,
244         parse_cidr = parse_cidr,
245         match=match};