#!/bin/sh
# This is a standard inet initialisation
# it supposed to to be sourced by inet0 .. inet9 script
# and also inetboot
#
# GNU GPL (c) Eko M. Budi, 2004
# (c) Vector Linux, 2004
#
###########################################################
## The sourcer must set these settings
#DEVICE=eth0
#DHCP="yes"
#IPADDR="192.168.1.1"
#NETMASK="255.255.255.0"
#GATEWAY="192.168.1.1"
#PROBE="yes"
###########################################################
## The script
PATH="/sbin:/usr/sbin:/bin:/usr/bin"
start()
{
if [ "$DHCP" = "yes" ]; then
## DYNAMIC IP
echo "Starting network ${DEVICE} using a DHCP server..."
## increase DELAY if using wireless
if [ -x /etc/rc.d/rc.wireless ]; then
DELAY=20
else
DELAY=10
fi
## use HOSTNAME if available
NETNAME=`cat /etc/HOSTNAME 2>/dev/null`
if [ "$NETNAME" ]; then
dhcpcd -t $DELAY -h ${NETNAME} -d $DEVICE
else
dhcpcd -t $DELAY -d $DEVICE
fi
if [ $? != 0 ]; then
echo "FAILED"
return 1
fi
# show the status
ifconfig $DEVICE
return $?
else
## STATIC
# Test IPADDR, if invalid, skip
IPMASK=`ipmask $NETMASK $IPADDR`
if [ ! $? = 0 ]; then
echo "Invalid IP/NETMASK = $IPADDR/$NETMASK"
return 1
fi
BROADCAST=`echo $IPMASK | cut -d " " -f 1`
NETWORK=`echo $IPMASK | cut -d " " -f 2`
# Going up ..
echo "Starting network ${DEVICE} as ${IPADDR}/${NETMASK}..."
ifconfig ${DEVICE} ${IPADDR} broadcast ${BROADCAST} netmask ${NETMASK}
if [ $? != 0 ]; then
return 1
fi
# if PROBE is defined, try the gateway 3 times
if [ "$GATEWAY" ]; then
if [ "$PROBE" = "yes" ]; then
echo "Probing the gateway $GATEWAY "
for ii in 1 2 3; do
sleep 1
ping -c 1 -w $ii $GATEWAY > /dev/null
if [ $? = 0 ]; then
break;
fi
if [ $ii = 3 ]; then
echo " FAILED !!!"
echo Canceling network $DEVICE ...
ifconfig $DEVICE down
return 1
fi
done
fi
# set the default gateway if it is not defined yet
route | grep -e "^default" &> /dev/null
if [ $? != 0 ]; then
echo "Setting up gateway $GATEWAY ..."
route add default gw ${GATEWAY} netmask 0.0.0.0 $DEVICE
fi
fi
return 0
fi
}
stop() {
# stop the default gateway if it is ours
current_gw=`route -n | grep -e "^0.0.0.0" | cut -f 2 -d ' '`
if [ "$current_gw" = "$GATEWAY" ]; then
echo "Deleting gateway $GATEWAY..."
route del default
fi
# stop the interface
echo "Stopping network $DEVICE ..."
if [ "$DHCP" = "yes" ]; then
dhcpcd -k $DEVICE
else
ifconfig $DEVICE down
fi
}
status() {
ifconfig $DEVICE
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
$0 stop
sleep 1
$0 start
;;
reload)
;;
status)
status
;;
*)
echo "Usage: $0 {start|stop|restart|reload|status}"
esac