blob: acf6160feef01188019827eff4310d7707de3fd5 [file] [log] [blame]
Bernie Innocenti55864192018-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
Bernie Innocenti55864192018-08-30 04:05:20 +090073#include <arpa/inet.h>
74#include <arpa/nameser.h>
75#include <ctype.h>
76#include <errno.h>
77#include <netdb.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090078#include <netinet/in.h>
79#include <sys/param.h>
80#include <sys/types.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090081#include <stdio.h>
82#include <stdlib.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090083#include <string.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090084#include <unistd.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090085
Bernie Innocenti189eb502018-10-01 23:10:18 +090086#include "resolv_cache.h"
87#include "resolv_private.h"
88
Bernie Innocenti55864192018-08-30 04:05:20 +090089/* Options. Leave them on. */
90#ifndef DEBUG
91#define DEBUG
92#endif
93
94#if PACKETSZ > 1024
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090095#define MAXPACKET PACKETSZ
Bernie Innocenti55864192018-08-30 04:05:20 +090096#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090097#define MAXPACKET 1024
Bernie Innocenti55864192018-08-30 04:05:20 +090098#endif
99
100/*
101 * Formulate a normal query, send, and await answer.
102 * Returned answer is placed in supplied buffer "answer".
103 * Perform preliminary check of answer, returning success only
104 * if no error is indicated and the answer count is nonzero.
105 * Return the size of the response on success, -1 on error.
Hungming Chen56273d72018-12-26 16:14:17 +0800106 * Error number is left in *herrno.
Bernie Innocenti55864192018-08-30 04:05:20 +0900107 *
108 * Caller must parse answer and determine whether it answers the question.
109 */
Mike Yu69615f62018-11-06 15:42:36 +0800110int res_nquery(res_state statp, const char* name, // domain name
111 int cl, int type, // class and type of query
112 u_char* answer, // buffer to put answer
113 int anslen, // size of answer buffer
Hungming Chen7f0d3292018-12-27 18:33:19 +0800114 int* herrno) // legacy and extended h_errno
115 // NETD_RESOLV_H_ERRNO_EXT_*
Bernie Innocenti55864192018-08-30 04:05:20 +0900116{
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900117 u_char buf[MAXPACKET];
118 HEADER* hp = (HEADER*) (void*) answer;
119 int n;
Mike Yu69615f62018-11-06 15:42:36 +0800120 int rcode = NOERROR;
Ken Chenbfd32022019-01-02 14:59:38 +0800121 bool retried = false;
Bernie Innocenti55864192018-08-30 04:05:20 +0900122
123again:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900124 hp->rcode = NOERROR; /* default */
Bernie Innocenti55864192018-08-30 04:05:20 +0900125
126#ifdef DEBUG
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900127 if (statp->options & RES_DEBUG) printf(";; res_query(%s, %d, %d)\n", name, cl, type);
Bernie Innocenti55864192018-08-30 04:05:20 +0900128#endif
129
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900130 n = res_nmkquery(statp, QUERY, name, cl, type, NULL, 0, NULL, buf, sizeof(buf));
Ken Chenbfd32022019-01-02 14:59:38 +0800131 if (n > 0 && (statp->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0U && !retried)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900132 n = res_nopt(statp, n, buf, sizeof(buf), anslen);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900133 if (n <= 0) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900134#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900135 if (statp->options & RES_DEBUG) printf(";; res_query: mkquery failed\n");
Bernie Innocenti55864192018-08-30 04:05:20 +0900136#endif
Hungming Chen56273d72018-12-26 16:14:17 +0800137 *herrno = NO_RECOVERY;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900138 return n;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900139 }
Luke Huang952d0942018-12-26 16:53:03 +0800140 n = res_nsend(statp, buf, n, answer, anslen, &rcode, 0);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900141 if (n < 0) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900142 /* if the query choked with EDNS0, retry without EDNS0 */
143 if ((statp->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0U &&
Ken Chenbfd32022019-01-02 14:59:38 +0800144 (statp->_flags & RES_F_EDNS0ERR) && !retried) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900145 if (statp->options & RES_DEBUG) printf(";; res_nquery: retry without EDNS0\n");
Ken Chenbfd32022019-01-02 14:59:38 +0800146 retried = true;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900147 goto again;
148 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900149#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900150 if (statp->options & RES_DEBUG) printf(";; res_query: send error\n");
Bernie Innocenti55864192018-08-30 04:05:20 +0900151#endif
Hungming Chen7f0d3292018-12-27 18:33:19 +0800152 // Note that rcodes SERVFAIL, NOTIMP, REFUSED may cause res_nquery() to return a general
153 // error code EAI_AGAIN, but mapping the error code from rcode as res_queryN() does for
154 // getaddrinfo(). Different rcodes trigger different behaviors:
155 //
156 // - SERVFAIL, NOTIMP, REFUSED
157 // These result in send_dg() returning 0, causing res_nsend() to try the next
158 // nameserver. After all nameservers failed, res_nsend() returns -ETIMEDOUT, causing
159 // res_nquery() to return EAI_AGAIN here regardless of the rcode from the DNS response.
160 //
161 // - NXDOMAIN, FORMERR
162 // These rcodes may cause res_nsend() to return successfully (i.e. the result is a
163 // positive integer). In this case, res_nquery() returns error number by referring
164 // the rcode from the DNS response.
165 switch (rcode) {
166 case RCODE_TIMEOUT: // Not defined in RFC.
167 // DNS metrics monitors DNS query timeout.
168 *herrno = NETD_RESOLV_H_ERRNO_EXT_TIMEOUT; // extended h_errno.
169 break;
170 default:
171 *herrno = TRY_AGAIN;
172 break;
173 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900174 return n;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900175 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900176
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900177 if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900178#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900179 if (statp->options & RES_DEBUG)
180 printf(";; rcode = (%s), counts = an:%d ns:%d ar:%d\n", p_rcode(hp->rcode),
181 ntohs(hp->ancount), ntohs(hp->nscount), ntohs(hp->arcount));
Bernie Innocenti55864192018-08-30 04:05:20 +0900182#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900183 switch (hp->rcode) {
184 case NXDOMAIN:
Hungming Chen56273d72018-12-26 16:14:17 +0800185 *herrno = HOST_NOT_FOUND;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900186 break;
187 case SERVFAIL:
Hungming Chen56273d72018-12-26 16:14:17 +0800188 *herrno = TRY_AGAIN;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900189 break;
190 case NOERROR:
Hungming Chen56273d72018-12-26 16:14:17 +0800191 *herrno = NO_DATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900192 break;
193 case FORMERR:
194 case NOTIMP:
195 case REFUSED:
196 default:
Hungming Chen56273d72018-12-26 16:14:17 +0800197 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900198 break;
199 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900200 return -1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900201 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900202 return n;
Bernie Innocenti55864192018-08-30 04:05:20 +0900203}
204
205/*
206 * Formulate a normal query, send, and retrieve answer in supplied buffer.
207 * Return the size of the response on success, -1 on error.
208 * If enabled, implement search rules until answer or unrecoverable failure
Hungming Chen56273d72018-12-26 16:14:17 +0800209 * is detected. Error code, if any, is left in *herrno.
Bernie Innocenti55864192018-08-30 04:05:20 +0900210 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900211int res_nsearch(res_state statp, const char* name, /* domain name */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900212 int cl, int type, /* class and type of query */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900213 u_char* answer, /* buffer to put answer */
Mike Yu69615f62018-11-06 15:42:36 +0800214 int anslen, /* size of answer */
Hungming Chen7f0d3292018-12-27 18:33:19 +0800215 int* herrno) /* legacy and extended
216 h_errno NETD_RESOLV_H_ERRNO_EXT_* */
Bernie Innocenti55864192018-08-30 04:05:20 +0900217{
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900218 const char *cp, *const *domain;
219 HEADER* hp = (HEADER*) (void*) answer;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900220 u_int dots;
221 int trailing_dot, ret, saved_herrno;
222 int got_nodata = 0, got_servfail = 0, root_on_list = 0;
223 int tried_as_is = 0;
224 int searched = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900225
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900226 errno = 0;
Hungming Chen56273d72018-12-26 16:14:17 +0800227 *herrno = HOST_NOT_FOUND; /* True if we never query. */
Bernie Innocenti55864192018-08-30 04:05:20 +0900228
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900229 dots = 0;
230 for (cp = name; *cp != '\0'; cp++) dots += (*cp == '.');
231 trailing_dot = 0;
232 if (cp > name && *--cp == '.') trailing_dot++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900233
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900234 /*
235 * If there are enough dots in the name, let's just give it a
236 * try 'as is'. The threshold can be set with the "ndots" option.
237 * Also, query 'as is', if there is a trailing dot in the name.
238 */
239 saved_herrno = -1;
240 if (dots >= statp->ndots || trailing_dot) {
Hungming Chen7f0d3292018-12-27 18:33:19 +0800241 ret = res_nquerydomain(statp, name, NULL, cl, type, answer, anslen, herrno);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900242 if (ret > 0 || trailing_dot) return ret;
Hungming Chen56273d72018-12-26 16:14:17 +0800243 saved_herrno = *herrno;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900244 tried_as_is++;
245 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900246
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900247 /*
248 * We do at least one level of search if
249 * - there is no dot and RES_DEFNAME is set, or
250 * - there is at least one dot, there is no trailing dot,
251 * and RES_DNSRCH is set.
252 */
253 if ((!dots && (statp->options & RES_DEFNAMES) != 0U) ||
254 (dots && !trailing_dot && (statp->options & RES_DNSRCH) != 0U)) {
255 int done = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900256
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900257 /* Unfortunately we need to load network-specific info
258 * (dns servers, search domains) before
259 * the domain stuff is tried. Will have a better
260 * fix after thread pools are used as this will
261 * be loaded once for the thread instead of each
262 * time a query is tried.
263 */
264 _resolv_populate_res_for_net(statp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900265
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900266 for (domain = (const char* const*) statp->dnsrch; *domain && !done; domain++) {
267 searched = 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900268
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900269 if (domain[0][0] == '\0' || (domain[0][0] == '.' && domain[0][1] == '\0'))
270 root_on_list++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900271
Hungming Chen7f0d3292018-12-27 18:33:19 +0800272 ret = res_nquerydomain(statp, name, *domain, cl, type, answer, anslen, herrno);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900273 if (ret > 0) return ret;
Bernie Innocenti55864192018-08-30 04:05:20 +0900274
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900275 /*
276 * If no server present, give up.
277 * If name isn't found in this domain,
278 * keep trying higher domains in the search list
279 * (if that's enabled).
280 * On a NO_DATA error, keep trying, otherwise
281 * a wildcard entry of another type could keep us
282 * from finding this entry higher in the domain.
283 * If we get some other error (negative answer or
284 * server failure), then stop searching up,
285 * but try the input name below in case it's
286 * fully-qualified.
287 */
288 if (errno == ECONNREFUSED) {
Hungming Chen56273d72018-12-26 16:14:17 +0800289 *herrno = TRY_AGAIN;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900290 return -1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900291 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900292
Hungming Chen56273d72018-12-26 16:14:17 +0800293 switch (*herrno) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900294 case NO_DATA:
295 got_nodata++;
Bernie Innocenti11cd0202018-10-12 21:27:45 +0900296 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900297 case HOST_NOT_FOUND:
298 /* keep trying */
299 break;
300 case TRY_AGAIN:
301 if (hp->rcode == SERVFAIL) {
302 /* try next search element, if any */
303 got_servfail++;
304 break;
305 }
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +0900306 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900307 default:
308 /* anything else implies that we're done */
309 done++;
310 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900311
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900312 /* if we got here for some reason other than DNSRCH,
313 * we only wanted one iteration of the loop, so stop.
314 */
315 if ((statp->options & RES_DNSRCH) == 0U) done++;
316 }
317 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900318
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900319 /*
320 * If the query has not already been tried as is then try it
321 * unless RES_NOTLDQUERY is set and there were no dots.
322 */
323 if ((dots || !searched || (statp->options & RES_NOTLDQUERY) == 0U) &&
324 !(tried_as_is || root_on_list)) {
Hungming Chen7f0d3292018-12-27 18:33:19 +0800325 ret = res_nquerydomain(statp, name, NULL, cl, type, answer, anslen, herrno);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900326 if (ret > 0) return ret;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900327 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900328
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900329 /* if we got here, we didn't satisfy the search.
330 * if we did an initial full query, return that query's H_ERRNO
331 * (note that we wouldn't be here if that query had succeeded).
332 * else if we ever got a nodata, send that back as the reason.
333 * else send back meaningless H_ERRNO, that being the one from
334 * the last DNSRCH we did.
335 */
336 if (saved_herrno != -1)
Hungming Chen56273d72018-12-26 16:14:17 +0800337 *herrno = saved_herrno;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900338 else if (got_nodata)
Hungming Chen56273d72018-12-26 16:14:17 +0800339 *herrno = NO_DATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900340 else if (got_servfail)
Hungming Chen56273d72018-12-26 16:14:17 +0800341 *herrno = TRY_AGAIN;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900342 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900343}
344
345/*
346 * Perform a call on res_query on the concatenation of name and domain,
347 * removing a trailing dot from name if domain is NULL.
348 */
Mike Yu69615f62018-11-06 15:42:36 +0800349int res_nquerydomain(res_state statp, const char* name, const char* domain, int cl,
350 int type, /* class and type of query */
351 u_char* answer, /* buffer to put answer */
352 int anslen, /* size of answer */
Hungming Chen7f0d3292018-12-27 18:33:19 +0800353 int* herrno) /* legacy and extended h_errno NETD_RESOLV_H_ERRNO_EXT_* */
Bernie Innocenti55864192018-08-30 04:05:20 +0900354{
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900355 char nbuf[MAXDNAME];
356 const char* longname = nbuf;
357 int n, d;
Bernie Innocenti55864192018-08-30 04:05:20 +0900358
359#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900360 if (statp->options & RES_DEBUG)
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900361 printf(";; res_nquerydomain(%s, %s, %d, %d)\n", name, domain ? domain : "<Nil>", cl,
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900362 type);
Bernie Innocenti55864192018-08-30 04:05:20 +0900363#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900364 if (domain == NULL) {
365 /*
366 * Check for trailing '.';
367 * copy without '.' if present.
368 */
369 n = strlen(name);
370 if (n >= MAXDNAME) {
Hungming Chen56273d72018-12-26 16:14:17 +0800371 *herrno = NO_RECOVERY;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900372 return -1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900373 }
374 n--;
375 if (n >= 0 && name[n] == '.') {
376 strncpy(nbuf, name, (size_t) n);
377 nbuf[n] = '\0';
378 } else
379 longname = name;
380 } else {
381 n = strlen(name);
382 d = strlen(domain);
383 if (n + d + 1 >= MAXDNAME) {
Hungming Chen56273d72018-12-26 16:14:17 +0800384 *herrno = NO_RECOVERY;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900385 return -1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900386 }
387 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
388 }
Hungming Chen7f0d3292018-12-27 18:33:19 +0800389 return res_nquery(statp, longname, cl, type, answer, anslen, herrno);
Bernie Innocenti55864192018-08-30 04:05:20 +0900390}