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, 255)-1 >= 254 || mbstowcs(0, host, 0) == -1) 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 static int name_from_numeric(struct address buf[static 1], const char *name, int family)
42 return __lookup_ipliteral(buf, name, family);
45 static int name_from_hosts(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family)
48 size_t l = strlen(name);
50 unsigned char _buf[1032];
51 FILE _f, *f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf);
53 while (fgets(line, sizeof line, f) && cnt < MAXADDRS) {
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++);
61 /* Isolate IP address to parse */
62 for (p=line; *p && !isspace(*p); p++);
64 if (name_from_numeric(buf+cnt, line, family))
67 /* Extract first name as canonical name */
68 for (; *p && isspace(*p); p++);
69 for (z=p; *z && !isspace(*z); z++);
71 if (is_valid_hostname(p)) memcpy(canon, p, z-p+1);
78 struct address *addrs;
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);
92 static int dns_parse_callback(void *c, int rr, const void *data, int len, const void *packet)
95 struct dpc_ctx *ctx = c;
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);
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);
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);
118 static int name_from_dns(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family)
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];
125 struct dpc_ctx ctx = { .addrs = buf, .canon = canon };
127 if (family != AF_INET6) {
128 qlens[nq] = __res_mkquery(0, name, 1, RR_A, 0, 0, 0,
129 qbuf[nq], sizeof *qbuf);
132 if (family != AF_INET) {
133 qlens[nq] = __res_mkquery(0, name, 1, RR_AAAA, 0, 0, 0,
134 qbuf[nq], sizeof *qbuf);
138 if (__res_msend(nq, qp, qlens, ap, alens, sizeof *abuf) < 0) return EAI_SYSTEM;
141 __dns_parse(abuf[i], alens[i], dns_parse_callback, &ctx);
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;
149 int __lookup_name(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family, int flags)
155 /* reject empty name and check len so it fits into temp bufs */
156 size_t l = strnlen(name, 255);
159 memcpy(canon, name, l+1);
162 /* Procedurally, a request for v6 addresses with the v4-mapped
163 * flag set is like a request for unspecified family, followed
164 * by filtering of the results. */
165 if (flags & AI_V4MAPPED) {
166 if (family == AF_INET6) family = AF_UNSPEC;
167 else flags -= AI_V4MAPPED;
170 /* Try each backend until there's at least one result. */
171 cnt = name_from_null(buf, name, family, flags);
172 if (!cnt) cnt = name_from_numeric(buf, name, family);
173 if (!cnt && !(flags & AI_NUMERICHOST)) {
174 cnt = name_from_hosts(buf, canon, name, family);
175 if (!cnt) cnt = name_from_dns(buf, canon, name, family);
177 if (cnt<=0) return cnt ? cnt : EAI_NONAME;
179 /* Filter/transform results for v4-mapped lookup, if requested. */
180 if (flags & AI_V4MAPPED) {
181 if (!(flags & AI_ALL)) {
182 /* If any v6 results exist, remove v4 results. */
183 for (i=0; i<cnt && buf[i].family != AF_INET6; i++);
185 for (j=0; i<cnt; i++) {
186 if (buf[i].family == AF_INET6)
192 /* Translate any remaining v4 results to v6 */
193 for (i=0; i<cnt; i++) {
194 if (buf[i].family != AF_INET) continue;
195 memcpy(buf[i].addr+12, buf[i].addr, 4);
196 memcpy(buf[i].addr, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
197 buf[i].family = AF_INET6;