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