#!/usr/bin/perl -w
#output /proc/net/rt_cache in a human readable format
use Socket;

@ipfields = ("Destination", "Source", "Gateway", "SpecDst") ; 

open(F, "/proc/net/rt_cache") || die "cannot open /proc/net/rt_cache: $!\n";

my $head = <F>;
%col = parsehead($head);


print "$head";

while (<F>) {	
	@f = split;
	foreach $i (@ipfields) { 
		if (defined($f[$col{$i}])) { 
			$f[$col{$i}] = inet_ntoa(pack "L", hex($f[$col{$i}]));
		}
	}

	print join("\t", @f),"\n"; 
} 

close F;



#intelligent /proc parser.
#don't hardcode columns, instead get them from the file header.
#return a hash which maps columns names to columns
sub parsehead($) { 
	my($head) = (@_); 
	my %columns = (); 

	$head =~ s/^\s+//; 
	
	my @f = split(/\s+/, $head); 
	my $c = 0; 
	foreach $i (@f) { 
		$columns{$i} = $c;
		$c++;
	}
	return %columns;
} 
