#!/bin/sh

export PATH="/usr/sbin:/usr/bin:/sbin:/bin"

lock_file=/tmp/.write_mdns_lease_lockfile
lease_file=/tmp/mdns_ipv4_result_tmp
leasev6_file=/tmp/mdns_ipv6_result_tmp
arp_result_file=/tmp/mdns_arp_result
arp_list_file=/tmp/mdns_arp_list
final_result_file=/tmp/mdns_final_result
final6_result_file=/tmp/mdns_final6_result
final_ipv6_result=/tmp/mdns_ipv6_result
final_ipv6_result_tmp=/tmp/mdns_final_ipv6_result_tmp
mdns_result_file=/tmp/mdns_ipv4_result
run_file=/tmp/mdns_result_maintain.pid

mac2ipv6(){
    # use br-lan mac to calculate local-link ipv6 addr of br-lan
    mac_local=$1
    mac_1=0x$(echo $mac_local | awk -F : '{print $1}') #first byte is hexadecimal
    mac_1=$(((mac_1)^0x02)) # due to mac of ipv4 record is ath0 mac, br-lan need to add 2 on fist byte of ath0 mac
    mac_1=$(echo $mac_1 | awk '{printf("%02x", $1)}')
    result=$(eval "echo $mac_local | sed 's/\(..\):\(..\):\(..\):\(..\):\(..\):\(..\)/fe80::$mac_1\2:\3ff:fe\4:\5\6/' | tr 'A-Z' 'a-z'")
    echo $result
}

add_my_ip_mac_to_list(){
    my_ip="`ifconfig br-lan | grep Bcast | cut -d ' ' -f 12 | cut -c 6-`"
    my_mac=$(uci get system.@system[0].basemac | tr 'a-z' 'A-Z')
    if [ "x$my_mac" = "x" ]; then
        my_mac=$(ifconfig br-lan | grep HWaddr | awk '{print $NF}')
    fi
    my_name=`uci get system.@system[0].hostname`
    if [ "x$my_name" = "x" ]; then
        my_name="EAX17"
    fi
    #delete old entry
    eval "sed -i '/$my_mac /d' $1"
    #insert new entry
    echo "$my_mac $my_ip $my_name @#$&*!" >> $1
}

add_my_ipv6_mac_to_list(){
    my_mac=$(uci get system.@system[0].basemac | tr 'a-z' 'A-Z')
    if [ "x$my_mac" = "x" ]; then
        my_mac=$(ifconfig br-lan | grep HWaddr | awk '{print $NF}')
    fi
    my_ip=$(mac2ipv6 $my_mac)
    my_name=`uci get system.@system[0].hostname`
    if [ "x$my_name" = "x" ]; then
        my_name="EAX17"
    fi
    #delete old entry
    eval "sed -i '/$my_mac /d' $1"
    #insert new entry
    echo "$my_mac [$my_ip] $my_name @#$&*!" >> $1
}

check_ip_valid(){
	ip=`echo $line| cut -d ' ' -f 2`
	/usr/sbin/arping -f -I  br-lan -c 2 $ip > ${arp_result_file}_${1}
}

#Don't have rm $run_file because it's a endless loop
#and if the script die unreason ,the run_file do not remove
#So I use process to check if there is a process or not

touch $lock_file
touch $arp_result_file
touch $arp_list_file
touch $final_result_file

#remove own record for gratuitous arp
own_ip="`ifconfig br-lan|grep Bcast|cut -d ' ' -f 12|cut -c 6-`"
eval "sed -i '/$own_ip /d' $lease_file"

ra0_mac=$(uci get wireless.2g.macAddr | awk '{print $NF}' | tr 'a-z' 'A-Z')
if [ "x$ra0_mac" = "x" ]; then
    ra0_mac=$(ifconfig ra0 | grep HWaddr | awk '{print $NF}' | tr 'a-z' 'A-Z')
fi
eval "sed -i '/$ra0_mac /d' $leasev6_file"
# for the whitespace  ^ :
# is to deal with its own ip is 192.168.1.2 and other record is 192.168.1.2xx 

if [ "x$(cat $lease_file)" != "x" ]; then

    #excute concurrently the 'arping'
    num=0
    while read line
    do
        num=$((num + 1))
        check_ip_valid $num &
        sleep 1
    done < $lease_file

    #waiting all 'arping' done
    while true
    do
        result_pid=$(pidof arping)
        if [ "$result_pid" = "" ]; then
            break;
        fi
        sleep 1
    done

    #collect arping result to a file
    cat ${arp_result_file}_* > $arp_result_file
    rm ${arp_result_file}_*

    sed -i 's/\[//g' $arp_result_file
    sed -i 's/\]//g' $arp_result_file
    while read line
    do
        [ `echo $line |cut -d ' ' -f 1` == "Unicast" ] &&{
            ip=`echo $line |cut -d ' ' -f 4`

            mac=`echo $line |cut -d ' ' -f 5`
            #use arping to get mac will get mac like 7:7:7:7:7:7
            #but we want is 07:07:07:07:07:07 ,so use this code to change it
            #for (i=1;i<=6;i++){
            #	mac[$i]=`echo $mac | cut -d ':' -f $i`
            #	[ ${mac[$i]} -lt 10 -a ${#mac[$i]} -lt 2 ]&&{
            #		[ ${#mac[$i]} -eq 0 ]&& mac[$i]=00 || mac[$i]=0${mac[$i]}
            #	}
            #}
            #mac=${mac[1]}:${mac[2]}:${mac[3]}:${mac[4]}:${mac[5]}:${mac[6]}
            mac1=`echo $mac | cut -d ':' -f 1`
            mac2=`echo $mac | cut -d ':' -f 2`
            mac3=`echo $mac | cut -d ':' -f 3`
            mac4=`echo $mac | cut -d ':' -f 4`
            mac5=`echo $mac | cut -d ':' -f 5`
            mac6=`echo $mac | cut -d ':' -f 6`
            [  ${#mac1} -lt 2 ] &&{
                [ ${#mac1} -eq 0 ]&& mac1="00"||mac1="0$mac1"
            }

            [ ${#mac2} -lt 2 ] &&{
                [ ${#mac2} -eq 0 ]&& mac2="00"||mac2="0$mac2"
            }

            [ ${#mac3} -lt 2 ] &&{
                [ ${#mac3} -eq 0 ]&& mac3="00"||mac3="0$mac3"
            }

            [ ${#mac4} -lt 2 ] &&{
                [ ${#mac4} -eq 0 ]&& mac4="00"||mac4="0$mac4"
            }

            [ ${#mac5} -lt 2 ] &&{
                [ ${#mac5} -eq 0 ]&& mac5="00"||mac5="0$mac5"
            }

            [ ${#mac6} -lt 2 ] &&{
                [ ${#mac6} -eq 0 ]&& mac6="00"||mac6="0$mac6"
            }

            mac="$mac1:$mac2:$mac3:$mac4:$mac5:$mac6"

            echo "$mac $ip" >> $arp_list_file
        }
    done < $arp_result_file
    rm -f $arp_result_file
fi

cp -f $lease_file $final_result_file
while read line
do
    online=0
    devname=`echo $line| cut -d ' ' -f 3`
    ip=`echo $line| cut -d ' ' -f 2`
    mac=`echo $line| cut -d ' ' -f 1`
    while read arplist
    do
        arpip=`echo $arplist|cut -d ' ' -f 2`
        arpmac=`echo $arplist|cut -d ' ' -f 1`
        arpmac=$(echo $arpmac | tr '[a-z]' '[A-Z]')
        # remove mac judgement
        [ "$arpip" == "$ip" ] && { 
            online=1
            break
        }
    done <$arp_list_file
    if [ "x$online" = "x0" ]; then
        #echo "$line" >>$final_result_file
        # we use delete action to avoid risk of string to be excute when it has some the special characters in device name
        # donot delete arpip, it will delete wrong, fix EX8000-205
        eval "sed -i '/$ip /d' $final_result_file"
    fi
done <$lease_file

cp -f $leasev6_file $final6_result_file
while read line
do
    online=0
    mac_v6=`echo $line| cut -d ' ' -f 1`
    while read lease_file
    do
        mac=`echo $line| cut -d ' ' -f 1`
        # remove mac judgement
        [ "$mac_v6" == "$mac" ] && { 
            online=1
            break
        }
    done <$final_result_file
    if [ "x$online" = "x0" ]; then
        eval "sed -i '/$mac_v6 /d' $final6_result_file"
    fi
done <$leasev6_file

add_my_ip_mac_to_list $final_result_file
add_my_ipv6_mac_to_list $final6_result_file
cat $final_result_file | sort -u > $mdns_result_file
cat $final6_result_file | sort -u > $final_ipv6_result

rm -f $arp_list_file
rm -f $final_result_file
rm -f $final6_result_file
rm -f $lock_file
