blob: 4c0ca1a61277464dc52e5ce25400001971d87f0b [file] [log] [blame]
Bernie Innocenti55864192018-08-30 04:05:20 +09001/* $NetBSD: res_send.c,v 1.9 2006/01/24 17:41:25 christos Exp $ */
2
3/*
4 * Copyright (c) 1985, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36/*
37 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
38 *
39 * Permission to use, copy, modify, and distribute this software for any
40 * purpose with or without fee is hereby granted, provided that the above
41 * copyright notice and this permission notice appear in all copies, and that
42 * the name of Digital Equipment Corporation not be used in advertising or
43 * publicity pertaining to distribution of the document or software without
44 * specific, written prior permission.
45 *
46 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
47 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
48 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
49 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
50 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
51 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
52 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
53 * SOFTWARE.
54 */
55
56/*
57 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
58 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
59 *
60 * Permission to use, copy, modify, and distribute this software for any
61 * purpose with or without fee is hereby granted, provided that the above
62 * copyright notice and this permission notice appear in all copies.
63 *
64 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
65 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
66 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
67 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
68 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
69 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
70 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
71 */
72
Bernie Innocenti55864192018-08-30 04:05:20 +090073/*
74 * Send query to name server and wait for reply.
75 */
76
Bernie Innocentie9ba09c2018-09-12 23:20:10 +090077constexpr bool kVerboseLogging = false;
78#define LOG_TAG "res_send"
79
Bernie Innocenti55864192018-08-30 04:05:20 +090080#include <sys/param.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090081#include <sys/socket.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090082#include <sys/time.h>
83#include <sys/types.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090084#include <sys/uio.h>
85
Bernie Innocenti55864192018-08-30 04:05:20 +090086#include <arpa/inet.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090087#include <arpa/nameser.h>
88#include <netinet/in.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090089
90#include <errno.h>
91#include <fcntl.h>
92#include <netdb.h>
93#include <poll.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090094#include <signal.h>
95#include <stdio.h>
96#include <stdlib.h>
97#include <string.h>
98#include <time.h>
99#include <unistd.h>
100
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900101#include <android-base/logging.h>
Bernie Innocenti55864192018-08-30 04:05:20 +0900102
Mike Yua46fae72018-11-01 20:07:00 +0800103#include <netdutils/Slice.h>
104#include "netd_resolv/DnsTlsDispatcher.h"
105#include "netd_resolv/DnsTlsTransport.h"
106#include "netd_resolv/PrivateDnsConfiguration.h"
Bernie Innocenti189eb502018-10-01 23:10:18 +0900107#include "netd_resolv/resolv.h"
108#include "netd_resolv/stats.h"
Bernie Innocenti2fd418e2018-08-30 12:04:03 +0900109#include "private/android_filesystem_config.h"
Bernie Innocenti189eb502018-10-01 23:10:18 +0900110#include "res_state_ext.h"
Bernie Innocenti2fd418e2018-08-30 12:04:03 +0900111#include "resolv_cache.h"
Bernie Innocenti2fd418e2018-08-30 12:04:03 +0900112#include "resolv_private.h"
Bernie Innocenti2fd418e2018-08-30 12:04:03 +0900113
Mike Yua46fae72018-11-01 20:07:00 +0800114// TODO: use the namespace something like android::netd_resolv for libnetd_resolv
115using namespace android::net;
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900116
117#define VLOG if (!kVerboseLogging) {} else LOG(INFO)
118
119#ifndef RESOLV_ALLOW_VERBOSE_LOGGING
120static_assert(kVerboseLogging == false,
121 "Verbose logging floods logs at high-rate and exposes privacy-sensitive information. "
122 "Do not enable in release builds.");
Bernie Innocenti55864192018-08-30 04:05:20 +0900123#endif
Bernie Innocenti2fd418e2018-08-30 12:04:03 +0900124
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900125#ifndef DEBUG
126#define Dprint(cond, args) /*empty*/
127#define DprintQ(cond, args, query, size) /*empty*/
128#else
129// TODO: convert to Android logging
130#define Dprint(cond, args) \
131 if (cond) { \
132 fprintf args; \
133 } else { \
134 }
135#define DprintQ(cond, args, query, size) \
136 if (cond) { \
137 fprintf args; \
138 res_pquery(statp, query, size, stdout); \
139 } else { \
140 }
141#endif // DEBUG
Bernie Innocenti55864192018-08-30 04:05:20 +0900142
Mike Yua46fae72018-11-01 20:07:00 +0800143typedef enum { res_goahead, res_nextns, res_modified, res_done, res_error } res_sendhookact;
144
145static DnsTlsDispatcher sDnsTlsDispatcher;
146
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900147static int get_salen(const struct sockaddr*);
148static struct sockaddr* get_nsaddr(res_state, size_t);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900149static int send_vc(res_state, struct __res_params* params, const u_char*, int, u_char*, int, int*,
150 int, time_t*, int*, int*);
151static int send_dg(res_state, struct __res_params* params, const u_char*, int, u_char*, int, int*,
152 int, int*, int*, time_t*, int*, int*);
153static void Aerror(const res_state, FILE*, const char*, int, const struct sockaddr*, int);
154static void Perror(const res_state, FILE*, const char*, int);
155static int sock_eq(struct sockaddr*, struct sockaddr*);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900156static int connect_with_timeout(int sock, const struct sockaddr* nsap, socklen_t salen,
157 const struct timespec timeout);
Bernie Innocenti55864192018-08-30 04:05:20 +0900158static int retrying_poll(const int sock, short events, const struct timespec* finish);
Mike Yua46fae72018-11-01 20:07:00 +0800159static res_sendhookact res_tls_send(res_state, const Slice query, const Slice answer, int* resplen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900160
161/* BIONIC-BEGIN: implement source port randomization */
Bernie Innocenti55864192018-08-30 04:05:20 +0900162
Bernie Innocentif89b3512018-08-30 07:34:37 +0900163// BEGIN: Code copied from ISC eventlib
164// TODO: move away from this code
165
166#define BILLION 1000000000
167
168static struct timespec evConsTime(time_t sec, long nsec) {
169 struct timespec x;
170
171 x.tv_sec = sec;
172 x.tv_nsec = nsec;
173 return (x);
174}
175
176static struct timespec evAddTime(struct timespec addend1, struct timespec addend2) {
177 struct timespec x;
178
179 x.tv_sec = addend1.tv_sec + addend2.tv_sec;
180 x.tv_nsec = addend1.tv_nsec + addend2.tv_nsec;
181 if (x.tv_nsec >= BILLION) {
182 x.tv_sec++;
183 x.tv_nsec -= BILLION;
184 }
185 return (x);
186}
187
188static struct timespec evSubTime(struct timespec minuend, struct timespec subtrahend) {
189 struct timespec x;
190
191 x.tv_sec = minuend.tv_sec - subtrahend.tv_sec;
192 if (minuend.tv_nsec >= subtrahend.tv_nsec)
193 x.tv_nsec = minuend.tv_nsec - subtrahend.tv_nsec;
194 else {
195 x.tv_nsec = BILLION - subtrahend.tv_nsec + minuend.tv_nsec;
196 x.tv_sec--;
197 }
198 return (x);
199}
200
201static int evCmpTime(struct timespec a, struct timespec b) {
202#define SGN(x) ((x) < 0 ? (-1) : (x) > 0 ? (1) : (0));
203 time_t s = a.tv_sec - b.tv_sec;
204 long n;
205
206 if (s != 0) return SGN(s);
207
208 n = a.tv_nsec - b.tv_nsec;
209 return SGN(n);
210}
211
Bernie Innocentif89b3512018-08-30 07:34:37 +0900212static struct timespec evNowTime(void) {
Bernie Innocentif89b3512018-08-30 07:34:37 +0900213 struct timespec tsnow;
Bernie Innocenti357339c2018-08-31 16:11:41 +0900214 clock_gettime(CLOCK_REALTIME, &tsnow);
215 return tsnow;
Bernie Innocentif89b3512018-08-30 07:34:37 +0900216}
217
218static struct iovec evConsIovec(void* buf, size_t cnt) {
219 struct iovec ret;
220
221 memset(&ret, 0xf5, sizeof ret);
222 ret.iov_base = buf;
223 ret.iov_len = cnt;
Bernie Innocenti357339c2018-08-31 16:11:41 +0900224 return ret;
Bernie Innocentif89b3512018-08-30 07:34:37 +0900225}
226
227// END: Code copied from ISC eventlib
228
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900229static int random_bind(int s, int family) {
nuccachene172a4e2018-10-23 17:10:58 +0800230 sockaddr_union u;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900231 int j;
232 socklen_t slen;
Bernie Innocenti55864192018-08-30 04:05:20 +0900233
234 /* clear all, this also sets the IP4/6 address to 'any' */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900235 memset(&u, 0, sizeof u);
Bernie Innocenti55864192018-08-30 04:05:20 +0900236
237 switch (family) {
238 case AF_INET:
239 u.sin.sin_family = family;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900240 slen = sizeof u.sin;
Bernie Innocenti55864192018-08-30 04:05:20 +0900241 break;
242 case AF_INET6:
243 u.sin6.sin6_family = family;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900244 slen = sizeof u.sin6;
Bernie Innocenti55864192018-08-30 04:05:20 +0900245 break;
246 default:
247 errno = EPROTO;
248 return -1;
249 }
250
251 /* first try to bind to a random source port a few times */
252 for (j = 0; j < 10; j++) {
253 /* find a random port between 1025 .. 65534 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900254 int port = 1025 + (res_randomid() % (65535 - 1025));
Bernie Innocenti55864192018-08-30 04:05:20 +0900255 if (family == AF_INET)
256 u.sin.sin_port = htons(port);
257 else
258 u.sin6.sin6_port = htons(port);
259
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900260 if (!bind(s, &u.sa, slen)) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900261 }
262
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900263 // nothing after 10 attempts, our network table is probably busy
264 // let the system decide which port is best
Bernie Innocenti55864192018-08-30 04:05:20 +0900265 if (family == AF_INET)
266 u.sin.sin_port = 0;
267 else
268 u.sin6.sin6_port = 0;
269
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900270 return bind(s, &u.sa, slen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900271}
272/* BIONIC-END */
273
Bernie Innocenti55864192018-08-30 04:05:20 +0900274/* int
275 * res_isourserver(ina)
276 * looks up "ina" in _res.ns_addr_list[]
277 * returns:
278 * 0 : not found
279 * >0 : found
280 * author:
281 * paul vixie, 29may94
282 */
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900283static int res_ourserver_p(const res_state statp, const sockaddr* sa) {
284 const sockaddr_in *inp, *srv;
285 const sockaddr_in6 *in6p, *srv6;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900286 int ns;
Bernie Innocenti55864192018-08-30 04:05:20 +0900287
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900288 switch (sa->sa_family) {
289 case AF_INET:
290 inp = (const struct sockaddr_in*) (const void*) sa;
291 for (ns = 0; ns < statp->nscount; ns++) {
292 srv = (struct sockaddr_in*) (void*) get_nsaddr(statp, (size_t) ns);
293 if (srv->sin_family == inp->sin_family && srv->sin_port == inp->sin_port &&
294 (srv->sin_addr.s_addr == INADDR_ANY ||
295 srv->sin_addr.s_addr == inp->sin_addr.s_addr))
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900296 return 1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900297 }
298 break;
299 case AF_INET6:
Ken Chencf6ded62018-10-16 23:12:09 +0800300 if (statp->_u._ext.ext == NULL) break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900301 in6p = (const struct sockaddr_in6*) (const void*) sa;
302 for (ns = 0; ns < statp->nscount; ns++) {
303 srv6 = (struct sockaddr_in6*) (void*) get_nsaddr(statp, (size_t) ns);
304 if (srv6->sin6_family == in6p->sin6_family && srv6->sin6_port == in6p->sin6_port &&
Bernie Innocenti55864192018-08-30 04:05:20 +0900305#ifdef HAVE_SIN6_SCOPE_ID
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900306 (srv6->sin6_scope_id == 0 || srv6->sin6_scope_id == in6p->sin6_scope_id) &&
Bernie Innocenti55864192018-08-30 04:05:20 +0900307#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900308 (IN6_IS_ADDR_UNSPECIFIED(&srv6->sin6_addr) ||
309 IN6_ARE_ADDR_EQUAL(&srv6->sin6_addr, &in6p->sin6_addr)))
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900310 return 1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900311 }
312 break;
313 default:
314 break;
315 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900316 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900317}
318
319/* int
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900320 * res_nameinquery(name, type, cl, buf, eom)
321 * look for (name, type, cl) in the query section of packet (buf, eom)
Bernie Innocenti55864192018-08-30 04:05:20 +0900322 * requires:
323 * buf + HFIXEDSZ <= eom
324 * returns:
325 * -1 : format error
326 * 0 : not found
327 * >0 : found
328 * author:
329 * paul vixie, 29may94
330 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900331int res_nameinquery(const char* name, int type, int cl, const u_char* buf, const u_char* eom) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900332 const u_char* cp = buf + HFIXEDSZ;
333 int qdcount = ntohs(((const HEADER*) (const void*) buf)->qdcount);
Bernie Innocenti55864192018-08-30 04:05:20 +0900334
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900335 while (qdcount-- > 0) {
336 char tname[MAXDNAME + 1];
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900337 int n = dn_expand(buf, eom, cp, tname, sizeof tname);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900338 if (n < 0) return (-1);
339 cp += n;
340 if (cp + 2 * INT16SZ > eom) return (-1);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900341 int ttype = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900342 cp += INT16SZ;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900343 int tclass = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900344 cp += INT16SZ;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900345 if (ttype == type && tclass == cl && ns_samename(tname, name) == 1) return (1);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900346 }
347 return (0);
Bernie Innocenti55864192018-08-30 04:05:20 +0900348}
349
350/* int
351 * res_queriesmatch(buf1, eom1, buf2, eom2)
352 * is there a 1:1 mapping of (name,type,class)
353 * in (buf1,eom1) and (buf2,eom2)?
354 * returns:
355 * -1 : format error
356 * 0 : not a 1:1 mapping
357 * >0 : is a 1:1 mapping
358 * author:
359 * paul vixie, 29may94
360 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900361int res_queriesmatch(const u_char* buf1, const u_char* eom1, const u_char* buf2,
362 const u_char* eom2) {
363 const u_char* cp = buf1 + HFIXEDSZ;
364 int qdcount = ntohs(((const HEADER*) (const void*) buf1)->qdcount);
Bernie Innocenti55864192018-08-30 04:05:20 +0900365
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900366 if (buf1 + HFIXEDSZ > eom1 || buf2 + HFIXEDSZ > eom2) return (-1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900367
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900368 /*
369 * Only header section present in replies to
370 * dynamic update packets.
371 */
372 if ((((const HEADER*) (const void*) buf1)->opcode == ns_o_update) &&
373 (((const HEADER*) (const void*) buf2)->opcode == ns_o_update))
374 return (1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900375
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900376 if (qdcount != ntohs(((const HEADER*) (const void*) buf2)->qdcount)) return (0);
377 while (qdcount-- > 0) {
378 char tname[MAXDNAME + 1];
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900379 int n = dn_expand(buf1, eom1, cp, tname, sizeof tname);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900380 if (n < 0) return (-1);
381 cp += n;
382 if (cp + 2 * INT16SZ > eom1) return (-1);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900383 int ttype = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900384 cp += INT16SZ;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900385 int tclass = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900386 cp += INT16SZ;
387 if (!res_nameinquery(tname, ttype, tclass, buf2, eom2)) return (0);
388 }
389 return (1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900390}
391
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900392int res_nsend(res_state statp, const u_char* buf, int buflen, u_char* ans, int anssiz) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900393 int gotsomewhere, terrno, v_circuit, resplen, n;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900394 ResolvCacheStatus cache_status = RESOLV_CACHE_UNSUPPORTED;
Bernie Innocenti55864192018-08-30 04:05:20 +0900395
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900396 if (anssiz < HFIXEDSZ) {
397 errno = EINVAL;
398 return (-1);
399 }
400 DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_QUERY),
401 (stdout, ";; res_send()\n"), buf, buflen);
402 v_circuit = (statp->options & RES_USEVC) || buflen > PACKETSZ;
403 gotsomewhere = 0;
404 terrno = ETIMEDOUT;
Bernie Innocenti55864192018-08-30 04:05:20 +0900405
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900406 int anslen = 0;
407 cache_status = _resolv_cache_lookup(statp->netid, buf, buflen, ans, anssiz, &anslen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900408
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900409 if (cache_status == RESOLV_CACHE_FOUND) {
410 return anslen;
411 } else if (cache_status != RESOLV_CACHE_UNSUPPORTED) {
412 // had a cache miss for a known network, so populate the thread private
413 // data so the normal resolve path can do its thing
414 _resolv_populate_res_for_net(statp);
415 }
416 if (statp->nscount == 0) {
417 // We have no nameservers configured, so there's no point trying.
418 // Tell the cache the query failed, or any retries and anyone else asking the same
419 // question will block for PENDING_REQUEST_TIMEOUT seconds instead of failing fast.
420 _resolv_cache_query_failed(statp->netid, buf, buflen);
421 errno = ESRCH;
422 return (-1);
423 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900424
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900425 /*
426 * If the ns_addr_list in the resolver context has changed, then
427 * invalidate our cached copy and the associated timing data.
428 */
Ken Chencf6ded62018-10-16 23:12:09 +0800429 if (statp->_u._ext.nscount != 0) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900430 int needclose = 0;
431 struct sockaddr_storage peer;
432 socklen_t peerlen;
Bernie Innocenti55864192018-08-30 04:05:20 +0900433
Ken Chencf6ded62018-10-16 23:12:09 +0800434 if (statp->_u._ext.nscount != statp->nscount) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900435 needclose++;
436 } else {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900437 for (int ns = 0; ns < statp->nscount; ns++) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900438 if (statp->nsaddr_list[ns].sin_family &&
439 !sock_eq((struct sockaddr*) (void*) &statp->nsaddr_list[ns],
Ken Chencf6ded62018-10-16 23:12:09 +0800440 (struct sockaddr*) (void*) &statp->_u._ext.ext->nsaddrs[ns])) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900441 needclose++;
442 break;
443 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900444
Ken Chencf6ded62018-10-16 23:12:09 +0800445 if (statp->_u._ext.nssocks[ns] == -1) continue;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900446 peerlen = sizeof(peer);
Ken Chencf6ded62018-10-16 23:12:09 +0800447 if (getpeername(statp->_u._ext.nssocks[ns], (struct sockaddr*) (void*) &peer,
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900448 &peerlen) < 0) {
449 needclose++;
450 break;
451 }
452 if (!sock_eq((struct sockaddr*) (void*) &peer, get_nsaddr(statp, (size_t) ns))) {
453 needclose++;
454 break;
455 }
456 }
457 }
458 if (needclose) {
459 res_nclose(statp);
Ken Chencf6ded62018-10-16 23:12:09 +0800460 statp->_u._ext.nscount = 0;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900461 }
462 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900463
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900464 /*
465 * Maybe initialize our private copy of the ns_addr_list.
466 */
Ken Chencf6ded62018-10-16 23:12:09 +0800467 if (statp->_u._ext.nscount == 0) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900468 for (int ns = 0; ns < statp->nscount; ns++) {
Ken Chencf6ded62018-10-16 23:12:09 +0800469 statp->_u._ext.nstimes[ns] = RES_MAXTIME;
470 statp->_u._ext.nssocks[ns] = -1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900471 if (!statp->nsaddr_list[ns].sin_family) continue;
Ken Chencf6ded62018-10-16 23:12:09 +0800472 statp->_u._ext.ext->nsaddrs[ns].sin = statp->nsaddr_list[ns];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900473 }
Ken Chencf6ded62018-10-16 23:12:09 +0800474 statp->_u._ext.nscount = statp->nscount;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900475 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900476
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900477 /*
478 * Some resolvers want to even out the load on their nameservers.
479 * Note that RES_BLAST overrides RES_ROTATE.
480 */
481 if ((statp->options & RES_ROTATE) != 0U && (statp->options & RES_BLAST) == 0U) {
nuccachene172a4e2018-10-23 17:10:58 +0800482 sockaddr_union inu;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900483 struct sockaddr_in ina;
484 int lastns = statp->nscount - 1;
485 int fd;
486 u_int16_t nstime;
Bernie Innocenti55864192018-08-30 04:05:20 +0900487
Ken Chencf6ded62018-10-16 23:12:09 +0800488 if (statp->_u._ext.ext != NULL) inu = statp->_u._ext.ext->nsaddrs[0];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900489 ina = statp->nsaddr_list[0];
Ken Chencf6ded62018-10-16 23:12:09 +0800490 fd = statp->_u._ext.nssocks[0];
491 nstime = statp->_u._ext.nstimes[0];
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900492 for (int ns = 0; ns < lastns; ns++) {
Ken Chencf6ded62018-10-16 23:12:09 +0800493 if (statp->_u._ext.ext != NULL)
494 statp->_u._ext.ext->nsaddrs[ns] = statp->_u._ext.ext->nsaddrs[ns + 1];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900495 statp->nsaddr_list[ns] = statp->nsaddr_list[ns + 1];
Ken Chencf6ded62018-10-16 23:12:09 +0800496 statp->_u._ext.nssocks[ns] = statp->_u._ext.nssocks[ns + 1];
497 statp->_u._ext.nstimes[ns] = statp->_u._ext.nstimes[ns + 1];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900498 }
Ken Chencf6ded62018-10-16 23:12:09 +0800499 if (statp->_u._ext.ext != NULL) statp->_u._ext.ext->nsaddrs[lastns] = inu;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900500 statp->nsaddr_list[lastns] = ina;
Ken Chencf6ded62018-10-16 23:12:09 +0800501 statp->_u._ext.nssocks[lastns] = fd;
502 statp->_u._ext.nstimes[lastns] = nstime;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900503 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900504
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900505 /*
506 * Send request, RETRY times, or until successful.
507 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900508 for (int attempt = 0; attempt < statp->retry; ++attempt) {
Bernie Innocenti189eb502018-10-01 23:10:18 +0900509 struct res_stats stats[MAXNS];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900510 struct __res_params params;
Bernie Innocenti189eb502018-10-01 23:10:18 +0900511 int revision_id = resolv_cache_get_resolver_stats(statp->netid, &params, stats);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900512 bool usable_servers[MAXNS];
513 android_net_res_stats_get_usable_servers(&params, stats, statp->nscount, usable_servers);
Bernie Innocenti55864192018-08-30 04:05:20 +0900514
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900515 for (int ns = 0; ns < statp->nscount; ns++) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900516 if (!usable_servers[ns]) continue;
517 struct sockaddr* nsap;
518 int nsaplen;
519 time_t now = 0;
520 int rcode = RCODE_INTERNAL_ERROR;
521 int delay = 0;
522 nsap = get_nsaddr(statp, (size_t) ns);
523 nsaplen = get_salen(nsap);
524 statp->_flags &= ~RES_F_LASTMASK;
525 statp->_flags |= (ns << RES_F_LASTSHIFT);
Bernie Innocenti55864192018-08-30 04:05:20 +0900526
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900527 same_ns:
Mike Yua46fae72018-11-01 20:07:00 +0800528 int done = 0, loops = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900529
Mike Yua46fae72018-11-01 20:07:00 +0800530 do {
531 res_sendhookact act;
Bernie Innocenti55864192018-08-30 04:05:20 +0900532
Mike Yua46fae72018-11-01 20:07:00 +0800533 // TODO: This function tries to query on all of private DNS servers. Putting
534 // it here is strange since we should expect there is only one DNS server
535 // being queried. Consider moving it to other reasonable place. In addition,
536 // enum res_sendhookact can be removed.
537 act = res_tls_send(statp, Slice(const_cast<u_char*>(buf), buflen),
538 Slice(ans, anssiz), &resplen);
539 switch (act) {
540 case res_goahead:
541 done = 1;
542 break;
543 case res_nextns:
544 res_nclose(statp);
545 goto next_ns;
546 case res_done:
547 if (cache_status == RESOLV_CACHE_NOTFOUND) {
548 _resolv_cache_add(statp->netid, buf, buflen, ans, resplen);
549 }
550 return (resplen);
551 case res_modified:
552 /* give the hook another try */
553 if (++loops < 42) /*doug adams*/
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900554 break;
Mike Yua46fae72018-11-01 20:07:00 +0800555 [[fallthrough]];
556 case res_error:
557 [[fallthrough]];
558 default:
559 goto fail;
560 }
561 } while (!done);
Bernie Innocenti55864192018-08-30 04:05:20 +0900562
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900563 [[maybe_unused]] static const int niflags = NI_NUMERICHOST | NI_NUMERICSERV;
564 [[maybe_unused]] char abuf[NI_MAXHOST];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900565 Dprint(((statp->options & RES_DEBUG) &&
566 getnameinfo(nsap, (socklen_t) nsaplen, abuf, sizeof(abuf), NULL, 0, niflags) ==
567 0),
568 (stdout, ";; Querying server (# %d) address = %s\n", ns + 1, abuf));
Bernie Innocenti55864192018-08-30 04:05:20 +0900569
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900570 if (v_circuit) {
571 /* Use VC; at most one attempt per server. */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900572 attempt = statp->retry;
Bernie Innocenti55864192018-08-30 04:05:20 +0900573
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900574 n = send_vc(statp, &params, buf, buflen, ans, anssiz, &terrno, ns, &now, &rcode,
575 &delay);
Bernie Innocenti55864192018-08-30 04:05:20 +0900576
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900577 /*
578 * Only record stats the first time we try a query. This ensures that
579 * queries that deterministically fail (e.g., a name that always returns
580 * SERVFAIL or times out) do not unduly affect the stats.
581 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900582 if (attempt == 0) {
Bernie Innocenti189eb502018-10-01 23:10:18 +0900583 res_sample sample;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900584 _res_stats_set_sample(&sample, now, rcode, delay);
585 _resolv_cache_add_resolver_stats_sample(statp->netid, revision_id, ns, &sample,
586 params.max_samples);
587 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900588
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900589 VLOG << "used send_vc " << n;
Bernie Innocenti55864192018-08-30 04:05:20 +0900590
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900591 if (n < 0) goto fail;
592 if (n == 0) goto next_ns;
593 resplen = n;
594 } else {
595 /* Use datagrams. */
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900596 VLOG << "using send_dg";
Bernie Innocenti55864192018-08-30 04:05:20 +0900597
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900598 n = send_dg(statp, &params, buf, buflen, ans, anssiz, &terrno, ns, &v_circuit,
599 &gotsomewhere, &now, &rcode, &delay);
Bernie Innocenti55864192018-08-30 04:05:20 +0900600
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900601 /* Only record stats the first time we try a query. See above. */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900602 if (attempt == 0) {
Bernie Innocenti189eb502018-10-01 23:10:18 +0900603 res_sample sample;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900604 _res_stats_set_sample(&sample, now, rcode, delay);
605 _resolv_cache_add_resolver_stats_sample(statp->netid, revision_id, ns, &sample,
606 params.max_samples);
607 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900608
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900609 VLOG << "used send_dg " << n;
Bernie Innocenti55864192018-08-30 04:05:20 +0900610
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900611 if (n < 0) goto fail;
612 if (n == 0) goto next_ns;
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900613 VLOG << "time=" << time(NULL);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900614 if (v_circuit) goto same_ns;
615 resplen = n;
616 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900617
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900618 Dprint((statp->options & RES_DEBUG) ||
619 ((statp->pfcode & RES_PRF_REPLY) && (statp->pfcode & RES_PRF_HEAD1)),
620 (stdout, ";; got answer:\n"));
Bernie Innocenti55864192018-08-30 04:05:20 +0900621
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900622 DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_REPLY),
623 (stdout, "%s", ""), ans, (resplen > anssiz) ? anssiz : resplen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900624
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900625 if (cache_status == RESOLV_CACHE_NOTFOUND) {
626 _resolv_cache_add(statp->netid, buf, buflen, ans, resplen);
627 }
628 /*
629 * If we have temporarily opened a virtual circuit,
630 * or if we haven't been asked to keep a socket open,
631 * close the socket.
632 */
633 if ((v_circuit && (statp->options & RES_USEVC) == 0U) ||
634 (statp->options & RES_STAYOPEN) == 0U) {
635 res_nclose(statp);
636 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900637 return (resplen);
638 next_ns:;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900639 } // for each ns
640 } // for each retry
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900641 res_nclose(statp);
642 if (!v_circuit) {
643 if (!gotsomewhere)
644 errno = ECONNREFUSED; /* no nameservers found */
645 else
646 errno = ETIMEDOUT; /* no answer obtained */
647 } else
648 errno = terrno;
Bernie Innocenti55864192018-08-30 04:05:20 +0900649
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900650 _resolv_cache_query_failed(statp->netid, buf, buflen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900651
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900652 return (-1);
653fail:
Bernie Innocenti55864192018-08-30 04:05:20 +0900654
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900655 _resolv_cache_query_failed(statp->netid, buf, buflen);
656 res_nclose(statp);
657 return (-1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900658}
659
660/* Private */
661
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900662static int get_salen(const struct sockaddr* sa) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900663 if (sa->sa_family == AF_INET)
664 return (sizeof(struct sockaddr_in));
665 else if (sa->sa_family == AF_INET6)
666 return (sizeof(struct sockaddr_in6));
667 else
668 return (0); /* unknown, die on connect */
Bernie Innocenti55864192018-08-30 04:05:20 +0900669}
670
671/*
672 * pick appropriate nsaddr_list for use. see res_init() for initialization.
673 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900674static struct sockaddr* get_nsaddr(res_state statp, size_t n) {
Ken Chencf6ded62018-10-16 23:12:09 +0800675 if (!statp->nsaddr_list[n].sin_family && statp->_u._ext.ext) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900676 /*
Ken Chencf6ded62018-10-16 23:12:09 +0800677 * - statp->_u._ext.ext->nsaddrs[n] holds an address that is larger
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900678 * than struct sockaddr, and
679 * - user code did not update statp->nsaddr_list[n].
680 */
Ken Chencf6ded62018-10-16 23:12:09 +0800681 return (struct sockaddr*) (void*) &statp->_u._ext.ext->nsaddrs[n];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900682 } else {
683 /*
684 * - user code updated statp->nsaddr_list[n], or
685 * - statp->nsaddr_list[n] has the same content as
Ken Chencf6ded62018-10-16 23:12:09 +0800686 * statp->_u._ext.ext->nsaddrs[n].
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900687 */
688 return (struct sockaddr*) (void*) &statp->nsaddr_list[n];
689 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900690}
691
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900692static struct timespec get_timeout(const res_state statp, const struct __res_params* params,
693 const int ns) {
694 int msec;
695 if (params->base_timeout_msec != 0) {
696 // TODO: scale the timeout by retry attempt and maybe number of servers
697 msec = params->base_timeout_msec;
698 } else {
699 // Legacy algorithm which scales the timeout by nameserver number.
700 // For instance, with 4 nameservers: 5s, 2.5s, 5s, 10s
701 // This has no effect with 1 or 2 nameservers
702 msec = (statp->retrans * 1000) << ns;
703 if (ns > 0) {
704 msec /= statp->nscount;
705 }
706 if (msec < 1000) {
707 msec = 1000; // Use at least 100ms
708 }
709 }
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900710 VLOG << "using timeout of " << msec << " msec";
Bernie Innocenti55864192018-08-30 04:05:20 +0900711
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900712 struct timespec result;
713 result.tv_sec = msec / 1000;
714 result.tv_nsec = (msec % 1000) * 1000000;
715 return result;
Bernie Innocenti55864192018-08-30 04:05:20 +0900716}
717
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900718static int send_vc(res_state statp, struct __res_params* params, const u_char* buf, int buflen,
719 u_char* ans, int anssiz, int* terrno, int ns, time_t* at, int* rcode,
720 int* delay) {
721 *at = time(NULL);
722 *rcode = RCODE_INTERNAL_ERROR;
723 *delay = 0;
724 const HEADER* hp = (const HEADER*) (const void*) buf;
725 HEADER* anhp = (HEADER*) (void*) ans;
726 struct sockaddr* nsap;
727 int nsaplen;
728 int truncating, connreset, resplen, n;
729 struct iovec iov[2];
730 u_short len;
731 u_char* cp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900732
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900733 VLOG << "using send_vc";
Bernie Innocenti55864192018-08-30 04:05:20 +0900734
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900735 nsap = get_nsaddr(statp, (size_t) ns);
736 nsaplen = get_salen(nsap);
Bernie Innocenti55864192018-08-30 04:05:20 +0900737
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900738 connreset = 0;
739same_ns:
740 truncating = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900741
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900742 struct timespec now = evNowTime();
Bernie Innocenti55864192018-08-30 04:05:20 +0900743
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900744 /* Are we still talking to whom we want to talk to? */
745 if (statp->_vcsock >= 0 && (statp->_flags & RES_F_VC) != 0) {
746 struct sockaddr_storage peer;
747 socklen_t size = sizeof peer;
748 unsigned old_mark;
749 socklen_t mark_size = sizeof(old_mark);
750 if (getpeername(statp->_vcsock, (struct sockaddr*) (void*) &peer, &size) < 0 ||
751 !sock_eq((struct sockaddr*) (void*) &peer, nsap) ||
752 getsockopt(statp->_vcsock, SOL_SOCKET, SO_MARK, &old_mark, &mark_size) < 0 ||
753 old_mark != statp->_mark) {
754 res_nclose(statp);
755 statp->_flags &= ~RES_F_VC;
756 }
757 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900758
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900759 if (statp->_vcsock < 0 || (statp->_flags & RES_F_VC) == 0) {
760 if (statp->_vcsock >= 0) res_nclose(statp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900761
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900762 statp->_vcsock = socket(nsap->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0);
763 if (statp->_vcsock < 0) {
764 switch (errno) {
765 case EPROTONOSUPPORT:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900766 case EPFNOSUPPORT:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900767 case EAFNOSUPPORT:
768 Perror(statp, stderr, "socket(vc)", errno);
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900769 return 0;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900770 default:
771 *terrno = errno;
772 Perror(statp, stderr, "socket(vc)", errno);
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900773 return -1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900774 }
775 }
776 fchown(statp->_vcsock, AID_DNS, -1);
777 if (statp->_mark != MARK_UNSET) {
778 if (setsockopt(statp->_vcsock, SOL_SOCKET, SO_MARK, &statp->_mark,
779 sizeof(statp->_mark)) < 0) {
780 *terrno = errno;
781 Perror(statp, stderr, "setsockopt", errno);
782 return -1;
783 }
784 }
785 errno = 0;
786 if (random_bind(statp->_vcsock, nsap->sa_family) < 0) {
787 *terrno = errno;
788 Aerror(statp, stderr, "bind/vc", errno, nsap, nsaplen);
789 res_nclose(statp);
790 return (0);
791 }
792 if (connect_with_timeout(statp->_vcsock, nsap, (socklen_t) nsaplen,
793 get_timeout(statp, params, ns)) < 0) {
794 *terrno = errno;
795 Aerror(statp, stderr, "connect/vc", errno, nsap, nsaplen);
796 res_nclose(statp);
797 /*
798 * The way connect_with_timeout() is implemented prevents us from reliably
799 * determining whether this was really a timeout or e.g. ECONNREFUSED. Since
800 * currently both cases are handled in the same way, there is no need to
801 * change this (yet). If we ever need to reliably distinguish between these
802 * cases, both connect_with_timeout() and retrying_poll() need to be
803 * modified, though.
804 */
805 *rcode = RCODE_TIMEOUT;
806 return (0);
807 }
808 statp->_flags |= RES_F_VC;
809 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900810
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900811 /*
812 * Send length & message
813 */
814 ns_put16((u_short) buflen, (u_char*) (void*) &len);
815 iov[0] = evConsIovec(&len, INT16SZ);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900816 iov[1] = evConsIovec((void*) buf, (size_t) buflen);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900817 if (writev(statp->_vcsock, iov, 2) != (INT16SZ + buflen)) {
818 *terrno = errno;
819 Perror(statp, stderr, "write failed", errno);
820 res_nclose(statp);
821 return (0);
822 }
823 /*
824 * Receive length & response
825 */
826read_len:
827 cp = ans;
828 len = INT16SZ;
829 while ((n = read(statp->_vcsock, (char*) cp, (size_t) len)) > 0) {
830 cp += n;
831 if ((len -= n) == 0) break;
832 }
833 if (n <= 0) {
834 *terrno = errno;
835 Perror(statp, stderr, "read failed", errno);
836 res_nclose(statp);
837 /*
838 * A long running process might get its TCP
839 * connection reset if the remote server was
840 * restarted. Requery the server instead of
841 * trying a new one. When there is only one
842 * server, this means that a query might work
843 * instead of failing. We only allow one reset
844 * per query to prevent looping.
845 */
846 if (*terrno == ECONNRESET && !connreset) {
847 connreset = 1;
848 res_nclose(statp);
849 goto same_ns;
850 }
851 res_nclose(statp);
852 return (0);
853 }
854 resplen = ns_get16(ans);
855 if (resplen > anssiz) {
856 Dprint(statp->options & RES_DEBUG, (stdout, ";; response truncated\n"));
857 truncating = 1;
858 len = anssiz;
859 } else
860 len = resplen;
861 if (len < HFIXEDSZ) {
862 /*
863 * Undersized message.
864 */
865 Dprint(statp->options & RES_DEBUG, (stdout, ";; undersized: %d\n", len));
866 *terrno = EMSGSIZE;
867 res_nclose(statp);
868 return (0);
869 }
870 cp = ans;
871 while (len != 0 && (n = read(statp->_vcsock, (char*) cp, (size_t) len)) > 0) {
872 cp += n;
873 len -= n;
874 }
875 if (n <= 0) {
876 *terrno = errno;
877 Perror(statp, stderr, "read(vc)", errno);
878 res_nclose(statp);
879 return (0);
880 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900881
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900882 if (truncating) {
883 /*
884 * Flush rest of answer so connection stays in synch.
885 */
886 anhp->tc = 1;
887 len = resplen - anssiz;
888 while (len != 0) {
889 char junk[PACKETSZ];
Bernie Innocenti55864192018-08-30 04:05:20 +0900890
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900891 n = read(statp->_vcsock, junk, (len > sizeof junk) ? sizeof junk : len);
892 if (n > 0)
893 len -= n;
894 else
895 break;
896 }
897 }
898 /*
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900899 * If the calling application has bailed out of
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900900 * a previous call and failed to arrange to have
901 * the circuit closed or the server has got
902 * itself confused, then drop the packet and
903 * wait for the correct one.
904 */
905 if (hp->id != anhp->id) {
906 DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_REPLY),
907 (stdout, ";; old answer (unexpected):\n"), ans,
908 (resplen > anssiz) ? anssiz : resplen);
909 goto read_len;
910 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900911
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900912 /*
913 * All is well, or the error is fatal. Signal that the
914 * next nameserver ought not be tried.
915 */
916 if (resplen > 0) {
917 struct timespec done = evNowTime();
918 *delay = _res_stats_calculate_rtt(&done, &now);
919 *rcode = anhp->rcode;
920 }
921 return (resplen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900922}
923
924/* return -1 on error (errno set), 0 on success */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900925static int connect_with_timeout(int sock, const struct sockaddr* nsap, socklen_t salen,
926 const struct timespec timeout) {
927 int res, origflags;
Bernie Innocenti55864192018-08-30 04:05:20 +0900928
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900929 origflags = fcntl(sock, F_GETFL, 0);
930 fcntl(sock, F_SETFL, origflags | O_NONBLOCK);
Bernie Innocenti55864192018-08-30 04:05:20 +0900931
Bernie Innocentif89b3512018-08-30 07:34:37 +0900932 res = connect(sock, nsap, salen);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900933 if (res < 0 && errno != EINPROGRESS) {
934 res = -1;
935 goto done;
936 }
937 if (res != 0) {
938 struct timespec now = evNowTime();
939 struct timespec finish = evAddTime(now, timeout);
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900940 VLOG << sock << " send_vc";
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900941 res = retrying_poll(sock, POLLIN | POLLOUT, &finish);
942 if (res <= 0) {
943 res = -1;
944 }
945 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900946done:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900947 fcntl(sock, F_SETFL, origflags);
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900948 VLOG << sock << " connect_with_const timeout returning " << res;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900949 return res;
Bernie Innocenti55864192018-08-30 04:05:20 +0900950}
951
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900952static int retrying_poll(const int sock, const short events, const struct timespec* finish) {
953 struct timespec now, timeout;
Bernie Innocenti55864192018-08-30 04:05:20 +0900954
955retry:
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900956 VLOG << " " << sock << " retrying_poll";
Bernie Innocenti55864192018-08-30 04:05:20 +0900957
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900958 now = evNowTime();
959 if (evCmpTime(*finish, now) > 0)
960 timeout = evSubTime(*finish, now);
961 else
962 timeout = evConsTime(0L, 0L);
963 struct pollfd fds = {.fd = sock, .events = events};
964 int n = ppoll(&fds, 1, &timeout, /*sigmask=*/NULL);
965 if (n == 0) {
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900966 VLOG << " " << sock << "retrying_poll timeout";
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900967 errno = ETIMEDOUT;
968 return 0;
969 }
970 if (n < 0) {
971 if (errno == EINTR) goto retry;
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900972 VLOG << " " << sock << " retrying_poll got error " << n;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900973 return n;
974 }
975 if (fds.revents & (POLLIN | POLLOUT | POLLERR)) {
976 int error;
977 socklen_t len = sizeof(error);
978 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0 || error) {
979 errno = error;
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900980 VLOG << " " << sock << " retrying_poll dot error2 " << errno;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900981 return -1;
982 }
983 }
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900984 VLOG << " " << sock << " retrying_poll returning " << n;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900985 return n;
Bernie Innocenti55864192018-08-30 04:05:20 +0900986}
987
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900988static int send_dg(res_state statp, struct __res_params* params, const u_char* buf, int buflen,
989 u_char* ans, int anssiz, int* terrno, int ns, int* v_circuit, int* gotsomewhere,
990 time_t* at, int* rcode, int* delay) {
991 *at = time(NULL);
992 *rcode = RCODE_INTERNAL_ERROR;
993 *delay = 0;
994 const HEADER* hp = (const HEADER*) (const void*) buf;
995 HEADER* anhp = (HEADER*) (void*) ans;
996 const struct sockaddr* nsap;
997 int nsaplen;
998 struct timespec now, timeout, finish, done;
999 struct sockaddr_storage from;
1000 socklen_t fromlen;
1001 int resplen, n, s;
Bernie Innocenti55864192018-08-30 04:05:20 +09001002
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001003 nsap = get_nsaddr(statp, (size_t) ns);
1004 nsaplen = get_salen(nsap);
Ken Chencf6ded62018-10-16 23:12:09 +08001005 if (statp->_u._ext.nssocks[ns] == -1) {
1006 statp->_u._ext.nssocks[ns] = socket(nsap->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
1007 if (statp->_u._ext.nssocks[ns] < 0) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001008 switch (errno) {
1009 case EPROTONOSUPPORT:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001010 case EPFNOSUPPORT:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001011 case EAFNOSUPPORT:
1012 Perror(statp, stderr, "socket(dg)", errno);
1013 return (0);
1014 default:
1015 *terrno = errno;
1016 Perror(statp, stderr, "socket(dg)", errno);
1017 return (-1);
1018 }
1019 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001020
Ken Chencf6ded62018-10-16 23:12:09 +08001021 fchown(statp->_u._ext.nssocks[ns], AID_DNS, -1);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001022 if (statp->_mark != MARK_UNSET) {
Ken Chencf6ded62018-10-16 23:12:09 +08001023 if (setsockopt(statp->_u._ext.nssocks[ns], SOL_SOCKET, SO_MARK, &(statp->_mark),
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001024 sizeof(statp->_mark)) < 0) {
1025 res_nclose(statp);
1026 return -1;
1027 }
1028 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001029#ifndef CANNOT_CONNECT_DGRAM
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001030 /*
1031 * On a 4.3BSD+ machine (client and server,
1032 * actually), sending to a nameserver datagram
1033 * port with no nameserver will cause an
1034 * ICMP port unreachable message to be returned.
1035 * If our datagram socket is "connected" to the
1036 * server, we get an ECONNREFUSED error on the next
1037 * socket operation, and select returns if the
1038 * error message is received. We can thus detect
1039 * the absence of a nameserver without timing out.
1040 */
Ken Chencf6ded62018-10-16 23:12:09 +08001041 if (random_bind(statp->_u._ext.nssocks[ns], nsap->sa_family) < 0) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001042 Aerror(statp, stderr, "bind(dg)", errno, nsap, nsaplen);
1043 res_nclose(statp);
1044 return (0);
1045 }
Ken Chencf6ded62018-10-16 23:12:09 +08001046 if (connect(statp->_u._ext.nssocks[ns], nsap, (socklen_t) nsaplen) < 0) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001047 Aerror(statp, stderr, "connect(dg)", errno, nsap, nsaplen);
1048 res_nclose(statp);
1049 return (0);
1050 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001051#endif /* !CANNOT_CONNECT_DGRAM */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001052 Dprint(statp->options & RES_DEBUG, (stdout, ";; new DG socket\n"))
1053 }
Ken Chencf6ded62018-10-16 23:12:09 +08001054 s = statp->_u._ext.nssocks[ns];
Bernie Innocenti55864192018-08-30 04:05:20 +09001055#ifndef CANNOT_CONNECT_DGRAM
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001056 if (send(s, (const char*) buf, (size_t) buflen, 0) != buflen) {
1057 Perror(statp, stderr, "send", errno);
1058 res_nclose(statp);
1059 return (0);
1060 }
1061#else /* !CANNOT_CONNECT_DGRAM */
1062 if (sendto(s, (const char*) buf, buflen, 0, nsap, nsaplen) != buflen) {
1063 Aerror(statp, stderr, "sendto", errno, nsap, nsaplen);
1064 res_nclose(statp);
1065 return (0);
1066 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001067#endif /* !CANNOT_CONNECT_DGRAM */
1068
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001069 /*
1070 * Wait for reply.
1071 */
1072 timeout = get_timeout(statp, params, ns);
1073 now = evNowTime();
1074 finish = evAddTime(now, timeout);
Bernie Innocenti55864192018-08-30 04:05:20 +09001075retry:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001076 n = retrying_poll(s, POLLIN, &finish);
Bernie Innocenti55864192018-08-30 04:05:20 +09001077
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001078 if (n == 0) {
1079 *rcode = RCODE_TIMEOUT;
1080 Dprint(statp->options & RES_DEBUG, (stdout, ";; timeout\n"));
1081 *gotsomewhere = 1;
1082 return (0);
1083 }
1084 if (n < 0) {
1085 Perror(statp, stderr, "poll", errno);
1086 res_nclose(statp);
1087 return (0);
1088 }
1089 errno = 0;
1090 fromlen = sizeof(from);
1091 resplen = recvfrom(s, (char*) ans, (size_t) anssiz, 0, (struct sockaddr*) (void*) &from,
1092 &fromlen);
1093 if (resplen <= 0) {
1094 Perror(statp, stderr, "recvfrom", errno);
1095 res_nclose(statp);
1096 return (0);
1097 }
1098 *gotsomewhere = 1;
1099 if (resplen < HFIXEDSZ) {
1100 /*
1101 * Undersized message.
1102 */
1103 Dprint(statp->options & RES_DEBUG, (stdout, ";; undersized: %d\n", resplen));
1104 *terrno = EMSGSIZE;
1105 res_nclose(statp);
1106 return (0);
1107 }
1108 if (hp->id != anhp->id) {
1109 /*
1110 * response from old query, ignore it.
1111 * XXX - potential security hazard could
1112 * be detected here.
1113 */
1114 DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_REPLY),
1115 (stdout, ";; old answer:\n"), ans, (resplen > anssiz) ? anssiz : resplen);
1116 goto retry;
1117 }
1118 if (!(statp->options & RES_INSECURE1) &&
1119 !res_ourserver_p(statp, (struct sockaddr*) (void*) &from)) {
1120 /*
1121 * response from wrong server? ignore it.
1122 * XXX - potential security hazard could
1123 * be detected here.
1124 */
1125 DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_REPLY),
1126 (stdout, ";; not our server:\n"), ans, (resplen > anssiz) ? anssiz : resplen);
1127 goto retry;
1128 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001129 if (anhp->rcode == FORMERR && (statp->options & RES_USE_EDNS0) != 0U) {
1130 /*
1131 * Do not retry if the server do not understand EDNS0.
1132 * The case has to be captured here, as FORMERR packet do not
1133 * carry query section, hence res_queriesmatch() returns 0.
1134 */
1135 DprintQ(statp->options & RES_DEBUG, (stdout, "server rejected query with EDNS0:\n"), ans,
1136 (resplen > anssiz) ? anssiz : resplen);
1137 /* record the error */
1138 statp->_flags |= RES_F_EDNS0ERR;
1139 res_nclose(statp);
1140 return (0);
1141 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001142 if (!(statp->options & RES_INSECURE2) &&
1143 !res_queriesmatch(buf, buf + buflen, ans, ans + anssiz)) {
1144 /*
1145 * response contains wrong query? ignore it.
1146 * XXX - potential security hazard could
1147 * be detected here.
1148 */
1149 DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_REPLY),
1150 (stdout, ";; wrong query name:\n"), ans, (resplen > anssiz) ? anssiz : resplen);
1151 goto retry;
1152 ;
1153 }
1154 done = evNowTime();
1155 *delay = _res_stats_calculate_rtt(&done, &now);
1156 if (anhp->rcode == SERVFAIL || anhp->rcode == NOTIMP || anhp->rcode == REFUSED) {
1157 DprintQ(statp->options & RES_DEBUG, (stdout, "server rejected query:\n"), ans,
1158 (resplen > anssiz) ? anssiz : resplen);
1159 res_nclose(statp);
1160 /* don't retry if called from dig */
1161 if (!statp->pfcode) {
1162 *rcode = anhp->rcode;
1163 return (0);
1164 }
1165 }
1166 if (!(statp->options & RES_IGNTC) && anhp->tc) {
1167 /*
1168 * To get the rest of answer,
1169 * use TCP with same server.
1170 */
1171 Dprint(statp->options & RES_DEBUG, (stdout, ";; truncated answer\n"));
1172 *v_circuit = 1;
1173 res_nclose(statp);
1174 return (1);
1175 }
1176 /*
1177 * All is well, or the error is fatal. Signal that the
1178 * next nameserver ought not be tried.
1179 */
1180 if (resplen > 0) {
1181 *rcode = anhp->rcode;
1182 }
1183 return (resplen);
Bernie Innocenti55864192018-08-30 04:05:20 +09001184}
1185
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001186static void Aerror(const res_state statp, FILE* file, const char* string, int error,
1187 const struct sockaddr* address, int alen) {
Bernie Innocentie9ba09c2018-09-12 23:20:10 +09001188 if (!kVerboseLogging) return;
1189
1190 const int save = errno;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001191 char hbuf[NI_MAXHOST];
1192 char sbuf[NI_MAXSERV];
Bernie Innocentie9ba09c2018-09-12 23:20:10 +09001193 constexpr int niflags = NI_NUMERICHOST | NI_NUMERICSERV;
Bernie Innocenti55864192018-08-30 04:05:20 +09001194
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001195 if ((statp->options & RES_DEBUG) != 0U) {
1196 if (getnameinfo(address, (socklen_t) alen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf),
1197 niflags)) {
1198 strncpy(hbuf, "?", sizeof(hbuf) - 1);
1199 hbuf[sizeof(hbuf) - 1] = '\0';
1200 strncpy(sbuf, "?", sizeof(sbuf) - 1);
1201 sbuf[sizeof(sbuf) - 1] = '\0';
1202 }
1203 fprintf(file, "res_send: %s ([%s].%s): %s\n", string, hbuf, sbuf, strerror(error));
1204 }
1205 errno = save;
Bernie Innocenti55864192018-08-30 04:05:20 +09001206}
1207
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001208static void Perror(const res_state statp, FILE* file, const char* string, int error) {
Bernie Innocentie9ba09c2018-09-12 23:20:10 +09001209 if (!kVerboseLogging) return;
Bernie Innocenti55864192018-08-30 04:05:20 +09001210
Bernie Innocentie9ba09c2018-09-12 23:20:10 +09001211 const int save = errno;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001212 if ((statp->options & RES_DEBUG) != 0U)
1213 fprintf(file, "res_send: %s: %s\n", string, strerror(error));
1214 errno = save;
Bernie Innocenti55864192018-08-30 04:05:20 +09001215}
1216
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001217static int sock_eq(struct sockaddr* a, struct sockaddr* b) {
1218 struct sockaddr_in *a4, *b4;
1219 struct sockaddr_in6 *a6, *b6;
Bernie Innocenti55864192018-08-30 04:05:20 +09001220
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001221 if (a->sa_family != b->sa_family) return 0;
1222 switch (a->sa_family) {
1223 case AF_INET:
1224 a4 = (struct sockaddr_in*) (void*) a;
1225 b4 = (struct sockaddr_in*) (void*) b;
1226 return a4->sin_port == b4->sin_port && a4->sin_addr.s_addr == b4->sin_addr.s_addr;
1227 case AF_INET6:
1228 a6 = (struct sockaddr_in6*) (void*) a;
1229 b6 = (struct sockaddr_in6*) (void*) b;
1230 return a6->sin6_port == b6->sin6_port &&
Bernie Innocenti55864192018-08-30 04:05:20 +09001231#ifdef HAVE_SIN6_SCOPE_ID
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001232 a6->sin6_scope_id == b6->sin6_scope_id &&
Bernie Innocenti55864192018-08-30 04:05:20 +09001233#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001234 IN6_ARE_ADDR_EQUAL(&a6->sin6_addr, &b6->sin6_addr);
1235 default:
1236 return 0;
1237 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001238}
Mike Yua46fae72018-11-01 20:07:00 +08001239
1240res_sendhookact res_tls_send(res_state statp, const Slice query, const Slice answer, int* resplen) {
1241 const unsigned netId = statp->netid;
1242 const unsigned mark = statp->_mark;
1243
1244 // TLS bypass
1245 if (statp->use_local_nameserver) {
1246 return res_goahead;
1247 }
1248
1249 const auto privateDnsStatus = gPrivateDnsConfiguration.getStatus(netId);
1250
1251 if (privateDnsStatus.mode == PrivateDnsMode::OFF) return res_goahead;
1252
1253 if (privateDnsStatus.validatedServers.empty()) {
1254 if (privateDnsStatus.mode == PrivateDnsMode::OPPORTUNISTIC) {
1255 return res_goahead;
1256 } else {
1257 // Sleep and iterate some small number of times checking for the
1258 // arrival of resolved and validated server IP addresses, instead
1259 // of returning an immediate error.
1260 using namespace std::chrono_literals;
1261 std::this_thread::sleep_for(100ms);
1262 return res_modified;
1263 }
1264 }
1265
1266 VLOG << __func__ << ": performing query over TLS";
1267
1268 const auto response = sDnsTlsDispatcher.query(privateDnsStatus.validatedServers, mark, query,
1269 answer, resplen);
1270 if (response == DnsTlsTransport::Response::success) {
1271 VLOG << __func__ << ": success";
1272 return res_done;
1273 }
1274
1275 VLOG << __func__ << ": abort: TLS query failed: " << static_cast<int>(response);
1276
1277 if (privateDnsStatus.mode == PrivateDnsMode::OPPORTUNISTIC) {
1278 // In opportunistic mode, handle falling back to cleartext in some
1279 // cases (DNS shouldn't fail if a validated opportunistic mode server
1280 // becomes unreachable for some reason).
1281 switch (response) {
1282 case DnsTlsTransport::Response::network_error:
1283 case DnsTlsTransport::Response::internal_error:
1284 // Note: this will cause cleartext queries to be emitted, with
1285 // all of the EDNS0 goodness enabled. Fingers crossed. :-/
1286 return res_goahead;
1287 default:
1288 break;
1289 }
1290 }
1291
1292 // There was an internal error. Fail hard.
1293 return res_error;
1294}