]> git.ipfire.org Git - people/stevee/network.git/blobdiff - network
ipv{6,4}: Simplify some functions and introduce new ones.
[people/stevee/network.git] / network
diff --git a/network b/network
index 3ecc439b633e75b95fd38335915ef0017dc9a8d2..1c642721b6989977235bbc882021c055330129b3 100755 (executable)
--- a/network
+++ b/network
@@ -50,6 +50,11 @@ function cli_config() {
 }
 
 function cli_device() {
+       if cli_help_requested $@; then
+               cli_show_man network-device
+               exit ${EXIT_OK}
+       fi
+
        local device=${1}
        local action=${2}
        shift 2
@@ -68,6 +73,9 @@ function cli_device() {
                status)
                        cli_device_status ${device}
                        ;;
+               unlock)
+                       cli_device_serial_unlock ${device} $@
+                       ;;
                *)
                        cli_show_man network-device
                        ;;
@@ -80,12 +88,22 @@ function cli_device_status() {
        local device=${1}
        assert device_exists ${device}
 
+       # Disable debugging output here.
+       local log_disable_stdout=${LOG_DISABLE_STDOUT}
+       LOG_DISABLE_STDOUT="true"
+
        # Save the type of the device for later.
        local type=$(device_get_type ${device})
 
        cli_headline 1 "Device status: ${device}"
        cli_print_fmt1 1 "Name"         "${device}"
 
+       # Handle serial devices.
+       if [ "${type}" = "serial" ]; then
+               cli_device_status_serial ${device}
+               return $?
+       fi
+
        # Print the device status.
        device_is_up ${device} &>/dev/null
        local status=$?
@@ -105,12 +123,14 @@ function cli_device_status() {
        cli_space
 
        # Print the link speed for ethernet devices.
-       case "${type}" in
-               ethernet)
-                       cli_print_fmt1 1 "Link" \
-                               "$(device_get_speed ${device}) MBit/s $(device_get_duplex ${device}) duplex"
-                       ;;
-       esac
+       if device_is_up ${device} &>/dev/null; then
+               case "${type}" in
+                       ethernet)
+                               cli_print_fmt1 1 "Link" \
+                                       "$(device_get_speed ${device}) MBit/s $(device_get_duplex ${device}) duplex"
+                               ;;
+               esac
+       fi
 
        cli_print_fmt1 1 "MTU"          "$(device_get_mtu ${device})"
        cli_space
@@ -138,6 +158,69 @@ function cli_device_status() {
                cli_space
        fi
 
+       # Reset the logging level.
+       LOG_DISABLE_STDOUT=${log_disable_stdout}
+}
+
+function cli_device_status_serial() {
+       local device=${1}
+       assert device_is_serial ${device}
+
+       serial_is_locked ${device} &>/dev/null
+       local locked=$?
+
+       cli_print_fmt1 1 "Locked" "$(cli_print_bool ${locked})"
+       cli_space
+
+       # Cannot go on when the device is locked.
+       [ ${locked} -eq ${EXIT_TRUE} ] && return ${EXIT_OK}
+
+       cli_print_fmt1 1 "Manufacturer" \
+               "$(modem_get_manufacturer ${device})"
+       cli_print_fmt1 1 "Model" \
+               "$(modem_get_model ${device})"
+       cli_print_fmt1 1 "Software version" \
+               "$(modem_get_software_version ${device})"
+
+       if modem_is_mobile ${device}; then
+               cli_print_fmt1 1 "IMEI" \
+                       "$(modem_get_device_imei ${device})"
+               cli_space
+
+               cli_headline 2 "Network status"
+               modem_sim_status ${device} &>/dev/null
+               local sim_status_code=$?
+
+               local sim_status="unknown"
+               case "${sim_status_code}" in
+                       ${EXIT_SIM_READY})
+                               sim_status="SIM ready"
+                               ;;
+                       ${EXIT_SIM_PIN})
+                               sim_status="PIN locked"
+                               ;;
+                       ${EXIT_SIM_PUK})
+                               sim_status="PUK locked"
+                               ;;
+               esac
+               cli_print_fmt1 2 "SIM status" "${sim_status}"
+
+               if [ ${sim_status_code} -eq ${EXIT_SIM_READY} ]; then
+                       cli_print_fmt1 2 "IMSI" \
+                               "$(modem_get_sim_imsi ${device})"
+                       cli_print_fmt1 2 "Operator" \
+                               "$(modem_get_network_operator ${device})"
+                       cli_print_fmt1 2 "Mode" \
+                               "$(modem_get_network_mode ${device})"
+                       cli_print_fmt1 2 "Signal quality" \
+                               "$(modem_get_signal_quality ${device}) dBm"
+
+                       local ber=$(modem_get_bit_error_rate ${device})
+                       isset ber || ber="unknown"
+                       cli_print_fmt1 2 "Bit Error Rate" "${ber}"
+               fi
+       fi
+       cli_space
 }
 
 function cli_device_discover() {
@@ -211,6 +294,92 @@ function cli_device_discover() {
        [ "${up}" = "1" ] || device_set_down ${device}
 }
 
+function cli_device_serial_unlock() {
+       if cli_help_requested $@; then
+               cli_show_man network-device
+               exit ${EXIT_OK}
+       fi
+
+       local device=${1}
+       assert isset device
+
+       if ! device_is_serial ${device}; then
+               error "${device} is not a serial device."
+               error "Unlocking is only supported for serial devices."
+               exit ${EXIT_ERROR}
+       fi
+
+       # Read the current state of the SIM card.
+       modem_sim_status ${device} &>/dev/null
+       local sim_status_code=$?
+
+       # If the SIM card is already unlocked, we don't need to do anything.
+       if [ ${sim_status_code} -eq ${EXIT_SIM_READY} ]; then
+               print "The SIM card is already unlocked."
+               exit ${EXIT_OK}
+
+       # If the SIM card is in an unknown state, we cannot do anything.
+       elif [ ${sim_status_code} -eq ${EXIT_SIM_UNKNOWN} ]; then
+               error "The SIM card is in an unknown state."
+               exit ${EXIT_ERROR}
+       fi
+
+       # Ask for the code.
+       local code=${2}
+       local require_new_pin="false"
+       local new_pin
+
+       while ! isinteger code; do
+               local message
+               case "${sim_status_code}" in
+                       ${EXIT_SIM_PIN})
+                               message="Please enter PIN:"
+                               ;;
+                       ${EXIT_SIM_PUK})
+                               message="Please enter PUK:"
+                               require_new_pin="true"
+                               ;;
+               esac
+               assert isset message
+
+               echo -n "${message} "
+               read -s code
+               echo # Print newline.
+
+               if enabled require_new_pin; then
+                       local i new_pin2
+                       for i in 0 1; do
+                               case "${i}" in
+                                       0)
+                                               message="Please enter a new PIN code:"
+                                               ;;
+                                       1)
+                                               message="Please confirm the new PIN code:"
+                                               ;;
+                               esac
+
+                               echo -n "${message} "
+                               read -s new_pin2
+                               echo # Print newline.
+
+                               if [ -n "${new_pin}" ]; then
+                                       if [ "${new_pin}" != "${new_pin2}" ]; then
+                                               error "The entered PIN codes did not match."
+                                               exit ${EXIT_ERROR}
+                                       fi
+                               else
+                                       new_pin=${new_pin2}
+                               fi
+                       done
+               fi
+       done
+
+       # Trying to unlock the SIM card.
+       modem_sim_unlock ${device} ${code} ${new_pin}
+
+       exit $?
+}
+
 function cli_hostname() {
        if cli_help_requested $@; then
                cli_show_man network
@@ -389,6 +558,43 @@ function cli_list_hooks() {
        done | sort -u
 }
 
+function cli_route() {
+       if cli_help_requested $@; then
+               cli_show_man network-route
+               exit ${EXIT_OK}
+       fi
+
+       local action=${1}
+       shift
+
+       case "${action}" in
+               # Add a new route.
+               add)
+                       route_add $@
+                       ;;
+               # Remove an existing route.
+               remove)
+                       route_remove $@
+                       ;;
+               # List all routes.
+               list)
+                       route_list $@
+                       return ${EXIT_OK}
+                       ;;
+               *)
+                       error "Unrecognized action: ${action}"
+                       cli_run_help network route
+
+                       exit ${EXIT_ERROR}
+                       ;;
+       esac
+
+       # Applying all routes.
+       route_apply
+
+       exit ${EXIT_OK}
+}
+
 function cli_start() {
        if cli_help_requested $@; then
                cli_show_man network
@@ -544,18 +750,23 @@ function cli_dns() {
                exit ${EXIT_ERROR}
        fi
 
+       # Get the new server to process (if any).
+       local server=${1}
+       local priority=${2}
+
        case "${cmd}" in
                list)
                        __dns_server_println "SERVER" "PRIORITY"
                        dns_server_list
+                       exit ${EXIT_OK}
                        ;;
                add)
-                       log INFO "Adding new DNS server: ${server}..."
-                       dns_server_add $@
+                       log INFO "Adding new DNS server: ${server}"
+                       dns_server_add ${server} ${priority}
                        ;;
                remove)
-                       log INFO "Removing DNS server: ${server}..."
-                       dns_server_remove $@
+                       log INFO "Removing DNS server: ${server}"
+                       dns_server_remove ${server} ${priority}
                        ;;
                update)
                        # Just run the update afterwards.
@@ -567,6 +778,7 @@ function cli_dns() {
 
        # Update the local DNS configuration after changes have been made.
        dns_generate_resolvconf
+       radvd_update
 
        exit ${EXIT_OK}
 }
@@ -577,7 +789,7 @@ case "${action}" in
                init_run
                ;;
 
-       config|hostname|port|device|zone|start|stop|restart|status|reset|dns)
+       config|hostname|port|device|zone|start|stop|restart|status|reset|dns|route)
                cli_${action} $@
                ;;