2 * Copyright (c) 1988 Regents of the University of California.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that: (1) source distributions retain this entire copyright
7 * notice and comment, and (2) distributions including binaries display
8 * the following acknowledgement: ``This product includes software
9 * developed by the University of California, Berkeley and its contributors''
10 * in the documentation or other materials provided with the distribution
11 * and in all advertising materials mentioning features or use of this
12 * software. Neither the name of the University nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 #if defined(LIBC_SCCS) && !defined(lint)
21 static char sccsid
[] = "@(#)res_query.c 5.7 (Berkeley) 6/1/90";
22 #endif /* LIBC_SCCS and not lint */
25 #include <sys/types.h>
33 #include <net/gen/in.h>
34 #include <net/gen/nameser.h>
35 #include <net/gen/netdb.h>
36 #include <net/gen/resolv.h>
38 #define bcopy(s,d,l) memcpy(d,s,l)
40 #define hostalias __hostalias
42 #include <sys/param.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
50 #include <arpa/inet.h>
51 #include <arpa/nameser.h>
64 #define MAXPACKET PACKETSZ
66 #define MAXPACKET 1024
72 * Formulate a normal query, send, and await answer.
73 * Returned answer is placed in supplied buffer "answer".
74 * Perform preliminary check of answer, returning success only
75 * if no error is indicated and the answer count is nonzero.
76 * Return the size of the response on success, -1 on error.
77 * Error number is left in h_errno.
78 * Caller must parse answer and determine whether it answers the question.
81 res_query(name
, class, type
, answer
, anslen
)
82 char *name
; /* domain name */
83 int class, type
; /* class and type of query */
84 u_char
*answer
; /* buffer to put answer */
85 int anslen
; /* size of answer buffer */
91 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1)
94 if (_res
.options
& RES_DEBUG
)
95 printf("res_query(%s, %d, %d)\n", name
, class, type
);
97 n
= res_mkquery(QUERY
, name
, class, type
, (char *)NULL
, 0, NULL
,
102 if (_res
.options
& RES_DEBUG
)
103 printf("res_query: mkquery failed\n");
105 h_errno
= NO_RECOVERY
;
108 n
= res_send(buf
, n
, (char *)answer
, anslen
);
111 if (_res
.options
& RES_DEBUG
)
112 printf("res_query: send error(%d)\n", errno
);
118 hp
= (dns_hdr_t
*) answer
;
119 if ((hp
->dh_flag2
& DHF_RCODE
) != NOERROR
||
120 ntohs(hp
->dh_ancount
) == 0) {
122 if (_res
.options
& RES_DEBUG
)
123 printf("rcode = %d, ancount=%d\n",
124 hp
->dh_flag2
& DHF_RCODE
,
125 ntohs(hp
->dh_ancount
));
127 switch (hp
->dh_flag2
& DHF_RCODE
) {
129 h_errno
= HOST_NOT_FOUND
;
141 h_errno
= NO_RECOVERY
;
150 * Formulate a normal query, send, and retrieve answer in supplied buffer.
151 * Return the size of the response on success, -1 on error.
152 * If enabled, implement search rules until answer or unrecoverable failure
153 * is detected. Error number is left in h_errno.
154 * Only useful for queries in the same name hierarchy as the local host
155 * (not, for example, for host address-to-name lookups in domain in-addr.arpa).
157 res_search(name
, class, type
, answer
, anslen
)
158 char *name
; /* domain name */
159 int class, type
; /* class and type of query */
160 u_char
*answer
; /* buffer to put answer */
161 int anslen
; /* size of answer */
163 register char *cp
, **domain
;
164 int n
, ret
, got_nodata
= 0;
166 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1)
170 h_errno
= HOST_NOT_FOUND
; /* default, if we never query */
171 for (cp
= name
, n
= 0; *cp
; cp
++)
174 if (n
== 0 && (cp
= hostalias(name
)))
175 return (res_query(cp
, class, type
, answer
, anslen
));
178 * We do at least one level of search if
179 * - there is no dot and RES_DEFNAME is set, or
180 * - there is at least one dot, there is no trailing dot,
181 * and RES_DNSRCH is set.
183 if ((n
== 0 && _res
.options
& RES_DEFNAMES
) ||
184 (n
!= 0 && *--cp
!= '.' && _res
.options
& RES_DNSRCH
))
185 for (domain
= _res
.dnsrch
; *domain
; domain
++) {
186 ret
= res_querydomain(name
, *domain
, class, type
,
191 * If no server present, give up.
192 * If name isn't found in this domain,
193 * keep trying higher domains in the search list
194 * (if that's enabled).
195 * On a NO_DATA error, keep trying, otherwise
196 * a wildcard entry of another type could keep us
197 * from finding this entry higher in the domain.
198 * If we get some other error (negative answer or
199 * server failure), then stop searching up,
200 * but try the input name below in case it's fully-qualified.
202 if (errno
== ECONNREFUSED
) {
206 if (h_errno
== NO_DATA
)
208 if ((h_errno
!= HOST_NOT_FOUND
&& h_errno
!= NO_DATA
) ||
209 (_res
.options
& RES_DNSRCH
) == 0)
213 * If the search/default failed, try the name as fully-qualified,
214 * but only if it contained at least one dot (even trailing).
215 * This is purely a heuristic; we assume that any reasonable query
216 * about a top-level domain (for servers, SOA, etc) will not use
219 if (n
&& (ret
= res_querydomain(name
, (char *)NULL
, class, type
,
220 answer
, anslen
)) > 0)
228 * Perform a call on res_query on the concatenation of name and domain,
229 * removing a trailing dot from name if domain is NULL.
232 res_querydomain(name
, domain
, class, type
, answer
, anslen
)
234 int class, type
; /* class and type of query */
235 u_char
*answer
; /* buffer to put answer */
236 int anslen
; /* size of answer */
238 char nbuf
[2*MAXDNAME
+2];
239 char *longname
= nbuf
;
243 if (_res
.options
& RES_DEBUG
)
244 printf("res_querydomain(%s, %s, %d, %d)\n",
245 name
, domain
, class, type
);
247 if (domain
== NULL
) {
249 * Check for trailing '.';
250 * copy without '.' if present.
252 n
= strlen(name
) - 1;
253 if (name
[n
] == '.' && n
< sizeof(nbuf
) - 1) {
254 bcopy(name
, nbuf
, n
);
259 (void)sprintf(nbuf
, "%.*s.%.*s",
260 MAXDNAME
, name
, MAXDNAME
, domain
);
262 return (res_query(longname
, class, type
, answer
, anslen
));
267 register CONST
char *name
;
269 register char *C1
, *C2
;
273 static char abuf
[MAXDNAME
];
275 file
= getenv("HOSTALIASES");
276 if (file
== NULL
|| (fp
= fopen(file
, "r")) == NULL
)
278 buf
[sizeof(buf
) - 1] = '\0';
279 while (fgets(buf
, sizeof(buf
), fp
)) {
280 for (C1
= buf
; *C1
&& !isspace(*C1
); ++C1
);
284 if (!strcasecmp(buf
, name
)) {
285 while (isspace(*++C1
));
288 for (C2
= C1
+ 1; *C2
&& !isspace(*C2
); ++C2
);
289 abuf
[sizeof(abuf
) - 1] = *C2
= '\0';
290 (void)strncpy(abuf
, C1
, sizeof(abuf
) - 1);