xref: /csrg-svn/lib/libc/net/res_query.c (revision 33826)
133736Sbostic /*
233736Sbostic  * Copyright (c) 1988 Regents of the University of California.
333736Sbostic  * All rights reserved.
433736Sbostic  *
533736Sbostic  * Redistribution and use in source and binary forms are permitted
633736Sbostic  * provided that this notice is preserved and that due credit is given
733736Sbostic  * to the University of California at Berkeley. The name of the University
833736Sbostic  * may not be used to endorse or promote products derived from this
933736Sbostic  * software without specific prior written permission. This software
1033736Sbostic  * is provided ``as is'' without express or implied warranty.
1133736Sbostic  */
1233736Sbostic 
1333736Sbostic #if defined(LIBC_SCCS) && !defined(lint)
14*33826Skarels static char sccsid[] = "@(#)res_query.c	5.2 (Berkeley) 03/29/88";
1533736Sbostic #endif /* LIBC_SCCS and not lint */
1633736Sbostic 
1733736Sbostic #include <sys/param.h>
1833736Sbostic #include <sys/socket.h>
1933736Sbostic #include <netinet/in.h>
2033736Sbostic #include <ctype.h>
2133736Sbostic #include <netdb.h>
2233736Sbostic #include <stdio.h>
2333736Sbostic #include <errno.h>
2433736Sbostic #include <strings.h>
2533736Sbostic #include <arpa/inet.h>
2633736Sbostic #include <arpa/nameser.h>
2733736Sbostic #include <resolv.h>
2833736Sbostic 
2933736Sbostic #if PACKETSZ > 1024
3033736Sbostic #define MAXPACKET	PACKETSZ
3133736Sbostic #else
3233736Sbostic #define MAXPACKET	1024
3333736Sbostic #endif
3433736Sbostic 
3533736Sbostic extern int errno;
3633736Sbostic int h_errno;
3733736Sbostic 
3833736Sbostic /*
3933736Sbostic  * Formulate a normal query, send, and await answer.
4033736Sbostic  * Returned answer is placed in supplied buffer "answer".
4133736Sbostic  * Perform preliminary check of answer, returning success only
4233736Sbostic  * if no error is indicated and the answer count is nonzero.
4333736Sbostic  * Return the size of the response on success, -1 on error.
4433736Sbostic  * Error number is left in h_errno.
45*33826Skarels  * Caller must parse answer and determine whether it answers the question.
4633736Sbostic  */
4733736Sbostic res_query(name, class, type, answer, anslen)
4833736Sbostic 	char *name;		/* domain name */
4933736Sbostic 	int class, type;	/* class and type of query */
5033736Sbostic 	u_char *answer;		/* buffer to put answer */
5133736Sbostic 	int anslen;		/* size of answer buffer */
5233736Sbostic {
5333736Sbostic 	char buf[MAXPACKET];
5433736Sbostic 	HEADER *hp;
5533736Sbostic 	int n;
5633736Sbostic 
5733736Sbostic 	if ((_res.options & RES_INIT) == 0 && res_init() == -1)
5833736Sbostic 		return (-1);
5933736Sbostic #ifdef DEBUG
6033736Sbostic 	if (_res.options & RES_DEBUG)
6133736Sbostic 		printf("res_query(%s, %d, %d)\n", name, class, type);
6233736Sbostic #endif
6333736Sbostic 	n = res_mkquery(QUERY, name, class, type, (char *)NULL, 0, NULL,
6433736Sbostic 	    buf, sizeof(buf));
6533736Sbostic 
6633736Sbostic 	if (n <= 0) {
6733736Sbostic #ifdef DEBUG
6833736Sbostic 		if (_res.options & RES_DEBUG)
6933736Sbostic 			printf("res_query: mkquery failed\n");
7033736Sbostic #endif
7133736Sbostic 		h_errno = NO_RECOVERY;
7233736Sbostic 		return (n);
7333736Sbostic 	}
7433736Sbostic 	n = res_send(buf, n, answer, anslen);
7533736Sbostic 	if (n < 0) {
7633736Sbostic #ifdef DEBUG
7733736Sbostic 		if (_res.options & RES_DEBUG)
7833736Sbostic 			printf("res_query: send error\n");
7933736Sbostic #endif
8033736Sbostic 		h_errno = TRY_AGAIN;
8133736Sbostic 		return(n);
8233736Sbostic 	}
8333736Sbostic 
8433736Sbostic 	hp = (HEADER *) answer;
8533736Sbostic 	if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
8633736Sbostic #ifdef DEBUG
8733736Sbostic 		if (_res.options & RES_DEBUG)
8833736Sbostic 			printf("rcode = %d, ancount=%d\n", hp->rcode,
8933736Sbostic 			    ntohs(hp->ancount));
9033736Sbostic #endif
9133736Sbostic 		switch (hp->rcode) {
9233736Sbostic 			case NXDOMAIN:
93*33826Skarels 				h_errno = HOST_NOT_FOUND;
9433736Sbostic 				break;
9533736Sbostic 			case SERVFAIL:
9633736Sbostic 				h_errno = TRY_AGAIN;
9733736Sbostic 				break;
9833736Sbostic 			case NOERROR:
99*33826Skarels 				h_errno = NO_ADDRESS;
10033736Sbostic 				break;
10133736Sbostic 			case FORMERR:
10233736Sbostic 			case NOTIMP:
10333736Sbostic 			case REFUSED:
104*33826Skarels 			default:
10533736Sbostic 				h_errno = NO_RECOVERY;
106*33826Skarels 				break;
10733736Sbostic 		}
10833736Sbostic 		return (-1);
10933736Sbostic 	}
11033736Sbostic 	return(n);
11133736Sbostic }
11233736Sbostic 
11333736Sbostic /*
11433736Sbostic  * Formulate a normal query, send, and retrieve answer in supplied buffer.
11533736Sbostic  * Return the size of the response on success, -1 on error.
11633736Sbostic  * If enabled, implement search rules until answer or unrecoverable failure
11733736Sbostic  * is detected.  Error number is left in h_errno.
11833736Sbostic  * Only useful for queries in the same name hierarchy as the local host
11933736Sbostic  * (not, for example, for host address-to-name lookups in domain in-addr.arpa).
12033736Sbostic  */
12133736Sbostic res_search(name, class, type, answer, anslen)
12233736Sbostic 	char *name;		/* domain name */
12333736Sbostic 	int class, type;	/* class and type of query */
12433736Sbostic 	u_char *answer;		/* buffer to put answer */
12533736Sbostic 	int anslen;		/* size of answer */
12633736Sbostic {
12733736Sbostic 	register char *cp, **domain;
12833736Sbostic 	int n, ret;
12933736Sbostic 	char *hostalias();
13033736Sbostic 
13133736Sbostic 	if ((_res.options & RES_INIT) == 0 && res_init() == -1)
13233736Sbostic 		return (-1);
13333736Sbostic 
13433736Sbostic 	errno = 0;
13533736Sbostic 	for (cp = name, n = 0; *cp; cp++)
13633736Sbostic 		if (*cp == '.')
13733736Sbostic 			n++;
13833736Sbostic 	if (n == 0 && (cp = hostalias(name)))
13933736Sbostic 		return (res_query(cp, class, type, answer, anslen));
14033736Sbostic 
14133736Sbostic 	if ((n == 0 || *--cp != '.') && (_res.options & RES_DEFNAMES))
14233736Sbostic 	    for (domain = _res.dnsrch; *domain; domain++) {
14333736Sbostic 		ret = res_querydomain(name, *domain, class, type,
14433736Sbostic 		    answer, anslen);
14533736Sbostic 		if (ret > 0)
14633736Sbostic 			return (ret);
14733736Sbostic 		/*
14833736Sbostic 		 * If no server present, give up.
14933736Sbostic 		 * If name isn't found in this domain,
15033736Sbostic 		 * keep trying higher domains in the search list
15133736Sbostic 		 * (if that's enabled).
15233736Sbostic 		 * On a NO_ADDRESS error, keep trying, otherwise
15333736Sbostic 		 * a wildcard entry of another type could keep us
15433736Sbostic 		 * from finding this entry higher in the domain.
15533736Sbostic 		 * If we get some other error (non-authoritative negative
15633736Sbostic 		 * answer or server failure), then stop searching up,
15733736Sbostic 		 * but try the input name below in case it's fully-qualified.
15833736Sbostic 		 */
15933736Sbostic 		if (errno == ECONNREFUSED) {
16033736Sbostic 			h_errno = TRY_AGAIN;
16133736Sbostic 			return (-1);
16233736Sbostic 		}
16333736Sbostic 		if ((h_errno != HOST_NOT_FOUND && h_errno != NO_ADDRESS) ||
16433736Sbostic 		    (_res.options & RES_DNSRCH) == 0)
16533736Sbostic 			break;
16633736Sbostic 		h_errno = 0;
16733736Sbostic 	}
16833736Sbostic 	/*
16933736Sbostic 	 * If the search/default failed, try the name as fully-qualified,
17033736Sbostic 	 * but only if it contained at least one dot (even trailing).
17133736Sbostic 	 */
17233736Sbostic 	if (n)
17333736Sbostic 		return (res_querydomain(name, (char *)NULL, class, type,
17433736Sbostic 		    answer, anslen));
17533736Sbostic 	h_errno = HOST_NOT_FOUND;
17633736Sbostic 	return (-1);
17733736Sbostic }
17833736Sbostic 
17933736Sbostic /*
18033736Sbostic  * Perform a call on res_query on the concatenation of name and domain,
18133736Sbostic  * removing a trailing dot from name if domain is NULL.
18233736Sbostic  */
18333736Sbostic res_querydomain(name, domain, class, type, answer, anslen)
18433736Sbostic 	char *name, *domain;
18533736Sbostic 	int class, type;	/* class and type of query */
18633736Sbostic 	u_char *answer;		/* buffer to put answer */
18733736Sbostic 	int anslen;		/* size of answer */
18833736Sbostic {
18933736Sbostic 	char nbuf[2*MAXDNAME+2];
19033736Sbostic 	char *longname = nbuf;
19133736Sbostic 	int n;
19233736Sbostic 
19333736Sbostic 	if (domain == NULL) {
19433736Sbostic 		/*
19533736Sbostic 		 * Check for trailing '.';
19633736Sbostic 		 * copy without '.' if present.
19733736Sbostic 		 */
19833736Sbostic 		n = strlen(name) - 1;
19933736Sbostic 		if (name[n] == '.' && n < sizeof(nbuf) - 1) {
20033736Sbostic 			bcopy(name, nbuf, n);
20133736Sbostic 			nbuf[n] = '\0';
20233736Sbostic 		} else
20333736Sbostic 			longname = name;
20433736Sbostic 	} else
20533736Sbostic 		(void)sprintf(nbuf, "%.*s.%.*s",
20633736Sbostic 		    MAXDNAME, name, MAXDNAME, domain);
20733736Sbostic 
20833736Sbostic 	return (res_query(longname, class, type, answer, anslen));
20933736Sbostic }
21033736Sbostic 
21133736Sbostic char *
21233736Sbostic hostalias(name)
21333736Sbostic 	register char *name;
21433736Sbostic {
21533736Sbostic 	register char *C1, *C2;
21633736Sbostic 	FILE *fp;
21733736Sbostic 	char *file, *getenv(), *strcpy(), *strncpy();
21833736Sbostic 	char buf[BUFSIZ];
21933736Sbostic 	static char abuf[MAXDNAME];
22033736Sbostic 
22133736Sbostic 	file = getenv("HOSTALIASES");
22233736Sbostic 	if (file == NULL || (fp = fopen(file, "r")) == NULL)
22333736Sbostic 		return (NULL);
22433736Sbostic 	buf[sizeof(buf) - 1] = '\0';
22533736Sbostic 	while (fgets(buf, sizeof(buf), fp)) {
22633736Sbostic 		for (C1 = buf; *C1 && !isspace(*C1); ++C1);
22733736Sbostic 		if (!*C1)
22833736Sbostic 			break;
22933736Sbostic 		*C1 = '\0';
23033736Sbostic 		if (!strcasecmp(buf, name)) {
23133736Sbostic 			while (isspace(*++C1));
23233736Sbostic 			if (!*C1)
23333736Sbostic 				break;
23433736Sbostic 			for (C2 = C1 + 1; *C2 && !isspace(*C2); ++C2);
23533736Sbostic 			abuf[sizeof(abuf) - 1] = *C2 = '\0';
23633736Sbostic 			(void)strncpy(abuf, C1, sizeof(abuf) - 1);
23733736Sbostic 			fclose(fp);
23833736Sbostic 			return (abuf);
23933736Sbostic 		}
24033736Sbostic 	}
24133736Sbostic 	fclose(fp);
24233736Sbostic 	return (NULL);
24333736Sbostic }
244