xref: /csrg-svn/lib/libc/net/res_query.c (revision 46604)
1 /*
2  * Copyright (c) 1988 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)res_query.c	5.9 (Berkeley) 02/24/91";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include <sys/param.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
15 #include <arpa/nameser.h>
16 #include <netdb.h>
17 #include <resolv.h>
18 #include <stdio.h>
19 #include <errno.h>
20 #include <stdlib.h>
21 #include <string.h>
22 
23 #if PACKETSZ > 1024
24 #define MAXPACKET	PACKETSZ
25 #else
26 #define MAXPACKET	1024
27 #endif
28 
29 int h_errno;
30 
31 /*
32  * Formulate a normal query, send, and await answer.
33  * Returned answer is placed in supplied buffer "answer".
34  * Perform preliminary check of answer, returning success only
35  * if no error is indicated and the answer count is nonzero.
36  * Return the size of the response on success, -1 on error.
37  * Error number is left in h_errno.
38  * Caller must parse answer and determine whether it answers the question.
39  */
40 res_query(name, class, type, answer, anslen)
41 	char *name;		/* domain name */
42 	int class, type;	/* class and type of query */
43 	u_char *answer;		/* buffer to put answer */
44 	int anslen;		/* size of answer buffer */
45 {
46 	char buf[MAXPACKET];
47 	HEADER *hp;
48 	int n;
49 
50 	if ((_res.options & RES_INIT) == 0 && res_init() == -1)
51 		return (-1);
52 #ifdef DEBUG
53 	if (_res.options & RES_DEBUG)
54 		printf("res_query(%s, %d, %d)\n", name, class, type);
55 #endif
56 	n = res_mkquery(QUERY, name, class, type, (char *)NULL, 0, NULL,
57 	    buf, sizeof(buf));
58 
59 	if (n <= 0) {
60 #ifdef DEBUG
61 		if (_res.options & RES_DEBUG)
62 			printf("res_query: mkquery failed\n");
63 #endif
64 		h_errno = NO_RECOVERY;
65 		return (n);
66 	}
67 	n = res_send(buf, n, (char *)answer, anslen);
68 	if (n < 0) {
69 #ifdef DEBUG
70 		if (_res.options & RES_DEBUG)
71 			printf("res_query: send error\n");
72 #endif
73 		h_errno = TRY_AGAIN;
74 		return(n);
75 	}
76 
77 	hp = (HEADER *) answer;
78 	if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
79 #ifdef DEBUG
80 		if (_res.options & RES_DEBUG)
81 			printf("rcode = %d, ancount=%d\n", hp->rcode,
82 			    ntohs(hp->ancount));
83 #endif
84 		switch (hp->rcode) {
85 			case NXDOMAIN:
86 				h_errno = HOST_NOT_FOUND;
87 				break;
88 			case SERVFAIL:
89 				h_errno = TRY_AGAIN;
90 				break;
91 			case NOERROR:
92 				h_errno = NO_DATA;
93 				break;
94 			case FORMERR:
95 			case NOTIMP:
96 			case REFUSED:
97 			default:
98 				h_errno = NO_RECOVERY;
99 				break;
100 		}
101 		return (-1);
102 	}
103 	return(n);
104 }
105 
106 /*
107  * Formulate a normal query, send, and retrieve answer in supplied buffer.
108  * Return the size of the response on success, -1 on error.
109  * If enabled, implement search rules until answer or unrecoverable failure
110  * is detected.  Error number is left in h_errno.
111  * Only useful for queries in the same name hierarchy as the local host
112  * (not, for example, for host address-to-name lookups in domain in-addr.arpa).
113  */
114 res_search(name, class, type, answer, anslen)
115 	char *name;		/* domain name */
116 	int class, type;	/* class and type of query */
117 	u_char *answer;		/* buffer to put answer */
118 	int anslen;		/* size of answer */
119 {
120 	register char *cp, **domain;
121 	int n, ret, got_nodata = 0;
122 	static char *hostalias();
123 
124 	if ((_res.options & RES_INIT) == 0 && res_init() == -1)
125 		return (-1);
126 
127 	errno = 0;
128 	h_errno = HOST_NOT_FOUND;		/* default, if we never query */
129 	for (cp = name, n = 0; *cp; cp++)
130 		if (*cp == '.')
131 			n++;
132 	if (n == 0 && (cp = hostalias(name)))
133 		return (res_query(cp, class, type, answer, anslen));
134 
135 	/*
136 	 * We do at least one level of search if
137 	 *	- there is no dot and RES_DEFNAME is set, or
138 	 *	- there is at least one dot, there is no trailing dot,
139 	 *	  and RES_DNSRCH is set.
140 	 */
141 	if ((n == 0 && _res.options & RES_DEFNAMES) ||
142 	   (n != 0 && *--cp != '.' && _res.options & RES_DNSRCH))
143 	     for (domain = _res.dnsrch; *domain; domain++) {
144 		ret = res_querydomain(name, *domain, class, type,
145 		    answer, anslen);
146 		if (ret > 0)
147 			return (ret);
148 		/*
149 		 * If no server present, give up.
150 		 * If name isn't found in this domain,
151 		 * keep trying higher domains in the search list
152 		 * (if that's enabled).
153 		 * On a NO_DATA error, keep trying, otherwise
154 		 * a wildcard entry of another type could keep us
155 		 * from finding this entry higher in the domain.
156 		 * If we get some other error (negative answer or
157 		 * server failure), then stop searching up,
158 		 * but try the input name below in case it's fully-qualified.
159 		 */
160 		if (errno == ECONNREFUSED) {
161 			h_errno = TRY_AGAIN;
162 			return (-1);
163 		}
164 		if (h_errno == NO_DATA)
165 			got_nodata++;
166 		if ((h_errno != HOST_NOT_FOUND && h_errno != NO_DATA) ||
167 		    (_res.options & RES_DNSRCH) == 0)
168 			break;
169 	}
170 	/*
171 	 * If the search/default failed, try the name as fully-qualified,
172 	 * but only if it contained at least one dot (even trailing).
173 	 * This is purely a heuristic; we assume that any reasonable query
174 	 * about a top-level domain (for servers, SOA, etc) will not use
175 	 * res_search.
176 	 */
177 	if (n && (ret = res_querydomain(name, (char *)NULL, class, type,
178 	    answer, anslen)) > 0)
179 		return (ret);
180 	if (got_nodata)
181 		h_errno = NO_DATA;
182 	return (-1);
183 }
184 
185 /*
186  * Perform a call on res_query on the concatenation of name and domain,
187  * removing a trailing dot from name if domain is NULL.
188  */
189 res_querydomain(name, domain, class, type, answer, anslen)
190 	char *name, *domain;
191 	int class, type;	/* class and type of query */
192 	u_char *answer;		/* buffer to put answer */
193 	int anslen;		/* size of answer */
194 {
195 	char nbuf[2*MAXDNAME+2];
196 	char *longname = nbuf;
197 	int n;
198 
199 #ifdef DEBUG
200 	if (_res.options & RES_DEBUG)
201 		printf("res_querydomain(%s, %s, %d, %d)\n",
202 		    name, domain, class, type);
203 #endif
204 	if (domain == NULL) {
205 		/*
206 		 * Check for trailing '.';
207 		 * copy without '.' if present.
208 		 */
209 		n = strlen(name) - 1;
210 		if (name[n] == '.' && n < sizeof(nbuf) - 1) {
211 			bcopy(name, nbuf, n);
212 			nbuf[n] = '\0';
213 		} else
214 			longname = name;
215 	} else
216 		(void)sprintf(nbuf, "%.*s.%.*s",
217 		    MAXDNAME, name, MAXDNAME, domain);
218 
219 	return (res_query(longname, class, type, answer, anslen));
220 }
221 
222 static char *
223 hostalias(name)
224 	register char *name;
225 {
226 	register char *C1, *C2;
227 	FILE *fp;
228 	char *file, *getenv(), *strcpy(), *strncpy();
229 	char buf[BUFSIZ];
230 	static char abuf[MAXDNAME];
231 
232 	file = getenv("HOSTALIASES");
233 	if (file == NULL || (fp = fopen(file, "r")) == NULL)
234 		return (NULL);
235 	buf[sizeof(buf) - 1] = '\0';
236 	while (fgets(buf, sizeof(buf), fp)) {
237 		for (C1 = buf; *C1 && !isspace(*C1); ++C1);
238 		if (!*C1)
239 			break;
240 		*C1 = '\0';
241 		if (!strcasecmp(buf, name)) {
242 			while (isspace(*++C1));
243 			if (!*C1)
244 				break;
245 			for (C2 = C1 + 1; *C2 && !isspace(*C2); ++C2);
246 			abuf[sizeof(abuf) - 1] = *C2 = '\0';
247 			(void)strncpy(abuf, C1, sizeof(abuf) - 1);
248 			fclose(fp);
249 			return (abuf);
250 		}
251 	}
252 	fclose(fp);
253 	return (NULL);
254 }
255