# SIP911.pl: main code to implement SIP 911 feature.
# Copyright 2002 (c) year by Columbia University. All rights reserved.
#!/bin/env perl

# Requred modules
require "m_pullPortInfo.pl";
require "m_getPortInfo.pl";
use Win32::ODBC;
use Net::SNMP;
use Net::Ping;
use Net::DNS;

# Debuging variables. Can be turned on from command line mode.
$debug = 0;
$debug2 = 0;
$debug3 = 0;
# Global variables for various use.
$SwitchReadCommunity = "sysuptime";
$Switch = "";
$SourceMAC = "";
# Option settings are detected.
if (!defined($ARGV[0])){
    print "Please enter a hostname.  Exiting....\n";
    print "Syntex: perl SIP911.pl hostname\n";
    exit 1;
} else {
    $hostname = $ARGV[0];
    if (defined($ARGV[1])){
	if($ARGV[1] eq "-debug"){$debug = $debug2 = $debug3 = 1;}
    }
}

# Test cases
#&getPortInfo('cs-4-1.net.columbia.edu', $SwitchReadCommunity, '08 00 20 9F 5A CC'); #disco
#&getPortInfo('cs-4-1.net.columbia.edu', $SwitchReadCommunity, '00 50 04 AA F7 91'); #mta
#&getPortInfo('cs-4-1.net.columbia.edu', $SwitchReadCommunity, '00 00 0C 01 AA A7'); #brooklyn
#&getPortInfo('cepsr-6-1.net.columbia.edu', $SwitchReadCommunity, '00 00 0C 07 23 B9'); #queensborough

# This determines the IP address of the source DNS.
@NslookupResult = `nslookup $hostname`;
if ($debug3) {print @NslookupResult;}
while(@NslookupResult){
    $CurrentLine = shift(@NslookupResult);
    if ($debug) {print "$CurrentLine\n";}
    if ($CurrentLine =~ /Name:/){
	$CurrentLine = shift(@NslookupResult);
	if ($CurrentLine =~ /Address:|Addresses:/){
	    $IpAddress = $CurrentLine;
	    $IpAddress =~ s/Address://g;
	    $IpAddress =~ s/\s//g;
	    if ($IpAddress !~ /d+\.d+\.d+\.d+/){
		$IpAddress =~ s/Addresses://g;
		$IpAddress =~ s/\s//g;
		@IpAddressArray = split(/\,/, $IpAddress);
		$IpAddress = $IpAddressArray[0];
	    }
	    if ($debug) {print "$IpAddress\n";}
	}
    }
}

# This determines whether or not the source is reachable at this moment.
# Tracert(windows) command is executed only after making sure the source is reachable.
$p = Net::Ping->new();
if (defined($p->ping($hostname))){
    if ($debug) {print "I could ping it\n";}
    $hop = 0;
    @TraceRouteResult = `tracert -d $IpAddress`;
    if ($debug3) {print @TraceRouteResult;}
    
    for ($j=3; $j<= ($#TraceRouteResult-2); $j++){
	$CurrentLine = $TraceRouteResult[$j];
	if($CurrentLine !~ /d+\.d+\.d+\.d+/){
	    if ($debug) {print "$CurrentLine\n";}
	    @tempArray = split(/\s\s/, $CurrentLine);
	    $thisTrace[$hop] = $tempArray[$#tempArray-1];
	    $hop++;
	}
    }
    if($debug2){
	for($i=0; $i<=$#thisTrace; $i++){
	    print "Hop:".$i." ".$thisTrace[$i]."\n";
	}
    }

    # From the last hop, resolve the ARP address if MAC is not given
    # From the last hop, figure out which interface the IP address goes to by using CDP

    # Determine if the connecting switch is Cisco or non-Cisco

    # From the switch, determine whether the address is directly connected or not
    # If the port is directly connected, pass it to &pullPortInfo() function
    # Return the result
} else {
    print "The host ".$hostname." is not pingable.\n";
}

