blob: e20548540759ba7a689df874f197b95ed75e97a8 [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
73#include <sys/cdefs.h>
74#if defined(LIBC_SCCS) && !defined(lint)
75#ifdef notdef
76static const char sccsid[] = "@(#)res_query.c 8.1 (Berkeley) 6/4/93";
77static const char rcsid[] = "Id: res_query.c,v 1.2.2.3.4.2 2004/03/16 12:34:19 marka Exp";
78#else
79__RCSID("$NetBSD: res_query.c,v 1.7 2006/01/24 17:41:25 christos Exp $");
80#endif
81#endif /* LIBC_SCCS and not lint */
82
Bernie Innocenti55864192018-08-30 04:05:20 +090083#include <arpa/inet.h>
84#include <arpa/nameser.h>
85#include <ctype.h>
86#include <errno.h>
87#include <netdb.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090088#include <netinet/in.h>
89#include <sys/param.h>
90#include <sys/types.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090091#ifdef ANDROID_CHANGES
92#include "resolv_cache.h"
93#include "resolv_private.h"
94#else
95#include <resolv.h>
96#endif
97#include <stdio.h>
98#include <stdlib.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090099#include <string.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900100#include <unistd.h>
Bernie Innocenti55864192018-08-30 04:05:20 +0900101
102#define DISABLE_HOST_ALIAS 1
103
104/* Options. Leave them on. */
105#ifndef DEBUG
106#define DEBUG
107#endif
108
109#if PACKETSZ > 1024
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900110#define MAXPACKET PACKETSZ
Bernie Innocenti55864192018-08-30 04:05:20 +0900111#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900112#define MAXPACKET 1024
Bernie Innocenti55864192018-08-30 04:05:20 +0900113#endif
114
115/*
116 * Formulate a normal query, send, and await answer.
117 * Returned answer is placed in supplied buffer "answer".
118 * Perform preliminary check of answer, returning success only
119 * if no error is indicated and the answer count is nonzero.
120 * Return the size of the response on success, -1 on error.
121 * Error number is left in H_ERRNO.
122 *
123 * Caller must parse answer and determine whether it answers the question.
124 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900125int res_nquery(res_state statp, const char* name, /* domain name */
126 int class, int type, /* class and type of query */
127 u_char* answer, /* buffer to put answer */
128 int anslen) /* size of answer buffer */
Bernie Innocenti55864192018-08-30 04:05:20 +0900129{
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900130 u_char buf[MAXPACKET];
131 HEADER* hp = (HEADER*) (void*) answer;
132 int n;
133 u_int oflags;
Bernie Innocenti55864192018-08-30 04:05:20 +0900134
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900135 oflags = statp->_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +0900136
137again:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900138 hp->rcode = NOERROR; /* default */
Bernie Innocenti55864192018-08-30 04:05:20 +0900139
140#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900141 if (statp->options & RES_DEBUG) printf(";; res_query(%s, %d, %d)\n", name, class, type);
Bernie Innocenti55864192018-08-30 04:05:20 +0900142#endif
143
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900144 n = res_nmkquery(statp, QUERY, name, class, type, NULL, 0, NULL, buf, sizeof(buf));
Bernie Innocenti55864192018-08-30 04:05:20 +0900145#ifdef RES_USE_EDNS0
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900146 if (n > 0 && (statp->_flags & RES_F_EDNS0ERR) == 0 &&
147 (statp->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0U)
148 n = res_nopt(statp, n, buf, sizeof(buf), anslen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900149#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900150 if (n <= 0) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900151#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900152 if (statp->options & RES_DEBUG) printf(";; res_query: mkquery failed\n");
Bernie Innocenti55864192018-08-30 04:05:20 +0900153#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900154 RES_SET_H_ERRNO(statp, NO_RECOVERY);
155 return (n);
156 }
157 n = res_nsend(statp, buf, n, answer, anslen);
158 if (n < 0) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900159#ifdef RES_USE_EDNS0
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900160 /* if the query choked with EDNS0, retry without EDNS0 */
161 if ((statp->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0U &&
162 ((oflags ^ statp->_flags) & RES_F_EDNS0ERR) != 0) {
163 statp->_flags |= RES_F_EDNS0ERR;
164 if (statp->options & RES_DEBUG) printf(";; res_nquery: retry without EDNS0\n");
165 goto again;
166 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900167#endif
168#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900169 if (statp->options & RES_DEBUG) printf(";; res_query: send error\n");
Bernie Innocenti55864192018-08-30 04:05:20 +0900170#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900171 RES_SET_H_ERRNO(statp, TRY_AGAIN);
172 return (n);
173 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900174
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900175 if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900176#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900177 if (statp->options & RES_DEBUG)
178 printf(";; rcode = (%s), counts = an:%d ns:%d ar:%d\n", p_rcode(hp->rcode),
179 ntohs(hp->ancount), ntohs(hp->nscount), ntohs(hp->arcount));
Bernie Innocenti55864192018-08-30 04:05:20 +0900180#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900181 switch (hp->rcode) {
182 case NXDOMAIN:
183 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
184 break;
185 case SERVFAIL:
186 RES_SET_H_ERRNO(statp, TRY_AGAIN);
187 break;
188 case NOERROR:
189 RES_SET_H_ERRNO(statp, NO_DATA);
190 break;
191 case FORMERR:
192 case NOTIMP:
193 case REFUSED:
194 default:
195 RES_SET_H_ERRNO(statp, NO_RECOVERY);
196 break;
197 }
198 return (-1);
199 }
200 return (n);
Bernie Innocenti55864192018-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
207 * is detected. Error code, if any, is left in H_ERRNO.
208 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900209int res_nsearch(res_state statp, const char* name, /* domain name */
210 int class, int type, /* class and type of query */
211 u_char* answer, /* buffer to put answer */
212 int anslen) /* size of answer */
Bernie Innocenti55864192018-08-30 04:05:20 +0900213{
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900214 const char *cp, *const *domain;
215 HEADER* hp = (HEADER*) (void*) answer;
216 char tmp[NS_MAXDNAME];
217 u_int dots;
218 int trailing_dot, ret, saved_herrno;
219 int got_nodata = 0, got_servfail = 0, root_on_list = 0;
220 int tried_as_is = 0;
221 int searched = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900222
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900223 errno = 0;
224 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND); /* True if we never query. */
Bernie Innocenti55864192018-08-30 04:05:20 +0900225
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900226 dots = 0;
227 for (cp = name; *cp != '\0'; cp++) dots += (*cp == '.');
228 trailing_dot = 0;
229 if (cp > name && *--cp == '.') trailing_dot++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900230
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900231 /* If there aren't any dots, it could be a user-level alias. */
232 if (!dots && (cp = res_hostalias(statp, name, tmp, sizeof tmp)) != NULL)
233 return (res_nquery(statp, cp, class, type, answer, anslen));
Bernie Innocenti55864192018-08-30 04:05:20 +0900234
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900235 /*
236 * If there are enough dots in the name, let's just give it a
237 * try 'as is'. The threshold can be set with the "ndots" option.
238 * Also, query 'as is', if there is a trailing dot in the name.
239 */
240 saved_herrno = -1;
241 if (dots >= statp->ndots || trailing_dot) {
242 ret = res_nquerydomain(statp, name, NULL, class, type, answer, anslen);
243 if (ret > 0 || trailing_dot) return (ret);
244 saved_herrno = statp->res_h_errno;
245 tried_as_is++;
246 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900247
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900248 /*
249 * We do at least one level of search if
250 * - there is no dot and RES_DEFNAME is set, or
251 * - there is at least one dot, there is no trailing dot,
252 * and RES_DNSRCH is set.
253 */
254 if ((!dots && (statp->options & RES_DEFNAMES) != 0U) ||
255 (dots && !trailing_dot && (statp->options & RES_DNSRCH) != 0U)) {
256 int done = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900257
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900258 /* Unfortunately we need to load network-specific info
259 * (dns servers, search domains) before
260 * the domain stuff is tried. Will have a better
261 * fix after thread pools are used as this will
262 * be loaded once for the thread instead of each
263 * time a query is tried.
264 */
265 _resolv_populate_res_for_net(statp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900266
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900267 for (domain = (const char* const*) statp->dnsrch; *domain && !done; domain++) {
268 searched = 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900269
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900270 if (domain[0][0] == '\0' || (domain[0][0] == '.' && domain[0][1] == '\0'))
271 root_on_list++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900272
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900273 ret = res_nquerydomain(statp, name, *domain, class, type, answer, anslen);
274 if (ret > 0) return (ret);
Bernie Innocenti55864192018-08-30 04:05:20 +0900275
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900276 /*
277 * If no server present, give up.
278 * If name isn't found in this domain,
279 * keep trying higher domains in the search list
280 * (if that's enabled).
281 * On a NO_DATA error, keep trying, otherwise
282 * a wildcard entry of another type could keep us
283 * from finding this entry higher in the domain.
284 * If we get some other error (negative answer or
285 * server failure), then stop searching up,
286 * but try the input name below in case it's
287 * fully-qualified.
288 */
289 if (errno == ECONNREFUSED) {
290 RES_SET_H_ERRNO(statp, TRY_AGAIN);
291 return (-1);
292 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900293
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900294 switch (statp->res_h_errno) {
295 case NO_DATA:
296 got_nodata++;
297 /* FALLTHROUGH */
298 case HOST_NOT_FOUND:
299 /* keep trying */
300 break;
301 case TRY_AGAIN:
302 if (hp->rcode == SERVFAIL) {
303 /* try next search element, if any */
304 got_servfail++;
305 break;
306 }
307 /* FALLTHROUGH */
308 default:
309 /* anything else implies that we're done */
310 done++;
311 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900312
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900313 /* if we got here for some reason other than DNSRCH,
314 * we only wanted one iteration of the loop, so stop.
315 */
316 if ((statp->options & RES_DNSRCH) == 0U) done++;
317 }
318 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900319
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900320 /*
321 * If the query has not already been tried as is then try it
322 * unless RES_NOTLDQUERY is set and there were no dots.
323 */
324 if ((dots || !searched || (statp->options & RES_NOTLDQUERY) == 0U) &&
325 !(tried_as_is || root_on_list)) {
326 ret = res_nquerydomain(statp, name, NULL, class, type, answer, anslen);
327 if (ret > 0) return (ret);
328 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900329
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900330 /* if we got here, we didn't satisfy the search.
331 * if we did an initial full query, return that query's H_ERRNO
332 * (note that we wouldn't be here if that query had succeeded).
333 * else if we ever got a nodata, send that back as the reason.
334 * else send back meaningless H_ERRNO, that being the one from
335 * the last DNSRCH we did.
336 */
337 if (saved_herrno != -1)
338 RES_SET_H_ERRNO(statp, saved_herrno);
339 else if (got_nodata)
340 RES_SET_H_ERRNO(statp, NO_DATA);
341 else if (got_servfail)
342 RES_SET_H_ERRNO(statp, TRY_AGAIN);
343 return (-1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900344}
345
346/*
347 * Perform a call on res_query on the concatenation of name and domain,
348 * removing a trailing dot from name if domain is NULL.
349 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900350int res_nquerydomain(res_state statp, const char* name, const char* domain, int class,
351 int type, /* class and type of query */
352 u_char* answer, /* buffer to put answer */
353 int anslen) /* size of answer */
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)
361 printf(";; res_nquerydomain(%s, %s, %d, %d)\n", name, domain ? domain : "<Nil>", class,
362 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) {
371 RES_SET_H_ERRNO(statp, NO_RECOVERY);
372 return (-1);
373 }
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) {
384 RES_SET_H_ERRNO(statp, NO_RECOVERY);
385 return (-1);
386 }
387 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
388 }
389 return (res_nquery(statp, longname, class, type, answer, anslen));
Bernie Innocenti55864192018-08-30 04:05:20 +0900390}
391
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900392const char* res_hostalias(const res_state statp, const char* name, char* dst, size_t siz) {
393 return (NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900394}