1 #include <sys/socket.h>
2 #include <netinet/in.h>
3 #include <netinet/tcp.h>
14 #include "stdio_impl.h"
18 static void cleanup(void *p)
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);
25 static unsigned long mtime()
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;
34 static int start_tcp(struct pollfd *pfd, int family, const void *sa, socklen_t sl, const unsigned char *q, int ql)
37 .msg_name = (void *)sa,
40 .msg_iov = (struct iovec [2]){
41 { .iov_base = (uint8_t[]){ ql>>8, ql }, .iov_len = 2 },
42 { .iov_base = (void *)q, .iov_len = ql } }
45 int fd = socket(family, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
47 pfd->events = POLLOUT;
48 if (!setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT,
49 &(int){1}, sizeof(int))) {
50 r = sendmsg(fd, &mh, MSG_FASTOPEN|MSG_NOSIGNAL);
51 if (r == ql+2) pfd->events = POLLIN;
53 if (errno == EINPROGRESS) return 0;
55 r = connect(fd, sa, sl);
56 if (!r || errno == EINPROGRESS) return 0;
62 static void step_mh(struct msghdr *mh, size_t n)
64 /* Adjust iovec in msghdr to skip first n bytes. */
65 while (mh->msg_iovlen && n >= mh->msg_iov->iov_len) {
66 n -= mh->msg_iov->iov_len;
70 if (!mh->msg_iovlen) return;
71 mh->msg_iov->iov_base = (char *)mh->msg_iov->iov_base + n;
72 mh->msg_iov->iov_len -= n;
75 /* Internal contract for __res_msend[_rc]: asize must be >=512, nqueries
76 * must be sufficiently small to be safe as VLA size. In practice it's
77 * either 1 or 2, anyway. */
79 int __res_msend_rc(int nqueries, const unsigned char *const *queries,
80 const int *qlens, unsigned char *const *answers, int *alens, int asize,
81 const struct resolvconf *conf)
84 int timeout, attempts, retry_interval, servfail_retry;
86 struct sockaddr_in sin;
87 struct sockaddr_in6 sin6;
88 } sa = {0}, ns[MAXNS] = {{0}};
89 socklen_t sl = sizeof sa.sin;
96 struct pollfd pfd[nqueries+2];
97 int qpos[nqueries], apos[nqueries];
98 unsigned char alen_buf[nqueries][2];
100 unsigned long t0, t1, t2;
102 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
104 timeout = 1000*conf->timeout;
105 attempts = conf->attempts;
107 for (nns=0; nns<conf->nns; nns++) {
108 const struct address *iplit = &conf->ns[nns];
109 if (iplit->family == AF_INET) {
110 memcpy(&ns[nns].sin.sin_addr, iplit->addr, 4);
111 ns[nns].sin.sin_port = htons(53);
112 ns[nns].sin.sin_family = AF_INET;
115 memcpy(&ns[nns].sin6.sin6_addr, iplit->addr, 16);
116 ns[nns].sin6.sin6_port = htons(53);
117 ns[nns].sin6.sin6_scope_id = iplit->scopeid;
118 ns[nns].sin6.sin6_family = family = AF_INET6;
122 /* Get local address and open/bind a socket */
123 fd = socket(family, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
125 /* Handle case where system lacks IPv6 support */
126 if (fd < 0 && family == AF_INET6 && errno == EAFNOSUPPORT) {
127 for (i=0; i<nns && conf->ns[nns].family == AF_INET6; i++);
129 pthread_setcancelstate(cs, 0);
132 fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
136 sa.sin.sin_family = family;
137 if (fd < 0 || bind(fd, (void *)&sa, sl) < 0) {
138 if (fd >= 0) close(fd);
139 pthread_setcancelstate(cs, 0);
143 /* Past this point, there are no errors. Each individual query will
144 * yield either no reply (indicated by zero length) or an answer
145 * packet which is up to the caller to interpret. */
147 for (i=0; i<nqueries; i++) pfd[i].fd = -1;
148 pfd[nqueries].fd = fd;
149 pfd[nqueries].events = POLLIN;
150 pfd[nqueries+1].fd = -2;
152 pthread_cleanup_push(cleanup, pfd);
153 pthread_setcancelstate(cs, 0);
155 /* Convert any IPv4 addresses in a mixed environment to v4-mapped */
156 if (family == AF_INET6) {
157 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &(int){0}, sizeof 0);
158 for (i=0; i<nns; i++) {
159 if (ns[i].sin.sin_family != AF_INET) continue;
160 memcpy(ns[i].sin6.sin6_addr.s6_addr+12,
161 &ns[i].sin.sin_addr, 4);
162 memcpy(ns[i].sin6.sin6_addr.s6_addr,
163 "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
164 ns[i].sin6.sin6_family = AF_INET6;
165 ns[i].sin6.sin6_flowinfo = 0;
166 ns[i].sin6.sin6_scope_id = 0;
170 memset(alens, 0, sizeof *alens * nqueries);
172 retry_interval = timeout / attempts;
175 t1 = t2 - retry_interval;
177 for (; t2-t0 < timeout; t2=mtime()) {
178 /* This is the loop exit condition: that all queries
179 * have an accepted answer. */
180 for (i=0; i<nqueries && alens[i]>0; i++);
181 if (i==nqueries) break;
183 if (t2-t1 >= retry_interval) {
184 /* Query all configured namservers in parallel */
185 for (i=0; i<nqueries; i++)
187 for (j=0; j<nns; j++)
188 sendto(fd, queries[i],
189 qlens[i], MSG_NOSIGNAL,
192 servfail_retry = 2 * nqueries;
195 /* Wait for a response, or until time to retry */
196 if (poll(pfd, nqueries+1, t1+retry_interval-t2) <= 0) continue;
198 while (next < nqueries) {
200 .msg_name = (void *)&sa,
203 .msg_iov = (struct iovec []){
204 { .iov_base = (void *)answers[next],
208 rlen = recvmsg(fd, &mh, 0);
211 /* Ignore non-identifiable packets */
212 if (rlen < 4) continue;
214 /* Ignore replies from addresses we didn't send to */
215 for (j=0; j<nns && memcmp(ns+j, &sa, sl); j++);
216 if (j==nns) continue;
218 /* Find which query this answer goes with, if any */
219 for (i=next; i<nqueries && (
220 answers[next][0] != queries[i][0] ||
221 answers[next][1] != queries[i][1] ); i++);
222 if (i==nqueries) continue;
223 if (alens[i]) continue;
225 /* Only accept positive or negative responses;
226 * retry immediately on server failure, and ignore
227 * all other codes such as refusal. */
228 switch (answers[next][3] & 15) {
233 if (servfail_retry && servfail_retry--)
234 sendto(fd, queries[i],
235 qlens[i], MSG_NOSIGNAL,
241 /* Store answer in the right slot, or update next
242 * available temp slot if it's already in place. */
245 for (; next<nqueries && alens[next]; next++);
247 memcpy(answers[i], answers[next], rlen);
249 /* Ignore further UDP if all slots full or TCP-mode */
250 if (next == nqueries) pfd[nqueries].events = 0;
252 /* If answer is truncated (TC bit), fallback to TCP */
253 if ((answers[i][2] & 2) || (mh.msg_flags & MSG_TRUNC)) {
255 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0);
256 r = start_tcp(pfd+i, family, ns+j, sl, queries[i], qlens[i]);
257 pthread_setcancelstate(cs, 0);
266 for (i=0; i<nqueries; i++) if (pfd[i].revents & POLLOUT) {
269 .msg_iov = (struct iovec [2]){
270 { .iov_base = (uint8_t[]){ qlens[i]>>8, qlens[i] }, .iov_len = 2 },
271 { .iov_base = (void *)queries[i], .iov_len = qlens[i] } }
273 step_mh(&mh, qpos[i]);
274 r = sendmsg(pfd[i].fd, &mh, MSG_NOSIGNAL);
277 if (qpos[i] == qlens[i]+2)
278 pfd[i].events = POLLIN;
281 for (i=0; i<nqueries; i++) if (pfd[i].revents & POLLIN) {
284 .msg_iov = (struct iovec [2]){
285 { .iov_base = alen_buf[i], .iov_len = 2 },
286 { .iov_base = answers[i], .iov_len = asize } }
288 step_mh(&mh, apos[i]);
289 r = recvmsg(pfd[i].fd, &mh, 0);
292 if (apos[i] < 2) continue;
293 int alen = alen_buf[i][0]*256 + alen_buf[i][1];
294 if (alen < 13) goto out;
295 if (apos[i] < alen+2 && apos[i] < asize+2)
297 int rcode = answers[i][3] & 15;
298 if (rcode != 0 && rcode != 3)
301 /* Storing the length here commits the accepted answer.
302 * Immediately close TCP socket so as not to consume
303 * resources we no longer need. */
305 __syscall(SYS_close, pfd[i].fd);
310 pthread_cleanup_pop(1);
312 /* Disregard any incomplete TCP results */
313 for (i=0; i<nqueries; i++) if (alens[i]<0) alens[i] = 0;
318 int __res_msend(int nqueries, const unsigned char *const *queries,
319 const int *qlens, unsigned char *const *answers, int *alens, int asize)
321 struct resolvconf conf;
322 if (__get_resolv_conf(&conf, 0, 0) < 0) return -1;
323 return __res_msend_rc(nqueries, queries, qlens, answers, alens, asize, &conf);