1 #include <sys/socket.h>
2 #include <netinet/in.h>
11 #include "stdio_impl.h"
14 static int is_valid_hostname(const char *host)
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++);
22 static int name_from_null(struct address buf[static 2], const char *name, int family, int flags)
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 };
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 } };
40 int __inet_aton(const char *, struct in_addr *);
42 static int name_from_numeric(struct address buf[static 1], const char *name, int family)
46 if (family != AF_INET6 && __inet_aton(name, &a4)>0) {
47 memcpy(&buf[0].addr, &a4, sizeof a4);
48 buf[0].family = AF_INET;
51 if (family != AF_INET) {
53 char *p = strchr(name, '%'), *z;
54 unsigned long long scopeid;
55 if (p && p-name < 64) {
56 memcpy(tmp, name, p-name);
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;
64 if (isdigit(*++p)) scopeid = strtoull(p, &z, 10);
67 if (!IN6_IS_ADDR_LINKLOCAL(&a6) &&
68 !IN6_IS_ADDR_MC_LINKLOCAL(&a6))
70 scopeid = if_nametoindex(p);
71 if (!scopeid) return EAI_NONAME;
73 if (scopeid > UINT_MAX) return EAI_NONAME;
74 buf[0].scopeid = scopeid;
81 static int name_from_hosts(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family)
84 size_t l = strlen(name);
86 unsigned char _buf[1032];
87 FILE _f, *f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf);
89 while (fgets(line, sizeof line, f) && cnt < MAXADDRS) {
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++);
97 /* Isolate IP address to parse */
98 for (p=line; *p && !isspace(*p); p++);
100 if (name_from_numeric(buf+cnt, line, family))
103 /* Extract first name as canonical name */
104 for (; *p && isspace(*p); p++);
105 for (z=p; *z && !isspace(*z); z++);
107 if (is_valid_hostname(p)) memcpy(canon, p, z-p+1);
114 struct address *addrs;
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);
128 static int dns_parse_callback(void *c, int rr, const void *data, int len, const void *packet)
131 struct dpc_ctx *ctx = c;
134 if (len != 4) return -1;
135 ctx->addrs[ctx->cnt].family = AF_INET;
136 memcpy(ctx->addrs[ctx->cnt++].addr, data, 4);
139 if (len != 16) return -1;
140 ctx->addrs[ctx->cnt].family = AF_INET6;
141 memcpy(ctx->addrs[ctx->cnt++].addr, data, 16);
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);
152 static int name_from_dns(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family)
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];
159 struct dpc_ctx ctx = { .addrs = buf, .canon = canon };
161 if (family != AF_INET6) {
162 qlens[nq] = __res_mkquery(0, name, 1, RR_A, 0, 0, 0,
163 qbuf[nq], sizeof *qbuf);
166 if (family != AF_INET) {
167 qlens[nq] = __res_mkquery(0, name, 1, RR_AAAA, 0, 0, 0,
168 qbuf[nq], sizeof *qbuf);
172 if (__res_msend(nq, qp, qlens, ap, alens, sizeof *abuf) < 0) return EAI_SYSTEM;
175 __dns_parse(abuf[i], alens[i], dns_parse_callback, &ctx);
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;
183 int __lookup_name(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family, int flags)
190 if ((l = strnlen(name, 256))-1 > 254)
192 memcpy(canon, name, l+1);
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;
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);
210 if (cnt<=0) return cnt ? cnt : EAI_NONAME;
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++);
218 for (j=0; i<cnt; i++) {
219 if (buf[i].family == AF_INET6)
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);
231 buf[i].family = AF_INET6;