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