/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │ vi: set noet ft=c ts=8 sw=8 fenc=utf-8 :vi │ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" #include "libc/calls/struct/timespec.h" #include "libc/calls/weirdtypes.h" #include "libc/dce.h" #include "libc/errno.h" #include "libc/sock/sock.h" #include "libc/sock/sock.h" #include "libc/sock/struct/msghdr.h" #include "libc/sock/struct/pollfd.h" #include "libc/str/str.h" #include "libc/sysv/consts/af.h" #include "libc/sysv/consts/clock.h" #include "libc/sysv/consts/ipproto.h" #include "libc/sysv/consts/ipv6.h" #include "libc/sysv/consts/msg.h" #include "libc/sysv/consts/poll.h" #include "libc/sysv/consts/sock.h" #include "libc/sysv/consts/tcp.h" #include "libc/thread/thread.h" #include "lookup.internal.h" __static_yoink("musl_libc_notice"); static void cleanup(void *p) { struct pollfd *pfd = p; for (int i=0; pfd[i].fd >= -1; i++) if (pfd[i].fd >= 0) close(pfd[i].fd); } static unsigned long mtime() { struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0 && errno == ENOSYS) clock_gettime(CLOCK_REALTIME, &ts); return (unsigned long)ts.tv_sec * 1000 + ts.tv_nsec / 1000000; } static int start_tcp(struct pollfd *pfd, int family, const void *sa, socklen_t sl, const unsigned char *q, int ql) { struct msghdr mh = { .msg_name = (void *)sa, .msg_namelen = sl, .msg_iovlen = 2, .msg_iov = (struct iovec [2]){ { .iov_base = (uint8_t[]){ ql>>8, ql }, .iov_len = 2 }, { .iov_base = (void *)q, .iov_len = ql } } }; int r; int fd = socket(family, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); pfd->fd = fd; pfd->events = POLLOUT; if (IsLinux() && !setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, &(int){1}, sizeof(int))) { r = sendmsg(fd, &mh, MSG_FASTOPEN|MSG_NOSIGNAL); if (r == ql+2) pfd->events = POLLIN; if (r >= 0) return r; if (errno == EINPROGRESS) return 0; } r = connect(fd, sa, sl); if (!r || errno == EINPROGRESS) return 0; close(fd); pfd->fd = -1; return -1; } static void step_mh(struct msghdr *mh, size_t n) { /* Adjust iovec in msghdr to skip first n bytes. */ while (mh->msg_iovlen && n >= mh->msg_iov->iov_len) { n -= mh->msg_iov->iov_len; mh->msg_iov++; mh->msg_iovlen--; } if (!mh->msg_iovlen) return; mh->msg_iov->iov_base = (char *)mh->msg_iov->iov_base + n; mh->msg_iov->iov_len -= n; } /* Internal contract for __res_msend[_rc]: asize must be >=512, nqueries * must be sufficiently small to be safe as VLA size. In practice it's * either 1 or 2, anyway. */ int __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) { int fd; int timeout, attempts, retry_interval, servfail_retry = 0; union { struct sockaddr_in sin; struct sockaddr_in6 sin6; } sa = {0}, ns[MAXNS] = {0}; socklen_t sl = sizeof sa.sin; int nns = 0; int family = AF_INET; int rlen; int next; int i, j; int cs; struct pollfd pfd[nqueries+2]; int qpos[nqueries], apos[nqueries]; unsigned char alen_buf[nqueries][2]; int r; unsigned long t0, t1, t2; pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); timeout = 1000*conf->timeout; attempts = conf->attempts; for (nns=0; nnsnns; nns++) { const struct address *iplit = &conf->ns[nns]; if (iplit->family == AF_INET) { memcpy(&ns[nns].sin.sin_addr, iplit->addr, 4); ns[nns].sin.sin_port = htons(53); ns[nns].sin.sin_family = AF_INET; } else { sl = sizeof sa.sin6; memcpy(&ns[nns].sin6.sin6_addr, iplit->addr, 16); ns[nns].sin6.sin6_port = htons(53); ns[nns].sin6.sin6_scope_id = iplit->scopeid; ns[nns].sin6.sin6_family = family = AF_INET6; } } /* Get local address and open/bind a socket */ fd = socket(family, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); /* Handle case where system lacks IPv6 support */ if (fd < 0 && family == AF_INET6 && errno == EAFNOSUPPORT) { for (i=0; ins[nns].family == AF_INET6; i++); if (i==nns) { pthread_setcancelstate(cs, 0); return -1; } fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); family = AF_INET; sl = sizeof sa.sin; } /* Convert any IPv4 addresses in a mixed environment to v4-mapped */ if (fd >= 0 && family == AF_INET6) { setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &(int){0}, sizeof 0); for (i=0; i= 0) close(fd); pthread_setcancelstate(cs, 0); return -1; } /* Past this point, there are no errors. Each individual query will * yield either no reply (indicated by zero length) or an answer * packet which is up to the caller to interpret. */ for (i=0; i0; i++); if (i==nqueries) break; if (t2-t1 >= retry_interval) { /* Query all configured namservers in parallel */ for (i=0; i= 0) { qpos[i] = r; apos[i] = 0; } continue; } } for (i=0; i>8, qlens[i] }, .iov_len = 2 }, { .iov_base = (void *)queries[i], .iov_len = qlens[i] } } }; step_mh(&mh, qpos[i]); r = sendmsg(pfd[i].fd, &mh, MSG_NOSIGNAL); if (r < 0) goto out; qpos[i] += r; if (qpos[i] == qlens[i]+2) pfd[i].events = POLLIN; } } for (i=0; i