Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1 | /* $NetBSD: res_query.c,v 1.7 2006/01/24 17:41:25 christos Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 1988, 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 | |
Ken Chen | 5471dca | 2019-04-15 15:25:35 +0800 | [diff] [blame] | 73 | #define LOG_TAG "resolv" |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 74 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 75 | #include <arpa/inet.h> |
| 76 | #include <arpa/nameser.h> |
| 77 | #include <ctype.h> |
| 78 | #include <errno.h> |
| 79 | #include <netdb.h> |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 80 | #include <netinet/in.h> |
| 81 | #include <sys/param.h> |
| 82 | #include <sys/types.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 83 | #include <stdlib.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 84 | #include <string.h> |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 85 | #include <unistd.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 86 | |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 87 | #include <android-base/logging.h> |
| 88 | |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 89 | #include "resolv_cache.h" |
| 90 | #include "resolv_private.h" |
| 91 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 92 | #if PACKETSZ > 1024 |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 93 | #define MAXPACKET PACKETSZ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 94 | #else |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 95 | #define MAXPACKET 1024 |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 96 | #endif |
| 97 | |
| 98 | /* |
| 99 | * Formulate a normal query, send, and await answer. |
| 100 | * Returned answer is placed in supplied buffer "answer". |
| 101 | * Perform preliminary check of answer, returning success only |
| 102 | * if no error is indicated and the answer count is nonzero. |
| 103 | * Return the size of the response on success, -1 on error. |
Hungming Chen | 6c84a3d | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 104 | * Error number is left in *herrno. |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 105 | * |
| 106 | * Caller must parse answer and determine whether it answers the question. |
| 107 | */ |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 108 | int res_nquery(res_state statp, const char* name, // domain name |
| 109 | int cl, int type, // class and type of query |
| 110 | u_char* answer, // buffer to put answer |
| 111 | int anslen, // size of answer buffer |
Hungming Chen | 947aab0 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 112 | int* herrno) // legacy and extended h_errno |
| 113 | // NETD_RESOLV_H_ERRNO_EXT_* |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 114 | { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 115 | u_char buf[MAXPACKET]; |
| 116 | HEADER* hp = (HEADER*) (void*) answer; |
| 117 | int n; |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 118 | int rcode = NOERROR; |
Ken Chen | 0a01553 | 2019-01-02 14:59:38 +0800 | [diff] [blame] | 119 | bool retried = false; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 120 | |
| 121 | again: |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 122 | hp->rcode = NOERROR; /* default */ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 123 | |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 124 | LOG(DEBUG) << __func__ << ": (" << cl << ", " << type << ")"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 125 | |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 126 | n = res_nmkquery(statp, QUERY, name, cl, type, NULL, 0, NULL, buf, sizeof(buf)); |
Ken Chen | 0a01553 | 2019-01-02 14:59:38 +0800 | [diff] [blame] | 127 | if (n > 0 && (statp->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0U && !retried) |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 128 | n = res_nopt(statp, n, buf, sizeof(buf), anslen); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 129 | if (n <= 0) { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 130 | LOG(DEBUG) << __func__ << ": mkquery failed"; |
Hungming Chen | 6c84a3d | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 131 | *herrno = NO_RECOVERY; |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 132 | return n; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 133 | } |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 134 | n = res_nsend(statp, buf, n, answer, anslen, &rcode, 0); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 135 | if (n < 0) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 136 | /* if the query choked with EDNS0, retry without EDNS0 */ |
| 137 | if ((statp->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0U && |
Ken Chen | 0a01553 | 2019-01-02 14:59:38 +0800 | [diff] [blame] | 138 | (statp->_flags & RES_F_EDNS0ERR) && !retried) { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 139 | LOG(DEBUG) << __func__ << ": retry without EDNS0"; |
Ken Chen | 0a01553 | 2019-01-02 14:59:38 +0800 | [diff] [blame] | 140 | retried = true; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 141 | goto again; |
| 142 | } |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 143 | LOG(DEBUG) << __func__ << ": send error"; |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 144 | |
Hungming Chen | 947aab0 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 145 | // Note that rcodes SERVFAIL, NOTIMP, REFUSED may cause res_nquery() to return a general |
| 146 | // error code EAI_AGAIN, but mapping the error code from rcode as res_queryN() does for |
| 147 | // getaddrinfo(). Different rcodes trigger different behaviors: |
| 148 | // |
| 149 | // - SERVFAIL, NOTIMP, REFUSED |
| 150 | // These result in send_dg() returning 0, causing res_nsend() to try the next |
| 151 | // nameserver. After all nameservers failed, res_nsend() returns -ETIMEDOUT, causing |
| 152 | // res_nquery() to return EAI_AGAIN here regardless of the rcode from the DNS response. |
| 153 | // |
| 154 | // - NXDOMAIN, FORMERR |
| 155 | // These rcodes may cause res_nsend() to return successfully (i.e. the result is a |
| 156 | // positive integer). In this case, res_nquery() returns error number by referring |
| 157 | // the rcode from the DNS response. |
| 158 | switch (rcode) { |
| 159 | case RCODE_TIMEOUT: // Not defined in RFC. |
| 160 | // DNS metrics monitors DNS query timeout. |
| 161 | *herrno = NETD_RESOLV_H_ERRNO_EXT_TIMEOUT; // extended h_errno. |
| 162 | break; |
| 163 | default: |
| 164 | *herrno = TRY_AGAIN; |
| 165 | break; |
| 166 | } |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 167 | return n; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 168 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 169 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 170 | if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 171 | LOG(DEBUG) << __func__ << ": rcode = (" << p_rcode(hp->rcode) |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 172 | << "), counts = an:" << ntohs(hp->ancount) << " ns:" << ntohs(hp->nscount) |
| 173 | << " ar:" << ntohs(hp->arcount); |
| 174 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 175 | switch (hp->rcode) { |
| 176 | case NXDOMAIN: |
Hungming Chen | 6c84a3d | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 177 | *herrno = HOST_NOT_FOUND; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 178 | break; |
| 179 | case SERVFAIL: |
Hungming Chen | 6c84a3d | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 180 | *herrno = TRY_AGAIN; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 181 | break; |
| 182 | case NOERROR: |
Hungming Chen | 6c84a3d | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 183 | *herrno = NO_DATA; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 184 | break; |
| 185 | case FORMERR: |
| 186 | case NOTIMP: |
| 187 | case REFUSED: |
| 188 | default: |
Hungming Chen | 6c84a3d | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 189 | *herrno = NO_RECOVERY; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 190 | break; |
| 191 | } |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 192 | return -1; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 193 | } |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 194 | return n; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /* |
| 198 | * Formulate a normal query, send, and retrieve answer in supplied buffer. |
| 199 | * Return the size of the response on success, -1 on error. |
| 200 | * If enabled, implement search rules until answer or unrecoverable failure |
Hungming Chen | 6c84a3d | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 201 | * is detected. Error code, if any, is left in *herrno. |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 202 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 203 | int res_nsearch(res_state statp, const char* name, /* domain name */ |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 204 | int cl, int type, /* class and type of query */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 205 | u_char* answer, /* buffer to put answer */ |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 206 | int anslen, /* size of answer */ |
Hungming Chen | 947aab0 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 207 | int* herrno) /* legacy and extended |
| 208 | h_errno NETD_RESOLV_H_ERRNO_EXT_* */ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 209 | { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 210 | const char *cp, *const *domain; |
| 211 | HEADER* hp = (HEADER*) (void*) answer; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 212 | u_int dots; |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame^] | 213 | int ret, saved_herrno; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 214 | int got_nodata = 0, got_servfail = 0, root_on_list = 0; |
| 215 | int tried_as_is = 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 216 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 217 | errno = 0; |
Hungming Chen | 6c84a3d | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 218 | *herrno = HOST_NOT_FOUND; /* True if we never query. */ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 219 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 220 | dots = 0; |
| 221 | for (cp = name; *cp != '\0'; cp++) dots += (*cp == '.'); |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame^] | 222 | const bool trailing_dot = (cp > name && *--cp == '.') ? true : false; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 223 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 224 | /* |
| 225 | * If there are enough dots in the name, let's just give it a |
| 226 | * try 'as is'. The threshold can be set with the "ndots" option. |
| 227 | * Also, query 'as is', if there is a trailing dot in the name. |
| 228 | */ |
| 229 | saved_herrno = -1; |
| 230 | if (dots >= statp->ndots || trailing_dot) { |
Hungming Chen | 947aab0 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 231 | ret = res_nquerydomain(statp, name, NULL, cl, type, answer, anslen, herrno); |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 232 | if (ret > 0 || trailing_dot) return ret; |
Hungming Chen | 6c84a3d | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 233 | saved_herrno = *herrno; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 234 | tried_as_is++; |
| 235 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 236 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 237 | /* |
| 238 | * We do at least one level of search if |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame^] | 239 | * - there is no dot, or |
| 240 | * - there is at least one dot and there is no trailing dot. |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 241 | */ |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame^] | 242 | if ((!dots) || (dots && !trailing_dot)) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 243 | int done = 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 244 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 245 | /* Unfortunately we need to load network-specific info |
| 246 | * (dns servers, search domains) before |
| 247 | * the domain stuff is tried. Will have a better |
| 248 | * fix after thread pools are used as this will |
| 249 | * be loaded once for the thread instead of each |
| 250 | * time a query is tried. |
| 251 | */ |
| 252 | _resolv_populate_res_for_net(statp); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 253 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 254 | for (domain = (const char* const*) statp->dnsrch; *domain && !done; domain++) { |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 255 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 256 | if (domain[0][0] == '\0' || (domain[0][0] == '.' && domain[0][1] == '\0')) |
| 257 | root_on_list++; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 258 | |
Hungming Chen | 947aab0 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 259 | ret = res_nquerydomain(statp, name, *domain, cl, type, answer, anslen, herrno); |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 260 | if (ret > 0) return ret; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 261 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 262 | /* |
| 263 | * If no server present, give up. |
| 264 | * If name isn't found in this domain, |
| 265 | * keep trying higher domains in the search list |
| 266 | * (if that's enabled). |
| 267 | * On a NO_DATA error, keep trying, otherwise |
| 268 | * a wildcard entry of another type could keep us |
| 269 | * from finding this entry higher in the domain. |
| 270 | * If we get some other error (negative answer or |
| 271 | * server failure), then stop searching up, |
| 272 | * but try the input name below in case it's |
| 273 | * fully-qualified. |
| 274 | */ |
| 275 | if (errno == ECONNREFUSED) { |
Hungming Chen | 6c84a3d | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 276 | *herrno = TRY_AGAIN; |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 277 | return -1; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 278 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 279 | |
Hungming Chen | 6c84a3d | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 280 | switch (*herrno) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 281 | case NO_DATA: |
| 282 | got_nodata++; |
Bernie Innocenti | 3a4002e | 2018-10-12 21:27:45 +0900 | [diff] [blame] | 283 | break; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 284 | case HOST_NOT_FOUND: |
| 285 | /* keep trying */ |
| 286 | break; |
| 287 | case TRY_AGAIN: |
| 288 | if (hp->rcode == SERVFAIL) { |
| 289 | /* try next search element, if any */ |
| 290 | got_servfail++; |
| 291 | break; |
| 292 | } |
Bernie Innocenti | f40b3bd | 2018-10-10 22:30:12 +0900 | [diff] [blame] | 293 | [[fallthrough]]; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 294 | default: |
| 295 | /* anything else implies that we're done */ |
| 296 | done++; |
| 297 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 298 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 299 | } |
| 300 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 301 | |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame^] | 302 | // if we have not already tried the name "as is", do that now. |
| 303 | // note that we do this regardless of how many dots were in the |
| 304 | // name or whether it ends with a dot. |
| 305 | if (!tried_as_is && !root_on_list) { |
Hungming Chen | 947aab0 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 306 | ret = res_nquerydomain(statp, name, NULL, cl, type, answer, anslen, herrno); |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 307 | if (ret > 0) return ret; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 308 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 309 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 310 | /* if we got here, we didn't satisfy the search. |
| 311 | * if we did an initial full query, return that query's H_ERRNO |
| 312 | * (note that we wouldn't be here if that query had succeeded). |
| 313 | * else if we ever got a nodata, send that back as the reason. |
| 314 | * else send back meaningless H_ERRNO, that being the one from |
| 315 | * the last DNSRCH we did. |
| 316 | */ |
| 317 | if (saved_herrno != -1) |
Hungming Chen | 6c84a3d | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 318 | *herrno = saved_herrno; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 319 | else if (got_nodata) |
Hungming Chen | 6c84a3d | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 320 | *herrno = NO_DATA; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 321 | else if (got_servfail) |
Hungming Chen | 6c84a3d | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 322 | *herrno = TRY_AGAIN; |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 323 | return -1; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | /* |
| 327 | * Perform a call on res_query on the concatenation of name and domain, |
| 328 | * removing a trailing dot from name if domain is NULL. |
| 329 | */ |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 330 | int res_nquerydomain(res_state statp, const char* name, const char* domain, int cl, |
| 331 | int type, /* class and type of query */ |
| 332 | u_char* answer, /* buffer to put answer */ |
| 333 | int anslen, /* size of answer */ |
Hungming Chen | 947aab0 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 334 | int* herrno) /* legacy and extended h_errno NETD_RESOLV_H_ERRNO_EXT_* */ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 335 | { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 336 | char nbuf[MAXDNAME]; |
| 337 | const char* longname = nbuf; |
| 338 | int n, d; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 339 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 340 | if (domain == NULL) { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 341 | LOG(DEBUG) << __func__ << ": (null, " << cl << ", " << type << ")"; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 342 | /* |
| 343 | * Check for trailing '.'; |
| 344 | * copy without '.' if present. |
| 345 | */ |
| 346 | n = strlen(name); |
| 347 | if (n >= MAXDNAME) { |
Hungming Chen | 6c84a3d | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 348 | *herrno = NO_RECOVERY; |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 349 | return -1; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 350 | } |
| 351 | n--; |
| 352 | if (n >= 0 && name[n] == '.') { |
| 353 | strncpy(nbuf, name, (size_t) n); |
| 354 | nbuf[n] = '\0'; |
| 355 | } else |
| 356 | longname = name; |
| 357 | } else { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 358 | LOG(DEBUG) << __func__ << ": (" << cl << ", " << type << ")"; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 359 | n = strlen(name); |
| 360 | d = strlen(domain); |
| 361 | if (n + d + 1 >= MAXDNAME) { |
Hungming Chen | 6c84a3d | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 362 | *herrno = NO_RECOVERY; |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 363 | return -1; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 364 | } |
| 365 | snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain); |
| 366 | } |
Hungming Chen | 947aab0 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 367 | return res_nquery(statp, longname, cl, type, answer, anslen, herrno); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 368 | } |