getting IP address of remote host from socket (stdin)

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Ordinarily I would use the getpeername and getsockname functions to get
the IP address associated with each end of a socket. However, this
appears not to work for a script which is started from xinetd[1].

xinetd connects the socket over which the connection arrives to stdin
and stdout of the spawned process so when you read from standard input
you are in fact reading from the network.

In a C application started in this way, you can retrieve the IP
addresses associated with the 'socket' as follows (pseudocode only):

    getpeername(fileno(stdin), (struct sockaddr *) &sa, &sz);

even though 'stdin' isn't a real socket.

However, doing a similar thing in Perl:

    $sockaddr = getpeername(fileno(STDIN));  # 0
    ($port, $addr) = sockaddr_in($sockaddr);

gives the following error:

getpeername() on unopened socket 0 at line 258.

Use of uninitialized value in subroutine entry at
/usr/local/lib/perl5/5.6.1/mach/ line 312.
Bad arg length for Socket::unpack_sockaddr_in, length is 0, should be 16
at /usr/local/lib/perl5/5.6.1/mach/ line 312.

Does anyone have any advice on how I might be able to get the IPs
associated with a socket, when that socket is in fact STDIN/STDOUT as
presented by xinetd? I am using Perl 5.6.1.

Any help would be greatly appreciated.



Site Timeline