Comparison

util/ip.lua @ 5720:449399a7e136

Merge
author Matthew Wild <mwild1@gmail.com>
date Sat, 29 Jun 2013 14:45:47 +0100
parent 5608:0200945313c9
child 5776:bd0ff8ae98a8
comparison
equal deleted inserted replaced
5719:84025249fc04 5720:449399a7e136
10 __tostring = function (ip) return ip.addr; end, 10 __tostring = function (ip) return ip.addr; end,
11 __eq = function (ipA, ipB) return ipA.addr == ipB.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" }; 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 13
14 local function new_ip(ipStr, proto) 14 local function new_ip(ipStr, proto)
15 if proto ~= "IPv4" and proto ~= "IPv6" then 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
16 return nil, "invalid protocol"; 26 return nil, "invalid protocol";
17 end 27 end
18 if proto == "IPv6" and ipStr:find('.', 1, true) then 28 if proto == "IPv6" and ipStr:find('.', 1, true) then
19 local changed; 29 local changed;
20 ipStr, changed = ipStr:gsub(":(%d+)%.(%d+)%.(%d+)%.(%d+)$", function(a,b,c,d) 30 ipStr, changed = ipStr:gsub(":(%d+)%.(%d+)%.(%d+)%.(%d+)$", function(a,b,c,d)
190 end 200 end
191 self.scope = value; 201 self.scope = value;
192 return value; 202 return value;
193 end 203 end
194 204
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;
209 local fields = {};
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
213 private = true;
214 end
215 end
216 self.private = private;
217 return private;
218 end
219
220 local function parse_cidr(cidr)
221 local bits;
222 local ip_len = cidr:find("/", 1, true);
223 if ip_len then
224 bits = tonumber(cidr:sub(ip_len+1, -1));
225 cidr = cidr:sub(1, ip_len-1);
226 end
227 return new_ip(cidr), bits;
228 end
229
230 local function match(ipA, ipB, bits)
231 local common_bits = commonPrefixLength(ipA, ipB);
232 if not bits then
233 return ipA == ipB;
234 end
235 if bits and ipB.proto == "IPv4" then
236 common_bits = common_bits - 96; -- v6 mapped addresses always share these bits
237 end
238 return common_bits >= bits;
239 end
240
195 return {new_ip = new_ip, 241 return {new_ip = new_ip,
196 commonPrefixLength = commonPrefixLength}; 242 commonPrefixLength = commonPrefixLength,
243 parse_cidr = parse_cidr,
244 match=match};