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