Don't use removed rpc headers
[jlayton/glibc.git] / resolv / gethnamaddr.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
52 #if defined(LIBC_SCCS) && !defined(lint)
53 static char sccsid[] = "@(#)gethostnamadr.c     8.1 (Berkeley) 6/4/93";
54 #endif /* LIBC_SCCS and not lint */
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 <netdb.h>
65 #include <resolv.h>
66 #include <ctype.h>
67 #include <errno.h>
68 #include <syslog.h>
69
70 #define RESOLVSORT
71
72 #ifndef LOG_AUTH
73 # define LOG_AUTH 0
74 #endif
75
76 #define MULTI_PTRS_ARE_ALIASES 1        /* XXX - experimental */
77
78 #if defined(BSD) && (BSD >= 199103) && defined(AF_INET6)
79 # include <stdlib.h>
80 # include <string.h>
81 #else
82 # include "../conf/portability.h"
83 #endif
84
85 #if defined(USE_OPTIONS_H)
86 # include <../conf/options.h>
87 #endif
88
89 #ifdef SPRINTF_CHAR
90 # define SPRINTF(x) strlen(sprintf/**/x)
91 #else
92 # define SPRINTF(x) ((size_t)sprintf x)
93 #endif
94
95 #define MAXALIASES      35
96 #define MAXADDRS        35
97
98 static const char AskedForGot[] =
99                           "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
100
101 static char *h_addr_ptrs[MAXADDRS + 1];
102
103 static struct hostent host;
104 static char *host_aliases[MAXALIASES];
105 static char hostbuf[8*1024];
106 static u_char host_addr[16];    /* IPv4 or IPv6 */
107 static FILE *hostf = NULL;
108 static int stayopen = 0;
109
110 static void map_v4v6_address (const char *src, char *dst) __THROW;
111 static void map_v4v6_hostent (struct hostent *hp, char **bp, int *len) __THROW;
112
113 #ifdef RESOLVSORT
114 extern void addrsort (char **, int) __THROW;
115 #endif
116
117 #if PACKETSZ > 65536
118 #define MAXPACKET       PACKETSZ
119 #else
120 #define MAXPACKET       65536
121 #endif
122
123 /* As per RFC 1034 and 1035 a host name cannot exceed 255 octets in length.  */
124 #ifdef MAXHOSTNAMELEN
125 # undef MAXHOSTNAMELEN
126 #endif
127 #define MAXHOSTNAMELEN 256
128
129 typedef union {
130     HEADER hdr;
131     u_char buf[MAXPACKET];
132 } querybuf;
133
134 typedef union {
135     int32_t al;
136     char ac;
137 } align;
138
139 #ifndef h_errno
140 extern int h_errno;
141 #endif
142
143 #ifdef DEBUG
144 static void
145 Dprintf(msg, num)
146         char *msg;
147         int num;
148 {
149         if (_res.options & RES_DEBUG) {
150                 int save = errno;
151
152                 printf(msg, num);
153                 __set_errno (save);
154         }
155 }
156 #else
157 # define Dprintf(msg, num) /*nada*/
158 #endif
159
160 #define BOUNDED_INCR(x) \
161         do { \
162                 cp += x; \
163                 if (cp > eom) { \
164                         __set_h_errno (NO_RECOVERY); \
165                         return (NULL); \
166                 } \
167         } while (0)
168
169 #define BOUNDS_CHECK(ptr, count) \
170         do { \
171                 if ((ptr) + (count) > eom) { \
172                         __set_h_errno (NO_RECOVERY); \
173                         return (NULL); \
174                 } \
175         } while (0)
176
177
178 static struct hostent *
179 getanswer (const querybuf *answer, int anslen, const char *qname, int qtype)
180 {
181         register const HEADER *hp;
182         register const u_char *cp;
183         register int n;
184         const u_char *eom, *erdata;
185         char *bp, **ap, **hap;
186         int type, class, buflen, ancount, qdcount;
187         int haveanswer, had_error;
188         int toobig = 0;
189         char tbuf[MAXDNAME];
190         const char *tname;
191         int (*name_ok) (const char *);
192
193         tname = qname;
194         host.h_name = NULL;
195         eom = answer->buf + anslen;
196         switch (qtype) {
197         case T_A:
198         case T_AAAA:
199                 name_ok = res_hnok;
200                 break;
201         case T_PTR:
202                 name_ok = res_dnok;
203                 break;
204         default:
205                 return (NULL);  /* XXX should be abort(); */
206         }
207         /*
208          * find first satisfactory answer
209          */
210         hp = &answer->hdr;
211         ancount = ntohs(hp->ancount);
212         qdcount = ntohs(hp->qdcount);
213         bp = hostbuf;
214         buflen = sizeof hostbuf;
215         cp = answer->buf;
216         BOUNDED_INCR(HFIXEDSZ);
217         if (qdcount != 1) {
218                 __set_h_errno (NO_RECOVERY);
219                 return (NULL);
220         }
221         n = dn_expand(answer->buf, eom, cp, bp, buflen);
222         if ((n < 0) || !(*name_ok)(bp)) {
223                 __set_h_errno (NO_RECOVERY);
224                 return (NULL);
225         }
226         BOUNDED_INCR(n + QFIXEDSZ);
227         if (qtype == T_A || qtype == T_AAAA) {
228                 /* res_send() has already verified that the query name is the
229                  * same as the one we sent; this just gets the expanded name
230                  * (i.e., with the succeeding search-domain tacked on).
231                  */
232                 n = strlen(bp) + 1;             /* for the \0 */
233                 if (n >= MAXHOSTNAMELEN) {
234                         __set_h_errno (NO_RECOVERY);
235                         return (NULL);
236                 }
237                 host.h_name = bp;
238                 bp += n;
239                 buflen -= n;
240                 /* The qname can be abbreviated, but h_name is now absolute. */
241                 qname = host.h_name;
242         }
243         ap = host_aliases;
244         *ap = NULL;
245         host.h_aliases = host_aliases;
246         hap = h_addr_ptrs;
247         *hap = NULL;
248         host.h_addr_list = h_addr_ptrs;
249         haveanswer = 0;
250         had_error = 0;
251         while (ancount-- > 0 && cp < eom && !had_error) {
252                 n = dn_expand(answer->buf, eom, cp, bp, buflen);
253                 if ((n < 0) || !(*name_ok)(bp)) {
254                         had_error++;
255                         continue;
256                 }
257                 cp += n;                        /* name */
258                 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
259                 type = ns_get16(cp);
260                 cp += INT16SZ;                  /* type */
261                 class = ns_get16(cp);
262                 cp += INT16SZ + INT32SZ;        /* class, TTL */
263                 n = ns_get16(cp);
264                 cp += INT16SZ;                  /* len */
265                 BOUNDS_CHECK(cp, n);
266                 erdata = cp + n;
267                 if (class != C_IN) {
268                         /* XXX - debug? syslog? */
269                         cp += n;
270                         continue;               /* XXX - had_error++ ? */
271                 }
272                 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
273                         if (ap >= &host_aliases[MAXALIASES-1])
274                                 continue;
275                         n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
276                         if ((n < 0) || !(*name_ok)(tbuf)) {
277                                 had_error++;
278                                 continue;
279                         }
280                         cp += n;
281                         if (cp != erdata) {
282                                 __set_h_errno (NO_RECOVERY);
283                                 return (NULL);
284                         }
285                         /* Store alias. */
286                         *ap++ = bp;
287                         n = strlen(bp) + 1;     /* for the \0 */
288                         if (n >= MAXHOSTNAMELEN) {
289                                 had_error++;
290                                 continue;
291                         }
292                         bp += n;
293                         buflen -= n;
294                         /* Get canonical name. */
295                         n = strlen(tbuf) + 1;   /* for the \0 */
296                         if (n > buflen || n >= MAXHOSTNAMELEN) {
297                                 had_error++;
298                                 continue;
299                         }
300                         strcpy(bp, tbuf);
301                         host.h_name = bp;
302                         bp += n;
303                         buflen -= n;
304                         continue;
305                 }
306                 if (qtype == T_PTR && type == T_CNAME) {
307                         n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
308                         if (n < 0 || !res_dnok(tbuf)) {
309                                 had_error++;
310                                 continue;
311                         }
312                         cp += n;
313                         if (cp != erdata) {
314                                 __set_h_errno (NO_RECOVERY);
315                                 return (NULL);
316                         }
317                         /* Get canonical name. */
318                         n = strlen(tbuf) + 1;   /* for the \0 */
319                         if (n > buflen || n >= MAXHOSTNAMELEN) {
320                                 had_error++;
321                                 continue;
322                         }
323                         strcpy(bp, tbuf);
324                         tname = bp;
325                         bp += n;
326                         buflen -= n;
327                         continue;
328                 }
329                 if ((type == T_SIG) || (type == T_KEY) || (type == T_NXT)) {
330                         /* We don't support DNSSEC yet.  For now, ignore
331                          * the record and send a low priority message
332                          * to syslog.
333                          */
334                         syslog(LOG_DEBUG|LOG_AUTH,
335                "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
336                                qname, p_class(C_IN), p_type(qtype),
337                                p_type(type));
338                         cp += n;
339                         continue;
340                 }
341                 if (type != qtype) {
342                         syslog(LOG_NOTICE|LOG_AUTH,
343                "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
344                                qname, p_class(C_IN), p_type(qtype),
345                                p_type(type));
346                         cp += n;
347                         continue;               /* XXX - had_error++ ? */
348                 }
349                 switch (type) {
350                 case T_PTR:
351                         if (strcasecmp(tname, bp) != 0) {
352                                 syslog(LOG_NOTICE|LOG_AUTH,
353                                        AskedForGot, qname, bp);
354                                 cp += n;
355                                 continue;       /* XXX - had_error++ ? */
356                         }
357                         n = dn_expand(answer->buf, eom, cp, bp, buflen);
358                         if ((n < 0) || !res_hnok(bp)) {
359                                 had_error++;
360                                 break;
361                         }
362 #if MULTI_PTRS_ARE_ALIASES
363                         cp += n;
364                         if (cp != erdata) {
365                                 __set_h_errno (NO_RECOVERY);
366                                 return (NULL);
367                         }
368                         if (!haveanswer)
369                                 host.h_name = bp;
370                         else if (ap < &host_aliases[MAXALIASES-1])
371                                 *ap++ = bp;
372                         else
373                                 n = -1;
374                         if (n != -1) {
375                                 n = strlen(bp) + 1;     /* for the \0 */
376                                 if (n >= MAXHOSTNAMELEN) {
377                                         had_error++;
378                                         break;
379                                 }
380                                 bp += n;
381                                 buflen -= n;
382                         }
383                         break;
384 #else
385                         host.h_name = bp;
386                         if (_res.options & RES_USE_INET6) {
387                                 n = strlen(bp) + 1;     /* for the \0 */
388                                 if (n >= MAXHOSTNAMELEN) {
389                                         had_error++;
390                                         break;
391                                 }
392                                 bp += n;
393                                 buflen -= n;
394                                 map_v4v6_hostent(&host, &bp, &buflen);
395                         }
396                         __set_h_errno (NETDB_SUCCESS);
397                         return (&host);
398 #endif
399                 case T_A:
400                 case T_AAAA:
401                         if (strcasecmp(host.h_name, bp) != 0) {
402                                 syslog(LOG_NOTICE|LOG_AUTH,
403                                        AskedForGot, host.h_name, bp);
404                                 cp += n;
405                                 continue;       /* XXX - had_error++ ? */
406                         }
407                         if (n != host.h_length) {
408                                 cp += n;
409                                 continue;
410                         }
411                         if (!haveanswer) {
412                                 register int nn;
413
414                                 host.h_name = bp;
415                                 nn = strlen(bp) + 1;    /* for the \0 */
416                                 bp += nn;
417                                 buflen -= nn;
418                         }
419
420                         /* XXX: when incrementing bp, we have to decrement
421                          * buflen by the same amount --okir */
422                         buflen -= sizeof(align) - ((u_long)bp % sizeof(align));
423
424                         bp += sizeof(align) - ((u_long)bp % sizeof(align));
425
426                         if (bp + n >= &hostbuf[sizeof hostbuf]) {
427                                 Dprintf("size (%d) too big\n", n);
428                                 had_error++;
429                                 continue;
430                         }
431                         if (hap >= &h_addr_ptrs[MAXADDRS-1]) {
432                                 if (!toobig++) {
433                                         Dprintf("Too many addresses (%d)\n",
434                                                 MAXADDRS);
435                                 }
436                                 cp += n;
437                                 continue;
438                         }
439                         memmove(*hap++ = bp, cp, n);
440                         bp += n;
441                         buflen -= n;
442                         cp += n;
443                         if (cp != erdata) {
444                                 __set_h_errno (NO_RECOVERY);
445                                 return (NULL);
446                         }
447                         break;
448                 default:
449                         abort();
450                 }
451                 if (!had_error)
452                         haveanswer++;
453         }
454         if (haveanswer) {
455                 *ap = NULL;
456                 *hap = NULL;
457 # if defined(RESOLVSORT)
458                 /*
459                  * Note: we sort even if host can take only one address
460                  * in its return structures - should give it the "best"
461                  * address in that case, not some random one
462                  */
463                 if (_res.nsort && haveanswer > 1 && qtype == T_A)
464                         addrsort(h_addr_ptrs, haveanswer);
465 # endif /*RESOLVSORT*/
466                 if (!host.h_name) {
467                         n = strlen(qname) + 1;  /* for the \0 */
468                         if (n > buflen || n >= MAXHOSTNAMELEN)
469                                 goto no_recovery;
470                         strcpy(bp, qname);
471                         host.h_name = bp;
472                         bp += n;
473                         buflen -= n;
474                 }
475                 if (_res.options & RES_USE_INET6)
476                         map_v4v6_hostent(&host, &bp, &buflen);
477                 __set_h_errno (NETDB_SUCCESS);
478                 return (&host);
479         }
480  no_recovery:
481         __set_h_errno (NO_RECOVERY);
482         return (NULL);
483 }
484
485 extern struct hostent *gethostbyname2(const char *name, int af);
486 libresolv_hidden_proto (gethostbyname2)
487
488 struct hostent *
489 gethostbyname(name)
490         const char *name;
491 {
492         struct hostent *hp;
493
494         if (__res_maybe_init (&_res, 0) == -1) {
495                 __set_h_errno (NETDB_INTERNAL);
496                 return (NULL);
497         }
498         if (_res.options & RES_USE_INET6) {
499                 hp = gethostbyname2(name, AF_INET6);
500                 if (hp)
501                         return (hp);
502         }
503         return (gethostbyname2(name, AF_INET));
504 }
505
506 struct hostent *
507 gethostbyname2(name, af)
508         const char *name;
509         int af;
510 {
511         union
512         {
513           querybuf *buf;
514           u_char *ptr;
515         } buf;
516         querybuf *origbuf;
517         register const char *cp;
518         char *bp;
519         int n, size, type, len;
520         struct hostent *ret;
521
522         if (__res_maybe_init (&_res, 0) == -1) {
523                 __set_h_errno (NETDB_INTERNAL);
524                 return (NULL);
525         }
526
527         switch (af) {
528         case AF_INET:
529                 size = INADDRSZ;
530                 type = T_A;
531                 break;
532         case AF_INET6:
533                 size = IN6ADDRSZ;
534                 type = T_AAAA;
535                 break;
536         default:
537                 __set_h_errno (NETDB_INTERNAL);
538                 __set_errno (EAFNOSUPPORT);
539                 return (NULL);
540         }
541
542         host.h_addrtype = af;
543         host.h_length = size;
544
545         /*
546          * if there aren't any dots, it could be a user-level alias.
547          * this is also done in res_query() since we are not the only
548          * function that looks up host names.
549          */
550         if (!strchr(name, '.') && (cp = __hostalias(name)))
551                 name = cp;
552
553         /*
554          * disallow names consisting only of digits/dots, unless
555          * they end in a dot.
556          */
557         if (isdigit(name[0]))
558                 for (cp = name;; ++cp) {
559                         if (!*cp) {
560                                 if (*--cp == '.')
561                                         break;
562                                 /*
563                                  * All-numeric, no dot at the end.
564                                  * Fake up a hostent as if we'd actually
565                                  * done a lookup.
566                                  */
567                                 if (inet_pton(af, name, host_addr) <= 0) {
568                                         __set_h_errno (HOST_NOT_FOUND);
569                                         return (NULL);
570                                 }
571                                 strncpy(hostbuf, name, MAXDNAME);
572                                 hostbuf[MAXDNAME] = '\0';
573                                 bp = hostbuf + MAXDNAME;
574                                 len = sizeof hostbuf - MAXDNAME;
575                                 host.h_name = hostbuf;
576                                 host.h_aliases = host_aliases;
577                                 host_aliases[0] = NULL;
578                                 h_addr_ptrs[0] = (char *)host_addr;
579                                 h_addr_ptrs[1] = NULL;
580                                 host.h_addr_list = h_addr_ptrs;
581                                 if (_res.options & RES_USE_INET6)
582                                         map_v4v6_hostent(&host, &bp, &len);
583                                 __set_h_errno (NETDB_SUCCESS);
584                                 return (&host);
585                         }
586                         if (!isdigit(*cp) && *cp != '.')
587                                 break;
588                }
589         if ((isxdigit(name[0]) && strchr(name, ':') != NULL) ||
590             name[0] == ':')
591                 for (cp = name;; ++cp) {
592                         if (!*cp) {
593                                 if (*--cp == '.')
594                                         break;
595                                 /*
596                                  * All-IPv6-legal, no dot at the end.
597                                  * Fake up a hostent as if we'd actually
598                                  * done a lookup.
599                                  */
600                                 if (inet_pton(af, name, host_addr) <= 0) {
601                                         __set_h_errno (HOST_NOT_FOUND);
602                                         return (NULL);
603                                 }
604                                 strncpy(hostbuf, name, MAXDNAME);
605                                 hostbuf[MAXDNAME] = '\0';
606                                 bp = hostbuf + MAXDNAME;
607                                 len = sizeof hostbuf - MAXDNAME;
608                                 host.h_name = hostbuf;
609                                 host.h_aliases = host_aliases;
610                                 host_aliases[0] = NULL;
611                                 h_addr_ptrs[0] = (char *)host_addr;
612                                 h_addr_ptrs[1] = NULL;
613                                 host.h_addr_list = h_addr_ptrs;
614                                 __set_h_errno (NETDB_SUCCESS);
615                                 return (&host);
616                         }
617                         if (!isxdigit(*cp) && *cp != ':' && *cp != '.')
618                                 break;
619                 }
620
621         buf.buf = origbuf = (querybuf *) alloca (1024);
622
623         if ((n = __libc_res_nsearch(&_res, name, C_IN, type, buf.buf->buf, 1024,
624                                     &buf.ptr, NULL, NULL, NULL)) < 0) {
625                 if (buf.buf != origbuf)
626                         free (buf.buf);
627                 Dprintf("res_nsearch failed (%d)\n", n);
628                 if (errno == ECONNREFUSED)
629                         return (_gethtbyname2(name, af));
630                 return (NULL);
631         }
632         ret = getanswer(buf.buf, n, name, type);
633         if (buf.buf != origbuf)
634                 free (buf.buf);
635         return ret;
636 }
637 libresolv_hidden_def (gethostbyname2)
638
639 struct hostent *
640 gethostbyaddr(addr, len, af)
641         const void *addr;
642         socklen_t len;
643         int af;
644 {
645         const u_char *uaddr = (const u_char *)addr;
646         static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
647         static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
648         int n;
649         socklen_t size;
650         union
651         {
652           querybuf *buf;
653           u_char *ptr;
654         } buf;
655         querybuf *orig_buf;
656         register struct hostent *hp;
657         char qbuf[MAXDNAME+1], *qp = NULL;
658 #ifdef SUNSECURITY
659         register struct hostent *rhp;
660         char **haddr;
661         u_long old_options;
662         char hname2[MAXDNAME+1];
663 #endif /*SUNSECURITY*/
664
665         if (__res_maybe_init (&_res, 0) == -1) {
666                 __set_h_errno (NETDB_INTERNAL);
667                 return (NULL);
668         }
669         if (af == AF_INET6 && len == IN6ADDRSZ &&
670             (!bcmp(uaddr, mapped, sizeof mapped) ||
671              !bcmp(uaddr, tunnelled, sizeof tunnelled))) {
672                 /* Unmap. */
673                 addr += sizeof mapped;
674                 uaddr += sizeof mapped;
675                 af = AF_INET;
676                 len = INADDRSZ;
677         }
678         switch (af) {
679         case AF_INET:
680                 size = INADDRSZ;
681                 break;
682         case AF_INET6:
683                 size = IN6ADDRSZ;
684                 break;
685         default:
686                 __set_errno (EAFNOSUPPORT);
687                 __set_h_errno (NETDB_INTERNAL);
688                 return (NULL);
689         }
690         if (size != len) {
691                 __set_errno (EINVAL);
692                 __set_h_errno (NETDB_INTERNAL);
693                 return (NULL);
694         }
695         switch (af) {
696         case AF_INET:
697                 (void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
698                                (uaddr[3] & 0xff),
699                                (uaddr[2] & 0xff),
700                                (uaddr[1] & 0xff),
701                                (uaddr[0] & 0xff));
702                 break;
703         case AF_INET6:
704                 qp = qbuf;
705                 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
706                         qp += SPRINTF((qp, "%x.%x.",
707                                        uaddr[n] & 0xf,
708                                        (uaddr[n] >> 4) & 0xf));
709                 }
710                 strcpy(qp, "ip6.arpa");
711                 break;
712         default:
713                 abort();
714         }
715
716         buf.buf = orig_buf = (querybuf *) alloca (1024);
717
718         n = __libc_res_nquery(&_res, qbuf, C_IN, T_PTR, buf.buf->buf, 1024,
719                               &buf.ptr, NULL, NULL, NULL);
720         if (n < 0 && af == AF_INET6 && (_res.options & RES_NOIP6DOTINT) == 0) {
721                 strcpy(qp, "ip6.int");
722                 n = __libc_res_nquery(&_res, qbuf, C_IN, T_PTR, buf.buf->buf,
723                                       buf.buf != orig_buf ? MAXPACKET : 1024,
724                                       &buf.ptr, NULL, NULL, NULL);
725         }
726         if (n < 0) {
727                 if (buf.buf != orig_buf)
728                         free (buf.buf);
729                 Dprintf("res_nquery failed (%d)\n", n);
730                 if (errno == ECONNREFUSED)
731                         return (_gethtbyaddr(addr, len, af));
732                 return (NULL);
733         }
734         hp = getanswer(buf.buf, n, qbuf, T_PTR);
735         if (buf.buf != orig_buf)
736                 free (buf.buf);
737         if (!hp)
738                 return (NULL);  /* h_errno was set by getanswer() */
739 #ifdef SUNSECURITY
740         if (af == AF_INET) {
741             /*
742              * turn off search as the name should be absolute,
743              * 'localhost' should be matched by defnames
744              */
745             strncpy(hname2, hp->h_name, MAXDNAME);
746             hname2[MAXDNAME] = '\0';
747             old_options = _res.options;
748             _res.options &= ~RES_DNSRCH;
749             _res.options |= RES_DEFNAMES;
750             if (!(rhp = gethostbyname(hname2))) {
751                 syslog(LOG_NOTICE|LOG_AUTH,
752                        "gethostbyaddr: No A record for %s (verifying [%s])",
753                        hname2, inet_ntoa(*((struct in_addr *)addr)));
754                 _res.options = old_options;
755                 __set_h_errno (HOST_NOT_FOUND);
756                 return (NULL);
757             }
758             _res.options = old_options;
759             for (haddr = rhp->h_addr_list; *haddr; haddr++)
760                 if (!memcmp(*haddr, addr, INADDRSZ))
761                         break;
762             if (!*haddr) {
763                 syslog(LOG_NOTICE|LOG_AUTH,
764                        "gethostbyaddr: A record of %s != PTR record [%s]",
765                        hname2, inet_ntoa(*((struct in_addr *)addr)));
766                 __set_h_errno (HOST_NOT_FOUND);
767                 return (NULL);
768             }
769         }
770 #endif /*SUNSECURITY*/
771         hp->h_addrtype = af;
772         hp->h_length = len;
773         memmove(host_addr, addr, len);
774         h_addr_ptrs[0] = (char *)host_addr;
775         h_addr_ptrs[1] = NULL;
776         if (af == AF_INET && (_res.options & RES_USE_INET6)) {
777                 map_v4v6_address((char*)host_addr, (char*)host_addr);
778                 hp->h_addrtype = AF_INET6;
779                 hp->h_length = IN6ADDRSZ;
780         }
781         __set_h_errno (NETDB_SUCCESS);
782         return (hp);
783 }
784
785 void
786 _sethtent(f)
787         int f;
788 {
789         if (!hostf)
790                 hostf = fopen(_PATH_HOSTS, "r" );
791         else
792                 rewind(hostf);
793         stayopen = f;
794 }
795 libresolv_hidden_def (_sethtent)
796
797 void
798 _endhtent()
799 {
800         if (hostf && !stayopen) {
801                 (void) fclose(hostf);
802                 hostf = NULL;
803         }
804 }
805
806 struct hostent *
807 _gethtent()
808 {
809         char *p;
810         register char *cp, **q;
811         int af, len;
812
813         if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) {
814                 __set_h_errno (NETDB_INTERNAL);
815                 return (NULL);
816         }
817  again:
818         if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) {
819                 __set_h_errno (HOST_NOT_FOUND);
820                 return (NULL);
821         }
822         if (*p == '#')
823                 goto again;
824         if (!(cp = strpbrk(p, "#\n")))
825                 goto again;
826         *cp = '\0';
827         if (!(cp = strpbrk(p, " \t")))
828                 goto again;
829         *cp++ = '\0';
830         if (inet_pton(AF_INET6, p, host_addr) > 0) {
831                 af = AF_INET6;
832                 len = IN6ADDRSZ;
833         } else if (inet_pton(AF_INET, p, host_addr) > 0) {
834                 if (_res.options & RES_USE_INET6) {
835                         map_v4v6_address((char*)host_addr, (char*)host_addr);
836                         af = AF_INET6;
837                         len = IN6ADDRSZ;
838                 } else {
839                         af = AF_INET;
840                         len = INADDRSZ;
841                 }
842         } else {
843                 goto again;
844         }
845         h_addr_ptrs[0] = (char *)host_addr;
846         h_addr_ptrs[1] = NULL;
847         host.h_addr_list = h_addr_ptrs;
848         host.h_length = len;
849         host.h_addrtype = af;
850         while (*cp == ' ' || *cp == '\t')
851                 cp++;
852         host.h_name = cp;
853         q = host.h_aliases = host_aliases;
854         if ((cp = strpbrk(cp, " \t")))
855                 *cp++ = '\0';
856         while (cp && *cp) {
857                 if (*cp == ' ' || *cp == '\t') {
858                         cp++;
859                         continue;
860                 }
861                 if (q < &host_aliases[MAXALIASES - 1])
862                         *q++ = cp;
863                 if ((cp = strpbrk(cp, " \t")))
864                         *cp++ = '\0';
865         }
866         *q = NULL;
867         __set_h_errno (NETDB_SUCCESS);
868         return (&host);
869 }
870 libresolv_hidden_def (_gethtent)
871
872 struct hostent *
873 _gethtbyname(name)
874         const char *name;
875 {
876         struct hostent *hp;
877
878         if (_res.options & RES_USE_INET6) {
879                 hp = _gethtbyname2(name, AF_INET6);
880                 if (hp)
881                         return (hp);
882         }
883         return (_gethtbyname2(name, AF_INET));
884 }
885
886 struct hostent *
887 _gethtbyname2(name, af)
888         const char *name;
889         int af;
890 {
891         register struct hostent *p;
892         register char **cp;
893
894         _sethtent(0);
895         while ((p = _gethtent())) {
896                 if (p->h_addrtype != af)
897                         continue;
898                 if (strcasecmp(p->h_name, name) == 0)
899                         break;
900                 for (cp = p->h_aliases; *cp != 0; cp++)
901                         if (strcasecmp(*cp, name) == 0)
902                                 goto found;
903         }
904  found:
905         _endhtent();
906         return (p);
907 }
908 libresolv_hidden_def (_gethtbyname2)
909
910 struct hostent *
911 _gethtbyaddr(addr, len, af)
912         const char *addr;
913         size_t len;
914         int af;
915 {
916         register struct hostent *p;
917
918         _sethtent(0);
919         while ((p = _gethtent()))
920                 if (p->h_addrtype == af && !bcmp(p->h_addr, addr, len))
921                         break;
922         _endhtent();
923         return (p);
924 }
925 libresolv_hidden_def (_gethtbyaddr)
926
927 static void
928 map_v4v6_address(src, dst)
929         const char *src;
930         char *dst;
931 {
932         u_char *p = (u_char *)dst;
933         char tmp[INADDRSZ];
934         int i;
935
936         /* Stash a temporary copy so our caller can update in place. */
937         memcpy(tmp, src, INADDRSZ);
938         /* Mark this ipv6 addr as a mapped ipv4. */
939         for (i = 0; i < 10; i++)
940                 *p++ = 0x00;
941         *p++ = 0xff;
942         *p++ = 0xff;
943         /* Retrieve the saved copy and we're done. */
944         memcpy((void*)p, tmp, INADDRSZ);
945 }
946
947 static void
948 map_v4v6_hostent(hp, bpp, lenp)
949         struct hostent *hp;
950         char **bpp;
951         int *lenp;
952 {
953         char **ap;
954
955         if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
956                 return;
957         hp->h_addrtype = AF_INET6;
958         hp->h_length = IN6ADDRSZ;
959         for (ap = hp->h_addr_list; *ap; ap++) {
960                 int i = sizeof(align) - ((u_long)*bpp % sizeof(align));
961
962                 if (*lenp < (i + IN6ADDRSZ)) {
963                         /* Out of memory.  Truncate address list here.  XXX */
964                         *ap = NULL;
965                         return;
966                 }
967                 *bpp += i;
968                 *lenp -= i;
969                 map_v4v6_address(*ap, *bpp);
970                 *ap = *bpp;
971                 *bpp += IN6ADDRSZ;
972                 *lenp -= IN6ADDRSZ;
973         }
974 }
975
976 #ifdef RESOLVSORT
977 extern void
978 addrsort(ap, num)
979         char **ap;
980         int num;
981 {
982         int i, j;
983         char **p;
984         short aval[MAXADDRS];
985         int needsort = 0;
986
987         p = ap;
988         for (i = 0; i < num; i++, p++) {
989             for (j = 0 ; (unsigned)j < _res.nsort; j++)
990                 if (_res.sort_list[j].addr.s_addr ==
991                     (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
992                         break;
993             aval[i] = j;
994             if (needsort == 0 && i > 0 && j < aval[i-1])
995                 needsort = i;
996         }
997         if (!needsort)
998             return;
999
1000         while (needsort < num) {
1001             for (j = needsort - 1; j >= 0; j--) {
1002                 if (aval[j] > aval[j+1]) {
1003                     char *hp;
1004
1005                     i = aval[j];
1006                     aval[j] = aval[j+1];
1007                     aval[j+1] = i;
1008
1009                     hp = ap[j];
1010                     ap[j] = ap[j+1];
1011                     ap[j+1] = hp;
1012
1013                 } else
1014                     break;
1015             }
1016             needsort++;
1017         }
1018 }
1019 #endif
1020
1021 #if defined(BSD43_BSD43_NFS) || defined(sun)
1022 /* some libc's out there are bound internally to these names (UMIPS) */
1023 void
1024 ht_sethostent(stayopen)
1025         int stayopen;
1026 {
1027         _sethtent(stayopen);
1028 }
1029
1030 void
1031 ht_endhostent()
1032 {
1033         _endhtent();
1034 }
1035
1036 struct hostent *
1037 ht_gethostbyname(name)
1038         char *name;
1039 {
1040         return (_gethtbyname(name));
1041 }
1042
1043 struct hostent *
1044 ht_gethostbyaddr(addr, len, af)
1045         const char *addr;
1046         size_t len;
1047         int af;
1048 {
1049         return (_gethtbyaddr(addr, len, af));
1050 }
1051
1052 struct hostent *
1053 gethostent()
1054 {
1055         return (_gethtent());
1056 }
1057
1058 void
1059 dns_service()
1060 {
1061         return;
1062 }
1063
1064 #undef dn_skipname
1065 dn_skipname(comp_dn, eom)
1066         const u_char *comp_dn, *eom;
1067 {
1068         return (__dn_skipname(comp_dn, eom));
1069 }
1070 #endif /*old-style libc with yp junk in it*/