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