aboutsummaryrefslogtreecommitdiffstats
path: root/util
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2013-05-17 14:52:52 +0100
committerMatthew Wild <mwild1@gmail.com>2013-05-17 14:52:52 +0100
commit2ab42c8dbf95ea250e4dd16b21b12c2b6dddb5b1 (patch)
tree43ac55332ead25577076ce17f119b2b4ced32076 /util
parent8178f7bd884410b53329d6fc0b9c49891081a91d (diff)
downloadprosody-2ab42c8dbf95ea250e4dd16b21b12c2b6dddb5b1.tar.gz
prosody-2ab42c8dbf95ea250e4dd16b21b12c2b6dddb5b1.zip
util.ip: Automatically determine protocol of IP address if none specified. Return error if invalid. [Backported from 0.10]
Diffstat (limited to 'util')
-rw-r--r--util/ip.lua10
1 files changed, 9 insertions, 1 deletions
diff --git a/util/ip.lua b/util/ip.lua
index 856bf034..226432cc 100644
--- a/util/ip.lua
+++ b/util/ip.lua
@@ -12,7 +12,15 @@ local ip_mt = { __index = function (ip, key) return (ip_methods[key])(ip); end,
local hex2bits = { ["0"] = "0000", ["1"] = "0001", ["2"] = "0010", ["3"] = "0011", ["4"] = "0100", ["5"] = "0101", ["6"] = "0110", ["7"] = "0111", ["8"] = "1000", ["9"] = "1001", ["A"] = "1010", ["B"] = "1011", ["C"] = "1100", ["D"] = "1101", ["E"] = "1110", ["F"] = "1111" };
local function new_ip(ipStr, proto)
- if proto ~= "IPv4" and proto ~= "IPv6" then
+ if not proto then
+ local sep = ipStr:match("^%x+(.)");
+ if sep == ":" then proto = "IPv6"
+ elseif sep == "." then proto = "IPv4"
+ end
+ if not proto then
+ return nil, "invalid address";
+ end
+ elseif proto ~= "IPv4" and proto ~= "IPv6" then
return nil, "invalid protocol";
end
if proto == "IPv6" and ipStr:find('.', 1, true) then