• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*	$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 
73 /*
74  * Send query to name server and wait for reply.
75  */
76 
77 #define LOG_TAG "res_send"
78 
79 #include <sys/param.h>
80 #include <sys/socket.h>
81 #include <sys/time.h>
82 #include <sys/types.h>
83 #include <sys/uio.h>
84 
85 #include <arpa/inet.h>
86 #include <arpa/nameser.h>
87 #include <netinet/in.h>
88 
89 #include <errno.h>
90 #include <fcntl.h>
91 #include <netdb.h>
92 #include <poll.h>
93 #include <signal.h>
94 #include <stdio.h>
95 #include <stdlib.h>
96 #include <string.h>
97 #include <time.h>
98 #include <unistd.h>
99 
100 #include <android-base/logging.h>
101 #include <android/multinetwork.h>  // ResNsendFlags
102 
103 #include <netdutils/Slice.h>
104 #include "DnsTlsDispatcher.h"
105 #include "DnsTlsTransport.h"
106 #include "PrivateDnsConfiguration.h"
107 #include "netd_resolv/resolv.h"
108 #include "netd_resolv/stats.h"
109 #include "private/android_filesystem_config.h"
110 #include "res_state_ext.h"
111 #include "resolv_cache.h"
112 #include "resolv_private.h"
113 
114 // TODO: use the namespace something like android::netd_resolv for libnetd_resolv
115 using namespace android::net;
116 using android::netdutils::Slice;
117 
118 static DnsTlsDispatcher sDnsTlsDispatcher;
119 
120 static int get_salen(const struct sockaddr*);
121 static struct sockaddr* get_nsaddr(res_state, size_t);
122 static int send_vc(res_state, res_params* params, const u_char*, int, u_char*, int, int*, int,
123                    time_t*, int*, int*);
124 static int send_dg(res_state, res_params* params, const u_char*, int, u_char*, int, int*, int, int*,
125                    int*, time_t*, int*, int*);
126 static void Aerror(const res_state, const char*, int, const struct sockaddr*, int);
127 static void Perror(const res_state, const char*, int);
128 
129 static int sock_eq(struct sockaddr*, struct sockaddr*);
130 static int connect_with_timeout(int sock, const struct sockaddr* nsap, socklen_t salen,
131                                 const struct timespec timeout);
132 static int retrying_poll(const int sock, short events, const struct timespec* finish);
133 static int res_tls_send(res_state, const Slice query, const Slice answer, int* rcode,
134                         bool* fallback);
135 
136 /* BIONIC-BEGIN: implement source port randomization */
137 
138 // BEGIN: Code copied from ISC eventlib
139 // TODO: move away from this code
140 
141 #define BILLION 1000000000
142 
evConsTime(time_t sec,long nsec)143 static struct timespec evConsTime(time_t sec, long nsec) {
144     struct timespec x;
145 
146     x.tv_sec = sec;
147     x.tv_nsec = nsec;
148     return (x);
149 }
150 
evAddTime(struct timespec addend1,struct timespec addend2)151 static struct timespec evAddTime(struct timespec addend1, struct timespec addend2) {
152     struct timespec x;
153 
154     x.tv_sec = addend1.tv_sec + addend2.tv_sec;
155     x.tv_nsec = addend1.tv_nsec + addend2.tv_nsec;
156     if (x.tv_nsec >= BILLION) {
157         x.tv_sec++;
158         x.tv_nsec -= BILLION;
159     }
160     return (x);
161 }
162 
evSubTime(struct timespec minuend,struct timespec subtrahend)163 static struct timespec evSubTime(struct timespec minuend, struct timespec subtrahend) {
164     struct timespec x;
165 
166     x.tv_sec = minuend.tv_sec - subtrahend.tv_sec;
167     if (minuend.tv_nsec >= subtrahend.tv_nsec)
168         x.tv_nsec = minuend.tv_nsec - subtrahend.tv_nsec;
169     else {
170         x.tv_nsec = BILLION - subtrahend.tv_nsec + minuend.tv_nsec;
171         x.tv_sec--;
172     }
173     return (x);
174 }
175 
evCmpTime(struct timespec a,struct timespec b)176 static int evCmpTime(struct timespec a, struct timespec b) {
177 #define SGN(x) ((x) < 0 ? (-1) : (x) > 0 ? (1) : (0));
178     time_t s = a.tv_sec - b.tv_sec;
179     long n;
180 
181     if (s != 0) return SGN(s);
182 
183     n = a.tv_nsec - b.tv_nsec;
184     return SGN(n);
185 }
186 
evNowTime(void)187 static struct timespec evNowTime(void) {
188     struct timespec tsnow;
189     clock_gettime(CLOCK_REALTIME, &tsnow);
190     return tsnow;
191 }
192 
evConsIovec(void * buf,size_t cnt)193 static struct iovec evConsIovec(void* buf, size_t cnt) {
194     struct iovec ret;
195 
196     memset(&ret, 0xf5, sizeof ret);
197     ret.iov_base = buf;
198     ret.iov_len = cnt;
199     return ret;
200 }
201 
202 // END: Code copied from ISC eventlib
203 
random_bind(int s,int family)204 static int random_bind(int s, int family) {
205     sockaddr_union u;
206     int j;
207     socklen_t slen;
208 
209     /* clear all, this also sets the IP4/6 address to 'any' */
210     memset(&u, 0, sizeof u);
211 
212     switch (family) {
213         case AF_INET:
214             u.sin.sin_family = family;
215             slen = sizeof u.sin;
216             break;
217         case AF_INET6:
218             u.sin6.sin6_family = family;
219             slen = sizeof u.sin6;
220             break;
221         default:
222             errno = EPROTO;
223             return -1;
224     }
225 
226     /* first try to bind to a random source port a few times */
227     for (j = 0; j < 10; j++) {
228         /* find a random port between 1025 .. 65534 */
229         int port = 1025 + (arc4random_uniform(65535 - 1025));
230         if (family == AF_INET)
231             u.sin.sin_port = htons(port);
232         else
233             u.sin6.sin6_port = htons(port);
234 
235         if (!bind(s, &u.sa, slen)) return 0;
236     }
237 
238     // nothing after 10 attempts, our network table is probably busy
239     // let the system decide which port is best
240     if (family == AF_INET)
241         u.sin.sin_port = 0;
242     else
243         u.sin6.sin6_port = 0;
244 
245     return bind(s, &u.sa, slen);
246 }
247 /* BIONIC-END */
248 
249 // Disables all nameservers other than selectedServer
res_set_usable_server(int selectedServer,int nscount,bool usable_servers[])250 static void res_set_usable_server(int selectedServer, int nscount, bool usable_servers[]) {
251     int usableIndex = 0;
252     for (int ns = 0; ns < nscount; ns++) {
253         if (usable_servers[ns]) ++usableIndex;
254         if (usableIndex != selectedServer) usable_servers[ns] = false;
255     }
256 }
257 
258 /* int
259  * res_isourserver(ina)
260  *	looks up "ina" in _res.ns_addr_list[]
261  * returns:
262  *	0  : not found
263  *	>0 : found
264  * author:
265  *	paul vixie, 29may94
266  */
res_ourserver_p(const res_state statp,const sockaddr * sa)267 static int res_ourserver_p(const res_state statp, const sockaddr* sa) {
268     const sockaddr_in *inp, *srv;
269     const sockaddr_in6 *in6p, *srv6;
270     int ns;
271 
272     switch (sa->sa_family) {
273         case AF_INET:
274             inp = (const struct sockaddr_in*) (const void*) sa;
275             for (ns = 0; ns < statp->nscount; ns++) {
276                 srv = (struct sockaddr_in*) (void*) get_nsaddr(statp, (size_t) ns);
277                 if (srv->sin_family == inp->sin_family && srv->sin_port == inp->sin_port &&
278                     (srv->sin_addr.s_addr == INADDR_ANY ||
279                      srv->sin_addr.s_addr == inp->sin_addr.s_addr))
280                     return 1;
281             }
282             break;
283         case AF_INET6:
284             if (statp->_u._ext.ext == NULL) break;
285             in6p = (const struct sockaddr_in6*) (const void*) sa;
286             for (ns = 0; ns < statp->nscount; ns++) {
287                 srv6 = (struct sockaddr_in6*) (void*) get_nsaddr(statp, (size_t) ns);
288                 if (srv6->sin6_family == in6p->sin6_family && srv6->sin6_port == in6p->sin6_port &&
289 #ifdef HAVE_SIN6_SCOPE_ID
290                     (srv6->sin6_scope_id == 0 || srv6->sin6_scope_id == in6p->sin6_scope_id) &&
291 #endif
292                     (IN6_IS_ADDR_UNSPECIFIED(&srv6->sin6_addr) ||
293                      IN6_ARE_ADDR_EQUAL(&srv6->sin6_addr, &in6p->sin6_addr)))
294                     return 1;
295             }
296             break;
297         default:
298             break;
299     }
300     return 0;
301 }
302 
303 /* int
304  * res_nameinquery(name, type, cl, buf, eom)
305  *	look for (name, type, cl) in the query section of packet (buf, eom)
306  * requires:
307  *	buf + HFIXEDSZ <= eom
308  * returns:
309  *	-1 : format error
310  *	0  : not found
311  *	>0 : found
312  * author:
313  *	paul vixie, 29may94
314  */
res_nameinquery(const char * name,int type,int cl,const u_char * buf,const u_char * eom)315 int res_nameinquery(const char* name, int type, int cl, const u_char* buf, const u_char* eom) {
316     const u_char* cp = buf + HFIXEDSZ;
317     int qdcount = ntohs(((const HEADER*) (const void*) buf)->qdcount);
318 
319     while (qdcount-- > 0) {
320         char tname[MAXDNAME + 1];
321         int n = dn_expand(buf, eom, cp, tname, sizeof tname);
322         if (n < 0) return (-1);
323         cp += n;
324         if (cp + 2 * INT16SZ > eom) return (-1);
325         int ttype = ntohs(*reinterpret_cast<const uint16_t*>(cp));
326         cp += INT16SZ;
327         int tclass = ntohs(*reinterpret_cast<const uint16_t*>(cp));
328         cp += INT16SZ;
329         if (ttype == type && tclass == cl && ns_samename(tname, name) == 1) return (1);
330     }
331     return (0);
332 }
333 
334 /* int
335  * res_queriesmatch(buf1, eom1, buf2, eom2)
336  *	is there a 1:1 mapping of (name,type,class)
337  *	in (buf1,eom1) and (buf2,eom2)?
338  * returns:
339  *	-1 : format error
340  *	0  : not a 1:1 mapping
341  *	>0 : is a 1:1 mapping
342  * author:
343  *	paul vixie, 29may94
344  */
res_queriesmatch(const u_char * buf1,const u_char * eom1,const u_char * buf2,const u_char * eom2)345 int res_queriesmatch(const u_char* buf1, const u_char* eom1, const u_char* buf2,
346                      const u_char* eom2) {
347     const u_char* cp = buf1 + HFIXEDSZ;
348     int qdcount = ntohs(((const HEADER*) (const void*) buf1)->qdcount);
349 
350     if (buf1 + HFIXEDSZ > eom1 || buf2 + HFIXEDSZ > eom2) return (-1);
351 
352     /*
353      * Only header section present in replies to
354      * dynamic update packets.
355      */
356     if ((((const HEADER*) (const void*) buf1)->opcode == ns_o_update) &&
357         (((const HEADER*) (const void*) buf2)->opcode == ns_o_update))
358         return (1);
359 
360     if (qdcount != ntohs(((const HEADER*) (const void*) buf2)->qdcount)) return (0);
361     while (qdcount-- > 0) {
362         char tname[MAXDNAME + 1];
363         int n = dn_expand(buf1, eom1, cp, tname, sizeof tname);
364         if (n < 0) return (-1);
365         cp += n;
366         if (cp + 2 * INT16SZ > eom1) return (-1);
367         int ttype = ntohs(*reinterpret_cast<const uint16_t*>(cp));
368         cp += INT16SZ;
369         int tclass = ntohs(*reinterpret_cast<const uint16_t*>(cp));
370         cp += INT16SZ;
371         if (!res_nameinquery(tname, ttype, tclass, buf2, eom2)) return (0);
372     }
373     return (1);
374 }
375 
res_nsend(res_state statp,const u_char * buf,int buflen,u_char * ans,int anssiz,int * rcode,uint32_t flags)376 int res_nsend(res_state statp, const u_char* buf, int buflen, u_char* ans, int anssiz, int* rcode,
377               uint32_t flags) {
378     int gotsomewhere, terrno, v_circuit, resplen, n;
379     ResolvCacheStatus cache_status = RESOLV_CACHE_UNSUPPORTED;
380 
381     if (anssiz < HFIXEDSZ) {
382         // TODO: Remove errno once callers stop using it
383         errno = EINVAL;
384         return -EINVAL;
385     }
386     LOG(DEBUG) << __func__;
387     res_pquery(buf, buflen);
388 
389     v_circuit = (statp->options & RES_USEVC) || buflen > PACKETSZ;
390     gotsomewhere = 0;
391     terrno = ETIMEDOUT;
392 
393     int anslen = 0;
394     cache_status = _resolv_cache_lookup(statp->netid, buf, buflen, ans, anssiz, &anslen, flags);
395 
396     if (cache_status == RESOLV_CACHE_FOUND) {
397         HEADER* hp = (HEADER*)(void*)ans;
398         *rcode = hp->rcode;
399         return anslen;
400     } else if (cache_status != RESOLV_CACHE_UNSUPPORTED) {
401         // had a cache miss for a known network, so populate the thread private
402         // data so the normal resolve path can do its thing
403         _resolv_populate_res_for_net(statp);
404     }
405     if (statp->nscount == 0) {
406         // We have no nameservers configured, so there's no point trying.
407         // Tell the cache the query failed, or any retries and anyone else asking the same
408         // question will block for PENDING_REQUEST_TIMEOUT seconds instead of failing fast.
409         _resolv_cache_query_failed(statp->netid, buf, buflen, flags);
410 
411         // TODO: Remove errno once callers stop using it
412         errno = ESRCH;
413         return -ESRCH;
414     }
415 
416     /*
417      * If the ns_addr_list in the resolver context has changed, then
418      * invalidate our cached copy and the associated timing data.
419      */
420     if (statp->_u._ext.nscount != 0) {
421         int needclose = 0;
422         struct sockaddr_storage peer;
423         socklen_t peerlen;
424 
425         if (statp->_u._ext.nscount != statp->nscount) {
426             needclose++;
427         } else {
428             for (int ns = 0; ns < statp->nscount; ns++) {
429                 if (statp->nsaddr_list[ns].sin_family &&
430                     !sock_eq((struct sockaddr*) (void*) &statp->nsaddr_list[ns],
431                              (struct sockaddr*) (void*) &statp->_u._ext.ext->nsaddrs[ns])) {
432                     needclose++;
433                     break;
434                 }
435 
436                 if (statp->_u._ext.nssocks[ns] == -1) continue;
437                 peerlen = sizeof(peer);
438                 if (getpeername(statp->_u._ext.nssocks[ns], (struct sockaddr*) (void*) &peer,
439                                 &peerlen) < 0) {
440                     needclose++;
441                     break;
442                 }
443                 if (!sock_eq((struct sockaddr*) (void*) &peer, get_nsaddr(statp, (size_t) ns))) {
444                     needclose++;
445                     break;
446                 }
447             }
448         }
449         if (needclose) {
450             res_nclose(statp);
451             statp->_u._ext.nscount = 0;
452         }
453     }
454 
455     /*
456      * Maybe initialize our private copy of the ns_addr_list.
457      */
458     if (statp->_u._ext.nscount == 0) {
459         for (int ns = 0; ns < statp->nscount; ns++) {
460             statp->_u._ext.nstimes[ns] = RES_MAXTIME;
461             statp->_u._ext.nssocks[ns] = -1;
462             if (!statp->nsaddr_list[ns].sin_family) continue;
463             statp->_u._ext.ext->nsaddrs[ns].sin = statp->nsaddr_list[ns];
464         }
465         statp->_u._ext.nscount = statp->nscount;
466     }
467 
468     /*
469      * Some resolvers want to even out the load on their nameservers.
470      * Note that RES_BLAST overrides RES_ROTATE.
471      */
472     if ((statp->options & RES_ROTATE) != 0U && (statp->options & RES_BLAST) == 0U) {
473         sockaddr_union inu;
474         struct sockaddr_in ina;
475         int lastns = statp->nscount - 1;
476         int fd;
477         u_int16_t nstime;
478 
479         if (statp->_u._ext.ext != NULL) inu = statp->_u._ext.ext->nsaddrs[0];
480         ina = statp->nsaddr_list[0];
481         fd = statp->_u._ext.nssocks[0];
482         nstime = statp->_u._ext.nstimes[0];
483         for (int ns = 0; ns < lastns; ns++) {
484             if (statp->_u._ext.ext != NULL)
485                 statp->_u._ext.ext->nsaddrs[ns] = statp->_u._ext.ext->nsaddrs[ns + 1];
486             statp->nsaddr_list[ns] = statp->nsaddr_list[ns + 1];
487             statp->_u._ext.nssocks[ns] = statp->_u._ext.nssocks[ns + 1];
488             statp->_u._ext.nstimes[ns] = statp->_u._ext.nstimes[ns + 1];
489         }
490         if (statp->_u._ext.ext != NULL) statp->_u._ext.ext->nsaddrs[lastns] = inu;
491         statp->nsaddr_list[lastns] = ina;
492         statp->_u._ext.nssocks[lastns] = fd;
493         statp->_u._ext.nstimes[lastns] = nstime;
494     }
495 
496     res_stats stats[MAXNS];
497     res_params params;
498     int revision_id = resolv_cache_get_resolver_stats(statp->netid, &params, stats);
499     if (revision_id < 0) {
500         // TODO: Remove errno once callers stop using it
501         errno = ESRCH;
502         return -ESRCH;
503     }
504     bool usable_servers[MAXNS];
505     int usableServersCount = android_net_res_stats_get_usable_servers(
506             &params, stats, statp->nscount, usable_servers);
507 
508     if ((flags & ANDROID_RESOLV_NO_RETRY) && usableServersCount > 1) {
509         auto hp = reinterpret_cast<const HEADER*>(buf);
510 
511         // Select a random server based on the query id
512         int selectedServer = (hp->id % usableServersCount) + 1;
513         res_set_usable_server(selectedServer, statp->nscount, usable_servers);
514     }
515 
516     /*
517      * Send request, RETRY times, or until successful.
518      */
519     int retryTimes = (flags & ANDROID_RESOLV_NO_RETRY) ? 1 : params.retry_count;
520 
521     for (int attempt = 0; attempt < retryTimes; ++attempt) {
522 
523         for (int ns = 0; ns < statp->nscount; ns++) {
524             if (!usable_servers[ns]) continue;
525             struct sockaddr* nsap;
526             int nsaplen;
527             time_t now = 0;
528             int delay = 0;
529             *rcode = RCODE_INTERNAL_ERROR;
530             nsap = get_nsaddr(statp, (size_t) ns);
531             nsaplen = get_salen(nsap);
532 
533         same_ns:
534             // TODO: Since we expect there is only one DNS server being queried here while this
535             // function tries to query all of private DNS servers. Consider moving it to other
536             // reasonable place. In addition, maybe add stats for private DNS.
537             if (!statp->use_local_nameserver) {
538                 bool fallback = false;
539                 resplen = res_tls_send(statp, Slice(const_cast<u_char*>(buf), buflen),
540                                        Slice(ans, anssiz), rcode, &fallback);
541                 if (resplen > 0) {
542                     if (cache_status == RESOLV_CACHE_NOTFOUND) {
543                         _resolv_cache_add(statp->netid, buf, buflen, ans, resplen);
544                     }
545                     return resplen;
546                 }
547                 if (!fallback) {
548                     _resolv_cache_query_failed(statp->netid, buf, buflen, flags);
549                     res_nclose(statp);
550                     return -terrno;
551                 }
552             }
553 
554             [[maybe_unused]] static const int niflags = NI_NUMERICHOST | NI_NUMERICSERV;
555             [[maybe_unused]] char abuf[NI_MAXHOST];
556 
557             if (getnameinfo(nsap, (socklen_t)nsaplen, abuf, sizeof(abuf), NULL, 0, niflags) == 0)
558                 LOG(DEBUG) << __func__ << ": Querying server (# " << ns + 1
559                            << ") address = " << abuf;
560 
561             if (v_circuit) {
562                 /* Use VC; at most one attempt per server. */
563                 bool shouldRecordStats = (attempt == 0);
564                 attempt = retryTimes;
565 
566                 n = send_vc(statp, &params, buf, buflen, ans, anssiz, &terrno, ns, &now, rcode,
567                             &delay);
568 
569                 /*
570                  * Only record stats the first time we try a query. This ensures that
571                  * queries that deterministically fail (e.g., a name that always returns
572                  * SERVFAIL or times out) do not unduly affect the stats.
573                  */
574                 if (shouldRecordStats) {
575                     res_sample sample;
576                     _res_stats_set_sample(&sample, now, *rcode, delay);
577                     _resolv_cache_add_resolver_stats_sample(statp->netid, revision_id, ns, &sample,
578                                                             params.max_samples);
579                 }
580 
581                 LOG(INFO) << __func__ << ": used send_vc " << n;
582 
583                 if (n < 0) {
584                     _resolv_cache_query_failed(statp->netid, buf, buflen, flags);
585                     res_nclose(statp);
586                     return -terrno;
587                 };
588                 if (n == 0) goto next_ns;
589                 resplen = n;
590             } else {
591                 /* Use datagrams. */
592                 LOG(INFO) << __func__ << ": using send_dg";
593 
594                 n = send_dg(statp, &params, buf, buflen, ans, anssiz, &terrno, ns, &v_circuit,
595                             &gotsomewhere, &now, rcode, &delay);
596 
597                 /* Only record stats the first time we try a query. See above. */
598                 if (attempt == 0) {
599                     res_sample sample;
600                     _res_stats_set_sample(&sample, now, *rcode, delay);
601                     _resolv_cache_add_resolver_stats_sample(statp->netid, revision_id, ns, &sample,
602                                                             params.max_samples);
603                 }
604 
605                 LOG(INFO) << __func__ << ": used send_dg " << n;
606 
607                 if (n < 0) {
608                     _resolv_cache_query_failed(statp->netid, buf, buflen, flags);
609                     res_nclose(statp);
610                     return -terrno;
611                 };
612                 if (n == 0) goto next_ns;
613                 if (v_circuit) goto same_ns;
614                 resplen = n;
615             }
616 
617             LOG(DEBUG) << __func__ << ": got answer:";
618             res_pquery(ans, (resplen > anssiz) ? anssiz : resplen);
619 
620             if (cache_status == RESOLV_CACHE_NOTFOUND) {
621                 _resolv_cache_add(statp->netid, buf, buflen, ans, resplen);
622             }
623             /*
624              * If we have temporarily opened a virtual circuit,
625              * or if we haven't been asked to keep a socket open,
626              * close the socket.
627              */
628             if ((v_circuit && (statp->options & RES_USEVC) == 0U) ||
629                 (statp->options & RES_STAYOPEN) == 0U) {
630                 res_nclose(statp);
631             }
632             return (resplen);
633         next_ns:;
634         }  // for each ns
635     }  // for each retry
636     res_nclose(statp);
637     if (!v_circuit) {
638         if (!gotsomewhere) {
639             // TODO: Remove errno once callers stop using it
640             errno = ECONNREFUSED; /* no nameservers found */
641             terrno = ECONNREFUSED;
642         } else {
643             // TODO: Remove errno once callers stop using it
644             errno = ETIMEDOUT; /* no answer obtained */
645             terrno = ETIMEDOUT;
646         }
647     } else {
648         errno = terrno;
649     }
650     _resolv_cache_query_failed(statp->netid, buf, buflen, flags);
651     return -terrno;
652 }
653 
654 /* Private */
655 
get_salen(const struct sockaddr * sa)656 static int get_salen(const struct sockaddr* sa) {
657     if (sa->sa_family == AF_INET)
658         return (sizeof(struct sockaddr_in));
659     else if (sa->sa_family == AF_INET6)
660         return (sizeof(struct sockaddr_in6));
661     else
662         return (0); /* unknown, die on connect */
663 }
664 
665 /*
666  * pick appropriate nsaddr_list for use.  see res_init() for initialization.
667  */
get_nsaddr(res_state statp,size_t n)668 static struct sockaddr* get_nsaddr(res_state statp, size_t n) {
669     if (!statp->nsaddr_list[n].sin_family && statp->_u._ext.ext) {
670         /*
671          * - statp->_u._ext.ext->nsaddrs[n] holds an address that is larger
672          *   than struct sockaddr, and
673          * - user code did not update statp->nsaddr_list[n].
674          */
675         return (struct sockaddr*) (void*) &statp->_u._ext.ext->nsaddrs[n];
676     } else {
677         /*
678          * - user code updated statp->nsaddr_list[n], or
679          * - statp->nsaddr_list[n] has the same content as
680          *   statp->_u._ext.ext->nsaddrs[n].
681          */
682         return (struct sockaddr*) (void*) &statp->nsaddr_list[n];
683     }
684 }
685 
get_timeout(const res_state statp,const res_params * params,const int ns)686 static struct timespec get_timeout(const res_state statp, const res_params* params, const int ns) {
687     int msec;
688     // Legacy algorithm which scales the timeout by nameserver number.
689     // For instance, with 4 nameservers: 5s, 2.5s, 5s, 10s
690     // This has no effect with 1 or 2 nameservers
691     msec = params->base_timeout_msec << ns;
692     if (ns > 0) {
693         msec /= statp->nscount;
694     }
695     // For safety, don't allow OEMs and experiments to configure a timeout shorter than 1s.
696     if (msec < 1000) {
697         msec = 1000;  // Use at least 1000ms
698     }
699     LOG(INFO) << __func__ << ": using timeout of " << msec << " msec";
700 
701     struct timespec result;
702     result.tv_sec = msec / 1000;
703     result.tv_nsec = (msec % 1000) * 1000000;
704     return result;
705 }
706 
send_vc(res_state statp,res_params * params,const u_char * buf,int buflen,u_char * ans,int anssiz,int * terrno,int ns,time_t * at,int * rcode,int * delay)707 static int send_vc(res_state statp, res_params* params, const u_char* buf, int buflen, u_char* ans,
708                    int anssiz, int* terrno, int ns, time_t* at, int* rcode, int* delay) {
709     *at = time(NULL);
710     *delay = 0;
711     const HEADER* hp = (const HEADER*) (const void*) buf;
712     HEADER* anhp = (HEADER*) (void*) ans;
713     struct sockaddr* nsap;
714     int nsaplen;
715     int truncating, connreset, n;
716     struct iovec iov[2];
717     u_char* cp;
718 
719     LOG(INFO) << __func__ << ": using send_vc";
720 
721     nsap = get_nsaddr(statp, (size_t) ns);
722     nsaplen = get_salen(nsap);
723 
724     connreset = 0;
725 same_ns:
726     truncating = 0;
727 
728     struct timespec now = evNowTime();
729 
730     /* Are we still talking to whom we want to talk to? */
731     if (statp->_vcsock >= 0 && (statp->_flags & RES_F_VC) != 0) {
732         struct sockaddr_storage peer;
733         socklen_t size = sizeof peer;
734         unsigned old_mark;
735         socklen_t mark_size = sizeof(old_mark);
736         if (getpeername(statp->_vcsock, (struct sockaddr*) (void*) &peer, &size) < 0 ||
737             !sock_eq((struct sockaddr*) (void*) &peer, nsap) ||
738             getsockopt(statp->_vcsock, SOL_SOCKET, SO_MARK, &old_mark, &mark_size) < 0 ||
739             old_mark != statp->_mark) {
740             res_nclose(statp);
741             statp->_flags &= ~RES_F_VC;
742         }
743     }
744 
745     if (statp->_vcsock < 0 || (statp->_flags & RES_F_VC) == 0) {
746         if (statp->_vcsock >= 0) res_nclose(statp);
747 
748         statp->_vcsock = socket(nsap->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0);
749         if (statp->_vcsock < 0) {
750             switch (errno) {
751                 case EPROTONOSUPPORT:
752                 case EPFNOSUPPORT:
753                 case EAFNOSUPPORT:
754                     Perror(statp, "socket(vc)", errno);
755                     return 0;
756                 default:
757                     *terrno = errno;
758                     Perror(statp, "socket(vc)", errno);
759                     return -1;
760             }
761         }
762         fchown(statp->_vcsock, AID_DNS, -1);
763         if (statp->_mark != MARK_UNSET) {
764             if (setsockopt(statp->_vcsock, SOL_SOCKET, SO_MARK, &statp->_mark,
765                            sizeof(statp->_mark)) < 0) {
766                 *terrno = errno;
767                 Perror(statp, "setsockopt", errno);
768                 return -1;
769             }
770         }
771         errno = 0;
772         if (random_bind(statp->_vcsock, nsap->sa_family) < 0) {
773             *terrno = errno;
774             Aerror(statp, "bind/vc", errno, nsap, nsaplen);
775             res_nclose(statp);
776             return (0);
777         }
778         if (connect_with_timeout(statp->_vcsock, nsap, (socklen_t) nsaplen,
779                                  get_timeout(statp, params, ns)) < 0) {
780             *terrno = errno;
781             Aerror(statp, "connect/vc", errno, nsap, nsaplen);
782             res_nclose(statp);
783             /*
784              * The way connect_with_timeout() is implemented prevents us from reliably
785              * determining whether this was really a timeout or e.g. ECONNREFUSED. Since
786              * currently both cases are handled in the same way, there is no need to
787              * change this (yet). If we ever need to reliably distinguish between these
788              * cases, both connect_with_timeout() and retrying_poll() need to be
789              * modified, though.
790              */
791             *rcode = RCODE_TIMEOUT;
792             return (0);
793         }
794         statp->_flags |= RES_F_VC;
795     }
796 
797     /*
798      * Send length & message
799      */
800     uint16_t len = htons(static_cast<uint16_t>(buflen));
801     iov[0] = evConsIovec(&len, INT16SZ);
802     iov[1] = evConsIovec((void*) buf, (size_t) buflen);
803     if (writev(statp->_vcsock, iov, 2) != (INT16SZ + buflen)) {
804         *terrno = errno;
805         Perror(statp, "write failed", errno);
806         res_nclose(statp);
807         return (0);
808     }
809     /*
810      * Receive length & response
811      */
812 read_len:
813     cp = ans;
814     len = INT16SZ;
815     while ((n = read(statp->_vcsock, (char*) cp, (size_t) len)) > 0) {
816         cp += n;
817         if ((len -= n) == 0) break;
818     }
819     if (n <= 0) {
820         *terrno = errno;
821         Perror(statp, "read failed", errno);
822         res_nclose(statp);
823         /*
824          * A long running process might get its TCP
825          * connection reset if the remote server was
826          * restarted.  Requery the server instead of
827          * trying a new one.  When there is only one
828          * server, this means that a query might work
829          * instead of failing.  We only allow one reset
830          * per query to prevent looping.
831          */
832         if (*terrno == ECONNRESET && !connreset) {
833             connreset = 1;
834             res_nclose(statp);
835             goto same_ns;
836         }
837         res_nclose(statp);
838         return (0);
839     }
840     uint16_t resplen = ntohs(*reinterpret_cast<const uint16_t*>(ans));
841     if (resplen > anssiz) {
842         LOG(DEBUG) << __func__ << ": response truncated";
843         truncating = 1;
844         len = anssiz;
845     } else
846         len = resplen;
847     if (len < HFIXEDSZ) {
848         /*
849          * Undersized message.
850          */
851         LOG(DEBUG) << __func__ << ": undersized: " << len;
852         *terrno = EMSGSIZE;
853         res_nclose(statp);
854         return (0);
855     }
856     cp = ans;
857     while (len != 0 && (n = read(statp->_vcsock, (char*) cp, (size_t) len)) > 0) {
858         cp += n;
859         len -= n;
860     }
861     if (n <= 0) {
862         *terrno = errno;
863         Perror(statp, "read(vc)", errno);
864         res_nclose(statp);
865         return (0);
866     }
867 
868     if (truncating) {
869         /*
870          * Flush rest of answer so connection stays in synch.
871          */
872         anhp->tc = 1;
873         len = resplen - anssiz;
874         while (len != 0) {
875             char junk[PACKETSZ];
876 
877             n = read(statp->_vcsock, junk, (len > sizeof junk) ? sizeof junk : len);
878             if (n > 0)
879                 len -= n;
880             else
881                 break;
882         }
883     }
884     /*
885      * If the calling application has bailed out of
886      * a previous call and failed to arrange to have
887      * the circuit closed or the server has got
888      * itself confused, then drop the packet and
889      * wait for the correct one.
890      */
891     if (hp->id != anhp->id) {
892         LOG(DEBUG) << __func__ << ": ld answer (unexpected):";
893         res_pquery(ans, (resplen > anssiz) ? anssiz : resplen);
894         goto read_len;
895     }
896 
897     /*
898      * All is well, or the error is fatal.  Signal that the
899      * next nameserver ought not be tried.
900      */
901     if (resplen > 0) {
902         struct timespec done = evNowTime();
903         *delay = _res_stats_calculate_rtt(&done, &now);
904         *rcode = anhp->rcode;
905     }
906     return (resplen);
907 }
908 
909 /* return -1 on error (errno set), 0 on success */
connect_with_timeout(int sock,const struct sockaddr * nsap,socklen_t salen,const struct timespec timeout)910 static int connect_with_timeout(int sock, const struct sockaddr* nsap, socklen_t salen,
911                                 const struct timespec timeout) {
912     int res, origflags;
913 
914     origflags = fcntl(sock, F_GETFL, 0);
915     fcntl(sock, F_SETFL, origflags | O_NONBLOCK);
916 
917     res = connect(sock, nsap, salen);
918     if (res < 0 && errno != EINPROGRESS) {
919         res = -1;
920         goto done;
921     }
922     if (res != 0) {
923         struct timespec now = evNowTime();
924         struct timespec finish = evAddTime(now, timeout);
925         LOG(INFO) << __func__ << ": " << sock << " send_vc";
926         res = retrying_poll(sock, POLLIN | POLLOUT, &finish);
927         if (res <= 0) {
928             res = -1;
929         }
930     }
931 done:
932     fcntl(sock, F_SETFL, origflags);
933     LOG(INFO) << __func__ << ": " << sock << " connect_with_const timeout returning " << res;
934     return res;
935 }
936 
retrying_poll(const int sock,const short events,const struct timespec * finish)937 static int retrying_poll(const int sock, const short events, const struct timespec* finish) {
938     struct timespec now, timeout;
939 
940 retry:
941     LOG(INFO) << __func__ << ": " << sock << " retrying_poll";
942 
943     now = evNowTime();
944     if (evCmpTime(*finish, now) > 0)
945         timeout = evSubTime(*finish, now);
946     else
947         timeout = evConsTime(0L, 0L);
948     struct pollfd fds = {.fd = sock, .events = events};
949     int n = ppoll(&fds, 1, &timeout, /*sigmask=*/NULL);
950     if (n == 0) {
951         LOG(INFO) << __func__ << ": " << sock << "retrying_poll timeout";
952         errno = ETIMEDOUT;
953         return 0;
954     }
955     if (n < 0) {
956         if (errno == EINTR) goto retry;
957         PLOG(INFO) << __func__ << ": " << sock << " retrying_poll failed";
958         return n;
959     }
960     if (fds.revents & (POLLIN | POLLOUT | POLLERR)) {
961         int error;
962         socklen_t len = sizeof(error);
963         if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0 || error) {
964             errno = error;
965             PLOG(INFO) << __func__ << ": " << sock << " retrying_poll getsockopt failed";
966             return -1;
967         }
968     }
969     LOG(INFO) << __func__ << ": " << sock << " retrying_poll returning " << n;
970     return n;
971 }
972 
send_dg(res_state statp,res_params * params,const u_char * buf,int buflen,u_char * ans,int anssiz,int * terrno,int ns,int * v_circuit,int * gotsomewhere,time_t * at,int * rcode,int * delay)973 static int send_dg(res_state statp, res_params* params, const u_char* buf, int buflen, u_char* ans,
974                    int anssiz, int* terrno, int ns, int* v_circuit, int* gotsomewhere, time_t* at,
975                    int* rcode, int* delay) {
976     *at = time(NULL);
977     *delay = 0;
978     const HEADER* hp = (const HEADER*) (const void*) buf;
979     HEADER* anhp = (HEADER*) (void*) ans;
980     const struct sockaddr* nsap;
981     int nsaplen;
982     struct timespec now, timeout, finish, done;
983     struct sockaddr_storage from;
984     socklen_t fromlen;
985     int resplen, n, s;
986 
987     nsap = get_nsaddr(statp, (size_t) ns);
988     nsaplen = get_salen(nsap);
989     if (statp->_u._ext.nssocks[ns] == -1) {
990         statp->_u._ext.nssocks[ns] = socket(nsap->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
991         if (statp->_u._ext.nssocks[ns] < 0) {
992             switch (errno) {
993                 case EPROTONOSUPPORT:
994                 case EPFNOSUPPORT:
995                 case EAFNOSUPPORT:
996                     Perror(statp, "socket(dg)", errno);
997                     return (0);
998                 default:
999                     *terrno = errno;
1000                     Perror(statp, "socket(dg)", errno);
1001                     return (-1);
1002             }
1003         }
1004 
1005         fchown(statp->_u._ext.nssocks[ns], AID_DNS, -1);
1006         if (statp->_mark != MARK_UNSET) {
1007             if (setsockopt(statp->_u._ext.nssocks[ns], SOL_SOCKET, SO_MARK, &(statp->_mark),
1008                            sizeof(statp->_mark)) < 0) {
1009                 res_nclose(statp);
1010                 return -1;
1011             }
1012         }
1013 #ifndef CANNOT_CONNECT_DGRAM
1014         /*
1015          * On a 4.3BSD+ machine (client and server,
1016          * actually), sending to a nameserver datagram
1017          * port with no nameserver will cause an
1018          * ICMP port unreachable message to be returned.
1019          * If our datagram socket is "connected" to the
1020          * server, we get an ECONNREFUSED error on the next
1021          * socket operation, and select returns if the
1022          * error message is received.  We can thus detect
1023          * the absence of a nameserver without timing out.
1024          */
1025         if (random_bind(statp->_u._ext.nssocks[ns], nsap->sa_family) < 0) {
1026             Aerror(statp, "bind(dg)", errno, nsap, nsaplen);
1027             res_nclose(statp);
1028             return (0);
1029         }
1030         if (connect(statp->_u._ext.nssocks[ns], nsap, (socklen_t) nsaplen) < 0) {
1031             Aerror(statp, "connect(dg)", errno, nsap, nsaplen);
1032             res_nclose(statp);
1033             return (0);
1034         }
1035 #endif /* !CANNOT_CONNECT_DGRAM */
1036         LOG(DEBUG) << __func__ << ": new DG socket";
1037     }
1038     s = statp->_u._ext.nssocks[ns];
1039 #ifndef CANNOT_CONNECT_DGRAM
1040     if (send(s, (const char*) buf, (size_t) buflen, 0) != buflen) {
1041         Perror(statp, "send", errno);
1042         res_nclose(statp);
1043         return 0;
1044     }
1045 #else  /* !CANNOT_CONNECT_DGRAM */
1046     if (sendto(s, (const char*) buf, buflen, 0, nsap, nsaplen) != buflen) {
1047         Aerror(statp, "sendto", errno, nsap, nsaplen);
1048         res_nclose(statp);
1049         return 0;
1050     }
1051 #endif /* !CANNOT_CONNECT_DGRAM */
1052 
1053     // Wait for reply.
1054     timeout = get_timeout(statp, params, ns);
1055     now = evNowTime();
1056     finish = evAddTime(now, timeout);
1057 retry:
1058     n = retrying_poll(s, POLLIN, &finish);
1059 
1060     if (n == 0) {
1061         *rcode = RCODE_TIMEOUT;
1062         LOG(DEBUG) << __func__ << ": timeout";
1063         *gotsomewhere = 1;
1064         return 0;
1065     }
1066     if (n < 0) {
1067         Perror(statp, "poll", errno);
1068         res_nclose(statp);
1069         return 0;
1070     }
1071     errno = 0;
1072     fromlen = sizeof(from);
1073     resplen = recvfrom(s, (char*) ans, (size_t) anssiz, 0, (struct sockaddr*) (void*) &from,
1074                        &fromlen);
1075     if (resplen <= 0) {
1076         Perror(statp, "recvfrom", errno);
1077         res_nclose(statp);
1078         return 0;
1079     }
1080     *gotsomewhere = 1;
1081     if (resplen < HFIXEDSZ) {
1082         /*
1083          * Undersized message.
1084          */
1085         LOG(DEBUG) << __func__ << ": undersized: " << resplen;
1086         *terrno = EMSGSIZE;
1087         res_nclose(statp);
1088         return 0;
1089     }
1090     if (hp->id != anhp->id) {
1091         /*
1092          * response from old query, ignore it.
1093          * XXX - potential security hazard could
1094          *	 be detected here.
1095          */
1096         LOG(DEBUG) << __func__ << ": old answer:";
1097         res_pquery(ans, (resplen > anssiz) ? anssiz : resplen);
1098         goto retry;
1099     }
1100     if (!(statp->options & RES_INSECURE1) &&
1101         !res_ourserver_p(statp, (struct sockaddr*) (void*) &from)) {
1102         /*
1103          * response from wrong server? ignore it.
1104          * XXX - potential security hazard could
1105          *	 be detected here.
1106          */
1107         LOG(DEBUG) << __func__ << ": not our server:";
1108         res_pquery(ans, (resplen > anssiz) ? anssiz : resplen);
1109         goto retry;
1110     }
1111     if (anhp->rcode == FORMERR && (statp->options & RES_USE_EDNS0) != 0U) {
1112         /*
1113          * Do not retry if the server do not understand EDNS0.
1114          * The case has to be captured here, as FORMERR packet do not
1115          * carry query section, hence res_queriesmatch() returns 0.
1116          */
1117         LOG(DEBUG) << __func__ << ": server rejected query with EDNS0:";
1118         res_pquery(ans, (resplen > anssiz) ? anssiz : resplen);
1119         /* record the error */
1120         statp->_flags |= RES_F_EDNS0ERR;
1121         res_nclose(statp);
1122         return 0;
1123     }
1124     if (!(statp->options & RES_INSECURE2) &&
1125         !res_queriesmatch(buf, buf + buflen, ans, ans + anssiz)) {
1126         /*
1127          * response contains wrong query? ignore it.
1128          * XXX - potential security hazard could
1129          *	 be detected here.
1130          */
1131         LOG(DEBUG) << __func__ << ": wrong query name:";
1132         res_pquery(ans, (resplen > anssiz) ? anssiz : resplen);
1133         goto retry;
1134     }
1135     done = evNowTime();
1136     *delay = _res_stats_calculate_rtt(&done, &now);
1137     if (anhp->rcode == SERVFAIL || anhp->rcode == NOTIMP || anhp->rcode == REFUSED) {
1138         LOG(DEBUG) << __func__ << ": server rejected query:";
1139         res_pquery(ans, (resplen > anssiz) ? anssiz : resplen);
1140         res_nclose(statp);
1141         *rcode = anhp->rcode;
1142         return 0;
1143     }
1144     if (!(statp->options & RES_IGNTC) && anhp->tc) {
1145         /*
1146          * To get the rest of answer,
1147          * use TCP with same server.
1148          */
1149         LOG(DEBUG) << __func__ << ": truncated answer";
1150         *v_circuit = 1;
1151         res_nclose(statp);
1152         return 1;
1153     }
1154     /*
1155      * All is well, or the error is fatal.  Signal that the
1156      * next nameserver ought not be tried.
1157      */
1158     if (resplen > 0) {
1159         *rcode = anhp->rcode;
1160     }
1161     return resplen;
1162 }
1163 
Aerror(const res_state statp,const char * string,int error,const struct sockaddr * address,int alen)1164 static void Aerror(const res_state statp, const char* string, int error,
1165                    const struct sockaddr* address, int alen) {
1166     const int save = errno;
1167     char hbuf[NI_MAXHOST];
1168     char sbuf[NI_MAXSERV];
1169     constexpr int niflags = NI_NUMERICHOST | NI_NUMERICSERV;
1170 
1171     if ((statp->options & RES_DEBUG) != 0U) {
1172         if (getnameinfo(address, (socklen_t) alen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf),
1173                         niflags)) {
1174             strncpy(hbuf, "?", sizeof(hbuf) - 1);
1175             hbuf[sizeof(hbuf) - 1] = '\0';
1176             strncpy(sbuf, "?", sizeof(sbuf) - 1);
1177             sbuf[sizeof(sbuf) - 1] = '\0';
1178         }
1179         LOG(DEBUG) << __func__ << ": " << string << " ([" << hbuf << "]." << sbuf
1180                    << "): " << strerror(error);
1181     }
1182     errno = save;
1183 }
1184 
Perror(const res_state statp,const char * string,int error)1185 static void Perror(const res_state statp, const char* string, int error) {
1186     if ((statp->options & RES_DEBUG) != 0U) {
1187         LOG(DEBUG) << __func__ << ": " << string << ": " << strerror(error);
1188     }
1189 }
1190 
sock_eq(struct sockaddr * a,struct sockaddr * b)1191 static int sock_eq(struct sockaddr* a, struct sockaddr* b) {
1192     struct sockaddr_in *a4, *b4;
1193     struct sockaddr_in6 *a6, *b6;
1194 
1195     if (a->sa_family != b->sa_family) return 0;
1196     switch (a->sa_family) {
1197         case AF_INET:
1198             a4 = (struct sockaddr_in*) (void*) a;
1199             b4 = (struct sockaddr_in*) (void*) b;
1200             return a4->sin_port == b4->sin_port && a4->sin_addr.s_addr == b4->sin_addr.s_addr;
1201         case AF_INET6:
1202             a6 = (struct sockaddr_in6*) (void*) a;
1203             b6 = (struct sockaddr_in6*) (void*) b;
1204             return a6->sin6_port == b6->sin6_port &&
1205 #ifdef HAVE_SIN6_SCOPE_ID
1206                    a6->sin6_scope_id == b6->sin6_scope_id &&
1207 #endif
1208                    IN6_ARE_ADDR_EQUAL(&a6->sin6_addr, &b6->sin6_addr);
1209         default:
1210             return 0;
1211     }
1212 }
1213 
res_tls_send(res_state statp,const Slice query,const Slice answer,int * rcode,bool * fallback)1214 static int res_tls_send(res_state statp, const Slice query, const Slice answer, int* rcode,
1215                         bool* fallback) {
1216     int resplen = 0;
1217     const unsigned netId = statp->netid;
1218     const unsigned mark = statp->_mark;
1219 
1220     PrivateDnsStatus privateDnsStatus = gPrivateDnsConfiguration.getStatus(netId);
1221 
1222     if (privateDnsStatus.mode == PrivateDnsMode::OFF) {
1223         *fallback = true;
1224         return -1;
1225     }
1226 
1227     if (privateDnsStatus.validatedServers.empty()) {
1228         if (privateDnsStatus.mode == PrivateDnsMode::OPPORTUNISTIC) {
1229             *fallback = true;
1230             return -1;
1231         } else {
1232             // Sleep and iterate some small number of times checking for the
1233             // arrival of resolved and validated server IP addresses, instead
1234             // of returning an immediate error.
1235             // This is needed because as soon as a network becomes the default network, apps will
1236             // send DNS queries on that network. If no servers have yet validated, and we do not
1237             // block those queries, they would immediately fail, causing application-visible errors.
1238             // Note that this can happen even before the network validates, since an unvalidated
1239             // network can become the default network if no validated networks are available.
1240             //
1241             // TODO: see if there is a better way to address this problem, such as buffering the
1242             // queries in a queue or only blocking queries for the first few seconds after a default
1243             // network change.
1244             for (int i = 0; i < 42; i++) {
1245                 std::this_thread::sleep_for(std::chrono::milliseconds(100));
1246                 if (!gPrivateDnsConfiguration.getStatus(netId).validatedServers.empty()) {
1247                     privateDnsStatus = gPrivateDnsConfiguration.getStatus(netId);
1248                     break;
1249                 }
1250             }
1251             if (privateDnsStatus.validatedServers.empty()) {
1252                 return -1;
1253             }
1254         }
1255     }
1256 
1257     LOG(INFO) << __func__ << ": performing query over TLS";
1258 
1259     const auto response = sDnsTlsDispatcher.query(privateDnsStatus.validatedServers, mark, query,
1260                                                   answer, &resplen);
1261 
1262     LOG(INFO) << __func__ << ": TLS query result: " << static_cast<int>(response);
1263 
1264     if (privateDnsStatus.mode == PrivateDnsMode::OPPORTUNISTIC) {
1265         // In opportunistic mode, handle falling back to cleartext in some
1266         // cases (DNS shouldn't fail if a validated opportunistic mode server
1267         // becomes unreachable for some reason).
1268         switch (response) {
1269             case DnsTlsTransport::Response::success:
1270                 *rcode = reinterpret_cast<HEADER*>(answer.base())->rcode;
1271                 return resplen;
1272             case DnsTlsTransport::Response::network_error:
1273                 // No need to set the error timeout here since it will fallback to UDP.
1274             case DnsTlsTransport::Response::internal_error:
1275                 // Note: this will cause cleartext queries to be emitted, with
1276                 // all of the EDNS0 goodness enabled. Fingers crossed.  :-/
1277                 *fallback = true;
1278                 [[fallthrough]];
1279             default:
1280                 return -1;
1281         }
1282     } else {
1283         // Strict mode
1284         switch (response) {
1285             case DnsTlsTransport::Response::success:
1286                 *rcode = reinterpret_cast<HEADER*>(answer.base())->rcode;
1287                 return resplen;
1288             case DnsTlsTransport::Response::network_error:
1289                 // This case happens when the query stored in DnsTlsTransport is expired since
1290                 // either 1) the query has been tried for 3 times but no response or 2) fail to
1291                 // establish the connection with the server.
1292                 *rcode = RCODE_TIMEOUT;
1293                 [[fallthrough]];
1294             default:
1295                 return -1;
1296         }
1297     }
1298 }
1299 
resolv_res_nsend(const android_net_context * netContext,const uint8_t * msg,int msgLen,uint8_t * ans,int ansLen,int * rcode,uint32_t flags)1300 int resolv_res_nsend(const android_net_context* netContext, const uint8_t* msg, int msgLen,
1301                      uint8_t* ans, int ansLen, int* rcode, uint32_t flags) {
1302     res_state res = res_get_state();
1303     res_setnetcontext(res, netContext);
1304     _resolv_populate_res_for_net(res);
1305     *rcode = NOERROR;
1306     return res_nsend(res, msg, msgLen, ans, ansLen, rcode, flags);
1307 }
1308