Merge branch 'vendor/DHCPCD'
[dragonfly.git] / lib / libc / net / gethostbydns.c
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. 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.
18  *
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.
32  *
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.
39  *
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--
50  *
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  */
55
56 #include <sys/types.h>
57 #include <sys/param.h>
58 #include <sys/socket.h>
59 #include <netinet/in.h>
60 #include <arpa/inet.h>
61 #include <arpa/nameser.h>
62
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <unistd.h>
66 #include <string.h>
67 #include <netdb.h>
68 #include <resolv.h>
69 #include <ctype.h>
70 #include <errno.h>
71 #include <syslog.h>
72 #include <stdarg.h>
73 #include <nsswitch.h>
74
75 #include "netdb_private.h"
76 #include "res_config.h"
77
78 #define SPRINTF(x) ((size_t)sprintf x)
79
80 static const char AskedForGot[] =
81                 "gethostby*.gethostanswer: asked for \"%s\", got \"%s\"";
82
83 #ifdef RESOLVSORT
84 static void addrsort(char **, int, res_state);
85 #endif
86
87 #ifdef DEBUG
88 static void dbg_printf(char *, int, res_state) __printflike(1, 0);
89 #endif
90
91 #define MAXPACKET       (64*1024)
92
93 typedef union {
94     HEADER hdr;
95     u_char buf[MAXPACKET];
96 } querybuf;
97
98 typedef union {
99     int32_t al;
100     char ac;
101 } align;
102
103 int _dns_ttl_;
104
105 #ifdef DEBUG
106 static void
107 dbg_printf(char *msg, int num, res_state res)
108 {
109         if (res->options & RES_DEBUG) {
110                 int save = errno;
111
112                 printf(msg, num);
113                 errno = save;
114         }
115 }
116 #else
117 # define dbg_printf(msg, num, res) /*nada*/
118 #endif
119
120 #define BOUNDED_INCR(x) \
121         do { \
122                 cp += x; \
123                 if (cp > eom) { \
124                         RES_SET_H_ERRNO(statp, NO_RECOVERY); \
125                         return (-1); \
126                 } \
127         } while (0)
128
129 #define BOUNDS_CHECK(ptr, count) \
130         do { \
131                 if ((ptr) + (count) > eom) { \
132                         RES_SET_H_ERRNO(statp, NO_RECOVERY); \
133                         return (-1); \
134                 } \
135         } while (0)
136
137 static int
138 gethostanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
139               struct hostent *he, struct hostent_data *hed, res_state statp)
140 {
141         const HEADER *hp;
142         const u_char *cp;
143         int n;
144         const u_char *eom, *erdata;
145         char *bp, *ep, **ap, **hap;
146         int type, class, ancount, qdcount;
147         int haveanswer, had_error;
148         int toobig = 0;
149         char tbuf[MAXDNAME];
150         const char *tname;
151         int (*name_ok)(const char *);
152
153         tname = qname;
154         he->h_name = NULL;
155         eom = answer->buf + anslen;
156         switch (qtype) {
157         case T_A:
158         case T_AAAA:
159                 name_ok = res_hnok;
160                 break;
161         case T_PTR:
162                 name_ok = res_dnok;
163                 break;
164         default:
165                 RES_SET_H_ERRNO(statp, NO_RECOVERY);
166                 return (-1);    /* XXX should be abort(); */
167         }
168         /*
169          * find first satisfactory answer
170          */
171         hp = &answer->hdr;
172         ancount = ntohs(hp->ancount);
173         qdcount = ntohs(hp->qdcount);
174         bp = hed->hostbuf;
175         ep = hed->hostbuf + sizeof hed->hostbuf;
176         cp = answer->buf;
177         BOUNDED_INCR(HFIXEDSZ);
178         if (qdcount != 1) {
179                 RES_SET_H_ERRNO(statp, NO_RECOVERY);
180                 return (-1);
181         }
182         n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
183         if ((n < 0) || !(*name_ok)(bp)) {
184                 RES_SET_H_ERRNO(statp, NO_RECOVERY);
185                 return (-1);
186         }
187         BOUNDED_INCR(n + QFIXEDSZ);
188         if (qtype == T_A || qtype == T_AAAA) {
189                 /* res_send() has already verified that the query name is the
190                  * same as the one we sent; this just gets the expanded name
191                  * (i.e., with the succeeding search-domain tacked on).
192                  */
193                 n = strlen(bp) + 1;             /* for the \0 */
194                 if (n >= MAXHOSTNAMELEN) {
195                         RES_SET_H_ERRNO(statp, NO_RECOVERY);
196                         return (-1);
197                 }
198                 he->h_name = bp;
199                 bp += n;
200                 /* The qname can be abbreviated, but h_name is now absolute. */
201                 qname = he->h_name;
202         }
203         ap = hed->host_aliases;
204         *ap = NULL;
205         he->h_aliases = hed->host_aliases;
206         hap = hed->h_addr_ptrs;
207         *hap = NULL;
208         he->h_addr_list = hed->h_addr_ptrs;
209         haveanswer = 0;
210         had_error = 0;
211         _dns_ttl_ = -1;
212         while (ancount-- > 0 && cp < eom && !had_error) {
213                 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
214                 if ((n < 0) || !(*name_ok)(bp)) {
215                         had_error++;
216                         continue;
217                 }
218                 cp += n;                        /* name */
219                 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
220                 type = _getshort(cp);
221                 cp += INT16SZ;                  /* type */
222                 class = _getshort(cp);
223                 cp += INT16SZ;                  /* class */
224                 if (qtype == T_A  && type == T_A)
225                         _dns_ttl_ = _getlong(cp);
226                 cp += INT32SZ;                  /* TTL */
227                 n = _getshort(cp);
228                 cp += INT16SZ;                  /* len */
229                 BOUNDS_CHECK(cp, n);
230                 erdata = cp + n;
231                 if (class != C_IN) {
232                         /* XXX - debug? syslog? */
233                         cp += n;
234                         continue;               /* XXX - had_error++ ? */
235                 }
236                 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
237                         if (ap >= &hed->host_aliases[_MAXALIASES-1])
238                                 continue;
239                         n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
240                         if ((n < 0) || !(*name_ok)(tbuf)) {
241                                 had_error++;
242                                 continue;
243                         }
244                         cp += n;
245                         if (cp != erdata) {
246                                 RES_SET_H_ERRNO(statp, NO_RECOVERY);
247                                 return (-1);
248                         }
249                         /* Store alias. */
250                         *ap++ = bp;
251                         n = strlen(bp) + 1;     /* for the \0 */
252                         if (n >= MAXHOSTNAMELEN) {
253                                 had_error++;
254                                 continue;
255                         }
256                         bp += n;
257                         /* Get canonical name. */
258                         n = strlen(tbuf) + 1;   /* for the \0 */
259                         if (n > ep - bp || n >= MAXHOSTNAMELEN) {
260                                 had_error++;
261                                 continue;
262                         }
263                         strcpy(bp, tbuf);
264                         he->h_name = bp;
265                         bp += n;
266                         continue;
267                 }
268                 if (qtype == T_PTR && type == T_CNAME) {
269                         n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
270                         if (n < 0 || !res_dnok(tbuf)) {
271                                 had_error++;
272                                 continue;
273                         }
274                         cp += n;
275                         if (cp != erdata) {
276                                 RES_SET_H_ERRNO(statp, NO_RECOVERY);
277                                 return (-1);
278                         }
279                         /* Get canonical name. */
280                         n = strlen(tbuf) + 1;   /* for the \0 */
281                         if (n > ep - bp || n >= MAXHOSTNAMELEN) {
282                                 had_error++;
283                                 continue;
284                         }
285                         strcpy(bp, tbuf);
286                         tname = bp;
287                         bp += n;
288                         continue;
289                 }
290                 if (type != qtype) {
291                         if (type != T_SIG)
292                                 syslog(LOG_NOTICE|LOG_AUTH,
293         "gethostby*.gethostanswer: asked for \"%s %s %s\", got type \"%s\"",
294                                        qname, p_class(C_IN), p_type(qtype),
295                                        p_type(type));
296                         cp += n;
297                         continue;               /* XXX - had_error++ ? */
298                 }
299                 switch (type) {
300                 case T_PTR:
301                         if (strcasecmp(tname, bp) != 0) {
302                                 syslog(LOG_NOTICE|LOG_AUTH,
303                                        AskedForGot, qname, bp);
304                                 cp += n;
305                                 continue;       /* XXX - had_error++ ? */
306                         }
307                         n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
308                         if ((n < 0) || !res_hnok(bp)) {
309                                 had_error++;
310                                 break;
311                         }
312 #if MULTI_PTRS_ARE_ALIASES
313                         cp += n;
314                         if (cp != erdata) {
315                                 RES_SET_H_ERRNO(statp, NO_RECOVERY);
316                                 return (-1);
317                         }
318                         if (!haveanswer)
319                                 he->h_name = bp;
320                         else if (ap < &hed->host_aliases[_MAXALIASES-1])
321                                 *ap++ = bp;
322                         else
323                                 n = -1;
324                         if (n != -1) {
325                                 n = strlen(bp) + 1;     /* for the \0 */
326                                 if (n >= MAXHOSTNAMELEN) {
327                                         had_error++;
328                                         break;
329                                 }
330                                 bp += n;
331                         }
332                         break;
333 #else
334                         he->h_name = bp;
335                         if (statp->options & RES_USE_INET6) {
336                                 n = strlen(bp) + 1;     /* for the \0 */
337                                 if (n >= MAXHOSTNAMELEN) {
338                                         had_error++;
339                                         break;
340                                 }
341                                 bp += n;
342                                 _map_v4v6_hostent(he, &bp, ep);
343                         }
344                         RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
345                         return (0);
346 #endif
347                 case T_A:
348                 case T_AAAA:
349                         if (strcasecmp(he->h_name, bp) != 0) {
350                                 syslog(LOG_NOTICE|LOG_AUTH,
351                                        AskedForGot, he->h_name, bp);
352                                 cp += n;
353                                 continue;       /* XXX - had_error++ ? */
354                         }
355                         if (n != he->h_length) {
356                                 cp += n;
357                                 continue;
358                         }
359                         if (!haveanswer) {
360                                 int nn;
361
362                                 he->h_name = bp;
363                                 nn = strlen(bp) + 1;    /* for the \0 */
364                                 bp += nn;
365                         }
366
367                         bp += sizeof(align) - ((u_long)bp % sizeof(align));
368
369                         if (bp + n >= ep) {
370                                 dbg_printf("size (%d) too big\n", n, statp);
371                                 had_error++;
372                                 continue;
373                         }
374                         if (hap >= &hed->h_addr_ptrs[_MAXADDRS-1]) {
375                                 if (!toobig++)
376                                         dbg_printf("Too many addresses (%d)\n",
377                                                 _MAXADDRS, statp);
378                                 cp += n;
379                                 continue;
380                         }
381                         memcpy(*hap++ = bp, cp, n);
382                         bp += n;
383                         cp += n;
384                         if (cp != erdata) {
385                                 RES_SET_H_ERRNO(statp, NO_RECOVERY);
386                                 return (-1);
387                         }
388                         break;
389                 default:
390                         dbg_printf("Impossible condition (type=%d)\n", type,
391                             statp);
392                         RES_SET_H_ERRNO(statp, NO_RECOVERY);
393                         return (-1);
394                         /* BIND has abort() here, too risky on bad data */
395                 }
396                 if (!had_error)
397                         haveanswer++;
398         }
399         if (haveanswer) {
400                 *ap = NULL;
401                 *hap = NULL;
402 # if defined(RESOLVSORT)
403                 /*
404                  * Note: we sort even if host can take only one address
405                  * in its return structures - should give it the "best"
406                  * address in that case, not some random one
407                  */
408                 if (statp->nsort && haveanswer > 1 && qtype == T_A)
409                         addrsort(hed->h_addr_ptrs, haveanswer, statp);
410 # endif /*RESOLVSORT*/
411                 if (!he->h_name) {
412                         n = strlen(qname) + 1;  /* for the \0 */
413                         if (n > ep - bp || n >= MAXHOSTNAMELEN)
414                                 goto no_recovery;
415                         strcpy(bp, qname);
416                         he->h_name = bp;
417                         bp += n;
418                 }
419                 if (statp->options & RES_USE_INET6)
420                         _map_v4v6_hostent(he, &bp, ep);
421                 RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
422                 return (0);
423         }
424  no_recovery:
425         RES_SET_H_ERRNO(statp, NO_RECOVERY);
426         return (-1);
427 }
428
429 /* XXX: for async DNS resolver in ypserv */
430 struct hostent *
431 __dns_getanswer(const char *answer, int anslen, const char *qname, int qtype)
432 {
433         struct hostent *he;
434         struct hostent_data *hed;
435         int error;
436         res_state statp;
437
438         statp = __res_state();
439         if ((he = __hostent_init()) == NULL ||
440             (hed = __hostent_data_init()) == NULL) {
441                 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
442                 return (NULL);
443         }
444         switch (qtype) {
445         case T_AAAA:
446                 he->h_addrtype = AF_INET6;
447                 he->h_length = NS_IN6ADDRSZ;
448                 break;
449         case T_A:
450         default:
451                 he->h_addrtype = AF_INET;
452                 he->h_length = NS_INADDRSZ;
453                 break;
454         }
455
456         error = gethostanswer((const querybuf *)answer, anslen, qname, qtype,
457             he, hed, statp);
458         return (error == 0) ? he : NULL;
459 }
460
461 int
462 _dns_gethostbyname(void *rval, void *cb_data __unused, va_list ap)
463 {
464         const char *name;
465         int af;
466         char *buffer;
467         size_t buflen;
468         int *errnop __unused;
469         int *h_errnop;
470         struct hostent *hptr, he;
471         struct hostent_data *hed;
472         querybuf *buf;
473         int n, type, error;
474         res_state statp;
475
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 *);
483
484         *((struct hostent **)rval) = NULL;
485
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);
491         }
492
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);
508         }
509
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);
514         }
515         n = res_nsearch(statp, name, C_IN, type, buf->buf, sizeof(buf->buf));
516         if (n < 0) {
517                 free(buf);
518                 dbg_printf("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                 dbg_printf("static buffer is too small (%d)\n", n, statp);
524                 *h_errnop = statp->res_h_errno;
525                 return (0);
526         }
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);
532         }
533         if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
534                 *h_errnop = statp->res_h_errno;
535                 return (NS_NOTFOUND);
536         }
537         *((struct hostent **)rval) = hptr;
538         return (NS_SUCCESS);
539 }
540
541 int
542 _dns_gethostbyaddr(void *rval, void *cb_data __unused, va_list ap)
543 {
544         const void *addr;
545         socklen_t len;
546         int af;
547         char *buffer;
548         size_t buflen;
549         int *errnop __unused;
550         int *h_errnop;
551         const u_char *uaddr;
552         struct hostent *hptr, he;
553         struct hostent_data *hed;
554         int n;
555         querybuf *buf;
556         char qbuf[MAXDNAME+1], *qp;
557         res_state statp;
558 #ifdef SUNSECURITY
559         struct hostdata rhd;
560         struct hostent *rhe;
561         char **haddr;
562         u_long old_options;
563         char hname2[MAXDNAME+1], numaddr[46];
564         int ret_h_error;
565 #endif /*SUNSECURITY*/
566
567         addr = va_arg(ap, const void *);
568         len = va_arg(ap, socklen_t);
569         af = va_arg(ap, int);
570         hptr = va_arg(ap, struct hostent *);
571         buffer = va_arg(ap, char *);
572         buflen = va_arg(ap, size_t);
573         errnop = va_arg(ap, int *);
574         h_errnop = va_arg(ap, int *);
575         uaddr = (const u_char *)addr;
576
577         *((struct hostent **)rval) = NULL;
578
579         statp = __res_state();
580         if ((hed = __hostent_data_init()) == NULL) {
581                 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
582                 *h_errnop = statp->res_h_errno;
583                 return (NS_NOTFOUND);
584         }
585
586         switch (af) {
587         case AF_INET:
588                 sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
589                                (uaddr[3] & 0xff),
590                                (uaddr[2] & 0xff),
591                                (uaddr[1] & 0xff),
592                                (uaddr[0] & 0xff));
593                 break;
594         case AF_INET6:
595                 qp = qbuf;
596                 for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
597                         qp += SPRINTF((qp, "%x.%x.",
598                                        uaddr[n] & 0xf,
599                                        (uaddr[n] >> 4) & 0xf));
600                 }
601                 strlcat(qbuf, "ip6.arpa", sizeof(qbuf));
602                 break;
603         default:
604                 abort();
605         }
606         if ((buf = malloc(sizeof(*buf))) == NULL) {
607                 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
608                 *h_errnop = statp->res_h_errno;
609                 return NS_NOTFOUND;
610         }
611         n = res_nquery(statp, qbuf, C_IN, T_PTR, (u_char *)buf->buf,
612             sizeof buf->buf);
613         if (n < 0) {
614                 free(buf);
615                 dbg_printf("res_nquery failed (%d)\n", n, statp);
616                 *h_errnop = statp->res_h_errno;
617                 return (NS_UNAVAIL);
618         }
619         if (n > sizeof buf->buf) {
620                 free(buf);
621                 dbg_printf("static buffer is too small (%d)\n", n, statp);
622                 *h_errnop = statp->res_h_errno;
623                 return (NS_UNAVAIL);
624         }
625         if (gethostanswer(buf, n, qbuf, T_PTR, &he, hed, statp) != 0) {
626                 free(buf);
627                 *h_errnop = statp->res_h_errno;
628                 return (NS_NOTFOUND);   /* h_errno was set by gethostanswer() */
629         }
630         free(buf);
631 #ifdef SUNSECURITY
632         if (af == AF_INET) {
633             /*
634              * turn off search as the name should be absolute,
635              * 'localhost' should be matched by defnames
636              */
637             strncpy(hname2, he.h_name, MAXDNAME);
638             hname2[MAXDNAME] = '\0';
639             old_options = statp->options;
640             statp->options &= ~RES_DNSRCH;
641             statp->options |= RES_DEFNAMES;
642             memset(&rhd, 0, sizeof rhd);
643             rhe = gethostbyname_r(hname2, &rhd.host, &rhd.data,
644                 sizeof(rhd.data), &ret_h_error);
645             if (rhe == NULL) {
646                 if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
647                     strlcpy(numaddr, "UNKNOWN", sizeof(numaddr));
648                 syslog(LOG_NOTICE|LOG_AUTH,
649                        "gethostbyaddr: No A record for %s (verifying [%s])",
650                        hname2, numaddr);
651                 statp->options = old_options;
652                 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
653                 *h_errnop = statp->res_h_errno;
654                 return (NS_NOTFOUND);
655             }
656             statp->options = old_options;
657             for (haddr = rhe->h_addr_list; *haddr; haddr++)
658                 if (!memcmp(*haddr, addr, NS_INADDRSZ))
659                         break;
660             if (!*haddr) {
661                 if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
662                     strlcpy(numaddr, "UNKNOWN", sizeof(numaddr));
663                 syslog(LOG_NOTICE|LOG_AUTH,
664                        "gethostbyaddr: A record of %s != PTR record [%s]",
665                        hname2, numaddr);
666                 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
667                 *h_errnop = statp->res_h_errno;
668                 return (NS_NOTFOUND);
669             }
670         }
671 #endif /*SUNSECURITY*/
672         he.h_addrtype = af;
673         he.h_length = len;
674         memcpy(hed->host_addr, uaddr, len);
675         hed->h_addr_ptrs[0] = (char *)hed->host_addr;
676         hed->h_addr_ptrs[1] = NULL;
677         if (af == AF_INET && (statp->options & RES_USE_INET6)) {
678                 _map_v4v6_address((char*)hed->host_addr, (char*)hed->host_addr);
679                 he.h_addrtype = AF_INET6;
680                 he.h_length = NS_IN6ADDRSZ;
681         }
682         RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
683         if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
684                 *h_errnop = statp->res_h_errno;
685                 return (NS_NOTFOUND);
686         }
687         *((struct hostent **)rval) = hptr;
688         return (NS_SUCCESS);
689 }
690
691 #ifdef RESOLVSORT
692 static void
693 addrsort(char **ap, int num, res_state res)
694 {
695         int i, j;
696         char **p;
697         short aval[_MAXADDRS];
698         int needsort = 0;
699
700         p = ap;
701         for (i = 0; i < num; i++, p++) {
702             for (j = 0 ; (unsigned)j < res->nsort; j++)
703                 if (res->sort_list[j].addr.s_addr ==
704                     (((struct in_addr *)(*p))->s_addr & res->sort_list[j].mask))
705                         break;
706             aval[i] = j;
707             if (needsort == 0 && i > 0 && j < aval[i-1])
708                 needsort = i;
709         }
710         if (!needsort)
711             return;
712
713         while (needsort < num) {
714             for (j = needsort - 1; j >= 0; j--) {
715                 if (aval[j] > aval[j+1]) {
716                     char *hp;
717
718                     i = aval[j];
719                     aval[j] = aval[j+1];
720                     aval[j+1] = i;
721
722                     hp = ap[j];
723                     ap[j] = ap[j+1];
724                     ap[j+1] = hp;
725
726                 } else
727                     break;
728             }
729             needsort++;
730         }
731 }
732 #endif
733
734 void
735 _sethostdnsent(int stayopen)
736 {
737         res_state statp;
738
739         statp = __res_state();
740         if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1)
741                 return;
742         if (stayopen)
743                 statp->options |= RES_STAYOPEN | RES_USEVC;
744 }
745
746 void
747 _endhostdnsent(void)
748 {
749         res_state statp;
750
751         statp = __res_state();
752         statp->options &= ~(RES_STAYOPEN | RES_USEVC);
753         res_nclose(statp);
754 }