blob: f81b33e2c7a1b1c6cb319e7e39a1e080b1d5538e [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;
120 u_int oflags;
Mike Yu69615f62018-11-06 15:42:36 +0800121 int rcode = NOERROR;
Bernie Innocenti55864192018-08-30 04:05:20 +0900122
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900123 oflags = statp->_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +0900124
125again:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900126 hp->rcode = NOERROR; /* default */
Bernie Innocenti55864192018-08-30 04:05:20 +0900127
128#ifdef DEBUG
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900129 if (statp->options & RES_DEBUG) printf(";; res_query(%s, %d, %d)\n", name, cl, type);
Bernie Innocenti55864192018-08-30 04:05:20 +0900130#endif
131
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900132 n = res_nmkquery(statp, QUERY, name, cl, type, NULL, 0, NULL, buf, sizeof(buf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900133 if (n > 0 && (statp->_flags & RES_F_EDNS0ERR) == 0 &&
134 (statp->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0U)
135 n = res_nopt(statp, n, buf, sizeof(buf), anslen);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900136 if (n <= 0) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900137#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900138 if (statp->options & RES_DEBUG) printf(";; res_query: mkquery failed\n");
Bernie Innocenti55864192018-08-30 04:05:20 +0900139#endif
Hungming Chen56273d72018-12-26 16:14:17 +0800140 *herrno = NO_RECOVERY;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900141 return n;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900142 }
Mike Yu69615f62018-11-06 15:42:36 +0800143 n = res_nsend(statp, buf, n, answer, anslen, &rcode);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900144 if (n < 0) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900145 /* if the query choked with EDNS0, retry without EDNS0 */
146 if ((statp->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0U &&
147 ((oflags ^ statp->_flags) & RES_F_EDNS0ERR) != 0) {
148 statp->_flags |= RES_F_EDNS0ERR;
149 if (statp->options & RES_DEBUG) printf(";; res_nquery: retry without EDNS0\n");
150 goto again;
151 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900152#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900153 if (statp->options & RES_DEBUG) printf(";; res_query: send error\n");
Bernie Innocenti55864192018-08-30 04:05:20 +0900154#endif
Hungming Chen7f0d3292018-12-27 18:33:19 +0800155 // Note that rcodes SERVFAIL, NOTIMP, REFUSED may cause res_nquery() to return a general
156 // error code EAI_AGAIN, but mapping the error code from rcode as res_queryN() does for
157 // getaddrinfo(). Different rcodes trigger different behaviors:
158 //
159 // - SERVFAIL, NOTIMP, REFUSED
160 // These result in send_dg() returning 0, causing res_nsend() to try the next
161 // nameserver. After all nameservers failed, res_nsend() returns -ETIMEDOUT, causing
162 // res_nquery() to return EAI_AGAIN here regardless of the rcode from the DNS response.
163 //
164 // - NXDOMAIN, FORMERR
165 // These rcodes may cause res_nsend() to return successfully (i.e. the result is a
166 // positive integer). In this case, res_nquery() returns error number by referring
167 // the rcode from the DNS response.
168 switch (rcode) {
169 case RCODE_TIMEOUT: // Not defined in RFC.
170 // DNS metrics monitors DNS query timeout.
171 *herrno = NETD_RESOLV_H_ERRNO_EXT_TIMEOUT; // extended h_errno.
172 break;
173 default:
174 *herrno = TRY_AGAIN;
175 break;
176 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900177 return n;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900178 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900179
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900180 if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900181#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900182 if (statp->options & RES_DEBUG)
183 printf(";; rcode = (%s), counts = an:%d ns:%d ar:%d\n", p_rcode(hp->rcode),
184 ntohs(hp->ancount), ntohs(hp->nscount), ntohs(hp->arcount));
Bernie Innocenti55864192018-08-30 04:05:20 +0900185#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900186 switch (hp->rcode) {
187 case NXDOMAIN:
Hungming Chen56273d72018-12-26 16:14:17 +0800188 *herrno = HOST_NOT_FOUND;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900189 break;
190 case SERVFAIL:
Hungming Chen56273d72018-12-26 16:14:17 +0800191 *herrno = TRY_AGAIN;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900192 break;
193 case NOERROR:
Hungming Chen56273d72018-12-26 16:14:17 +0800194 *herrno = NO_DATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900195 break;
196 case FORMERR:
197 case NOTIMP:
198 case REFUSED:
199 default:
Hungming Chen56273d72018-12-26 16:14:17 +0800200 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900201 break;
202 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900203 return -1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900204 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900205 return n;
Bernie Innocenti55864192018-08-30 04:05:20 +0900206}
207
208/*
209 * Formulate a normal query, send, and retrieve answer in supplied buffer.
210 * Return the size of the response on success, -1 on error.
211 * If enabled, implement search rules until answer or unrecoverable failure
Hungming Chen56273d72018-12-26 16:14:17 +0800212 * is detected. Error code, if any, is left in *herrno.
Bernie Innocenti55864192018-08-30 04:05:20 +0900213 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900214int res_nsearch(res_state statp, const char* name, /* domain name */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900215 int cl, int type, /* class and type of query */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900216 u_char* answer, /* buffer to put answer */
Mike Yu69615f62018-11-06 15:42:36 +0800217 int anslen, /* size of answer */
Hungming Chen7f0d3292018-12-27 18:33:19 +0800218 int* herrno) /* legacy and extended
219 h_errno NETD_RESOLV_H_ERRNO_EXT_* */
Bernie Innocenti55864192018-08-30 04:05:20 +0900220{
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900221 const char *cp, *const *domain;
222 HEADER* hp = (HEADER*) (void*) answer;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900223 u_int dots;
224 int trailing_dot, ret, saved_herrno;
225 int got_nodata = 0, got_servfail = 0, root_on_list = 0;
226 int tried_as_is = 0;
227 int searched = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900228
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900229 errno = 0;
Hungming Chen56273d72018-12-26 16:14:17 +0800230 *herrno = HOST_NOT_FOUND; /* True if we never query. */
Bernie Innocenti55864192018-08-30 04:05:20 +0900231
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900232 dots = 0;
233 for (cp = name; *cp != '\0'; cp++) dots += (*cp == '.');
234 trailing_dot = 0;
235 if (cp > name && *--cp == '.') trailing_dot++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900236
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900237 /*
238 * If there are enough dots in the name, let's just give it a
239 * try 'as is'. The threshold can be set with the "ndots" option.
240 * Also, query 'as is', if there is a trailing dot in the name.
241 */
242 saved_herrno = -1;
243 if (dots >= statp->ndots || trailing_dot) {
Hungming Chen7f0d3292018-12-27 18:33:19 +0800244 ret = res_nquerydomain(statp, name, NULL, cl, type, answer, anslen, herrno);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900245 if (ret > 0 || trailing_dot) return ret;
Hungming Chen56273d72018-12-26 16:14:17 +0800246 saved_herrno = *herrno;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900247 tried_as_is++;
248 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900249
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900250 /*
251 * We do at least one level of search if
252 * - there is no dot and RES_DEFNAME is set, or
253 * - there is at least one dot, there is no trailing dot,
254 * and RES_DNSRCH is set.
255 */
256 if ((!dots && (statp->options & RES_DEFNAMES) != 0U) ||
257 (dots && !trailing_dot && (statp->options & RES_DNSRCH) != 0U)) {
258 int done = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900259
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900260 /* Unfortunately we need to load network-specific info
261 * (dns servers, search domains) before
262 * the domain stuff is tried. Will have a better
263 * fix after thread pools are used as this will
264 * be loaded once for the thread instead of each
265 * time a query is tried.
266 */
267 _resolv_populate_res_for_net(statp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900268
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900269 for (domain = (const char* const*) statp->dnsrch; *domain && !done; domain++) {
270 searched = 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900271
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900272 if (domain[0][0] == '\0' || (domain[0][0] == '.' && domain[0][1] == '\0'))
273 root_on_list++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900274
Hungming Chen7f0d3292018-12-27 18:33:19 +0800275 ret = res_nquerydomain(statp, name, *domain, cl, type, answer, anslen, herrno);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900276 if (ret > 0) return ret;
Bernie Innocenti55864192018-08-30 04:05:20 +0900277
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900278 /*
279 * If no server present, give up.
280 * If name isn't found in this domain,
281 * keep trying higher domains in the search list
282 * (if that's enabled).
283 * On a NO_DATA error, keep trying, otherwise
284 * a wildcard entry of another type could keep us
285 * from finding this entry higher in the domain.
286 * If we get some other error (negative answer or
287 * server failure), then stop searching up,
288 * but try the input name below in case it's
289 * fully-qualified.
290 */
291 if (errno == ECONNREFUSED) {
Hungming Chen56273d72018-12-26 16:14:17 +0800292 *herrno = TRY_AGAIN;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900293 return -1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900294 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900295
Hungming Chen56273d72018-12-26 16:14:17 +0800296 switch (*herrno) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900297 case NO_DATA:
298 got_nodata++;
Bernie Innocenti11cd0202018-10-12 21:27:45 +0900299 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900300 case HOST_NOT_FOUND:
301 /* keep trying */
302 break;
303 case TRY_AGAIN:
304 if (hp->rcode == SERVFAIL) {
305 /* try next search element, if any */
306 got_servfail++;
307 break;
308 }
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +0900309 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900310 default:
311 /* anything else implies that we're done */
312 done++;
313 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900314
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900315 /* if we got here for some reason other than DNSRCH,
316 * we only wanted one iteration of the loop, so stop.
317 */
318 if ((statp->options & RES_DNSRCH) == 0U) done++;
319 }
320 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900321
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900322 /*
323 * If the query has not already been tried as is then try it
324 * unless RES_NOTLDQUERY is set and there were no dots.
325 */
326 if ((dots || !searched || (statp->options & RES_NOTLDQUERY) == 0U) &&
327 !(tried_as_is || root_on_list)) {
Hungming Chen7f0d3292018-12-27 18:33:19 +0800328 ret = res_nquerydomain(statp, name, NULL, cl, type, answer, anslen, herrno);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900329 if (ret > 0) return ret;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900330 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900331
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900332 /* if we got here, we didn't satisfy the search.
333 * if we did an initial full query, return that query's H_ERRNO
334 * (note that we wouldn't be here if that query had succeeded).
335 * else if we ever got a nodata, send that back as the reason.
336 * else send back meaningless H_ERRNO, that being the one from
337 * the last DNSRCH we did.
338 */
339 if (saved_herrno != -1)
Hungming Chen56273d72018-12-26 16:14:17 +0800340 *herrno = saved_herrno;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900341 else if (got_nodata)
Hungming Chen56273d72018-12-26 16:14:17 +0800342 *herrno = NO_DATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900343 else if (got_servfail)
Hungming Chen56273d72018-12-26 16:14:17 +0800344 *herrno = TRY_AGAIN;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900345 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900346}
347
348/*
349 * Perform a call on res_query on the concatenation of name and domain,
350 * removing a trailing dot from name if domain is NULL.
351 */
Mike Yu69615f62018-11-06 15:42:36 +0800352int res_nquerydomain(res_state statp, const char* name, const char* domain, int cl,
353 int type, /* class and type of query */
354 u_char* answer, /* buffer to put answer */
355 int anslen, /* size of answer */
Hungming Chen7f0d3292018-12-27 18:33:19 +0800356 int* herrno) /* legacy and extended h_errno NETD_RESOLV_H_ERRNO_EXT_* */
Bernie Innocenti55864192018-08-30 04:05:20 +0900357{
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900358 char nbuf[MAXDNAME];
359 const char* longname = nbuf;
360 int n, d;
Bernie Innocenti55864192018-08-30 04:05:20 +0900361
362#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900363 if (statp->options & RES_DEBUG)
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900364 printf(";; res_nquerydomain(%s, %s, %d, %d)\n", name, domain ? domain : "<Nil>", cl,
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900365 type);
Bernie Innocenti55864192018-08-30 04:05:20 +0900366#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900367 if (domain == NULL) {
368 /*
369 * Check for trailing '.';
370 * copy without '.' if present.
371 */
372 n = strlen(name);
373 if (n >= MAXDNAME) {
Hungming Chen56273d72018-12-26 16:14:17 +0800374 *herrno = NO_RECOVERY;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900375 return -1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900376 }
377 n--;
378 if (n >= 0 && name[n] == '.') {
379 strncpy(nbuf, name, (size_t) n);
380 nbuf[n] = '\0';
381 } else
382 longname = name;
383 } else {
384 n = strlen(name);
385 d = strlen(domain);
386 if (n + d + 1 >= MAXDNAME) {
Hungming Chen56273d72018-12-26 16:14:17 +0800387 *herrno = NO_RECOVERY;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900388 return -1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900389 }
390 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
391 }
Hungming Chen7f0d3292018-12-27 18:33:19 +0800392 return res_nquery(statp, longname, cl, type, answer, anslen, herrno);
Bernie Innocenti55864192018-08-30 04:05:20 +0900393}