#!/bin/sh
#
# Copyright (c) 2000-2012 University of Utah and the Flux Group.
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file 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 Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#
. /etc/emulab/paths.sh

echo "`date`: ${interface}: ${reason}" >>$LOGDIR/dhclient-enter.log 2>&1

# For Freebsd 10
resolvconf_enable="NO"

#
# XXX Hack to force the duplex on interfaces
#
setduplex() {
    _if=$1
    _rc=0

    _out=`ifconfig $_if`
    _hmb=`echo $_out | grep -c '100baseTX'`
    _fdx=`echo $_out | grep -c 'full-duplex'`

    if [ $_hmb -ne 0 -a $_fdx -ne 0 ]; then
	echo "$_if: speed/duplex correct"
    else
        # If we are fixing the current interface, just let dhclient do it
        if [ x$_if = x$interface ]; then
	    echo "$_if: telling dhclient to fix speed/duplex"
	    medium="media 100baseTX mediaopt full-duplex"
        else
	    echo -n "$_if: fixing speed/duplex..."
	    ifconfig $_if media 100baseTX mediaopt full-duplex
	    _rc=$?
	    echo "returns $_rc"
	fi
    fi
    return $_rc
}

if [ x$reason != xREBOOT -a x$reason != xBOUND -a x$reason != xRENEW -a x$reason != xREBIND ]
then
    # do nothing
    true
elif [ -e "$ETCDIR/outer_bossnode" ]; then
    #
    # ElabinElab support.
    #
    # On inner boss/ops, we do not accept the following from the DHCPD
    # server on outer boss. This stuff has been setup in /etc/rc.conf
    # when the inner boss/ops was created. 
    #
    unset new_host_name
    unset new_domain_name_servers
    unset new_domain_name
    unset new_routers
elif [ x"$new_domain_name_servers" = "x1.1.1.1" ]; then
    #
    # ElabinElab support.
    #
    # XXX oh so hacky.  Real boss is set to return 1.1.1.1 as a name server
    # for nodes in inner elabs.  This is the hack de jour for determining
    # who has responded to our DHCP request.  If it is outer boss and we are
    # an inner node, we want to decline this offer and not configure the
    # interface
    #
    exit_status=1

    #
    # XXX since we now know that we are in an inner elab and we know which
    # interface is the real control net, we force 100Mb full-duplex on all
    # other (experimental) interfaces.  This is necessary to ensure a
    # response from the inner control net.
    #
    for _if in `ifconfig -lu`
    do
	if [ $_if != "lo0" -a x$_if != x$interface ]; then
	    setduplex $_if >>$LOGDIR/dhclient-enter.log 2>&1
	fi
    done

    #
    # XXX sleep here so we don't pummel boss with REQUEST/DECLINE pairs.
    #
    sleep 5
elif [ "$new_network_number" = "10.200.1.0" ]; then
    #
    # XXX sometime we can get a reply even if the duplex is wrong.
    # If we get such a reply and we are inside an inner elab, again
    # force 100Mb full-duplex to make sure we continue to communicate
    # with the server.
    #
    setduplex $interface >>$LOGDIR/dhclient-enter.log 2>&1
fi

echo "`date`: ${interface}: ${reason}: done" >>$LOGDIR/dhclient-enter.log 2>&1
