aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2013-05-18 16:45:29 +0100
committerMatthew Wild <mwild1@gmail.com>2013-05-18 16:45:29 +0100
commit38abedd4387beda9e443ad25f1b7d478a1a92520 (patch)
tree40044404584f77f1699c9a924824a65a83fd4023
parentae3f10826d62c09f5ee5a7ac5be2a0746ae23c08 (diff)
downloadprosody-38abedd4387beda9e443ad25f1b7d478a1a92520.tar.gz
prosody-38abedd4387beda9e443ad25f1b7d478a1a92520.zip
util.ip: Add CIDR notation parsing and matching
-rw-r--r--util/ip.lua25
1 files changed, 24 insertions, 1 deletions
diff --git a/util/ip.lua b/util/ip.lua
index 20ea3dd7..8cf0076e 100644
--- a/util/ip.lua
+++ b/util/ip.lua
@@ -215,5 +215,28 @@ function ip_methods:private()
return private;
end
+local function parse_cidr(cidr)
+ local bits;
+ local ip_len = cidr:find("/", 1, true);
+ if ip_len then
+ bits = tonumber(cidr:sub(ip_len+1, -1));
+ cidr = cidr:sub(1, ip_len-1);
+ end
+ return new_ip(cidr), bits;
+end
+
+local function match(ipA, ipB, bits)
+ local common_bits = commonPrefixLength(ipA, ipB);
+ if not bits then
+ return ipA == ipB;
+ end
+ if bits and ipB.proto == "IPv4" then
+ common_bits = common_bits - 96; -- v6 mapped addresses always share these bits
+ end
+ return common_bits >= bits;
+end
+
return {new_ip = new_ip,
- commonPrefixLength = commonPrefixLength};
+ commonPrefixLength = commonPrefixLength,
+ parse_cidr = parse_cidr,
+ match=match};