1 #include <sys/socket.h>
2 #include <netinet/in.h>
14 #include "stdio_impl.h"
17 static int is_valid_hostname(const char *host)
19 const unsigned char *s;
20 if (strnlen(host, 255)-1 >= 254 || mbstowcs(0, host, 0) == -1) return 0;
21 for (s=(void *)host; *s>=0x80 || *s=='.' || *s=='-' || isalnum(*s); s++);
25 static int name_from_null(struct address buf[static 2], const char *name, int family, int flags)
29 if (flags & AI_PASSIVE) {
30 if (family != AF_INET6)
31 buf[cnt++] = (struct address){ .family = AF_INET };
32 if (family != AF_INET)
33 buf[cnt++] = (struct address){ .family = AF_INET6 };
35 if (family != AF_INET6)
36 buf[cnt++] = (struct address){ .family = AF_INET, .addr = { 127,0,0,1 } };
37 if (family != AF_INET)
38 buf[cnt++] = (struct address){ .family = AF_INET6, .addr = { [15] = 1 } };
43 static int name_from_numeric(struct address buf[static 1], const char *name, int family)
45 return __lookup_ipliteral(buf, name, family);
48 static int name_from_hosts(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family)
51 size_t l = strlen(name);
52 int cnt = 0, badfam = 0;
53 unsigned char _buf[1032];
54 FILE _f, *f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf);
55 if (!f) switch (errno) {
63 while (fgets(line, sizeof line, f) && cnt < MAXADDRS) {
66 if ((p=strchr(line, '#'))) *p++='\n', *p=0;
67 for(p=line+1; (p=strstr(p, name)) &&
68 (!isspace(p[-1]) || !isspace(p[l])); p++);
71 /* Isolate IP address to parse */
72 for (p=line; *p && !isspace(*p); p++);
74 switch (name_from_numeric(buf+cnt, line, family)) {
85 /* Extract first name as canonical name */
86 for (; *p && isspace(*p); p++);
87 for (z=p; *z && !isspace(*z); z++);
89 if (is_valid_hostname(p)) memcpy(canon, p, z-p+1);
92 return cnt ? cnt : badfam;
96 struct address *addrs;
101 int __dn_expand(const unsigned char *, const unsigned char *, const unsigned char *, char *, int);
102 int __res_mkquery(int, const char *, int, int, const unsigned char *, int, const unsigned char*, unsigned char *, int);
108 static int dns_parse_callback(void *c, int rr, const void *data, int len, const void *packet)
111 struct dpc_ctx *ctx = c;
112 if (ctx->cnt >= MAXADDRS) return -1;
115 if (len != 4) return -1;
116 ctx->addrs[ctx->cnt].family = AF_INET;
117 ctx->addrs[ctx->cnt].scopeid = 0;
118 memcpy(ctx->addrs[ctx->cnt++].addr, data, 4);
121 if (len != 16) return -1;
122 ctx->addrs[ctx->cnt].family = AF_INET6;
123 ctx->addrs[ctx->cnt].scopeid = 0;
124 memcpy(ctx->addrs[ctx->cnt++].addr, data, 16);
127 if (__dn_expand(packet, (const unsigned char *)packet + 512,
128 data, tmp, sizeof tmp) > 0 && is_valid_hostname(tmp))
129 strcpy(ctx->canon, tmp);
135 static int name_from_dns(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family, const struct resolvconf *conf)
137 unsigned char qbuf[2][280], abuf[2][512];
138 const unsigned char *qp[2] = { qbuf[0], qbuf[1] };
139 unsigned char *ap[2] = { abuf[0], abuf[1] };
140 int qlens[2], alens[2];
142 struct dpc_ctx ctx = { .addrs = buf, .canon = canon };
143 static const struct { int af; int rr; } afrr[2] = {
144 { .af = AF_INET6, .rr = RR_A },
145 { .af = AF_INET, .rr = RR_AAAA },
148 for (i=0; i<2; i++) {
149 if (family != afrr[i].af) {
150 qlens[nq] = __res_mkquery(0, name, 1, afrr[i].rr,
151 0, 0, 0, qbuf[nq], sizeof *qbuf);
158 if (__res_msend_rc(nq, qp, qlens, ap, alens, sizeof *abuf, conf) < 0)
162 __dns_parse(abuf[i], alens[i], dns_parse_callback, &ctx);
164 if (ctx.cnt) return ctx.cnt;
165 if (alens[0] < 4 || (abuf[0][3] & 15) == 2) return EAI_AGAIN;
166 if ((abuf[0][3] & 15) == 0) return EAI_NONAME;
167 if ((abuf[0][3] & 15) == 3) return 0;
171 static int name_from_dns_search(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family)
174 struct resolvconf conf;
178 if (__get_resolv_conf(&conf, search, sizeof search) < 0) return -1;
180 /* Count dots, suppress search when >=ndots or name ends in
181 * a dot, which is an explicit request for global scope. */
182 for (dots=l=0; name[l]; l++) if (name[l]=='.') dots++;
183 if (dots >= conf.ndots || name[l-1]=='.') *search = 0;
185 /* Strip final dot for canon, fail if multiple trailing dots. */
186 if (name[l-1]=='.') l--;
187 if (!l || name[l-1]=='.') return EAI_NONAME;
189 /* This can never happen; the caller already checked length. */
190 if (l >= 256) return EAI_NONAME;
192 /* Name with search domain appended is setup in canon[]. This both
193 * provides the desired default canonical name (if the requested
194 * name is not a CNAME record) and serves as a buffer for passing
195 * the full requested name to name_from_dns. */
196 memcpy(canon, name, l);
199 for (p=search; *p; p=z) {
200 for (; isspace(*p); p++);
201 for (z=p; *z && !isspace(*z); z++);
203 if (z-p < 256 - l - 1) {
204 memcpy(canon+l+1, p, z-p);
206 int cnt = name_from_dns(buf, canon, canon, family, &conf);
212 return name_from_dns(buf, canon, name, family, &conf);
215 static const struct policy {
216 unsigned char addr[16];
217 unsigned char len, mask;
218 unsigned char prec, label;
220 { "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1", 15, 0xff, 50, 0 },
221 { "\0\0\0\0\0\0\0\0\0\0\xff\xff", 11, 0xff, 35, 4 },
222 { "\x20\2", 1, 0xff, 30, 2 },
223 { "\x20\1", 3, 0xff, 5, 5 },
224 { "\xfc", 0, 0xfe, 3, 13 },
226 /* These are deprecated and/or returned to the address
227 * pool, so despite the RFC, treating them as special
228 * is probably wrong. */
229 { "", 11, 0xff, 1, 3 },
230 { "\xfe\xc0", 1, 0xc0, 1, 11 },
231 { "\x3f\xfe", 1, 0xff, 1, 12 },
233 /* Last rule must match all addresses to stop loop. */
237 static const struct policy *policyof(const struct in6_addr *a)
241 if (memcmp(a->s6_addr, defpolicy[i].addr, defpolicy[i].len))
243 if ((a->s6_addr[defpolicy[i].len] & defpolicy[i].mask)
244 != defpolicy[i].addr[defpolicy[i].len])
250 static int labelof(const struct in6_addr *a)
252 return policyof(a)->label;
255 static int scopeof(const struct in6_addr *a)
257 if (IN6_IS_ADDR_MULTICAST(a)) return a->s6_addr[1] & 15;
258 if (IN6_IS_ADDR_LINKLOCAL(a)) return 2;
259 if (IN6_IS_ADDR_LOOPBACK(a)) return 2;
260 if (IN6_IS_ADDR_SITELOCAL(a)) return 5;
264 static int prefixmatch(const struct in6_addr *s, const struct in6_addr *d)
266 /* FIXME: The common prefix length should be limited to no greater
267 * than the nominal length of the prefix portion of the source
268 * address. However the definition of the source prefix length is
269 * not clear and thus this limiting is not yet implemented. */
271 for (i=0; i<128 && !((s->s6_addr[i/8]^d->s6_addr[i/8])&(128>>(i%8))); i++);
275 #define DAS_USABLE 0x40000000
276 #define DAS_MATCHINGSCOPE 0x20000000
277 #define DAS_MATCHINGLABEL 0x10000000
278 #define DAS_PREC_SHIFT 20
279 #define DAS_SCOPE_SHIFT 16
280 #define DAS_PREFIX_SHIFT 8
281 #define DAS_ORDER_SHIFT 0
283 static int addrcmp(const void *_a, const void *_b)
285 const struct address *a = _a, *b = _b;
286 return b->sortkey - a->sortkey;
289 int __lookup_name(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family, int flags)
295 /* reject empty name and check len so it fits into temp bufs */
296 size_t l = strnlen(name, 255);
299 memcpy(canon, name, l+1);
302 /* Procedurally, a request for v6 addresses with the v4-mapped
303 * flag set is like a request for unspecified family, followed
304 * by filtering of the results. */
305 if (flags & AI_V4MAPPED) {
306 if (family == AF_INET6) family = AF_UNSPEC;
307 else flags -= AI_V4MAPPED;
310 /* Try each backend until there's at least one result. */
311 cnt = name_from_null(buf, name, family, flags);
312 if (!cnt) cnt = name_from_numeric(buf, name, family);
313 if (!cnt && !(flags & AI_NUMERICHOST)) {
314 cnt = name_from_hosts(buf, canon, name, family);
315 if (!cnt) cnt = name_from_dns_search(buf, canon, name, family);
317 if (cnt<=0) return cnt ? cnt : EAI_NONAME;
319 /* Filter/transform results for v4-mapped lookup, if requested. */
320 if (flags & AI_V4MAPPED) {
321 if (!(flags & AI_ALL)) {
322 /* If any v6 results exist, remove v4 results. */
323 for (i=0; i<cnt && buf[i].family != AF_INET6; i++);
325 for (j=0; i<cnt; i++) {
326 if (buf[i].family == AF_INET6)
332 /* Translate any remaining v4 results to v6 */
333 for (i=0; i<cnt; i++) {
334 if (buf[i].family != AF_INET) continue;
335 memcpy(buf[i].addr+12, buf[i].addr, 4);
336 memcpy(buf[i].addr, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
337 buf[i].family = AF_INET6;
341 /* No further processing is needed if there are fewer than 2
342 * results or if there are only IPv4 results. */
343 if (cnt<2 || family==AF_INET) return cnt;
344 for (i=0; i<cnt; i++) if (buf[i].family != AF_INET) break;
345 if (i==cnt) return cnt;
348 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
350 /* The following implements a subset of RFC 3484/6724 destination
351 * address selection by generating a single 31-bit sort key for
352 * each address. Rules 3, 4, and 7 are omitted for having
353 * excessive runtime and code size cost and dubious benefit.
354 * So far the label/precedence table cannot be customized. */
355 for (i=0; i<cnt; i++) {
356 int family = buf[i].family;
358 struct sockaddr_in6 sa6 = { 0 }, da6 = {
359 .sin6_family = AF_INET6,
360 .sin6_scope_id = buf[i].scopeid,
363 struct sockaddr_in sa4 = { 0 }, da4 = {
364 .sin_family = AF_INET,
368 socklen_t salen, dalen;
369 if (family == AF_INET6) {
370 memcpy(da6.sin6_addr.s6_addr, buf[i].addr, 16);
371 da = &da6; dalen = sizeof da6;
372 sa = &sa6; salen = sizeof sa6;
374 memcpy(sa6.sin6_addr.s6_addr,
375 "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
376 memcpy(da6.sin6_addr.s6_addr+12, buf[i].addr, 4);
377 memcpy(da6.sin6_addr.s6_addr,
378 "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
379 memcpy(da6.sin6_addr.s6_addr+12, buf[i].addr, 4);
380 memcpy(&da4.sin_addr, buf[i].addr, 4);
381 da = &da4; dalen = sizeof da4;
382 sa = &sa4; salen = sizeof sa4;
384 const struct policy *dpolicy = policyof(&da6.sin6_addr);
385 int dscope = scopeof(&da6.sin6_addr);
386 int dlabel = dpolicy->label;
387 int dprec = dpolicy->prec;
389 int fd = socket(family, SOCK_DGRAM|SOCK_CLOEXEC, IPPROTO_UDP);
391 if (!connect(fd, da, dalen)) {
393 if (!getsockname(fd, sa, &salen)) {
394 if (family == AF_INET) memcpy(
395 sa6.sin6_addr.s6_addr+12,
397 if (dscope == scopeof(&sa6.sin6_addr))
398 key |= DAS_MATCHINGSCOPE;
399 if (dlabel == labelof(&sa6.sin6_addr))
400 key |= DAS_MATCHINGLABEL;
401 prefixlen = prefixmatch(&sa6.sin6_addr,
407 key |= dprec << DAS_PREC_SHIFT;
408 key |= (15-dscope) << DAS_SCOPE_SHIFT;
409 key |= prefixlen << DAS_PREFIX_SHIFT;
410 key |= (MAXADDRS-i) << DAS_ORDER_SHIFT;
411 buf[i].sortkey = key;
413 qsort(buf, cnt, sizeof *buf, addrcmp);
415 pthread_setcancelstate(cs, 0);