#!/usr/bin/perl
# Done by James Seng (jseng@technet.sg)

($host,$remote,$local)=@ARGV;
$host;

$port=113;
$AF_INET = 2;
$SOCK_STREAM = 1;

$SIG{'INT'}='dokill';
sub dokill {
    kill 9,$child if $child;
}

$sockaddr = 'S n a4 x8';

chop($hostname = `hostname`);

($name,$aliases,$proto) = getprotobyname('tcp');
($name,$aliases,$port) = getservbyname($port,'tcp') unless $port =~ /^\d+$/;
($name,$aliases,$type,$len,$thisaddr)=gethostbyname($hostname);
($name,$aliases,$type,$len,$thataddr)=gethostbyname($host);

$this=pack($sockaddr,$AF_INET,0,$thisaddr);
$that=pack($sockaddr,$AF_INET,$port,$thataddr);

socket(S,$AF_INET,$SOCK_STREAM,$proto);
bind(S,$this);
connect(S,$that);

select(S); $|=1; select(STDOUT);

if ($child=fork) {
   print(S "$remote, $local\n");
   sleep 2;
   do dokill();
} else {
   while (<S>) {
	print;
   }
}
