#!/usr/bin/perl

require "sys/socket.ph";

($them, $port) = @ARGV;

if(!$them)
{
    print "Usage: test_net server_name [port number] \n";
    print "       port number defaults to 119, the NNTP port.\n";
    print "\n";
    exit;
     
}
$port = 119 unless $port;

$AF_INET = &AF_INET;
$SOCK_STREAM = &SOCK_STREAM;

$sockaddr = 'S n a4 x8';

$hostname = $ENV{'LOCALHOST'} unless $hostname; # get name of local host 
# from environment variable
chop($hostname = `hostname`) unless $hostname;	# get name of local host 
#from Unix hostname utility
$them = $hostname unless $them; # try local machine as NNTP Server

# translate protocol name to associated number
($name,				# human-readable name of protocol
 $aliases,			# human-readable aliases of protocol
 $proto) = getprotobyname('tcp'); # protocol number ( 6 for tcp )

# translates service (port) name to corresponding number
($name,				# name of service (NNTP in this case)
 $aliases,			# aliases for service
 $port,				# port number of service (119 in this case)
 $proto) = getservbyname('nntp' ,'tcp')	# protocol (TCP in this case)
	unless $port =~ /^\d+$/;

# translates network hostname to corresponding number
($name,$aliases,$type,$len,$thisaddr) =
	gethostbyname($hostname);

$this_numeric = unpack('N', $thisaddr);
if( !defined $name ) # something went wrong in gethostbyname
{				# try fully qualified domain name (FQDN)
				# if $hostname from Unix hostname command
    chop($local_host = `hostname`);
    if($hostname eq $local_host)
    {
	chop($domain = `domainname`);
	$fqdn = $local_host . '.' . $domain; 
# try Fully Qualified Domain Name
	($name,		   # Domain name of host
	 $aliases,	   # aliases of the host
	 $type,		   # type of address AF_INET = 2 (IP Address Type)
	 $len,		   # length of address in bytes (4 for IP)
	 $thisaddr) = gethostbyname($fqdn);
    }
}
elsif( $hostname =~ /\d+/ && $this_numeric == $hostname )
{			      # if hostname is strict number gethostbyname
			      # may return that number as the IP address!
    chop($local_host = `hostname`);

    if($hostname eq $local_host) # hostname from hostname command
    {
	chop($domain = `domainname`);
	$fqdn = $local_host . '.' . $domain; 
# try fully qualified domain name
	($name, $aliases, $type, $len, $thisaddr) =
	    gethostbyname($fqdn);
    }
}



# translate network hostname to corresponding number
if($them =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/)
{
	$thataddr = pack('C4',$1,$2,$3,$4);
}
elsif($them =~ /(\w+)(\.\w+)*/)
{
	($name,$aliases,$type,$len,$thataddr) = gethostbyname($them);
}
else
{
	die "Fatal error: NNTP host not specified in proper format!\n";
}
$this = pack($sockaddr, $AF_INET, 0, $thisaddr);
$that = pack($sockaddr, $AF_INET, $port, $thataddr);


# Make the socket a filehandle

if(socket(S, $AF_INET, $SOCK_STREAM, $proto))
{
	print "socket created ok\n";
}
else
{
	die "Fatal Error: socket failed ", $1,"\n";
}

# give the socket an address
if(bind(S, $this))
{
	print "bind socket to local address ok\n";
}
else
{
    ($ip1, $ip2, $ip3, $ip4) = unpack('C4', $thisaddr);
    $localaddr = $ip1 . '.' . $ip2 . '.' . $ip3 . '.' . $ip4;
    $this_numeric = unpack('N', $thisaddr);

        if ( $hostname =~ /^\d+$/ )
	{
	    print <<"EofError";
newscan: FATAL ERROR!!  
newscan: Bind to local host $hostname ( IP $localaddr ) failed!
newscan:
newscan: Your official host name ( $hostname ) is a strict number.
newscan: This can cause problems with some implementations of the sockets
newscan: networking software.  The software may interpret the numeric name
newscan: as an Internet address instead of looking up the Internet address
newscan: in the host table or domain name system.
newscan:
newscan: newscan thinks your local host's Internet address is $localaddr
newscan: As a 32 bit number, this address is $this_numeric
newscan:
newscan: If $this_numeric = $hostname ( your official host name )
newscan:    the host table lookup almost certainly failed!!
newscan:
newscan: Quick Fix (works on some systems)
newscan:
newscan:   If your host has a valid alias host name that 
nwescan: is not a strict number, e.g. $hostname.megacorp.com
newscan: specify this alias to newscan via the LOCALHOST environment
newscan: variable or the -H <alias host name> command line argument:
newscan:
newscan:     newscan -H $hostname.megacorp.com
newscan:
EofError

    if ( $opt_a ) # sound effects only if requested by user
{
    &PlayFile($audio_file); # play an audio file in Sun/Next format
}
    die "newscan: Aborting newscan - bye now :-( \n" ;
	}
	else
	{
print <<"EofError";
newscan: ERROR!!  Bind to local host $hostname (IP $localaddr ) failed!
newscan: You may want to check that TCP/IP is up and configured correctly
newscan: on your system.  For example, if you run SLIP as the author
newscan: of newscan does, you may want to verify that SLIP is running.
newscan:
EofError

    if ($opt_a)
{				# play audio file
    &PlayFile($audio_file);
}				# 
die "newscan: Aborting newscan - bye now :-( \n";
}
}

print "Just before attempt to connect to $them\n";

@nums = unpack('C4', $thataddr);
$them_ipaddr = $nums[0] . '.' . $nums[1] . '.' . $nums[2] . '.' . $nums[3];

# Call up the server
if(connect(S,$that))
{
	print "Connect to $them ( $them_ipaddr ) ok\n";
}
else
{
 die "Fatal Error: Apparently, can't connect to $them ( $them_ipaddr ) tcp/ip port $port.",
 "Error: ", $1, "\n";		# list to avoid over 80 char lines
}

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

$response = <S>; 	

print $response;

exit;







