3 #include <sys/socket.h>
6 #include <netinet/in.h>
11 int gethostbyname2_r(const char *name, int af,
12 struct hostent *h, char *buf, size_t buflen,
13 struct hostent **res, int *err)
15 struct address addrs[MAXADDRS];
21 cnt = __lookup_name(addrs, canon, name, af, AI_CANONNAME);
22 if (cnt<0) switch (cnt) {
24 *err = HOST_NOT_FOUND;
42 h->h_length = af==AF_INET6 ? 16 : 4;
45 align = -(uintptr_t)buf & sizeof(char *)-1;
47 need = 4*sizeof(char *);
48 need += (cnt + 1) * (sizeof(char *) + h->h_length);
49 need += strlen(name)+1;
50 need += strlen(canon)+1;
53 if (need > buflen) return ERANGE;
56 h->h_aliases = (void *)buf;
57 buf += 3*sizeof(char *);
58 h->h_addr_list = (void *)buf;
59 buf += (cnt+1)*sizeof(char *);
61 h->h_name = h->h_aliases[0] = buf;
62 strcpy(h->h_name, canon);
63 buf += strlen(h->h_name)+1;
65 if (strcmp(h->h_name, name)) {
66 h->h_aliases[1] = buf;
67 strcpy(h->h_aliases[1], name);
68 buf += strlen(h->h_aliases[1])+1;
69 } else h->h_aliases[1] = 0;
73 for (i=0; i<cnt; i++) {
74 h->h_addr_list[i] = (void *)buf;
76 memcpy(h->h_addr_list[i], addrs[i].addr, h->h_length);
78 h->h_addr_list[i] = 0;