#  2006-09-11 JR Debugging version 
#     - produces log to aid debbuging with 64/32 bit problem.
#     - use enclosed two functions GetLocation() and ParseNetRange() to
#       replace those in ~/reports/30min/0traffic/MakeReport30 from the
#       package ipaudit-web-1.0BETA9.tar.gz.

sub GetLocation {
   my ($IP) = @_;
   my ($i,$InRange);

	#  Is IP in local range?
	for ($i=0;$i<@LocalNet;$i+=2) {
		$InRange = $LocalNet[$i] le $IP && $IP le $LocalNet[$i+1];
		last if $InRange;
	}

	#  Return local if InRange or out of range but range reversed.
	if ($InRange ^ $LocalNetReverse) {
		$result = "L";

	} else {

		#  Is IP in other range?
		for ($i=0;$i<@OtherNet;$i+=2) {
			$InRange = $OtherNet[$i] le $IP && $IP le $OtherNet[$i+1];
			last if $InRange;
		}
		
		#  Return other if IP InRange or out of range but range reversed
		if ($InRange ^ $OtherNetReverse) {
			$result = "O";

		#  IP is in remove range by the process of elimination.
		} else{
			$result = "R";
		}

	}
	
	#  START LOGGING CODE
	#  Initiailzie LOG FILE if not already done.
	unless ($LOGFILE) {
		$LOGFILE = '/tmp/GetLocation.log';
		open LOG, ">> $LOGFILE" or die "Cannot open log file";
	}
	# Log result
	print LOG "GetLocation: IP result ($IP) ($result)\n";
	#  END LOGGING CODE

	return $result;
}



sub ParseNetRange {
   my ($local) = @_;
   my ($NetReverse, @Net);

   ($prefix,$net) = $local=~/^([!]*)(.*)/;
   $NetReverse =  $prefix eq '!'; 

   for (split(/:/,$net)) {

      #  aaa.aaa.aaa.aaa-bbb.bbb.bbb.bbb
      if (/^([0-9\.]+)-([0-9\.]+)$/) {
         @n1 = (split(/\./,$1) , 0, 0, 0, 0);
         @n2 = (split(/\./,$2), 255, 255, 255, 255);
         $n1 = unpack("N", pack("C4",@n1));
         $n2 = unpack("N", pack("C4",@n2));

      #  aaa.aaa.aaa.aaa/nn
      } elsif (/^([0-9\.]+)\/([0-9]{1,2})$/) {
         #Find right network address
         $mask = 0; ($mask = ~0  >> $2) unless($2 > 31);
         #Find IP range
         $n1 = unpack("N",inet_aton($1)) & ~$mask;  
         $n2 = $n1 | $mask;

      #  aaa.aaa.aaa.aaa/255.255.255.0
      } elsif
(/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/) {
        $mask = unpack("N",inet_aton($2));
        $n1 = unpack("N",inet_aton($1)) & $mask;  
        $n2 = $n1 | ~$mask; 

      #  aaa.aaa
      } elsif (/^([0-9\.]+)$/) {
         @n1 = ( split(/\./,$1), 0, 0, 0, 0);
         $n1 = unpack("N", pack("C4",@n1));
         @n2 = ( split(/\./,$1), 255, 255, 255, 255);
         $n2 = unpack("N", pack("C4",@n2));

      #  Other
      } else {
         printf STDERR  "ERROR: Local Net strings format not recognized\n";
         printf STDERR  <<EOM;
         Accpted formats are:
         network/netmask,   ie: 10.1.2.0/24
                            ie: 10.20.99.64/26
                            ie: 10.100.0.0/255.255.0.0
         starthost-endhost, ie: 10.1.0.0-10.20.255.255 (or 10.1-10.20)
         single host,       ie: 10.20.30.40
         starting host,     ie: 192 (expands to 192.255.255.255)
EOM

         exit;   
      }

      #  Save localnet
      push @Net, sprintf ("%03d.%03d.%03d.%03d",
      unpack("C4",pack("N", $n1))), sprintf ("%03d.%03d.%03d.%03d",
      unpack("C4",pack("N", $n2))); 
   }
	#  START LOGGING CODE
	#  Initialize LOG FILE if not already done.
	unless ($LOGFILE) {
		$LOGFILE = '/tmp/GetLocation.log';
		open LOG, ">> $LOGFILE" or die "Cannot open log file";
	}
	print LOG "ParseNetRange: local NetRevesre Net ($local) ($NetReverse) (@Net)\n";
	#  END LOGGING CODE
   return ($NetReverse, @Net);
}
