#!/bin/sh
#
#	$Id: SendArp.in,v 1.1.2.2 2005/08/17 09:10:17 horms Exp $
#
#	Copyright (C) 2004 Horms <horms@verge.net.au>
#
#       Based on IPaddr2: Copyright (C) 2003 Tuomo Soini <tis@foobar.fi>
#
#	This script send out gratuitous Arp for an IP address
#
#       It can be used _instead_ of the IPaddr2 or IPaddr resource
#       to send gratuitous arp for an IP address on a given interface, 
#       without adding the address to that interface. I.e. if for
#       some reason you want to send gratuitous arp for addresses
#       managed by IPaddr2 or IPaddr on an additional interface.
#
#	usage: $0 ip-address[/netmaskbits[/interface[:label][/broadcast]]] \
#	    {start|stop|status|monitor}
#
#	The "start" arg adds an IP alias.
#
#	Surprisingly, the "stop" arg removes one.	:-)
#
#

set -e

prefix=/usr
exec_prefix=/usr
. /etc/ha.d/shellfuncs

SENDARP=$HA_BIN/send_arp
VARLIB=/var/lib/heartbeat
VLDIR=$VARLIB/rsctmp/IPaddr
SENDARPPIDDIR=$VARLIB/rsctmp/send_arp
USAGE="usage: $0 ip-address/interface {start|stop|status|monitor}";

#
#	Set BASEIP for use in other parts of script.
#	Find out which interface to use with findif utility and
#	parse findif output.
#
BASEIP=`echo $1 | sed "s%/.*%%"`
INTERFACE=`echo $1 | sed "s%${BASEIP}/%%"`
RESIDUAL=`echo $1 | sed "s%${BASEIP}/${INTERFACE}%%"`
SENDARPPIDFILE="$SENDARPPIDDIR/send_arp-$BASEIP"

#
#	Check if ip-address is running.
#	We return stopped or running.
#
status() {
    if
	[ -f "$SENDARPPIDFILE" ]
    then
	echo "running"
    else
	echo "stopped"
    fi
}

#
#	Send gratuitous arp
#
start() {

    case `status $1` in
	running)
	    exit 0
	    ;;
    esac

    [ -r ${HA_CONFDIR}/arp_config ] && . ${HA_CONFDIR}/arp_config
    if [ -r "${HA_CONFDIR}/arp_config:${TARGET_INTERFACE}" ]
    then
	. "${HA_CONFDIR}/arp_config:${TARGET_INTERFACE}"
    fi

    # Set default values

    : ${ARP_INTERVAL_MS=200}	# milliseconds between ARPs
    : ${ARP_REPEAT=5}		# repeat count
    : ${ARP_BACKGROUND=yes}	# no to run in foreground
    : ${ARP_NETMASK=ffffffffffff}	# netmask for ARP


    ARGS="-i $ARP_INTERVAL_MS -r $ARP_REPEAT -p $SENDARPPIDFILE $INTERFACE $BASEIP auto $BASEIP $ARP_NETMASK"
    ha_log "$SENDARP $ARGS"
    case $ARP_BACKGROUND in
	yes) 
	    ($SENDARP $ARGS || 
	        ha_log "ERROR: Could not send gratuitous arps") &
	    ;;
	*)
	    $SENDARP $ARGS || ha_log "ERROR: Could not send gratuitous arps"
	    ;;
    esac
}

#
#	Stop sending gratuitous arp
#
stop() {
    rc=0
    if
	[ -f "$SENDARPPIDFILE" ]
    then
	kill `cat "$SENDARPPIDFILE"`
	rc=$?
	case $rc in
	    0)
		ha_log "info: killed previously running send_arp for $BASEIP"
		rm -f "$SENDARPPIDFILE"
		;;
	    *)
		ha_log "WARN: Could not kill previously running send_arp for $BASEIP"
		;;
	esac
    fi

    case $rc in
	0)
	    ha_log "info: SendArp for $BASEIP/$INTERFACE released"
	    ;;
	*)
	    ha_log "WARN: SendArp for $BASEIP/$INTERFACE NOT released"
	    ;;
    esac
    return $rc
}

#
#	This is always active, because it doesn't do much
#
monitor() {
    echo "OK"
    return 0
}

usage() {
  echo -e $USAGE >&2
}

if
    [ $# -ne 2 -o -z "$INTERFACE" -o -z "$BASEIP" -o -n "$RESIDUAL" ]
then
    usage
    exit 1
fi

case $2 in
    start)
	start $1
	;;
    stop)
	stop $1
	;;
    status)
	status $1
	;;
    monitor)
	monitor $1
	;;
    *)
	usage
 	exit 1
	;;
esac

# EOF - end of file
