[comp.unix.wizards] Given a filedescriptor, how can I find source IP??

jap@convex.cl.msu.edu (Joe Porkka) (03/23/91)

jap@convex.cl.msu.edu (Joe Porkka) writes:

>Well, the subject says it all...

>On a SUN, I am trying to track down the address of the machine
>that telneted to me. The reason is that I want to be able
>to open another connection going back the other way.


>Any ideas???

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).

Sombody already suggested that I place a program between inetd
and telnetd, but I was hoping to not have to resort to such
yuck - I would rather not modify the system.

ed@mtxinu.COM (Ed Gould) (03/26/91)

>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).

A program can inherit sockets, just as it can inherit files.  "File"
descriptors can refer to either.  If you're asking about the child
of a telnet session - which has pseudo-terminals connected to 0,
1, and 2 - then you are out of luck.  In general, there is no way
to track backwards through the descriptor of a pty to its controlling
process and then through one of that processe's descriptors to the
foreign machine.

-- 
Ed Gould                    mt Xinu, 2560 Ninth St., Berkeley, CA  94710  USA
ed@mtxinu.COM		    +1 415 644 0146

"I'll fight them as a woman, not a lady.  I'll fight them as an engineer."

jap@convex.cl.msu.edu (Joe Porkka) (03/27/91)

ed@mtxinu.COM (Ed Gould) writes:

>>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).

>A program can inherit sockets, just as it can inherit files.  "File"
>descriptors can refer to either.  If you're asking about the child
>of a telnet session - which has pseudo-terminals connected to 0,
>1, and 2 - then you are out of luck.  In general, there is no way
Yes, it would seem to not be possible. It *oughta* be!

Well, we have come to a pretty workable solution to the problem.

It turns out that in SunOS3.x, inetd by default passed the ip address
as a command line arg to in.telnetd. 

In SunOS4.1 they "fixed" it so that it does not. However, you can
turn that feature back on by editing inetd.conf

So, with that feature back on, I just gotta look at my parents (in.telnetd)
command line args to get the IP address.

guy@auspex.auspex.com (Guy Harris) (03/28/91)

>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...). 

guy@auspex.auspex.com (Guy Harris) (03/31/91)

>It turns out that in SunOS3.x, inetd by default passed the ip address
>as a command line arg to in.telnetd. 
>
>In SunOS4.1 they "fixed" it so that it does not.

More correctly, in SunOS 4.*0* they picked up the 4.3BSD "inetd", which
handed you a socket for the connection on file descriptors 0, 1, and 2,
and didn't hand you the IP address as a command-line argument.