-- Copyright 2010 Jo-Philipp Wich <jow@openwrt.org>
+-- Copyright 2017 Dan Luedtke <mail@danrl.com>
-- Licensed to the public under the Apache License 2.0.
local fs = require "nixio.fs"
return ( val and val >= 0 and val <= 128 )
end
+function cidr4(val)
+ local ip, mask = val:match("^([^/]+)/([^/]+)$")
+
+ return ip4addr(ip) and ip4prefix(mask)
+end
+
+function cidr6(val)
+ local ip, mask = val:match("^([^/]+)/([^/]+)$")
+
+ return ip6addr(ip) and ip6prefix(mask)
+end
+
+function ipnet4(val)
+ local ip, mask = val:match("^([^/]+)/([^/]+)$")
+
+ return ip4addr(ip) and ip4addr(mask)
+end
+
+function ipnet6(val)
+ local ip, mask = val:match("^([^/]+)/([^/]+)$")
+
+ return ip6addr(ip) and ip6addr(mask)
+end
+
+function ipmask(val)
+ return ipmask4(val) or ipmask6(val)
+end
+
+function ipmask4(val)
+ return cidr4(val) or ipnet4(val) or ip4addr(val)
+end
+
+function ipmask6(val)
+ return cidr6(val) or ipnet6(val) or ip6addr(val)
+end
+
+function ip6hostid(val)
+ if val == "eui64" or val == "random" then
+ return true
+ else
+ local addr = ip.IPv6(val)
+ if addr and addr:prefix() == 128 and addr:lower("::1:0:0:0:0") then
+ return true
+ end
+ end
+
+ return false
+end
+
function port(val)
val = tonumber(val)
return ( val and val >= 0 and val <= 65535 )
end
function macaddr(val)
- if val and val:match(
- "^[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+:" ..
- "[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+$"
- ) then
- local parts = util.split( val, ":" )
-
- for i = 1,6 do
- parts[i] = tonumber( parts[i], 16 )
- if parts[i] < 0 or parts[i] > 255 then
- return false
- end
- end
-
- return true
- end
-
- return false
+ return ip.checkmac(val) and true or false
end
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 ip4addrport(val, bracket)
+ local h, p = val:match("^([^:]+):([^:]+)$")
+ return (h and p and ip4addr(h) and port(p))
end
function ip4addrport(val)
if (h and p and ip4addr(h) and port(p)) then
return true
elseif (bracket == 1) then
- h, p = val:match("^(%[.+%]):([^:]+)$")
+ h, p = val:match("^%[(.+)%]:([^:]+)$")
if (h and p and ip6addr(h) and port(p)) then
return true
end
end
end
+function hexstring(val)
+ if val then
+ return (val:match("^[a-fA-F0-9]+$") ~= nil)
+ end
+ return false
+end
+
+function hex(val, maxbytes)
+ maxbytes = tonumber(maxbytes)
+ if val and maxbytes ~= nil then
+ return ((val:match("^0x[a-fA-F0-9]+$") ~= nil) and (#val <= 2 + maxbytes * 2))
+ end
+ return false
+end
+
+function base64(val)
+ if val then
+ return (val:match("^[a-zA-Z0-9/+]+=?=?$") ~= nil) and (math.fmod(#val, 4) == 0)
+ end
+ return false
+end
+
function string(val)
return true -- Everything qualifies as valid string
end
-function directory( val, seen )
+function directory(val, seen)
local s = fs.stat(val)
seen = seen or { }
return false
end
-function file( val, seen )
+function file(val, seen)
local s = fs.stat(val)
seen = seen or { }
return false
end
-function device( val, seen )
+function device(val, seen)
local s = fs.stat(val)
seen = seen or { }
return false;
end
- local days_in_month = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
-
- local function is_leap_year(year)
- return (year % 4 == 0) and ((year % 100 ~= 0) or (year % 400 == 0))
- end
-
- function get_days_in_month(month, year)
- if (month == 2) and is_leap_year(year) then
- return 29
- else
- return days_in_month[month]
- end
- end
- if (year < 2015) then
- return false
- end
- if ((month == 0) or (month > 12)) then
- return false
- end
- if ((day == 0) or (day > get_days_in_month(month, year))) then
- return false
- end
- return true
+ local days_in_month = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
+
+ local function is_leap_year(year)
+ return (year % 4 == 0) and ((year % 100 ~= 0) or (year % 400 == 0))
+ end
+
+ function get_days_in_month(month, year)
+ if (month == 2) and is_leap_year(year) then
+ return 29
+ else
+ return days_in_month[month]
+ end
+ end
+ if (year < 2015) then
+ return false
+ end
+ if ((month == 0) or (month > 12)) then
+ return false
+ end
+ if ((day == 0) or (day > get_days_in_month(month, year))) then
+ return false
+ end
+ return true
end
return false
end
-