Software /
code /
prosody
Changeset
5603:e07f4f02e4f9
util.ip: Add CIDR notation parsing and matching
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 18 May 2013 16:45:29 +0100 |
parents | 5602:e8a0e545ee05 |
children | 5604:6df0ec991f2e |
files | util/ip.lua |
diffstat | 1 files changed, 24 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/util/ip.lua Sat May 18 15:29:31 2013 +0100 +++ b/util/ip.lua Sat May 18 16:45:29 2013 +0100 @@ -215,5 +215,28 @@ 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};