Given a filedescriptor, how can I find source IP??

Guy Harris guy at auspex.auspex.com
Thu Mar 28 05:44:13 AEST 1991


>Remember boys and girls, "getpeername()" only works on sockets.
>I guess I was not clear - the program does not open a socket -
>it inherits some filedescriptors (0 1 and 2).

You're still not entirely clear.  If the program inherits a socket on
those file descriptors, well, a socket is a socket, so "getpeername()"
should work even if the program itself didn't open it.

If, however, you mean you're running in a session initiated by "telnet"
- i.e., the file descriptors it inherited are for a pseudo-tty set up by
"in.telnetd" - then the easiest way to do this may not be to grovel
through the kernel, but to look up the "utmp" entry for your session and
get the host name from there. 

The advantage of this is that there's no groveling to be done (and said
groveling would be a bit complicated - you'd have to find the process
that had the *master* side of your pseudo-tty open, and then see what
sockets it had open).

The disadvantage of this is that, unless your Sun (not SUN) is running
SunOS/SVR4, the host name field in the "utmp" file is only 16 bytes
long, so the host name may be truncated (it's 257 bytes long - 256
characters plus a '\0' - in the SVR4 "utmpx" file; time for Berkeley to
play catch-up in this case...). 



More information about the Comp.unix.wizards mailing list