3 #include <sys/socket.h>
6 #include <netinet/in.h>
10 int gethostbyname2_r(const char *name, int af,
11 struct hostent *h, char *buf, size_t buflen,
12 struct hostent **res, int *err)
14 struct addrinfo hint = {
15 .ai_family = af==AF_INET6 ? af : AF_INET,
16 .ai_flags = AI_CANONNAME
18 struct addrinfo *ai, *p;
26 i = (uintptr_t)buf & sizeof(char *)-1;
28 if (buflen < sizeof(char *)-i) {
32 buf += sizeof(char *)-i;
33 buflen -= sizeof(char *)-i;
36 getaddrinfo(name, 0, &hint, &ai);
37 switch (getaddrinfo(name, 0, &hint, &ai)) {
39 *err = HOST_NOT_FOUND;
55 h->h_length = af==AF_INET6 ? 16 : 4;
57 canon = ai->ai_canonname ? ai->ai_canonname : name;
58 need = 4*sizeof(char *);
59 for (i=0, p=ai; p; i++, p=p->ai_next)
60 need += sizeof(char *) + h->h_length;
61 need += strlen(name)+1;
62 need += strlen(canon)+1;
70 h->h_aliases = (void *)buf;
71 buf += 3*sizeof(char *);
72 h->h_addr_list = (void *)buf;
73 buf += (i+1)*sizeof(char *);
75 h->h_name = h->h_aliases[0] = buf;
76 strcpy(h->h_name, canon);
77 buf += strlen(h->h_name)+1;
79 if (strcmp(h->h_name, name)) {
80 h->h_aliases[1] = buf;
81 strcpy(h->h_aliases[1], name);
82 buf += strlen(h->h_aliases[1])+1;
83 } else h->h_aliases[1] = 0;
87 for (i=0, p=ai; p; i++, p=p->ai_next) {
88 h->h_addr_list[i] = (void *)buf;
90 memcpy(h->h_addr_list[i],
91 &((struct sockaddr_in *)p->ai_addr)->sin_addr,
94 h->h_addr_list[i] = 0;