]> git.ipfire.org Git - people/stevee/network.git/blobdiff - src/functions/functions.ports
Convert HOOK_SETTINGS into an array
[people/stevee/network.git] / src / functions / functions.ports
index cfe9b50e6d8e203ca19e7a40349a891bd768b59e..fb227150fc287d51f4ccea9c28708d1edbcbabf7 100644 (file)
 #                                                                             #
 ###############################################################################
 
-function port_dir() {
-       echo "${NETWORK_CONFIG_DIR}/ports"
-}
-
-function port_list() {
+ports_get_all() {
        local port
-       for port in $(port_dir)/*; do
-               port="$(basename "${port}")"
+       for port in $(list_directory "${NETWORK_PORTS_DIR}"); do
                if port_exists "${port}"; then
                        print "${port}"
                fi
        done
 }
 
-function port_list_in_use() {
+# XXX TO BE REMOVED
+port_list() {
+       ports_get_all "$@"
+}
+
+port_list_in_use() {
        local ports_in_use
 
        # Collect all ports that are attached to a zone
@@ -51,7 +51,7 @@ function port_list_in_use() {
        list_sort ${ports_in_use}
 }
 
-function port_list_free() {
+port_list_free() {
        local ports_in_use="$(port_list_in_use)"
 
        local port
@@ -64,34 +64,34 @@ function port_list_free() {
        return ${EXIT_OK}
 }
 
-function port_get_hook() {
+port_get_hook() {
        local port=${1}
        assert isset port
 
        config_get_hook $(port_file ${port})
 }
 
-function port_config_dir() {
+port_config_dir() {
        local port=${1}
 
        print "${RUN_DIR}/ports/${port}"
        return ${EXIT_OK}
 }
 
-function port_settings_read() {
+port_settings_read() {
        local port="${1}"
        assert isset port
 
        # Save the HOOK variable.
        local hook="${HOOK}"
 
-       settings_read "$(port_file "${port}")" ${HOOK_SETTINGS}
+       settings_read "$(port_file "${port}")" ${HOOK_SETTINGS[*]}
 
        # Restore hook.
        HOOK="${hook}"
 }
 
-function port_settings_write() {
+port_settings_write() {
        local port="${1}"
        assert isset port
        shift
@@ -100,29 +100,25 @@ function port_settings_write() {
        if function_exists "hook_check_settings"; then
                list_append args "--check=\"hook_check_settings\""
        fi
-       list_append args ${HOOK_SETTINGS}
+       list_append args HOOK ${HOOK_SETTINGS[*]}
 
        settings_write "$(port_file "${port}")" ${args}
 }
 
-function ports_get_all() {
-       port_list
-}
-
-function port_file() {
+port_file() {
        local port="${1}"
        assert isset port
 
-       echo "$(port_dir)/${port}"
+       echo "${NETWORK_PORTS_DIR}/${port}/settings"
 }
 
-function port_exists() {
+port_exists() {
        local port=${1}
 
-       [ -f "${NETWORK_CONFIG_DIR}/ports/${port}" ]
+       [ -d "${NETWORK_CONFIG_DIR}/ports/${port}" ]
 }
 
-function port_get_hook() {
+port_get_hook() {
        local port=${1}
 
        assert isset port
@@ -130,7 +126,7 @@ function port_get_hook() {
        config_get_hook $(port_file ${port})
 }
 
-function port_is_attached() {
+port_is_attached() {
        local port=${1}
        shift
 
@@ -138,11 +134,7 @@ function port_is_attached() {
 
        local zone
        for zone in $(zones_get_all); do
-
-               assert isset zone
-               assert zone_exists ${zone}
-
-               if listmatch ${port} $(zone_get_ports ${zone}); then
+               if list_match ${port} $(zone_get_ports ${zone}); then
                        echo "${zone}"
                        return ${EXIT_OK}
                fi
@@ -151,46 +143,38 @@ function port_is_attached() {
        return ${EXIT_ERROR}
 }
 
-function port_new() {
-       #local port=${1}
-       #shift
-       #
-       #if port_exists ${port}; then
-       #       error "Port '${port}' does already exist."
-       #       return ${EXIT_ERROR}
-       #fi
+port_is_up() {
+       device_is_up "$@"
+}
 
-       local hook=${1}
+port_new() {
+       local hook="${1}"
        shift
 
-       if ! hook_exists port ${hook}; then
+       if ! hook_exists port "${hook}"; then
                error "Port hook '${hook}' does not exist."
                return ${EXIT_ERROR}
        fi
 
-       #port_edit ${port} ${hook} $@
-       #
-       #if [ $? -ne ${EXIT_OK} ]; then
-       #       port_destroy ${port}
-       #fi
-
-       hook_exec port ${hook} new $@
+       hook_exec port "${hook}" new "$@"
 }
 
-function port_destroy() {
+port_destroy() {
        local port=${1}
-
        assert isset port
 
-       port_exists ${port} || return ${EXIT_OK}
-
-       # Check if the port is attached to any zone and don't delete it.
-       local ok=${EXIT_OK}
+       # Cannot delete a port that does not exist
+       if ! port_exists ${port}; then
+               error "No such port: ${port}"
+               return ${EXIT_ERROR}
+       fi
 
        local attached_zone=$(port_is_attached ${port})
        if [ -n "${attached_zone}" ]; then
-               error_log "Cannot destroy port '${port}' which is attached to zone '${attached_zone}'."
-               ok=${EXIT_ERROR}
+               if ! zone_port_detach "${attached_zone}" "${port}"; then
+                       error "Could not remove port ${port} from zone ${zone}"
+                       return ${EXIT_ERROR}
+               fi
        fi
 
        # Check if the port is linked to any other port and don't allow the user
@@ -199,35 +183,39 @@ function port_destroy() {
        for other_port in $(ports_get); do
                [ "${other_port}" = "${port}" ] && continue
 
-               if listmatch ${port} $(port_get_parents ${other_port}); then
-                       error_log "Cannot destroy port '${port}' which is a parent port to '${other_port}'."
-                       ok=${EXIT_ERROR}
-               fi
-
-               if listmatch ${port} $(port_get_children ${other_port}); then
-                       error_log "Cannot destroy port '${port}' which is child of port '${other_port}'."
-                       ok=${EXIT_ERROR}
+               if list_match ${port} $(port_get_children ${other_port}); then
+                       log ERROR "Cannot destroy port '${port}' which is child of port '${other_port}'."
+                       return ${EXIT_ERROR}
                fi
        done
 
-       # If ok says we are not okay --> exit
-       if [ ${ok} -ne ${EXIT_OK} ]; then
+       # Shut down the port before destroying it
+       if ! port_remove "${port}"; then
                return ${EXIT_ERROR}
        fi
 
-       port_remove "${port}"
+       if ! rm -rf "${NETWORK_PORTS_DIR}/${port}"; then
+               log ERROR "Could not destroy port ${port}"
+               return ${EXIT_ERROR}
+       fi
 
-       rm -f $(port_file ${port})
+       log INFO "Destroyed port ${port}"
+       return ${EXIT_OK}
 }
 
-function port_create() {
-       port_cmd "create" $@
+port_create() {
+       port_cmd "create" "$@"
 }
 
-function port_remove() {
+port_remove() {
        local port="${1}"
        assert isset port
 
+       if ! port_exists "${port}"; then
+               log ERROR "Port ${port} does not exist"
+               return ${EXIT_ERROR}
+       fi
+
        # If the device is still up, we need to bring it down first.
        if device_is_up "${port}"; then
                port_down "${port}"
@@ -236,27 +224,43 @@ function port_remove() {
        port_cmd "remove" "${port}"
 }
 
-function port_edit() {
-       port_cmd edit $@
+# Restarts the port by removing it and then re-creating it
+port_restart() {
+       local port="${1}"
+       assert isset port
+
+       port_remove "${port}"
+
+       port_create "${port}"
 }
 
-function port_up() {
-       port_cmd up $@
+port_edit() {
+       port_cmd edit "$@"
 }
 
-function port_down() {
-       port_cmd down $@
+port_up() {
+       assert [ $# -eq 1 ]
+
+       local port="${1}"
+
+       # Check if the port exists
+       if ! device_exists "${port}"; then
+               log ERROR "Could not bring up port ${port} which has not been created"
+               return ${EXIT_ERROR}
+       fi
+
+       port_cmd up "${port}"
 }
 
-function port_status() {
-       port_cmd status $@
+port_down() {
+       port_cmd down "$@"
 }
 
-function port_info() {
-       port_cmd info $@
+port_status() {
+       port_cmd status "$@"
 }
 
-function port_cmd() {
+port_cmd() {
        local cmd=${1}
        local port=${2}
        shift 2
@@ -266,22 +270,25 @@ function port_cmd() {
 
        local hook=$(port_get_hook ${port})
 
-       assert isset hook
+       # Abort if we could not find a hook
+       if ! isset hook; then
+               log CRITICAL "Port ${port} does not have a hook associated with it"
+               return ${EXIT_ERROR}
+       fi
 
-       hook_exec port ${hook} ${cmd} ${port} $@
+       hook_exec port ${hook} ${cmd} ${port} "$@"
 }
 
-function ports_get() {
+ports_get() {
        local port
-       for port in $(port_dir)/*; do
-               port=$(basename ${port})
+       for port in $(list_directory "${NETWORK_PORTS_DIR}"); do
                if port_exists ${port}; then
                        echo "${port}"
                fi
        done
 }
 
-function port_find_free() {
+port_find_free() {
        local pattern=${1}
 
        assert isset pattern
@@ -301,33 +308,15 @@ function port_find_free() {
        return ${EXIT_ERROR}
 }
 
-function port_get_info() {
-       local port=${1}
-       local key=${2}
-
-       assert isset port
-       assert port_exists ${port}
-       assert isset key
-
-       (
-               eval $(port_info ${port})
-               echo "${!key}"
-       )
-}
-
-function port_get_parents() {
+port_get_children() {
        local port=${1}
 
-       port_get_info ${port} PORT_PARENTS
-}
-
-function port_get_children() {
-       local port=${1}
+       assert port_exists "${port}"
 
-       port_get_info ${port} PORT_CHILDREN
+       port_cmd "children" "${port}"
 }
 
-function port_zone() {
+port_zone() {
        # Get name of the zones, this port is configured in.
        local port=${1}
        shift
@@ -345,7 +334,7 @@ function port_zone() {
        return ${EXIT_OK}
 }
 
-function port_hotplug_event() {
+port_hotplug_event() {
        local port="${1}"
        assert isset port
 
@@ -354,7 +343,7 @@ function port_hotplug_event() {
        port_cmd "hotplug" "${port}"
 }
 
-function port_get_slaves() {
+port_get_slaves() {
        local port="${1}"
 
        port_settings_read "${port}" \
@@ -362,7 +351,7 @@ function port_get_slaves() {
        print "${SLAVES}"
 }
 
-function port_device_is_slave() {
+port_device_is_slave() {
        assert [ $# -eq 2 ]
 
        local port="${1}"
@@ -375,7 +364,7 @@ function port_device_is_slave() {
        list_match "${device}" ${slaves}
 }
 
-function port_get_phy() {
+port_get_phy() {
        local port="${1}"
 
        port_settings_read "${port}" \
@@ -383,7 +372,7 @@ function port_get_phy() {
        print "${PHY}"
 }
 
-function port_uses_phy() {
+port_uses_phy() {
        assert [ $# -eq 2 ]
 
        local port="${1}"
@@ -399,3 +388,46 @@ function port_uses_phy() {
        local port_phy="$(port_get_phy "${port}")"
        [ "${port_phy}" = "${phy}" ]
 }
+
+ports_lowest_address() {
+       local address
+       local addresses
+
+       local port
+       for port in $(port_list); do
+               # Skip all ports that do not exist
+               # any more or are not plugged in
+               device_exists "${port}" || continue
+
+               # Skip all ports that are not proper ethernet devices
+               device_is_wireless "${port}" && continue
+               device_is_ethernet "${port}" || continue
+
+               list_append addresses "$(device_get_address "${port}")"
+       done
+
+       # Sort the list
+       addresses="$(list_sort ${addresses})"
+
+       # Get the first element which is the lowest MAC address
+       list_head ${addresses}
+}
+
+port_identify() {
+       device_identify "$@"
+}
+
+port_get_color() {
+       # This function return the color of a port
+       assert [ $# -eq 1 ]
+
+       local name=${1}
+       color_read "port" ${name}
+}
+
+port_get_description_title() {
+       assert [ $# -eq 1 ]
+
+       local name=${1}
+       description_title_read $(description_format_filename "port" "${name}")
+}