adapt res_msend DNS query core for working with multiple sockets
[musl] / src / network / res_msend.c
1 #include <sys/socket.h>
2 #include <netinet/in.h>
3 #include <netdb.h>
4 #include <arpa/inet.h>
5 #include <stdint.h>
6 #include <string.h>
7 #include <poll.h>
8 #include <time.h>
9 #include <ctype.h>
10 #include <unistd.h>
11 #include <errno.h>
12 #include <pthread.h>
13 #include "stdio_impl.h"
14 #include "syscall.h"
15 #include "lookup.h"
16
17 static void cleanup(void *p)
18 {
19         struct pollfd *pfd = p;
20         for (int i=0; pfd[i].fd >= -1; i++)
21                 if (pfd[i].fd >= 0) __syscall(SYS_close, pfd[i].fd);
22 }
23
24 static unsigned long mtime()
25 {
26         struct timespec ts;
27         clock_gettime(CLOCK_REALTIME, &ts);
28         return (unsigned long)ts.tv_sec * 1000
29                 + ts.tv_nsec / 1000000;
30 }
31
32 int __res_msend_rc(int nqueries, const unsigned char *const *queries,
33         const int *qlens, unsigned char *const *answers, int *alens, int asize,
34         const struct resolvconf *conf)
35 {
36         int fd;
37         int timeout, attempts, retry_interval, servfail_retry;
38         union {
39                 struct sockaddr_in sin;
40                 struct sockaddr_in6 sin6;
41         } sa = {0}, ns[MAXNS] = {{0}};
42         socklen_t sl = sizeof sa.sin;
43         int nns = 0;
44         int family = AF_INET;
45         int rlen;
46         int next;
47         int i, j;
48         int cs;
49         struct pollfd pfd[nqueries+2];
50         unsigned long t0, t1, t2;
51
52         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
53
54         timeout = 1000*conf->timeout;
55         attempts = conf->attempts;
56
57         for (nns=0; nns<conf->nns; nns++) {
58                 const struct address *iplit = &conf->ns[nns];
59                 if (iplit->family == AF_INET) {
60                         memcpy(&ns[nns].sin.sin_addr, iplit->addr, 4);
61                         ns[nns].sin.sin_port = htons(53);
62                         ns[nns].sin.sin_family = AF_INET;
63                 } else {
64                         sl = sizeof sa.sin6;
65                         memcpy(&ns[nns].sin6.sin6_addr, iplit->addr, 16);
66                         ns[nns].sin6.sin6_port = htons(53);
67                         ns[nns].sin6.sin6_scope_id = iplit->scopeid;
68                         ns[nns].sin6.sin6_family = family = AF_INET6;
69                 }
70         }
71
72         /* Get local address and open/bind a socket */
73         fd = socket(family, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
74
75         /* Handle case where system lacks IPv6 support */
76         if (fd < 0 && family == AF_INET6 && errno == EAFNOSUPPORT) {
77                 for (i=0; i<nns && conf->ns[nns].family == AF_INET6; i++);
78                 if (i==nns) {
79                         pthread_setcancelstate(cs, 0);
80                         return -1;
81                 }
82                 fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
83                 family = AF_INET;
84                 sl = sizeof sa.sin;
85         }
86         sa.sin.sin_family = family;
87         if (fd < 0 || bind(fd, (void *)&sa, sl) < 0) {
88                 if (fd >= 0) close(fd);
89                 pthread_setcancelstate(cs, 0);
90                 return -1;
91         }
92
93         /* Past this point, there are no errors. Each individual query will
94          * yield either no reply (indicated by zero length) or an answer
95          * packet which is up to the caller to interpret. */
96
97         for (i=0; i<nqueries; i++) pfd[i].fd = -1;
98         pfd[nqueries].fd = fd;
99         pfd[nqueries].events = POLLIN;
100         pfd[nqueries+1].fd = -2;
101
102         pthread_cleanup_push(cleanup, pfd);
103         pthread_setcancelstate(cs, 0);
104
105         /* Convert any IPv4 addresses in a mixed environment to v4-mapped */
106         if (family == AF_INET6) {
107                 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &(int){0}, sizeof 0);
108                 for (i=0; i<nns; i++) {
109                         if (ns[i].sin.sin_family != AF_INET) continue;
110                         memcpy(ns[i].sin6.sin6_addr.s6_addr+12,
111                                 &ns[i].sin.sin_addr, 4);
112                         memcpy(ns[i].sin6.sin6_addr.s6_addr,
113                                 "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
114                         ns[i].sin6.sin6_family = AF_INET6;
115                         ns[i].sin6.sin6_flowinfo = 0;
116                         ns[i].sin6.sin6_scope_id = 0;
117                 }
118         }
119
120         memset(alens, 0, sizeof *alens * nqueries);
121
122         retry_interval = timeout / attempts;
123         next = 0;
124         t0 = t2 = mtime();
125         t1 = t2 - retry_interval;
126
127         for (; t2-t0 < timeout; t2=mtime()) {
128                 if (t2-t1 >= retry_interval) {
129                         /* Query all configured namservers in parallel */
130                         for (i=0; i<nqueries; i++)
131                                 if (!alens[i])
132                                         for (j=0; j<nns; j++)
133                                                 sendto(fd, queries[i],
134                                                         qlens[i], MSG_NOSIGNAL,
135                                                         (void *)&ns[j], sl);
136                         t1 = t2;
137                         servfail_retry = 2 * nqueries;
138                 }
139
140                 /* Wait for a response, or until time to retry */
141                 if (poll(pfd, nqueries+1, t1+retry_interval-t2) <= 0) continue;
142
143                 while ((rlen = recvfrom(fd, answers[next], asize, 0,
144                   (void *)&sa, (socklen_t[1]){sl})) >= 0) {
145
146                         /* Ignore non-identifiable packets */
147                         if (rlen < 4) continue;
148
149                         /* Ignore replies from addresses we didn't send to */
150                         for (j=0; j<nns && memcmp(ns+j, &sa, sl); j++);
151                         if (j==nns) continue;
152
153                         /* Find which query this answer goes with, if any */
154                         for (i=next; i<nqueries && (
155                                 answers[next][0] != queries[i][0] ||
156                                 answers[next][1] != queries[i][1] ); i++);
157                         if (i==nqueries) continue;
158                         if (alens[i]) continue;
159
160                         /* Only accept positive or negative responses;
161                          * retry immediately on server failure, and ignore
162                          * all other codes such as refusal. */
163                         switch (answers[next][3] & 15) {
164                         case 0:
165                         case 3:
166                                 break;
167                         case 2:
168                                 if (servfail_retry && servfail_retry--)
169                                         sendto(fd, queries[i],
170                                                 qlens[i], MSG_NOSIGNAL,
171                                                 (void *)&ns[j], sl);
172                         default:
173                                 continue;
174                         }
175
176                         /* Store answer in the right slot, or update next
177                          * available temp slot if it's already in place. */
178                         alens[i] = rlen;
179                         if (i == next)
180                                 for (; next<nqueries && alens[next]; next++);
181                         else
182                                 memcpy(answers[i], answers[next], rlen);
183
184                         if (next == nqueries) goto out;
185                 }
186         }
187 out:
188         pthread_cleanup_pop(1);
189
190         return 0;
191 }
192
193 int __res_msend(int nqueries, const unsigned char *const *queries,
194         const int *qlens, unsigned char *const *answers, int *alens, int asize)
195 {
196         struct resolvconf conf;
197         if (__get_resolv_conf(&conf, 0, 0) < 0) return -1;
198         return __res_msend_rc(nqueries, queries, qlens, answers, alens, asize, &conf);
199 }