add support for reverse name lookups from hosts file to 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 static int name_from_numeric(struct address buf[static 1], const char *name, int family)
41 {
42         return __lookup_ipliteral(buf, name, family);
43 }
44
45 static int name_from_hosts(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family)
46 {
47         char line[512];
48         size_t l = strlen(name);
49         int cnt = 0;
50         unsigned char _buf[1032];
51         FILE _f, *f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf);
52         if (!f) return 0;
53         while (fgets(line, sizeof line, f) && cnt < MAXADDRS) {
54                 char *p, *z;
55
56                 if ((p=strchr(line, '#'))) *p++='\n', *p=0;
57                 for(p=line+1; (p=strstr(p, name)) &&
58                         (!isspace(p[-1]) || !isspace(p[l])); p++);
59                 if (!p) continue;
60
61                 /* Isolate IP address to parse */
62                 for (p=line; *p && !isspace(*p); p++);
63                 *p++ = 0;
64                 if (name_from_numeric(buf+cnt, line, family))
65                         cnt++;
66
67                 /* Extract first name as canonical name */
68                 for (; *p && isspace(*p); p++);
69                 for (z=p; *z && !isspace(*z); z++);
70                 *z = 0;
71                 if (is_valid_hostname(p)) memcpy(canon, p, z-p+1);
72         }
73         __fclose_ca(f);
74         return cnt;
75 }
76
77 struct dpc_ctx {
78         struct address *addrs;
79         char *canon;
80         int cnt;
81 };
82
83 int __dns_parse(const unsigned char *, int, int (*)(void *, int, const void *, int, const void *), void *);
84 int __dn_expand(const unsigned char *, const unsigned char *, const unsigned char *, char *, int);
85 int __res_mkquery(int, const char *, int, int, const unsigned char *, int, const unsigned char*, unsigned char *, int);
86 int __res_msend(int, const unsigned char *const *, const int *, unsigned char *const *, int *, int);
87
88 #define RR_A 1
89 #define RR_CNAME 5
90 #define RR_AAAA 28
91
92 static int dns_parse_callback(void *c, int rr, const void *data, int len, const void *packet)
93 {
94         char tmp[256];
95         struct dpc_ctx *ctx = c;
96         switch (rr) {
97         case RR_A:
98                 if (len != 4) return -1;
99                 ctx->addrs[ctx->cnt].family = AF_INET;
100                 ctx->addrs[ctx->cnt].scopeid = 0;
101                 memcpy(ctx->addrs[ctx->cnt++].addr, data, 4);
102                 break;
103         case RR_AAAA:
104                 if (len != 16) return -1;
105                 ctx->addrs[ctx->cnt].family = AF_INET6;
106                 ctx->addrs[ctx->cnt].scopeid = 0;
107                 memcpy(ctx->addrs[ctx->cnt++].addr, data, 16);
108                 break;
109         case RR_CNAME:
110                 if (__dn_expand(packet, (const unsigned char *)packet + 512,
111                     data, tmp, sizeof tmp) > 0 && is_valid_hostname(tmp))
112                         strcpy(ctx->canon, tmp);
113                 break;
114         }
115         return 0;
116 }
117
118 static int name_from_dns(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family)
119 {
120         unsigned char qbuf[2][280], abuf[2][512];
121         const unsigned char *qp[2] = { qbuf[0], qbuf[1] };
122         unsigned char *ap[2] = { abuf[0], abuf[1] };
123         int qlens[2], alens[2];
124         int i, nq = 0;
125         struct dpc_ctx ctx = { .addrs = buf, .canon = canon };
126
127         if (family != AF_INET6) {
128                 qlens[nq] = __res_mkquery(0, name, 1, RR_A, 0, 0, 0,
129                         qbuf[nq], sizeof *qbuf);
130                 nq++;
131         }
132         if (family != AF_INET) {
133                 qlens[nq] = __res_mkquery(0, name, 1, RR_AAAA, 0, 0, 0,
134                         qbuf[nq], sizeof *qbuf);
135                 nq++;
136         }
137
138         if (__res_msend(nq, qp, qlens, ap, alens, sizeof *abuf) < 0) return EAI_SYSTEM;
139
140         for (i=0; i<nq; i++)
141                 __dns_parse(abuf[i], alens[i], dns_parse_callback, &ctx);
142
143         if (ctx.cnt) return ctx.cnt;
144         if (alens[0] < 4 || (abuf[0][3] & 15) == 2) return EAI_AGAIN;
145         if ((abuf[0][3] & 15) == 3) return EAI_NONAME;
146         return EAI_FAIL;
147 }
148
149 int __lookup_name(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family, int flags)
150 {
151         int cnt = 0, i, j;
152
153         *canon = 0;
154         if (name) {
155                 size_t l;
156                 if ((l = strnlen(name, 256))-1 > 254)
157                         return EAI_NONAME;
158                 memcpy(canon, name, l+1);
159         }
160
161         /* Procedurally, a request for v6 addresses with the v4-mapped
162          * flag set is like a request for unspecified family, followed
163          * by filtering of the results. */
164         if (flags & AI_V4MAPPED) {
165                 if (family == AF_INET6) family = AF_UNSPEC;
166                 else flags -= AI_V4MAPPED;
167         }
168
169         /* Try each backend until there's at least one result. */
170         cnt = name_from_null(buf, name, family, flags);
171         if (!cnt) cnt = name_from_numeric(buf, name, family);
172         if (!cnt && !(flags & AI_NUMERICHOST)) {
173                 cnt = name_from_hosts(buf, canon, name, family);
174                 if (!cnt) cnt = name_from_dns(buf, canon, name, family);
175         }
176         if (cnt<=0) return cnt ? cnt : EAI_NONAME;
177
178         /* Filter/transform results for v4-mapped lookup, if requested. */
179         if (flags & AI_V4MAPPED) {
180                 if (!(flags & AI_ALL)) {
181                         /* If any v6 results exist, remove v4 results. */
182                         for (i=0; i<cnt && buf[i].family != AF_INET6; i++);
183                         if (i<cnt) {
184                                 for (j=0; i<cnt; i++) {
185                                         if (buf[i].family == AF_INET6)
186                                                 buf[j++] = buf[i];
187                                 }
188                                 cnt = i = j;
189                         }
190                 }
191                 /* Translate any remaining v4 results to v6 */
192                 for (i=0; i<cnt; i++) {
193                         if (buf[i].family != AF_INET) continue;
194                         memcpy(buf[i].addr+12, buf[i].addr, 4);
195                         memcpy(buf[i].addr, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
196                         buf[i].family = AF_INET6;
197                 }
198         }
199
200         return cnt;
201 }