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"; 561363f04cSPaul Traina #endif /* LIBC_SCCS and not lint */ 57333fc21eSDavid E. O'Brien #include <sys/cdefs.h> 58333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 591363f04cSPaul Traina 601363f04cSPaul Traina #include <sys/param.h> 611363f04cSPaul Traina #include <sys/socket.h> 621363f04cSPaul Traina #include <netinet/in.h> 631363f04cSPaul Traina #include <arpa/inet.h> 641363f04cSPaul Traina #include <netdb.h> 651363f04cSPaul Traina #include <stdio.h> 661363f04cSPaul Traina #include <ctype.h> 671363f04cSPaul Traina #include <string.h> 68248aee62SJacques Vidrine #include <stdarg.h> 69248aee62SJacques Vidrine #include <nsswitch.h> 705ce1c533SPeter Wemm #include <arpa/nameser.h> /* XXX */ 715ce1c533SPeter Wemm #include <resolv.h> /* XXX */ 721363f04cSPaul Traina 731363f04cSPaul Traina #define MAXALIASES 35 741363f04cSPaul Traina 751363f04cSPaul Traina static struct hostent host; 761363f04cSPaul Traina static char *host_aliases[MAXALIASES]; 771363f04cSPaul Traina static char hostbuf[BUFSIZ+1]; 781363f04cSPaul Traina static FILE *hostf = NULL; 79141894afSDmitrij Tejblum static u_int32_t host_addr[4]; /* IPv4 or IPv6 */ 804faad310SPeter Wemm static char *h_addr_ptrs[2]; 811363f04cSPaul Traina static int stayopen = 0; 821363f04cSPaul Traina 831363f04cSPaul Traina void 841363f04cSPaul Traina _sethosthtent(f) 851363f04cSPaul Traina int f; 861363f04cSPaul Traina { 874faad310SPeter Wemm if (!hostf) 881363f04cSPaul Traina hostf = fopen(_PATH_HOSTS, "r" ); 891363f04cSPaul Traina else 901363f04cSPaul Traina rewind(hostf); 91ca785773SPeter Wemm stayopen = f; 921363f04cSPaul Traina } 931363f04cSPaul Traina 941363f04cSPaul Traina void 951363f04cSPaul Traina _endhosthtent() 961363f04cSPaul Traina { 971363f04cSPaul Traina if (hostf && !stayopen) { 981363f04cSPaul Traina (void) fclose(hostf); 991363f04cSPaul Traina hostf = NULL; 1001363f04cSPaul Traina } 1011363f04cSPaul Traina } 1021363f04cSPaul Traina 1031363f04cSPaul Traina struct hostent * 1041363f04cSPaul Traina gethostent() 1051363f04cSPaul Traina { 1061363f04cSPaul Traina char *p; 1078fb3f3f6SDavid E. O'Brien char *cp, **q; 1085ce1c533SPeter Wemm int af, len; 1091363f04cSPaul Traina 1104faad310SPeter Wemm if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) { 1114faad310SPeter Wemm h_errno = NETDB_INTERNAL; 1121363f04cSPaul Traina return (NULL); 1134faad310SPeter Wemm } 1141363f04cSPaul Traina again: 1154faad310SPeter Wemm if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) { 1164faad310SPeter Wemm h_errno = HOST_NOT_FOUND; 1171363f04cSPaul Traina return (NULL); 1184faad310SPeter Wemm } 1191363f04cSPaul Traina if (*p == '#') 1201363f04cSPaul Traina goto again; 1217b6cc404SMaxim Sobolev cp = strpbrk(p, "#\n"); 1227b6cc404SMaxim Sobolev if (cp != NULL) 1231363f04cSPaul Traina *cp = '\0'; 1244faad310SPeter Wemm if (!(cp = strpbrk(p, " \t"))) 1251363f04cSPaul Traina goto again; 1261363f04cSPaul Traina *cp++ = '\0'; 1276c5aff80SPeter Wemm if (inet_pton(AF_INET6, p, host_addr) > 0) { 1285ce1c533SPeter Wemm af = AF_INET6; 1295ce1c533SPeter Wemm len = IN6ADDRSZ; 1305ce1c533SPeter Wemm } else if (inet_pton(AF_INET, p, host_addr) > 0) { 1315ce1c533SPeter Wemm if (_res.options & RES_USE_INET6) { 1325ce1c533SPeter Wemm _map_v4v6_address((char*)host_addr, (char*)host_addr); 1335ce1c533SPeter Wemm af = AF_INET6; 1345ce1c533SPeter Wemm len = IN6ADDRSZ; 1355ce1c533SPeter Wemm } else { 1365ce1c533SPeter Wemm af = AF_INET; 1375ce1c533SPeter Wemm len = INADDRSZ; 1385ce1c533SPeter Wemm } 1395ce1c533SPeter Wemm } else { 1404faad310SPeter Wemm goto again; 1415ce1c533SPeter Wemm } 1425ce1c533SPeter Wemm h_addr_ptrs[0] = (char *)host_addr; 1434faad310SPeter Wemm h_addr_ptrs[1] = NULL; 1444faad310SPeter Wemm host.h_addr_list = h_addr_ptrs; 1455ce1c533SPeter Wemm host.h_length = len; 1465ce1c533SPeter Wemm host.h_addrtype = af; 1471363f04cSPaul Traina while (*cp == ' ' || *cp == '\t') 1481363f04cSPaul Traina cp++; 1491363f04cSPaul Traina host.h_name = cp; 1501363f04cSPaul Traina q = host.h_aliases = host_aliases; 151ca785773SPeter Wemm if ((cp = strpbrk(cp, " \t")) != NULL) 1521363f04cSPaul Traina *cp++ = '\0'; 1531363f04cSPaul Traina while (cp && *cp) { 1541363f04cSPaul Traina if (*cp == ' ' || *cp == '\t') { 1551363f04cSPaul Traina cp++; 1561363f04cSPaul Traina continue; 1571363f04cSPaul Traina } 1581363f04cSPaul Traina if (q < &host_aliases[MAXALIASES - 1]) 1591363f04cSPaul Traina *q++ = cp; 160ca785773SPeter Wemm if ((cp = strpbrk(cp, " \t")) != NULL) 1611363f04cSPaul Traina *cp++ = '\0'; 1621363f04cSPaul Traina } 1631363f04cSPaul Traina *q = NULL; 1644faad310SPeter Wemm h_errno = NETDB_SUCCESS; 1651363f04cSPaul Traina return (&host); 1661363f04cSPaul Traina } 1671363f04cSPaul Traina 168248aee62SJacques Vidrine int 169248aee62SJacques Vidrine _ht_gethostbyname(void *rval, void *cb_data, va_list ap) 170248aee62SJacques Vidrine { 171a4c5661fSPeter Wemm const char *name; 1725ce1c533SPeter Wemm int af; 1738fb3f3f6SDavid E. O'Brien struct hostent *p; 1748fb3f3f6SDavid E. O'Brien char **cp; 1751363f04cSPaul Traina 176248aee62SJacques Vidrine name = va_arg(ap, const char *); 177248aee62SJacques Vidrine af = va_arg(ap, int); 178248aee62SJacques Vidrine 1791363f04cSPaul Traina sethostent(0); 180ca785773SPeter Wemm while ((p = gethostent()) != NULL) { 1815ce1c533SPeter Wemm if (p->h_addrtype != af) 1825ce1c533SPeter Wemm continue; 1831363f04cSPaul Traina if (strcasecmp(p->h_name, name) == 0) 1841363f04cSPaul Traina break; 1851363f04cSPaul Traina for (cp = p->h_aliases; *cp != 0; cp++) 1861363f04cSPaul Traina if (strcasecmp(*cp, name) == 0) 1871363f04cSPaul Traina goto found; 1881363f04cSPaul Traina } 1891363f04cSPaul Traina found: 1901363f04cSPaul Traina endhostent(); 191248aee62SJacques Vidrine *(struct hostent **)rval = p; 192248aee62SJacques Vidrine 193248aee62SJacques Vidrine return (p != NULL) ? NS_SUCCESS : NS_NOTFOUND; 1941363f04cSPaul Traina } 1951363f04cSPaul Traina 196248aee62SJacques Vidrine int 197248aee62SJacques Vidrine _ht_gethostbyaddr(void *rval, void *cb_data, va_list ap) 198248aee62SJacques Vidrine { 1991363f04cSPaul Traina const char *addr; 2005ce1c533SPeter Wemm int len, af; 2018fb3f3f6SDavid E. O'Brien struct hostent *p; 2021363f04cSPaul Traina 203248aee62SJacques Vidrine addr = va_arg(ap, const char *); 204248aee62SJacques Vidrine len = va_arg(ap, int); 205248aee62SJacques Vidrine af = va_arg(ap, int); 206248aee62SJacques Vidrine 2071363f04cSPaul Traina sethostent(0); 208ca785773SPeter Wemm while ((p = gethostent()) != NULL) 2095ce1c533SPeter Wemm if (p->h_addrtype == af && !bcmp(p->h_addr, addr, len)) 2101363f04cSPaul Traina break; 2111363f04cSPaul Traina endhostent(); 212248aee62SJacques Vidrine 213248aee62SJacques Vidrine *(struct hostent **)rval = p; 214248aee62SJacques Vidrine return (p != NULL) ? NS_SUCCESS : NS_NOTFOUND; 2151363f04cSPaul Traina } 216