#! /bin/sh
#
#   Copyright (c) International Business Machines  Corp., 2000
#
#   This program is free software;  you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY;  without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
#   the GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program;  if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
#
#  FILE             : mc_cmds
#  TEST DESCRIPTION : To determine the stability of the IP Multicast product
#                and to verify the accuracy and usablility of IP Multicast
#                related publications associated with changes and/or
#                additions to command level interfaces for this implementations
#                of IP Multicast.
#  HISTORY:
#    03/26/01 Robbie Williamson (robbiew@us.ibm.com)
#      -Ported
#
#*******************************************************************************

#Uncomment line below for debug output.
#trace_logic=${trace_logic:-"set -x"}
$trace_logic

TC=mc_cmds
PING_OUT=/tmp/${TC}_out
# Set the LTPROOT variable to the correct path
# from the ltp/testcases/bin directory or the
# /ltp/testcases/network/multicast/mc_cmds directory
pwd | grep -q bin
if [ $? -eq 0 ]; then
  LTPROOT=${LTPROOT:-../..}
else
  LTPROOT=${LTPROOT:-../../../..}
fi
HOSTNAME=`hostname`
CLEANUP=${CLEANUP:-ON}
#*******************************************************************************
#
# FUNCTION:  do_test
# PURPOSE:   Test changes to ifconfig, netstat, and ping commands
# INPUT:     None
# OUTPUT:    Error messages are logged when any verification test
#            fails.
#
#-----------------------------------------------------------------------
do_test()
{
   $trace_logic
   echo "Getting ALL enabled interfaces which are configured !"

   IFNAME=${IFNAME:-$(netstat -i -n | grep "^[b-z]\{2,4\}[0-9][^*]"|\
      awk '{print $1}')}
   INTERFACE=${INTERFACE:-$($LTPROOT/tools/gethost `hostname` | grep addresses: | awk '{print $2}')}

   echo "Testing ifconfig and netstat"

# Commented begin
#   # See if interface can be used for IP Multicast
#   ifconfig $IFNAME | grep -q MULTICAST
#   if [ $? != 0 ]; then
#	end_testcase "Multicast not listed for $IFNAME"
#   fi
#   netstat -gn | grep $IFNAME | grep -q 224.0.0.1
#   [ $? = 0 ] || end_testcase "all-host-group for $IFNAME not joined"
# Commented end

#Finds the active ethernet interface
for eth in $IFNAME
    do
         ifconfig $eth | grep -q $INTERFACE
          if [ $? -eq 0 ]
          then
                INTERFACE_DEVICE=$eth
		break
           fi
    done

# Added for support for more adapters configured
    MulticastAdap=
    for eth in $IFNAME 
    do

	$trace_logic
	ifconfig $eth | grep -q MULTICAST
	if [ $? -eq 0 ]
	then
	    $trace_logic
	    MulticastAdap=$eth
	    break
	fi
    done
    if [ -z $MulticastAdap ]
    then
	end_testcase "Multicast not listed for $IFNAME"
    fi
    MulticastAdapJoined=
    for eth in $IFNAME
    do
	$trace_logic
	netstat -gn | grep $eth | grep -q 224.0.0.1
	if [ $? -eq 0 ]
	then
	    $trace_logic
	    MulticastAdapJoined=1
	    break
	fi
    done
    if [ -z $MulticastAdapJoined ]
    then
	end_testcase "all-host-group for $IFNAME not joined"
    fi
# Added end

   # Do the ping tests: Execute ping 224.0.0.1 - Verify that the proper 
   # number of responses are returned.

   echo "Ping all-host-groups address(over default route!)"
   ping -c2 224.0.0.1 > $PING_OUT
   [ $? = 0 ] || end_testcase "No response from MC hosts to ping 224.0.0.1"

   echo "Ping all-host-groups over specified interface"
   ping -c2 -I $INTERFACE 224.0.0.1 > $PING_OUT
  if [ $? -ne 0 ]
   then
        echo "Trying to ping with $INTERFACE_DEVICE instead of IP address"
        echo "with the -I option"
        ping -c2 -I $INTERFACE_DEVICE 224.0.0.1 > $PING_OUT
   [ $? = 0 ] || end_testcase "No response from MC hosts to ping -c2 -I$INTERFACE 224.0.0.1"
fi
   grep -q $INTERFACE $PING_OUT
   [ $? = 0 ] || end_testcase "Local host did not respond to ping -c2 -I$INTERFACE 224.0.0.1"


}

#-----------------------------------------------------------------------
#
# FUNCTION:     do_cleanup
# PURPOSE:      Called when the testcase is interrupted by the user
#               or by interrupt_testcase() when time limit expired
# INPUT:        None.
# OUTPUT:       None.
#
#-----------------------------------------------------------------------

do_cleanup()
{
   $trace_logic
   echo "$this_file: doing $0."
   rm -rf $PING_OUT
}

#=============================================================================
# FUNCTION NAME:        end_testcase
#
# FUNCTION DESCRIPTION: Clean up
#
# PARAMETERS:           string, IF AND ONLY IF the testcase fails
#
# RETURNS:              None.
#=============================================================================

end_testcase()
{
   $trace_logic
   echo "$this_file: doing $0."

   # Call other cleanup functions
   [ $CLEANUP = "ON" ] && do_cleanup

   [ $# = 0 ] && { echo "Test Successful"; exit 0; }
   echo "Test Failed: $@"
   exit 1
}

#*******************************************************************************
#
# FUNCTION:  MAIN
# PURPOSE:   To invoke functions that perform the tasks as described in
#            the design in the prolog above.
# INPUT:     See SETUP in the prolog above.
# OUTPUT:    Logged run results written to testcase run log
#
#*******************************************************************************
do_test
end_testcase
