#!/bin/bash
#Simple Network recognition based on MAC address
# it is based on wpa-service and shares the same network files

. /etc/sysconfig/network-recognition/network-recognition.conf
[ "x$IFACE" == "x" ] && export IFACE=$1
[ "x$IFACE" == "x" ] && exit 1

	OLDIFS=$IFS

# This function san all the net it can and
# only exits when it have finished
function scan_network {
RET=$($NET_DISCOVER_NAME -i $IFACE 2>&1 > $NETWORK_SCAN_FILE) &
}

# this function only returns smth if the device is connected (IP & MAC) 
# smth interresting is: the device doesn't need to be up.
function arpdiscover_method {
#$ARP_DISCOVER_NAME 192.168.0.254 1 eth0 | grep '00:0F:B5:EE:85:8C'
RETVAL=`$ARP_DISCOVER_NAME $ip $SCAN_ITERATIONS $IFACE | grep -i $mac`
if [ "x$RETVAL" == "x" ] ; then
	return 1
else
	return 0
fi
}

function netdiscover_method {
RET=$(cat $NETWORK_SCAN_FILE | grep -i $mac)
IFS=$OLDIFS 
if [ "x$RET" == "x" ] ; then
	return 1
else
	return 0
fi
}

# data are:
# network={
# #the network_name is the name of the file located in
#/etc/sysconfig/network.d/network_name
# #which contains network configuration (such as dhcp or ipv4-static)
# network_name=HOME
# #device name is not needed but helps
# device_name=ROUTER
# ip=IP
# mac=MAC
# }

function recognize_network {
	#Read the net devices config file
	LINENUMBER=0
	CONFIGFILE="$NETWORKDIR/$NETWORK_DEVICE_FILE"
	LINES="$(wc -l $CONFIGFILE | sed 's/ .*//')"
	while [ "$LINENUMBER" -lt "$LINES" ]
	do
		# Increase line number
		let ++LINENUMBER
		# Fetch a line
		preline="$(head -n $LINENUMBER $CONFIGFILE | tail -n 1)"
		# Remove everything after a '#' (comment)
		line="$(echo $preline | sed -e 's/#.*//')"
		var="`echo "$line" | sed 's,=, ,' | awk '{print $1 }'`"
		data="`echo "$line" | sed 's,=, ,' | awk '{print $2 }'`"
		#echo "Parsing $LINENUMBER: '$preline' => '$line'"

		if [ -z "`echo $line`" ]; then
		# ignore now empty lines
			continue
		# here starts network configuration,
		elif [ "$line" = "network={" ]; then
			unset network_name ip mac
			continue
		elif [ "$line" = "}" ]; then
		# i have all network data, trying to find device now
			if [ "$DISCOVER_METHOD" == "arpdiscover" ] ; then
		# This is the first basic and slow method, not so sure if the other is faster...
				arpdiscover_method
				RET=$?
			elif [ "$DISCOVER_METHOD" == "netdiscover" ] ; then
				netdiscover_method
				RET=$?
			else
				echo "Bad conf"
				IFS=$OLDIFS
				exit 1
			fi
		# if it ouputs 1 this is not the our network
			if [ "$RET" == "1" ]; then
				unset network_name ip mac
				continue
			fi
		# now we exit this loop
			IFS=$OLDIFS
			echo "network found"
			export network_name ip mac
			return 0
		elif [ "$var" = "network_name" ]; then
			export network_name=$data
		elif [ "$var" = "ip" ]; then
			export ip=$data
		elif [ "$var" = "mac" ]; then
			export mac=$data
		fi
	IFS=$OLDIFS
	done
}

function get_over_bugged_promiscuous_mode {
# i can't sniff the network without an ipv4 address... ok (sk2 stupid card...)
# then set it to a stupid fake ipv4 address...
# smth like: ip addr add 132.231.213.675/16 brd 255.255.255.255 scope link dev eth0
case $1 in
up)
	ip addr add $FAKE_IP/$FAKE_MASK brd $FAKE_NET_MASK scope link dev $IFACE
	;;
down)
	ip addr del $FAKE_IP/$FAKE_MASK brd $FAKE_NET_MASK scope link dev $IFACE
	;;
esac
}

# SCAN
[ "$GET_OVER_BUGGED_PROMISCUOUS_MODE" == "1" ] && get_over_bugged_promiscuous_mode $1 up
if [ "$DISCOVER_METHOD" == "arpdiscover" ] ; then
	recognize_network
elif [ "$DISCOVER_METHOD" == "netdiscover" ] ; then
	err=1
	scan_network
	sleep 3 # really needed if i don't wait the prg fails
	while [ "$err" == "1" ]
	do
		recognize_network
		err=$?
		[ "$err" == "0" ] && continue
		RET=`ps -C netdiscover | grep "netdiscover"`
		IFS=$OLDIFS
		[ "x$RET" == "x" ] && err=0
		sleep 3
		recognize_network
	done
	killall netdiscover
fi
[ "$GET_OVER_BUGGED_PROMISCUOUS_MODE" == "1" ] && get_over_bugged_promiscuous_mode $1 down

# ok test were done let see if we found somthing
if [ "x$network_name" == "x" ]; then	
	# i have nothing exiting with error 2
	exit 2
else
	# great, i have a network, setting IFCONFIG
	IFS=$OLDIFS
	echo "$network_name"
	exit 0
fi
