summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJames Rayner <james@archlinux.org>2009-08-10 12:43:58 +0200
committerJames Rayner <james@archlinux.org>2009-08-10 12:43:58 +0200
commit78e2fa6621750e4f2a108f90598d9a7e798b46b3 (patch)
tree525873acd2adacd093bc1695fb080021944c26b2 /src
parent9a3078d8cec470b3820c5597d5c8819d642e558d (diff)
parent1400a8362753016dacc699679e8b9daebd3c8513 (diff)
downloadnetctl-78e2fa6621750e4f2a108f90598d9a7e798b46b3.tar.gz
netctl-78e2fa6621750e4f2a108f90598d9a7e798b46b3.tar.xz
merge changes since revert
Diffstat (limited to 'src')
-rw-r--r--src/8021x4
-rw-r--r--src/connections/ethernet60
-rw-r--r--src/connections/ethernet-iproute53
-rw-r--r--src/connections/wireless110
-rw-r--r--src/netcfg1
-rw-r--r--src/network63
-rw-r--r--src/wireless100
7 files changed, 301 insertions, 90 deletions
diff --git a/src/8021x b/src/8021x
index 75030e9..cccfd3a 100644
--- a/src/8021x
+++ b/src/8021x
@@ -16,9 +16,8 @@ wpa_check()
sleep 1
let timeout++
done
-
+ echo "$wpa_state"
wpa_cli terminate >/dev/null 2>&1
- err_append "Authentication/association failed"
return 1
}
@@ -30,7 +29,6 @@ start_wpa()
sleep 1
if [[ ! -f "/var/run/wpa_supplicant_${INTERFACE}.pid" ]]; then
- err_append "wpa_supplicant did not start, possible configuration error"
return 1
fi
}
diff --git a/src/connections/ethernet b/src/connections/ethernet
index e24e9f5..86d8b96 100644
--- a/src/connections/ethernet
+++ b/src/connections/ethernet
@@ -6,16 +6,16 @@ ethernet_up() {
if [[ ! -e /sys/class/net/"$INTERFACE" ]]; then
if ! echo "$INTERFACE"|grep ":"; then
- err_append "interface $INTERFACE does not exist"
+ report_fail "interface $INTERFACE does not exist"
return 1
fi
fi
- ip link set $INTERFACE up
- sleep 1
+ report_debug ethernet_up ifup
+ set_interface up $INTERFACE
if ip link show $INTERFACE|grep -q "NO-CARRIER"; then
- err_append "No connection"
+ report_fail "No connection"
return 1
fi
@@ -23,7 +23,11 @@ ethernet_up() {
. "${SUBR_DIR}"/8021x
[[ -z "$WPA_CONF" ]] && WPA_CONF="/etc/wpa_supplicant.conf"
[[ -z "$WPA_OPTS" ]] && WPA_OPTS="-Dwired"
- start_wpa "$INTERFACE" "$WPA_CONF" "$WPA_OPTS"
+ report_debug ethernet_up start_wpa "$INTERFACE" "$WPA_CONF" "$WPA_OPTS"
+ if ! start_wpa $INTERFACE $WPA_CONF $WPA_OPTS; then
+ report_fail "wpa_supplicant did not start, possible configuration error"
+ return 1
+ fi
if ! wpa_check "$INTERFACE"; then
ifconfig "$INTERFACE" down
return 1
@@ -34,8 +38,9 @@ ethernet_up() {
dhcp)
if checkyesno "${DHCLIENT:-no}"; then
rm -r /var/run/dhclient-${INTERFACE}.pid >/dev/null 2>&1
+ report_debug ethernet_up dhclient -q -e TIMEOUT="${DHCP_TIMEOUT:-10}" -pf "/var/run/dhclient-$INTERFACE.pid" "$INTERFACE"
if ! dhclient -q -e TIMEOUT="${DHCP_TIMEOUT:-10}" -pf /var/run/dhclient-${INTERFACE}.pid $INTERFACE; then
- err_append "DHCP IP lease attempt failed."
+ report_fail "DHCP IP lease attempt failed."
return 1
fi
else
@@ -44,37 +49,44 @@ ethernet_up() {
# If using own dns, tell dhcpcd to NOT replace resolv.conf
[[ -n "$DNS1" ]] && DHCP_OPTIONS="-C resolv.conf $DHCP_OPTIONS"
# Start dhcpcd
+ report_debug ethernet_up dhcpcd -qL -t "${DHCP_TIMEOUT:-10}" $DHCP_OPTIONS "$INTERFACE"
if ! dhcpcd -qL -t "${DHCP_TIMEOUT:-10}" $DHCP_OPTIONS "$INTERFACE"; then
- err_append "DHCP IP lease attempt failed."
+ report_fail "DHCP IP lease attempt failed."
return 1
fi
fi
- [[ -n "$IFOPTS" ]] && ifconfig "$INTERFACE" $IFOPTS
+ if [[ -n "$IFOPTS" ]]; then
+ report_debug ethernet_up ifup $IFOPTS
+ ifconfig "$INTERFACE" $IFOPTS
+ fi
;;
static)
+ report_debug ethernet_up ifup $IFOPTS
if ! ifconfig "$INTERFACE" $IFOPTS up; then
- err_append "Bringing interface up failed."
+ report_fail "Bringing interface up failed."
return 1
fi
# bring up the default route (gateway)
if [[ -n "$GATEWAY" ]]; then
+ report_debug ethernet_up route add default gw "$GATEWAY"
if ! route add default gw $GATEWAY; then
- err_append "Adding gateway failed."
+ report_fail "Adding gateway failed."
return 1
fi
fi
;;
*)
- err_append "IP=\"\" must be either 'dhcp' or 'static'."
+ report_fail "IP=\"\" must be either 'dhcp' or 'static'."
return 1
;;
esac
# set the hostname
if [[ -n "$HOSTNAME" ]]; then
+ report_debug ethernet_up hostname "$HOSTNAME"
if ! hostname "$HOSTNAME"; then
- err_append "Setting hostname failed."
+ report_fail "Setting hostname failed."
return 1
fi
fi
@@ -103,25 +115,33 @@ ethernet_down() {
dhcp)
if checkyesno "${DHCLIENT:-no}"; then
if [[ -f /var/run/dhclient-${INTERFACE}.pid ]]; then
- kill `cat /var/run/dhclient-${INTERFACE}.pid`
+ report_debug ethernet_down kill dhclient
+ kill $(cat /var/run/dhclient-${INTERFACE}.pid)
fi
else
if [[ -f /var/run/dhcpcd-${INTERFACE}.pid ]]; then
+ report_debug ethernet_down dhcpcd -qx "$INTERFACE"
dhcpcd -qx "$INTERFACE"
fi
fi
;;
static)
- [[ -n "$GATEWAY" ]] && route del default gw $GATEWAY
+ if [[ -n "$GATEWAY" ]]; then
+ report_debug ethernet_down route del default gw "$GATEWAY"
+ route del default gw $GATEWAY
+ fi
;;
esac
- ifconfig $INTERFACE 0.0.0.0
- case "$CONNECTION" in # Keep interface up for wireless
- ethernet|ethernet-old)
- quirk "nodown" || ifconfig $INTERFACE down
- ;;
- esac
+ report_debug ethernet_down ifdown
+ set_interface down $INTERFACE
+}
+
+# Returns status of profile - is it still functional?
+ethernet_status() {
+ if ! ip link show dev ra0|grep -q "state UP"; then
+ return 1
+ fi
}
ethernet_$1 $2
diff --git a/src/connections/ethernet-iproute b/src/connections/ethernet-iproute
index 1a4a6e4..2f0c485 100644
--- a/src/connections/ethernet-iproute
+++ b/src/connections/ethernet-iproute
@@ -1,9 +1,9 @@
#! /bin/bash
. /usr/lib/network/network
-error()
+report_iproute()
{
- err_append "$*"
+ report_fail "$*"
ip addr flush $INTERFACE &>/dev/null
quirk "nodown" || ip link set $INTERFACE down &>/dev/null
exit 1
@@ -14,15 +14,15 @@ ethernet_up() {
if [[ ! -e /sys/class/net/$INTERFACE ]]; then
if ! echo "$INTERFACE"|grep ":"; then
- error "Interface $INTERFACE does not exist"
+ report_iproute "Interface $INTERFACE does not exist"
fi
fi
- ip link set $INTERFACE up
- sleep 1
+ report_debug ethernet_iproute_up ifup
+ set_interface up $INTERFACE
- if ip link show $INTERFACE|grep -q "NO-CARRIER"; then
- err_append "No connection"
+ if ip link show $INTERFACE|grep -Fq "NO-CARRIER"; then
+ report_fail "No connection"
return 1
fi
@@ -31,7 +31,11 @@ ethernet_up() {
[[ -z "$WPA_CONF" ]] && WPA_CONF="/etc/wpa_supplicant.conf"
[[ -z "$WPA_OPTS" ]] && WPA_OPTS="-Dwired"
- start_wpa "$INTERFACE" "$WPA_CONF" "$WPA_OPTS"
+ if ! start_wpa $INTERFACE $WPA_CONF $WPA_OPTS; then
+ report_fail "wpa_supplicant did not start, possible configuration error"
+ return 1
+ fi
+
if ! wpa_check "$INTERFACE"; then
ip link set $INTERFACE down
return 1
@@ -45,41 +49,46 @@ ethernet_up() {
# If using own dns, tell dhcpcd to NOT replace resolv.conf
[[ -n "$DNS" ]] && DHCP_OPTIONS="-C resolv.conf $DHCP_OPTIONS"
-
+
+ report_debug ethernet_iproute_up dhcpcd -qL -t "${DHCP_TIMEOUT:-10}" $DHCP_OPTIONS "$INTERFACE"
if ! dhcpcd -qL -t ${DHCP_TIMEOUT:-10} $DHCP_OPTIONS $INTERFACE; then
- error "DHCP IP lease attempt failed"
+ report_iproute "DHCP IP lease attempt failed"
fi
;;
static)
if [[ -n "$ADDR" ]]; then
+ report_debug ethernet_iproute_up ip addr add "$ADDR/24" brd + dev "$INTERFACE"
if ! ip addr add ${ADDR}/24 brd + dev $INTERFACE; then
- error "Could not configure interface"
+ report_iproute "Could not configure interface"
fi
fi
if [[ -n "$GATEWAY" ]]; then
+ report_debug ethernet_iproute_up ip route add default via "$GATEWAY"
if ! ip route add default via $GATEWAY; then
- error "Adding gateway failed"
+ report_iproute "Adding gateway failed"
fi
fi
;;
*)
- error "Profile error: IP must be either 'dhcp' or 'static'"
+ report_iproute "Profile report_iproute: IP must be either 'dhcp' or 'static'"
;;
esac
if [[ -n "$IPCFG" ]]; then
for line in "${IPCFG[@]}"; do
-
+
+ report_debug ethernet_iproute_up ip "$line"
if ! ip $line; then
- error "Could not configure interface"
+ report_iproute "Could not configure interface"
fi
done
fi
# Set hostname
if [[ -n "$HOSTNAME" ]]; then
+ report_debug ethernet_iproute_up hostname "$HOSTNAME"
if ! hostname $HOSTNAME; then
- error "Cannot set hostname"
+ report_iproute "Cannot set hostname"
fi
fi
@@ -103,13 +112,21 @@ ethernet_down() {
if [[ "$IP" == "dhcp" ]]; then
if [[ -f /var/run/dhcpcd-${INTERFACE}.pid ]]; then
+ report_debug ethernet_iproute_down dhcpcd -qx "$INTERFACE"
dhcpcd -qx $INTERFACE
fi
fi
+
+ report_debug ethernet_iproute_down if_down
+ set_interface down $INTERFACE
- ip addr flush $INTERFACE
- quirk "nodown" || ip link set $INTERFACE down
+}
+# Returns status of profile - is it still functional?
+ethernet_status() {
+ if ! ip link show dev ra0|grep -q "state UP"; then
+ return 1
+ fi
}
ethernet_$1 $2
diff --git a/src/connections/wireless b/src/connections/wireless
index 8b43ed1..10f751e 100644
--- a/src/connections/wireless
+++ b/src/connections/wireless
@@ -1,43 +1,73 @@
#! /bin/bash
. /usr/lib/network/network
+
+rfkill_from_name() {
+ local name=$1
+ for rfkill in /sys/class/rfkill/*; do
+ if [[ "$(cat $rfkill/name)" == $name ]]; then
+ echo $rfkill
+ return 0
+ fi
+ done
+ echo "none"
+ return 1
+}
+
wireless_up() {
load_profile $1
. ${SUBR_DIR}/8021x
. ${SUBR_DIR}/wireless
+ # If rfkill is specified, enable device.
+ if [[ -n "$RFKILL_NAME" ]]; then
+ path=$(rfkill_from_name $RFKILL_NAME)
+ if [[ $? -ne 0 ]]; then
+ report_fail "no rfkill switch with the name $RFKILL_NAME";
+ fi
+ echo 1 > ${path}/state
+ sleep 1
+ fi
+
# Check if interface exists
if [[ ! -e /sys/class/net/"$INTERFACE" ]]; then
- if ! echo "$INTERFACE"|grep ":"; then
- err_append "interface $INTERFACE does not exist"
+ if ! echo "$INTERFACE"|grep -Fq ":"; then
+ report_fail "interface $INTERFACE does not exist"
return 1
fi
fi
# Kill any lingering wpa_supplicants.
+ report_debug wireless_up stop_wpa "$INTERFACE"
stop_wpa $INTERFACE
-
+
# Most drivers (mac80211) need mode set before device is brought up
# Drivers generally default to managed, but set this to be sure.
- if [[ "$(iwgetid -sm $INTERFACE)" -ne "Managed" ]]; then
+ if [[ $(iwgetid -sm $INTERFACE) -ne Managed ]]; then
+ report_debug wireless_up iwconfig "$INTERFACE" mode managed
iwconfig $INTERFACE mode managed
fi
- ifconfig $INTERFACE up
+ report_debug wireless_up ifup
+ set_interface up $INTERFACE || return 1
quirk "prescan" && iwlist $INTERFACE scan &> /dev/null # bcm43xx
quirk "preessid" && eval "iwconfig $INTERFACE mode managed essid \"$ESSID\"" # ipw3945
if checkyesno ${SCAN:-no}; then
+ report_debug wireless_up scanning
if ! find_essid $INTERFACE "$ESSID"; then
- err_append "Network not present."
+ report_fail "Network not present."
return 1
fi
fi
# Manually set iwconfig options
- [[ "$IWCONFIG" ]] && iwconfig $INTERFACE $IWCONFIG
+ if [[ "$IWCONFIG" ]]; then
+ report_debug wireless_up iwconfig "$INTERFACE" $IWCONFIG
+ iwconfig $INTERFACE $IWCONFIG
+ fi
# Set to 'none' if not set
[[ -z "$SECURITY" ]] && SECURITY="none"
@@ -52,15 +82,21 @@ wireless_up() {
fi
quirk "predown" && ifconfig $INTERFACE down # madwifi FS#10585
-
+
+ report_debug wireless_up iwconfig "$INTERFACE" $WEP_OPTS
if ! eval iwconfig $INTERFACE $WEP_OPTS; then
- err_append "Could not set wireless configuration."
+ report_fail "Could not set wireless configuration."
return 1
fi
quirk "predown" && ifconfig $INTERFACE up # madwifi FS#10585
- wep_check $INTERFACE $TIMEOUT||return 1
+ report_debug ethernet_up wep_check
+
+ if ! wep_check $INTERFACE $TIMEOUT; then
+ report_fail "WEP Association Failed"
+ return 1
+ fi
;;
wpa)
@@ -76,14 +112,17 @@ wireless_up() {
if [[ "${#KEY}" == "64" ]]; then
echo -e "network={ \nssid=\"$ESSID\" \npsk=$KEY \n}">> $WPA_CONF
elif ! echo "$KEY" | wpa_passphrase "$ESSID" >> $WPA_CONF; then
- err_append "Configuration generation failed. $(cat $WPA_CONF)"
+ report_fail "Configuration generation failed. $(cat $WPA_CONF)"
return 1
fi
# Connect!
[[ -z "$WPA_OPTS" ]] && WPA_OPTS="-Dwext"
+ report_debug wireless_up start_wpa "$INTERFACE" "$WPA_CONF/wpa.conf" "$WPA_OPTS"
start_wpa $INTERFACE $WPA_CONF $WPA_OPTS || return 1
+ report_debug wireless_up wpa_check
if ! wpa_check $INTERFACE $TIMEOUT; then
+ report_fail "WPA Authentication/Association Failed"
stop_wpa $INTERFACE
return 1
fi
@@ -92,31 +131,64 @@ wireless_up() {
. ${SUBR_DIR}/8021x
[[ -z "$WPA_CONF" ]] && WPA_CONF="/etc/wpa_supplicant.conf" # defaults
[[ -z "$WPA_OPTS" ]] && WPA_OPTS="-Dwext"
- start_wpa $INTERFACE $WPA_CONF $WPA_OPTS || return 1
+ report_debug wireless_up start_wpa "$INTERFACE" "$WPA_CONF" "$WPA_OPTS"
+ if ! start_wpa $INTERFACE $WPA_CONF $WPA_OPTS; then
+ report_fail "wpa_supplicant did not start, possible configuration error"
+ return 1
+ fi
+ report_debug wireless_up wpa_check
if ! wpa_check $INTERFACE $TIMEOUT; then
+ report_fail "WPA Authentication/Association Failed"
stop_wpa $INTERFACE
return 1
fi
;;
esac
- if ! ${CONN_DIR}/ethernet up $1; then
+ conn=ethernet
+ checkyesno ${IPROUTE:-no} && conn=ethernet-iproute
+ if ! ${CONN_DIR}/$conn up $1; then
wireless_down $1 YES
return 1
fi
}
wireless_down() {
- load_profile $1
- . ${SUBR_DIR}/8021x
PROFILE=$1 NOETHERNETDOWN=$2
- if ! checkyesno $2; then
- ${CONN_DIR}/ethernet down $1
+ load_profile $PROFILE
+ . ${SUBR_DIR}/8021x
+ if ! checkyesno $NOETHERNETDOWN; then
+ conn=ethernet
+ checkyesno ${IPROUTE:-no} && conn=ethernet-iproute
+ $CONN_DIR/$conn down $PROFILE
fi
+ report_debug wireless_down stop_wpa "$INTERFACE"
stop_wpa $INTERFACE
- [[ "$SECURITY" == "wpa" ]] && rm -f "/tmp/wpa.${1// /}" # remove wpa config
+ [[ "$SECURITY" == "wpa" ]] && rm -f "/tmp/wpa.${PROFILE// /}" # remove wpa config
+ report_debug wireless_down iwconfig "$INTERFACE" essid off key off
iwconfig $INTERFACE essid off key off &> /dev/null
- ifconfig $INTERFACE down
+ set_interface down $INTERFACE
+
+ # If rfkill is specified, disable device.
+ if [[ -n "$RFKILL_NAME" ]]; then
+ path=$(rfkill_from_name $RFKILL_NAME)
+ if [[ $? -ne 0 ]]; then
+ report_fail "no rfkill switch with the name $RFKILL_NAME";
+ fi
+ echo 0 > ${path}/state
+ fi
+
+}
+
+# Returns status of profile - is it still functional?
+wireless_status() {
+ load_profile $1
+ if [[ "$(iwgetid -r)" -ne $ESSID ]]; then
+ return 1
+ elif ! ip link show dev ra0|grep -q "state UP"; then
+ return 1
+ fi
+
}
wireless_$1 $2
diff --git a/src/netcfg b/src/netcfg
index 3222d46..bea963c 100644
--- a/src/netcfg
+++ b/src/netcfg
@@ -1,7 +1,6 @@
#!/bin/bash
. /etc/rc.conf
-. /etc/rc.d/functions
. /usr/lib/network/network
NETCFG_VER=2.2.1
diff --git a/src/network b/src/network
index b45caaa..ccf0893 100644
--- a/src/network
+++ b/src/network
@@ -1,20 +1,5 @@
### Globals
-PROFILE_DIR="/etc/network.d/"
-SUBR_DIR="/usr/lib/network/"
-CONN_DIR="${SUBR_DIR}/connections/"
-STATE_DIR="/var/run/network/"
-
-### Messages
-##
-# err msg
-# output specified message
-err_append() {
- echo " - $*"
-}
-
-err() {
- printhl "$*"
-}
+. /usr/lib/network/globals
### Profile loading
##
@@ -30,16 +15,16 @@ validate_profile()
{
[[ -z "$1" ]] && return 1
if [[ ! -f $PROFILE_DIR/$1 ]]; then
- err "Profile \"$1\" does not exist"
+ report_fail "Profile \"$1\" does not exist"
return 1
fi
. $PROFILE_DIR/$1
if [[ -z "$INTERFACE" ]]; then
- err "Profile missing an interface to configure"
+ report_fail "Profile missing an interface to configure"
return 1
fi
if [[ ! -f $CONN_DIR/$CONNECTION ]]; then
- err "$CONNECTION is not a valid connection, check spelling or look at examples"
+ report_fail "$CONNECTION is not a valid connection, check spelling or look at examples"
return 1
fi
}
@@ -93,7 +78,7 @@ profile_up()
load_profile $1 || exit 1
- check_profile $1 && err "$1 already connected" && exit 1
+ check_profile $1 && report_fail "$1 already connected" && exit 1
# NETWORKS_EXCLUSIVE, rc.conf: Profiles are globally mutually exclusive
# EXCLUSIVE, network.d/profile: Individual profile is mutually exclusive
@@ -101,12 +86,12 @@ profile_up()
all_down
fi
- stat_busy "$1 up"
+ report_try "$1 up"
if check_iface $INTERFACE; then
if checkyesno $CHECK; then
err_append "Interface $INTERFACE already in use"
- stat_fail && exit 1
+ report_fail && exit 1
else
interface_down $INTERFACE || exit 1
load_profile $1
@@ -117,7 +102,7 @@ profile_up()
eval $PRE_UP || exit 1
if ! ${CONN_DIR}/${CONNECTION} up $1; then
- stat_fail
+ report_fail
exit 1
fi
@@ -126,7 +111,7 @@ profile_up()
set_profile up $1
unset EXCLUSIVE
- stat_done
+ report_success
); return $?
}
@@ -141,28 +126,28 @@ profile_down()
load_profile $1 || exit 1
if ! check_profile $1; then
- err "Profile not connected"
+ report_fail "Profile not connected"
exit 1
fi
- stat_busy "$1 down"
+ report_try "$1 down"
if [[ "$(get_iface_prof $INTERFACE)" == "external" ]]; then
err_append "$interface was connected by another application"
- stat_fail
+ report_fail
exit 1
fi
eval $PRE_DOWN || exit 1
if ! ${CONN_DIR}/${CONNECTION} down $1; then
- stat_fail
+ report_fail
exit 1
fi
eval $POST_DOWN || exit 1
set_profile down $1
- stat_done
+ report_success
); return $?
}
@@ -267,6 +252,26 @@ set_iface() {
fi
}
+set_interface()
+{
+ INTERFACE=$2
+ case $1 in
+ up)
+ at_interface_up
+ ip link set dev $INTERFACE up
+ sleep ${UP_SLEEP:-2}
+ ;;
+ down)
+ at_interface_down
+ ip addr flush dev "$INTERFACE"
+ quirk nodown || ip link set dev "$INTERFACE" down
+ ;;
+ *)
+ return 1
+ ;;
+ esac
+}
+
### From FreeBSD's /etc/rc.subr
##
diff --git a/src/wireless b/src/wireless
new file mode 100644
index 0000000..f45028e
--- /dev/null
+++ b/src/wireless
@@ -0,0 +1,100 @@
+# Uses wireless_tools, to check for association to a network.
+# wep_check interface [timeout]
+wep_check()
+{
+ local INTERFACE=$1 TIMEOUT=${2:-15} timeout=0 bssid
+
+ while [[ $timeout -ne $TIMEOUT ]]; do
+ bssid=`iwgetid $INTERFACE -ra`
+ [[ ! "$bssid" = "00:00:00:00:00:00" ]] && return 0
+ sleep 1
+ let timeout++
+ done
+ return 1
+}
+
+# Check if a particular network is within range
+# find_essid interface essid
+find_essid() {
+ local INTERFACE="$1" ESSID="$2" RETRIES=20 try=0 res scanned
+ while [[ "$try" -lt "$RETRIES" ]]; do
+ sleep 0.5
+ let try++
+ found=$(
+ res=$(iwlist "$INTERFACE" scan 2>/dev/null)
+ [[ -z "$res" ]] && exit 1
+ # if results were non-null, process them and exit 0
+ echo "$res" | sed -nr 's/^\s+ESSID:"([^"]*)"$/\1/p' | fgrep -xm1 "$ESSID"
+ ) && {
+ scanned=1
+ report_debug find_essid "\"$found\""
+ # we only bother with at most 5 successful scans
+ if (( try < RETRIES-4 )); then try=$((RETRIES-4)); fi
+ }
+ if [[ -n "$found" ]]; then
+ return 0 # network found
+ fi
+ done
+ if [[ "$scanned" -ne 1 ]]; then
+ report_debug find_essid "unable to scan"
+ fi
+ return 1
+}
+
+# Check if a particular network is within range
+# find_ap interface ap
+find_ap() {
+ local INTERFACE="$1" ap=$(echo "$2" | tr 'abcdef' 'ABCDEF') RETRIES=20 try=0 res scanned
+ while [[ "$try" -lt "$RETRIES" ]]; do
+ sleep 0.5
+ let try++
+ found=$(
+ res=$(iwlist "$INTERFACE" scan 2> /dev/null)
+ [[ -z "$res" ]] && exit 1
+ # if results were non-null, process them and exit 0
+ echo "$res" | sed -nr '/^\s+Cell .. - Address: ([[:xdigit:]:]+)$/ { s//\1/; N; s/(.*)\n\s+ESSID:"([^"]*)"$/\1\t\2/p }' \
+ | egrep -m1 "^$ap\t"
+ ) && {
+ scanned=1
+ report_debug find_ap "\"$found\""
+ # we only bother with at most 5 successful scans
+ if (( try < RETRIES-4 )); then try=$((RETRIES-4)); fi
+ }
+ if [[ -n "$found" ]]; then
+ echo "$found" | cut -f2 # JP: echo literal ESSID
+ return 0
+ fi
+ done
+ if [[ "$scanned" -ne 1 ]]; then
+ report_debug find_ap "unable to scan"
+ fi
+ return 1
+}
+
+# Return a filename containing a list of network ESSID's found.
+# list_networks interface
+list_networks()
+{
+ # temp file used, as keeping ESSID's with spaces in their name in arrays
+ # is hard, obscure and kinda nasty. This is simpler and clearer.
+
+ [[ -z "$1" ]] && return 1
+ essids=$(mktemp /tmp/essid.XXXXX)
+
+ let try=0;
+ RETRIES=6;
+ while [[ $try -ne $RETRIES ]]; do
+ iwlist $1 scan 2> /dev/null|grep ESSID|sed 's/.*ESSID:"\([^"]\+\)".*/\1/' > $essids
+ sleep 0.5; let try++
+ done
+ sort -u $essids -o $essids
+
+ # File of 0 length, ie. no ssid's.
+ if [[ ! -s $essids ]]; then
+ return 1
+ fi
+
+ echo $essids
+ return 0
+}
+# vim: set ts=4 et sw=4 ft=sh: