1 #include <sys/socket.h>
2 #include <netinet/in.h>
10 #include "stdio_impl.h"
13 static int is_valid_hostname(const char *host)
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++);
21 static int name_from_null(struct address buf[static 2], const char *name, int family, int flags)
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 };
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 } };
39 static int name_from_numeric(struct address buf[static 1], const char *name, int family)
43 if (family != AF_INET6 && inet_aton(name, &a4)>0) {
44 memcpy(&buf[0].addr, &a4, sizeof a4);
45 buf[0].family = AF_INET;
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;
56 static int name_from_hosts(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family)
59 size_t l = strlen(name);
61 unsigned char _buf[1032];
62 FILE _f, *f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf);
64 while (fgets(line, sizeof line, f) && cnt < MAXADDRS) {
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++);
72 /* Isolate IP address to parse */
73 for (p=line; *p && !isspace(*p); p++);
75 if (name_from_numeric(buf+cnt, line, family))
78 /* Extract first name as canonical name */
79 for (; *p && isspace(*p); p++);
80 for (z=p; *z && !isspace(*z); z++);
82 if (is_valid_hostname(p)) memcpy(canon, p, z-p+1);
89 struct address *addrs;
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);
103 static int dns_parse_callback(void *c, int rr, const void *data, int len, const void *packet)
106 struct dpc_ctx *ctx = c;
109 if (len != 4) return -1;
110 ctx->addrs[ctx->cnt].family = AF_INET;
111 memcpy(ctx->addrs[ctx->cnt++].addr, data, 4);
114 if (len != 16) return -1;
115 ctx->addrs[ctx->cnt].family = AF_INET6;
116 memcpy(ctx->addrs[ctx->cnt++].addr, data, 16);
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);
127 static int name_from_dns(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family)
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];
134 struct dpc_ctx ctx = { .addrs = buf, .canon = canon };
136 if (family != AF_INET6) {
137 qlens[nq] = __res_mkquery(0, name, 1, RR_A, 0, 0, 0,
138 qbuf[nq], sizeof *qbuf);
141 if (family != AF_INET) {
142 qlens[nq] = __res_mkquery(0, name, 1, RR_AAAA, 0, 0, 0,
143 qbuf[nq], sizeof *qbuf);
147 if (__res_msend(nq, qp, qlens, ap, alens, sizeof *abuf) < 0) return EAI_SYSTEM;
150 __dns_parse(abuf[i], alens[i], dns_parse_callback, &ctx);
155 int __lookup_name(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family, int flags)
162 if ((l = strnlen(name, 256))-1 > 254)
164 memcpy(canon, name, l+1);
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;
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);
182 if (cnt<=0) return cnt ? cnt : EAI_NONAME;
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++);
190 for (j=0; i<cnt; i++) {
191 if (buf[i].family == AF_INET6)
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);
203 buf[i].family = AF_INET6;