Re: output from test-socket

Eric Morgan (eric_morgan@library.lib.ncsu.edu)
22 Sep 1994 15:30:31 U


RE>>output from test-socket.pl

Jack,

I have added the unpack-print line to our code, and it correctly displays
Internet addresses in dot notation.

I find this whole problem a bit puzzling to me because I have written a
pseudo-SMTP mailer using perl, and that code seems to open sockets just fine,
but I was using a library and I didn't write the library myself.

I am going to put all of this on the back burner for now.

Eric Lease Morgan
NCSU Libraries

=====

#!/usr/local/bin/perl
package sock;

;# USAGE:
;# ======
;#
;# To open a connection to a socket:
;#
;#      $handle = &sock'open($hostname, $port) || die $!;
;#      # hostname & port can each be either a name or a number
;#
;# Read and write the same as with any other file handle:
;#
;#      print $handle "hello, socket\n";
;#      $response = <$handle>;
;#
;# To close cleanly:
;#
;#      &sock'close($handle);
;#
;# To close all open sockets, in case of an emergency exit:
;#
;#      &sock'close_all;
;#
;# AUTHOR:      David Noble (dnoble@ufo.jpl.nasa.gov)
;# DATE:        11 Feb 1993
;#
;# Modify and use as you see fit, but please leave my name on
;# it as long as it still resembles the original code.
;#
;#############################################################################

;# Get system-specific socket parameters, make assumptions if necessary.
$sockaddr_t = 'S n a4 x8';
eval "require 'sys/socket.ph'";
eval <<'END_SOCKET_DEFINITIONS' if $@;
  sub AF_INET           { 2; }
  sub SOCK_STREAM       { 1; }
  sub SOL_SOCKET        { 65535; }
  sub SO_REUSEADDR      { 4; }
END_SOCKET_DEFINITIONS

;# Seed the generation of names for file handles.
$latest_handle = 'sock0000000001';

sub open {
  local ($remote_host, $remote_port) = @_;
  if (!$remote_port) {
    $! = "bad arguments to sock'open()";
    return 0;
  }
  $sock = ++$latest_handle;

  ;# Look up the port if it was specified by name instead of by number.
  if ($remote_port =~ /\D/o) {
    ($name,$aliases,$remote_port) = getservbyname($remote_port,'tcp');
  }

  ;# Look up the address if it was specified by name instead of by number.
  if ($remote_host =~ /\D/o) {
    ($name,$aliases,$type,$len,$remote_addr) = gethostbyname($remote_host);
  } else {
    $remote_addr = $remote_host;
  }

  ;# Make the socket structures.
  $this = pack($sockaddr_t, &AF_INET, 0, "\0\0\0\0");
  $remote_sock = pack($sockaddr_t, &AF_INET, $remote_port, $remote_addr);

  ;# Make the socket filehandle.
  ($name,$aliases,$proto) = getprotobyname('tcp');
  socket($sock, &AF_INET, &SOCK_STREAM, $proto) || return 0;

  ;# Set up the port so it's freed as soon as we're done.
  setsockopt($sock, &SOL_SOCKET, &SO_REUSEADDR, 1);

  ;# Bind this socket to an address.
  bind($sock, $this) || return 0;

  ;# Call up the remote socket.
  connect($sock,$remote_sock) || return 0;

  $handles{$sock} = 1;
  $oldfh = select($sock); $| = 1; select($oldfh);
  return "sock'" . $sock;
}

sub close {
  local ($sock) = shift(@_) || return 0;
  shutdown ($sock, 2);
  delete $handles{$sock};
}

sub close_all {
  for $sock (keys %handles) {
    shutdown ($sock, 2);
    delete $handles{$sock};
  }
}


=====

--------------------------------------
Date: 9/19/94 6:41 PM
To: Eric Morgan
From: Jack Shirazi - BIU

> I have taken your perl script and saved it as test-socket.pl. Since I don't
> have a server running on port 13, and since I wanted to do a bit of
> diagnostics, I only slightly modified it as shown below:
> 
>     # diagnositic by Eric
>     print "address: $addr\n";

$addr is a 4 byte structure giving the ip address. It  can be
be looked at properly using join('.',unpack("C4",$addr)) to
give the dotted number address, i.e.
print "address:",join('.',unpack("C4",$addr)),"\n";

>     $server_sockaddress = pack($SOCKADDRESS_TEMPLATE,&AF_INET,13,$addr);
>     socket(S,&PF_INET,&SOCK_STREAM,0) || die "Couldn't create socket: $!\n";
>     
>     # diagnositic by Eric
>     print "server_sockaddress: $server_sockaddress\n";

$server_sockaddress is a C struct giving the two endpoints
of the connection. The pack just creates it as an internet socket
on port 13 using $addr as the rtemote ip address and no local address.
No point in unpacking it to have a look.

Given that you were printing out structires, there were probably
unprintable characters being prinetd, so the output was meaningless.
You might want to try again with the above print. It should print
out the dotted address (the same one you see when you try telnet).

> 1. When I use "localhost", my address and server_socketaddress are empty.
> 
> 2. When I use 'vega' my address is "*" with a linefeed, and my
> server_socketaddress is also "*" with a carriage-return/linefeed.
> 
> 3. When I use 'vega.lib.ncsu.edu' I get the same ouput as #2.
> 
> 4. When I use 'www.ncsu.edu', a  WWW server on campus, my output is just like
> #1. (This machine does not support a time service on port 13.)
> 
> 5. When I use 'rtfm.mit.edu' my address is 5, as well as my socket address.
> 
> In all cases, the connection is refused.
> 
> 6. If I put in 'morgan.lib.ncsu.edu' for my address then the script dies
> telling me the host name is invalid, and it is.
> 
> What sort of output should $addr have? Is it an IP number or a physical
> address? Based on my output, what do you suggest I do? What sort of value
> should "S", I never got any output from that either?

Anything that doesn't support the daytime service will refuse connection.
I would be surprised if they don't though. You can test it by te,telnetting
to port 13, as in 
telnet www.ncsu.edu 13
The daytime service on 13 just prints the date and time. Thats what should
have been read by trhe socket 'S'.

Try telnetting to rtfm on port 13.
telnet rtfm.mit.edu
If this succeeds, but the perl script fails, then there is something
wrong with your compilation of Perl (whoever installed it installed
it wrong). If the telnet fails, then its a LAN problem, or a badly
configured machine.


------------------ RFC822 Header Follows ------------------
Received: by library.lib.ncsu.edu with SMTP;19 Sep 1994 18:41:39 U
Received: from moose.lif.icnet.uk.biu
           by bison.lif.icnet.uk; Mon, 19 Sep 94 23:32:45 BST
Date: Mon, 19 Sep 94 23:32:45 BST
From: js@biu.icnet.uk (Jack Shirazi - BIU)
Full-Name: Jack Shirazi - BIU
Sender: js@biu.icnet.uk
To: eric_morgan.ncsu#u#library@library.lib.ncsu.edu
Subject: Re: output from test-socket.pl