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 static char rcsid[] = "$Id: gethostnamadr.c,v 1.1 1994/05/27 04:57:16 rgrimes 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> 681363f04cSPaul Traina 691363f04cSPaul Traina #define MAXALIASES 35 701363f04cSPaul Traina #define MAXADDRS 35 711363f04cSPaul Traina 721363f04cSPaul Traina static struct hostent host; 731363f04cSPaul Traina static char *host_aliases[MAXALIASES]; 741363f04cSPaul Traina static char hostbuf[BUFSIZ+1]; 751363f04cSPaul Traina static FILE *hostf = NULL; 761363f04cSPaul Traina static char hostaddr[MAXADDRS]; 771363f04cSPaul Traina static char *host_addrs[2]; 781363f04cSPaul Traina static int stayopen = 0; 791363f04cSPaul Traina 801363f04cSPaul Traina void 811363f04cSPaul Traina _sethosthtent(f) 821363f04cSPaul Traina int f; 831363f04cSPaul Traina { 841363f04cSPaul Traina if (hostf == NULL) 851363f04cSPaul Traina hostf = fopen(_PATH_HOSTS, "r" ); 861363f04cSPaul Traina else 871363f04cSPaul Traina rewind(hostf); 881363f04cSPaul Traina stayopen |= f; 891363f04cSPaul Traina } 901363f04cSPaul Traina 911363f04cSPaul Traina void 921363f04cSPaul Traina _endhosthtent() 931363f04cSPaul Traina { 941363f04cSPaul Traina if (hostf && !stayopen) { 951363f04cSPaul Traina (void) fclose(hostf); 961363f04cSPaul Traina hostf = NULL; 971363f04cSPaul Traina } 981363f04cSPaul Traina } 991363f04cSPaul Traina 1001363f04cSPaul Traina struct hostent * 1011363f04cSPaul Traina gethostent() 1021363f04cSPaul Traina { 1031363f04cSPaul Traina char *p; 1041363f04cSPaul Traina register char *cp, **q; 1051363f04cSPaul Traina 1061363f04cSPaul Traina if (hostf == NULL && (hostf = fopen(_PATH_HOSTS, "r" )) == NULL) 1071363f04cSPaul Traina return (NULL); 1081363f04cSPaul Traina again: 1091363f04cSPaul Traina if ((p = fgets(hostbuf, BUFSIZ, hostf)) == NULL) 1101363f04cSPaul Traina return (NULL); 1111363f04cSPaul Traina if (*p == '#') 1121363f04cSPaul Traina goto again; 1131363f04cSPaul Traina cp = strpbrk(p, "#\n"); 1141363f04cSPaul Traina if (cp == NULL) 1151363f04cSPaul Traina goto again; 1161363f04cSPaul Traina *cp = '\0'; 1171363f04cSPaul Traina cp = strpbrk(p, " \t"); 1181363f04cSPaul Traina if (cp == NULL) 1191363f04cSPaul Traina goto again; 1201363f04cSPaul Traina *cp++ = '\0'; 1211363f04cSPaul Traina /* THIS STUFF IS INTERNET SPECIFIC */ 1221363f04cSPaul Traina host.h_addr_list = host_addrs; 1231363f04cSPaul Traina host.h_addr = hostaddr; 1241363f04cSPaul Traina *((u_int32_t *)host.h_addr) = inet_addr(p); 1251363f04cSPaul Traina host.h_length = sizeof (u_int32_t); 1261363f04cSPaul Traina host.h_addrtype = AF_INET; 1271363f04cSPaul Traina while (*cp == ' ' || *cp == '\t') 1281363f04cSPaul Traina cp++; 1291363f04cSPaul Traina host.h_name = cp; 1301363f04cSPaul Traina q = host.h_aliases = host_aliases; 1311363f04cSPaul Traina cp = strpbrk(cp, " \t"); 1321363f04cSPaul Traina if (cp != NULL) 1331363f04cSPaul Traina *cp++ = '\0'; 1341363f04cSPaul Traina while (cp && *cp) { 1351363f04cSPaul Traina if (*cp == ' ' || *cp == '\t') { 1361363f04cSPaul Traina cp++; 1371363f04cSPaul Traina continue; 1381363f04cSPaul Traina } 1391363f04cSPaul Traina if (q < &host_aliases[MAXALIASES - 1]) 1401363f04cSPaul Traina *q++ = cp; 1411363f04cSPaul Traina cp = strpbrk(cp, " \t"); 1421363f04cSPaul Traina if (cp != NULL) 1431363f04cSPaul Traina *cp++ = '\0'; 1441363f04cSPaul Traina } 1451363f04cSPaul Traina *q = NULL; 1461363f04cSPaul Traina return (&host); 1471363f04cSPaul Traina } 1481363f04cSPaul Traina 1491363f04cSPaul Traina struct hostent * 1501363f04cSPaul Traina _gethostbyhtname(name) 1511363f04cSPaul Traina char *name; 1521363f04cSPaul Traina { 1531363f04cSPaul Traina register struct hostent *p; 1541363f04cSPaul Traina register char **cp; 1551363f04cSPaul Traina 1561363f04cSPaul Traina sethostent(0); 1571363f04cSPaul Traina while ((p = gethostent())) { 1581363f04cSPaul Traina if (strcasecmp(p->h_name, name) == 0) 1591363f04cSPaul Traina break; 1601363f04cSPaul Traina for (cp = p->h_aliases; *cp != 0; cp++) 1611363f04cSPaul Traina if (strcasecmp(*cp, name) == 0) 1621363f04cSPaul Traina goto found; 1631363f04cSPaul Traina } 1641363f04cSPaul Traina found: 1651363f04cSPaul Traina endhostent(); 1661363f04cSPaul Traina return (p); 1671363f04cSPaul Traina } 1681363f04cSPaul Traina 1691363f04cSPaul Traina struct hostent * 1701363f04cSPaul Traina _gethostbyhtaddr(addr, len, type) 1711363f04cSPaul Traina const char *addr; 1721363f04cSPaul Traina int len, type; 1731363f04cSPaul Traina { 1741363f04cSPaul Traina register struct hostent *p; 1751363f04cSPaul Traina 1761363f04cSPaul Traina sethostent(0); 1771363f04cSPaul Traina while ((p = gethostent())) 1781363f04cSPaul Traina if (p->h_addrtype == type && !bcmp(p->h_addr, addr, len)) 1791363f04cSPaul Traina break; 1801363f04cSPaul Traina endhostent(); 1811363f04cSPaul Traina return (p); 1821363f04cSPaul Traina } 183