#!/bin/sh
# Shell test the duplicate address detection with command arping

############
# CHANGELOG
#===========
# 2011/03/03    CRMS00296013    Michel Sulyan
#               Popup timeout of 10 seconds instead 6
#
# 2010/10/19    CRMS00267137    Thanh lam NGUYEN
#               Update the delay between arp ping packet to "$delay" seconds
#
# 2010/08/24    CRMS00255212    Thanh lam NGUYEN
#               Fix the awk that get the duplicate mac address
#
# 2010/07/20    CRMS00243340    Thanh lam NGUYEN
#               Fix the ip address display
#
# 2010/05/05    Thanh lam NGUYEN
#               Adapt the script to our environment, add oneshot mode.
#
# 2010/04/26    Francois MEYER
#               Creation
############

source /etc/functions
scriptname="ipdad"
POPUP_ID="$scriptname.$$.popup"

attr="$esc[32m" default="$esc[0m"

MinTime=0
MaxTime=600

# CRMS00267137
arp_delay=30
# CRMS00267137 END

has_duplicate()
# IN:   $1  IP address to check
#       $2  1 not blocking mode
# RET:  0   IP address has been found
#       1   IP address has not been found
{
    local addr="$1" \
        noblock="$2" \
        args \
        stdout \
        ret \

    dupmac=
    if [[ "$noblock" -eq 1 ]]; then
        args="-D -c 2"
    else
# CRMS00267137
        args="-D -f -d $arp_delay"
# CRMS00267137 END
    fi
# CRMS00243340
    stdout="$(/usr/bin/arping -I $ENETCFG_INTERFACE $args -s $addr $addr)"
# CRMS00243340 END
    ret=$?
    if [[ $ret -ne 0 ]]; then
        ## IP address found
        ##------------------
        : "${attr}stdout=$stdout${default}"
# CRMS00255212
        dupmac=$(echo "$stdout" | awk '/(reply|request) from/{mac=$5; gsub( /\[|\].*/, "", mac ); print mac}')
# CRMS00255212 end
        : "${attr}dupmac=$dupmac${default}"
        # reformat the mac to AA:BB:CC:DD:EE:FF format
        eval " \
        IFS=\":\"; \
        set -- \$dupmac; \
        dupmac=\$(printf \"%02X-%02X-%02X-%02X-%02X-%02X\" \$((0x\${1:-0})) \$((0x\${2:-0})) \$((0x\${3:-0})) \$((0x\${4:-0})) \$((0x\${5:-0})) \$((0x\${6:-0}))); \
        unset IFS
        "
        : "${attr}dupmac=$dupmac${default}"
        ret=0
    else
        ## IP address not found
        ##----------------------
        ret=1
    fi
    return $ret
}

Usage()
{
    echo "Syntax: $scriptname [<ip_addr>][<time>]"
    echo ""
    echo "Test if the <ip_addr> is found on the network.";
    echo ""
    echo "Where:"
    echo "    <ip_addr>\tis the IPv4 address to test."
    echo "    <time>\tis the elapse time beetween 2 tests."
    echo ""
    echo "Notes:"
    echo "    - when <time> is absent, the test is done only once."
    echo "      The result is return in the return code."
    echo "          0 means that the address has been found."
    echo "          1 means that the address has not been found"
    echo "    - <time> has to be beetween $MinTime and $MaxTime secons."
    echo ""
    echo "Example:"
    echo "    To test 172.25.26.33 every 2 minutes, the command is"
    echo "        $scriptname 172.25.26.33 120"
    echo ""
}

exit_ko()
{
    echo "KO"
    exit 1
}

exit_ok()
{
    echo "OK"
    exit 0
}

## Check argument number
##-----------------------
if [[ $# -gt 2 ]]; then
    Usage
    exit_ko
fi

## Read current configuration
##----------------------------
readCurrentConfig

## Get param from cmd line
##-------------------------
ipAddr=$1
delay=${2:-0}

## Check param
##-------------
#### IP ADDRESS
if [[ -n "$ipAddr" ]]; then
    check_ip $ipAddr
    ret=$?
    if [[ $ret -ne 0 ]]; then
    	# check if it is a number
    	delay=$(echo "$(($ipAddr+0))") 2>/dev/null
    	unset ipAddr
    	ret=$?
    fi
    if [[ $ret -ne 0 ]]; then
        # ipAddr is neither an ip or a delay
        Usage
        exit_ko
    fi
fi

#### DELAY VALUE
if [[ ${delay} -ne 0 ]]; then
    oneshot=0
    [[ $delay -lt $MinTime ]] && delay=$MinTime
    [[ $delay -gt $MaxTime ]] && delay=$MaxTime
else
    oneshot=1
fi

## Do the job
##------------
## polling loop
while :; do
    : "${attr}======================================================================${default}"
    : "${attr}======================================================================${default}"
    displaypopup -k -i $POPUP_ID 2>/dev/null
    displaymsg ""
    readCurrentConfig
    : "${attr}looking for duplicate ip${default}"
    has_duplicate ${ipAddr:-$ENETCFG_IPADDR} $oneshot
    ret=$?
    if [[ $ret -eq 0 ]] && [[ -n "$dupmac" ]]; then
        ## duplicate found
# CRMS00243340
        MESSAGE="Duplicate IP ${ipAddr:-$ENETCFG_IPADDR} used by [$dupmac]"
# CRMS00243340 END
# CRMS00296013
        displaypopup -i $POPUP_ID -t 10 -l error $MESSAGE
# CRMS00296013 END
        displaymsg $MESSAGE
        display "$MESSAGE"
    fi

    ## stop here if no delay was given
    : "${attr}only oneshot ?${default}"
    [[ $oneshot -eq 1 ]] && exit $res

    : "${attr}sleeping for $delay second(s)${default}"
    sleep $delay
    : "${attr}waking up${default}"
done
displaypopup -k -i $POPUP_ID 2>/dev/null
displaymsg ""
