3 #include <sys/socket.h>
6 #include <netinet/in.h>
10 int gethostbyaddr_r(const void *a, socklen_t l, int af,
11 struct hostent *h, char *buf, size_t buflen,
12 struct hostent **res, int *err)
15 struct sockaddr_in sin;
16 struct sockaddr_in6 sin6;
17 } sa = { .sin.sin_family = af };
18 socklen_t sl = af==AF_INET6 ? sizeof sa.sin6 : sizeof sa.sin;
21 /* Load address argument into sockaddr structure */
22 if (af==AF_INET6 && l==16) memcpy(&sa.sin6.sin6_addr, a, 16);
23 else if (af==AF_INET && l==4) memcpy(&sa.sin.sin_addr, a, 4);
29 /* Align buffer and check for space for pointers and ip address */
30 i = (uintptr_t)buf & sizeof(char *)-1;
31 if (!i) i = sizeof(char *);
32 if (buflen <= 5*sizeof(char *)-i + l) {
36 buf += sizeof(char *)-i;
37 buflen -= 5*sizeof(char *)-i + l;
39 h->h_addr_list = (void *)buf;
40 buf += 2*sizeof(char *);
41 h->h_aliases = (void *)buf;
42 buf += 2*sizeof(char *);
44 h->h_addr_list[0] = buf;
45 memcpy(h->h_addr_list[0], a, l);
47 h->h_addr_list[1] = 0;
48 h->h_aliases[0] = buf;
51 switch (getnameinfo((void *)&sa, sl, buf, buflen, 0, 0, 0)) {
68 h->h_name = h->h_aliases[0];