Commit 1ae84f05 authored by Jouke Witteveen's avatar Jouke Witteveen
Browse files

Code and comments streamlining

parent 8c873b49
......@@ -51,7 +51,9 @@ ethernet_up() {
fi
if ! ip_set; then
stop_8021x
if is_yes "${Auth8021X:-no}"; then
wpa_stop "$Interface"
fi
bring_interface_down "$Interface"
return 1
fi
......@@ -59,17 +61,12 @@ ethernet_up() {
ethernet_down() {
ip_unset
stop_8021x
bring_interface_down "$Interface"
}
# Stop wpa_supplicant if neccessary
stop_8021x() {
if is_yes "${Auth8021X:-no}"; then
. "$SUBR_DIR/wpa"
: ${WPAConfigFile:=/etc/wpa_supplicant.conf}
do_debug wpa_stop "$Interface"
fi
bring_interface_down "$Interface"
}
......
......@@ -121,6 +121,7 @@ wpa_stop() {
## Scan for networks within range
# $1: interface name
# $2: fields of the wpa_supplicant scan_results
# 1: bssid 2: frequency 3: signal level 4: flags 5: ssid
wpa_supplicant_scan() {
local interface=$1 fields=$2 spawned_wpa=no essids
# temp file used, as keeping ESSID's with spaces in their name in arrays
......@@ -140,19 +141,12 @@ wpa_supplicant_scan() {
# Sometimes, that is not enough
wpa_wait_while_state 7 "$interface" "SCANNING"
wpa_call "$interface" scan_results |
tail -n+2 |
sort -rn -k3 |
sort -u -k5 |
sort -rn -k3 |
tail -n+2 | # Remove extraneous output
sort -rn -k3 | # Sort by signal strength
sort -u -k5 | # Remove duplicates
sort -rn -k3 | # Re-sort as the removal disorders the list
cut -f"$fields" > "$essids"
# Fields are tab delimited
# Remove extraneous output from wpa_cli
# Sort by strength
# Remove duplicates
# Re-sort by strength as the removal disorders the list
# Cut to the AP/essid fields only
if is_yes "$spawned_wpa"; then
wpa_stop "$interface"
fi
......@@ -179,10 +173,9 @@ wpa_quote() {
fi
}
## Unquote a string, i.e. returned from wpa_cli
## Quoted: "string" -> string
## Non-Quoted: string -> string
## Hex: 737472696e67 -> string
## Unquote a string returned by wpa_call
## Possible representations of string: "string" 737472696e67 string
# $1: string
wpa_unquote() {
local string="$1"
if [[ $string == \"*\" ]]; then
......
......@@ -20,7 +20,7 @@ Arguments:
END
}
# Prepares $1 for use in a special quoting context
# Prepare $1 for use in a special quoting context
quote_safe() {
if [[ "$1" = \"* ]]; then
printf '""%s"' "$1"
......@@ -29,8 +29,8 @@ quote_safe() {
fi
}
# Fills PROFILES and ESSIDS with the profile names and essids of the profiles
# for interface $1.
# Fill PROFILES and ESSIDS with the profile names and essids of the profiles
# for interface $1
init_profiles() {
local i=0 essid profile
while IFS= read -r profile; do
......@@ -60,13 +60,15 @@ init_profiles() {
done < <(list_profiles)
}
# Builds ENTRIES as an argument list for dialog based on scan results in $1.
# Build ENTRIES as an argument list for dialog based on scan results in $1
init_entries() {
local i=0 sep=$'\t' flags signal ssid
while IFS=$'\t' read -r signal flags ssid; do
ENTRIES[i++]="--" # $ssid might look like an option to dialog.
ENTRIES[i++]="--" # the SSID might look like an option to dialog
ENTRIES[i++]=$ssid
if in_array "$ssid" "${ESSIDS[@]}"; then
if is_yes "${CONNECTED:-no}" && [[ "$ssid" = "$CONNECTION" ]]; then
ENTRIES[i]="*" # Currently connected
elif in_array "$ssid" "${ESSIDS[@]}"; then
if in_array "$(ssid_to_profile "$ssid")" "${GENERATED[@]}"; then
ENTRIES[i]="." # Automatically generated
else
......@@ -75,9 +77,6 @@ init_entries() {
else
ENTRIES[i]=" " # Not present
fi
if is_yes "${CONNECTED:-no}" && [[ "$ssid" = "$CONNECTION" ]]; then
ENTRIES[i]="*" # Currently connected
fi
if [[ "$flags" =~ WPA2|WPA|WEP ]]; then
ENTRIES[i]+="${sep}${BASH_REMATCH[0],,}"
else
......@@ -88,7 +87,7 @@ init_entries() {
done < "$1"
}
# Finds a profile name for ssid $1.
# Find a profile name for ssid $1
ssid_to_profile() {
local i
for i in $(seq 0 $((${#ESSIDS[@]}-1))); do
......@@ -114,7 +113,7 @@ Do you want to overwrite it?"
fi
}
# Creates a profile for ssid $1.
# Create a profile for ssid $1
create_profile() {
local box flags key msg security
PROFILE="$INTERFACE-${1//\//_}"
......@@ -140,13 +139,13 @@ create_profile() {
else
key=$(quote_safe "$key")
fi
elif [[ "${#key}" -ge 8 && "${#key}" -le 63 ]]; then
elif [[ ${#key} -ge 8 && ${#key} -le 63 ]]; then
if is_yes "${OBSCURE:-no}"; then
key="\"$(wpa_passphrase "$1" "$key" | sed -n "s/^[[:space:]]*psk=//p")"
else
key=$(quote_safe "$key")
fi
elif [[ "${#key}" -eq 64 && "$key" = +([[:xdigit:]]) ]]; then
elif [[ ${#key} -eq 64 && "$key" = +([[:xdigit:]]) ]]; then
key="\"$key"
else
return 4
......@@ -165,8 +164,8 @@ EOF
return 0
}
# Connects to ssid $1 using an available profile or an automatically created
# one if none exists.
# Connect to ssid $1 using an available profile or an automatically created one
# if none exists
connect_to_ssid() {
local msg
PROFILE=$(ssid_to_profile "$1")
......@@ -194,6 +193,7 @@ Do you want to keep the generated profile ('$PROFILE')?"
return 0
}
while [[ "$1" = -* ]]; do
case "$1" in
-h|--help)
......@@ -225,7 +225,7 @@ fi
INTERFACE=$1
if [[ -z "$INTERFACE" ]]; then
INTERFACE=(/sys/class/net/*/wireless/)
if [[ "${#INTERFACE[@]}" -ne 1 || ! -d "$INTERFACE" ]]; then
if [[ ${#INTERFACE[@]} -ne 1 || ! -d "$INTERFACE" ]]; then
report_error "Invalid interface specification"
usage
exit 255
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment