blob: f54ff3f32cb3ae25dac861e4825b422bff771ca5 [file] [log] [blame]
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001/* $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 Chen5471dca2019-04-15 15:25:35 +080073#define LOG_TAG "resolv"
Bernie Innocenti3952ccc2019-03-03 19:39:53 +090074
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090075#include <arpa/inet.h>
76#include <arpa/nameser.h>
77#include <ctype.h>
78#include <errno.h>
79#include <netdb.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090080#include <netinet/in.h>
81#include <sys/param.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090082#include <stdlib.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090083#include <string.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090084#include <unistd.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090085
chenbruceacb832c2019-02-20 19:45:50 +080086#include <android-base/logging.h>
87
Mike Yu34433c62019-08-20 20:17:36 +080088#include "res_debug.h"
Bernie Innocentiac18b122018-10-01 23:10:18 +090089#include "resolv_cache.h"
90#include "resolv_private.h"
91
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090092#if PACKETSZ > 1024
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090093#define MAXPACKET PACKETSZ
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090094#else
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090095#define MAXPACKET 1024
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090096#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 Chen6c84a3d2018-12-26 16:14:17 +0800104 * Error number is left in *herrno.
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900105 *
106 * Caller must parse answer and determine whether it answers the question.
107 */
Mike Yubfb1b342018-11-06 15:42:36 +0800108int res_nquery(res_state statp, const char* name, // domain name
109 int cl, int type, // class and type of query
chenbrucec51f1212019-09-12 16:59:33 +0800110 uint8_t* answer, // buffer to put answer
Mike Yubfb1b342018-11-06 15:42:36 +0800111 int anslen, // size of answer buffer
Hungming Chen947aab02018-12-27 18:33:19 +0800112 int* herrno) // legacy and extended h_errno
113 // NETD_RESOLV_H_ERRNO_EXT_*
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900114{
chenbrucec51f1212019-09-12 16:59:33 +0800115 uint8_t buf[MAXPACKET];
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900116 HEADER* hp = (HEADER*) (void*) answer;
117 int n;
Mike Yubfb1b342018-11-06 15:42:36 +0800118 int rcode = NOERROR;
Ken Chen0a015532019-01-02 14:59:38 +0800119 bool retried = false;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900120
121again:
chenbruced8cbb9b2019-06-20 18:25:28 +0800122 hp->rcode = NOERROR; // default
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900123
Ken Chenffc224a2019-03-19 17:41:28 +0800124 LOG(DEBUG) << __func__ << ": (" << cl << ", " << type << ")";
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900125
Bernie Innocenti9c575932018-09-07 21:10:25 +0900126 n = res_nmkquery(statp, QUERY, name, cl, type, NULL, 0, NULL, buf, sizeof(buf));
chenbruced8cbb9b2019-06-20 18:25:28 +0800127 if (n > 0 &&
128 (statp->netcontext_flags &
129 (NET_CONTEXT_FLAG_USE_DNS_OVER_TLS | NET_CONTEXT_FLAG_USE_EDNS)) &&
130 !retried)
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900131 n = res_nopt(statp, n, buf, sizeof(buf), anslen);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900132 if (n <= 0) {
Ken Chenffc224a2019-03-19 17:41:28 +0800133 LOG(DEBUG) << __func__ << ": mkquery failed";
Hungming Chen6c84a3d2018-12-26 16:14:17 +0800134 *herrno = NO_RECOVERY;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900135 return n;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900136 }
Luke Huangba7bef92018-12-26 16:53:03 +0800137 n = res_nsend(statp, buf, n, answer, anslen, &rcode, 0);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900138 if (n < 0) {
chenbruced8cbb9b2019-06-20 18:25:28 +0800139 // If the query choked with EDNS0, retry without EDNS0 that when the server
140 // has no response, resovler won't retry and do nothing. Even fallback to UDP,
141 // we also has the same symptom if EDNS is enabled.
142 if ((statp->netcontext_flags &
143 (NET_CONTEXT_FLAG_USE_DNS_OVER_TLS | NET_CONTEXT_FLAG_USE_EDNS)) &&
Ken Chen0a015532019-01-02 14:59:38 +0800144 (statp->_flags & RES_F_EDNS0ERR) && !retried) {
Ken Chenffc224a2019-03-19 17:41:28 +0800145 LOG(DEBUG) << __func__ << ": retry without EDNS0";
Ken Chen0a015532019-01-02 14:59:38 +0800146 retried = true;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900147 goto again;
148 }
Ken Chenffc224a2019-03-19 17:41:28 +0800149 LOG(DEBUG) << __func__ << ": send error";
chenbruceacb832c2019-02-20 19:45:50 +0800150
Hungming Chen947aab02018-12-27 18:33:19 +0800151 // Note that rcodes SERVFAIL, NOTIMP, REFUSED may cause res_nquery() to return a general
152 // error code EAI_AGAIN, but mapping the error code from rcode as res_queryN() does for
153 // getaddrinfo(). Different rcodes trigger different behaviors:
154 //
155 // - SERVFAIL, NOTIMP, REFUSED
156 // These result in send_dg() returning 0, causing res_nsend() to try the next
157 // nameserver. After all nameservers failed, res_nsend() returns -ETIMEDOUT, causing
158 // res_nquery() to return EAI_AGAIN here regardless of the rcode from the DNS response.
159 //
160 // - NXDOMAIN, FORMERR
161 // These rcodes may cause res_nsend() to return successfully (i.e. the result is a
162 // positive integer). In this case, res_nquery() returns error number by referring
163 // the rcode from the DNS response.
164 switch (rcode) {
165 case RCODE_TIMEOUT: // Not defined in RFC.
166 // DNS metrics monitors DNS query timeout.
167 *herrno = NETD_RESOLV_H_ERRNO_EXT_TIMEOUT; // extended h_errno.
168 break;
169 default:
170 *herrno = TRY_AGAIN;
171 break;
172 }
Bernie Innocenti9c575932018-09-07 21:10:25 +0900173 return n;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900174 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900175
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900176 if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
Ken Chenffc224a2019-03-19 17:41:28 +0800177 LOG(DEBUG) << __func__ << ": rcode = (" << p_rcode(hp->rcode)
chenbruceacb832c2019-02-20 19:45:50 +0800178 << "), counts = an:" << ntohs(hp->ancount) << " ns:" << ntohs(hp->nscount)
179 << " ar:" << ntohs(hp->arcount);
180
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900181 switch (hp->rcode) {
182 case NXDOMAIN:
Hungming Chen6c84a3d2018-12-26 16:14:17 +0800183 *herrno = HOST_NOT_FOUND;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900184 break;
185 case SERVFAIL:
Hungming Chen6c84a3d2018-12-26 16:14:17 +0800186 *herrno = TRY_AGAIN;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900187 break;
188 case NOERROR:
Hungming Chen6c84a3d2018-12-26 16:14:17 +0800189 *herrno = NO_DATA;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900190 break;
191 case FORMERR:
192 case NOTIMP:
193 case REFUSED:
194 default:
Hungming Chen6c84a3d2018-12-26 16:14:17 +0800195 *herrno = NO_RECOVERY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900196 break;
197 }
Bernie Innocenti9c575932018-09-07 21:10:25 +0900198 return -1;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900199 }
Bernie Innocenti9c575932018-09-07 21:10:25 +0900200 return n;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900201}
202
203/*
204 * Formulate a normal query, send, and retrieve answer in supplied buffer.
205 * Return the size of the response on success, -1 on error.
206 * If enabled, implement search rules until answer or unrecoverable failure
Hungming Chen6c84a3d2018-12-26 16:14:17 +0800207 * is detected. Error code, if any, is left in *herrno.
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900208 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900209int res_nsearch(res_state statp, const char* name, /* domain name */
Bernie Innocenti9c575932018-09-07 21:10:25 +0900210 int cl, int type, /* class and type of query */
chenbrucec51f1212019-09-12 16:59:33 +0800211 uint8_t* answer, /* buffer to put answer */
Mike Yubfb1b342018-11-06 15:42:36 +0800212 int anslen, /* size of answer */
Hungming Chen947aab02018-12-27 18:33:19 +0800213 int* herrno) /* legacy and extended
214 h_errno NETD_RESOLV_H_ERRNO_EXT_* */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900215{
Luke Huang2dac4382019-06-24 13:28:44 +0800216 const char* cp;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900217 HEADER* hp = (HEADER*) (void*) answer;
chenbrucec51f1212019-09-12 16:59:33 +0800218 uint32_t dots;
chenbruce018fdb22019-06-12 18:08:04 +0800219 int ret, saved_herrno;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900220 int got_nodata = 0, got_servfail = 0, root_on_list = 0;
221 int tried_as_is = 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900222
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900223 errno = 0;
Hungming Chen6c84a3d2018-12-26 16:14:17 +0800224 *herrno = HOST_NOT_FOUND; /* True if we never query. */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900225
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900226 dots = 0;
227 for (cp = name; *cp != '\0'; cp++) dots += (*cp == '.');
chenbruce018fdb22019-06-12 18:08:04 +0800228 const bool trailing_dot = (cp > name && *--cp == '.') ? true : false;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900229
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900230 /*
231 * If there are enough dots in the name, let's just give it a
232 * try 'as is'. The threshold can be set with the "ndots" option.
233 * Also, query 'as is', if there is a trailing dot in the name.
234 */
235 saved_herrno = -1;
236 if (dots >= statp->ndots || trailing_dot) {
Hungming Chen947aab02018-12-27 18:33:19 +0800237 ret = res_nquerydomain(statp, name, NULL, cl, type, answer, anslen, herrno);
Bernie Innocenti9c575932018-09-07 21:10:25 +0900238 if (ret > 0 || trailing_dot) return ret;
Hungming Chen6c84a3d2018-12-26 16:14:17 +0800239 saved_herrno = *herrno;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900240 tried_as_is++;
241 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900242
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900243 /*
244 * We do at least one level of search if
chenbruce018fdb22019-06-12 18:08:04 +0800245 * - there is no dot, or
246 * - there is at least one dot and there is no trailing dot.
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900247 */
chenbruce018fdb22019-06-12 18:08:04 +0800248 if ((!dots) || (dots && !trailing_dot)) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900249 int done = 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900250
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900251 /* Unfortunately we need to load network-specific info
252 * (dns servers, search domains) before
253 * the domain stuff is tried. Will have a better
254 * fix after thread pools are used as this will
255 * be loaded once for the thread instead of each
256 * time a query is tried.
257 */
258 _resolv_populate_res_for_net(statp);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900259
Luke Huang2dac4382019-06-24 13:28:44 +0800260 for (const auto& domain : statp->search_domains) {
261 if (domain == "." || domain == "") ++root_on_list;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900262
Luke Huang2dac4382019-06-24 13:28:44 +0800263 ret = res_nquerydomain(statp, name, domain.c_str(), cl, type, answer, anslen, herrno);
Bernie Innocenti9c575932018-09-07 21:10:25 +0900264 if (ret > 0) return ret;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900265
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900266 /*
267 * If no server present, give up.
268 * If name isn't found in this domain,
269 * keep trying higher domains in the search list
270 * (if that's enabled).
271 * On a NO_DATA error, keep trying, otherwise
272 * a wildcard entry of another type could keep us
273 * from finding this entry higher in the domain.
274 * If we get some other error (negative answer or
275 * server failure), then stop searching up,
276 * but try the input name below in case it's
277 * fully-qualified.
278 */
279 if (errno == ECONNREFUSED) {
Hungming Chen6c84a3d2018-12-26 16:14:17 +0800280 *herrno = TRY_AGAIN;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900281 return -1;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900282 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900283
Hungming Chen6c84a3d2018-12-26 16:14:17 +0800284 switch (*herrno) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900285 case NO_DATA:
286 got_nodata++;
Bernie Innocenti3a4002e2018-10-12 21:27:45 +0900287 break;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900288 case HOST_NOT_FOUND:
289 /* keep trying */
290 break;
291 case TRY_AGAIN:
292 if (hp->rcode == SERVFAIL) {
293 /* try next search element, if any */
294 got_servfail++;
295 break;
296 }
Bernie Innocentif40b3bd2018-10-10 22:30:12 +0900297 [[fallthrough]];
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900298 default:
299 /* anything else implies that we're done */
300 done++;
301 }
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900302 }
303 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900304
chenbruce018fdb22019-06-12 18:08:04 +0800305 // if we have not already tried the name "as is", do that now.
306 // note that we do this regardless of how many dots were in the
307 // name or whether it ends with a dot.
308 if (!tried_as_is && !root_on_list) {
Hungming Chen947aab02018-12-27 18:33:19 +0800309 ret = res_nquerydomain(statp, name, NULL, cl, type, answer, anslen, herrno);
Bernie Innocenti9c575932018-09-07 21:10:25 +0900310 if (ret > 0) return ret;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900311 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900312
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900313 /* if we got here, we didn't satisfy the search.
314 * if we did an initial full query, return that query's H_ERRNO
315 * (note that we wouldn't be here if that query had succeeded).
316 * else if we ever got a nodata, send that back as the reason.
317 * else send back meaningless H_ERRNO, that being the one from
318 * the last DNSRCH we did.
319 */
320 if (saved_herrno != -1)
Hungming Chen6c84a3d2018-12-26 16:14:17 +0800321 *herrno = saved_herrno;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900322 else if (got_nodata)
Hungming Chen6c84a3d2018-12-26 16:14:17 +0800323 *herrno = NO_DATA;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900324 else if (got_servfail)
Hungming Chen6c84a3d2018-12-26 16:14:17 +0800325 *herrno = TRY_AGAIN;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900326 return -1;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900327}
328
329/*
330 * Perform a call on res_query on the concatenation of name and domain,
331 * removing a trailing dot from name if domain is NULL.
332 */
Mike Yubfb1b342018-11-06 15:42:36 +0800333int res_nquerydomain(res_state statp, const char* name, const char* domain, int cl,
chenbrucec51f1212019-09-12 16:59:33 +0800334 int type, /* class and type of query */
335 uint8_t* answer, /* buffer to put answer */
336 int anslen, /* size of answer */
337 int* herrno) /* legacy and extended h_errno NETD_RESOLV_H_ERRNO_EXT_* */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900338{
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900339 char nbuf[MAXDNAME];
340 const char* longname = nbuf;
341 int n, d;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900342
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900343 if (domain == NULL) {
Ken Chenffc224a2019-03-19 17:41:28 +0800344 LOG(DEBUG) << __func__ << ": (null, " << cl << ", " << type << ")";
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900345 /*
346 * Check for trailing '.';
347 * copy without '.' if present.
348 */
349 n = strlen(name);
350 if (n >= MAXDNAME) {
Hungming Chen6c84a3d2018-12-26 16:14:17 +0800351 *herrno = NO_RECOVERY;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900352 return -1;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900353 }
354 n--;
355 if (n >= 0 && name[n] == '.') {
356 strncpy(nbuf, name, (size_t) n);
357 nbuf[n] = '\0';
358 } else
359 longname = name;
360 } else {
Ken Chenffc224a2019-03-19 17:41:28 +0800361 LOG(DEBUG) << __func__ << ": (" << cl << ", " << type << ")";
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900362 n = strlen(name);
363 d = strlen(domain);
364 if (n + d + 1 >= MAXDNAME) {
Hungming Chen6c84a3d2018-12-26 16:14:17 +0800365 *herrno = NO_RECOVERY;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900366 return -1;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900367 }
368 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
369 }
Hungming Chen947aab02018-12-27 18:33:19 +0800370 return res_nquery(statp, longname, cl, type, answer, anslen, herrno);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900371}