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