gethostbydns.c revision 145677
1/* 2 * ++Copyright++ 1985, 1988, 1993 3 * - 4 * Copyright (c) 1985, 1988, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * - 35 * Portions Copyright (c) 1993 by Digital Equipment Corporation. 36 * 37 * Permission to use, copy, modify, and distribute this software for any 38 * purpose with or without fee is hereby granted, provided that the above 39 * copyright notice and this permission notice appear in all copies, and that 40 * the name of Digital Equipment Corporation not be used in advertising or 41 * publicity pertaining to distribution of the document or software without 42 * specific, written prior permission. 43 * 44 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 45 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 46 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 47 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 48 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 49 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 50 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 51 * SOFTWARE. 52 * - 53 * --Copyright-- 54 */ 55 56#if defined(LIBC_SCCS) && !defined(lint) 57static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93"; 58static char fromrcsid[] = "From: Id: gethnamaddr.c,v 8.23 1998/04/07 04:59:46 vixie Exp $"; 59#endif /* LIBC_SCCS and not lint */ 60#include <sys/cdefs.h> 61__FBSDID("$FreeBSD: head/lib/libc/net/gethostbydns.c 145677 2005-04-29 12:01:36Z ume $"); 62 63#include <sys/types.h> 64#include <sys/param.h> 65#include <sys/socket.h> 66#include <netinet/in.h> 67#include <arpa/inet.h> 68#include <arpa/nameser.h> 69 70#include <stdio.h> 71#include <stdlib.h> 72#include <unistd.h> 73#include <string.h> 74#include <netdb.h> 75#include <resolv.h> 76#include <ctype.h> 77#include <errno.h> 78#include <syslog.h> 79#include <stdarg.h> 80#include <nsswitch.h> 81 82#include "netdb_private.h" 83#include "res_config.h" 84 85#define SPRINTF(x) ((size_t)sprintf x) 86 87static const char AskedForGot[] = 88 "gethostby*.gethostanswer: asked for \"%s\", got \"%s\""; 89 90#ifdef RESOLVSORT 91static void addrsort(char **, int); 92#endif 93 94#ifdef DEBUG 95static void dprintf(char *, int) __printflike(1, 0); 96#endif 97 98#define MAXPACKET (64*1024) 99 100typedef union { 101 HEADER hdr; 102 u_char buf[MAXPACKET]; 103} querybuf; 104 105typedef union { 106 int32_t al; 107 char ac; 108} align; 109 110int _dns_ttl_; 111 112#ifdef DEBUG 113static void 114dprintf(msg, num) 115 char *msg; 116 int num; 117{ 118 if (_res.options & RES_DEBUG) { 119 int save = errno; 120 121 printf(msg, num); 122 errno = save; 123 } 124} 125#else 126# define dprintf(msg, num) /*nada*/ 127#endif 128 129#define BOUNDED_INCR(x) \ 130 do { \ 131 cp += x; \ 132 if (cp > eom) { \ 133 h_errno = NO_RECOVERY; \ 134 return -1; \ 135 } \ 136 } while (0) 137 138#define BOUNDS_CHECK(ptr, count) \ 139 do { \ 140 if ((ptr) + (count) > eom) { \ 141 h_errno = NO_RECOVERY; \ 142 return -1; \ 143 } \ 144 } while (0) 145 146static int 147gethostanswer(const querybuf *answer, int anslen, const char *qname, int qtype, 148 struct hostent *he, struct hostent_data *hed) 149{ 150 const HEADER *hp; 151 const u_char *cp; 152 int n; 153 const u_char *eom, *erdata; 154 char *bp, *ep, **ap, **hap; 155 int type, class, ancount, qdcount; 156 int haveanswer, had_error; 157 int toobig = 0; 158 char tbuf[MAXDNAME]; 159 const char *tname; 160 int (*name_ok)(const char *); 161 162 tname = qname; 163 he->h_name = NULL; 164 eom = answer->buf + anslen; 165 switch (qtype) { 166 case T_A: 167 case T_AAAA: 168 name_ok = res_hnok; 169 break; 170 case T_PTR: 171 name_ok = res_dnok; 172 break; 173 default: 174 h_errno = NO_RECOVERY; 175 return -1; /* XXX should be abort(); */ 176 } 177 /* 178 * find first satisfactory answer 179 */ 180 hp = &answer->hdr; 181 ancount = ntohs(hp->ancount); 182 qdcount = ntohs(hp->qdcount); 183 bp = hed->hostbuf; 184 ep = hed->hostbuf + sizeof hed->hostbuf; 185 cp = answer->buf; 186 BOUNDED_INCR(HFIXEDSZ); 187 if (qdcount != 1) { 188 h_errno = NO_RECOVERY; 189 return -1; 190 } 191 n = dn_expand(answer->buf, eom, cp, bp, ep - bp); 192 if ((n < 0) || !(*name_ok)(bp)) { 193 h_errno = NO_RECOVERY; 194 return -1; 195 } 196 BOUNDED_INCR(n + QFIXEDSZ); 197 if (qtype == T_A || qtype == T_AAAA) { 198 /* res_send() has already verified that the query name is the 199 * same as the one we sent; this just gets the expanded name 200 * (i.e., with the succeeding search-domain tacked on). 201 */ 202 n = strlen(bp) + 1; /* for the \0 */ 203 if (n >= MAXHOSTNAMELEN) { 204 h_errno = NO_RECOVERY; 205 return -1; 206 } 207 he->h_name = bp; 208 bp += n; 209 /* The qname can be abbreviated, but h_name is now absolute. */ 210 qname = he->h_name; 211 } 212 ap = hed->host_aliases; 213 *ap = NULL; 214 he->h_aliases = hed->host_aliases; 215 hap = hed->h_addr_ptrs; 216 *hap = NULL; 217 he->h_addr_list = hed->h_addr_ptrs; 218 haveanswer = 0; 219 had_error = 0; 220 _dns_ttl_ = -1; 221 while (ancount-- > 0 && cp < eom && !had_error) { 222 n = dn_expand(answer->buf, eom, cp, bp, ep - bp); 223 if ((n < 0) || !(*name_ok)(bp)) { 224 had_error++; 225 continue; 226 } 227 cp += n; /* name */ 228 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ); 229 type = _getshort(cp); 230 cp += INT16SZ; /* type */ 231 class = _getshort(cp); 232 cp += INT16SZ; /* class */ 233 if (qtype == T_A && type == T_A) 234 _dns_ttl_ = _getlong(cp); 235 cp += INT32SZ; /* TTL */ 236 n = _getshort(cp); 237 cp += INT16SZ; /* len */ 238 BOUNDS_CHECK(cp, n); 239 erdata = cp + n; 240 if (class != C_IN) { 241 /* XXX - debug? syslog? */ 242 cp += n; 243 continue; /* XXX - had_error++ ? */ 244 } 245 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) { 246 if (ap >= &hed->host_aliases[_MAXALIASES-1]) 247 continue; 248 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); 249 if ((n < 0) || !(*name_ok)(tbuf)) { 250 had_error++; 251 continue; 252 } 253 cp += n; 254 if (cp != erdata) { 255 h_errno = NO_RECOVERY; 256 return -1; 257 } 258 /* Store alias. */ 259 *ap++ = bp; 260 n = strlen(bp) + 1; /* for the \0 */ 261 if (n >= MAXHOSTNAMELEN) { 262 had_error++; 263 continue; 264 } 265 bp += n; 266 /* Get canonical name. */ 267 n = strlen(tbuf) + 1; /* for the \0 */ 268 if (n > ep - bp || n >= MAXHOSTNAMELEN) { 269 had_error++; 270 continue; 271 } 272 strcpy(bp, tbuf); 273 he->h_name = bp; 274 bp += n; 275 continue; 276 } 277 if (qtype == T_PTR && type == T_CNAME) { 278 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); 279 if (n < 0 || !res_dnok(tbuf)) { 280 had_error++; 281 continue; 282 } 283 cp += n; 284 if (cp != erdata) { 285 h_errno = NO_RECOVERY; 286 return -1; 287 } 288 /* Get canonical name. */ 289 n = strlen(tbuf) + 1; /* for the \0 */ 290 if (n > ep - bp || n >= MAXHOSTNAMELEN) { 291 had_error++; 292 continue; 293 } 294 strcpy(bp, tbuf); 295 tname = bp; 296 bp += n; 297 continue; 298 } 299 if (type != qtype) { 300 if (type != T_SIG) 301 syslog(LOG_NOTICE|LOG_AUTH, 302 "gethostby*.gethostanswer: asked for \"%s %s %s\", got type \"%s\"", 303 qname, p_class(C_IN), p_type(qtype), 304 p_type(type)); 305 cp += n; 306 continue; /* XXX - had_error++ ? */ 307 } 308 switch (type) { 309 case T_PTR: 310 if (strcasecmp(tname, bp) != 0) { 311 syslog(LOG_NOTICE|LOG_AUTH, 312 AskedForGot, qname, bp); 313 cp += n; 314 continue; /* XXX - had_error++ ? */ 315 } 316 n = dn_expand(answer->buf, eom, cp, bp, ep - bp); 317 if ((n < 0) || !res_hnok(bp)) { 318 had_error++; 319 break; 320 } 321#if MULTI_PTRS_ARE_ALIASES 322 cp += n; 323 if (cp != erdata) { 324 h_errno = NO_RECOVERY; 325 return -1; 326 } 327 if (!haveanswer) 328 he->h_name = bp; 329 else if (ap < &hed->host_aliases[_MAXALIASES-1]) 330 *ap++ = bp; 331 else 332 n = -1; 333 if (n != -1) { 334 n = strlen(bp) + 1; /* for the \0 */ 335 if (n >= MAXHOSTNAMELEN) { 336 had_error++; 337 break; 338 } 339 bp += n; 340 } 341 break; 342#else 343 he->h_name = bp; 344 if (_res.options & RES_USE_INET6) { 345 n = strlen(bp) + 1; /* for the \0 */ 346 if (n >= MAXHOSTNAMELEN) { 347 had_error++; 348 break; 349 } 350 bp += n; 351 _map_v4v6_hostent(he, &bp, ep); 352 } 353 h_errno = NETDB_SUCCESS; 354 return 0; 355#endif 356 case T_A: 357 case T_AAAA: 358 if (strcasecmp(he->h_name, bp) != 0) { 359 syslog(LOG_NOTICE|LOG_AUTH, 360 AskedForGot, he->h_name, bp); 361 cp += n; 362 continue; /* XXX - had_error++ ? */ 363 } 364 if (n != he->h_length) { 365 cp += n; 366 continue; 367 } 368 if (!haveanswer) { 369 int nn; 370 371 he->h_name = bp; 372 nn = strlen(bp) + 1; /* for the \0 */ 373 bp += nn; 374 } 375 376 bp += sizeof(align) - ((u_long)bp % sizeof(align)); 377 378 if (bp + n >= ep) { 379 dprintf("size (%d) too big\n", n); 380 had_error++; 381 continue; 382 } 383 if (hap >= &hed->h_addr_ptrs[_MAXADDRS-1]) { 384 if (!toobig++) 385 dprintf("Too many addresses (%d)\n", 386 _MAXADDRS); 387 cp += n; 388 continue; 389 } 390 bcopy(cp, *hap++ = bp, n); 391 bp += n; 392 cp += n; 393 if (cp != erdata) { 394 h_errno = NO_RECOVERY; 395 return -1; 396 } 397 break; 398 default: 399 dprintf("Impossible condition (type=%d)\n", type); 400 h_errno = NO_RECOVERY; 401 return -1; 402 /* BIND has abort() here, too risky on bad data */ 403 } 404 if (!had_error) 405 haveanswer++; 406 } 407 if (haveanswer) { 408 *ap = NULL; 409 *hap = NULL; 410# if defined(RESOLVSORT) 411 /* 412 * Note: we sort even if host can take only one address 413 * in its return structures - should give it the "best" 414 * address in that case, not some random one 415 */ 416 if (_res.nsort && haveanswer > 1 && qtype == T_A) 417 addrsort(hed->h_addr_ptrs, haveanswer); 418# endif /*RESOLVSORT*/ 419 if (!he->h_name) { 420 n = strlen(qname) + 1; /* for the \0 */ 421 if (n > ep - bp || n >= MAXHOSTNAMELEN) 422 goto no_recovery; 423 strcpy(bp, qname); 424 he->h_name = bp; 425 bp += n; 426 } 427 if (_res.options & RES_USE_INET6) 428 _map_v4v6_hostent(he, &bp, ep); 429 h_errno = NETDB_SUCCESS; 430 return 0; 431 } 432 no_recovery: 433 h_errno = NO_RECOVERY; 434 return -1; 435} 436 437/* XXX: for async DNS resolver in ypserv */ 438struct hostent * 439__dns_getanswer(const char *answer, int anslen, const char *qname, int qtype) 440{ 441 struct hostdata *hd; 442 int error; 443 444 if ((hd = __hostdata_init()) == NULL) { 445 h_errno = NETDB_INTERNAL; 446 return NULL; 447 } 448 switch (qtype) { 449 case T_AAAA: 450 hd->host.h_addrtype = AF_INET6; 451 hd->host.h_length = IN6ADDRSZ; 452 break; 453 case T_A: 454 default: 455 hd->host.h_addrtype = AF_INET; 456 hd->host.h_length = INADDRSZ; 457 break; 458 } 459 460 error = gethostanswer((const querybuf *)answer, anslen, qname, qtype, 461 &hd->host, &hd->data); 462 return (error == 0) ? &hd->host : NULL; 463} 464 465int 466_dns_gethostbyname(void *rval, void *cb_data, va_list ap) 467{ 468 const char *name; 469 int af; 470 struct hostent *he; 471 struct hostent_data *hed; 472 querybuf *buf; 473 int n, size, type, error; 474 475 name = va_arg(ap, const char *); 476 af = va_arg(ap, int); 477 he = va_arg(ap, struct hostent *); 478 hed = va_arg(ap, struct hostent_data *); 479 480 switch (af) { 481 case AF_INET: 482 size = INADDRSZ; 483 type = T_A; 484 break; 485 case AF_INET6: 486 size = IN6ADDRSZ; 487 type = T_AAAA; 488 break; 489 default: 490 h_errno = NETDB_INTERNAL; 491 errno = EAFNOSUPPORT; 492 return NS_UNAVAIL; 493 } 494 495 he->h_addrtype = af; 496 he->h_length = size; 497 498 if ((buf = malloc(sizeof(*buf))) == NULL) { 499 h_errno = NETDB_INTERNAL; 500 return NS_NOTFOUND; 501 } 502 n = res_search(name, C_IN, type, buf->buf, sizeof(buf->buf)); 503 if (n < 0) { 504 free(buf); 505 dprintf("res_search failed (%d)\n", n); 506 return (0); 507 } else if (n > sizeof(buf->buf)) { 508 free(buf); 509 dprintf("static buffer is too small (%d)\n", n); 510 return (0); 511 } 512 error = gethostanswer(buf, n, name, type, he, hed); 513 free(buf); 514 return (error == 0) ? NS_SUCCESS : NS_NOTFOUND; 515} 516 517int 518_dns_gethostbyaddr(void *rval, void *cb_data, va_list ap) 519{ 520 const char *addr; /* XXX should have been def'd as u_char! */ 521 int len, af; 522 struct hostent *he; 523 struct hostent_data *hed; 524 const u_char *uaddr; 525 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff }; 526 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 }; 527 int n, size, error; 528 querybuf *buf; 529 char qbuf[MAXDNAME+1], *qp; 530#ifdef SUNSECURITY 531 struct hostdata rhd; 532 struct hostent *rhe; 533 char **haddr; 534 u_long old_options; 535 char hname2[MAXDNAME+1], numaddr[46]; 536#endif /*SUNSECURITY*/ 537 538 addr = va_arg(ap, const char *); 539 uaddr = (const u_char *)addr; 540 len = va_arg(ap, int); 541 af = va_arg(ap, int); 542 he = va_arg(ap, struct hostent *); 543 hed = va_arg(ap, struct hostent_data *); 544 545 if ((_res.options & RES_INIT) == 0 && res_init() == -1) { 546 h_errno = NETDB_INTERNAL; 547 return NS_UNAVAIL; 548 } 549 if (af == AF_INET6 && len == IN6ADDRSZ && 550 (!bcmp(uaddr, mapped, sizeof mapped) || 551 !bcmp(uaddr, tunnelled, sizeof tunnelled))) { 552 /* Unmap. */ 553 addr += sizeof mapped; 554 uaddr += sizeof mapped; 555 af = AF_INET; 556 len = INADDRSZ; 557 } 558 switch (af) { 559 case AF_INET: 560 size = INADDRSZ; 561 break; 562 case AF_INET6: 563 size = IN6ADDRSZ; 564 break; 565 default: 566 errno = EAFNOSUPPORT; 567 h_errno = NETDB_INTERNAL; 568 return NS_UNAVAIL; 569 } 570 if (size != len) { 571 errno = EINVAL; 572 h_errno = NETDB_INTERNAL; 573 return NS_UNAVAIL; 574 } 575 switch (af) { 576 case AF_INET: 577 (void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", 578 (uaddr[3] & 0xff), 579 (uaddr[2] & 0xff), 580 (uaddr[1] & 0xff), 581 (uaddr[0] & 0xff)); 582 break; 583 case AF_INET6: 584 qp = qbuf; 585 for (n = IN6ADDRSZ - 1; n >= 0; n--) { 586 qp += SPRINTF((qp, "%x.%x.", 587 uaddr[n] & 0xf, 588 (uaddr[n] >> 4) & 0xf)); 589 } 590 strlcat(qbuf, "ip6.arpa", sizeof(qbuf)); 591 break; 592 default: 593 abort(); 594 } 595 if ((buf = malloc(sizeof(*buf))) == NULL) { 596 h_errno = NETDB_INTERNAL; 597 return NS_NOTFOUND; 598 } 599 n = res_query(qbuf, C_IN, T_PTR, (u_char *)buf->buf, sizeof buf->buf); 600 if (n < 0) { 601 free(buf); 602 dprintf("res_query failed (%d)\n", n); 603 return NS_UNAVAIL; 604 } 605 if (n > sizeof buf->buf) { 606 free(buf); 607 dprintf("static buffer is too small (%d)\n", n); 608 return NS_UNAVAIL; 609 } 610 if ((error = gethostanswer(buf, n, qbuf, T_PTR, he, hed)) != 0) { 611 free(buf); 612 return NS_NOTFOUND; /* h_errno was set by gethostanswer() */ 613 } 614 free(buf); 615#ifdef SUNSECURITY 616 if (af == AF_INET) { 617 /* 618 * turn off search as the name should be absolute, 619 * 'localhost' should be matched by defnames 620 */ 621 strncpy(hname2, he->h_name, MAXDNAME); 622 hname2[MAXDNAME] = '\0'; 623 old_options = _res.options; 624 _res.options &= ~RES_DNSRCH; 625 _res.options |= RES_DEFNAMES; 626 memset(&rhd, 0, sizeof rhd); 627 if (!(rhe = gethostbyname_r(hname2, &rhd.host, &rhd.data))) { 628 if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL) 629 strlcpy(numaddr, "UNKNOWN", sizeof(numaddr)); 630 syslog(LOG_NOTICE|LOG_AUTH, 631 "gethostbyaddr: No A record for %s (verifying [%s])", 632 hname2, numaddr); 633 _res.options = old_options; 634 h_errno = HOST_NOT_FOUND; 635 return NS_NOTFOUND; 636 } 637 _res.options = old_options; 638 for (haddr = rhe->h_addr_list; *haddr; haddr++) 639 if (!memcmp(*haddr, addr, INADDRSZ)) 640 break; 641 if (!*haddr) { 642 if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL) 643 strlcpy(numaddr, "UNKNOWN", sizeof(numaddr)); 644 syslog(LOG_NOTICE|LOG_AUTH, 645 "gethostbyaddr: A record of %s != PTR record [%s]", 646 hname2, numaddr); 647 h_errno = HOST_NOT_FOUND; 648 return NS_NOTFOUND; 649 } 650 } 651#endif /*SUNSECURITY*/ 652 he->h_addrtype = af; 653 he->h_length = len; 654 bcopy(addr, hed->host_addr, len); 655 hed->h_addr_ptrs[0] = (char *)hed->host_addr; 656 hed->h_addr_ptrs[1] = NULL; 657 if (af == AF_INET && (_res.options & RES_USE_INET6)) { 658 _map_v4v6_address((char*)hed->host_addr, (char*)hed->host_addr); 659 he->h_addrtype = AF_INET6; 660 he->h_length = IN6ADDRSZ; 661 } 662 h_errno = NETDB_SUCCESS; 663 return (error == 0) ? NS_SUCCESS : NS_NOTFOUND; 664} 665 666#ifdef RESOLVSORT 667static void 668addrsort(ap, num) 669 char **ap; 670 int num; 671{ 672 int i, j; 673 char **p; 674 short aval[_MAXADDRS]; 675 int needsort = 0; 676 677 p = ap; 678 for (i = 0; i < num; i++, p++) { 679 for (j = 0 ; (unsigned)j < _res.nsort; j++) 680 if (_res.sort_list[j].addr.s_addr == 681 (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask)) 682 break; 683 aval[i] = j; 684 if (needsort == 0 && i > 0 && j < aval[i-1]) 685 needsort = i; 686 } 687 if (!needsort) 688 return; 689 690 while (needsort < num) { 691 for (j = needsort - 1; j >= 0; j--) { 692 if (aval[j] > aval[j+1]) { 693 char *hp; 694 695 i = aval[j]; 696 aval[j] = aval[j+1]; 697 aval[j+1] = i; 698 699 hp = ap[j]; 700 ap[j] = ap[j+1]; 701 ap[j+1] = hp; 702 703 } else 704 break; 705 } 706 needsort++; 707 } 708} 709#endif 710void 711_sethostdnsent(stayopen) 712 int stayopen; 713{ 714 if ((_res.options & RES_INIT) == 0 && res_init() == -1) 715 return; 716 if (stayopen) 717 _res.options |= RES_STAYOPEN | RES_USEVC; 718} 719 720void 721_endhostdnsent() 722{ 723 _res.options &= ~(RES_STAYOPEN | RES_USEVC); 724 res_close(); 725} 726