bug-glibc
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

incorrect return value in gethostbyname_r() ?


From: Greg Herlein
Subject: incorrect return value in gethostbyname_r() ?
Date: Sat, 28 Oct 2000 08:05:10 -0700 (PDT)

The code below illustrates a problem I have found with
gethostbyname_r().  It is returning NSS_STATUS_NOTFOUND but does
in fact have an answer.

I am using SuSE 6.4, and I upgraded the shared libraries again
just be sure.  Here is some info:

address@hidden:~ > ldd getname
        libc.so.6 => /lib/libc.so.6 (0x40023000)
        /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)                

address@hidden:~ > gcc -v
Reading specs from /usr/lib/gcc-lib/i486-suse-linux/2.95.2/specs
gcc version 2.95.2 19991024 (release)


And, the code that generates the problem:

/* makefile
  getname: getname.c
        gcc -o getname getname.c -D_REENTRANT
*/
#include <unistd.h>
#include <stdio.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <alloca.h>
#include <string.h>
#include <nss.h>
/*----------------------------------------------------------------------*/
int
main(int argc, char* argv[]){

  struct hostent  he;
  struct hostent  *rhe;
  struct in_addr  sSrvAddr;
  int herr = 0,n = 0, tmpbuflen = 2048;
  char    *p, *pHost;
  char    szSrvAddr[128];
  char    szBuffer[2048];

  if(argc<2)
  {
    printf("Use:  %s <host>\n",argv[0]);
    exit(0);
  }
  pHost=argv[1];

  n = gethostbyname_r(pHost, &he,
                      szBuffer,tmpbuflen,&rhe,&herr);

  switch(n)
  {
    case NSS_STATUS_TRYAGAIN:
    case NSS_STATUS_UNAVAIL:
    default:
      printf("Error getting address\n");
      printf("nss_status: %d        herr: %d\n",n,herr);
      break;
    case NSS_STATUS_NOTFOUND:
      printf("NSS_STATUS_NOTFOUND...\n");
    case NSS_STATUS_SUCCESS:
    case NSS_STATUS_RETURN:
      sSrvAddr = *((struct in_addr*)rhe->h_addr);
      p = inet_ntoa(sSrvAddr);
      strncpy(szSrvAddr,p,sizeof(szSrvAddr));
      printf("gethostbyname_r: %s = %s\n",pHost,szSrvAddr);
      break;
  }
  return 0;
}                                                                      

/**************************************************************
Greg Herlein                                   address@hidden
Herlein Engineering                             www.herlein.com
***************************************************************/






reply via email to

[Prev in Thread] Current Thread [Next in Thread]