Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 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 <inttypes.h> |
| 9 | |
| 10 | int gethostbyname2_r(const char *name, int af, |
| 11 | struct hostent *h, char *buf, size_t buflen, |
| 12 | struct hostent **res, int *err) |
| 13 | { |
| 14 | struct addrinfo hint = { |
| 15 | .ai_family = af==AF_INET6 ? af : AF_INET, |
| 16 | .ai_flags = AI_CANONNAME |
| 17 | }; |
| 18 | struct addrinfo *ai, *p; |
| 19 | int i; |
| 20 | size_t need; |
| 21 | const char *canon; |
| 22 | |
| 23 | af = hint.ai_family; |
| 24 | |
| 25 | /* Align buffer */ |
| 26 | i = (uintptr_t)buf & sizeof(char *)-1; |
| 27 | if (i) { |
Rich Felker | 70b584b | 2013-02-02 01:31:10 -0500 | [diff] [blame^] | 28 | if (buflen < sizeof(char *)-i) return ERANGE; |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 29 | buf += sizeof(char *)-i; |
| 30 | buflen -= sizeof(char *)-i; |
| 31 | } |
| 32 | |
| 33 | getaddrinfo(name, 0, &hint, &ai); |
| 34 | switch (getaddrinfo(name, 0, &hint, &ai)) { |
| 35 | case EAI_NONAME: |
| 36 | *err = HOST_NOT_FOUND; |
Rich Felker | 70b584b | 2013-02-02 01:31:10 -0500 | [diff] [blame^] | 37 | return errno; |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 38 | case EAI_AGAIN: |
| 39 | *err = TRY_AGAIN; |
Rich Felker | 70b584b | 2013-02-02 01:31:10 -0500 | [diff] [blame^] | 40 | return errno; |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 41 | default: |
| 42 | case EAI_MEMORY: |
| 43 | case EAI_SYSTEM: |
| 44 | case EAI_FAIL: |
| 45 | *err = NO_RECOVERY; |
Rich Felker | 70b584b | 2013-02-02 01:31:10 -0500 | [diff] [blame^] | 46 | return errno; |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 47 | case 0: |
| 48 | break; |
| 49 | } |
| 50 | |
| 51 | h->h_addrtype = af; |
| 52 | h->h_length = af==AF_INET6 ? 16 : 4; |
| 53 | |
| 54 | canon = ai->ai_canonname ? ai->ai_canonname : name; |
| 55 | need = 4*sizeof(char *); |
| 56 | for (i=0, p=ai; p; i++, p=p->ai_next) |
| 57 | need += sizeof(char *) + h->h_length; |
| 58 | need += strlen(name)+1; |
| 59 | need += strlen(canon)+1; |
| 60 | |
| 61 | if (need > buflen) { |
| 62 | freeaddrinfo(ai); |
Rich Felker | 70b584b | 2013-02-02 01:31:10 -0500 | [diff] [blame^] | 63 | return ERANGE; |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | h->h_aliases = (void *)buf; |
| 67 | buf += 3*sizeof(char *); |
| 68 | h->h_addr_list = (void *)buf; |
| 69 | buf += (i+1)*sizeof(char *); |
| 70 | |
| 71 | h->h_name = h->h_aliases[0] = buf; |
| 72 | strcpy(h->h_name, canon); |
| 73 | buf += strlen(h->h_name)+1; |
| 74 | |
| 75 | if (strcmp(h->h_name, name)) { |
| 76 | h->h_aliases[1] = buf; |
| 77 | strcpy(h->h_aliases[1], name); |
| 78 | buf += strlen(h->h_aliases[1])+1; |
| 79 | } else h->h_aliases[1] = 0; |
| 80 | |
| 81 | h->h_aliases[2] = 0; |
| 82 | |
| 83 | for (i=0, p=ai; p; i++, p=p->ai_next) { |
| 84 | h->h_addr_list[i] = (void *)buf; |
| 85 | buf += h->h_length; |
| 86 | memcpy(h->h_addr_list[i], |
| 87 | &((struct sockaddr_in *)p->ai_addr)->sin_addr, |
| 88 | h->h_length); |
| 89 | } |
| 90 | h->h_addr_list[i] = 0; |
| 91 | |
| 92 | *res = h; |
| 93 | freeaddrinfo(ai); |
| 94 | return 0; |
| 95 | } |