#!/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:
#   add_ipv6addr
#
# Description:
#   Add an IPv6 address to the interface which belongs to the specified
#   test link
#
# Author:
#   Mitsuru Chinen <mitch@jp.ibm.com>
#
# Arguments:
#   $1: target host to add the IPv6 address
#	  lhost - local host / rhost - remote host
#   $2: number of the test link
#   $3: network portion of the IPv6 address
#   $4: host portion of the IPv6 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_number 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 IPv6 address and netmask
addr="${network_part}:${host_part}"
netmask=64

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

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

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