wpa-service 0.2 release in progress

Eloi Primaux eloi at bliscat.org
Thu Oct 12 15:48:26 MDT 2006


i've strongly updated the wpa-service script, here is the (0.2pre1)
now it can use ifplugd, i used functions to make it cleaner and simpler

the advantages are:
- wpa_supplicant can deal with encryptions from NULL to WPA2
- all interfaces are controlled by only one instance of the
wpa_supplicant daemon 
- all other regular network services can be use as if we were dealing
wih an regular internet card


#!/bin/bash
# Begin $network-devices/services/wpa-service
# release 0.1+4m (0.2pre1)
# (k) 2006 Eloi Primaux for anything else than LFS (kidding)
# eloi AT bliscat DOT org


IFACE=$1
BRING=$2
VERBOSE=1

. /etc/rc.d/init.d/functions
# . $IFCONFIG
. /etc/sysconfig/network-devices/ifconfig.$IFACE/wpa-service

function get_real_pid {
	if [ -e $1 ]; then
	RET=`fuser $1` &> /dev/null
	else
	return 1
	fi
}

function wpa_is_up {
	get_real_pid $WPA_GLOBAL_FILE
	if [ $? != 0 ]; then
		return 2
	fi
}

function start_wpa {
	verbose -n "Starting $WPA_DAEMON_NAME"
	$WPA_DAEMON_NAME -g$WPA_GLOBAL_FILE -P$WPA_PID_FILE -B
	RET=$?
	if [ $RET != 0 ]; then
		boot_mesg "Can't start $WPA_DAEMON_NAME, error $RET, exiting"
		evaluate_retval
		exit $RET
	else
		verbose '' " OK"
		evaluate_retval
	fi
}

function wpa {
	wpa_is_up
	[ $? != 0 ] && start_wpa
}

function is_iface_not_managed {
	[ -n "`get_real_pid $WPA_ACCESS_DIR/$IFACE`" ] && return 2
}

function verbose {
	[ -n "$VERBOSE" ] && boot_mesg $1 "$2"
}

function test_fail {
	[ "$1" != 0 ] && RET="FAIL:$1"
	verbose '' " $RET"
	if [[ $RET = FAIL* ]]; then
		echo_failure ; return 2
	else
		return $1
	fi
}

function add_iface {
	verbose -n "$WPA_CLIENT_NAME -g$WPA_GLOBAL_FILE interface_add $IFACE ''
$WPA_DRIVER $WPA_ACCESS_DIR :"
	RET=`$WPA_CLIENT_NAME -g$WPA_GLOBAL_FILE interface_add $IFACE ''
$WPA_DRIVER $WPA_ACCESS_DIR` &> /dev/null
	test_fail $?
}

function ctrl_iface {
	verbose -n "$WPA_CLIENT_NAME -i$IFACE $@ :"
	RET=`$WPA_CLIENT_NAME -i$IFACE $@` &> /dev/null
	test_fail $?
}

function remove_iface {
	verbose -n "$WPA_CLIENT_NAME -g$WPA_GLOBAL_FILE interface_remove
$IFACE :"
	RET=`$WPA_CLIENT_NAME -g$WPA_GLOBAL_FILE interface_remove $IFACE`
&> /dev/null
	test_fail $?
}

function is_iface_status {
	ctrl_iface status
	echo $?
	verbose '' "Expected status is $1, status is $RET"
	echo $?
	if [[ $RET = *$1 ]]; then
		return 0
	else
		return 2
	fi
}

function available_netwok {
	ctrl_iface scan
	while [ $? != 0 ]
	do
		sleep 2
		is_iface_status SCANNING
	done
	ctrl_iface scan_results
	RET0="`echo "$RET" | cut -f5- -d/`"
	RET="`echo "$RET0" | cut -f5-`"
	RET0="`echo "$RET" | grep -v "ssid"`"
	RET="$RET0"
	unset RET0
}


function configure_network { 
	#Read the wpa config file
	LINENUMBER=0
	CONFIGFILE="$WPA_CONFIG_DIR/$WPA_CONFIG_FILE"
	LINES="$(wc -l $CONFIGFILE | sed 's/ .*//')"
	while [ "$LINENUMBER" -lt "$LINES" ]
	do
				echo "NETWORK=$NETWORK"
		# Increase line number
		let ++LINENUMBER
		verbose '' "Parsing line ${LINENUMBER}"
		# Fetch a line
		preline="$(head -n $LINENUMBER $CONFIGFILE | tail -n 1)"
		# Remove everything after a '#' (comment)
		line="$(echo $preline | sed -e 's/#.*//')"

		#echo "Parsing $LINENUMBER: '$preline' => '$line'"

		if [ -z "`echo $line`" ]; then
		# ignore now empty lines
			continue
		elif [ "$line" = "network={" ]; then
		# creating a new network configuration,
		# saving network number to NETWORK
			ctrl_iface add_network
			NETWORK="$RET"
		elif [ "$line" = "}" ]; then
		# now the network is configured, enabling it
			ctrl_iface "enable_network $NETWORK"
			unset NETWORK
		elif [ -n "$( echo "$line" | grep -v "ctrl_interface")" ]; then
			# all others lines should be network parameters...
			# i need to replace the first '=' character by a pace
			line="`echo "$line" | sed 's,=, ,'`"
			verbose '' "ctrl_iface set_network $NETWORK $line"
			RET="`$WPA_CLIENT_NAME -i $IFACE set_network $NETWORK $line`"
			#ctrl_iface set_network $NETWORK $line
		fi
		if [[ $RET = FAIL* ]]; then
			boot_mesg "Parse error on line ${LINENUMBER}"
			remove_iface 
			echo_failure
			return 2
		fi
	done
}


function wait_for_up {
	COUNTER=0
	while (( "$COUNTER" <= "$WPA_MAX_WAIT" ))
	do let ++COUNTER
		is_iface_status CONNECTED
		if [ $? = 0 ]; then
			return 0
		fi
		sleep 2s
	done
	return 1
}

function if_plugd {
	case "$1" in
	start)
	ifplugd -Msf -i$IFACE -r /etc/sysconfig/network-devices/ifplugd.$IFACE
	;;
	stop)
	ifplugd -kW -i$IFACE
	;;
	esac
}

function real_fail {
	[ "$1" != 0 ] && RET="FAIL:$1"
	verbose '' " $RET"
	if [[ $RET = FAIL* ]]; then
		exit $1
	else
		return $1
	fi
}

function iface_up {
	wpa
	is_iface_not_managed
	if [ $? = 0 ]; then
		verbose '' "Interface already managed, continuing"
	else
		add_iface
		real_fail $?
	fi
	configure_network
	real_fail $?
	if_plugd start
}


function iface_down {
	wpa
	if_plugd stop
	ctrl_iface disconnect
	test_fail $?
	wpa_cli -g$WPA_GLOBAL_FILE interface_remove $IFACE
	test_fail $?	
}

case "$2" in
	up)
	iface_up
	;;
	down)
	iface_down
	;;
esac

# end of wpa-service




More information about the hints mailing list