adapt res_msend DNS query core for working with multiple sockets
[musl] / src / network / res_msend.c
index de7f615..98e6d8a 100644 (file)
@@ -16,7 +16,9 @@
 
 static void cleanup(void *p)
 {
-       __syscall(SYS_close, (intptr_t)p);
+       struct pollfd *pfd = p;
+       for (int i=0; pfd[i].fd >= -1; i++)
+               if (pfd[i].fd >= 0) __syscall(SYS_close, pfd[i].fd);
 }
 
 static unsigned long mtime()
@@ -44,7 +46,7 @@ int __res_msend_rc(int nqueries, const unsigned char *const *queries,
        int next;
        int i, j;
        int cs;
-       struct pollfd pfd;
+       struct pollfd pfd[nqueries+2];
        unsigned long t0, t1, t2;
 
        pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
@@ -68,21 +70,36 @@ int __res_msend_rc(int nqueries, const unsigned char *const *queries,
        }
 
        /* 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) {
+               for (i=0; i<nns && conf->ns[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;
+       }
+       sa.sin.sin_family = family;
+       if (fd < 0 || bind(fd, (void *)&sa, sl) < 0) {
+               if (fd >= 0) close(fd);
+               pthread_setcancelstate(cs, 0);
+               return -1;
        }
-       if (fd < 0 || bind(fd, (void *)&sa, sl) < 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);
+       for (i=0; i<nqueries; i++) pfd[i].fd = -1;
+       pfd[nqueries].fd = fd;
+       pfd[nqueries].events = POLLIN;
+       pfd[nqueries+1].fd = -2;
+
+       pthread_cleanup_push(cleanup, pfd);
        pthread_setcancelstate(cs, 0);
 
        /* Convert any IPv4 addresses in a mixed environment to v4-mapped */
@@ -102,8 +119,6 @@ int __res_msend_rc(int nqueries, const unsigned char *const *queries,
 
        memset(alens, 0, sizeof *alens * nqueries);
 
-       pfd.fd = fd;
-       pfd.events = POLLIN;
        retry_interval = timeout / attempts;
        next = 0;
        t0 = t2 = mtime();
@@ -123,7 +138,7 @@ int __res_msend_rc(int nqueries, const unsigned char *const *queries,
                }
 
                /* Wait for a response, or until time to retry */
-               if (poll(&pfd, 1, t1+retry_interval-t2) <= 0) continue;
+               if (poll(pfd, nqueries+1, t1+retry_interval-t2) <= 0) continue;
 
                while ((rlen = recvfrom(fd, answers[next], asize, 0,
                  (void *)&sa, (socklen_t[1]){sl})) >= 0) {