xref: /csrg-svn/lib/libc/net/res_query.c (revision 33977)
1 /*
2  * Copyright (c) 1988 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #if defined(LIBC_SCCS) && !defined(lint)
14 static char sccsid[] = "@(#)res_query.c	5.3 (Berkeley) 04/05/88";
15 #endif /* LIBC_SCCS and not lint */
16 
17 #include <sys/param.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <ctype.h>
21 #include <netdb.h>
22 #include <stdio.h>
23 #include <errno.h>
24 #include <strings.h>
25 #include <arpa/inet.h>
26 #include <arpa/nameser.h>
27 #include <resolv.h>
28 
29 #if PACKETSZ > 1024
30 #define MAXPACKET	PACKETSZ
31 #else
32 #define MAXPACKET	1024
33 #endif
34 
35 extern int errno;
36 int h_errno;
37 
38 /*
39  * Formulate a normal query, send, and await answer.
40  * Returned answer is placed in supplied buffer "answer".
41  * Perform preliminary check of answer, returning success only
42  * if no error is indicated and the answer count is nonzero.
43  * Return the size of the response on success, -1 on error.
44  * Error number is left in h_errno.
45  * Caller must parse answer and determine whether it answers the question.
46  */
47 res_query(name, class, type, answer, anslen)
48 	char *name;		/* domain name */
49 	int class, type;	/* class and type of query */
50 	u_char *answer;		/* buffer to put answer */
51 	int anslen;		/* size of answer buffer */
52 {
53 	char buf[MAXPACKET];
54 	HEADER *hp;
55 	int n;
56 
57 	if ((_res.options & RES_INIT) == 0 && res_init() == -1)
58 		return (-1);
59 #ifdef DEBUG
60 	if (_res.options & RES_DEBUG)
61 		printf("res_query(%s, %d, %d)\n", name, class, type);
62 #endif
63 	n = res_mkquery(QUERY, name, class, type, (char *)NULL, 0, NULL,
64 	    buf, sizeof(buf));
65 
66 	if (n <= 0) {
67 #ifdef DEBUG
68 		if (_res.options & RES_DEBUG)
69 			printf("res_query: mkquery failed\n");
70 #endif
71 		h_errno = NO_RECOVERY;
72 		return (n);
73 	}
74 	n = res_send(buf, n, answer, anslen);
75 	if (n < 0) {
76 #ifdef DEBUG
77 		if (_res.options & RES_DEBUG)
78 			printf("res_query: send error\n");
79 #endif
80 		h_errno = TRY_AGAIN;
81 		return(n);
82 	}
83 
84 	hp = (HEADER *) answer;
85 	if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
86 #ifdef DEBUG
87 		if (_res.options & RES_DEBUG)
88 			printf("rcode = %d, ancount=%d\n", hp->rcode,
89 			    ntohs(hp->ancount));
90 #endif
91 		switch (hp->rcode) {
92 			case NXDOMAIN:
93 				h_errno = HOST_NOT_FOUND;
94 				break;
95 			case SERVFAIL:
96 				h_errno = TRY_AGAIN;
97 				break;
98 			case NOERROR:
99 				h_errno = NO_DATA;
100 				break;
101 			case FORMERR:
102 			case NOTIMP:
103 			case REFUSED:
104 			default:
105 				h_errno = NO_RECOVERY;
106 				break;
107 		}
108 		return (-1);
109 	}
110 	return(n);
111 }
112 
113 /*
114  * Formulate a normal query, send, and retrieve answer in supplied buffer.
115  * Return the size of the response on success, -1 on error.
116  * If enabled, implement search rules until answer or unrecoverable failure
117  * is detected.  Error number is left in h_errno.
118  * Only useful for queries in the same name hierarchy as the local host
119  * (not, for example, for host address-to-name lookups in domain in-addr.arpa).
120  */
121 res_search(name, class, type, answer, anslen)
122 	char *name;		/* domain name */
123 	int class, type;	/* class and type of query */
124 	u_char *answer;		/* buffer to put answer */
125 	int anslen;		/* size of answer */
126 {
127 	register char *cp, **domain;
128 	int n, ret, got_nodata = 0;
129 	char *hostalias();
130 
131 	if ((_res.options & RES_INIT) == 0 && res_init() == -1)
132 		return (-1);
133 
134 	errno = 0;
135 	h_errno = HOST_NOT_FOUND;		/* default, if we never query */
136 	for (cp = name, n = 0; *cp; cp++)
137 		if (*cp == '.')
138 			n++;
139 	if (n == 0 && (cp = hostalias(name)))
140 		return (res_query(cp, class, type, answer, anslen));
141 
142 	if ((n == 0 || *--cp != '.') && (_res.options & RES_DEFNAMES))
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 (non-authoritative negative
157 		 * answer or 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 		h_errno = 0;
170 	}
171 	/*
172 	 * If the search/default failed, try the name as fully-qualified,
173 	 * but only if it contained at least one dot (even trailing).
174 	 */
175 	if (n)
176 		return (res_querydomain(name, (char *)NULL, class, type,
177 		    answer, anslen));
178 	if (got_nodata)
179 		h_errno = NO_DATA;
180 	return (-1);
181 }
182 
183 /*
184  * Perform a call on res_query on the concatenation of name and domain,
185  * removing a trailing dot from name if domain is NULL.
186  */
187 res_querydomain(name, domain, class, type, answer, anslen)
188 	char *name, *domain;
189 	int class, type;	/* class and type of query */
190 	u_char *answer;		/* buffer to put answer */
191 	int anslen;		/* size of answer */
192 {
193 	char nbuf[2*MAXDNAME+2];
194 	char *longname = nbuf;
195 	int n;
196 
197 #ifdef DEBUG
198 	if (_res.options & RES_DEBUG)
199 		printf("res_querydomain(%s, %s, %d, %d)\n",
200 		    name, domain, class, type);
201 #endif
202 	if (domain == NULL) {
203 		/*
204 		 * Check for trailing '.';
205 		 * copy without '.' if present.
206 		 */
207 		n = strlen(name) - 1;
208 		if (name[n] == '.' && n < sizeof(nbuf) - 1) {
209 			bcopy(name, nbuf, n);
210 			nbuf[n] = '\0';
211 		} else
212 			longname = name;
213 	} else
214 		(void)sprintf(nbuf, "%.*s.%.*s",
215 		    MAXDNAME, name, MAXDNAME, domain);
216 
217 	return (res_query(longname, class, type, answer, anslen));
218 }
219 
220 char *
221 hostalias(name)
222 	register char *name;
223 {
224 	register char *C1, *C2;
225 	FILE *fp;
226 	char *file, *getenv(), *strcpy(), *strncpy();
227 	char buf[BUFSIZ];
228 	static char abuf[MAXDNAME];
229 
230 	file = getenv("HOSTALIASES");
231 	if (file == NULL || (fp = fopen(file, "r")) == NULL)
232 		return (NULL);
233 	buf[sizeof(buf) - 1] = '\0';
234 	while (fgets(buf, sizeof(buf), fp)) {
235 		for (C1 = buf; *C1 && !isspace(*C1); ++C1);
236 		if (!*C1)
237 			break;
238 		*C1 = '\0';
239 		if (!strcasecmp(buf, name)) {
240 			while (isspace(*++C1));
241 			if (!*C1)
242 				break;
243 			for (C2 = C1 + 1; *C2 && !isspace(*C2); ++C2);
244 			abuf[sizeof(abuf) - 1] = *C2 = '\0';
245 			(void)strncpy(abuf, C1, sizeof(abuf) - 1);
246 			fclose(fp);
247 			return (abuf);
248 		}
249 	}
250 	fclose(fp);
251 	return (NULL);
252 }
253