838621668a7bfc241b7c2b1852211a07a6114dce
[musl] / src / network / getaddrinfo.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <netdb.h>
4 #include <netinet/in.h>
5 #include <sys/socket.h>
6 #include <unistd.h>
7 #include <string.h>
8 #include <ctype.h>
9 #include "__dns.h"
10 #include "stdio_impl.h"
11
12 static int is_valid(const char *host)
13 {
14         const unsigned char *s;
15         if (strlen(host)-1 > 254 || mbstowcs(0, host, 0) > 255) return 0;
16         for (s=(void *)host; *s>=0x80 || *s=='.' || *s=='-' || isalnum(*s); s++);
17         return !*s;
18 }
19
20 #if 0
21 static int have_af(int family)
22 {
23         struct sockaddr_in6 sin6 = { .sin6_family = family };
24         socklen_t sl = family == AF_INET
25                 ? sizeof(struct sockaddr_in)
26                 : sizeof(struct sockaddr_in6);
27         int sock = socket(family, SOCK_STREAM, 0);
28         int have = !bind(sock, (void *)&sin6, sl);
29         close(sock);
30         return have;
31 }
32 #endif
33
34 #include <stdlib.h>
35 #include <netdb.h>
36
37 union sa {
38         struct sockaddr_in sin;
39         struct sockaddr_in6 sin6;
40 };
41
42 struct aibuf {
43         struct addrinfo ai;
44         union sa sa;
45 };
46
47 /* Extra slots needed for storing canonical name */
48 #define EXTRA ((256+sizeof(struct aibuf)-1)/sizeof(struct aibuf))
49
50 int getaddrinfo(const char *host, const char *serv, const struct addrinfo *hint, struct addrinfo **res)
51 {
52         int flags = hint ? hint->ai_flags : 0;
53         int family = hint ? hint->ai_family : AF_UNSPEC;
54         int type = hint ? hint->ai_socktype : 0;
55         int proto = hint ? hint->ai_protocol : 0;
56         unsigned long port = 0;
57         struct aibuf *buf;
58         union sa sa = {{0}};
59         unsigned char reply[1024];
60         int i, j;
61         char line[512];
62         FILE *f, _f;
63         unsigned char _buf[1024];
64         char *z;
65         int result;
66         int cnt;
67
68         if (host && strlen(host)>255) return EAI_NONAME;
69         if (serv && strlen(serv)>32) return EAI_SERVICE;
70
71         if (type && !proto)
72                 proto = type==SOCK_DGRAM ? IPPROTO_UDP : IPPROTO_TCP;
73         if (!type && proto)
74                 type = proto==IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
75
76         if (serv) {
77                 if (!*serv) return EAI_SERVICE;
78                 port = strtoul(serv, &z, 10);
79                 if (!*z && port > 65535) return EAI_SERVICE;
80                 if (!port) {
81                         size_t servlen = strlen(serv);
82                         char *end = line;
83
84                         if (flags & AI_NUMERICSERV) return EAI_SERVICE;
85
86                         f = __fopen_rb_ca("/etc/services", &_f, _buf, sizeof _buf);
87                         if (!f) return EAI_SERVICE;
88                         while (fgets(line, sizeof line, f)) {
89                                 if (strncmp(line, serv, servlen) || !isspace(line[servlen]))
90                                         continue;
91                                 port = strtoul(line+servlen, &end, 10);
92                                 if (strncmp(end, proto==IPPROTO_UDP ? "/udp" : "/tcp", 4))
93                                         continue;
94                                 break;
95                         }
96                         __fclose_ca(f);
97                         if (feof(f)) return EAI_SERVICE;
98                 }
99                 port = htons(port);
100         }
101
102         if (!host) {
103                 if (family == AF_UNSPEC) family = AF_INET;
104                 buf = calloc(sizeof *buf, 1+EXTRA);
105                 if (!buf) return EAI_MEMORY;
106                 buf->ai.ai_protocol = proto;
107                 buf->ai.ai_socktype = type;
108                 buf->ai.ai_addr = (void *)&buf->sa;
109                 buf->ai.ai_addrlen = family==AF_INET6 ? sizeof sa.sin6 : sizeof sa.sin;
110                 buf->ai.ai_family = family;
111                 buf->sa.sin.sin_family = family;
112                 buf->sa.sin.sin_port = port;
113                 if (!(flags & AI_PASSIVE)) {
114                         if (family == AF_INET) {
115                                 0[(uint8_t*)&buf->sa.sin.sin_addr.s_addr]=127;
116                                 3[(uint8_t*)&buf->sa.sin.sin_addr.s_addr]=1;
117                         } else buf[0].sa.sin6.sin6_addr.s6_addr[15] = 1;
118                 }
119                 *res = &buf->ai;
120                 return 0;
121         }
122
123         if (!*host) return EAI_NONAME;
124
125         /* Try as a numeric address */
126         if (__ipparse(&sa, family, host) >= 0) {
127                 buf = calloc(sizeof *buf, 1+EXTRA);
128                 if (!buf) return EAI_MEMORY;
129                 family = sa.sin.sin_family;
130                 buf->ai.ai_protocol = proto;
131                 buf->ai.ai_socktype = type;
132                 buf->ai.ai_addr = (void *)&buf->sa;
133                 buf->ai.ai_addrlen = family==AF_INET6 ? sizeof sa.sin6 : sizeof sa.sin;
134                 buf->ai.ai_family = family;
135                 buf->ai.ai_canonname = (char *)host;
136                 buf->sa = sa;
137                 buf->sa.sin.sin_port = port;
138                 *res = &buf->ai;
139                 return 0;
140         }
141
142         if (flags & AI_NUMERICHOST) return EAI_NONAME;
143
144         f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf);
145         if (f) while (fgets(line, sizeof line, f)) {
146                 char *p;
147                 size_t l = strlen(host);
148
149                 if ((p=strchr(line, '#'))) *p++='\n', *p=0;
150                 for(p=line+1; (p=strstr(p, host)) &&
151                         (!isspace(p[-1]) || !isspace(p[l])); p++);
152                 if (!p) continue;
153                 __fclose_ca(f);
154
155                 /* Isolate IP address to parse */
156                 for (p=line; *p && !isspace(*p); p++);
157                 *p++ = 0;
158                 if (__ipparse(&sa, family, line) < 0) return EAI_NONAME;
159
160                 /* Allocate and fill result buffer */
161                 buf = calloc(sizeof *buf, 1+EXTRA);
162                 if (!buf) return EAI_MEMORY;
163                 family = sa.sin.sin_family;
164                 buf->ai.ai_protocol = proto;
165                 buf->ai.ai_socktype = type;
166                 buf->ai.ai_addr = (void *)&buf->sa;
167                 buf->ai.ai_addrlen = family==AF_INET6 ? sizeof sa.sin6 : sizeof sa.sin;
168                 buf->ai.ai_family = family;
169                 buf->sa = sa;
170                 buf->sa.sin.sin_port = port;
171
172                 /* Extract first name as canonical name */
173                 for (; *p && isspace(*p); p++);
174                 buf->ai.ai_canonname = (void *)(buf+1);
175                 snprintf(buf->ai.ai_canonname, 256, "%s", p);
176                 for (p=buf->ai.ai_canonname; *p && !isspace(*p); p++);
177                 *p = 0;
178                 if (!is_valid(buf->ai.ai_canonname))
179                         buf->ai.ai_canonname = 0;
180
181                 *res = &buf->ai;
182                 return 0;
183         }
184         if (f) __fclose_ca(f);
185
186 #if 0
187         f = __fopen_rb_ca("/etc/resolv.conf", &_f, _buf, sizeof _buf);
188         if (f) while (fgets(line, sizeof line, f)) {
189                 if (!isspace(line[10]) || (strncmp(line, "search", 6)
190                         && strncmp(line, "domain", 6))) continue;
191         }
192         if (f) __fclose_ca(f);
193 #endif
194
195         /* Perform one or more DNS queries for host */
196         memset(reply, 0, sizeof reply);
197         result = __dns_query(reply, host, family, 0);
198         if (result < 0) return result;
199
200         cnt = __dns_count_addrs(reply, result);
201         if (cnt <= 0) return EAI_NONAME;
202
203         buf = calloc(sizeof *buf, cnt+EXTRA);
204         if (!buf) return EAI_MEMORY;
205
206         i = 0;
207         if (family != AF_INET6) {
208                 j = __dns_get_rr(&buf[i].sa.sin.sin_addr, sizeof *buf, 4, cnt-i, reply, RR_A, 0);
209                 while (j--) buf[i++].sa.sin.sin_family = AF_INET;
210         }
211         if (family != AF_INET) {
212                 j = __dns_get_rr(&buf[i].sa.sin6.sin6_addr, sizeof *buf, 16, cnt-i, reply, RR_AAAA, 0);
213                 while (j--) buf[i++].sa.sin.sin_family = AF_INET6;
214         }
215         if (result>1) {
216                 j = __dns_get_rr(&buf[i].sa.sin.sin_addr, sizeof *buf, 4, cnt-i, reply+512, RR_A, 0);
217                 while (j--) buf[i++].sa.sin.sin_family = AF_INET;
218                 j = __dns_get_rr(&buf[i].sa.sin6.sin6_addr, sizeof *buf, 16, cnt-i, reply+512, RR_AAAA, 0);
219                 while (j--) buf[i++].sa.sin.sin_family = AF_INET6;
220         }
221
222         if (__dns_get_rr((void *)&buf[cnt], 0, 256, 1, reply, RR_CNAME, 1) < 0)
223                 strcpy((void *)&buf[cnt], host);
224
225         for (i=0; i<cnt; i++) {
226                 buf[i].ai.ai_protocol = proto;
227                 buf[i].ai.ai_socktype = type;
228                 buf[i].ai.ai_addr = (void *)&buf[i].sa;
229                 buf[i].ai.ai_addrlen = buf[i].sa.sin.sin_family==AF_INET6
230                         ? sizeof sa.sin6 : sizeof sa.sin;
231                 buf[i].ai.ai_family = buf[i].sa.sin.sin_family;
232                 buf[i].sa.sin.sin_port = port;
233                 buf[i].ai.ai_next = &buf[i+1].ai;
234                 buf[i].ai.ai_canonname = (void *)&buf[cnt];
235         }
236         buf[cnt-1].ai.ai_next = 0;
237         *res = &buf->ai;
238
239         return 0;
240 }