Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!uunet!auspex!guy From: guy@auspex.auspex.com (Guy Harris) Newsgroups: comp.unix.wizards Subject: Re: Given a filedescriptor, how can I find source IP?? Message-ID: <6854@auspex.auspex.com> Date: 27 Mar 91 19:44:13 GMT References: <1991Mar22.201818.25515@msuinfo.cl.msu.edu> <1991Mar23.041824.3130@msuinfo.cl.msu.edu> Organization: Auspex Systems, Santa Clara Lines: 24 >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...).