]> git.ipfire.org Git - people/stevee/network.git/blobdiff - src/hooks/ports/dummy
Convert HOOK_SETTINGS into an array
[people/stevee/network.git] / src / hooks / ports / dummy
index 3af50cd070951c36cd032bcdca2cce5b45a9c571..1c4b3c9fdd21260472f7c68b043645b9ea094532 100644 (file)
 
 . /usr/lib/network/header-port
 
-HOOK_SETTINGS="HOOK ADDRESS"
+HOOK_SETTINGS=(
+       "ADDRESS"
+)
 
-function hook_check() {
+hook_check_settings() {
        assert ismac ADDRESS
 }
 
-function hook_create() {
+hook_parse_cmdline() {
        while [ $# -gt 0 ]; do
                case "${1}" in
                        --address=*)
-                               ADDRESS=$(cli_get_val ${1})
+                               ADDRESS=$(cli_get_val "${1}")
                                ;;
                        *)
                                warning "Unknown argument '${1}'"
@@ -40,55 +42,57 @@ function hook_create() {
                shift
        done
 
-       # Generate a random MAC address if non was set.
-       if ! isset ADDRESS; then
+       if isset ADDRESS; then
+               if ! ismac ADDRESS; then
+                       error "Invalid MAC address given: ${ADDRESS}"
+                       return ${EXIT_ERROR}
+               fi
+
+       # Generate a random but static MAC address if none was set
+       else
                ADDRESS=$(mac_generate)
        fi
+}
+
+hook_new() {
+       if ! hook_parse_cmdline "$@"; then
+               return ${EXIT_ERROR}
+       fi
 
        local port=$(port_find_free ${DUMMY_PORT_PATTERN})
        assert isset port
 
-       if config_write $(port_file ${port}) ${HOOK_SETTINGS}; then
+       if port_settings_write "${port}" ${HOOK_SETTINGS[*]}; then
                log INFO "New dummy port '${port}' has been created"
        fi
 
        exit ${EXIT_OK}
 }
 
-function hook_edit() {
-       local port=${1}
+hook_create() {
+       local port="${1}"
        assert isset port
-       shift
-
-       config_read $(port_file ${port})
 
-       while [ $# -gt 0 ]; do
-               case "${1}" in
-                       --address=*)
-                               ADDRESS=$(cli_get_val ${1})
-                               ;;
-                       *)
-                               warning "Unknown argument '${1}'"
-                               ;;
-               esac
-               shift
-       done
+       # Read configuration
+       port_settings_read "${port}" ${HOOK_SETTINGS[*]}
 
-       config_write $(port_file ${port}) ${HOOK_SETTINGS}
+       # Create the dummy device
+       dummy_create "${port}" "${ADDRESS}"
 
-       exit ${EXIT_OK} 
+       exit ${EXIT_OK}
 }
 
-function hook_up() {
-       local port=${1}
+hook_remove() {
+       local port="${1}"
        assert isset port
 
-       config_read $(port_file ${port})
+       # Remove the dummy device
+       dummy_remove "${port}"
+}
 
-       # Create device if not already exists.
-       if ! device_exists ${port}; then
-               dummy_create ${port} "${ADDRESS}"
-       fi
+hook_up() {
+       local port="${1}"
+       assert isset port
 
        # Bring up the port.
        device_set_up ${port}
@@ -96,31 +100,24 @@ function hook_up() {
        exit ${EXIT_OK}
 }
 
-function hook_down() {
-       local port=${1}
+hook_down() {
+       local port="${1}"
        assert isset port
 
-       if ! device_exists ${port}; then
-               exit ${EXIT_OK}
-       fi
-
        # Tear down the port.
        device_set_down ${port}
 
-       # Remove the dummy port.
-       dummy_remove ${port}
-
        exit ${EXIT_OK}
 }
 
-function hook_hotplug_rename() {
+hook_hotplug_rename() {
        local port=${1}
        assert isset port
 
        local device=${2}
        assert isset device
 
-       config_read $(port_file ${port})
+       port_settings_read "${port}" ${HOOK_SETTINGS[*]}
 
        if [ "${ADDRESS}" = "$(device_get_address ${device})" ]; then
                log DEBUG "Device '${device}' equals port '${port}'."