#include #include #include #include #include #include #include #include #include #include #include #include #include "stdio_impl.h" #include "syscall.h" #include "lookup.h" static void cleanup(void *p) { __syscall(SYS_close, (intptr_t)p); } static unsigned long mtime() { struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); return (unsigned long)ts.tv_sec * 1000 + ts.tv_nsec / 1000000; } 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; 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; 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 */ sa.sin.sin_family = family; 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) { fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); family = AF_INET; } if (fd < 0 || bind(fd, (void *)&sa, sl) < 0) { if (fd >= 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. */ pthread_cleanup_push(cleanup, (void *)(intptr_t)fd); pthread_setcancelstate(cs, 0); /* Convert any IPv4 addresses in a mixed environment to v4-mapped */ if (family == AF_INET6) { setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &(int){0}, sizeof 0); for (i=0; i= retry_interval) { /* Query all configured namservers in parallel */ for (i=0; i= 0) { /* Ignore non-identifiable packets */ if (rlen < 4) continue; /* Ignore replies from addresses we didn't send to */ for (j=0; j