#!/usr/bin/sh
#
# checkTCP.sh 
#
# Checks a TCP connection by sending a large amount of data over a TCP
# connection measuring the bandwidth or the time until the connection
# aborts.
# The key is the variable drop rate of the router. Using this special
# kernel, we are able to simulate "bad" connections.
# 
#
# Steps:
#         - set the drop rate of the router (via rexec)
#         - start the timer
#         - start a ftp-put (50MB) to the target system
#         - stop time timer
#         - store the time needed (negative if ftp aborts)
#
# ----------------------------------------------------------------------------

#
# Definitions
#
ROUTER_NAME=10.2.0.1                    # router with variable "drop rate"
ROUTER_USER=sr                          # remote user to set the drop rate
ROUTER_PW=geheim                        # the password of the user
TARGET_NAME=10.2.1.100                  # target system of the connection
TARGET_USER=sr                          # the FTP user on the target system
TARGET_PW=sr                            # the password of the FTP user
DROP_START=0                            # start drop rate (in percent)
DROP_END=020                            # end drop rate (in percent)
DROP_DELTA=1                            # delta rate for drop rate (in percent)
DATA_SIZE=10000                         # amount of data to be transferred
					# (in kB)
MAXLOOP=5                               # transfer loops per drop rate
TMPFILE=/tmp/TCP.data                   # temporary test data file
RESULTFILE=checkTCP.result              # the result file

#
# Variables
#
o_trace=0                               # Trace on/off


#
# Set drop rate of router
#
set_drop_rate() # rate_percent
{
  [ $o_trace -eq 1 ] && set -x

  _rate=`expr ${1} \* 10`

  echo "Setting drop rate on '${ROUTER_NAME}' to ${1}%"

  rsh -l ${ROUTER_USER} ${ROUTER_NAME} \
	"echo ${_rate} >> /proc/sys/net/ipv4/ip_drop_rate"

  if [ $? -ne 0 ]; then
    echo "ERR: Cannot set drop rate on ${ROUTER_NAME}"
    exit 1
  fi
}

#
# Send the data and delivers the time needed as RESULT (in seconds)
#
send_data() # host user password file
{
  [ $o_trace -eq 1 ] && set -x

  echo "Starting data transfer ..."
  _Start=${SECONDS}

  ftp -n <<EOFEOF
open ${1}
user ${2} ${3}
lcd `dirname ${4}`
cd `dirname ${4}`
bin
put `basename ${4}`
EOFEOF

  _res=$?

  _End=${SECONDS}
  _Delta=`expr ${_End} - ${_Start}`

  if [ ${_res} -ne 0 ]; then
    _Delta=`expr ${_Delta} \* -1` 
  fi

  echo "Data transfer ended in ${_Delta} seconds"

  RESULT=${_Delta}
}

#
# Make a test file of desired size
#
make_test_file() # size_in_kB filename
{
  [ $o_trace -eq 1 ] && set -x
 
  _root=`awk '$2 == "/" { print $1; }' /etc/fstab`

  echo "Creating the test file '${2}' with ${1}kB ..."

  dd if=${_root} of=${2} bs=1k count=${1}
}

#
# Main
#

# Inits
trap 'exit 1' INT QUIT

# make a test file
make_test_file ${DATA_SIZE} ${TMPFILE}

# start the tests with variable drop rate
_Drop=${DROP_START}
while [ ${_Drop} -le ${DROP_END} ]; do
  set_drop_rate ${_Drop}
  echo -n "${_Drop};${DATA_SIZE}" >>${RESULTFILE}

  _i=0
  while [ ${_i} -lt ${MAXLOOP} ]; do
    send_data ${TARGET_NAME} ${TARGET_USER} ${TARGET_PW} ${TMPFILE}
    _Elapsed=${RESULT}

    echo -n ";${_Elapsed}" >>${RESULTFILE}
    _i=`expr ${_i} + 1`
  done
  echo "" >>${RESULTFILE}

  _Drop=`expr ${_Drop} + ${DROP_DELTA}`
done
