2 * ++Copyright++ 1985, 1988, 1993
4 * Copyright (c) 1985, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
35 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
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.
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
56 #if defined(LIBC_SCCS) && !defined(lint)
57 static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
58 static char rcsid[] = "$Id$";
59 #endif /* LIBC_SCCS and not lint */
61 #include <sys/types.h>
62 #include <sys/param.h>
63 #include <sys/socket.h>
64 #include <netinet/in.h>
65 #include <arpa/inet.h>
66 #include <arpa/nameser.h>
79 #define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
81 #if defined(BSD) && (BSD >= 199103) && defined(AF_INET6)
85 # include "../conf/portability.h"
88 #if defined(USE_OPTIONS_H)
89 # include <../conf/options.h>
93 # define SPRINTF(x) strlen(sprintf/**/x)
95 # define SPRINTF(x) ((size_t)sprintf x)
101 static const char AskedForGot[] =
102 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
104 static char *h_addr_ptrs[MAXADDRS + 1];
106 static struct hostent host;
107 static char *host_aliases[MAXALIASES];
108 static char hostbuf[8*1024];
109 static u_char host_addr[16]; /* IPv4 or IPv6 */
110 static FILE *hostf = NULL;
111 static int stayopen = 0;
113 static void map_v4v6_address __P((const char *src, char *dst));
114 static void map_v4v6_hostent __P((struct hostent *hp, char **bp, int *len));
117 extern void addrsort __P((char **, int));
121 #define MAXPACKET PACKETSZ
123 #define MAXPACKET 1024
126 /* As per RFC 1034 and 1035 a host name cannot exceed 255 octets in length. */
127 #ifdef MAXHOSTNAMELEN
128 # undef MAXHOSTNAMELEN
130 #define MAXHOSTNAMELEN 256
134 u_char buf[MAXPACKET];
152 if (_res.options & RES_DEBUG) {
160 # define dprintf(msg, num) /*nada*/
163 #define BOUNDED_INCR(x) \
167 __set_h_errno (NO_RECOVERY); \
172 #define BOUNDS_CHECK(ptr, count) \
174 if ((ptr) + (count) > eom) { \
175 __set_h_errno (NO_RECOVERY); \
181 static struct hostent *
182 getanswer(answer, anslen, qname, qtype)
183 const querybuf *answer;
188 register const HEADER *hp;
189 register const u_char *cp;
191 const u_char *eom, *erdata;
192 char *bp, **ap, **hap;
193 int type, class, buflen, ancount, qdcount;
194 int haveanswer, had_error;
198 int (*name_ok) __P((const char *));
202 eom = answer->buf + anslen;
212 return (NULL); /* XXX should be abort(); */
215 * find first satisfactory answer
218 ancount = ntohs(hp->ancount);
219 qdcount = ntohs(hp->qdcount);
221 buflen = sizeof hostbuf;
223 BOUNDED_INCR(HFIXEDSZ);
225 __set_h_errno (NO_RECOVERY);
228 n = dn_expand(answer->buf, eom, cp, bp, buflen);
229 if ((n < 0) || !(*name_ok)(bp)) {
230 __set_h_errno (NO_RECOVERY);
233 BOUNDED_INCR(n + QFIXEDSZ);
234 if (qtype == T_A || qtype == T_AAAA) {
235 /* res_send() has already verified that the query name is the
236 * same as the one we sent; this just gets the expanded name
237 * (i.e., with the succeeding search-domain tacked on).
239 n = strlen(bp) + 1; /* for the \0 */
240 if (n >= MAXHOSTNAMELEN) {
241 __set_h_errno (NO_RECOVERY);
247 /* The qname can be abbreviated, but h_name is now absolute. */
252 host.h_aliases = host_aliases;
255 host.h_addr_list = h_addr_ptrs;
258 while (ancount-- > 0 && cp < eom && !had_error) {
259 n = dn_expand(answer->buf, eom, cp, bp, buflen);
260 if ((n < 0) || !(*name_ok)(bp)) {
265 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
266 type = _getshort(cp);
267 cp += INT16SZ; /* type */
268 class = _getshort(cp);
269 cp += INT16SZ + INT32SZ; /* class, TTL */
271 cp += INT16SZ; /* len */
275 /* XXX - debug? syslog? */
277 continue; /* XXX - had_error++ ? */
279 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
280 if (ap >= &host_aliases[MAXALIASES-1])
282 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
283 if ((n < 0) || !(*name_ok)(tbuf)) {
289 __set_h_errno (NO_RECOVERY);
294 n = strlen(bp) + 1; /* for the \0 */
295 if (n >= MAXHOSTNAMELEN) {
301 /* Get canonical name. */
302 n = strlen(tbuf) + 1; /* for the \0 */
303 if (n > buflen || n >= MAXHOSTNAMELEN) {
313 if (qtype == T_PTR && type == T_CNAME) {
314 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
315 if (n < 0 || !res_dnok(tbuf)) {
321 __set_h_errno (NO_RECOVERY);
324 /* Get canonical name. */
325 n = strlen(tbuf) + 1; /* for the \0 */
326 if (n > buflen || n >= MAXHOSTNAMELEN) {
337 syslog(LOG_NOTICE|LOG_AUTH,
338 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
339 qname, p_class(C_IN), p_type(qtype),
342 continue; /* XXX - had_error++ ? */
346 if (strcasecmp(tname, bp) != 0) {
347 syslog(LOG_NOTICE|LOG_AUTH,
348 AskedForGot, qname, bp);
350 continue; /* XXX - had_error++ ? */
352 n = dn_expand(answer->buf, eom, cp, bp, buflen);
353 if ((n < 0) || !res_hnok(bp)) {
357 #if MULTI_PTRS_ARE_ALIASES
360 __set_h_errno (NO_RECOVERY);
365 else if (ap < &host_aliases[MAXALIASES-1])
370 n = strlen(bp) + 1; /* for the \0 */
371 if (n >= MAXHOSTNAMELEN) {
381 if (_res.options & RES_USE_INET6) {
382 n = strlen(bp) + 1; /* for the \0 */
383 if (n >= MAXHOSTNAMELEN) {
389 map_v4v6_hostent(&host, &bp, &buflen);
391 __set_h_errno (NETDB_SUCCESS);
396 if (strcasecmp(host.h_name, bp) != 0) {
397 syslog(LOG_NOTICE|LOG_AUTH,
398 AskedForGot, host.h_name, bp);
400 continue; /* XXX - had_error++ ? */
402 if (n != host.h_length) {
410 nn = strlen(bp) + 1; /* for the \0 */
415 bp += sizeof(align) - ((u_long)bp % sizeof(align));
417 if (bp + n >= &hostbuf[sizeof hostbuf]) {
418 dprintf("size (%d) too big\n", n);
422 if (hap >= &h_addr_ptrs[MAXADDRS-1]) {
424 dprintf("Too many addresses (%d)\n",
430 bcopy(cp, *hap++ = bp, n);
435 __set_h_errno (NO_RECOVERY);
448 # if defined(RESOLVSORT)
450 * Note: we sort even if host can take only one address
451 * in its return structures - should give it the "best"
452 * address in that case, not some random one
454 if (_res.nsort && haveanswer > 1 && qtype == T_A)
455 addrsort(h_addr_ptrs, haveanswer);
456 # endif /*RESOLVSORT*/
458 n = strlen(qname) + 1; /* for the \0 */
459 if (n > buflen || n >= MAXHOSTNAMELEN)
466 if (_res.options & RES_USE_INET6)
467 map_v4v6_hostent(&host, &bp, &buflen);
468 __set_h_errno (NETDB_SUCCESS);
472 __set_h_errno (NO_RECOVERY);
482 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
483 __set_h_errno (NETDB_INTERNAL);
486 if (_res.options & RES_USE_INET6) {
487 hp = gethostbyname2(name, AF_INET6);
491 return (gethostbyname2(name, AF_INET));
495 gethostbyname2(name, af)
500 register const char *cp;
502 int n, size, type, len;
503 extern struct hostent *_gethtbyname2();
505 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
506 __set_h_errno (NETDB_INTERNAL);
520 __set_h_errno (NETDB_INTERNAL);
521 __set_errno (EAFNOSUPPORT);
525 host.h_addrtype = af;
526 host.h_length = size;
529 * if there aren't any dots, it could be a user-level alias.
530 * this is also done in res_query() since we are not the only
531 * function that looks up host names.
533 if (!strchr(name, '.') && (cp = __hostalias(name)))
537 * disallow names consisting only of digits/dots, unless
540 if (isdigit(name[0]))
541 for (cp = name;; ++cp) {
546 * All-numeric, no dot at the end.
547 * Fake up a hostent as if we'd actually
550 if (inet_pton(af, name, host_addr) <= 0) {
551 __set_h_errno (HOST_NOT_FOUND);
554 strncpy(hostbuf, name, MAXDNAME);
555 hostbuf[MAXDNAME] = '\0';
556 bp = hostbuf + MAXDNAME;
557 len = sizeof hostbuf - MAXDNAME;
558 host.h_name = hostbuf;
559 host.h_aliases = host_aliases;
560 host_aliases[0] = NULL;
561 h_addr_ptrs[0] = (char *)host_addr;
562 h_addr_ptrs[1] = NULL;
563 host.h_addr_list = h_addr_ptrs;
564 if (_res.options & RES_USE_INET6)
565 map_v4v6_hostent(&host, &bp, &len);
566 __set_h_errno (NETDB_SUCCESS);
569 if (!isdigit(*cp) && *cp != '.')
572 if ((isxdigit(name[0]) && strchr(name, ':') != NULL) ||
574 for (cp = name;; ++cp) {
579 * All-IPv6-legal, no dot at the end.
580 * Fake up a hostent as if we'd actually
583 if (inet_pton(af, name, host_addr) <= 0) {
584 __set_h_errno (HOST_NOT_FOUND);
587 strncpy(hostbuf, name, MAXDNAME);
588 hostbuf[MAXDNAME] = '\0';
589 bp = hostbuf + MAXDNAME;
590 len = sizeof hostbuf - MAXDNAME;
591 host.h_name = hostbuf;
592 host.h_aliases = host_aliases;
593 host_aliases[0] = NULL;
594 h_addr_ptrs[0] = (char *)host_addr;
595 h_addr_ptrs[1] = NULL;
596 host.h_addr_list = h_addr_ptrs;
597 __set_h_errno (NETDB_SUCCESS);
600 if (!isxdigit(*cp) && *cp != ':' && *cp != '.')
604 if ((n = res_search(name, C_IN, type, buf.buf, sizeof(buf.buf))) < 0) {
605 dprintf("res_search failed (%d)\n", n);
606 if (errno == ECONNREFUSED)
607 return (_gethtbyname2(name, af));
610 return (getanswer(&buf, n, name, type));
614 gethostbyaddr(addr, len, af)
615 const char *addr; /* XXX should have been def'd as u_char! */
618 const u_char *uaddr = (const u_char *)addr;
619 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
620 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
623 register struct hostent *hp;
624 char qbuf[MAXDNAME+1], *qp;
626 register struct hostent *rhp;
629 char hname2[MAXDNAME+1];
630 #endif /*SUNSECURITY*/
631 extern struct hostent *_gethtbyaddr();
633 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
634 __set_h_errno (NETDB_INTERNAL);
637 if (af == AF_INET6 && len == IN6ADDRSZ &&
638 (!bcmp(uaddr, mapped, sizeof mapped) ||
639 !bcmp(uaddr, tunnelled, sizeof tunnelled))) {
641 addr += sizeof mapped;
642 uaddr += sizeof mapped;
654 __set_errno (EAFNOSUPPORT);
655 __set_h_errno (NETDB_INTERNAL);
659 __set_errno (EINVAL);
660 __set_h_errno (NETDB_INTERNAL);
665 (void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
673 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
674 qp += SPRINTF((qp, "%x.%x.",
676 (uaddr[n] >> 4) & 0xf));
678 strcpy(qp, "ip6.int");
683 n = res_query(qbuf, C_IN, T_PTR, (u_char *)buf.buf, sizeof buf.buf);
685 dprintf("res_query failed (%d)\n", n);
686 if (errno == ECONNREFUSED)
687 return (_gethtbyaddr(addr, len, af));
690 if (!(hp = getanswer(&buf, n, qbuf, T_PTR)))
691 return (NULL); /* h_errno was set by getanswer() */
695 * turn off search as the name should be absolute,
696 * 'localhost' should be matched by defnames
698 strncpy(hname2, hp->h_name, MAXDNAME);
699 hname2[MAXDNAME] = '\0';
700 old_options = _res.options;
701 _res.options &= ~RES_DNSRCH;
702 _res.options |= RES_DEFNAMES;
703 if (!(rhp = gethostbyname(hname2))) {
704 syslog(LOG_NOTICE|LOG_AUTH,
705 "gethostbyaddr: No A record for %s (verifying [%s])",
706 hname2, inet_ntoa(*((struct in_addr *)addr)));
707 _res.options = old_options;
708 __set_h_errno (HOST_NOT_FOUND);
711 _res.options = old_options;
712 for (haddr = rhp->h_addr_list; *haddr; haddr++)
713 if (!memcmp(*haddr, addr, INADDRSZ))
716 syslog(LOG_NOTICE|LOG_AUTH,
717 "gethostbyaddr: A record of %s != PTR record [%s]",
718 hname2, inet_ntoa(*((struct in_addr *)addr)));
719 __set_h_errno (HOST_NOT_FOUND);
723 #endif /*SUNSECURITY*/
726 bcopy(addr, host_addr, len);
727 h_addr_ptrs[0] = (char *)host_addr;
728 h_addr_ptrs[1] = NULL;
729 if (af == AF_INET && (_res.options & RES_USE_INET6)) {
730 map_v4v6_address((char*)host_addr, (char*)host_addr);
731 hp->h_addrtype = AF_INET6;
732 hp->h_length = IN6ADDRSZ;
734 __set_h_errno (NETDB_SUCCESS);
743 hostf = fopen(_PATH_HOSTS, "r" );
752 if (hostf && !stayopen) {
753 (void) fclose(hostf);
762 register char *cp, **q;
765 if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) {
766 __set_h_errno (NETDB_INTERNAL);
770 if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) {
771 __set_h_errno (HOST_NOT_FOUND);
776 if (!(cp = strpbrk(p, "#\n")))
779 if (!(cp = strpbrk(p, " \t")))
782 if (inet_pton(AF_INET6, p, host_addr) > 0) {
785 } else if (inet_pton(AF_INET, p, host_addr) > 0) {
786 if (_res.options & RES_USE_INET6) {
787 map_v4v6_address((char*)host_addr, (char*)host_addr);
797 h_addr_ptrs[0] = (char *)host_addr;
798 h_addr_ptrs[1] = NULL;
799 host.h_addr_list = h_addr_ptrs;
801 host.h_addrtype = af;
802 while (*cp == ' ' || *cp == '\t')
805 q = host.h_aliases = host_aliases;
806 if (cp = strpbrk(cp, " \t"))
809 if (*cp == ' ' || *cp == '\t') {
813 if (q < &host_aliases[MAXALIASES - 1])
815 if (cp = strpbrk(cp, " \t"))
819 __set_h_errno (NETDB_SUCCESS);
827 extern struct hostent *_gethtbyname2();
830 if (_res.options & RES_USE_INET6) {
831 hp = _gethtbyname2(name, AF_INET6);
835 return (_gethtbyname2(name, AF_INET));
839 _gethtbyname2(name, af)
843 register struct hostent *p;
847 while (p = _gethtent()) {
848 if (p->h_addrtype != af)
850 if (strcasecmp(p->h_name, name) == 0)
852 for (cp = p->h_aliases; *cp != 0; cp++)
853 if (strcasecmp(*cp, name) == 0)
862 _gethtbyaddr(addr, len, af)
866 register struct hostent *p;
869 while (p = _gethtent())
870 if (p->h_addrtype == af && !bcmp(p->h_addr, addr, len))
877 map_v4v6_address(src, dst)
881 u_char *p = (u_char *)dst;
885 /* Stash a temporary copy so our caller can update in place. */
886 bcopy(src, tmp, INADDRSZ);
887 /* Mark this ipv6 addr as a mapped ipv4. */
888 for (i = 0; i < 10; i++)
892 /* Retrieve the saved copy and we're done. */
893 bcopy(tmp, (void*)p, INADDRSZ);
897 map_v4v6_hostent(hp, bpp, lenp)
904 if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
906 hp->h_addrtype = AF_INET6;
907 hp->h_length = IN6ADDRSZ;
908 for (ap = hp->h_addr_list; *ap; ap++) {
909 int i = sizeof(align) - ((u_long)*bpp % sizeof(align));
911 if (*lenp < (i + IN6ADDRSZ)) {
912 /* Out of memory. Truncate address list here. XXX */
918 map_v4v6_address(*ap, *bpp);
933 short aval[MAXADDRS];
937 for (i = 0; i < num; i++, p++) {
938 for (j = 0 ; (unsigned)j < _res.nsort; j++)
939 if (_res.sort_list[j].addr.s_addr ==
940 (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
943 if (needsort == 0 && i > 0 && j < aval[i-1])
949 while (needsort < num) {
950 for (j = needsort - 1; j >= 0; j--) {
951 if (aval[j] > aval[j+1]) {
970 #if defined(BSD43_BSD43_NFS) || defined(sun)
971 /* some libc's out there are bound internally to these names (UMIPS) */
973 ht_sethostent(stayopen)
986 ht_gethostbyname(name)
989 return (_gethtbyname(name));
993 ht_gethostbyaddr(addr, len, af)
997 return (_gethtbyaddr(addr, len, af));
1003 return (_gethtent());
1013 dn_skipname(comp_dn, eom)
1014 const u_char *comp_dn, *eom;
1016 return (__dn_skipname(comp_dn, eom));
1018 #endif /*old-style libc with yp junk in it*/