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