dynamically allocate storage for gethostby* buffers
[musl] / src / network / gethostbyaddr.c
1 #define _GNU_SOURCE
2
3 #include <netdb.h>
4 #include <string.h>
5 #include <netinet/in.h>
6 #include <errno.h>
7 #include <stdlib.h>
8
9 struct hostent *gethostbyaddr(const void *a, socklen_t l, int af)
10 {
11         static struct hostent *h;
12         size_t size = 63;
13         struct hostent *res;
14         int err;
15         do {
16                 free(h);
17                 h = malloc(size+=size+1);
18                 if (!h) {
19                         h_errno = NO_RECOVERY;
20                         return 0;
21                 }
22                 err = gethostbyaddr_r(a, l, af, h,
23                         (void *)(h+1), size-sizeof *h, &res, &h_errno);
24         } while (err == ERANGE);
25         return err ? 0 : h;
26 }