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