• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <sys/socket.h>
2 #include <netinet/in.h>
3 #include <netinet/tcp.h>
4 #include <netdb.h>
5 #include <arpa/inet.h>
6 #include <stdint.h>
7 #include <string.h>
8 #include <poll.h>
9 #include <time.h>
10 #include <ctype.h>
11 #include <unistd.h>
12 #include <errno.h>
13 #include <pthread.h>
14 #include "stdio_impl.h"
15 #include "syscall.h"
16 #include "lookup.h"
17 #include <errno.h>
18 
cleanup(void * p)19 static void cleanup(void *p)
20 {
21 	struct pollfd *pfd = p;
22 	for (int i=0; pfd[i].fd >= -1; i++)
23 		if (pfd[i].fd >= 0) __syscall(SYS_close, pfd[i].fd);
24 }
25 
mtime()26 static unsigned long mtime()
27 {
28 	struct timespec ts;
29 	if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0 && errno == ENOSYS)
30 		clock_gettime(CLOCK_REALTIME, &ts);
31 	return (unsigned long)ts.tv_sec * 1000
32 		+ ts.tv_nsec / 1000000;
33 }
34 
start_tcp(struct pollfd * pfd,int family,const void * sa,socklen_t sl,const unsigned char * q,int ql,int netid)35 static int start_tcp(struct pollfd *pfd, int family, const void *sa,
36 	socklen_t sl, const unsigned char *q, int ql, int netid)
37 {
38 	struct msghdr mh = {
39 		.msg_name = (void *)sa,
40 		.msg_namelen = sl,
41 		.msg_iovlen = 2,
42 		.msg_iov = (struct iovec [2]){
43 			{ .iov_base = (uint8_t[]){ ql>>8, ql }, .iov_len = 2 },
44 			{ .iov_base = (void *)q, .iov_len = ql } }
45 	};
46 	int r;
47 	int fd = socket(family, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
48 #ifndef __LITEOS__
49 	if (fd < 0) {
50 		MUSL_LOGE("%{public}s: %{public}d: create TCP socket failed, errno id: %{public}d",
51 			__func__, __LINE__, errno);
52 	}
53 	/**
54 	 * Todo FwmarkClient::BindSocket
55 	*/
56 	if (netid > 0) {
57 		res_bind_socket(fd, netid);
58 	}
59 #endif
60 	pfd->fd = fd;
61 	pfd->events = POLLOUT;
62 	if (!setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT,
63 	    &(int){1}, sizeof(int))) {
64 		r = sendmsg(fd, &mh, MSG_FASTOPEN|MSG_NOSIGNAL);
65 		if (r == ql+2) pfd->events = POLLIN;
66 		if (r >= 0) return r;
67 		if (errno == EINPROGRESS) return 0;
68 	}
69 	r = connect(fd, sa, sl);
70 	if (!r || errno == EINPROGRESS) return 0;
71 	close(fd);
72 	pfd->fd = -1;
73 	return -1;
74 }
75 
step_mh(struct msghdr * mh,size_t n)76 static void step_mh(struct msghdr *mh, size_t n)
77 {
78 	/* Adjust iovec in msghdr to skip first n bytes. */
79 	while (mh->msg_iovlen && n >= mh->msg_iov->iov_len) {
80 		n -= mh->msg_iov->iov_len;
81 		mh->msg_iov++;
82 		mh->msg_iovlen--;
83 	}
84 	if (!mh->msg_iovlen) return;
85 	mh->msg_iov->iov_base = (char *)mh->msg_iov->iov_base + n;
86 	mh->msg_iov->iov_len -= n;
87 }
88 
89 // equal to answer buffer size
90 #define BPBUF_SIZE 4800
91 
92 /* Internal contract for __res_msend[_rc]: asize must be >=512, nqueries
93  * must be sufficiently small to be safe as VLA size. In practice it's
94  * either 1 or 2, anyway. */
95 
__res_msend_rc(int nqueries,const unsigned char * const * queries,const int * qlens,unsigned char * const * answers,int * alens,int asize,const struct resolvconf * conf)96 int __res_msend_rc(int nqueries, const unsigned char *const *queries,
97 	const int *qlens, unsigned char *const *answers, int *alens, int asize,
98 	const struct resolvconf *conf)
99 {
100 	return res_msend_rc_ext(0, nqueries, queries, qlens, answers, alens, asize, conf, NULL);
101 }
102 
res_msend_rc_ext(int netid,int nqueries,const unsigned char * const * queries,const int * qlens,unsigned char * const * answers,int * alens,int asize,const struct resolvconf * conf,int * dns_errno)103 int res_msend_rc_ext(int netid, int nqueries, const unsigned char *const *queries,
104 	const int *qlens, unsigned char *const *answers, int *alens, int asize,
105 	const struct resolvconf *conf, int *dns_errno)
106 {
107 	int fd;
108 	int timeout, attempts, retry_interval, servfail_retry;
109 	union {
110 		struct sockaddr_in sin;
111 		struct sockaddr_in6 sin6;
112 	} sa = {0}, ns[MAXNS] = {{0}};
113 	socklen_t sl = sizeof sa.sin;
114 	int nns = 0;
115 	int family = AF_INET;
116 	int rlen;
117 	int next;
118 	int i, j;
119 	int cs;
120 	struct pollfd pfd[nqueries+2];
121 	int qpos[nqueries], apos[nqueries], retry[nqueries];
122 	unsigned char alen_buf[nqueries][2];
123 	int r;
124 	unsigned long t0, t1, t2, temp_t;
125 	uint8_t nres, end_query;
126 	int blens[2] = {0};
127 	unsigned char *bp[2] = { NULL, NULL };
128 #if OHOS_DNS_PROXY_BY_NETSYS
129     int retry_count = 0;
130 	int retry_limit;
131 #endif
132 
133 	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
134 
135 	timeout = 1000*conf->timeout;
136 	attempts = conf->attempts;
137 
138 	for (nns=0; nns<conf->nns; nns++) {
139 		const struct address *iplit = &conf->ns[nns];
140 		if (iplit->family == AF_INET) {
141 			memcpy(&ns[nns].sin.sin_addr, iplit->addr, 4);
142 			ns[nns].sin.sin_port = htons(53);
143 			ns[nns].sin.sin_family = AF_INET;
144 		} else {
145 			sl = sizeof sa.sin6;
146 			memcpy(&ns[nns].sin6.sin6_addr, iplit->addr, 16);
147 			ns[nns].sin6.sin6_port = htons(53);
148 			ns[nns].sin6.sin6_scope_id = iplit->scopeid;
149 			ns[nns].sin6.sin6_family = family = AF_INET6;
150 		}
151 	}
152 
153 	/* Get local address and open/bind a socket */
154 	fd = socket(family, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
155 #ifndef __LITEOS__
156 	if (fd < 0) {
157 		MUSL_LOGE("%{public}s: %{public}d: create UDP socket failed, errno id: %{public}d",
158 			__func__, __LINE__, errno);
159 	}
160 #endif
161 
162 	/* Handle case where system lacks IPv6 support */
163 	if (fd < 0 && family == AF_INET6 && errno == EAFNOSUPPORT) {
164 		for (i=0; i<nns && conf->ns[nns].family == AF_INET6; i++);
165 		if (i==nns) {
166 #ifndef __LITEOS__
167 			MUSL_LOGE("%{public}s: %{public}d: system lacks IPv6 support: %{public}d",
168 				__func__, __LINE__, errno);
169 #endif
170 			pthread_setcancelstate(cs, 0);
171 			return DNS_FAIL_REASON_LACK_V6_SUPPORT;
172 		}
173 		fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
174 		family = AF_INET;
175 		sl = sizeof sa.sin;
176 	}
177 
178 #ifndef __LITEOS__
179 	/**
180 	 * Todo FwmarkClient::BindSocket
181 	*/
182 	if (netid > 0) {
183 		res_bind_socket(fd, netid);
184 	}
185 #endif
186 
187 	/* Convert any IPv4 addresses in a mixed environment to v4-mapped */
188 	if (fd >= 0 && family == AF_INET6) {
189 		setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &(int){0}, sizeof 0);
190 		for (i=0; i<nns; i++) {
191 			if (ns[i].sin.sin_family != AF_INET) continue;
192 			memcpy(ns[i].sin6.sin6_addr.s6_addr+12,
193 				&ns[i].sin.sin_addr, 4);
194 			memcpy(ns[i].sin6.sin6_addr.s6_addr,
195 				"\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
196 			ns[i].sin6.sin6_family = AF_INET6;
197 			ns[i].sin6.sin6_flowinfo = 0;
198 			ns[i].sin6.sin6_scope_id = 0;
199 		}
200 	}
201 
202 	sa.sin.sin_family = family;
203 	if (fd < 0 || bind(fd, (void *)&sa, sl) < 0) {
204 #ifndef __LITEOS__
205 		MUSL_LOGE("%{public}s: %{public}d: AF_INET fd failed or bind failed, fd: %{public}d, errno: %{public}d",
206 			__func__, __LINE__, fd, errno);
207 #endif
208 		if (fd >= 0) close(fd);
209 		pthread_setcancelstate(cs, 0);
210 		return DNS_FAIL_REASON_CREATE_UDP_SOCKET_FAILED;
211 	}
212 
213 	/* Past this point, there are no errors. Each individual query will
214 	 * yield either no reply (indicated by zero length) or an answer
215 	 * packet which is up to the caller to interpret. */
216 
217 	for (i=0; i<nqueries; i++) pfd[i].fd = -1;
218 	pfd[nqueries].fd = fd;
219 	pfd[nqueries].events = POLLIN;
220 	pfd[nqueries+1].fd = -2;
221 
222 	pthread_cleanup_push(cleanup, pfd);
223 	pthread_setcancelstate(cs, 0);
224 
225 	memset(alens, 0, sizeof *alens * nqueries);
226 
227 	retry_interval = timeout / attempts;
228 	next = 0;
229 	t0 = t2 = mtime();
230 	t1 = t2 - retry_interval;
231 	temp_t = 0;
232 	nres = 0;
233 	end_query = 0;
234 
235 	for (; t2-t0 < timeout; t2=mtime()) {
236 #if OHOS_DNS_PROXY_BY_NETSYS
237         retry_count++;
238 #endif
239 		/* This is the loop exit condition: that all queries
240 		 * have an accepted answer. */
241 		for (i=0; i<nqueries && alens[i]>0; i++);
242 		if (i==nqueries) break;
243 
244 		/* if the temp_t * 2 timeout, return result immediately. */
245 		if (end_query) {
246 			goto out;
247 		}
248 
249 		if (t2-t1 >= retry_interval) {
250 			/* if the first query round timeout, determine whether
251 			 * to return based on the num of answers. */
252 			if (nres) {
253 #ifndef __LITEOS__
254 				MUSL_LOGE("%{public}s: %{public}d: first round timeout and had answer", __func__, __LINE__);
255 #endif
256 				goto out;
257 			}
258 			/* Query all configured namservers in parallel */
259 			for (i=0; i<nqueries; i++) {
260 				retry[i] = 0;
261 				if (!alens[i]) {
262 #if OHOS_DNS_PROXY_BY_NETSYS
263                     /* First time only use non public ns, public ns is used after first query failed */
264                     if (retry_count <= 1 && conf->non_public > 0) {
265 						retry_limit = conf->non_public;
266 					} else {
267 						retry_limit = nns;
268 					}
269 					for (j=0; j<retry_limit; j++) {
270 #else
271                     for (j=0; j<nns; j++) {
272 #endif
273 						if (sendto(fd, queries[i], qlens[i], MSG_NOSIGNAL, (void *)&ns[j], sl) == -1) {
274 							int errno_code = errno;
275 #ifndef __LITEOS__
276 							MUSL_LOGE("%{public}s: %{public}d: sendto failed, errno id: %{public}d",
277 								__func__, __LINE__, errno_code);
278 #endif
279 							if (dns_errno) {
280 								*dns_errno = errno_code;
281 							}
282 						}
283 					}
284 				}
285 			}
286 			t1 = t2;
287 			servfail_retry = 2 * nqueries;
288 		}
289 
290 		unsigned long remaining_time = t1 + retry_interval - t2;
291         if (nres) {
292             if (!temp_t) {
293                 temp_t = t2 - t1;
294             }
295             if (temp_t >= retry_interval / 2 && temp_t < retry_interval) {
296                 remaining_time = retry_interval - temp_t;
297             } else if (temp_t < retry_interval / 2 && temp_t > 0) {
298                 remaining_time = temp_t;
299                 end_query = 1;
300             } else {
301                 goto out;
302             }
303         }
304 
305 		/* Wait for a response, or until time to retry */
306 		if (poll(pfd, nqueries+1, remaining_time) <= 0) continue;
307 
308 		while (next < nqueries) {
309 			struct msghdr mh = {
310 				.msg_name = (void *)&sa,
311 				.msg_namelen = sl,
312 				.msg_iovlen = 1,
313 				.msg_iov = (struct iovec []){
314 					{ .iov_base = (void *)answers[next],
315 					  .iov_len = asize }
316 				}
317 			};
318 			rlen = recvmsg(fd, &mh, 0);
319 			if (rlen < 0) {
320 #ifndef __LITEOS__
321 				if (errno != EAGAIN) {
322 					MUSL_LOGE("%{public}s: %{public}d: recvmsg failed, errno id: %{public}d",
323 					__func__, __LINE__, errno);
324 				}
325 #endif
326 				break;
327 			}
328 
329 			/* Ignore non-identifiable packets */
330 			if (rlen < 4) continue;
331 
332 			/* Ignore replies from addresses we didn't send to */
333 			switch (sa.sin.sin_family) {
334 				// for ipv4 response, need to compare family, port and address
335 				case AF_INET:
336 					for (j = 0; j < nns; j++) {
337 						if (ns[j].sin.sin_family == AF_INET && ns[j].sin.sin_port == sa.sin.sin_port && (
338 							ns[j].sin.sin_addr.s_addr == INADDR_ANY ||
339 							ns[j].sin.sin_addr.s_addr == sa.sin.sin_addr.s_addr)) {
340 							break;
341 						}
342 					}
343 					break;
344 				// for ipv6 response, need to compare family, port and address, flowinfo and scopeid is not necessary
345 				case AF_INET6:
346 					for (j = 0; j < nns; j++) {
347 						if (ns[j].sin6.sin6_family == AF_INET6 &&
348 							ns[j].sin6.sin6_port == sa.sin6.sin6_port && (
349 							IN6_IS_ADDR_UNSPECIFIED(&ns[j].sin6.sin6_addr) ||
350 							IN6_ARE_ADDR_EQUAL(&ns[j].sin6.sin6_addr, &sa.sin6.sin6_addr))) {
351 							break;
352 						}
353 					}
354 					break;
355 				default:
356 					j = nns;
357 					break;
358 			}
359 			if (j==nns) {
360 #ifndef __LITEOS__
361 				MUSL_LOGE("%{public}s: %{public}d: replies from wrong addresses, ignore it", __func__, __LINE__);
362 #endif
363 				continue;
364 			}
365 
366 			/* Find which query this answer goes with, if any */
367 			for (i=next; i<nqueries && (
368 				answers[next][0] != queries[i][0] ||
369 				answers[next][1] != queries[i][1] ); i++);
370 			if (i==nqueries) continue;
371 			if (alens[i]) continue;
372 
373 			/* Only accept positive or negative responses;
374 			 * retry immediately on server failure, and ignore
375 			 * all other codes such as refusal. */
376 			switch (answers[next][3] & 15) {
377 			case 0:
378 				break;
379 			case 3:
380 				if (retry[i] + 1 < nns) {
381 					retry[i]++;
382 					continue;
383 				} else {
384 #ifndef __LITEOS__
385 					MUSL_LOGE("%{public}s: %{public}d: retry failed for %{public}d nameservers, and get no such name",
386 						__func__, __LINE__, retry[i]);
387 #endif
388 					break;
389 				}
390 			case 2:
391 				if (servfail_retry && servfail_retry--)
392 					sendto(fd, queries[i],
393 						qlens[i], MSG_NOSIGNAL,
394 						(void *)&ns[j], sl);
395 			default:
396 				continue;
397 			}
398 
399 			/* Store answer in the right slot, or update next
400 			 * available temp slot if it's already in place. */
401 			alens[i] = rlen;
402 			nres++;
403 			if (i == next)
404 				for (; next<nqueries && alens[next]; next++);
405 			else
406 				memcpy(answers[i], answers[next], rlen);
407 
408 			/* If answer is truncated (TC bit), before fallback to TCP, restore the UDP answer*/
409 			if ((answers[i][2] & 2) || (mh.msg_flags & MSG_TRUNC)) {
410 				if (bp[i] == NULL) {
411 					bp[i] = calloc(1, sizeof(unsigned char) * BPBUF_SIZE);
412 					/* If fail to calloc backup buffer, only use TCP even if it fails*/
413 					if (bp[i] != NULL) {
414 						blens[i] = rlen;
415 						memcpy(bp[i], answers[i], rlen);
416 					}
417 				}
418 			}
419 
420 			/* Ignore further UDP if all slots full or TCP-mode */
421 			if (next == nqueries) pfd[nqueries].events = 0;
422 
423 			/* If answer is truncated (TC bit), fallback to TCP */
424 			if ((answers[i][2] & 2) || (mh.msg_flags & MSG_TRUNC)) {
425 #ifndef __LITEOS__
426 				MUSL_LOGE("%{public}s: %{public}d: fallback to TCP, msg_flags: %{public}d",
427 					__func__, __LINE__, mh.msg_flags);
428 #endif
429 				alens[i] = -1;
430 				nres--;
431 				if (dns_errno) {
432 					*dns_errno = FALLBACK_TCP_QUERY;
433 				}
434 				pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0);
435 				r = start_tcp(pfd+i, family, ns+j, sl, queries[i], qlens[i], netid);
436 				pthread_setcancelstate(cs, 0);
437 				if (r >= 0) {
438 					qpos[i] = r;
439 					apos[i] = 0;
440 				}
441 				continue;
442 			}
443 		}
444 
445 		for (i=0; i<nqueries; i++) if (pfd[i].revents & POLLOUT) {
446 			struct msghdr mh = {
447 				.msg_iovlen = 2,
448 				.msg_iov = (struct iovec [2]){
449 					{ .iov_base = (uint8_t[]){ qlens[i]>>8, qlens[i] }, .iov_len = 2 },
450 					{ .iov_base = (void *)queries[i], .iov_len = qlens[i] } }
451 			};
452 			step_mh(&mh, qpos[i]);
453 			r = sendmsg(pfd[i].fd, &mh, MSG_NOSIGNAL);
454 			if (r < 0) goto out;
455 			qpos[i] += r;
456 			if (qpos[i] == qlens[i]+2)
457 				pfd[i].events = POLLIN;
458 		}
459 
460 		for (i=0; i<nqueries; i++) if (pfd[i].revents & POLLIN) {
461 			struct msghdr mh = {
462 				.msg_iovlen = 2,
463 				.msg_iov = (struct iovec [2]){
464 					{ .iov_base = alen_buf[i], .iov_len = 2 },
465 					{ .iov_base = answers[i], .iov_len = asize } }
466 			};
467 			step_mh(&mh, apos[i]);
468 			r = recvmsg(pfd[i].fd, &mh, 0);
469 			if (r <= 0) goto out;
470 			apos[i] += r;
471 			if (apos[i] < 2) continue;
472 			int alen = alen_buf[i][0]*256 + alen_buf[i][1];
473 			if (alen < 13) goto out;
474 			if (apos[i] < alen+2 && apos[i] < asize+2)
475 				continue;
476 			int rcode = answers[i][3] & 15;
477 			if (rcode != 0 && rcode != 3)
478 				goto out;
479 
480 			/* Storing the length here commits the accepted answer.
481 			 * Immediately close TCP socket so as not to consume
482 			 * resources we no longer need. */
483 			alens[i] = alen;
484 			nres++;
485 			__syscall(SYS_close, pfd[i].fd);
486 			pfd[i].fd = -1;
487 		}
488 	}
489 out:
490 	pthread_cleanup_pop(1);
491 	/* Disregard any incomplete TCP results and try to reuse UDP */
492 	for (i = 0; i < nqueries; i++) {
493 		if (alens[i] < 0) {
494 			if (blens[i] != 0 && bp[i] != NULL) {
495 				alens[i] = blens[i];
496 				memcpy(answers[i], bp[i], blens[i]);
497 #ifndef __LITEOS__
498 				MUSL_LOGE("%{public}s: %{public}d: rollback to UDP", __func__, __LINE__);
499 #endif
500 			} else {
501 				alens[i] = 0;
502 			}
503 		}
504 		if (bp[i] != NULL) {
505 			free(bp[i]);
506 		}
507 	}
508 
509 	return 0;
510 }
511 
512 int __res_msend(int nqueries, const unsigned char *const *queries,
513 	const int *qlens, unsigned char *const *answers, int *alens, int asize)
514 {
515 	struct resolvconf conf;
516 	if (__get_resolv_conf(&conf, 0, 0) < 0) return -1;
517 	return __res_msend_rc(nqueries, queries, qlens, answers, alens, asize, &conf);
518 }
519