switch standard resolver functions to use the new dns backend
[musl] / src / network / lookup_name.c
1 #include <sys/socket.h>
2 #include <netinet/in.h>
3 #include <netdb.h>
4 #include <arpa/inet.h>
5 #include <ctype.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <fcntl.h>
9 #include "lookup.h"
10 #include "stdio_impl.h"
11 #include "syscall.h"
12
13 static int is_valid_hostname(const char *host)
14 {
15         const unsigned char *s;
16         if (strnlen(host, 256)-1 > 254 || mbstowcs(0, host, 0) > 255) return 0;
17         for (s=(void *)host; *s>=0x80 || *s=='.' || *s=='-' || isalnum(*s); s++);
18         return !*s;
19 }
20
21 static int name_from_null(struct address buf[static 2], const char *name, int family, int flags)
22 {
23         int cnt = 0;
24         if (name) return 0;
25         if (flags & AI_PASSIVE) {
26                 if (family != AF_INET6)
27                         buf[cnt++] = (struct address){ .family = AF_INET };
28                 if (family != AF_INET)
29                         buf[cnt++] = (struct address){ .family = AF_INET6 };
30         } else {
31                 if (family != AF_INET6)
32                         buf[cnt++] = (struct address){ .family = AF_INET, .addr = { 127,0,0,1 } };
33                 if (family != AF_INET)
34                         buf[cnt++] = (struct address){ .family = AF_INET6, .addr = { [15] = 1 } };
35         }
36         return cnt;
37 }
38
39 static int name_from_numeric(struct address buf[static 1], const char *name, int family)
40 {
41         struct in_addr a4;
42         struct in6_addr a6;
43         if (family != AF_INET6 && inet_aton(name, &a4)>0) {
44                 memcpy(&buf[0].addr, &a4, sizeof a4);
45                 buf[0].family = AF_INET;
46                 return 1;
47         }
48         if (family != AF_INET && inet_pton(AF_INET6, name, &a6)>0) {
49                 memcpy(&buf[0].addr, &a6, sizeof a6);
50                 buf[0].family = AF_INET6;
51                 return 1;
52         }
53         return 0;
54 }
55
56 static int name_from_hosts(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family)
57 {
58         char line[512];
59         size_t l = strlen(name);
60         int cnt = 0;
61         unsigned char _buf[1032];
62         FILE _f, *f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf);
63         if (!f) return 0;
64         while (fgets(line, sizeof line, f) && cnt < MAXADDRS) {
65                 char *p, *z;
66
67                 if ((p=strchr(line, '#'))) *p++='\n', *p=0;
68                 for(p=line+1; (p=strstr(p, name)) &&
69                         (!isspace(p[-1]) || !isspace(p[l])); p++);
70                 if (!p) continue;
71
72                 /* Isolate IP address to parse */
73                 for (p=line; *p && !isspace(*p); p++);
74                 *p++ = 0;
75                 if (name_from_numeric(buf+cnt, line, family))
76                         cnt++;
77
78                 /* Extract first name as canonical name */
79                 for (; *p && isspace(*p); p++);
80                 for (z=p; *z && !isspace(*z); z++);
81                 *z = 0;
82                 if (is_valid_hostname(p)) memcpy(canon, p, z-p+1);
83         }
84         __fclose_ca(f);
85         return cnt;
86 }
87
88 struct dpc_ctx {
89         struct address *addrs;
90         char *canon;
91         int cnt;
92 };
93
94 int __dns_parse(const unsigned char *, int, int (*)(void *, int, const void *, int, const void *), void *);
95 int __dn_expand(const unsigned char *, const unsigned char *, const unsigned char *, char *, int);
96 int __res_mkquery(int, const char *, int, int, const unsigned char *, int, const unsigned char*, unsigned char *, int);
97 int __res_msend(int, const unsigned char *const *, const int *, unsigned char *const *, int *, int);
98
99 #define RR_A 1
100 #define RR_CNAME 5
101 #define RR_AAAA 28
102
103 static int dns_parse_callback(void *c, int rr, const void *data, int len, const void *packet)
104 {
105         char tmp[256];
106         struct dpc_ctx *ctx = c;
107         switch (rr) {
108         case RR_A:
109                 if (len != 4) return -1;
110                 ctx->addrs[ctx->cnt].family = AF_INET;
111                 memcpy(ctx->addrs[ctx->cnt++].addr, data, 4);
112                 break;
113         case RR_AAAA:
114                 if (len != 16) return -1;
115                 ctx->addrs[ctx->cnt].family = AF_INET6;
116                 memcpy(ctx->addrs[ctx->cnt++].addr, data, 16);
117                 break;
118         case RR_CNAME:
119                 if (__dn_expand(packet, (const unsigned char *)packet + 512,
120                     data, tmp, sizeof tmp) > 0 && is_valid_hostname(tmp))
121                         strcpy(ctx->canon, tmp);
122                 break;
123         }
124         return 0;
125 }
126
127 static int name_from_dns(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family)
128 {
129         unsigned char qbuf[2][280], abuf[2][512];
130         const unsigned char *qp[2] = { qbuf[0], qbuf[1] };
131         unsigned char *ap[2] = { abuf[0], abuf[1] };
132         int qlens[2], alens[2];
133         int i, nq = 0;
134         struct dpc_ctx ctx = { .addrs = buf, .canon = canon };
135
136         if (family != AF_INET6) {
137                 qlens[nq] = __res_mkquery(0, name, 1, RR_A, 0, 0, 0,
138                         qbuf[nq], sizeof *qbuf);
139                 nq++;
140         }
141         if (family != AF_INET) {
142                 qlens[nq] = __res_mkquery(0, name, 1, RR_AAAA, 0, 0, 0,
143                         qbuf[nq], sizeof *qbuf);
144                 nq++;
145         }
146
147         if (__res_msend(nq, qp, qlens, ap, alens, sizeof *abuf) < 0) return EAI_SYSTEM;
148
149         for (i=0; i<nq; i++)
150                 __dns_parse(abuf[i], alens[i], dns_parse_callback, &ctx);
151
152         return ctx.cnt;
153 }
154
155 int __lookup_name(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family, int flags)
156 {
157         int cnt = 0, i, j;
158
159         *canon = 0;
160         if (name) {
161                 size_t l;
162                 if ((l = strnlen(name, 256))-1 > 254)
163                         return EAI_NONAME;
164                 memcpy(canon, name, l+1);
165         }
166
167         /* Procedurally, a request for v6 addresses with the v4-mapped
168          * flag set is like a request for unspecified family, followed
169          * by filtering of the results. */
170         if (flags & AI_V4MAPPED) {
171                 if (family == AF_INET6) family = AF_UNSPEC;
172                 else flags -= AI_V4MAPPED;
173         }
174
175         /* Try each backend until there's at least one result. */
176         cnt = name_from_null(buf, name, family, flags);
177         if (cnt<=0) cnt = name_from_numeric(buf, name, family);
178         if (cnt<=0 && !(flags & AI_NUMERICHOST)) {
179                 cnt = name_from_hosts(buf, canon, name, family);
180                 if (cnt<=0) cnt = name_from_dns(buf, canon, name, family);
181         }
182         if (cnt<=0) return cnt ? cnt : EAI_NONAME;
183
184         /* Filter/transform results for v4-mapped lookup, if requested. */
185         if (flags & AI_V4MAPPED) {
186                 if (!(flags & AI_ALL)) {
187                         /* If any v6 results exist, remove v4 results. */
188                         for (i=0; i<cnt && buf[i].family != AF_INET6; i++);
189                         if (i<cnt) {
190                                 for (j=0; i<cnt; i++) {
191                                         if (buf[i].family == AF_INET6)
192                                                 buf[j++] = buf[i];
193                                 }
194                                 cnt = i = j;
195                         }
196                 }
197                 /* Translate any remaining v4 results to v6 */
198                 for (i=0; i<cnt; i++) {
199                         if (buf[i].family != AF_INET) continue;
200                         memcpy(buf[i].addr+12, buf[i].addr, 4);
201                         memcpy(buf[i].addr, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
202                         buf[i].scopeid = 0;
203                         buf[i].family = AF_INET6;
204                 }
205         }
206
207         return cnt;
208 }