source: asp3_tinet_ecnl_arm/trunk/musl-1.1.18/src/network/lookup_name.c@ 387

Last change on this file since 387 was 387, checked in by coas-nagasima, 5 years ago

ファイルディスクリプタ処理を更新

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 12.1 KB
Line 
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 <unistd.h>
11#include <pthread.h>
12#include <errno.h>
13#include <resolv.h>
14#include "lookup.h"
15#include "stdio_impl.h"
16#include "syscall.h"
17
18static int is_valid_hostname(const char *host)
19{
20 const unsigned char *s;
21 if (strnlen(host, 255)-1 >= 254 || mbstowcs(0, host, 0) == -1) return 0;
22 for (s=(void *)host; *s>=0x80 || *s=='.' || *s=='-' || isalnum(*s); s++);
23 return !*s;
24}
25
26static int name_from_null(struct address buf[static 2], const char *name, int family, int flags)
27{
28 int cnt = 0;
29 if (name) return 0;
30 if (flags & AI_PASSIVE) {
31 if (family != AF_INET6)
32 buf[cnt++] = (struct address){ .family = AF_INET };
33 if (family != AF_INET)
34 buf[cnt++] = (struct address){ .family = AF_INET6 };
35 } else {
36 if (family != AF_INET6)
37 buf[cnt++] = (struct address){ .family = AF_INET, .addr = { 127,0,0,1 } };
38 if (family != AF_INET)
39 buf[cnt++] = (struct address){ .family = AF_INET6, .addr = { [15] = 1 } };
40 }
41 return cnt;
42}
43
44static int name_from_numeric(struct address buf[static 1], const char *name, int family)
45{
46 return __lookup_ipliteral(buf, name, family);
47}
48
49static int name_from_hosts(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family)
50{
51 char line[512];
52 size_t l = strlen(name);
53 int cnt = 0, badfam = 0;
54 unsigned char _buf[1032];
55 FILE _f, *f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf);
56 if (!f) switch (errno) {
57 case ENOENT:
58 case ENOTDIR:
59 case EACCES:
60 return 0;
61 default:
62 return EAI_SYSTEM;
63 }
64 while (fgets(line, sizeof line, f) && cnt < MAXADDRS) {
65 char *p, *z;
66
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++);
70 if (!p) continue;
71
72 /* Isolate IP address to parse */
73 for (p=line; *p && !isspace(*p); p++);
74 *p++ = 0;
75 switch (name_from_numeric(buf+cnt, line, family)) {
76 case 1:
77 cnt++;
78 break;
79 case 0:
80 continue;
81 default:
82 badfam = EAI_NONAME;
83 continue;
84 }
85
86 /* Extract first name as canonical name */
87 for (; *p && isspace(*p); p++);
88 for (z=p; *z && !isspace(*z); z++);
89 *z = 0;
90 if (is_valid_hostname(p)) memcpy(canon, p, z-p+1);
91 }
92 __fclose_ca(f);
93 return cnt ? cnt : badfam;
94}
95
96struct dpc_ctx {
97 struct address *addrs;
98 char *canon;
99 int cnt;
100};
101
102int __dns_parse(const unsigned char *, int, int (*)(void *, int, const void *, int, const void *), void *);
103int __dn_expand(const unsigned char *, const unsigned char *, const unsigned char *, char *, int);
104int __res_mkquery(int, const char *, int, int, const unsigned char *, int, const unsigned char*, unsigned char *, int);
105int __res_msend_rc(int, const unsigned char *const *, const int *, unsigned char *const *, int *, int, const struct resolvconf *);
106
107#define RR_A 1
108#define RR_CNAME 5
109#define RR_AAAA 28
110
111static int dns_parse_callback(void *c, int rr, const void *data, int len, const void *packet)
112{
113 char tmp[256];
114 struct dpc_ctx *ctx = c;
115 if (ctx->cnt >= MAXADDRS) return -1;
116 switch (rr) {
117 case RR_A:
118 if (len != 4) return -1;
119 ctx->addrs[ctx->cnt].family = AF_INET;
120 ctx->addrs[ctx->cnt].scopeid = 0;
121 memcpy(ctx->addrs[ctx->cnt++].addr, data, 4);
122 break;
123 case RR_AAAA:
124 if (len != 16) return -1;
125 ctx->addrs[ctx->cnt].family = AF_INET6;
126 ctx->addrs[ctx->cnt].scopeid = 0;
127 memcpy(ctx->addrs[ctx->cnt++].addr, data, 16);
128 break;
129 case RR_CNAME:
130 if (__dn_expand(packet, (const unsigned char *)packet + 512,
131 data, tmp, sizeof tmp) > 0 && is_valid_hostname(tmp))
132 strcpy(ctx->canon, tmp);
133 break;
134 }
135 return 0;
136}
137
138static int name_from_dns(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family, const struct resolvconf *conf)
139{
140 unsigned char qbuf[2][280], abuf[2][512];
141 const unsigned char *qp[2] = { qbuf[0], qbuf[1] };
142 unsigned char *ap[2] = { abuf[0], abuf[1] };
143 int qlens[2], alens[2];
144 int i, nq = 0;
145 struct dpc_ctx ctx = { .addrs = buf, .canon = canon };
146 static const struct { int af; int rr; } afrr[2] = {
147 { .af = AF_INET6, .rr = RR_A },
148 { .af = AF_INET, .rr = RR_AAAA },
149 };
150
151 for (i=0; i<2; i++) {
152 if (family != afrr[i].af) {
153 qlens[nq] = __res_mkquery(0, name, 1, afrr[i].rr,
154 0, 0, 0, qbuf[nq], sizeof *qbuf);
155 if (qlens[nq] == -1)
156 return EAI_NONAME;
157 nq++;
158 }
159 }
160
161 if (__res_msend_rc(nq, qp, qlens, ap, alens, sizeof *abuf, conf) < 0)
162 return EAI_SYSTEM;
163
164 for (i=0; i<nq; i++)
165 __dns_parse(abuf[i], alens[i], dns_parse_callback, &ctx);
166
167 if (ctx.cnt) return ctx.cnt;
168 if (alens[0] < 4 || (abuf[0][3] & 15) == 2) return EAI_AGAIN;
169 if ((abuf[0][3] & 15) == 0) return EAI_NONAME;
170 if ((abuf[0][3] & 15) == 3) return 0;
171 return EAI_FAIL;
172}
173
174static int name_from_dns_search(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family)
175{
176 char search[256];
177 struct resolvconf conf;
178 size_t l, dots;
179 char *p, *z;
180
181 if (__get_resolv_conf(&conf, search, sizeof search) < 0) return -1;
182
183 /* Count dots, suppress search when >=ndots or name ends in
184 * a dot, which is an explicit request for global scope. */
185 for (dots=l=0; name[l]; l++) if (name[l]=='.') dots++;
186 if (dots >= conf.ndots || name[l-1]=='.') *search = 0;
187
188 /* Strip final dot for canon, fail if multiple trailing dots. */
189 if (name[l-1]=='.') l--;
190 if (!l || name[l-1]=='.') return EAI_NONAME;
191
192 /* This can never happen; the caller already checked length. */
193 if (l >= 256) return EAI_NONAME;
194
195 /* Name with search domain appended is setup in canon[]. This both
196 * provides the desired default canonical name (if the requested
197 * name is not a CNAME record) and serves as a buffer for passing
198 * the full requested name to name_from_dns. */
199 memcpy(canon, name, l);
200 canon[l] = '.';
201
202 for (p=search; *p; p=z) {
203 for (; isspace(*p); p++);
204 for (z=p; *z && !isspace(*z); z++);
205 if (z==p) break;
206 if (z-p < 256 - l - 1) {
207 memcpy(canon+l+1, p, z-p);
208 canon[z-p+1+l] = 0;
209 int cnt = name_from_dns(buf, canon, canon, family, &conf);
210 if (cnt) return cnt;
211 }
212 }
213
214 canon[l] = 0;
215 return name_from_dns(buf, canon, name, family, &conf);
216}
217
218static const struct policy {
219 unsigned char addr[16];
220 unsigned char len, mask;
221 unsigned char prec, label;
222} defpolicy[] = {
223 { "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1", 15, 0xff, 50, 0 },
224 { "\0\0\0\0\0\0\0\0\0\0\xff\xff", 11, 0xff, 35, 4 },
225 { "\x20\2", 1, 0xff, 30, 2 },
226 { "\x20\1", 3, 0xff, 5, 5 },
227 { "\xfc", 0, 0xfe, 3, 13 },
228#if 0
229 /* These are deprecated and/or returned to the address
230 * pool, so despite the RFC, treating them as special
231 * is probably wrong. */
232 { "", 11, 0xff, 1, 3 },
233 { "\xfe\xc0", 1, 0xc0, 1, 11 },
234 { "\x3f\xfe", 1, 0xff, 1, 12 },
235#endif
236 /* Last rule must match all addresses to stop loop. */
237 { "", 0, 0, 40, 1 },
238};
239
240static const struct policy *policyof(const struct in6_addr *a)
241{
242 int i;
243 for (i=0; ; i++) {
244 if (memcmp(a->s6_addr, defpolicy[i].addr, defpolicy[i].len))
245 continue;
246 if ((a->s6_addr[defpolicy[i].len] & defpolicy[i].mask)
247 != defpolicy[i].addr[defpolicy[i].len])
248 continue;
249 return defpolicy+i;
250 }
251}
252
253static int labelof(const struct in6_addr *a)
254{
255 return policyof(a)->label;
256}
257
258static int scopeof(const struct in6_addr *a)
259{
260 if (IN6_IS_ADDR_MULTICAST(a)) return a->s6_addr[1] & 15;
261 if (IN6_IS_ADDR_LINKLOCAL(a)) return 2;
262 if (IN6_IS_ADDR_LOOPBACK(a)) return 2;
263 if (IN6_IS_ADDR_SITELOCAL(a)) return 5;
264 return 14;
265}
266
267static int prefixmatch(const struct in6_addr *s, const struct in6_addr *d)
268{
269 /* FIXME: The common prefix length should be limited to no greater
270 * than the nominal length of the prefix portion of the source
271 * address. However the definition of the source prefix length is
272 * not clear and thus this limiting is not yet implemented. */
273 unsigned i;
274 for (i=0; i<128 && !((s->s6_addr[i/8]^d->s6_addr[i/8])&(128>>(i%8))); i++);
275 return i;
276}
277
278#define DAS_USABLE 0x40000000
279#define DAS_MATCHINGSCOPE 0x20000000
280#define DAS_MATCHINGLABEL 0x10000000
281#define DAS_PREC_SHIFT 20
282#define DAS_SCOPE_SHIFT 16
283#define DAS_PREFIX_SHIFT 8
284#define DAS_ORDER_SHIFT 0
285
286static int addrcmp(const void *_a, const void *_b)
287{
288 const struct address *a = _a, *b = _b;
289 return b->sortkey - a->sortkey;
290}
291
292int __lookup_name(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family, int flags)
293{
294 int cnt = 0, i, j;
295
296 *canon = 0;
297 if (name) {
298 /* reject empty name and check len so it fits into temp bufs */
299 size_t l = strnlen(name, 255);
300 if (l-1 >= 254)
301 return EAI_NONAME;
302 memcpy(canon, name, l+1);
303 }
304
305 /* Procedurally, a request for v6 addresses with the v4-mapped
306 * flag set is like a request for unspecified family, followed
307 * by filtering of the results. */
308 if (flags & AI_V4MAPPED) {
309 if (family == AF_INET6) family = AF_UNSPEC;
310 else flags -= AI_V4MAPPED;
311 }
312
313 /* Try each backend until there's at least one result. */
314 cnt = name_from_null(buf, name, family, flags);
315 if (!cnt) cnt = name_from_numeric(buf, name, family);
316 if (!cnt && !(flags & AI_NUMERICHOST)) {
317 cnt = name_from_hosts(buf, canon, name, family);
318 if (cnt<=0) cnt = name_from_dns_search(buf, canon, name, family);
319 }
320 if (cnt<=0) return cnt ? cnt : EAI_NONAME;
321
322 /* Filter/transform results for v4-mapped lookup, if requested. */
323 if (flags & AI_V4MAPPED) {
324 if (!(flags & AI_ALL)) {
325 /* If any v6 results exist, remove v4 results. */
326 for (i=0; i<cnt && buf[i].family != AF_INET6; i++);
327 if (i<cnt) {
328 for (j=0; i<cnt; i++) {
329 if (buf[i].family == AF_INET6)
330 buf[j++] = buf[i];
331 }
332 cnt = i = j;
333 }
334 }
335 /* Translate any remaining v4 results to v6 */
336 for (i=0; i<cnt; i++) {
337 if (buf[i].family != AF_INET) continue;
338 memcpy(buf[i].addr+12, buf[i].addr, 4);
339 memcpy(buf[i].addr, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
340 buf[i].family = AF_INET6;
341 }
342 }
343
344 /* No further processing is needed if there are fewer than 2
345 * results or if there are only IPv4 results. */
346 if (cnt<2 || family==AF_INET) return cnt;
347 for (i=0; i<cnt; i++) if (buf[i].family != AF_INET) break;
348 if (i==cnt) return cnt;
349
350 int cs;
351 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
352
353 /* The following implements a subset of RFC 3484/6724 destination
354 * address selection by generating a single 31-bit sort key for
355 * each address. Rules 3, 4, and 7 are omitted for having
356 * excessive runtime and code size cost and dubious benefit.
357 * So far the label/precedence table cannot be customized. */
358 for (i=0; i<cnt; i++) {
359 int family = buf[i].family;
360 int key = 0;
361 struct sockaddr_in6 sa6 = { 0 }, da6 = {
362 .sin6_family = AF_INET6,
363 .sin6_scope_id = buf[i].scopeid,
364 .sin6_port = 65535
365 };
366 struct sockaddr_in sa4 = { 0 }, da4 = {
367 .sin_family = AF_INET,
368 .sin_port = 65535
369 };
370 void *sa, *da;
371 socklen_t salen, dalen;
372 if (family == AF_INET6) {
373 memcpy(da6.sin6_addr.s6_addr, buf[i].addr, 16);
374 da = &da6; dalen = sizeof da6;
375 sa = &sa6; salen = sizeof sa6;
376 } else {
377 memcpy(sa6.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(da6.sin6_addr.s6_addr,
381 "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
382 memcpy(da6.sin6_addr.s6_addr+12, buf[i].addr, 4);
383 memcpy(&da4.sin_addr, buf[i].addr, 4);
384 da = &da4; dalen = sizeof da4;
385 sa = &sa4; salen = sizeof sa4;
386 }
387 const struct policy *dpolicy = policyof(&da6.sin6_addr);
388 int dscope = scopeof(&da6.sin6_addr);
389 int dlabel = dpolicy->label;
390 int dprec = dpolicy->prec;
391 int prefixlen = 0;
392 int fd = socket(family, SOCK_DGRAM|SOCK_CLOEXEC, IPPROTO_UDP);
393 if (fd >= 0) {
394 if (!connect(fd, da, dalen)) {
395 key |= DAS_USABLE;
396 if (!getsockname(fd, sa, &salen)) {
397 if (family == AF_INET) memcpy(
398 sa6.sin6_addr.s6_addr+12,
399 &sa4.sin_addr, 4);
400 if (dscope == scopeof(&sa6.sin6_addr))
401 key |= DAS_MATCHINGSCOPE;
402 if (dlabel == labelof(&sa6.sin6_addr))
403 key |= DAS_MATCHINGLABEL;
404 prefixlen = prefixmatch(&sa6.sin6_addr,
405 &da6.sin6_addr);
406 }
407 }
408 close(fd);
409 }
410 key |= dprec << DAS_PREC_SHIFT;
411 key |= (15-dscope) << DAS_SCOPE_SHIFT;
412 key |= prefixlen << DAS_PREFIX_SHIFT;
413 key |= (MAXADDRS-i) << DAS_ORDER_SHIFT;
414 buf[i].sortkey = key;
415 }
416 qsort(buf, cnt, sizeof *buf, addrcmp);
417
418 pthread_setcancelstate(cs, 0);
419
420 return cnt;
421}
Note: See TracBrowser for help on using the repository browser.