# HG changeset patch # User Kim Alvefur # Date 1512095365 -3600 # Node ID 021129f7b0a393a83f12b4061e6e2978c2e213c2 # Parent ab9ddfb03d4d036afd4b257bf68fc0007eb727c2 util.ip: Do CIDR matching by comparing all bits at once instead of using O(n) function diff -r ab9ddfb03d4d -r 021129f7b0a3 util/ip.lua --- a/util/ip.lua Fri Dec 01 02:25:25 2017 +0100 +++ b/util/ip.lua Fri Dec 01 03:29:25 2017 +0100 @@ -228,11 +228,20 @@ end function match(ipA, ipB, bits) - local common_bits = commonPrefixLength(ipA, ipB); - if bits and ipB.proto == "IPv4" then - common_bits = common_bits - 96; -- v6 mapped addresses always share these bits + if not bits then + return ipA == ipB; + elseif bits < 1 then + return true; end - return common_bits >= (bits or 128); + if ipA.proto ~= ipB.proto then + if ipA.proto == "IPv4" then + ipA = ipA.toV4mapped; + elseif ipB.proto == "IPv4" then + ipB = ipA.toV4mapped; + bits = bits + (128 - 32); + end + end + return ipA.bits:sub(1, bits) == ipB.bits:sub(1, bits); end return {