Getpeername function fails

Getpeername function fails

Post by 0000-Admin(00 » Fri, 18 Apr 1997 04:00:00



Hi, all.
I'm a relative novice when it comes to network programming, and I am
attempting to program a client/server app in C.  The problem I am having is
when the server calls getpeername to find out the hostname of the client.

Every time I call getpeername, it returns a "bad file number" error.  Here is
how I am calling it:

-----------------------------------------------------------------------
  struct sockaddr peername;
  int namelen;
  struct hostent *host, *gethostbyaddr();

  /* find out who is connected to us */
  namelen = sizeof(peername);
  if(getpeername(stdin, &peername, &namelen) == -1) {

    /* error getting peername */
    fprintf(logfile,"error [getpeername: %s]", strerror(errno));

  }
  else {  /* Got peername */

    fprintf(logfile,"[getpeername: %s]", peername.sa_data);

    host = gethostbyaddr(peername.sa_data,sizeof(peername.sa_data), AF_INET);
    if(host == NULL) {
      fprintf(logfile,"error [gethostbyaddr: %s]", strerror(errno));
    }
    else {
      fprintf(logfile,"[gethostbyaddr: %s]", host->h_name);
    }
  }
--------------------------------------------------------------------------

It is my understanding that, since I am using inetd to spawn this process,
the input and output sockets are provided to me in the form of standard
input and standard output.  Thus, I use stdin as the socket parameter in the
call to getpeername.

Does anybody know what I am doing wrong?  Any help would be appreciated.
Thanks,
-----------------------------------------------------------------------------

TACTech, Inc.                                    Voice: (714) 974-7676 ext.15
Yorba Linda, CA

 
 
 

Getpeername function fails

Post by Andrew Giert » Sat, 19 Apr 1997 04:00:00


 sharding> Every time I call getpeername, it returns a "bad file
 sharding> number" error.  Here is how I am calling it:

 sharding> -----------------------------------------------------------------
 sharding>   struct sockaddr peername;
 sharding>   int namelen;
 sharding>   struct hostent *host, *gethostbyaddr();

 sharding>   /* find out who is connected to us */
 sharding>   namelen = sizeof(peername);
 sharding>   if(getpeername(stdin, &peername, &namelen) == -1) {

That first parameter should be STDIN_FILENO, or fileno(stdin), not stdin.

stdin is a FILE*, not a file descriptor, and getpeername needs an FD.

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>

 
 
 

Getpeername function fails

Post by Bill Pa » Sat, 19 Apr 1997 04:00:00


Daring to challenge the will of the almighty Leviam00se,

: Hi, all.
: I'm a relative novice when it comes to network programming, and I am
: attempting to program a client/server app in C.  The problem I am having is
: when the server calls getpeername to find out the hostname of the client.

: Every time I call getpeername, it returns a "bad file number" error.  Here is
: how I am calling it:

: -----------------------------------------------------------------------
:   struct sockaddr peername;
:   int namelen;
:   struct hostent *host, *gethostbyaddr();

:   /* find out who is connected to us */
:   namelen = sizeof(peername);
:   if(getpeername(stdin, &peername, &namelen) == -1) {
                   ^^^^^
Err, no. Getpeername(2) takes a socket _descriptor_, which is an int.

: It is my understanding that, since I am using inetd to spawn this process,
: the input and output sockets are provided to me in the form of standard
: input and standard output.  Thus, I use stdin as the socket parameter in the
: call to getpeername.

Stdin is a stdio stream (FILE *). Stdin does have a descriptor associated
with it, but getpeername() won't find it for you. This would work:

   if(getpeername(fileno(stdin), &peername, &namelen) == -1) {

Try 'man fileno' for more information; fileno() returns the file descriptor
associated with a given stdio stream, which is what you want to make
getpeername() happy.

-Bill

--
=============================================================================
-Bill Paul            (212) 854-6020 | System Manager, Master of Unix-Fu


=============================================================================
  "Now, that's "Open" as used in the sentence "Open your wallet", right?"
=============================================================================

 
 
 

Getpeername function fails

Post by Vidar Hokst » Sat, 19 Apr 1997 04:00:00




[...]

Quote:> Every time I call getpeername, it returns a "bad file number" error.  Here is
> how I am calling it:

> -----------------------------------------------------------------------
>   struct sockaddr peername;
>   int namelen;
>   struct hostent *host, *gethostbyaddr();

>   /* find out who is connected to us */
>   namelen = sizeof(peername);
>   if(getpeername(stdin, &peername, &namelen) == -1) {

                   ^^^^^
The above is wrong. getpeername() take a filedescriptor, not a pointer to
FILE, as it's first argument. replace stdin above with "fileno(stdin)"
(or "0").

--


 
 
 

1. URGENT - sshd getpeername failed: Bad file descriptor

The problem has been descripted in below...
1/ I have a redhat linux 6.1 server with sshd and ssh installed.
2/ A new window 98 with ssh client contact the redhat server and login
as "root".
3/ The window98 received a new key.
4/ Inputed two times incorrect root password.
5/ Inputed a correct root password at third time.
6/ Login as root from window 98
7/ Everything works fine, memory usage ok, cpu usage ok.
8/ But an error appear in the /var/log/message file.

Sep  1 21:52:44 solar sshd[28003]: log: Closing connection to
163.70.127.240
Sep  1 21:52:47 solar sshd[28502]: log: Connection from 163.70.127.240
port 1425
Sep  1 21:52:47 solar sshd[28502]: log: Could not reverse map address
163.70.127.240.
Sep  1 21:52:47 solar sshd[28502]: error: getpeername failed: Bad file
descriptor
Sep  1 21:52:48 solar sshd[28502]: log: Password authentication for
root accepted.
Sep  1 21:52:48 solar sshd[28502]: log: ROOT LOGIN as 'root' from
163.70.127.240

9/ Tried to login again from the window98, same error appears again.
10/ Tried to login from another window95, same error appears again.

Please help, what is the getpeername failed: Bad file descriptor?
How to fix this error?
Do I have to fix this error or just leave it?

Thanks in advance...
Regards,
Johnny

Sent via Deja.com http://www.deja.com/
Before you buy.

2. PLIP on a laptop???

3. gethostbyaddr & getpeername FAILS ?

4. PD Program to send remote requests

5. URGENT - sshd getpeername failed: Bad file descriptor

6. IDE, ATA, ATAPI

7. Apache fails on getpeername and getsockname

8. Ftp to remote.

9. function, function...who's got the function?

10. Ftape compiles fine, fails to function correctly.

11. NFS server fails: [nfssvc: Function not implemented]

12. Fork function failed

13. CGI C set env var and call a c external function failed on UNIX