#!/bin/sh

################################################################################
##                                                                            ##
## Copyright (c) International Business Machines  Corp., 2005                 ##
##                                                                            ##
## 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    ##
##                                                                            ##
##                                                                            ##
################################################################################
#
# File:
#   bg_tcp_traffic
#
# Description:
#   Control the background TCP traffic
#
# Author:
#   Mitsuru Chinen <mitch@jp.ibm.com>
#
# Arguments:
#   $1: command (make/check/kill/killall)
#   $2: IP address when the command is make
#       Process ID when the command is check or kill
#
# Outputs:
#   Process ID of the TCP traffic server when the command is make
#
# Exit Value:
#    0: Exit normally
#   >0: Exit abnormally
#
# History:
#   Oct 19 2005 - Created (Mitsuru Chinen)
#
#-----------------------------------------------------------------------
#Uncomment line below for debug output.
#trace_logic=${trace_logic:-"set -x"}
$trace_logic

# Make sure the value of LTPROOT
LTPROOT=${LTPROOT:-`(cd ../../../../ ; pwd)`}
export LTPROOT

# Check the environmanet variable for the test
. check_envval || exit 1

# Timeout till client starts up [sec]
CLIENT_TIMEOUT=10


#-----------------------------------------------------------------------
#
# Function: usage
#
# Description:
#   Print the usage of this script, then exit
#
#-----------------------------------------------------------------------
usage(){
    cat << EOD >&2
bg_tcp_traffic command [argument]
  command:  make / check / kill / killall
  argument: IP address of the server (if command is make)
            Process ID (if command is check/kill)
            (none)  (if command is killall)
EOD

    exit 1
}


#-----------------------------------------------------------------------
#
# Function: make_traffic
#
# Description:
#   Make a background tcp traffic.
#   The local host will be a server, and the remote host will be a client
#
# Arguments:
#   $1: IP address of the server
#
# Exit Value:
#   0: Success
#   1: Fail
#
#-----------------------------------------------------------------------
make_traffic() {
    server_ipaddr=$1

    # Identify the family is ipv4 or ipv6
    family=0
    echo $server_ipaddr | fgrep "." >/dev/null 2>&1
    if [ $? -eq 0 ]; then
	family=4
    else
	echo $server_ipaddr | fgrep ":" >/dev/null 2>&1
	if [ $? -eq 0 ]; then
	    family=6
	fi
    fi

    if [ $family -eq 0 ]; then
	echo "The IP address of the server is something wrong." >&2
	exit 1
    fi

    # Find the available consecutive ports
    server_port=`find_portbundle tcp 1025 1`
    if [ $? -ne 0 ]; then
	echo "No port is available." >&2
	exit 1
    fi

    # Start up a server
    infofile=`mktemp -p $TMPDIR`
    ns-tcpserver -b -f $family -p ${server_port} -o $infofile

    while true ; do	# Wait till ns-tcpserver outputs the information
	if [ -s $infofile ]; then
	    break
	fi
    done

    server_pid=`fgrep PID: $infofile | awk '{ print $2 }'`

    rm -f $infofile

    if [ x$server_pid = x ]; then
	echo "TCP traffic server does not run" >&2
	exit 1
    fi

    # Start up a client
    $LTP_RSH $RHOST "${LTPROOT}/testcases/bin/ns-tcpclient -b -S $server_ipaddr -f $family -p $server_port"

    count=0
    while true ; do
	$LTP_RSH $RHOST "ps auxw | fgrep -v grep | fgrep -l ns-tcpclient >/dev/null 2>&1"
	if [ $? -ne 0 ]; then
	    if [ $count -lt $CLIENT_TIMEOUT ]; then
		count=`expr $count + 1`
		sleep 1
		continue
	    else
		echo "TCP traffic client does not run" >&2
		kill -SIGHUP $server_pid
		exit 1
	    fi
	else
	    # Output the process ID of the server, the finished
	    echo $server_pid
	    exit 0
	fi
    done
}


#-----------------------------------------------------------------------
#
# Function: check_traffic
#
# Description:
#   Check the TCP traffic exists
#
# Arguments:
#   $1: Process ID of the TCP traffic server
#
# Exit Value:
#   0: The connectivity is good.
#   1: The connectivity is something wrong.
#
#-----------------------------------------------------------------------
check_traffic()
{
    server_pid=$1

    if [ ! -d /proc/$server_pid ]; then
	echo "TCP traffic server has gone." >&2
	exit 1
    fi
    exit 0
}


#-----------------------------------------------------------------------
#
# Function: kill_traffic
#
# Description:
#   Kill the TCP traffic
#
# Arguments:
#   $1: Process ID of the TCP traffic server
#
# Exit Value:
#   Always 0
#
#-----------------------------------------------------------------------
kill_traffic()
{
    server_pid=$1

    kill -SIGHUP $server_pid >/dev/null 2>&1
    exit 0
}


#-----------------------------------------------------------------------
#
# Function: killall_traffic
#
# Description:
#   Kill all of the TCP traffic
#
# Exit Value:
#   Always 0
#
#-----------------------------------------------------------------------
killall_traffic()
{
    killall_tcp_traffic
}

#
# Main
#
command=$1

case $command in
    make)
    if [ $# -ne 2 ]; then
	usage
    fi
    make_traffic $2
    ;;

    check)
    if [ $# -ne 2 ]; then
	usage
    fi
    check_traffic $2
    ;;

    kill)
    if [ $# -ne 2 ]; then
	usage
    fi
    kill_traffic $2
    ;;

    killall)
    if [ $# -ne 1 ]; then
	usage
    fi
    killall_traffic $2
    ;;

    *)
    usage
    ;;
esac