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