Changeset

5587:e108c3f97f26

util.ip: Automatically determine protocol of IP address if none specified. Return error if invalid.
author Matthew Wild <mwild1@gmail.com>
date Fri, 17 May 2013 14:52:52 +0100
parents 5586:7e1264bf7af8
children 5588:8c1a3243d16f
files util/ip.lua
diffstat 1 files changed, 9 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/util/ip.lua	Fri May 17 14:56:06 2013 +0200
+++ b/util/ip.lua	Fri May 17 14:52:52 2013 +0100
@@ -12,7 +12,15 @@
 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