]> git.ipfire.org Git - people/ms/network.git/blobdiff - src/hooks/ports/dummy
hotplug: Remove multiple copies of the same function
[people/ms/network.git] / src / hooks / ports / dummy
index 41b830b57eac14c32ba18a155faa70266bcacbe9..36888319439cb8cb504c866f099c8b8fad21f084 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,93 +42,54 @@ 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 port_settings_write "${port}" ${HOOK_SETTINGS}; then
+       if port_settings_write "${port}"; then
                log INFO "New dummy port '${port}' has been created"
        fi
 
        exit ${EXIT_OK}
 }
 
-function hook_edit() {
-       local port=${1}
-       assert isset port
-       shift
-
-       port_settings_read "${port}" ${HOOK_SETTINGS}
-
-       while [ $# -gt 0 ]; do
-               case "${1}" in
-                       --address=*)
-                               ADDRESS=$(cli_get_val ${1})
-                               ;;
-                       *)
-                               warning "Unknown argument '${1}'"
-                               ;;
-               esac
-               shift
-       done
-
-       port_settings_write "${port}" ${HOOK_SETTINGS}
-
-       exit ${EXIT_OK} 
-}
-
-function hook_up() {
-       local port=${1}
+hook_create() {
+       local port="${1}"
        assert isset port
 
-       port_settings_read "${port}" ${HOOK_SETTINGS}
+       # Read configuration
+       port_settings_read "${port}"
 
-       # Create device if not already exists.
-       if ! device_exists ${port}; then
-               dummy_create ${port} "${ADDRESS}"
-       fi
-
-       # Bring up the port.
-       device_set_up ${port}
+       # Create the dummy device
+       dummy_create "${port}" "${ADDRESS}"
 
        exit ${EXIT_OK}
 }
 
-function hook_down() {
-       local port=${1}
+hook_remove() {
+       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}
+       # Remove the dummy device
+       dummy_remove "${port}"
 }
 
-function hook_hotplug_rename() {
-       local port=${1}
-       assert isset port
-
-       local device=${2}
-       assert isset device
-
-       port_settings_read "${port}" ${HOOK_SETTINGS}
-
-       if [ "${ADDRESS}" = "$(device_get_address ${device})" ]; then
-               log DEBUG "Device '${device}' equals port '${port}'."
-               exit ${EXIT_OK}
-       fi
-
-       log DEBUG "Device '${device}' does not equal port '${port}'."
-       exit ${EXIT_ERROR}
+hook_hotplug_rename() {
+       hook_hotplug_rename_by_address "$@"
 }