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 */ 72bcb131aaSHajimu UMEMOTO #include "netdb_private.h" 731363f04cSPaul Traina 741363f04cSPaul Traina #define MAXALIASES 35 751363f04cSPaul Traina 761363f04cSPaul Traina static struct hostent host; 771363f04cSPaul Traina static char *host_aliases[MAXALIASES]; 781363f04cSPaul Traina static char hostbuf[BUFSIZ+1]; 791363f04cSPaul Traina static FILE *hostf = NULL; 80141894afSDmitrij Tejblum static u_int32_t host_addr[4]; /* IPv4 or IPv6 */ 814faad310SPeter Wemm static char *h_addr_ptrs[2]; 821363f04cSPaul Traina static int stayopen = 0; 831363f04cSPaul Traina 841363f04cSPaul Traina void 851363f04cSPaul Traina _sethosthtent(f) 861363f04cSPaul Traina int f; 871363f04cSPaul Traina { 884faad310SPeter Wemm if (!hostf) 891363f04cSPaul Traina hostf = fopen(_PATH_HOSTS, "r" ); 901363f04cSPaul Traina else 911363f04cSPaul Traina rewind(hostf); 92ca785773SPeter Wemm stayopen = f; 931363f04cSPaul Traina } 941363f04cSPaul Traina 951363f04cSPaul Traina void 961363f04cSPaul Traina _endhosthtent() 971363f04cSPaul Traina { 981363f04cSPaul Traina if (hostf && !stayopen) { 991363f04cSPaul Traina (void) fclose(hostf); 1001363f04cSPaul Traina hostf = NULL; 1011363f04cSPaul Traina } 1021363f04cSPaul Traina } 1031363f04cSPaul Traina 1041363f04cSPaul Traina struct hostent * 1051363f04cSPaul Traina gethostent() 1061363f04cSPaul Traina { 1071363f04cSPaul Traina char *p; 1088fb3f3f6SDavid E. O'Brien char *cp, **q; 1095ce1c533SPeter Wemm int af, len; 1101363f04cSPaul Traina 1114faad310SPeter Wemm if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) { 1124faad310SPeter Wemm h_errno = NETDB_INTERNAL; 1131363f04cSPaul Traina return (NULL); 1144faad310SPeter Wemm } 1151363f04cSPaul Traina again: 1164faad310SPeter Wemm if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) { 1174faad310SPeter Wemm h_errno = HOST_NOT_FOUND; 1181363f04cSPaul Traina return (NULL); 1194faad310SPeter Wemm } 1201363f04cSPaul Traina if (*p == '#') 1211363f04cSPaul Traina goto again; 1227b6cc404SMaxim Sobolev cp = strpbrk(p, "#\n"); 1237b6cc404SMaxim Sobolev if (cp != NULL) 1241363f04cSPaul Traina *cp = '\0'; 1254faad310SPeter Wemm if (!(cp = strpbrk(p, " \t"))) 1261363f04cSPaul Traina goto again; 1271363f04cSPaul Traina *cp++ = '\0'; 1286c5aff80SPeter Wemm if (inet_pton(AF_INET6, p, host_addr) > 0) { 1295ce1c533SPeter Wemm af = AF_INET6; 1305ce1c533SPeter Wemm len = IN6ADDRSZ; 1315ce1c533SPeter Wemm } else if (inet_pton(AF_INET, p, host_addr) > 0) { 1325ce1c533SPeter Wemm if (_res.options & RES_USE_INET6) { 1335ce1c533SPeter Wemm _map_v4v6_address((char*)host_addr, (char*)host_addr); 1345ce1c533SPeter Wemm af = AF_INET6; 1355ce1c533SPeter Wemm len = IN6ADDRSZ; 1365ce1c533SPeter Wemm } else { 1375ce1c533SPeter Wemm af = AF_INET; 1385ce1c533SPeter Wemm len = INADDRSZ; 1395ce1c533SPeter Wemm } 1405ce1c533SPeter Wemm } else { 1414faad310SPeter Wemm goto again; 1425ce1c533SPeter Wemm } 1435ce1c533SPeter Wemm h_addr_ptrs[0] = (char *)host_addr; 1444faad310SPeter Wemm h_addr_ptrs[1] = NULL; 1454faad310SPeter Wemm host.h_addr_list = h_addr_ptrs; 1465ce1c533SPeter Wemm host.h_length = len; 1475ce1c533SPeter Wemm host.h_addrtype = af; 1481363f04cSPaul Traina while (*cp == ' ' || *cp == '\t') 1491363f04cSPaul Traina cp++; 1501363f04cSPaul Traina host.h_name = cp; 1511363f04cSPaul Traina q = host.h_aliases = host_aliases; 152ca785773SPeter Wemm if ((cp = strpbrk(cp, " \t")) != NULL) 1531363f04cSPaul Traina *cp++ = '\0'; 1541363f04cSPaul Traina while (cp && *cp) { 1551363f04cSPaul Traina if (*cp == ' ' || *cp == '\t') { 1561363f04cSPaul Traina cp++; 1571363f04cSPaul Traina continue; 1581363f04cSPaul Traina } 1591363f04cSPaul Traina if (q < &host_aliases[MAXALIASES - 1]) 1601363f04cSPaul Traina *q++ = cp; 161ca785773SPeter Wemm if ((cp = strpbrk(cp, " \t")) != NULL) 1621363f04cSPaul Traina *cp++ = '\0'; 1631363f04cSPaul Traina } 1641363f04cSPaul Traina *q = NULL; 1654faad310SPeter Wemm h_errno = NETDB_SUCCESS; 1661363f04cSPaul Traina return (&host); 1671363f04cSPaul Traina } 1681363f04cSPaul Traina 169248aee62SJacques Vidrine int 170248aee62SJacques Vidrine _ht_gethostbyname(void *rval, void *cb_data, va_list ap) 171248aee62SJacques Vidrine { 172a4c5661fSPeter Wemm const char *name; 1735ce1c533SPeter Wemm int af; 1748fb3f3f6SDavid E. O'Brien struct hostent *p; 1758fb3f3f6SDavid E. O'Brien char **cp; 1761363f04cSPaul Traina 177248aee62SJacques Vidrine name = va_arg(ap, const char *); 178248aee62SJacques Vidrine af = va_arg(ap, int); 179248aee62SJacques Vidrine 1801363f04cSPaul Traina sethostent(0); 181ca785773SPeter Wemm while ((p = gethostent()) != NULL) { 1825ce1c533SPeter Wemm if (p->h_addrtype != af) 1835ce1c533SPeter Wemm continue; 1841363f04cSPaul Traina if (strcasecmp(p->h_name, name) == 0) 1851363f04cSPaul Traina break; 1861363f04cSPaul Traina for (cp = p->h_aliases; *cp != 0; cp++) 1871363f04cSPaul Traina if (strcasecmp(*cp, name) == 0) 1881363f04cSPaul Traina goto found; 1891363f04cSPaul Traina } 1901363f04cSPaul Traina found: 1911363f04cSPaul Traina endhostent(); 192248aee62SJacques Vidrine *(struct hostent **)rval = p; 193248aee62SJacques Vidrine 194248aee62SJacques Vidrine return (p != NULL) ? NS_SUCCESS : NS_NOTFOUND; 1951363f04cSPaul Traina } 1961363f04cSPaul Traina 197248aee62SJacques Vidrine int 198248aee62SJacques Vidrine _ht_gethostbyaddr(void *rval, void *cb_data, va_list ap) 199248aee62SJacques Vidrine { 2001363f04cSPaul Traina const char *addr; 2015ce1c533SPeter Wemm int len, af; 2028fb3f3f6SDavid E. O'Brien struct hostent *p; 2031363f04cSPaul Traina 204248aee62SJacques Vidrine addr = va_arg(ap, const char *); 205248aee62SJacques Vidrine len = va_arg(ap, int); 206248aee62SJacques Vidrine af = va_arg(ap, int); 207248aee62SJacques Vidrine 2081363f04cSPaul Traina sethostent(0); 209ca785773SPeter Wemm while ((p = gethostent()) != NULL) 2105ce1c533SPeter Wemm if (p->h_addrtype == af && !bcmp(p->h_addr, addr, len)) 2111363f04cSPaul Traina break; 2121363f04cSPaul Traina endhostent(); 213248aee62SJacques Vidrine 214248aee62SJacques Vidrine *(struct hostent **)rval = p; 215248aee62SJacques Vidrine return (p != NULL) ? NS_SUCCESS : NS_NOTFOUND; 2161363f04cSPaul Traina } 217