########################################################################
#  By accessing this software, USER_FILE.PL, you are duly informed of
#  and agree to be bound by the conditions described below in
#  this notice:
#
#  This software product, USER_FILE.PL, is developed by Bradley Huffaker
#  copyrighted(C) 1998 by the University of California, San Diego (UCSD),
#  with all rights reserved.  UCSD administers the NSF grant to CAIDA,
#  number NCR-9711092, under which this code was developed. 
#
#  USER_FILE.PL is a free software. You can redistribute it and/or modify it
#  under the terms of the CAIDA General Public License v.1 as
#  published by UCSD and is incorporated by reference herein.  USER_FILE.PL is
#  distributed WITHOUT ANY WARRANTY, IMPLIED OR EXPRESS, OF MERCHANTABILITY
#  OR FITNESS FOR A PARTICULAR PURPOSE or that the use of it will not
#  infringe on any third party's intellectual property rights.
#
#  You should have received a copy of the CAIDA General Public
#  License v.1 along with the USER_FILE.PL program.  If for any reason you
#  have not received a copy, please write to
#
#                       University of California, San Diego
#                       SDSC/CAIDA
#                       9500 Gilman Dr., MS-0505
#                       La Jolla, CA 92093 - 0505  USA
#
#  Or contact INFO@CAIDA.ORG
#######################################################################
# Goal: This parses domain specific code to build the tree, current
#	file parse mtrace.


sub ProcessNode
{
	my ($node) = @_;
	CreatePipe("mrinfo $node");
	$SIG{'ALRM'} = handler;
	alarm(18);
	my @nodes;
	my %alias;
	while ($line = <TR>)
	{
		if ($line=~/^([\d|\.]+) \(([^\)]+)\) \[([^\]]+)\]\:/)
		{
			$ip = $1;
			$node = $2;
			print ": $node($ip) $3\n";
		}
		elsif ($line=~/^\s+([\d|\.]+) -> ([\d|\.]+) \(([^\)]+)\) ([^\n]+)/)
		{
			my ($ip_a,$ip_o, $node_o, $rest) = ($1,$2,$3,$4);
			
			if (!($ip_o=~/0\.0\.0\.0/))
			{
				if ($ip_a ne $ip && !$alias{$ip_a})
				{
					$reached{$ip_a} = 1;
					$alias{$ip_a} = 1;
					my ($alias_n,$alias_i) 
						= nslookup($ip_a);
					$alias_n = $ip_a
						if (!$alias_n);
					print "= $node($ip) $alias_n($ip_a)\n"
				}
				push (@nodes,$ip_o);
				$ip2name{$ip_o} = $node_o;
				print "> $node($ip) $node_o($ip_o) $rest\n";
			}
		}
	}
	close IN;
	return @nodes;
}

sub CreatePipe
{
	my ($command) = @_;
        pipe(TR,TW);
        $pid = fork;
        if ($pid == 0)
        {
                close STDIN; close STDOUT; close STDERR; close TR;
                open (STDOUT,">&TW");
                open (STDERR,">&STDOUT");
                exec $command;
                die("I failed!");
        }
        elsif ($pid < 0)
        {
                die("I was not able to fork");
        }
                
        close TW;
}

$SIG{CHLD} = sub { wait };
sub handler
{
	kill 9, $pid
		if ($pid);
	undef $pid;
	print STDERR ("handler was called\n");
}
1;
