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