gethostnamadr.c revision 7324
1/*- 2 * Copyright (c) 1994, Garrett Wollman 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26#if defined(LIBC_SCCS) && !defined(lint) 27static char sccsid[] = "@(#)$Id: gethostnamadr.c,v 1.5 1994/09/25 02:12:18 pst Exp $"; 28static char rcsid[] = "$Id: gethostnamadr.c,v 1.5 1994/09/25 02:12:18 pst Exp $"; 29#endif /* LIBC_SCCS and not lint */ 30 31#include <sys/param.h> 32#include <sys/socket.h> 33#include <netinet/in.h> 34#include <arpa/inet.h> 35#include <netdb.h> 36#include <stdio.h> 37#include <ctype.h> 38#include <errno.h> 39#include <string.h> 40 41extern struct hostent * _gethostbyhtname __P((const char *)); 42extern struct hostent * _gethostbydnsname __P((const char *)); 43extern struct hostent * _gethostbynisname __P((const char *)); 44extern struct hostent * _gethostbyhtaddr __P((const char *, int, int)); 45extern struct hostent * _gethostbydnsaddr __P((const char *, int, int)); 46extern struct hostent * _gethostbynisaddr __P((const char *, int, int)); 47 48#define _PATH_HOSTCONF "/etc/host.conf" 49 50enum service_type { 51 SERVICE_NONE = 0, 52 SERVICE_BIND, 53 SERVICE_HOSTS, 54 SERVICE_NIS }; 55#define SERVICE_MAX SERVICE_NIS 56 57static struct { 58 const char *name; 59 enum service_type type; 60} service_names[] = { 61 { "hosts", SERVICE_HOSTS }, 62 { "/etc/hosts", SERVICE_HOSTS }, 63 { "hosttable", SERVICE_HOSTS }, 64 { "htable", SERVICE_HOSTS }, 65 { "bind", SERVICE_BIND }, 66 { "dns", SERVICE_BIND }, 67 { "domain", SERVICE_BIND }, 68 { "yp", SERVICE_NIS }, 69 { "yellowpages", SERVICE_NIS }, 70 { "nis", SERVICE_NIS }, 71 { 0, SERVICE_NONE } 72}; 73 74static enum service_type service_order[SERVICE_MAX + 1]; 75static int service_done = 0; 76 77static enum service_type 78get_service_name(const char *name) { 79 int i; 80 for(i = 0; service_names[i].type != SERVICE_NONE; i++) { 81 if(!strcasecmp(name, service_names[i].name)) { 82 return service_names[i].type; 83 } 84 } 85 return SERVICE_NONE; 86} 87 88static void 89init_services() 90{ 91 char *cp, *p, buf[BUFSIZ]; 92 register int cc = 0; 93 FILE *fd; 94 95 if ((fd = (FILE *)fopen(_PATH_HOSTCONF, "r")) == NULL) { 96 /* make some assumptions */ 97 service_order[0] = SERVICE_BIND; 98 service_order[1] = SERVICE_HOSTS; 99 service_order[2] = SERVICE_NONE; 100 } else { 101 while (fgets(buf, BUFSIZ, fd) != NULL && cc < SERVICE_MAX) { 102 if(buf[0] == '#') 103 continue; 104 105 p = buf; 106 while ((cp = strsep(&p, "\n \t,:;")) != NULL && *cp == '\0') 107 ; 108 if (cp == NULL) 109 continue; 110 do { 111 if (isalpha(cp[0])) { 112 service_order[cc] = get_service_name(cp); 113 if(service_order[cc] != SERVICE_NONE) 114 cc++; 115 } 116 while ((cp = strsep(&p, "\n \t,:;")) != NULL && *cp == '\0') 117 ; 118 } while(cp != NULL && cc < SERVICE_MAX); 119 } 120 service_order[cc] = SERVICE_NONE; 121 fclose(fd); 122 } 123 service_done = 1; 124} 125 126struct hostent * 127gethostbyname(const char *name) 128{ 129 struct hostent *hp = 0; 130 int nserv = 0; 131 132 if (!service_done) 133 init_services(); 134 135 while (!hp) { 136 switch (service_order[nserv]) { 137 case SERVICE_NONE: 138 return NULL; 139 case SERVICE_HOSTS: 140 hp = _gethostbyhtname(name); 141 break; 142 case SERVICE_BIND: 143 hp = _gethostbydnsname(name); 144 break; 145 case SERVICE_NIS: 146 hp = _gethostbynisname(name); 147 break; 148 } 149 nserv++; 150 } 151 return hp; 152} 153 154struct hostent * 155gethostbyaddr(const char *addr, int len, int type) 156{ 157 struct hostent *hp = 0; 158 int nserv = 0; 159 160 if (!service_done) 161 init_services(); 162 163 while (!hp) { 164 switch (service_order[nserv]) { 165 case SERVICE_NONE: 166 return 0; 167 case SERVICE_HOSTS: 168 hp = _gethostbyhtaddr(addr, len, type); 169 break; 170 case SERVICE_BIND: 171 hp = _gethostbydnsaddr(addr, len, type); 172 break; 173 case SERVICE_NIS: 174 hp = _gethostbynisaddr(addr, len, type); 175 break; 176 } 177 nserv++; 178 } 179 return hp; 180} 181 182void 183sethostent(stayopen) 184 int stayopen; 185{ 186 _sethosthtent(stayopen); 187 _sethostdnsent(stayopen); 188} 189 190void 191endhostent() 192{ 193 _endhosthtent(); 194 _endhostdnsent(); 195} 196