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