#!/bin/sh
#
#
# Description:	wrapper of OCF RA IPaddr, based on original heartbeat RA.
#		See OCF RA IPaddr for more information.
#
# Author:	Xun Sun <xunsun@cn.ibm.com> 
# License:      GNU General Public License (GPL)
# Support:      linux-ha@lists.linux-ha.org
# Copyright:	(C) 2005 International Business Machines
#
#	This script manages IP alias IP addresses
#
#	It can add an IP alias, or remove one.
#
#	usage: $0 <ip-address> {start|stop|status|monitor}
#
#	The "start" arg adds an IP alias.
#
#	Surprisingly, the "stop" arg removes one.	:-)
#

. @HB_RA_DIR@/hto-mapfuncs


usage() {
    echo "usage: $0 <ip-address> $LEGAL_ACTIONS"
    exit 1
}

if [ $# != 2 ]; then
    usage
fi

# We need to split the argument into pieces that IPaddr OCF RA can
# recognize, sed is prefered over Bash specific builtin functions 
# for portability.

BASEIP=`echo $1 | sed 's%/.*%%'`
OCF_RESKEY_ip=$BASEIP; export OCF_RESKEY_ip

str=`echo $1 | sed 's%^'$BASEIP'/*%%'`
if [ ! -z "$str" ]; then
    NETMASK=`echo $str | sed 's%/.*%%'`
    OCF_RESKEY_cidr_netmask=$NETMASK; export OCF_RESKEY_cidr_netmask

    str=`echo $str | sed 's%^'$NETMASK'/*%%'`
    NIC=`echo $str | sed 's%/.*%%'`
    case $NIC in
	[0-9]*)	BROADCAST=$NIC
	    OCF_RESKEY_broadcast=$BROADCAST; export OCF_RESKEY_broadcast
	    NIC=
	    ;;
	"")	;;
	*)	BROADCAST=`echo $str | sed -e 's%^'$NIC'/*%%' -e 's%/.*%%'`
	    OCF_RESKEY_nic=$NIC; export OCF_RESKEY_nic
	    OCF_RESKEY_broadcast=$BROADCAST; export OCF_RESKEY_broadcast
	    ;;
    esac
fi

#
# Determine if this IP address is really being served, or not.
# Note that we don't distinguish if *we're* serving it locally...
#
ip_monitor() {
	PINGARGS="`pingargs $BASEIP`"
	for j in 1 2 3
	do
	  # for R1 style clusters, CTS runs this on the test monitor node
	  # so we cannot check to see if the IP address is served locally
	  # This means that the ARP spoofing is also tested
	  # But we can't tell for sure which node is serving the IP
	  if
	    @PING@ $PINGARGS >/dev/null 2>&1
	  then
	    exit 0
	  fi
	done
	
	exit 1
}

case $2 in
  monitor)	
	ip_monitor
	;;
  *)	
	;;
esac

OCF_TYPE=IPaddr
OCF_RESKEY_lvs_support=0
OCF_RESOURCE_INSTANCE=${OCF_TYPE}_$BASEIP
export OCF_TYPE OCF_RESOURCE_INSTANCE OCF_RESKEY_lvs_support

ra_execocf $2
