aa8b0a9ee5031bf67e6259ca5921ca61f957a24d
[musl] / src / network / gethostbyname2_r.c
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         cnt = __lookup_name(addrs, canon, name, af, AI_CANONNAME);
21         if (cnt<0) switch (cnt) {
22         case EAI_NONAME:
23                 *err = HOST_NOT_FOUND;
24                 return ENOENT;
25         case EAI_AGAIN:
26                 *err = TRY_AGAIN;
27                 return EAGAIN;
28         default:
29         case EAI_FAIL:
30                 *err = NO_RECOVERY;
31                 return EBADMSG;
32         case EAI_MEMORY:
33         case EAI_SYSTEM:
34                 *err = NO_RECOVERY;
35                 return errno;
36         case 0:
37                 break;
38         }
39
40         h->h_addrtype = af;
41         h->h_length = af==AF_INET6 ? 16 : 4;
42
43         /* Align buffer */
44         align = -(uintptr_t)buf & sizeof(char *)-1;
45
46         need = 4*sizeof(char *);
47         need += (cnt + 1) * (sizeof(char *) + h->h_length);
48         need += strlen(name)+1;
49         need += strlen(canon)+1;
50         need += align;
51
52         if (need > buflen) return ERANGE;
53
54         buf += align;
55         h->h_aliases = (void *)buf;
56         buf += 3*sizeof(char *);
57         h->h_addr_list = (void *)buf;
58         buf += (cnt+1)*sizeof(char *);
59
60         h->h_name = h->h_aliases[0] = buf;
61         strcpy(h->h_name, canon);
62         buf += strlen(h->h_name)+1;
63
64         if (strcmp(h->h_name, name)) {
65                 h->h_aliases[1] = buf;
66                 strcpy(h->h_aliases[1], name);
67                 buf += strlen(h->h_aliases[1])+1;
68         } else h->h_aliases[1] = 0;
69
70         h->h_aliases[2] = 0;
71
72         for (i=0; i<cnt; i++) {
73                 h->h_addr_list[i] = (void *)buf;
74                 buf += h->h_length;
75                 memcpy(h->h_addr_list[i], addrs[i].addr, h->h_length);
76         }
77         h->h_addr_list[i] = 0;
78
79         *res = h;
80         return 0;
81 }