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