#!/usr/bin/perl
# $Source: /cvs/failoverd/ha-switch,v $
# $Revision: 1.3 $ 
# $Date: 1999/05/20 20:46:02 $
#
use Failover;

$interface_file = "/proc/net/dev";
$ifconfig = "/sbin/ifconfig";
$route = "/sbin/route";
$arp = "/sbin/arp";
$default_route = "192.168.2.1";
$ip_chains = "/sbin/ipchains";

@eth0 = ('192.168.2.111','192.168.2.255','255.255.255.0','00:90:27:37:05:CF');
@eth1 = ('10.0.0.1','10.0.0.255','255.0.0.0','00:90:27:37:0A:F5','10.0.0.0');

if ( $ARGV[0] eq "" ) {
	print "Usage: $0 [start|stop]\n";
	exit(1);
	} elsif ( $ARGV[0] eq "start" ) { start();
	} elsif ( $ARGV[0] eq "stop" ) { stop(); 
	}
	

sub start {
Failover::logger("$0 executing --> start");
for ($i=0; $i<=10; $i++) {
# This is very brute force, but I've noticed that sometimes the interface 
# does not want to allow this to happen.  Can't hurt anything..
system("$ifconfig eth0 down");
system("$ifconfig eth0 hw ether $eth0[3]");
system("$ifconfig eth1 down");
system("$ifconfig eth1 hw ether $eth1[3]");
}

system("$ifconfig", "eth0", "$eth0[0]", "broadcast", "$eth0[1]", "netmask", "$eth0[2]", "up");
if ( $? != 0 ) { print "ERROR configuring eth0\n"; &logger("eth0"); }
system("$ifconfig", "eth1", "$eth1[0]", "broadcast", "$eth1[1]", "netmask", "$eth1[2]", "up");
if ( $? != 0 ) { print "ERROR configuring eth1\n"; &logger("eth1"); }
system("$route", "add", "default", "gw", "$default_route");
if ( $? != 0 ) { print "ERROR adding default route: $default_route\n"; }

while ( $count <= 100 ) {
# brute force, but is proven to work..

	system("$arp", "-i", "eth0",  "-s", "$eth0[0]", "$eth0[3]","pub");
	system("$arp", "-i", "eth1", "-s", "$eth1[0]", "$eth1[3]","pub");
	$count++;
  } # while

 system("$ip_chains", "-A", "forward", "-j", "MASQ", "-s", 
"$eth1[4]\/$eth1[2]", "-d", "0\.0\.0\.0\/0");
} # sub start

sub stop {
  system("$ip_chains", "-F");
  logger("$0 executing --> halt");

for ($i=0; $i<=10; $i++) {
  system("$ifconfig", "eth0", "down");
  system("$ifconfig", "eth1", "1.9.8.2");
  system("$ifconfig", "eth1", "down");
 }
sleep 1;
logger("$0 finished --> halt");
exit(0);
} # sub fail
