]> git.ipfire.org Git - people/ms/network.git/commitdiff
DHCP: Allow configuring lease times in human-readable format
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 14 Sep 2016 09:05:43 +0000 (11:05 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 14 Sep 2016 09:05:43 +0000 (11:05 +0200)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/functions/functions.dhcpd
src/functions/functions.util

index 0c8f7056372deee08fbf347335221c2fc854cce3..e782691485dd87530544450c57561f65b58a2600 100644 (file)
@@ -188,27 +188,30 @@ _dhcpd_edit_ipv4() {
                                fi
                                ;;
                        --default-lease-time=*)
-                               DEFAULT_LEASE_TIME=$(cli_get_val ${1})
+                               local val=$(cli_get_val ${1})
+                               DEFAULT_LEASE_TIME=$(parse_time ${val})
 
                                if ! isinteger DEFAULT_LEASE_TIME; then
-                                       error "Invalid value for --default-lease-time."
+                                       error "Invalid value for --default-lease-time: ${val}"
                                        return ${EXIT_ERROR}
                                fi
                                ;;
                        --max-lease-time=*)
-                               MAX_LEASE_TIME=$(cli_get_val ${1})
+                               local val=$(cli_get_val ${1})
+                               MAX_LEASE_TIME=$(parse_time ${val})
 
                                if ! isinteger MAX_LEASE_TIME; then
-                                       error "Invalid value for --max-lease-time."
+                                       error "Invalid value for --max-lease-time: ${val}"
                                        return ${EXIT_ERROR}
                                fi
                                ;;
                        --min-lease-time=*)
-                               MIN_LEASE_TIME=$(cli_get_val ${1})
+                               local val=$(cli_get_val ${1})
+                               MIN_LEASE_TIME=$(parse_time ${val})
 
                                if isset MIN_LEASE_TIME; then
                                        if ! isinteger MIN_LEASE_TIME; then
-                                               error "Invalid value for --min-lease-time."
+                                               error "Invalid value for --min-lease-time: ${val}"
                                                return ${EXIT_ERROR}
                                        fi
                                fi
@@ -231,18 +234,20 @@ _dhcpd_edit_ipv6() {
        while [ $# -gt 0 ]; do
                case "${1}" in
                        --preferred-lifetime=*)
-                               PREFERRED_LIFETIME=$(cli_get_val ${1})
+                               local val=$(cli_get_val ${1})
+                               PREFERRED_LIFETIME=$(parse_time ${val})
 
                                if ! isinteger PREFERRED_LIFETIME; then
-                                       error "Invalid value for --preferred-lifetime."
+                                       error "Invalid value for --preferred-lifetime: ${val}"
                                        return ${EXIT_ERROR}
                                fi
                                ;;
                        --valid-lifetime=*)
-                               VALID_LIFETIME=$(cli_get_val ${1})
+                               local val=$(cli_get_val ${1})
+                               VALID_LIFETIME=$(parse_time ${val})
 
                                if ! isinteger VALID_LIFETIME; then
-                                       error "Invalid value for --valid-lifetime."
+                                       error "Invalid value for --valid-lifetime: ${val}"
                                        return ${EXIT_ERROR}
                                fi
                                ;;
index 9c3601b1b18c843ae83d9865b8764a37ac944985..8ae38ee63d38f7d8ab1f5b26e5dfc51f503926a0 100644 (file)
@@ -133,6 +133,44 @@ format_time() {
        echo ${ret}
 }
 
+parse_time() {
+       local ret=0
+
+       local arg
+       for arg in $@; do
+               local unit
+
+               case "${arg}" in
+                       *h|*m|*s)
+                               # Store unit
+                               unit="${arg: -1}"
+
+                               # Remove unit
+                               arg="${arg:0:-1}"
+                               ;;
+               esac
+
+               if ! isinteger arg; then
+                       return ${EXIT_ERROR}
+               fi
+
+               # Convert hours and minutes into seconds
+               case "${unit}" in
+                       h)
+                               arg=$(( ${arg} * 3600 ))
+                               ;;
+                       m)
+                               arg=$(( ${arg} * 60 ))
+                               ;;
+               esac
+
+               # Add up everything
+               ret=$(( ${ret} + ${arg} ))
+       done
+
+       print "${ret}"
+}
+
 assign() {
        local key=${1}
        assert isset key