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