diff options
Diffstat (limited to 'src/lib/libc/net/res_query.c')
-rw-r--r-- | src/lib/libc/net/res_query.c | 116 |
1 files changed, 58 insertions, 58 deletions
diff --git a/src/lib/libc/net/res_query.c b/src/lib/libc/net/res_query.c index fb2d22c119..fcb3954009 100644 --- a/src/lib/libc/net/res_query.c +++ b/src/lib/libc/net/res_query.c | |||
@@ -1,7 +1,11 @@ | |||
1 | /*- | 1 | /* $OpenBSD: res_query.c,v 1.8 1997/03/13 19:07:40 downsj Exp $ */ |
2 | |||
3 | /* | ||
4 | * ++Copyright++ 1988, 1993 | ||
5 | * - | ||
2 | * Copyright (c) 1988, 1993 | 6 | * Copyright (c) 1988, 1993 |
3 | * The Regents of the University of California. All rights reserved. | 7 | * The Regents of the University of California. All rights reserved. |
4 | * | 8 | * |
5 | * Redistribution and use in source and binary forms, with or without | 9 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions | 10 | * modification, are permitted provided that the following conditions |
7 | * are met: | 11 | * are met: |
@@ -12,12 +16,12 @@ | |||
12 | * documentation and/or other materials provided with the distribution. | 16 | * documentation and/or other materials provided with the distribution. |
13 | * 3. All advertising materials mentioning features or use of this software | 17 | * 3. All advertising materials mentioning features or use of this software |
14 | * must display the following acknowledgement: | 18 | * must display the following acknowledgement: |
15 | * This product includes software developed by the University of | 19 | * This product includes software developed by the University of |
16 | * California, Berkeley and its contributors. | 20 | * California, Berkeley and its contributors. |
17 | * 4. Neither the name of the University nor the names of its contributors | 21 | * 4. Neither the name of the University nor the names of its contributors |
18 | * may be used to endorse or promote products derived from this software | 22 | * may be used to endorse or promote products derived from this software |
19 | * without specific prior written permission. | 23 | * without specific prior written permission. |
20 | * | 24 | * |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | 25 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 26 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
@@ -52,16 +56,23 @@ | |||
52 | */ | 56 | */ |
53 | 57 | ||
54 | #if defined(LIBC_SCCS) && !defined(lint) | 58 | #if defined(LIBC_SCCS) && !defined(lint) |
55 | static char rcsid[] = "$OpenBSD: res_query.c,v 1.7 1996/08/27 03:32:54 deraadt Exp $"; | 59 | #if 0 |
60 | static char sccsid[] = "@(#)res_query.c 8.1 (Berkeley) 6/4/93"; | ||
61 | static char rcsid[] = "$From: res_query.c,v 8.9 1996/09/22 00:13:28 vixie Exp $"; | ||
62 | #else | ||
63 | static char rcsid[] = "$OpenBSD: res_query.c,v 1.8 1997/03/13 19:07:40 downsj Exp $"; | ||
64 | #endif | ||
56 | #endif /* LIBC_SCCS and not lint */ | 65 | #endif /* LIBC_SCCS and not lint */ |
57 | 66 | ||
67 | #include <sys/types.h> | ||
58 | #include <sys/param.h> | 68 | #include <sys/param.h> |
59 | #include <netinet/in.h> | 69 | #include <netinet/in.h> |
60 | #include <arpa/inet.h> | 70 | #include <arpa/inet.h> |
61 | #include <arpa/nameser.h> | 71 | #include <arpa/nameser.h> |
72 | |||
73 | #include <stdio.h> | ||
62 | #include <netdb.h> | 74 | #include <netdb.h> |
63 | #include <resolv.h> | 75 | #include <resolv.h> |
64 | #include <stdio.h> | ||
65 | #include <ctype.h> | 76 | #include <ctype.h> |
66 | #include <errno.h> | 77 | #include <errno.h> |
67 | #include <stdlib.h> | 78 | #include <stdlib.h> |
@@ -73,7 +84,7 @@ static char rcsid[] = "$OpenBSD: res_query.c,v 1.7 1996/08/27 03:32:54 deraadt E | |||
73 | #define MAXPACKET 1024 | 84 | #define MAXPACKET 1024 |
74 | #endif | 85 | #endif |
75 | 86 | ||
76 | char *__hostalias __P((const char *)); | 87 | const char *hostalias __P((const char *)); |
77 | int h_errno; | 88 | int h_errno; |
78 | 89 | ||
79 | /* | 90 | /* |
@@ -109,7 +120,7 @@ res_query(name, class, type, answer, anslen) | |||
109 | #endif | 120 | #endif |
110 | 121 | ||
111 | n = res_mkquery(QUERY, name, class, type, NULL, 0, NULL, | 122 | n = res_mkquery(QUERY, name, class, type, NULL, 0, NULL, |
112 | buf, sizeof(buf)); | 123 | buf, sizeof(buf)); |
113 | if (n <= 0) { | 124 | if (n <= 0) { |
114 | #ifdef DEBUG | 125 | #ifdef DEBUG |
115 | if (_res.options & RES_DEBUG) | 126 | if (_res.options & RES_DEBUG) |
@@ -128,7 +139,6 @@ res_query(name, class, type, answer, anslen) | |||
128 | return (n); | 139 | return (n); |
129 | } | 140 | } |
130 | 141 | ||
131 | hp = (HEADER *) answer; | ||
132 | if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) { | 142 | if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) { |
133 | #ifdef DEBUG | 143 | #ifdef DEBUG |
134 | if (_res.options & RES_DEBUG) | 144 | if (_res.options & RES_DEBUG) |
@@ -136,21 +146,21 @@ res_query(name, class, type, answer, anslen) | |||
136 | ntohs(hp->ancount)); | 146 | ntohs(hp->ancount)); |
137 | #endif | 147 | #endif |
138 | switch (hp->rcode) { | 148 | switch (hp->rcode) { |
139 | case NXDOMAIN: | 149 | case NXDOMAIN: |
140 | h_errno = HOST_NOT_FOUND; | 150 | h_errno = HOST_NOT_FOUND; |
141 | break; | 151 | break; |
142 | case SERVFAIL: | 152 | case SERVFAIL: |
143 | h_errno = TRY_AGAIN; | 153 | h_errno = TRY_AGAIN; |
144 | break; | 154 | break; |
145 | case NOERROR: | 155 | case NOERROR: |
146 | h_errno = NO_DATA; | 156 | h_errno = NO_DATA; |
147 | break; | 157 | break; |
148 | case FORMERR: | 158 | case FORMERR: |
149 | case NOTIMP: | 159 | case NOTIMP: |
150 | case REFUSED: | 160 | case REFUSED: |
151 | default: | 161 | default: |
152 | h_errno = NO_RECOVERY; | 162 | h_errno = NO_RECOVERY; |
153 | break; | 163 | break; |
154 | } | 164 | } |
155 | return (-1); | 165 | return (-1); |
156 | } | 166 | } |
@@ -161,9 +171,7 @@ res_query(name, class, type, answer, anslen) | |||
161 | * Formulate a normal query, send, and retrieve answer in supplied buffer. | 171 | * Formulate a normal query, send, and retrieve answer in supplied buffer. |
162 | * Return the size of the response on success, -1 on error. | 172 | * Return the size of the response on success, -1 on error. |
163 | * If enabled, implement search rules until answer or unrecoverable failure | 173 | * If enabled, implement search rules until answer or unrecoverable failure |
164 | * is detected. Error number is left in h_errno. | 174 | * is detected. Error code, if any, is left in h_errno. |
165 | * Only useful for queries in the same name hierarchy as the local host | ||
166 | * (not, for example, for host address-to-name lookups in domain in-addr.arpa). | ||
167 | */ | 175 | */ |
168 | int | 176 | int |
169 | res_search(name, class, type, answer, anslen) | 177 | res_search(name, class, type, answer, anslen) |
@@ -172,7 +180,7 @@ res_search(name, class, type, answer, anslen) | |||
172 | u_char *answer; /* buffer to put answer */ | 180 | u_char *answer; /* buffer to put answer */ |
173 | int anslen; /* size of answer */ | 181 | int anslen; /* size of answer */ |
174 | { | 182 | { |
175 | register char *cp, **domain; | 183 | register const char *cp, * const *domain; |
176 | HEADER *hp = (HEADER *) answer; | 184 | HEADER *hp = (HEADER *) answer; |
177 | u_int dots; | 185 | u_int dots; |
178 | int trailing_dot, ret, saved_herrno; | 186 | int trailing_dot, ret, saved_herrno; |
@@ -182,15 +190,11 @@ res_search(name, class, type, answer, anslen) | |||
182 | h_errno = NETDB_INTERNAL; | 190 | h_errno = NETDB_INTERNAL; |
183 | return (-1); | 191 | return (-1); |
184 | } | 192 | } |
185 | |||
186 | got_nodata = 0; | ||
187 | errno = 0; | 193 | errno = 0; |
188 | h_errno = HOST_NOT_FOUND; /* default, if we never query */ | 194 | h_errno = HOST_NOT_FOUND; /* default, if we never query */ |
189 | dots = 0; | 195 | dots = 0; |
190 | for (cp = (char *)name; *cp; cp++) { | 196 | for (cp = name; *cp; cp++) |
191 | if (*cp == '.') | 197 | dots += (*cp == '.'); |
192 | dots++; | ||
193 | } | ||
194 | trailing_dot = 0; | 198 | trailing_dot = 0; |
195 | if (cp > name && *--cp == '.') | 199 | if (cp > name && *--cp == '.') |
196 | trailing_dot++; | 200 | trailing_dot++; |
@@ -206,7 +210,6 @@ res_search(name, class, type, answer, anslen) | |||
206 | * 'as is'. The threshold can be set with the "ndots" option. | 210 | * 'as is'. The threshold can be set with the "ndots" option. |
207 | */ | 211 | */ |
208 | saved_herrno = -1; | 212 | saved_herrno = -1; |
209 | tried_as_is = 0; | ||
210 | if (dots >= _res.ndots) { | 213 | if (dots >= _res.ndots) { |
211 | ret = res_querydomain(name, NULL, class, type, answer, anslen); | 214 | ret = res_querydomain(name, NULL, class, type, answer, anslen); |
212 | if (ret > 0) | 215 | if (ret > 0) |
@@ -223,13 +226,17 @@ res_search(name, class, type, answer, anslen) | |||
223 | */ | 226 | */ |
224 | if ((!dots && (_res.options & RES_DEFNAMES)) || | 227 | if ((!dots && (_res.options & RES_DEFNAMES)) || |
225 | (dots && !trailing_dot && (_res.options & RES_DNSRCH))) { | 228 | (dots && !trailing_dot && (_res.options & RES_DNSRCH))) { |
226 | for (domain = _res.dnsrch; *domain; domain++) { | 229 | int done = 0; |
227 | int done = 0; | 230 | |
231 | for (domain = (const char * const *)_res.dnsrch; | ||
232 | *domain && !done; | ||
233 | domain++) { | ||
228 | 234 | ||
229 | ret = res_querydomain(name, *domain, class, type, | 235 | ret = res_querydomain(name, *domain, class, type, |
230 | answer, anslen); | 236 | answer, anslen); |
231 | if (ret > 0) | 237 | if (ret > 0) |
232 | return (ret); | 238 | return (ret); |
239 | |||
233 | /* | 240 | /* |
234 | * If no server present, give up. | 241 | * If no server present, give up. |
235 | * If name isn't found in this domain, | 242 | * If name isn't found in this domain, |
@@ -266,20 +273,16 @@ res_search(name, class, type, answer, anslen) | |||
266 | /* anything else implies that we're done */ | 273 | /* anything else implies that we're done */ |
267 | done++; | 274 | done++; |
268 | } | 275 | } |
269 | /* | 276 | |
270 | * if we got here for some reason other than DNSRCH, | 277 | /* if we got here for some reason other than DNSRCH, |
271 | * we only wanted one iteration of the loop, so stop. | 278 | * we only wanted one iteration of the loop, so stop. |
272 | */ | 279 | */ |
273 | if (!(_res.options & RES_DNSRCH)) | 280 | if (!(_res.options & RES_DNSRCH)) |
274 | done++; | 281 | done++; |
275 | |||
276 | if (done) | ||
277 | break; | ||
278 | } | 282 | } |
279 | } | 283 | } |
280 | 284 | ||
281 | /* | 285 | /* if we have not already tried the name "as is", do that now. |
282 | * if we have not already tried the name "as is", do that now. | ||
283 | * note that we do this regardless of how many dots were in the | 286 | * note that we do this regardless of how many dots were in the |
284 | * name or whether it ends with a dot. | 287 | * name or whether it ends with a dot. |
285 | */ | 288 | */ |
@@ -287,11 +290,9 @@ res_search(name, class, type, answer, anslen) | |||
287 | ret = res_querydomain(name, NULL, class, type, answer, anslen); | 290 | ret = res_querydomain(name, NULL, class, type, answer, anslen); |
288 | if (ret > 0) | 291 | if (ret > 0) |
289 | return (ret); | 292 | return (ret); |
290 | saved_herrno = h_errno; | ||
291 | } | 293 | } |
292 | 294 | ||
293 | /* | 295 | /* if we got here, we didn't satisfy the search. |
294 | * if we got here, we didn't satisfy the search. | ||
295 | * if we did an initial full query, return that query's h_errno | 296 | * if we did an initial full query, return that query's h_errno |
296 | * (note that we wouldn't be here if that query had succeeded). | 297 | * (note that we wouldn't be here if that query had succeeded). |
297 | * else if we ever got a nodata, send that back as the reason. | 298 | * else if we ever got a nodata, send that back as the reason. |
@@ -318,7 +319,7 @@ res_querydomain(name, domain, class, type, answer, anslen) | |||
318 | u_char *answer; /* buffer to put answer */ | 319 | u_char *answer; /* buffer to put answer */ |
319 | int anslen; /* size of answer */ | 320 | int anslen; /* size of answer */ |
320 | { | 321 | { |
321 | char nbuf[2*MAXDNAME+2]; | 322 | char nbuf[MAXDNAME]; |
322 | const char *longname = nbuf; | 323 | const char *longname = nbuf; |
323 | int n; | 324 | int n; |
324 | 325 | ||
@@ -329,7 +330,7 @@ res_querydomain(name, domain, class, type, answer, anslen) | |||
329 | #ifdef DEBUG | 330 | #ifdef DEBUG |
330 | if (_res.options & RES_DEBUG) | 331 | if (_res.options & RES_DEBUG) |
331 | printf(";; res_querydomain(%s, %s, %d, %d)\n", | 332 | printf(";; res_querydomain(%s, %s, %d, %d)\n", |
332 | name, domain?domain:"<Nil>", class, type); | 333 | name, domain?domain:"<Nil>", class, type); |
333 | #endif | 334 | #endif |
334 | if (domain == NULL) { | 335 | if (domain == NULL) { |
335 | /* | 336 | /* |
@@ -343,14 +344,13 @@ res_querydomain(name, domain, class, type, answer, anslen) | |||
343 | } else | 344 | } else |
344 | longname = name; | 345 | longname = name; |
345 | } else | 346 | } else |
346 | (void)sprintf(nbuf, "%.*s.%.*s", | 347 | sprintf(nbuf, "%.*s.%.*s", MAXDNAME, name, MAXDNAME, domain); |
347 | MAXDNAME, name, MAXDNAME, domain); | ||
348 | 348 | ||
349 | return (res_query(longname, class, type, answer, anslen)); | 349 | return (res_query(longname, class, type, answer, anslen)); |
350 | } | 350 | } |
351 | 351 | ||
352 | char * | 352 | const char * |
353 | __hostalias(name) | 353 | hostalias(name) |
354 | register const char *name; | 354 | register const char *name; |
355 | { | 355 | { |
356 | register char *cp1, *cp2; | 356 | register char *cp1, *cp2; |
@@ -379,7 +379,7 @@ __hostalias(name) | |||
379 | break; | 379 | break; |
380 | for (cp2 = cp1 + 1; *cp2 && !isspace(*cp2); ++cp2) | 380 | for (cp2 = cp1 + 1; *cp2 && !isspace(*cp2); ++cp2) |
381 | ; | 381 | ; |
382 | (void)strncpy(abuf, cp1, sizeof(abuf) - 1); | 382 | strncpy(abuf, cp1, sizeof(abuf) - 1); |
383 | abuf[sizeof(abuf) - 1] = *cp2 = '\0'; | 383 | abuf[sizeof(abuf) - 1] = *cp2 = '\0'; |
384 | fclose(fp); | 384 | fclose(fp); |
385 | return (abuf); | 385 | return (abuf); |