validation: Add option ipv4only option to host and hostport datatypes
[project/luci.git] / modules / luci-base / luasrc / cbi / datatypes.lua
index 4c003be..626ad91 100644 (file)
@@ -176,22 +176,41 @@ function hostname(val)
        return false
 end
 
-function host(val)
-       return hostname(val) or ipaddr(val)
+function host(val, ipv4only)
+       return hostname(val) or ((ipv4only == 1) and ip4addr(val)) or ((not (ipv4only == 1)) and ipaddr(val))
 end
 
 function network(val)
        return uciname(val) or host(val)
 end
 
-function hostport(val)
+function hostport(val, ipv4only)
        local h, p = val:match("^([^:]+):([^:]+)$")
-       return not not (h and p and host(h) and port(p))
+       return not not (h and p and host(h, ipv4only) and port(p))
 end
 
-function ipaddrport(val)
+function ip4addrport(val, bracket)
        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 ip4addrport(val)
+       local h, p = val:match("^([^:]+):([^:]+)$")
+       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)