aboutsummaryrefslogtreecommitdiffstats
path: root/util/ip.lua
diff options
context:
space:
mode:
Diffstat (limited to 'util/ip.lua')
-rw-r--r--util/ip.lua21
1 files changed, 19 insertions, 2 deletions
diff --git a/util/ip.lua b/util/ip.lua
index 4b450934..268b7d10 100644
--- a/util/ip.lua
+++ b/util/ip.lua
@@ -5,8 +5,8 @@
-- COPYING file in the source package for more information.
--
-local net = require "util.net";
-local hex = require "util.hex";
+local net = require "prosody.util.net";
+local hex = require "prosody.util.hex";
local ip_methods = {};
@@ -241,9 +241,26 @@ function match(ipA, ipB, bits)
return ipA.bits:sub(1, bits) == ipB.bits:sub(1, bits);
end
+local function is_ip(obj)
+ return getmetatable(obj) == ip_mt;
+end
+
+local function truncate(ip, n_bits)
+ if n_bits % 8 ~= 0 then
+ return error("ip.truncate() only supports multiples of 8 bits");
+ end
+ local n_octets = n_bits / 8;
+ if not is_ip(ip) then
+ ip = new_ip(ip);
+ end
+ return new_ip(net.ntop(ip.packed:sub(1, n_octets)..("\0"):rep(#ip.packed-n_octets)))
+end
+
return {
new_ip = new_ip,
commonPrefixLength = commonPrefixLength,
parse_cidr = parse_cidr,
match = match,
+ is_ip = is_ip;
+ truncate = truncate;
};