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"; 567f3dea24SPeter Wemm static char rcsid[] = "$FreeBSD$"; 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 <string.h> 67248aee62SJacques Vidrine #include <stdarg.h> 68248aee62SJacques Vidrine #include <nsswitch.h> 695ce1c533SPeter Wemm #include <arpa/nameser.h> /* XXX */ 705ce1c533SPeter Wemm #include <resolv.h> /* XXX */ 711363f04cSPaul Traina 721363f04cSPaul Traina #define MAXALIASES 35 731363f04cSPaul Traina 741363f04cSPaul Traina static struct hostent host; 751363f04cSPaul Traina static char *host_aliases[MAXALIASES]; 761363f04cSPaul Traina static char hostbuf[BUFSIZ+1]; 771363f04cSPaul Traina static FILE *hostf = NULL; 78141894afSDmitrij Tejblum static u_int32_t host_addr[4]; /* IPv4 or IPv6 */ 794faad310SPeter Wemm static char *h_addr_ptrs[2]; 801363f04cSPaul Traina static int stayopen = 0; 811363f04cSPaul Traina 821363f04cSPaul Traina void 831363f04cSPaul Traina _sethosthtent(f) 841363f04cSPaul Traina int f; 851363f04cSPaul Traina { 864faad310SPeter Wemm if (!hostf) 871363f04cSPaul Traina hostf = fopen(_PATH_HOSTS, "r" ); 881363f04cSPaul Traina else 891363f04cSPaul Traina rewind(hostf); 90ca785773SPeter Wemm stayopen = f; 911363f04cSPaul Traina } 921363f04cSPaul Traina 931363f04cSPaul Traina void 941363f04cSPaul Traina _endhosthtent() 951363f04cSPaul Traina { 961363f04cSPaul Traina if (hostf && !stayopen) { 971363f04cSPaul Traina (void) fclose(hostf); 981363f04cSPaul Traina hostf = NULL; 991363f04cSPaul Traina } 1001363f04cSPaul Traina } 1011363f04cSPaul Traina 1021363f04cSPaul Traina struct hostent * 1031363f04cSPaul Traina gethostent() 1041363f04cSPaul Traina { 1051363f04cSPaul Traina char *p; 1068fb3f3f6SDavid E. O'Brien char *cp, **q; 1075ce1c533SPeter Wemm int af, len; 1081363f04cSPaul Traina 1094faad310SPeter Wemm if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) { 1104faad310SPeter Wemm h_errno = NETDB_INTERNAL; 1111363f04cSPaul Traina return (NULL); 1124faad310SPeter Wemm } 1131363f04cSPaul Traina again: 1144faad310SPeter Wemm if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) { 1154faad310SPeter Wemm h_errno = HOST_NOT_FOUND; 1161363f04cSPaul Traina return (NULL); 1174faad310SPeter Wemm } 1181363f04cSPaul Traina if (*p == '#') 1191363f04cSPaul Traina goto again; 1204faad310SPeter Wemm if (!(cp = strpbrk(p, "#\n"))) 1211363f04cSPaul Traina goto again; 1221363f04cSPaul Traina *cp = '\0'; 1234faad310SPeter Wemm if (!(cp = strpbrk(p, " \t"))) 1241363f04cSPaul Traina goto again; 1251363f04cSPaul Traina *cp++ = '\0'; 1266c5aff80SPeter Wemm if (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; 150ca785773SPeter Wemm if ((cp = strpbrk(cp, " \t")) != NULL) 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; 159ca785773SPeter Wemm if ((cp = strpbrk(cp, " \t")) != NULL) 1601363f04cSPaul Traina *cp++ = '\0'; 1611363f04cSPaul Traina } 1621363f04cSPaul Traina *q = NULL; 1634faad310SPeter Wemm h_errno = NETDB_SUCCESS; 1641363f04cSPaul Traina return (&host); 1651363f04cSPaul Traina } 1661363f04cSPaul Traina 167248aee62SJacques Vidrine int 168248aee62SJacques Vidrine _ht_gethostbyname(void *rval, void *cb_data, va_list ap) 169248aee62SJacques Vidrine { 170a4c5661fSPeter Wemm const char *name; 1715ce1c533SPeter Wemm int af; 1728fb3f3f6SDavid E. O'Brien struct hostent *p; 1738fb3f3f6SDavid E. O'Brien char **cp; 1741363f04cSPaul Traina 175248aee62SJacques Vidrine name = va_arg(ap, const char *); 176248aee62SJacques Vidrine af = va_arg(ap, int); 177248aee62SJacques Vidrine 1781363f04cSPaul Traina sethostent(0); 179ca785773SPeter Wemm while ((p = gethostent()) != NULL) { 1805ce1c533SPeter Wemm if (p->h_addrtype != af) 1815ce1c533SPeter Wemm continue; 1821363f04cSPaul Traina if (strcasecmp(p->h_name, name) == 0) 1831363f04cSPaul Traina break; 1841363f04cSPaul Traina for (cp = p->h_aliases; *cp != 0; cp++) 1851363f04cSPaul Traina if (strcasecmp(*cp, name) == 0) 1861363f04cSPaul Traina goto found; 1871363f04cSPaul Traina } 1881363f04cSPaul Traina found: 1891363f04cSPaul Traina endhostent(); 190248aee62SJacques Vidrine *(struct hostent **)rval = p; 191248aee62SJacques Vidrine 192248aee62SJacques Vidrine return (p != NULL) ? NS_SUCCESS : NS_NOTFOUND; 1931363f04cSPaul Traina } 1941363f04cSPaul Traina 195248aee62SJacques Vidrine int 196248aee62SJacques Vidrine _ht_gethostbyaddr(void *rval, void *cb_data, va_list ap) 197248aee62SJacques Vidrine { 1981363f04cSPaul Traina const char *addr; 1995ce1c533SPeter Wemm int len, af; 2008fb3f3f6SDavid E. O'Brien struct hostent *p; 2011363f04cSPaul Traina 202248aee62SJacques Vidrine addr = va_arg(ap, const char *); 203248aee62SJacques Vidrine len = va_arg(ap, int); 204248aee62SJacques Vidrine af = va_arg(ap, int); 205248aee62SJacques Vidrine 2061363f04cSPaul Traina sethostent(0); 207ca785773SPeter Wemm while ((p = gethostent()) != NULL) 2085ce1c533SPeter Wemm if (p->h_addrtype == af && !bcmp(p->h_addr, addr, len)) 2091363f04cSPaul Traina break; 2101363f04cSPaul Traina endhostent(); 211248aee62SJacques Vidrine 212248aee62SJacques Vidrine *(struct hostent **)rval = p; 213248aee62SJacques Vidrine return (p != NULL) ? NS_SUCCESS : NS_NOTFOUND; 2141363f04cSPaul Traina } 215