diff options
author | Matthew Wild <mwild1@gmail.com> | 2013-05-18 16:45:29 +0100 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2013-05-18 16:45:29 +0100 |
commit | 38abedd4387beda9e443ad25f1b7d478a1a92520 (patch) | |
tree | 40044404584f77f1699c9a924824a65a83fd4023 /util/ip.lua | |
parent | ae3f10826d62c09f5ee5a7ac5be2a0746ae23c08 (diff) | |
download | prosody-38abedd4387beda9e443ad25f1b7d478a1a92520.tar.gz prosody-38abedd4387beda9e443ad25f1b7d478a1a92520.zip |
util.ip: Add CIDR notation parsing and matching
Diffstat (limited to 'util/ip.lua')
-rw-r--r-- | util/ip.lua | 25 |
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}; |