| 1 | #define _GNU_SOURCE |
| 2 | |
| 3 | #include <sys/socket.h> |
| 4 | #include <netdb.h> |
| 5 | #include <string.h> |
| 6 | #include <netinet/in.h> |
| 7 | #include <errno.h> |
| 8 | #include <stdint.h> |
| 9 | #include "lookup.h" |
| 10 | |
| 11 | int gethostbyname2_r(const char *name, int af, |
| 12 | 	struct hostent *h, char *buf, size_t buflen, |
| 13 | 	struct hostent **res, int *err) |
| 14 | { |
| 15 | 	struct address addrs[MAXADDRS]; |
| 16 | 	char canon[256]; |
| 17 | 	int i, cnt; |
| 18 | 	size_t align, need; |
| 19 | |
| 20 | 	*res = 0; |
| 21 | 	cnt = __lookup_name(addrs, canon, name, af, AI_CANONNAME); |
| 22 | 	if (cnt<0) switch (cnt) { |
| 23 | 	case EAI_NONAME: |
| 24 | 		*err = HOST_NOT_FOUND; |
| 25 | 		return 0; |
| 26 | 	case EAI_NODATA: |
| 27 | 		*err = NO_DATA; |
| 28 | 		return 0; |
| 29 | 	case EAI_AGAIN: |
| 30 | 		*err = TRY_AGAIN; |
| 31 | 		return EAGAIN; |
| 32 | 	default: |
| 33 | 	case EAI_FAIL: |
| 34 | 		*err = NO_RECOVERY; |
| 35 | 		return EBADMSG; |
| 36 | 	case EAI_SYSTEM: |
| 37 | 		*err = NO_RECOVERY; |
| 38 | 		return errno; |
| 39 | 	} |
| 40 | |
| 41 | 	h->h_addrtype = af; |
| 42 | 	h->h_length = af==AF_INET6 ? 16 : 4; |
| 43 | |
| 44 | 	/* Align buffer */ |
| 45 | 	align = -(uintptr_t)buf & sizeof(char *)-1; |
| 46 | |
| 47 | 	need = 4*sizeof(char *); |
| 48 | 	need += (cnt + 1) * (sizeof(char *) + h->h_length); |
| 49 | 	need += strlen(name)+1; |
| 50 | 	need += strlen(canon)+1; |
| 51 | 	need += align; |
| 52 | |
| 53 | 	if (need > buflen) return ERANGE; |
| 54 | |
| 55 | 	buf += align; |
| 56 | 	h->h_aliases = (void *)buf; |
| 57 | 	buf += 3*sizeof(char *); |
| 58 | 	h->h_addr_list = (void *)buf; |
| 59 | 	buf += (cnt+1)*sizeof(char *); |
| 60 | |
| 61 | 	for (i=0; i<cnt; i++) { |
| 62 | 		h->h_addr_list[i] = (void *)buf; |
| 63 | 		buf += h->h_length; |
| 64 | 		memcpy(h->h_addr_list[i], addrs[i].addr, h->h_length); |
| 65 | 	} |
| 66 | 	h->h_addr_list[i] = 0; |
| 67 | |
| 68 | 	h->h_name = h->h_aliases[0] = buf; |
| 69 | 	strcpy(h->h_name, canon); |
| 70 | 	buf += strlen(h->h_name)+1; |
| 71 | |
| 72 | 	if (strcmp(h->h_name, name)) { |
| 73 | 		h->h_aliases[1] = buf; |
| 74 | 		strcpy(h->h_aliases[1], name); |
| 75 | 		buf += strlen(h->h_aliases[1])+1; |
| 76 | 	} else h->h_aliases[1] = 0; |
| 77 | |
| 78 | 	h->h_aliases[2] = 0; |
| 79 | |
| 80 | 	*res = h; |
| 81 | 	return 0; |
| 82 | } |