modules/luci-base: Fix ipaddrport validator to support ipv6
[project/luci.git] / modules / luci-base / luasrc / cbi / datatypes.lua
index 4c003be..95d19d8 100644 (file)
@@ -189,9 +189,23 @@ function hostport(val)
        return not not (h and p and host(h) and port(p))
 end
 
-function ipaddrport(val)
+function ip4addrport(val)
        local h, p = val:match("^([^:]+):([^:]+)$")
-       return not not (h and p and ipaddr(h) and port(p))
+       return (h and p and ip4addr(h) and port(p))
+end
+
+function ipaddrport(val, bracket)
+       local h, p = val:match("^([^%[%]:]+):([^:]+)$")
+       if (h and p and ip4addr(h) and port(p)) then
+               return true
+       elseif (bracket == 1) then
+               h, p = val:match("^(%[.+%]):([^:]+)$")
+               if  (h and p and ip6addr(h) and port(p)) then
+                       return true
+               end
+       end
+       h, p = val:match("^([^%[%]]+):([^:]+)$")
+       return (h and p and ip6addr(h) and port(p))
 end
 
 function wpakey(val)