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