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