applications/olsr: Validate input for lqmult, #654
[project/luci.git] / applications / luci-olsr / luasrc / model / cbi / olsr / olsrdiface.lua
index a981eb6..9673d9a 100644 (file)
@@ -13,6 +13,16 @@ $Id$
 
 ]]--
 
+local util = require "luci.util"
+local ip = require "luci.ip"
+
+function write_float(self, section, value)
+    local n = tonumber(value)
+    if n ~= nil then
+        return Value.write(self, section, "%.1f" % n)
+    end
+end
+
 m = Map("olsrd", translate("OLSR Daemon - Interface"),
         translate("The OLSR daemon is an implementation of the Optimized Link State Routing protocol. "..
        "As such it allows mesh routing for any network equipment. "..
@@ -70,7 +80,7 @@ weight.datatype = "uinteger"
 weight.placeholder = "0"
 
 lqmult = i:taboption("general", DynamicList, "LinkQualityMult", translate("LinkQuality Multiplicator"),
-       translate("Multiply routes with the factor given here. Allowed values are between 0.01 and 1. "..
+       translate("Multiply routes with the factor given here. Allowed values are between 0.01 and 1.0. "..
        "It is only used when LQ-Level is greater than 0. Examples:<br />"..
        "reduce LQ to 192.168.0.1 by half: 192.168.0.1 0.5<br />"..
        "reduce LQ to all nodes on this interface by 20%: default 0.8"))
@@ -79,6 +89,28 @@ lqmult.rmempty = true
 lqmult.cast = "table"
 lqmult.placeholder = "default 1.0"
 
+function lqmult.validate(self, value)
+       for _, v in pairs(value) do
+               if v ~= "" then
+                       local val = util.split(v, " ")
+                       local host = val[1]
+                       local mult = val[2]
+                       if not host or not mult then
+                               return nil, translate("LQMult requires two values (IP address or 'default' and multiplicator) seperated by space.")
+                       end
+                       if not (host == "default" or ip.IPv4(host) or ip.IPv6(host)) then
+                               return nil, translate("Can only be a valid IPv4 or IPv6 address or 'default'")
+                       end
+                       if not tonumber(mult) or tonumber(mult) > 1 or tonumber(mult) < 0.01 then
+                               return nil, translate("Invalid Value for LQMult-Value. Must be between 0.01 and 1.0.")
+                       end
+                       if not mult:match("[0-1]%.[0-9]+") then
+                               return nil, translate("Invalid Value for LQMult-Value. You must use a decimal number between 0.01 and 1.0 here.")
+                       end
+               end
+       end
+       return value
+end
 
 ip4b = i:taboption("addrs", Value, "Ip4Broadcast", translate("IPv4 broadcast"),
        translate("IPv4 broadcast address for outgoing OLSR packets. One useful example would be 255.255.255.255. "..
@@ -106,46 +138,52 @@ ip6s.optional = true
 ip6s.datatype = "ip6addr"
 ip6s.placeholder = "0::/0"
 
-
 hi = i:taboption("timing", Value, "HelloInterval", translate("Hello interval"))
 hi.optional = true
 hi.datatype = "ufloat"
 hi.placeholder = "5.0"
+hi.write = write_float
 
 hv = i:taboption("timing", Value, "HelloValidityTime", translate("Hello validity time"))
 hv.optional = true
 hv.datatype = "ufloat"
 hv.placeholder = "40.0"
+hv.write = write_float
 
 ti = i:taboption("timing", Value, "TcInterval", translate("TC interval"))
 ti.optional = true
 ti.datatype = "ufloat"
 ti.placeholder = "2.0"
+ti.write = write_float
 
 tv = i:taboption("timing", Value, "TcValidityTime", translate("TC validity time"))
 tv.optional = true
 tv.datatype = "ufloat"
 tv.placeholder = "256.0"
+tv.write = write_float
 
 mi = i:taboption("timing", Value, "MidInterval", translate("MID interval"))
 mi.optional = true
 mi.datatype = "ufloat"
 mi.placeholder = "18.0"
+mi.write = write_float
 
 mv = i:taboption("timing", Value, "MidValidityTime", translate("MID validity time"))
 mv.optional = true
 mv.datatype = "ufloat"
 mv.placeholder = "324.0"
+mv.write = write_float
 
 ai = i:taboption("timing", Value, "HnaInterval", translate("HNA interval"))
 ai.optional = true
 ai.datatype = "ufloat"
 ai.placeholder = "18.0"
+ai.write = write_float
 
 av = i:taboption("timing", Value, "HnaValidityTime", translate("HNA validity time"))
 av.optional = true
 av.datatype = "ufloat"
 av.placeholder = "108.0"
-
+av.write = write_float
 
 return m