xref: /csrg-svn/lib/libc/net/res_query.c (revision 33736)
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.1 (Berkeley) 03/14/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  */
46 res_query(name, class, type, answer, anslen)
47 	char *name;		/* domain name */
48 	int class, type;	/* class and type of query */
49 	u_char *answer;		/* buffer to put answer */
50 	int anslen;		/* size of answer buffer */
51 {
52 	char buf[MAXPACKET];
53 	HEADER *hp;
54 	int n;
55 
56 	if ((_res.options & RES_INIT) == 0 && res_init() == -1)
57 		return (-1);
58 #ifdef DEBUG
59 	if (_res.options & RES_DEBUG)
60 		printf("res_query(%s, %d, %d)\n", name, class, type);
61 #endif
62 	n = res_mkquery(QUERY, name, class, type, (char *)NULL, 0, NULL,
63 	    buf, sizeof(buf));
64 
65 	if (n <= 0) {
66 #ifdef DEBUG
67 		if (_res.options & RES_DEBUG)
68 			printf("res_query: mkquery failed\n");
69 #endif
70 		h_errno = NO_RECOVERY;
71 		return (n);
72 	}
73 	n = res_send(buf, n, answer, anslen);
74 	if (n < 0) {
75 #ifdef DEBUG
76 		if (_res.options & RES_DEBUG)
77 			printf("res_query: send error\n");
78 #endif
79 		h_errno = TRY_AGAIN;
80 		return(n);
81 	}
82 
83 	hp = (HEADER *) answer;
84 	if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
85 #ifdef DEBUG
86 		if (_res.options & RES_DEBUG)
87 			printf("rcode = %d, ancount=%d\n", hp->rcode,
88 			    ntohs(hp->ancount));
89 #endif
90 		switch (hp->rcode) {
91 			case NXDOMAIN:
92 				/* Check if it's an authoritive answer */
93 				if (hp->aa)
94 					h_errno = HOST_NOT_FOUND;
95 				else
96 					h_errno = TRY_AGAIN;
97 				break;
98 			case SERVFAIL:
99 				h_errno = TRY_AGAIN;
100 				break;
101 			case NOERROR:
102 				if (hp->aa)
103 					h_errno = NO_ADDRESS;
104 				else
105 					h_errno = TRY_AGAIN;
106 				break;
107 			case FORMERR:
108 			case NOTIMP:
109 			case REFUSED:
110 				h_errno = NO_RECOVERY;
111 		}
112 		return (-1);
113 	}
114 	return(n);
115 }
116 
117 /*
118  * Formulate a normal query, send, and retrieve answer in supplied buffer.
119  * Return the size of the response on success, -1 on error.
120  * If enabled, implement search rules until answer or unrecoverable failure
121  * is detected.  Error number is left in h_errno.
122  * Only useful for queries in the same name hierarchy as the local host
123  * (not, for example, for host address-to-name lookups in domain in-addr.arpa).
124  */
125 res_search(name, class, type, answer, anslen)
126 	char *name;		/* domain name */
127 	int class, type;	/* class and type of query */
128 	u_char *answer;		/* buffer to put answer */
129 	int anslen;		/* size of answer */
130 {
131 	register char *cp, **domain;
132 	int n, ret;
133 	char *hostalias();
134 
135 	if ((_res.options & RES_INIT) == 0 && res_init() == -1)
136 		return (-1);
137 
138 	errno = 0;
139 	for (cp = name, n = 0; *cp; cp++)
140 		if (*cp == '.')
141 			n++;
142 	if (n == 0 && (cp = hostalias(name)))
143 		return (res_query(cp, class, type, answer, anslen));
144 
145 	if ((n == 0 || *--cp != '.') && (_res.options & RES_DEFNAMES))
146 	    for (domain = _res.dnsrch; *domain; domain++) {
147 		ret = res_querydomain(name, *domain, class, type,
148 		    answer, anslen);
149 		if (ret > 0)
150 			return (ret);
151 		/*
152 		 * If no server present, give up.
153 		 * If name isn't found in this domain,
154 		 * keep trying higher domains in the search list
155 		 * (if that's enabled).
156 		 * On a NO_ADDRESS error, keep trying, otherwise
157 		 * a wildcard entry of another type could keep us
158 		 * from finding this entry higher in the domain.
159 		 * If we get some other error (non-authoritative negative
160 		 * answer or server failure), then stop searching up,
161 		 * but try the input name below in case it's fully-qualified.
162 		 */
163 		if (errno == ECONNREFUSED) {
164 			h_errno = TRY_AGAIN;
165 			return (-1);
166 		}
167 		if ((h_errno != HOST_NOT_FOUND && h_errno != NO_ADDRESS) ||
168 		    (_res.options & RES_DNSRCH) == 0)
169 			break;
170 		h_errno = 0;
171 	}
172 	/*
173 	 * If the search/default failed, try the name as fully-qualified,
174 	 * but only if it contained at least one dot (even trailing).
175 	 */
176 	if (n)
177 		return (res_querydomain(name, (char *)NULL, class, type,
178 		    answer, anslen));
179 	h_errno = HOST_NOT_FOUND;
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 	if (domain == NULL) {
198 		/*
199 		 * Check for trailing '.';
200 		 * copy without '.' if present.
201 		 */
202 		n = strlen(name) - 1;
203 		if (name[n] == '.' && n < sizeof(nbuf) - 1) {
204 			bcopy(name, nbuf, n);
205 			nbuf[n] = '\0';
206 		} else
207 			longname = name;
208 	} else
209 		(void)sprintf(nbuf, "%.*s.%.*s",
210 		    MAXDNAME, name, MAXDNAME, domain);
211 
212 	return (res_query(longname, class, type, answer, anslen));
213 }
214 
215 char *
216 hostalias(name)
217 	register char *name;
218 {
219 	register char *C1, *C2;
220 	FILE *fp;
221 	char *file, *getenv(), *strcpy(), *strncpy();
222 	char buf[BUFSIZ];
223 	static char abuf[MAXDNAME];
224 
225 	file = getenv("HOSTALIASES");
226 	if (file == NULL || (fp = fopen(file, "r")) == NULL)
227 		return (NULL);
228 	buf[sizeof(buf) - 1] = '\0';
229 	while (fgets(buf, sizeof(buf), fp)) {
230 		for (C1 = buf; *C1 && !isspace(*C1); ++C1);
231 		if (!*C1)
232 			break;
233 		*C1 = '\0';
234 		if (!strcasecmp(buf, name)) {
235 			while (isspace(*++C1));
236 			if (!*C1)
237 				break;
238 			for (C2 = C1 + 1; *C2 && !isspace(*C2); ++C2);
239 			abuf[sizeof(abuf) - 1] = *C2 = '\0';
240 			(void)strncpy(abuf, C1, sizeof(abuf) - 1);
241 			fclose(fp);
242 			return (abuf);
243 		}
244 	}
245 	fclose(fp);
246 	return (NULL);
247 }
248