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