luci-base: datatypes: add cidr, ipnet validator type
authorYousong Zhou <yszhou4tech@gmail.com>
Mon, 21 Aug 2017 13:33:02 +0000 (21:33 +0800)
committerYousong Zhou <yszhou4tech@gmail.com>
Tue, 22 Aug 2017 12:19:49 +0000 (20:19 +0800)
- Rewrite ipmask to use these subtypes
- Add ip{4,6}prefix validators to cbi.js

Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
modules/luci-base/htdocs/luci-static/resources/cbi.js
modules/luci-base/luasrc/cbi/datatypes.lua

index b819230cf3af0d96accfb710ead029d7e9ced59f..884eb62f68168c7cdfdaffb7bdd46b3dddb2a30c 100644 (file)
@@ -118,48 +118,88 @@ var cbi_validators = {
                return false;
        },
 
-       'ipmask': function()
+       'ip4prefix': function()
        {
-               return cbi_validators.ipmask4.apply(this) ||
-                       cbi_validators.ipmask6.apply(this);
+               return !isNaN(this) && this >= 0 && this <= 32;
        },
 
-       'ipmask4': function()
+       'ip6prefix': function()
        {
-               var ip = this, mask = 32;
+               return !isNaN(this) && this >= 0 && this <= 128;
+       },
 
-               if (ip.match(/^(\S+)\/(\S+)$/))
+       'cidr': function()
+       {
+               return cbi_validators.cidr4.apply(this) ||
+                       cbi_validators.cidr6.apply(this);
+       },
+
+       'cidr4': function()
+       {
+               if (this.match(/^(\S+)\/(\S+)$/))
                {
                        ip = RegExp.$1;
                        mask = RegExp.$2;
+                       return cbi_validators.ip4addr.apply(ip) &&
+                               cbi_validators.ip4prefix.apply(mask);
                }
+               return false;
+       },
 
-               if (!isNaN(mask) && (mask < 0 || mask > 32))
-                       return false;
-
-               if (isNaN(mask) && !cbi_validators.ip4addr.apply(mask))
-                       return false;
-
-               return cbi_validators.ip4addr.apply(ip);
+       'cidr6': function()
+       {
+               if (this.match(/^(\S+)\/(\S+)$/))
+               {
+                       ip = RegExp.$1;
+                       mask = RegExp.$2;
+                       return cbi_validators.ip6addr.apply(ip) &&
+                               cbi_validators.ip6prefix.apply(mask);
+               }
+               return false;
        },
 
-       'ipmask6': function()
+       'ipnet4': function()
        {
-               var ip = this, mask = 128;
+               if (this.match(/^(\S+)\/(\S+)$/))
+               {
+                       ip = RegExp.$1;
+                       net = RegExp.$2;
+                       return cbi_validators.ip4addr.apply(ip) &&
+                               cbi_validators.ip4addr.apply(net);
+               }
+               return false;
+       },
 
-               if (ip.match(/^(\S+)\/(\S+)$/))
+       'ipnet6': function()
+       {
+               if (this.match(/^(\S+)\/(\S+)$/))
                {
                        ip = RegExp.$1;
-                       mask = RegExp.$2;
+                       net = RegExp.$2;
+                       return cbi_validators.ip6addr.apply(ip) &&
+                               cbi_validators.ip6addr.apply(net);
                }
+               return false;
+       },
 
-               if (!isNaN(mask) && (mask < 0 || mask > 128))
-                       return false;
+       'ipmask': function()
+       {
+               return cbi_validators.ipmask4.apply(this) ||
+                       cbi_validators.ipmask6.apply(this);
+       },
 
-               if (isNaN(mask) && !cbi_validators.ip6addr.apply(mask))
-                       return false;
+       'ipmask4': function()
+       {
+               return cbi_validators.cidr4.apply(this) ||
+                       cbi_validators.ipnet4.apply(this) ||
+                       cbi_validators.ip4addr.apply(this);
+       },
 
-               return cbi_validators.ip6addr.apply(ip);
+       'ipmask6': function()
+       {
+               return cbi_validators.cidr6.apply(this) ||
+                       cbi_validators.ipnet6.apply(this) ||
+                       cbi_validators.ip6addr.apply(this);
        },
 
        'port': function()
index cf56566287760f0dfb0ecf218b692d807768dc9a..df23aaf13591b730bca7c309aaee0cdc49f2e4fd 100644 (file)
@@ -132,38 +132,40 @@ function ip6prefix(val)
        return ( val and val >= 0 and val <= 128 )
 end
 
-function ipmask(val)
-       return ipmask4(val) or ipmask6(val)
+function cidr4(val)
+       local ip, mask = val:match("^([^/]+)/([^/]+)$")
+
+       return ip4addr(ip) and ip4prefix(mask)
 end
 
-function ipmask4(val)
+function cidr6(val)
        local ip, mask = val:match("^([^/]+)/([^/]+)$")
-       local bits = tonumber(mask)
 
-       if bits and (bits < 0 or bits > 32) then
-               return false
-       end
+       return ip6addr(ip) and ip6prefix(mask)
+end
 
-       if not bits and mask and not ip4addr(mask) then
-               return false
-       end
+function ipnet4(val)
+       local ip, mask = val:match("^([^/]+)/([^/]+)$")
 
-       return ip4addr(ip or val)
+       return ip4addr(ip) and ip4addr(mask)
 end
 
-function ipmask6(val)
+function ipnet6(val)
        local ip, mask = val:match("^([^/]+)/([^/]+)$")
-       local bits = tonumber(mask)
 
-       if bits and (bits < 0 or bits > 128) then
-               return false
-       end
+       return ip6addr(ip) and ip6addr(mask)
+end
 
-       if not bits and mask and not ip6addr(mask) then
-               return false
-       end
+function ipmask(val)
+       return ipmask4(val) or ipmask6(val)
+end
+
+function ipmask4(val)
+       return cidr4(val) or ipnet4(val) or ip4addr(val)
+end
 
-       return ip6addr(ip or val)
+function ipmask6(val)
+       return cidr6(val) or ipnet6(val) or ip6addr(val)
 end
 
 function ip6hostid(val)