#!/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:
#   set_ipv4addr
#
# Description:
#   Set an IPv4 address to the interface which belongs to the specified
#   test link
#
# Author:
#   Mitsuru Chinen <mitch@jp.ibm.com>
#
# Arguments:
#   $1:	target host to set the IPv6 address
#	    lhost - local host / rhost - remote host
#   $2: number of the test link
#   $3:	network portion of the IPv4 address
#   $4:	host portion of the IPv4 address
#
# 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

# Arguments
if [ $# -ne 4 ]; then
    echo "Usage: $0 host_type link_num network_portion host_portion" >&2
    exit 1
fi
host_type=$1
link_num=$2
network_part=$3
host_part=$4

# Check the host type
if [ $host_type != lhost -a $host_type != rhost ]; then
    echo "$0: 1st argumet is lhost or rhost" >&2
    exit 1
fi

# Define IPv4 address, netmask and broadcast
addr=${network_part}.${host_part}
netmask=`echo $network_part | sed "s/[[:digit:]]*/255/g"`.`echo $host_part | sed "s/[[:digit:]]*/0/g"`
broadcast=${network_part}.`echo $host_part | sed "s/[[:digit:]]*/255/g"`

# Assign IPv4 address to the interface belongs the link_num Test Link
ifname=`get_ifname $host_type $link_num` || exit 1

if [ $host_type = lhost ]; then
    ifconfig $ifname up
    ifconfig $ifname $addr netmask $netmask broadcast $broadcast
    ret=$?
else
    ret=`$LTP_RSH $RHOST '( PATH=/sbin:/usr/sbin:$PATH ; ifconfig '$ifname' up && ifconfig '$ifname $addr' netmask '$netmask' broadcast '$broadcast' ) >/dev/null 2>&1; echo $?'`
fi

if [ $ret -gt 0 ]; then
    echo "Cannot set $addr to $ifname" >&2
    exit 1
fi