summaryrefslogtreecommitdiffstats
path: root/mdk-stage1/dietlibc/libcruft/gethostbyname2.c
blob: 3d829661602b3611c088dfedac1febd43981400f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/poll.h>
#include <unistd.h>
#include <errno.h>

extern int h_errno;

struct hostent* gethostbyname2(const char *host,int AF)
{
  static struct hostent hostbuf;
  struct hostent *hp;
  size_t hstbuflen;
  char *tmphstbuf;
  int res;
  int herr;

  hstbuflen = 1024;
  /* Allocate buffer, remember to free it to avoid a memory leakage.  */
  tmphstbuf = malloc (hstbuflen);

  while ((res = gethostbyname2_r (host, AF,&hostbuf, tmphstbuf, hstbuflen,
				  &hp, &herr)) == ERANGE)
    {
      /* Enlarge the buffer.  */
      hstbuflen *= 2;
      tmphstbuf = realloc (tmphstbuf, hstbuflen);
    }
  /*  Check for errors.  */
  if (res || hp == NULL)
    return NULL;
  return hp;
}