ipv4-static: Update hook
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 28 Sep 2014 21:18:52 +0000 (23:18 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 28 Sep 2014 21:18:52 +0000 (23:18 +0200)
Some minor cleanup without functional changes.

src/functions/functions.cli
src/functions/functions.hook
src/functions/functions.ipv4
src/header-zone
src/hooks/configs/ipv4-static

index ca60d14..a3ee722 100644 (file)
@@ -26,11 +26,11 @@ function cli_help_requested() {
 
        if [ -n "${argument}" ]; then
                if listmatch ${argument} help -h --help; then
-                       return ${EXIT_OK}
+                       return ${EXIT_TRUE}
                fi
        fi
 
-       return ${EXIT_ERROR}
+       return ${EXIT_FALSE}
 }
 
 function cli_run_help() {
index 0434f9f..5b82835 100644 (file)
@@ -19,7 +19,8 @@
 #                                                                             #
 ###############################################################################
 
-HOOK_COMMANDS_CONFIG="hook_create hook_down hook_status hook_remove hook_up"
+HOOK_COMMANDS_CONFIG="hook_create hook_down hook_edit hook_status hook_remove"
+HOOK_COMMANDS_CONFIG="${HOOK_COMMANDS_CONFIG} hook_up"
 
 HOOK_COMMANDS_PORT="hook_create hook_down hook_edit hook_hotplug \
        hook_hotplug_rename hook_info hook_status hook_up"
@@ -141,6 +142,19 @@ function hook_list() {
        done
 }
 
+# The default help function.
+function hook_help() {
+       # If no man page has been configured, we print an error message.
+       if [ -z "${HOOK_MANPAGE}" ]; then
+               error "There is no help available for hook '${HOOK}'. Exiting."
+               exit ${EXIT_ERROR}
+       fi
+
+       cli_show_man "${HOOK_MANPAGE}"
+
+       exit $?
+}
+
 function config_get_hook() {
        local config=${1}
 
index d7a6b85..067b87c 100644 (file)
@@ -45,6 +45,15 @@ function ipv4_prefix_is_valid() {
        return ${EXIT_TRUE}
 }
 
+function ipv4_netmask_is_valid() {
+       local netmask="${1}"
+
+       # XXX this check could be much better by checking
+       # if the netmask only contains leading ones
+
+       ipv4_is_valid "${netmask}"
+}
+
 function ipv4_detect_duplicate() {
        local device=${1}
        local address=${2}
@@ -161,6 +170,30 @@ function ipv4_prefix2netmask() {
        esac
 }
 
+function ipv4_netmask2prefix() {
+       local netmask="${1}"
+       assert isset netmask
+
+       local mask=0
+
+       local field
+       for field in ${netmask//\./ }; do
+               mask=$(( $(( ${mask} << 8 )) | ${field} ))
+       done
+
+       local cidr=0
+       local x="$(( 128 << 24 ))" # 0x80000000
+
+       while [ $(( ${x} & ${mask} )) -ne 0 ]; do
+               [ ${mask} -eq ${x} ] && mask=0 || mask=$(( ${mask} << 1 ))
+               cidr=$(( ${cidr} + 1 ))
+       done
+
+       assert [ $(( ${mask} & 2147483647 )) -eq 0 ]
+
+       print "${cidr}"
+}
+
 function ipv4_get_network() {
        local network=$(ipv4_get_network $@)
 
index 2120377..808a54a 100644 (file)
@@ -69,17 +69,6 @@ function hook_discover() {
        exit ${DISCOVER_NOT_SUPPORTED}
 }
 
-# The default help function.
-function hook_help() {
-       # If no man page has been configured, we print an error message.
-       if [ -z "${HOOK_MANPAGE}" ]; then
-               error "There is no help available for hook '${HOOK}'. Exiting."
-               exit ${EXIT_ERROR}
-       fi
-
-       cli_show_man ${HOOK_MANPAGE}
-}
-
 # Do nothing
 function hook_parse_cmdline() {
        return ${EXIT_OK}
index ceec9fe..50eda81 100644 (file)
@@ -21,6 +21,8 @@
 
 . /usr/lib/network/header-config
 
+HOOK_MANPAGE="network-config-ipv4-static"
+
 HOOK_SETTINGS="HOOK ADDRESS PREFIX GATEWAY"
 
 function hook_check() {
@@ -35,29 +37,73 @@ function hook_check() {
 
 function hook_create() {
        local zone="${1}"
-       assert isset zone
+       assert zone_exists "${zone}"
        shift
 
-       while [ $# -gt 0 ]; do
-               case "${1}" in
-                       --address=*)
-                               ADDRESS=${1#--address=}
+       local arg
+       while read -r arg; do
+               local key="$(cli_get_key "${arg}")"
+               local val="$(cli_get_val "${arg}")"
+
+               case "${key}" in
+                       address)
+                               if ! ipv4_is_valid "${val}"; then
+                                       error "Invalid IPv4 address: ${val}"
+                                       exit ${EXIT_CONF_ERROR}
+                               fi
+
+                               ADDRESS="${val}"
                                ;;
-                       --netmask=*)
-                               NETMASK=${1#--netmask=}
+
+                       prefix)
+                               if ! ipv4_prefix_is_valid "${val}"; then
+                                       error "Invalid IPv4 prefix: ${val}"
+                                       exit ${EXIT_CONF_ERROR}
+                               fi
+
+                               PREFIX="${val}"
                                ;;
-                       --prefix=*)
-                               PREFIX=${1#--prefix=}
+
+                       gateway)
+                               if ! ipv4_is_valid "${val}"; then
+                                       error "Invalid IPv4 address for gateway: ${val}"
+                                       exit ${EXIT_CONF_ERROR}
+                               fi
+
+                               GATEWAY="${val}"
                                ;;
-                       --gateway=*)
-                               GATEWAY=${1#--gateway=}
+
+                       # Compatibility switches
+                       netmask)
+                               if ! ipv4_netmask_is_valid "${val}"; then
+                                       error "Invalid netmask: ${val}"
+                                       exit ${EXIT_CONF_ERROR}
+                               fi
+
+                               # The netmask will be converted into a prefix
+                               PREFIX="$(ipv4_netmask2prefix ${val})"
+                               ;;
+
+                       # Unknown switches
+                       *)
+                               error "Unhandled argument: ${arg}"
+                               exit ${EXIT_CONF_ERROR}
                                ;;
                esac
-               shift
-       done
+       done <<< "$(args $@)"
 
-       if [ -z "${PREFIX}" -a -n "${NETMASK}" ]; then
-               PREFIX=$(ipv4_mask_to_cidr ${NETMASK})
+       if ! isset ADDRESS; then
+               error "You need to provide an IPv4 address"
+               exit ${EXIT_CONF_ERROR}
+       fi
+
+       if ! isset PREFIX; then
+               error "You need to provide an IPv4 prefix"
+               exit ${EXIT_CONF_ERROR}
+       fi
+
+       if ! isset GATEWAY && zone_is_nonlocal "${zone}"; then
+               warning "You did not configure a gateway for a non-local zone"
        fi
 
        # XXX maybe we can add some hashing to identify a configuration again
@@ -141,25 +187,3 @@ function hook_status() {
        exit ${EXIT_OK}
 }
 
-function ipv4_mask_to_cidr() {
-       local mask=0
-
-       local field
-       for field in $(tr '.' ' ' <<<${1}); do
-               mask=$(( $(( ${mask} << 8 )) | ${field} ))
-       done
-
-       local cidr=0
-       local x=$(( 128 << 24 )) # 0x80000000
-
-       while [ $(( ${x} & ${mask} )) -ne 0 ]; do
-               [ ${mask} -eq ${x} ] && mask=0 || mask=$(( ${mask} << 1 ))
-               cidr=$((${cidr} + 1))
-       done
-
-       if [ $(( ${mask} & 2147483647 )) -ne 0 ]; then # 2147483647 = 0x7fffffff
-               echo "Invalid net mask: $1" >&2
-       else
-               echo ${cidr}
-       fi
-}