161023Skarels /*-
2*61151Sbostic * Copyright (c) 1988, 1993
3*61151Sbostic * The Regents of the University of California. All rights reserved.
433736Sbostic *
542627Sbostic * %sccs.include.redist.c%
661023Skarels * -
761023Skarels * Portions Copyright (c) 1993 by Digital Equipment Corporation.
861023Skarels *
961023Skarels * Permission to use, copy, modify, and distribute this software for any
1061023Skarels * purpose with or without fee is hereby granted, provided that the above
1161023Skarels * copyright notice and this permission notice appear in all copies, and that
1261023Skarels * the name of Digital Equipment Corporation not be used in advertising or
1361023Skarels * publicity pertaining to distribution of the document or software without
1461023Skarels * specific, written prior permission.
1561023Skarels *
1661023Skarels * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
1761023Skarels * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
1861023Skarels * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
1961023Skarels * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
2061023Skarels * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
2161023Skarels * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
2261023Skarels * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
2361023Skarels * SOFTWARE.
2461023Skarels * -
2561023Skarels * --Copyright--
2633736Sbostic */
2733736Sbostic
2833736Sbostic #if defined(LIBC_SCCS) && !defined(lint)
29*61151Sbostic static char sccsid[] = "@(#)res_query.c 8.1 (Berkeley) 06/04/93";
3061023Skarels static char rcsid[] = "$Id: res_query.c,v 1.1 1993/06/01 09:42:14 vixie Exp vixie $";
3133736Sbostic #endif /* LIBC_SCCS and not lint */
3233736Sbostic
3333736Sbostic #include <sys/param.h>
3433736Sbostic #include <netinet/in.h>
3546604Sbostic #include <arpa/inet.h>
3646604Sbostic #include <arpa/nameser.h>
3733736Sbostic #include <netdb.h>
3846604Sbostic #include <resolv.h>
3933736Sbostic #include <stdio.h>
4046606Sbostic #include <ctype.h>
4133736Sbostic #include <errno.h>
4246604Sbostic #include <stdlib.h>
4342026Sbostic #include <string.h>
4433736Sbostic
4533736Sbostic #if PACKETSZ > 1024
4633736Sbostic #define MAXPACKET PACKETSZ
4733736Sbostic #else
4833736Sbostic #define MAXPACKET 1024
4933736Sbostic #endif
5033736Sbostic
5133736Sbostic int h_errno;
5233736Sbostic
5333736Sbostic /*
5433736Sbostic * Formulate a normal query, send, and await answer.
5533736Sbostic * Returned answer is placed in supplied buffer "answer".
5633736Sbostic * Perform preliminary check of answer, returning success only
5733736Sbostic * if no error is indicated and the answer count is nonzero.
5833736Sbostic * Return the size of the response on success, -1 on error.
5933736Sbostic * Error number is left in h_errno.
6033826Skarels * Caller must parse answer and determine whether it answers the question.
6133736Sbostic */
res_query(name,class,type,answer,anslen)6233736Sbostic res_query(name, class, type, answer, anslen)
6333736Sbostic char *name; /* domain name */
6433736Sbostic int class, type; /* class and type of query */
6533736Sbostic u_char *answer; /* buffer to put answer */
6633736Sbostic int anslen; /* size of answer buffer */
6733736Sbostic {
6833736Sbostic char buf[MAXPACKET];
6933736Sbostic HEADER *hp;
7033736Sbostic int n;
7133736Sbostic
7233736Sbostic if ((_res.options & RES_INIT) == 0 && res_init() == -1)
7333736Sbostic return (-1);
7433736Sbostic #ifdef DEBUG
7533736Sbostic if (_res.options & RES_DEBUG)
7661023Skarels printf(";; res_query(%s, %d, %d)\n", name, class, type);
7733736Sbostic #endif
7833736Sbostic n = res_mkquery(QUERY, name, class, type, (char *)NULL, 0, NULL,
7933736Sbostic buf, sizeof(buf));
8033736Sbostic
8133736Sbostic if (n <= 0) {
8233736Sbostic #ifdef DEBUG
8333736Sbostic if (_res.options & RES_DEBUG)
8461023Skarels printf(";; res_query: mkquery failed\n");
8533736Sbostic #endif
8633736Sbostic h_errno = NO_RECOVERY;
8733736Sbostic return (n);
8833736Sbostic }
8946604Sbostic n = res_send(buf, n, (char *)answer, anslen);
9033736Sbostic if (n < 0) {
9133736Sbostic #ifdef DEBUG
9233736Sbostic if (_res.options & RES_DEBUG)
9361023Skarels printf(";; res_query: send error\n");
9433736Sbostic #endif
9533736Sbostic h_errno = TRY_AGAIN;
9633736Sbostic return(n);
9733736Sbostic }
9833736Sbostic
9933736Sbostic hp = (HEADER *) answer;
10033736Sbostic if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
10133736Sbostic #ifdef DEBUG
10233736Sbostic if (_res.options & RES_DEBUG)
10361023Skarels printf(";; rcode = %d, ancount=%d\n", hp->rcode,
10433736Sbostic ntohs(hp->ancount));
10533736Sbostic #endif
10633736Sbostic switch (hp->rcode) {
10733736Sbostic case NXDOMAIN:
10833826Skarels h_errno = HOST_NOT_FOUND;
10933736Sbostic break;
11033736Sbostic case SERVFAIL:
11133736Sbostic h_errno = TRY_AGAIN;
11233736Sbostic break;
11333736Sbostic case NOERROR:
11433977Skarels h_errno = NO_DATA;
11533736Sbostic break;
11633736Sbostic case FORMERR:
11733736Sbostic case NOTIMP:
11833736Sbostic case REFUSED:
11933826Skarels default:
12033736Sbostic h_errno = NO_RECOVERY;
12133826Skarels break;
12233736Sbostic }
12333736Sbostic return (-1);
12433736Sbostic }
12533736Sbostic return(n);
12633736Sbostic }
12733736Sbostic
12833736Sbostic /*
12933736Sbostic * Formulate a normal query, send, and retrieve answer in supplied buffer.
13033736Sbostic * Return the size of the response on success, -1 on error.
13133736Sbostic * If enabled, implement search rules until answer or unrecoverable failure
13233736Sbostic * is detected. Error number is left in h_errno.
13333736Sbostic * Only useful for queries in the same name hierarchy as the local host
13433736Sbostic * (not, for example, for host address-to-name lookups in domain in-addr.arpa).
13533736Sbostic */
13661023Skarels int
res_search(name,class,type,answer,anslen)13733736Sbostic res_search(name, class, type, answer, anslen)
13833736Sbostic char *name; /* domain name */
13933736Sbostic int class, type; /* class and type of query */
14033736Sbostic u_char *answer; /* buffer to put answer */
14133736Sbostic int anslen; /* size of answer */
14233736Sbostic {
14333736Sbostic register char *cp, **domain;
14433977Skarels int n, ret, got_nodata = 0;
14547045Sbostic char *__hostalias();
14633736Sbostic
14733736Sbostic if ((_res.options & RES_INIT) == 0 && res_init() == -1)
14833736Sbostic return (-1);
14933736Sbostic
15033736Sbostic errno = 0;
15161023Skarels h_errno = HOST_NOT_FOUND; /* default, if we never query */
15233736Sbostic for (cp = name, n = 0; *cp; cp++)
15333736Sbostic if (*cp == '.')
15433736Sbostic n++;
15547045Sbostic if (n == 0 && (cp = __hostalias(name)))
15633736Sbostic return (res_query(cp, class, type, answer, anslen));
15733736Sbostic
15835655Skarels /*
15935655Skarels * We do at least one level of search if
16035655Skarels * - there is no dot and RES_DEFNAME is set, or
16135655Skarels * - there is at least one dot, there is no trailing dot,
16235655Skarels * and RES_DNSRCH is set.
16335655Skarels */
16435655Skarels if ((n == 0 && _res.options & RES_DEFNAMES) ||
16535655Skarels (n != 0 && *--cp != '.' && _res.options & RES_DNSRCH))
16635655Skarels for (domain = _res.dnsrch; *domain; domain++) {
16733736Sbostic ret = res_querydomain(name, *domain, class, type,
16833736Sbostic answer, anslen);
16933736Sbostic if (ret > 0)
17033736Sbostic return (ret);
17133736Sbostic /*
17233736Sbostic * If no server present, give up.
17333736Sbostic * If name isn't found in this domain,
17433736Sbostic * keep trying higher domains in the search list
17533736Sbostic * (if that's enabled).
17633977Skarels * On a NO_DATA error, keep trying, otherwise
17733736Sbostic * a wildcard entry of another type could keep us
17833736Sbostic * from finding this entry higher in the domain.
17935655Skarels * If we get some other error (negative answer or
18035655Skarels * server failure), then stop searching up,
18133736Sbostic * but try the input name below in case it's fully-qualified.
18233736Sbostic */
18333736Sbostic if (errno == ECONNREFUSED) {
18433736Sbostic h_errno = TRY_AGAIN;
18533736Sbostic return (-1);
18633736Sbostic }
18733977Skarels if (h_errno == NO_DATA)
18833977Skarels got_nodata++;
18933977Skarels if ((h_errno != HOST_NOT_FOUND && h_errno != NO_DATA) ||
19033736Sbostic (_res.options & RES_DNSRCH) == 0)
19133736Sbostic break;
19233736Sbostic }
19333736Sbostic /*
19433736Sbostic * If the search/default failed, try the name as fully-qualified,
19533736Sbostic * but only if it contained at least one dot (even trailing).
19635655Skarels * This is purely a heuristic; we assume that any reasonable query
19735655Skarels * about a top-level domain (for servers, SOA, etc) will not use
19835655Skarels * res_search.
19933736Sbostic */
20035655Skarels if (n && (ret = res_querydomain(name, (char *)NULL, class, type,
20135655Skarels answer, anslen)) > 0)
20235655Skarels return (ret);
20333977Skarels if (got_nodata)
20433977Skarels h_errno = NO_DATA;
20533736Sbostic return (-1);
20633736Sbostic }
20733736Sbostic
20833736Sbostic /*
20933736Sbostic * Perform a call on res_query on the concatenation of name and domain,
21033736Sbostic * removing a trailing dot from name if domain is NULL.
21133736Sbostic */
res_querydomain(name,domain,class,type,answer,anslen)21233736Sbostic res_querydomain(name, domain, class, type, answer, anslen)
21333736Sbostic char *name, *domain;
21433736Sbostic int class, type; /* class and type of query */
21533736Sbostic u_char *answer; /* buffer to put answer */
21633736Sbostic int anslen; /* size of answer */
21733736Sbostic {
21833736Sbostic char nbuf[2*MAXDNAME+2];
21933736Sbostic char *longname = nbuf;
22033736Sbostic int n;
22133736Sbostic
22233977Skarels #ifdef DEBUG
22333977Skarels if (_res.options & RES_DEBUG)
22461023Skarels printf(";; res_querydomain(%s, %s, %d, %d)\n",
22533977Skarels name, domain, class, type);
22633977Skarels #endif
22733736Sbostic if (domain == NULL) {
22833736Sbostic /*
22933736Sbostic * Check for trailing '.';
23033736Sbostic * copy without '.' if present.
23133736Sbostic */
23233736Sbostic n = strlen(name) - 1;
23333736Sbostic if (name[n] == '.' && n < sizeof(nbuf) - 1) {
23433736Sbostic bcopy(name, nbuf, n);
23533736Sbostic nbuf[n] = '\0';
23633736Sbostic } else
23733736Sbostic longname = name;
23833736Sbostic } else
23933736Sbostic (void)sprintf(nbuf, "%.*s.%.*s",
24033736Sbostic MAXDNAME, name, MAXDNAME, domain);
24133736Sbostic
24233736Sbostic return (res_query(longname, class, type, answer, anslen));
24333736Sbostic }
24433736Sbostic
24547045Sbostic char *
__hostalias(name)24647045Sbostic __hostalias(name)
24747045Sbostic register const char *name;
24833736Sbostic {
24933736Sbostic register char *C1, *C2;
25033736Sbostic FILE *fp;
25133736Sbostic char *file, *getenv(), *strcpy(), *strncpy();
25233736Sbostic char buf[BUFSIZ];
25333736Sbostic static char abuf[MAXDNAME];
25433736Sbostic
25533736Sbostic file = getenv("HOSTALIASES");
25633736Sbostic if (file == NULL || (fp = fopen(file, "r")) == NULL)
25733736Sbostic return (NULL);
25833736Sbostic buf[sizeof(buf) - 1] = '\0';
25933736Sbostic while (fgets(buf, sizeof(buf), fp)) {
26033736Sbostic for (C1 = buf; *C1 && !isspace(*C1); ++C1);
26133736Sbostic if (!*C1)
26233736Sbostic break;
26333736Sbostic *C1 = '\0';
26433736Sbostic if (!strcasecmp(buf, name)) {
26533736Sbostic while (isspace(*++C1));
26633736Sbostic if (!*C1)
26733736Sbostic break;
26833736Sbostic for (C2 = C1 + 1; *C2 && !isspace(*C2); ++C2);
26933736Sbostic abuf[sizeof(abuf) - 1] = *C2 = '\0';
27033736Sbostic (void)strncpy(abuf, C1, sizeof(abuf) - 1);
27133736Sbostic fclose(fp);
27233736Sbostic return (abuf);
27333736Sbostic }
27433736Sbostic }
27533736Sbostic fclose(fp);
27633736Sbostic return (NULL);
27733736Sbostic }
278