Bernie Innocenti | 5586419 | 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 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 73 | #include <arpa/inet.h> |
| 74 | #include <arpa/nameser.h> |
| 75 | #include <ctype.h> |
| 76 | #include <errno.h> |
| 77 | #include <netdb.h> |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 78 | #include <netinet/in.h> |
| 79 | #include <sys/param.h> |
| 80 | #include <sys/types.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 81 | #include "resolv_cache.h" |
| 82 | #include "resolv_private.h" |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 83 | #include <stdio.h> |
| 84 | #include <stdlib.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 85 | #include <string.h> |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 86 | #include <unistd.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 87 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 88 | /* Options. Leave them on. */ |
| 89 | #ifndef DEBUG |
| 90 | #define DEBUG |
| 91 | #endif |
| 92 | |
| 93 | #if PACKETSZ > 1024 |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 94 | #define MAXPACKET PACKETSZ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 95 | #else |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 96 | #define MAXPACKET 1024 |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 97 | #endif |
| 98 | |
| 99 | /* |
| 100 | * Formulate a normal query, send, and await answer. |
| 101 | * Returned answer is placed in supplied buffer "answer". |
| 102 | * Perform preliminary check of answer, returning success only |
| 103 | * if no error is indicated and the answer count is nonzero. |
| 104 | * Return the size of the response on success, -1 on error. |
| 105 | * Error number is left in H_ERRNO. |
| 106 | * |
| 107 | * Caller must parse answer and determine whether it answers the question. |
| 108 | */ |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 109 | int res_nquery(res_state statp, const char* name, // domain name |
| 110 | int cl, int type, // class and type of query |
| 111 | u_char* answer, // buffer to put answer |
| 112 | int anslen) // size of answer buffer |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 113 | { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 114 | u_char buf[MAXPACKET]; |
| 115 | HEADER* hp = (HEADER*) (void*) answer; |
| 116 | int n; |
| 117 | u_int oflags; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 118 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 119 | oflags = statp->_flags; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 120 | |
| 121 | again: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 122 | hp->rcode = NOERROR; /* default */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 123 | |
| 124 | #ifdef DEBUG |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 125 | if (statp->options & RES_DEBUG) printf(";; res_query(%s, %d, %d)\n", name, cl, type); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 126 | #endif |
| 127 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 128 | n = res_nmkquery(statp, QUERY, name, cl, type, NULL, 0, NULL, buf, sizeof(buf)); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 129 | if (n > 0 && (statp->_flags & RES_F_EDNS0ERR) == 0 && |
| 130 | (statp->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0U) |
| 131 | n = res_nopt(statp, n, buf, sizeof(buf), anslen); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 132 | if (n <= 0) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 133 | #ifdef DEBUG |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 134 | if (statp->options & RES_DEBUG) printf(";; res_query: mkquery failed\n"); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 135 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 136 | RES_SET_H_ERRNO(statp, NO_RECOVERY); |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 137 | return n; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 138 | } |
| 139 | n = res_nsend(statp, buf, n, answer, anslen); |
| 140 | if (n < 0) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 141 | /* if the query choked with EDNS0, retry without EDNS0 */ |
| 142 | if ((statp->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0U && |
| 143 | ((oflags ^ statp->_flags) & RES_F_EDNS0ERR) != 0) { |
| 144 | statp->_flags |= RES_F_EDNS0ERR; |
| 145 | if (statp->options & RES_DEBUG) printf(";; res_nquery: retry without EDNS0\n"); |
| 146 | goto again; |
| 147 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 148 | #ifdef DEBUG |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 149 | if (statp->options & RES_DEBUG) printf(";; res_query: send error\n"); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 150 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 151 | RES_SET_H_ERRNO(statp, TRY_AGAIN); |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 152 | return n; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 153 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 154 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 155 | if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 156 | #ifdef DEBUG |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 157 | if (statp->options & RES_DEBUG) |
| 158 | printf(";; rcode = (%s), counts = an:%d ns:%d ar:%d\n", p_rcode(hp->rcode), |
| 159 | ntohs(hp->ancount), ntohs(hp->nscount), ntohs(hp->arcount)); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 160 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 161 | switch (hp->rcode) { |
| 162 | case NXDOMAIN: |
| 163 | RES_SET_H_ERRNO(statp, HOST_NOT_FOUND); |
| 164 | break; |
| 165 | case SERVFAIL: |
| 166 | RES_SET_H_ERRNO(statp, TRY_AGAIN); |
| 167 | break; |
| 168 | case NOERROR: |
| 169 | RES_SET_H_ERRNO(statp, NO_DATA); |
| 170 | break; |
| 171 | case FORMERR: |
| 172 | case NOTIMP: |
| 173 | case REFUSED: |
| 174 | default: |
| 175 | RES_SET_H_ERRNO(statp, NO_RECOVERY); |
| 176 | break; |
| 177 | } |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 178 | return -1; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 179 | } |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 180 | return n; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /* |
| 184 | * Formulate a normal query, send, and retrieve answer in supplied buffer. |
| 185 | * Return the size of the response on success, -1 on error. |
| 186 | * If enabled, implement search rules until answer or unrecoverable failure |
| 187 | * is detected. Error code, if any, is left in H_ERRNO. |
| 188 | */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 189 | int res_nsearch(res_state statp, const char* name, /* domain name */ |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 190 | int cl, int type, /* class and type of query */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 191 | u_char* answer, /* buffer to put answer */ |
| 192 | int anslen) /* size of answer */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 193 | { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 194 | const char *cp, *const *domain; |
| 195 | HEADER* hp = (HEADER*) (void*) answer; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 196 | u_int dots; |
| 197 | int trailing_dot, ret, saved_herrno; |
| 198 | int got_nodata = 0, got_servfail = 0, root_on_list = 0; |
| 199 | int tried_as_is = 0; |
| 200 | int searched = 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 201 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 202 | errno = 0; |
| 203 | RES_SET_H_ERRNO(statp, HOST_NOT_FOUND); /* True if we never query. */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 204 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 205 | dots = 0; |
| 206 | for (cp = name; *cp != '\0'; cp++) dots += (*cp == '.'); |
| 207 | trailing_dot = 0; |
| 208 | if (cp > name && *--cp == '.') trailing_dot++; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 209 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 210 | /* |
| 211 | * If there are enough dots in the name, let's just give it a |
| 212 | * try 'as is'. The threshold can be set with the "ndots" option. |
| 213 | * Also, query 'as is', if there is a trailing dot in the name. |
| 214 | */ |
| 215 | saved_herrno = -1; |
| 216 | if (dots >= statp->ndots || trailing_dot) { |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 217 | ret = res_nquerydomain(statp, name, NULL, cl, type, answer, anslen); |
| 218 | if (ret > 0 || trailing_dot) return ret; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 219 | saved_herrno = statp->res_h_errno; |
| 220 | tried_as_is++; |
| 221 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 222 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 223 | /* |
| 224 | * We do at least one level of search if |
| 225 | * - there is no dot and RES_DEFNAME is set, or |
| 226 | * - there is at least one dot, there is no trailing dot, |
| 227 | * and RES_DNSRCH is set. |
| 228 | */ |
| 229 | if ((!dots && (statp->options & RES_DEFNAMES) != 0U) || |
| 230 | (dots && !trailing_dot && (statp->options & RES_DNSRCH) != 0U)) { |
| 231 | int done = 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 232 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 233 | /* Unfortunately we need to load network-specific info |
| 234 | * (dns servers, search domains) before |
| 235 | * the domain stuff is tried. Will have a better |
| 236 | * fix after thread pools are used as this will |
| 237 | * be loaded once for the thread instead of each |
| 238 | * time a query is tried. |
| 239 | */ |
| 240 | _resolv_populate_res_for_net(statp); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 241 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 242 | for (domain = (const char* const*) statp->dnsrch; *domain && !done; domain++) { |
| 243 | searched = 1; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 244 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 245 | if (domain[0][0] == '\0' || (domain[0][0] == '.' && domain[0][1] == '\0')) |
| 246 | root_on_list++; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 247 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 248 | ret = res_nquerydomain(statp, name, *domain, cl, type, answer, anslen); |
| 249 | if (ret > 0) return ret; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 250 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 251 | /* |
| 252 | * If no server present, give up. |
| 253 | * If name isn't found in this domain, |
| 254 | * keep trying higher domains in the search list |
| 255 | * (if that's enabled). |
| 256 | * On a NO_DATA error, keep trying, otherwise |
| 257 | * a wildcard entry of another type could keep us |
| 258 | * from finding this entry higher in the domain. |
| 259 | * If we get some other error (negative answer or |
| 260 | * server failure), then stop searching up, |
| 261 | * but try the input name below in case it's |
| 262 | * fully-qualified. |
| 263 | */ |
| 264 | if (errno == ECONNREFUSED) { |
| 265 | RES_SET_H_ERRNO(statp, TRY_AGAIN); |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 266 | return -1; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 267 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 268 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 269 | switch (statp->res_h_errno) { |
| 270 | case NO_DATA: |
| 271 | got_nodata++; |
Bernie Innocenti | 11cd020 | 2018-10-12 21:27:45 +0900 | [diff] [blame] | 272 | break; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 273 | case HOST_NOT_FOUND: |
| 274 | /* keep trying */ |
| 275 | break; |
| 276 | case TRY_AGAIN: |
| 277 | if (hp->rcode == SERVFAIL) { |
| 278 | /* try next search element, if any */ |
| 279 | got_servfail++; |
| 280 | break; |
| 281 | } |
Bernie Innocenti | 8bb94ba | 2018-10-10 22:30:12 +0900 | [diff] [blame] | 282 | [[fallthrough]]; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 283 | default: |
| 284 | /* anything else implies that we're done */ |
| 285 | done++; |
| 286 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 287 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 288 | /* if we got here for some reason other than DNSRCH, |
| 289 | * we only wanted one iteration of the loop, so stop. |
| 290 | */ |
| 291 | if ((statp->options & RES_DNSRCH) == 0U) done++; |
| 292 | } |
| 293 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 294 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 295 | /* |
| 296 | * If the query has not already been tried as is then try it |
| 297 | * unless RES_NOTLDQUERY is set and there were no dots. |
| 298 | */ |
| 299 | if ((dots || !searched || (statp->options & RES_NOTLDQUERY) == 0U) && |
| 300 | !(tried_as_is || root_on_list)) { |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 301 | ret = res_nquerydomain(statp, name, NULL, cl, type, answer, anslen); |
| 302 | if (ret > 0) return ret; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 303 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 304 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 305 | /* if we got here, we didn't satisfy the search. |
| 306 | * if we did an initial full query, return that query's H_ERRNO |
| 307 | * (note that we wouldn't be here if that query had succeeded). |
| 308 | * else if we ever got a nodata, send that back as the reason. |
| 309 | * else send back meaningless H_ERRNO, that being the one from |
| 310 | * the last DNSRCH we did. |
| 311 | */ |
| 312 | if (saved_herrno != -1) |
| 313 | RES_SET_H_ERRNO(statp, saved_herrno); |
| 314 | else if (got_nodata) |
| 315 | RES_SET_H_ERRNO(statp, NO_DATA); |
| 316 | else if (got_servfail) |
| 317 | RES_SET_H_ERRNO(statp, TRY_AGAIN); |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 318 | return -1; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | /* |
| 322 | * Perform a call on res_query on the concatenation of name and domain, |
| 323 | * removing a trailing dot from name if domain is NULL. |
| 324 | */ |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 325 | int res_nquerydomain(res_state statp, const char* name, const char* domain, |
| 326 | int cl, int type, /* class and type of query */ |
| 327 | u_char* answer, /* buffer to put answer */ |
| 328 | int anslen) /* size of answer */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 329 | { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 330 | char nbuf[MAXDNAME]; |
| 331 | const char* longname = nbuf; |
| 332 | int n, d; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 333 | |
| 334 | #ifdef DEBUG |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 335 | if (statp->options & RES_DEBUG) |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 336 | printf(";; res_nquerydomain(%s, %s, %d, %d)\n", name, domain ? domain : "<Nil>", cl, |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 337 | type); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 338 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 339 | if (domain == NULL) { |
| 340 | /* |
| 341 | * Check for trailing '.'; |
| 342 | * copy without '.' if present. |
| 343 | */ |
| 344 | n = strlen(name); |
| 345 | if (n >= MAXDNAME) { |
| 346 | RES_SET_H_ERRNO(statp, NO_RECOVERY); |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 347 | return -1; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 348 | } |
| 349 | n--; |
| 350 | if (n >= 0 && name[n] == '.') { |
| 351 | strncpy(nbuf, name, (size_t) n); |
| 352 | nbuf[n] = '\0'; |
| 353 | } else |
| 354 | longname = name; |
| 355 | } else { |
| 356 | n = strlen(name); |
| 357 | d = strlen(domain); |
| 358 | if (n + d + 1 >= MAXDNAME) { |
| 359 | RES_SET_H_ERRNO(statp, NO_RECOVERY); |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 360 | return -1; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 361 | } |
| 362 | snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain); |
| 363 | } |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 364 | return res_nquery(statp, longname, cl, type, answer, anslen); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 365 | } |