diff options
Diffstat (limited to 'src/lib/libc/net/res_query.c')
| -rw-r--r-- | src/lib/libc/net/res_query.c | 73 | 
1 files changed, 52 insertions, 21 deletions
| diff --git a/src/lib/libc/net/res_query.c b/src/lib/libc/net/res_query.c index 7649462e56..81c4d3194d 100644 --- a/src/lib/libc/net/res_query.c +++ b/src/lib/libc/net/res_query.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $NetBSD: res_query.c,v 1.9 1995/02/25 06:58:58 cgd Exp $ */ | 1 | /* $NetBSD: res_query.c,v 1.10 1996/02/02 15:22:34 mrg Exp $ */ | 
| 2 | 2 | ||
| 3 | /*- | 3 | /*- | 
| 4 | * Copyright (c) 1988, 1993 | 4 | * Copyright (c) 1988, 1993 | 
| @@ -56,9 +56,9 @@ | |||
| 56 | #if defined(LIBC_SCCS) && !defined(lint) | 56 | #if defined(LIBC_SCCS) && !defined(lint) | 
| 57 | #if 0 | 57 | #if 0 | 
| 58 | static char sccsid[] = "@(#)res_query.c 8.1 (Berkeley) 6/4/93"; | 58 | static char sccsid[] = "@(#)res_query.c 8.1 (Berkeley) 6/4/93"; | 
| 59 | static char rcsid[] = "$Id: res_query.c,v 1.1 1993/06/01 09:42:14 vixie Exp vixie "; | 59 | static char rcsid[] = "$Id: res_query.c,v 8.6 1995/06/29 09:26:28 vixie Exp "; | 
| 60 | #else | 60 | #else | 
| 61 | static char rcsid[] = "$NetBSD: res_query.c,v 1.9 1995/02/25 06:58:58 cgd Exp $"; | 61 | static char rcsid[] = "$NetBSD: res_query.c,v 1.10 1996/02/02 15:22:34 mrg Exp $"; | 
| 62 | #endif | 62 | #endif | 
| 63 | #endif /* LIBC_SCCS and not lint */ | 63 | #endif /* LIBC_SCCS and not lint */ | 
| 64 | 64 | ||
| @@ -90,27 +90,33 @@ int h_errno; | |||
| 90 | * if no error is indicated and the answer count is nonzero. | 90 | * if no error is indicated and the answer count is nonzero. | 
| 91 | * Return the size of the response on success, -1 on error. | 91 | * Return the size of the response on success, -1 on error. | 
| 92 | * Error number is left in h_errno. | 92 | * Error number is left in h_errno. | 
| 93 | * | ||
| 93 | * Caller must parse answer and determine whether it answers the question. | 94 | * Caller must parse answer and determine whether it answers the question. | 
| 94 | */ | 95 | */ | 
| 96 | int | ||
| 95 | res_query(name, class, type, answer, anslen) | 97 | res_query(name, class, type, answer, anslen) | 
| 96 | char *name; /* domain name */ | 98 | const char *name; /* domain name */ | 
| 97 | int class, type; /* class and type of query */ | 99 | int class, type; /* class and type of query */ | 
| 98 | u_char *answer; /* buffer to put answer */ | 100 | u_char *answer; /* buffer to put answer */ | 
| 99 | int anslen; /* size of answer buffer */ | 101 | int anslen; /* size of answer buffer */ | 
| 100 | { | 102 | { | 
| 101 | char buf[MAXPACKET]; | 103 | u_char buf[MAXPACKET]; | 
| 102 | HEADER *hp; | 104 | register HEADER *hp = (HEADER *) answer; | 
| 103 | int n; | 105 | int n; | 
| 104 | 106 | ||
| 105 | if ((_res.options & RES_INIT) == 0 && res_init() == -1) | 107 | hp->rcode = NOERROR; /* default */ | 
| 108 | |||
| 109 | if ((_res.options & RES_INIT) == 0 && res_init() == -1) { | ||
| 110 | h_errno = NETDB_INTERNAL; | ||
| 106 | return (-1); | 111 | return (-1); | 
| 112 | } | ||
| 107 | #ifdef DEBUG | 113 | #ifdef DEBUG | 
| 108 | if (_res.options & RES_DEBUG) | 114 | if (_res.options & RES_DEBUG) | 
| 109 | printf(";; res_query(%s, %d, %d)\n", name, class, type); | 115 | printf(";; res_query(%s, %d, %d)\n", name, class, type); | 
| 110 | #endif | 116 | #endif | 
| 111 | n = res_mkquery(QUERY, name, class, type, (char *)NULL, 0, NULL, | ||
| 112 | buf, sizeof(buf)); | ||
| 113 | 117 | ||
| 118 | n = res_mkquery(QUERY, name, class, type, NULL, 0, NULL, | ||
| 119 | buf, sizeof(buf)); | ||
| 114 | if (n <= 0) { | 120 | if (n <= 0) { | 
| 115 | #ifdef DEBUG | 121 | #ifdef DEBUG | 
| 116 | if (_res.options & RES_DEBUG) | 122 | if (_res.options & RES_DEBUG) | 
| @@ -119,7 +125,7 @@ res_query(name, class, type, answer, anslen) | |||
| 119 | h_errno = NO_RECOVERY; | 125 | h_errno = NO_RECOVERY; | 
| 120 | return (n); | 126 | return (n); | 
| 121 | } | 127 | } | 
| 122 | n = res_send(buf, n, (char *)answer, anslen); | 128 | n = res_send(buf, n, answer, anslen); | 
| 123 | if (n < 0) { | 129 | if (n < 0) { | 
| 124 | #ifdef DEBUG | 130 | #ifdef DEBUG | 
| 125 | if (_res.options & RES_DEBUG) | 131 | if (_res.options & RES_DEBUG) | 
| @@ -174,10 +180,15 @@ res_search(name, class, type, answer, anslen) | |||
| 174 | int anslen; /* size of answer */ | 180 | int anslen; /* size of answer */ | 
| 175 | { | 181 | { | 
| 176 | register char *cp, **domain; | 182 | register char *cp, **domain; | 
| 177 | int dots, trailing_dot, ret, got_nodata, saved_herrno, tried_as_is; | 183 | HEADER *hp = (HEADER *) answer; | 
| 184 | u_int dots; | ||
| 185 | int trailing_dot, ret, saved_herrno; | ||
| 186 | int got_nodata = 0, got_servfail = 0, tried_as_is = 0; | ||
| 178 | 187 | ||
| 179 | if ((_res.options & RES_INIT) == 0 && res_init() == -1) | 188 | if ((_res.options & RES_INIT) == 0 && res_init() == -1) { | 
| 189 | h_errno = NETDB_INTERNAL; | ||
| 180 | return (-1); | 190 | return (-1); | 
| 191 | } | ||
| 181 | 192 | ||
| 182 | got_nodata = 0; | 193 | got_nodata = 0; | 
| 183 | errno = 0; | 194 | errno = 0; | 
| @@ -188,13 +199,13 @@ res_search(name, class, type, answer, anslen) | |||
| 188 | dots++; | 199 | dots++; | 
| 189 | } | 200 | } | 
| 190 | trailing_dot = 0; | 201 | trailing_dot = 0; | 
| 191 | if ((cp > name) && (*--cp == '.')) | 202 | if (cp > name && *--cp == '.') | 
| 192 | trailing_dot++; | 203 | trailing_dot++; | 
| 193 | 204 | ||
| 194 | /* | 205 | /* | 
| 195 | * if there aren't any dots, it could be a user-level alias | 206 | * if there aren't any dots, it could be a user-level alias | 
| 196 | */ | 207 | */ | 
| 197 | if (!dots && (cp = __hostalias(name))) | 208 | if (!dots && (cp = __hostalias(name)) != NULL) | 
| 198 | return (res_query(cp, class, type, answer, anslen)); | 209 | return (res_query(cp, class, type, answer, anslen)); | 
| 199 | 210 | ||
| 200 | /* | 211 | /* | 
| @@ -251,6 +262,13 @@ res_search(name, class, type, answer, anslen) | |||
| 251 | case HOST_NOT_FOUND: | 262 | case HOST_NOT_FOUND: | 
| 252 | /* keep trying */ | 263 | /* keep trying */ | 
| 253 | break; | 264 | break; | 
| 265 | case TRY_AGAIN: | ||
| 266 | if (hp->rcode == SERVFAIL) { | ||
| 267 | /* try next search element, if any */ | ||
| 268 | got_servfail++; | ||
| 269 | break; | ||
| 270 | } | ||
| 271 | /* FALLTHROUGH */ | ||
| 254 | default: | 272 | default: | 
| 255 | /* anything else implies that we're done */ | 273 | /* anything else implies that we're done */ | 
| 256 | done++; | 274 | done++; | 
| @@ -291,6 +309,8 @@ res_search(name, class, type, answer, anslen) | |||
| 291 | h_errno = saved_herrno; | 309 | h_errno = saved_herrno; | 
| 292 | else if (got_nodata) | 310 | else if (got_nodata) | 
| 293 | h_errno = NO_DATA; | 311 | h_errno = NO_DATA; | 
| 312 | else if (got_servfail) | ||
| 313 | h_errno = TRY_AGAIN; | ||
| 294 | return (-1); | 314 | return (-1); | 
| 295 | } | 315 | } | 
| 296 | 316 | ||
| @@ -298,20 +318,25 @@ res_search(name, class, type, answer, anslen) | |||
| 298 | * Perform a call on res_query on the concatenation of name and domain, | 318 | * Perform a call on res_query on the concatenation of name and domain, | 
| 299 | * removing a trailing dot from name if domain is NULL. | 319 | * removing a trailing dot from name if domain is NULL. | 
| 300 | */ | 320 | */ | 
| 321 | int | ||
| 301 | res_querydomain(name, domain, class, type, answer, anslen) | 322 | res_querydomain(name, domain, class, type, answer, anslen) | 
| 302 | char *name, *domain; | 323 | const char *name, *domain; | 
| 303 | int class, type; /* class and type of query */ | 324 | int class, type; /* class and type of query */ | 
| 304 | u_char *answer; /* buffer to put answer */ | 325 | u_char *answer; /* buffer to put answer */ | 
| 305 | int anslen; /* size of answer */ | 326 | int anslen; /* size of answer */ | 
| 306 | { | 327 | { | 
| 307 | char nbuf[2*MAXDNAME+2]; | 328 | char nbuf[2*MAXDNAME+2]; | 
| 308 | char *longname = nbuf; | 329 | const char *longname = nbuf; | 
| 309 | int n; | 330 | int n; | 
| 310 | 331 | ||
| 332 | if ((_res.options & RES_INIT) == 0 && res_init() == -1) { | ||
| 333 | h_errno = NETDB_INTERNAL; | ||
| 334 | return (-1); | ||
| 335 | } | ||
| 311 | #ifdef DEBUG | 336 | #ifdef DEBUG | 
| 312 | if (_res.options & RES_DEBUG) | 337 | if (_res.options & RES_DEBUG) | 
| 313 | printf(";; res_querydomain(%s, %s, %d, %d)\n", | 338 | printf(";; res_querydomain(%s, %s, %d, %d)\n", | 
| 314 | name, domain, class, type); | 339 | name, domain?domain:"<Nil>", class, type); | 
| 315 | #endif | 340 | #endif | 
| 316 | if (domain == NULL) { | 341 | if (domain == NULL) { | 
| 317 | /* | 342 | /* | 
| @@ -337,24 +362,30 @@ __hostalias(name) | |||
| 337 | { | 362 | { | 
| 338 | register char *cp1, *cp2; | 363 | register char *cp1, *cp2; | 
| 339 | FILE *fp; | 364 | FILE *fp; | 
| 340 | char *file, *getenv(), *strcpy(), *strncpy(); | 365 | char *file; | 
| 341 | char buf[BUFSIZ]; | 366 | char buf[BUFSIZ]; | 
| 342 | static char abuf[MAXDNAME]; | 367 | static char abuf[MAXDNAME]; | 
| 343 | 368 | ||
| 369 | if (_res.options & RES_NOALIASES) | ||
| 370 | return (NULL); | ||
| 344 | file = getenv("HOSTALIASES"); | 371 | file = getenv("HOSTALIASES"); | 
| 345 | if (file == NULL || (fp = fopen(file, "r")) == NULL) | 372 | if (file == NULL || (fp = fopen(file, "r")) == NULL) | 
| 346 | return (NULL); | 373 | return (NULL); | 
| 374 | setbuf(fp, NULL); | ||
| 347 | buf[sizeof(buf) - 1] = '\0'; | 375 | buf[sizeof(buf) - 1] = '\0'; | 
| 348 | while (fgets(buf, sizeof(buf), fp)) { | 376 | while (fgets(buf, sizeof(buf), fp)) { | 
| 349 | for (cp1 = buf; *cp1 && !isspace(*cp1); ++cp1); | 377 | for (cp1 = buf; *cp1 && !isspace(*cp1); ++cp1) | 
| 378 | ; | ||
| 350 | if (!*cp1) | 379 | if (!*cp1) | 
| 351 | break; | 380 | break; | 
| 352 | *cp1 = '\0'; | 381 | *cp1 = '\0'; | 
| 353 | if (!strcasecmp(buf, name)) { | 382 | if (!strcasecmp(buf, name)) { | 
| 354 | while (isspace(*++cp1)); | 383 | while (isspace(*++cp1)) | 
| 384 | ; | ||
| 355 | if (!*cp1) | 385 | if (!*cp1) | 
| 356 | break; | 386 | break; | 
| 357 | for (cp2 = cp1 + 1; *cp2 && !isspace(*cp2); ++cp2); | 387 | for (cp2 = cp1 + 1; *cp2 && !isspace(*cp2); ++cp2) | 
| 388 | ; | ||
| 358 | abuf[sizeof(abuf) - 1] = *cp2 = '\0'; | 389 | abuf[sizeof(abuf) - 1] = *cp2 = '\0'; | 
| 359 | (void)strncpy(abuf, cp1, sizeof(abuf) - 1); | 390 | (void)strncpy(abuf, cp1, sizeof(abuf) - 1); | 
| 360 | fclose(fp); | 391 | fclose(fp); | 
