gethostnamadr.c revision 3070
11573Srgrimes/*- 23070Spst * Copyright (c) 1994, Garrett Wollman 31573Srgrimes * 41573Srgrimes * Redistribution and use in source and binary forms, with or without 51573Srgrimes * modification, are permitted provided that the following conditions 61573Srgrimes * are met: 71573Srgrimes * 1. Redistributions of source code must retain the above copyright 81573Srgrimes * notice, this list of conditions and the following disclaimer. 91573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 101573Srgrimes * notice, this list of conditions and the following disclaimer in the 111573Srgrimes * documentation and/or other materials provided with the distribution. 121573Srgrimes * 133070Spst * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND 141573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 151573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 161573Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 171573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 181573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 191573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 201573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 211573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 221573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 231573Srgrimes * SUCH DAMAGE. 241573Srgrimes */ 251573Srgrimes 261573Srgrimes#if defined(LIBC_SCCS) && !defined(lint) 273070Spststatic char sccsid[] = "@(#)$Id$"; 283070Spststatic char rcsid[] = "$Id$"; 291573Srgrimes#endif /* LIBC_SCCS and not lint */ 301573Srgrimes 311573Srgrimes#include <sys/param.h> 321573Srgrimes#include <sys/socket.h> 331573Srgrimes#include <netinet/in.h> 341573Srgrimes#include <arpa/inet.h> 351573Srgrimes#include <netdb.h> 361573Srgrimes#include <stdio.h> 371573Srgrimes#include <ctype.h> 381573Srgrimes#include <errno.h> 391573Srgrimes#include <string.h> 401573Srgrimes 413070Spstextern struct hostent * _gethostbyhtname __P((const char *)); 423070Spstextern struct hostent * _gethostbydnsname __P((const char *)); 433070Spstextern struct hostent * _gethostbynisname __P((const char *)); 443070Spstextern struct hostent * _gethostbyhtaddr __P((const char *, int, int)); 453070Spstextern struct hostent * _gethostbydnsaddr __P((const char *, int, int)); 463070Spstextern struct hostent * _gethostbynisaddr __P((const char *, int, int)); 471573Srgrimes 481991Swollman#define _PATH_HOSTCONF "/etc/host.conf" 491991Swollman 501991Swollmanenum service_type { 511991Swollman SERVICE_NONE = 0, 521991Swollman SERVICE_BIND, 531991Swollman SERVICE_HOSTS, 541991Swollman SERVICE_NIS }; 551991Swollman#define SERVICE_MAX SERVICE_NIS 561991Swollman 571991Swollmanstatic struct { 581991Swollman const char *name; 591991Swollman enum service_type type; 601991Swollman} service_names[] = { 611991Swollman { "hosts", SERVICE_HOSTS }, 621991Swollman { "/etc/hosts", SERVICE_HOSTS }, 631991Swollman { "hosttable", SERVICE_HOSTS }, 641991Swollman { "htable", SERVICE_HOSTS }, 651991Swollman { "bind", SERVICE_BIND }, 661991Swollman { "dns", SERVICE_BIND }, 671991Swollman { "domain", SERVICE_BIND }, 681991Swollman { "yp", SERVICE_NIS }, 691991Swollman { "yellowpages", SERVICE_NIS }, 701991Swollman { "nis", SERVICE_NIS }, 711991Swollman { 0, SERVICE_NONE } 721991Swollman}; 731991Swollman 741991Swollmanstatic enum service_type service_order[SERVICE_MAX + 1]; 751991Swollmanstatic int service_done = 0; 761991Swollman 771991Swollmanstatic enum service_type 781991Swollmanget_service_name(const char *name) { 791991Swollman int i; 801991Swollman for(i = 0; service_names[i].type != SERVICE_NONE; i++) { 811991Swollman if(!strcasecmp(name, service_names[i].name)) { 821991Swollman return service_names[i].type; 831991Swollman } 841991Swollman } 851991Swollman return SERVICE_NONE; 861991Swollman} 871991Swollman 881991Swollmanstatic void 891991Swollmaninit_services() 901991Swollman{ 911991Swollman char *cp, buf[BUFSIZ]; 921991Swollman register int cc = 0; 931991Swollman FILE *fd; 941991Swollman 951991Swollman if ((fd = (FILE *)fopen(_PATH_HOSTCONF, "r")) == NULL) { 961991Swollman /* make some assumptions */ 971991Swollman service_order[0] = SERVICE_BIND; 981991Swollman service_order[1] = SERVICE_HOSTS; 991991Swollman service_order[2] = SERVICE_NONE; 1001991Swollman } else { 1011991Swollman while (fgets(buf, BUFSIZ, fd) != NULL && cc < SERVICE_MAX) { 1021991Swollman if(buf[0] == '#') 1031991Swollman continue; 1041991Swollman 1051991Swollman cp = strtok(buf, "\n \t,:;"); 1061991Swollman do { 1071991Swollman if(!isalpha(cp[0])) continue; 1081991Swollman service_order[cc] = get_service_name(buf); 1091991Swollman if(service_order[cc] != SERVICE_NONE) 1101991Swollman cc++; 1111991Swollman } while((cp = strtok((char *)0, "\n \t,:;")) 1121991Swollman && (cc < SERVICE_MAX)); 1131991Swollman } 1141991Swollman service_order[cc] = SERVICE_NONE; 1151991Swollman fclose(fd); 1161991Swollman } 1171991Swollman service_done = 1; 1181991Swollman} 1191991Swollman 1201573Srgrimesstruct hostent * 1211991Swollmangethostbyname(const char *name) 1221991Swollman{ 1231991Swollman struct hostent *hp = 0; 1241991Swollman int nserv = 0; 1251991Swollman 1263070Spst if (!service_done) 1271991Swollman init_services(); 1281991Swollman 1293070Spst while (!hp) { 1303070Spst switch (service_order[nserv]) { 1311991Swollman case SERVICE_NONE: 1323070Spst return NULL; 1331991Swollman case SERVICE_HOSTS: 1343070Spst hp = _gethostbyhtname(name); 1351991Swollman break; 1361991Swollman case SERVICE_BIND: 1373070Spst hp = _gethostbydnsname(name); 1381991Swollman break; 1391991Swollman case SERVICE_NIS: 1403070Spst hp = _gethostbynisname(name); 1411991Swollman break; 1421991Swollman } 1431991Swollman nserv++; 1441991Swollman } 1451991Swollman return hp; 1461991Swollman} 1471991Swollman 1481991Swollmanstruct hostent * 1491991Swollmangethostbyaddr(const char *addr, int len, int type) 1501991Swollman{ 1511991Swollman struct hostent *hp = 0; 1521991Swollman int nserv = 0; 1531991Swollman 1543070Spst if (!service_done) 1551992Swollman init_services(); 1561991Swollman 1573070Spst while (!hp) { 1583070Spst switch (service_order[nserv]) { 1591991Swollman case SERVICE_NONE: 1601991Swollman return 0; 1611991Swollman case SERVICE_HOSTS: 1623070Spst hp = _gethostbyhtaddr(addr, len, type); 1631991Swollman break; 1641991Swollman case SERVICE_BIND: 1653070Spst hp = _gethostbydnsaddr(addr, len, type); 1661991Swollman break; 1671991Swollman case SERVICE_NIS: 1683070Spst hp = _gethostbynisaddr(addr, len, type); 1691991Swollman break; 1701991Swollman } 1711991Swollman nserv++; 1721991Swollman } 1731991Swollman return hp; 1741991Swollman} 1751991Swollman 1763070Spstvoid 1773070Spstsethostent(stayopen) 1783070Spst int stayopen; 1793070Spst{ 1803070Spst _sethosthtent(stayopen); 1813070Spst _sethostdnsent(stayopen); 1823070Spst} 1833070Spst 1843070Spstvoid 1853070Spstendhostent() 1863070Spst{ 1873070Spst _endhosthtent(); 1883070Spst _endhostdnsent(); 1893070Spst} 190