Blame nss/bug-erange.c

Packit 6c4009
/* Test case for gethostbyname_r bug when buffer expansion required.  */
Packit 6c4009
Packit 6c4009
#include <netdb.h>
Packit 6c4009
#include <arpa/inet.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main (void)
Packit 6c4009
{
Packit 6c4009
  const char *host = "www.gnu.org";
Packit 6c4009
Packit 6c4009
  /* This code approximates the example code in the library manual.  */
Packit 6c4009
Packit 6c4009
  struct hostent hostbuf, *hp;
Packit 6c4009
  size_t hstbuflen;
Packit 6c4009
  char *tmphstbuf;
Packit 6c4009
  int res;
Packit 6c4009
  int herr;
Packit 6c4009
Packit 6c4009
  hstbuflen = 16;		/* Make it way small to ensure ERANGE.  */
Packit 6c4009
  /* Allocate buffer, remember to free it to avoid memory leakage.  */
Packit 6c4009
  tmphstbuf = malloc (hstbuflen);
Packit 6c4009
Packit 6c4009
  while ((res = gethostbyname_r (host, &hostbuf, tmphstbuf, hstbuflen,
Packit 6c4009
                                 &hp, &herr)) == ERANGE)
Packit 6c4009
    {
Packit 6c4009
      /* Enlarge the buffer.  */
Packit 6c4009
      hstbuflen *= 2;
Packit 6c4009
      tmphstbuf = realloc (tmphstbuf, hstbuflen);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (res != 0 || hp == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("gethostbyname_r failed: %s (errno: %m)\n", strerror (res));
Packit 6c4009
Packit 6c4009
      if (access ("/etc/resolv.conf", R_OK))
Packit 6c4009
	{
Packit 6c4009
	  puts ("DNS probably not set up");
Packit 6c4009
	  return 0;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  printf ("Got: %s %s\n", hp->h_name,
Packit 6c4009
	  inet_ntoa (*(struct in_addr *) hp->h_addr));
Packit 6c4009
  return 0;
Packit 6c4009
}