xref: /freebsd/lib/libc/net/gethostbyht.c (revision a4c5661fb7aa8f02d45efc91f579c19dcd2d9d31)
11363f04cSPaul Traina /*-
21363f04cSPaul Traina  * Copyright (c) 1985, 1988, 1993
31363f04cSPaul Traina  *	The Regents of the University of California.  All rights reserved.
41363f04cSPaul Traina  *
51363f04cSPaul Traina  * Redistribution and use in source and binary forms, with or without
61363f04cSPaul Traina  * modification, are permitted provided that the following conditions
71363f04cSPaul Traina  * are met:
81363f04cSPaul Traina  * 1. Redistributions of source code must retain the above copyright
91363f04cSPaul Traina  *    notice, this list of conditions and the following disclaimer.
101363f04cSPaul Traina  * 2. Redistributions in binary form must reproduce the above copyright
111363f04cSPaul Traina  *    notice, this list of conditions and the following disclaimer in the
121363f04cSPaul Traina  *    documentation and/or other materials provided with the distribution.
131363f04cSPaul Traina  * 3. All advertising materials mentioning features or use of this software
141363f04cSPaul Traina  *    must display the following acknowledgement:
151363f04cSPaul Traina  *	This product includes software developed by the University of
161363f04cSPaul Traina  *	California, Berkeley and its contributors.
171363f04cSPaul Traina  * 4. Neither the name of the University nor the names of its contributors
181363f04cSPaul Traina  *    may be used to endorse or promote products derived from this software
191363f04cSPaul Traina  *    without specific prior written permission.
201363f04cSPaul Traina  *
211363f04cSPaul Traina  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221363f04cSPaul Traina  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231363f04cSPaul Traina  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241363f04cSPaul Traina  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251363f04cSPaul Traina  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261363f04cSPaul Traina  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271363f04cSPaul Traina  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281363f04cSPaul Traina  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291363f04cSPaul Traina  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301363f04cSPaul Traina  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311363f04cSPaul Traina  * SUCH DAMAGE.
321363f04cSPaul Traina  * -
331363f04cSPaul Traina  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
341363f04cSPaul Traina  *
351363f04cSPaul Traina  * Permission to use, copy, modify, and distribute this software for any
361363f04cSPaul Traina  * purpose with or without fee is hereby granted, provided that the above
371363f04cSPaul Traina  * copyright notice and this permission notice appear in all copies, and that
381363f04cSPaul Traina  * the name of Digital Equipment Corporation not be used in advertising or
391363f04cSPaul Traina  * publicity pertaining to distribution of the document or software without
401363f04cSPaul Traina  * specific, written prior permission.
411363f04cSPaul Traina  *
421363f04cSPaul Traina  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
431363f04cSPaul Traina  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
441363f04cSPaul Traina  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
451363f04cSPaul Traina  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
461363f04cSPaul Traina  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
471363f04cSPaul Traina  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
481363f04cSPaul Traina  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
491363f04cSPaul Traina  * SOFTWARE.
501363f04cSPaul Traina  * -
511363f04cSPaul Traina  * --Copyright--
521363f04cSPaul Traina  */
531363f04cSPaul Traina 
541363f04cSPaul Traina #if defined(LIBC_SCCS) && !defined(lint)
551363f04cSPaul Traina static char sccsid[] = "@(#)gethostnamadr.c	8.1 (Berkeley) 6/4/93";
56a4c5661fSPeter Wemm static char rcsid[] = "$Id: gethostbyht.c,v 1.5 1996/08/29 20:07:51 peter Exp $";
571363f04cSPaul Traina #endif /* LIBC_SCCS and not lint */
581363f04cSPaul Traina 
591363f04cSPaul Traina #include <sys/param.h>
601363f04cSPaul Traina #include <sys/socket.h>
611363f04cSPaul Traina #include <netinet/in.h>
621363f04cSPaul Traina #include <arpa/inet.h>
631363f04cSPaul Traina #include <netdb.h>
641363f04cSPaul Traina #include <stdio.h>
651363f04cSPaul Traina #include <ctype.h>
661363f04cSPaul Traina #include <errno.h>
671363f04cSPaul Traina #include <string.h>
685ce1c533SPeter Wemm #include <arpa/nameser.h>	/* XXX */
695ce1c533SPeter Wemm #include <resolv.h>		/* XXX */
701363f04cSPaul Traina 
711363f04cSPaul Traina #define	MAXALIASES	35
721363f04cSPaul Traina 
731363f04cSPaul Traina static struct hostent host;
741363f04cSPaul Traina static char *host_aliases[MAXALIASES];
751363f04cSPaul Traina static char hostbuf[BUFSIZ+1];
761363f04cSPaul Traina static FILE *hostf = NULL;
775ce1c533SPeter Wemm static u_char host_addr[16];	/* IPv4 or IPv6 */
784faad310SPeter Wemm static char *h_addr_ptrs[2];
791363f04cSPaul Traina static int stayopen = 0;
801363f04cSPaul Traina 
811363f04cSPaul Traina void
821363f04cSPaul Traina _sethosthtent(f)
831363f04cSPaul Traina 	int f;
841363f04cSPaul Traina {
854faad310SPeter Wemm 	if (!hostf)
861363f04cSPaul Traina 		hostf = fopen(_PATH_HOSTS, "r" );
871363f04cSPaul Traina 	else
881363f04cSPaul Traina 		rewind(hostf);
891363f04cSPaul Traina 	stayopen |= f;
901363f04cSPaul Traina }
911363f04cSPaul Traina 
921363f04cSPaul Traina void
931363f04cSPaul Traina _endhosthtent()
941363f04cSPaul Traina {
951363f04cSPaul Traina 	if (hostf && !stayopen) {
961363f04cSPaul Traina 		(void) fclose(hostf);
971363f04cSPaul Traina 		hostf = NULL;
981363f04cSPaul Traina 	}
991363f04cSPaul Traina }
1001363f04cSPaul Traina 
1011363f04cSPaul Traina struct hostent *
1021363f04cSPaul Traina gethostent()
1031363f04cSPaul Traina {
1041363f04cSPaul Traina 	char *p;
1051363f04cSPaul Traina 	register char *cp, **q;
1065ce1c533SPeter Wemm 	int af, len;
1071363f04cSPaul Traina 
1084faad310SPeter Wemm 	if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) {
1094faad310SPeter Wemm 		h_errno = NETDB_INTERNAL;
1101363f04cSPaul Traina 		return (NULL);
1114faad310SPeter Wemm 	}
1121363f04cSPaul Traina  again:
1134faad310SPeter Wemm 	if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) {
1144faad310SPeter Wemm 		h_errno = HOST_NOT_FOUND;
1151363f04cSPaul Traina 		return (NULL);
1164faad310SPeter Wemm 	}
1171363f04cSPaul Traina 	if (*p == '#')
1181363f04cSPaul Traina 		goto again;
1194faad310SPeter Wemm 	if (!(cp = strpbrk(p, "#\n")))
1201363f04cSPaul Traina 		goto again;
1211363f04cSPaul Traina 	*cp = '\0';
1224faad310SPeter Wemm 	if (!(cp = strpbrk(p, " \t")))
1231363f04cSPaul Traina 		goto again;
1241363f04cSPaul Traina 	*cp++ = '\0';
1255ce1c533SPeter Wemm 	if ((_res.options & RES_USE_INET6) &&
1265ce1c533SPeter Wemm 	    inet_pton(AF_INET6, p, host_addr) > 0) {
1275ce1c533SPeter Wemm 		af = AF_INET6;
1285ce1c533SPeter Wemm 		len = IN6ADDRSZ;
1295ce1c533SPeter Wemm 	} else if (inet_pton(AF_INET, p, host_addr) > 0) {
1305ce1c533SPeter Wemm 		if (_res.options & RES_USE_INET6) {
1315ce1c533SPeter Wemm 			_map_v4v6_address((char*)host_addr, (char*)host_addr);
1325ce1c533SPeter Wemm 			af = AF_INET6;
1335ce1c533SPeter Wemm 			len = IN6ADDRSZ;
1345ce1c533SPeter Wemm 		} else {
1355ce1c533SPeter Wemm 			af = AF_INET;
1365ce1c533SPeter Wemm 			len = INADDRSZ;
1375ce1c533SPeter Wemm 		}
1385ce1c533SPeter Wemm 	} else {
1394faad310SPeter Wemm 		goto again;
1405ce1c533SPeter Wemm 	}
1415ce1c533SPeter Wemm 	h_addr_ptrs[0] = (char *)host_addr;
1424faad310SPeter Wemm 	h_addr_ptrs[1] = NULL;
1434faad310SPeter Wemm 	host.h_addr_list = h_addr_ptrs;
1445ce1c533SPeter Wemm 	host.h_length = len;
1455ce1c533SPeter Wemm 	host.h_addrtype = af;
1461363f04cSPaul Traina 	while (*cp == ' ' || *cp == '\t')
1471363f04cSPaul Traina 		cp++;
1481363f04cSPaul Traina 	host.h_name = cp;
1491363f04cSPaul Traina 	q = host.h_aliases = host_aliases;
1505ce1c533SPeter Wemm 	if (cp = strpbrk(cp, " \t"))
1511363f04cSPaul Traina 		*cp++ = '\0';
1521363f04cSPaul Traina 	while (cp && *cp) {
1531363f04cSPaul Traina 		if (*cp == ' ' || *cp == '\t') {
1541363f04cSPaul Traina 			cp++;
1551363f04cSPaul Traina 			continue;
1561363f04cSPaul Traina 		}
1571363f04cSPaul Traina 		if (q < &host_aliases[MAXALIASES - 1])
1581363f04cSPaul Traina 			*q++ = cp;
1595ce1c533SPeter Wemm 		if (cp = strpbrk(cp, " \t"))
1601363f04cSPaul Traina 			*cp++ = '\0';
1611363f04cSPaul Traina 	}
1621363f04cSPaul Traina 	*q = NULL;
1635ce1c533SPeter Wemm 	if (_res.options & RES_USE_INET6) {
1645ce1c533SPeter Wemm 		char *bp = hostbuf;
1655ce1c533SPeter Wemm 		int buflen = sizeof hostbuf;
1665ce1c533SPeter Wemm 
1675ce1c533SPeter Wemm 		_map_v4v6_hostent(&host, &bp, &buflen);
1685ce1c533SPeter Wemm 	}
1694faad310SPeter Wemm 	h_errno = NETDB_SUCCESS;
1701363f04cSPaul Traina 	return (&host);
1711363f04cSPaul Traina }
1721363f04cSPaul Traina 
1731363f04cSPaul Traina struct hostent *
174a4c5661fSPeter Wemm _gethostbyhtname(name, af)
175a4c5661fSPeter Wemm 	const char *name;
1765ce1c533SPeter Wemm 	int af;
1771363f04cSPaul Traina {
1781363f04cSPaul Traina 	register struct hostent *p;
1791363f04cSPaul Traina 	register char **cp;
1801363f04cSPaul Traina 
1811363f04cSPaul Traina 	sethostent(0);
18251295a4dSJordan K. Hubbard 	while ( (p = gethostent()) ) {
1835ce1c533SPeter Wemm 		if (p->h_addrtype != af)
1845ce1c533SPeter Wemm 			continue;
1851363f04cSPaul Traina 		if (strcasecmp(p->h_name, name) == 0)
1861363f04cSPaul Traina 			break;
1871363f04cSPaul Traina 		for (cp = p->h_aliases; *cp != 0; cp++)
1881363f04cSPaul Traina 			if (strcasecmp(*cp, name) == 0)
1891363f04cSPaul Traina 				goto found;
1901363f04cSPaul Traina 	}
1911363f04cSPaul Traina found:
1921363f04cSPaul Traina 	endhostent();
1931363f04cSPaul Traina 	return (p);
1941363f04cSPaul Traina }
1951363f04cSPaul Traina 
1961363f04cSPaul Traina struct hostent *
1975ce1c533SPeter Wemm _gethostbyhtaddr(addr, len, af)
1981363f04cSPaul Traina 	const char *addr;
1995ce1c533SPeter Wemm 	int len, af;
2001363f04cSPaul Traina {
2011363f04cSPaul Traina 	register struct hostent *p;
2021363f04cSPaul Traina 
2031363f04cSPaul Traina 	sethostent(0);
20451295a4dSJordan K. Hubbard 	while ( (p = gethostent()) )
2055ce1c533SPeter Wemm 		if (p->h_addrtype == af && !bcmp(p->h_addr, addr, len))
2061363f04cSPaul Traina 			break;
2071363f04cSPaul Traina 	endhostent();
2081363f04cSPaul Traina 	return (p);
2091363f04cSPaul Traina }
2104faad310SPeter Wemm 
211