#!/bin/sh
# ----------------------------------------------------------------------------
# iproute.sh    Network routing configuration script.
# Version:      0.2
#
# Notes:        This script offers multiple routing configuration options.
#
#               Arg     Description
#               devup   Brings the specified device interface up.
#               devdn   Brings the specified device interface down.
#               client  Sets up the local machine's default gateway.
#               host    Sets up the local machine as network gateway.
#               ?       Gives interface information and configuration.
#
# Author:       Sean Blankenship <seanb/at/writeme/dot/com>.
# Modified:     12-19-2000.
# -----------------------------------------------------------------------------
# Configuration Area:
#
PPPIF="ppp0"
EXTIF="eth0"
EXTIP="192.168.1.2"
NETWORK="192.168.1.0"
NETMASK="255.255.255.0"
GATEWAY="192.168.1.1"
UNIVERSE="0.0.0.0/0"
BROADCAST="255.255.255.255"
# -----------------------------------------------------------------------------
case $1 in
        devup)
                # if external interface is not up, bring it up...
                if ! /sbin/ifconfig | grep $EXTIF >/dev/null; then
                        /sbin/ifconfig $EXTIF $EXTIP netmask $NETMASK up
                        /sbin/route add -net $NETWORK netmask $NETMASK $EXTIF
                fi
                ;;
        devdn)
                /sbin/ifconfig $EXTIF down
                ;;
        client)
                echo -e -n "Gateway Address? [$GATEWAY] "
                read GATEWAY
                if [ "$GATEWAY" = "" ]; then
                        echo -e "\a"
                        exit 0
                fi
                /sbin/route -n|grep -A 4 UG|grep $GATEWAY >/dev/null 2>&1; TEST="$?"
                if [ "$TEST" = "1" ]; then
                        /sbin/route add default gw $GATEWAY
                        echo -e ""
                else
                        /sbin/route del default gw $GATEWAY
                        echo -e "Gateway $GATEWAY exists. Removed."
                        echo -e ""
                fi
                ;;
        host)
                echo "1" > /proc/sys/net/ipv4/ip_forward
                echo "1" > /proc/sys/net/ipv4/ip_always_defrag
                /sbin/ipchains -P forward DENY
                /sbin/ipchains -A forward -s $NETWORK/24 -d $UNIVERSE -j MASQ
                /sbin/modprobe ip_masq_ftp
                /sbin/modprobe ip_masq_irc
                ;;
        ?)
                if /sbin/ifconfig | grep $EXTIF >/dev/null; then
                        EXTIP=`/sbin/ifconfig $EXTIF|awk '/inet addr/ {gsub(".*:","",$2); print $2}'`
                        EXTBR=`/sbin/ifconfig $EXTIF|awk '/inet addr/ {gsub(".*:","",$3); print $3}'`
                        EXTMA=`/sbin/ifconfig $EXTIF|awk '/inet addr/ {gsub(".*:","",$4); print $4}'`
                        echo -e "$EXTIF interface IP.....: $EXTIP"
                        echo -e "$EXTIF broadcast IP.....: $EXTBR"
                        echo -e "$EXTIF subnet mask......: $EXTMA"
                        echo -e ""
                fi
                if /sbin/ifconfig | grep $PPPIF >/dev/null; then
                        PPPIP=`/sbin/ifconfig $PPPIF|awk '/inet addr/ {gsub(".*:","",$2); print $2}'`
                        PPPPE=`/sbin/ifconfig $PPPIF|awk '/inet addr/ {gsub(".*:","",$3); print $3}'`
                        PPPMA=`/sbin/ifconfig $PPPIF|awk '/inet addr/ {gsub(".*:","",$4); print $4}'`
                        echo -e "$PPPIF interface IP.....: $PPPIP"
                        echo -e "$PPPIF peer IP..........: $PPPPE"
                        echo -e "$PPPIF subnet mask......: $PPPMA"
                        echo -e ""
                fi
                EXTGW=`/sbin/route -n|grep -A 4 UG|awk '{print $2}'`
                if [ ! "$EXTGW" = "" ]; then
                        echo -e "Default Gateway IP....: $EXTGW"
                        echo -e ""
                fi
                ;;
        *)
                echo "Usage: $(basename $0) [devup|devdn|client|host|?]"
                echo ""
                exit 1
                ;;
esac
exit 0