libc: Fix -Wunused-parameter.
[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  * 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.
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 dprintf(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 dprintf(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 dprintf(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                                 dprintf("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                                         dprintf("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                         dprintf("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, *h_errnop;
469         struct hostent *hptr, he;
470         struct hostent_data *hed;
471         querybuf *buf;
472         int n, type, error;
473         res_state statp;
474
475         name = va_arg(ap, const char *);
476         af = va_arg(ap, int);
477         hptr = va_arg(ap, struct hostent *);
478         buffer = va_arg(ap, char *);
479         buflen = va_arg(ap, size_t);
480         errnop = va_arg(ap, int *);
481         h_errnop = va_arg(ap, int *);
482
483         *((struct hostent **)rval) = NULL;
484
485         statp = __res_state();
486         if ((hed = __hostent_data_init()) == NULL) {
487                 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
488                 *h_errnop = statp->res_h_errno;
489                 return (NS_NOTFOUND);
490         }
491
492         he.h_addrtype = af;
493         switch (af) {
494         case AF_INET:
495                 he.h_length = NS_INADDRSZ;
496                 type = T_A;
497                 break;
498         case AF_INET6:
499                 he.h_length = NS_IN6ADDRSZ;
500                 type = T_AAAA;
501                 break;
502         default:
503                 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
504                 *h_errnop = statp->res_h_errno;
505                 errno = EAFNOSUPPORT;
506                 return (NS_UNAVAIL);
507         }
508
509         if ((buf = malloc(sizeof(*buf))) == NULL) {
510                 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
511                 *h_errnop = statp->res_h_errno;
512                 return (NS_NOTFOUND);
513         }
514         n = res_nsearch(statp, name, C_IN, type, buf->buf, sizeof(buf->buf));
515         if (n < 0) {
516                 free(buf);
517                 dprintf("res_nsearch failed (%d)\n", n, statp);
518                 *h_errnop = statp->res_h_errno;
519                 return (0);
520         } else if (n > sizeof(buf->buf)) {
521                 free(buf);
522                 dprintf("static buffer is too small (%d)\n", n, statp);
523                 *h_errnop = statp->res_h_errno;
524                 return (0);
525         }
526         error = gethostanswer(buf, n, name, type, &he, hed, statp);
527         free(buf);
528         if (error != 0) {
529                 *h_errnop = statp->res_h_errno;
530                 return (NS_NOTFOUND);
531         }
532         if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
533                 *h_errnop = statp->res_h_errno;
534                 return (NS_NOTFOUND);
535         }
536         *((struct hostent **)rval) = hptr;
537         return (NS_SUCCESS);
538 }
539
540 int
541 _dns_gethostbyaddr(void *rval, void *cb_data __unused, va_list ap)
542 {
543         const void *addr;
544         socklen_t len;
545         int af;
546         char *buffer;
547         size_t buflen;
548         int *errnop, *h_errnop;
549         const u_char *uaddr;
550         struct hostent *hptr, he;
551         struct hostent_data *hed;
552         int n;
553         querybuf *buf;
554         char qbuf[MAXDNAME+1], *qp;
555         res_state statp;
556 #ifdef SUNSECURITY
557         struct hostdata rhd;
558         struct hostent *rhe;
559         char **haddr;
560         u_long old_options;
561         char hname2[MAXDNAME+1], numaddr[46];
562         int ret_h_error;
563 #endif /*SUNSECURITY*/
564
565         addr = va_arg(ap, const void *);
566         len = va_arg(ap, socklen_t);
567         af = va_arg(ap, int);
568         hptr = va_arg(ap, struct hostent *);
569         buffer = va_arg(ap, char *);
570         buflen = va_arg(ap, size_t);
571         errnop = va_arg(ap, int *);
572         h_errnop = va_arg(ap, int *);
573         uaddr = (const u_char *)addr;
574
575         *((struct hostent **)rval) = NULL;
576
577         statp = __res_state();
578         if ((hed = __hostent_data_init()) == NULL) {
579                 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
580                 *h_errnop = statp->res_h_errno;
581                 return (NS_NOTFOUND);
582         }
583
584         switch (af) {
585         case AF_INET:
586                 sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
587                                (uaddr[3] & 0xff),
588                                (uaddr[2] & 0xff),
589                                (uaddr[1] & 0xff),
590                                (uaddr[0] & 0xff));
591                 break;
592         case AF_INET6:
593                 qp = qbuf;
594                 for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
595                         qp += SPRINTF((qp, "%x.%x.",
596                                        uaddr[n] & 0xf,
597                                        (uaddr[n] >> 4) & 0xf));
598                 }
599                 strlcat(qbuf, "ip6.arpa", sizeof(qbuf));
600                 break;
601         default:
602                 abort();
603         }
604         if ((buf = malloc(sizeof(*buf))) == NULL) {
605                 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
606                 *h_errnop = statp->res_h_errno;
607                 return NS_NOTFOUND;
608         }
609         n = res_nquery(statp, qbuf, C_IN, T_PTR, (u_char *)buf->buf,
610             sizeof buf->buf);
611         if (n < 0) {
612                 free(buf);
613                 dprintf("res_nquery failed (%d)\n", n, statp);
614                 *h_errnop = statp->res_h_errno;
615                 return (NS_UNAVAIL);
616         }
617         if (n > sizeof buf->buf) {
618                 free(buf);
619                 dprintf("static buffer is too small (%d)\n", n, statp);
620                 *h_errnop = statp->res_h_errno;
621                 return (NS_UNAVAIL);
622         }
623         if (gethostanswer(buf, n, qbuf, T_PTR, &he, hed, statp) != 0) {
624                 free(buf);
625                 *h_errnop = statp->res_h_errno;
626                 return (NS_NOTFOUND);   /* h_errno was set by gethostanswer() */
627         }
628         free(buf);
629 #ifdef SUNSECURITY
630         if (af == AF_INET) {
631             /*
632              * turn off search as the name should be absolute,
633              * 'localhost' should be matched by defnames
634              */
635             strncpy(hname2, he.h_name, MAXDNAME);
636             hname2[MAXDNAME] = '\0';
637             old_options = statp->options;
638             statp->options &= ~RES_DNSRCH;
639             statp->options |= RES_DEFNAMES;
640             memset(&rhd, 0, sizeof rhd);
641             rhe = gethostbyname_r(hname2, &rhd.host, &rhd.data,
642                 sizeof(rhd.data), &ret_h_error);
643             if (rhe == NULL) {
644                 if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
645                     strlcpy(numaddr, "UNKNOWN", sizeof(numaddr));
646                 syslog(LOG_NOTICE|LOG_AUTH,
647                        "gethostbyaddr: No A record for %s (verifying [%s])",
648                        hname2, numaddr);
649                 statp->options = old_options;
650                 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
651                 *h_errnop = statp->res_h_errno;
652                 return (NS_NOTFOUND);
653             }
654             statp->options = old_options;
655             for (haddr = rhe->h_addr_list; *haddr; haddr++)
656                 if (!memcmp(*haddr, addr, NS_INADDRSZ))
657                         break;
658             if (!*haddr) {
659                 if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
660                     strlcpy(numaddr, "UNKNOWN", sizeof(numaddr));
661                 syslog(LOG_NOTICE|LOG_AUTH,
662                        "gethostbyaddr: A record of %s != PTR record [%s]",
663                        hname2, numaddr);
664                 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
665                 *h_errnop = statp->res_h_errno;
666                 return (NS_NOTFOUND);
667             }
668         }
669 #endif /*SUNSECURITY*/
670         he.h_addrtype = af;
671         he.h_length = len;
672         memcpy(hed->host_addr, uaddr, len);
673         hed->h_addr_ptrs[0] = (char *)hed->host_addr;
674         hed->h_addr_ptrs[1] = NULL;
675         if (af == AF_INET && (statp->options & RES_USE_INET6)) {
676                 _map_v4v6_address((char*)hed->host_addr, (char*)hed->host_addr);
677                 he.h_addrtype = AF_INET6;
678                 he.h_length = NS_IN6ADDRSZ;
679         }
680         RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
681         if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
682                 *h_errnop = statp->res_h_errno;
683                 return (NS_NOTFOUND);
684         }
685         *((struct hostent **)rval) = hptr;
686         return (NS_SUCCESS);
687 }
688
689 #ifdef RESOLVSORT
690 static void
691 addrsort(char **ap, int num, res_state res)
692 {
693         int i, j;
694         char **p;
695         short aval[_MAXADDRS];
696         int needsort = 0;
697
698         p = ap;
699         for (i = 0; i < num; i++, p++) {
700             for (j = 0 ; (unsigned)j < res->nsort; j++)
701                 if (res->sort_list[j].addr.s_addr ==
702                     (((struct in_addr *)(*p))->s_addr & res->sort_list[j].mask))
703                         break;
704             aval[i] = j;
705             if (needsort == 0 && i > 0 && j < aval[i-1])
706                 needsort = i;
707         }
708         if (!needsort)
709             return;
710
711         while (needsort < num) {
712             for (j = needsort - 1; j >= 0; j--) {
713                 if (aval[j] > aval[j+1]) {
714                     char *hp;
715
716                     i = aval[j];
717                     aval[j] = aval[j+1];
718                     aval[j+1] = i;
719
720                     hp = ap[j];
721                     ap[j] = ap[j+1];
722                     ap[j+1] = hp;
723
724                 } else
725                     break;
726             }
727             needsort++;
728         }
729 }
730 #endif
731
732 void
733 _sethostdnsent(int stayopen)
734 {
735         res_state statp;
736
737         statp = __res_state();
738         if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1)
739                 return;
740         if (stayopen)
741                 statp->options |= RES_STAYOPEN | RES_USEVC;
742 }
743
744 void
745 _endhostdnsent(void)
746 {
747         res_state statp;
748
749         statp = __res_state();
750         statp->options &= ~(RES_STAYOPEN | RES_USEVC);
751         res_nclose(statp);
752 }