Comparison

util/ip.lua @ 6926:d96b2aa7a11d

util.ip: Improve comparison, == doesn't necessarily handle IPv6 addresses correctly if they aren't normalized (case, ::, etc.)
author Matthew Wild <mwild1@gmail.com>
date Sun, 22 Nov 2015 15:30:27 +0000
parent 5776:bd0ff8ae98a8
child 7061:eda0feeaf759
comparison
equal deleted inserted replaced
6924:c37ad3e1fdd9 6926:d96b2aa7a11d
227 return new_ip(cidr), bits; 227 return new_ip(cidr), bits;
228 end 228 end
229 229
230 local function match(ipA, ipB, bits) 230 local function match(ipA, ipB, bits)
231 local common_bits = commonPrefixLength(ipA, ipB); 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 232 if bits and ipB.proto == "IPv4" then
236 common_bits = common_bits - 96; -- v6 mapped addresses always share these bits 233 common_bits = common_bits - 96; -- v6 mapped addresses always share these bits
237 end 234 end
238 return common_bits >= bits; 235 return common_bits >= (bits or 128);
239 end 236 end
240 237
241 return {new_ip = new_ip, 238 return {new_ip = new_ip,
242 commonPrefixLength = commonPrefixLength, 239 commonPrefixLength = commonPrefixLength,
243 parse_cidr = parse_cidr, 240 parse_cidr = parse_cidr,