initial check-in, version 0.5.0
[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 <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) {
28                 if (buflen < sizeof(char *)-i) {
29                         errno = ERANGE;
30                         return -1;
31                 }
32                 buf += sizeof(char *)-i;
33                 buflen -= sizeof(char *)-i;
34         }
35
36         getaddrinfo(name, 0, &hint, &ai);
37         switch (getaddrinfo(name, 0, &hint, &ai)) {
38         case EAI_NONAME:
39                 *err = HOST_NOT_FOUND;
40                 return -1;
41         case EAI_AGAIN:
42                 *err = TRY_AGAIN;
43                 return -1;
44         default:
45         case EAI_MEMORY:
46         case EAI_SYSTEM:
47         case EAI_FAIL:
48                 *err = NO_RECOVERY;
49                 return -1;
50         case 0:
51                 break;
52         }
53
54         h->h_addrtype = af;
55         h->h_length = af==AF_INET6 ? 16 : 4;
56
57         canon = ai->ai_canonname ? ai->ai_canonname : name;
58         need = 4*sizeof(char *);
59         for (i=0, p=ai; p; i++, p=p->ai_next)
60                 need += sizeof(char *) + h->h_length;
61         need += strlen(name)+1;
62         need += strlen(canon)+1;
63
64         if (need > buflen) {
65                 freeaddrinfo(ai);
66                 errno = ERANGE;
67                 return -1;
68         }
69
70         h->h_aliases = (void *)buf;
71         buf += 3*sizeof(char *);
72         h->h_addr_list = (void *)buf;
73         buf += (i+1)*sizeof(char *);
74
75         h->h_name = h->h_aliases[0] = buf;
76         strcpy(h->h_name, canon);
77         buf += strlen(h->h_name)+1;
78
79         if (strcmp(h->h_name, name)) {
80                 h->h_aliases[1] = buf;
81                 strcpy(h->h_aliases[1], name);
82                 buf += strlen(h->h_aliases[1])+1;
83         } else h->h_aliases[1] = 0;
84
85         h->h_aliases[2] = 0;
86
87         for (i=0, p=ai; p; i++, p=p->ai_next) {
88                 h->h_addr_list[i] = (void *)buf;
89                 buf += h->h_length;
90                 memcpy(h->h_addr_list[i],
91                         &((struct sockaddr_in *)p->ai_addr)->sin_addr,
92                         h->h_length);
93         }
94         h->h_addr_list[i] = 0;
95
96         *res = h;
97         freeaddrinfo(ai);
98         return 0;
99 }