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) 27 static char sccsid[] = "@(#)$Id$"; 28 static char rcsid[] = "$Id$"; 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 41 extern struct hostent * _gethostbyhtname __P((const char *)); 42 extern struct hostent * _gethostbydnsname __P((const char *)); 43 extern struct hostent * _gethostbynisname __P((const char *)); 44 extern struct hostent * _gethostbyhtaddr __P((const char *, int, int)); 45 extern struct hostent * _gethostbydnsaddr __P((const char *, int, int)); 46 extern struct hostent * _gethostbynisaddr __P((const char *, int, int)); 47 48 #define _PATH_HOSTCONF "/etc/host.conf" 49 50 enum service_type { 51 SERVICE_NONE = 0, 52 SERVICE_BIND, 53 SERVICE_HOSTS, 54 SERVICE_NIS }; 55 #define SERVICE_MAX SERVICE_NIS 56 57 static 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 74 static enum service_type service_order[SERVICE_MAX + 1]; 75 static int service_done = 0; 76 77 static enum service_type 78 get_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 88 static void 89 init_services() 90 { 91 char *cp, 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 cp = strtok(buf, "\n \t,:;"); 106 do { 107 if(!isalpha(cp[0])) continue; 108 service_order[cc] = get_service_name(buf); 109 if(service_order[cc] != SERVICE_NONE) 110 cc++; 111 } while((cp = strtok((char *)0, "\n \t,:;")) 112 && (cc < SERVICE_MAX)); 113 } 114 service_order[cc] = SERVICE_NONE; 115 fclose(fd); 116 } 117 service_done = 1; 118 } 119 120 struct hostent * 121 gethostbyname(const char *name) 122 { 123 struct hostent *hp = 0; 124 int nserv = 0; 125 126 if (!service_done) 127 init_services(); 128 129 while (!hp) { 130 switch (service_order[nserv]) { 131 case SERVICE_NONE: 132 return NULL; 133 case SERVICE_HOSTS: 134 hp = _gethostbyhtname(name); 135 break; 136 case SERVICE_BIND: 137 hp = _gethostbydnsname(name); 138 break; 139 case SERVICE_NIS: 140 hp = _gethostbynisname(name); 141 break; 142 } 143 nserv++; 144 } 145 return hp; 146 } 147 148 struct hostent * 149 gethostbyaddr(const char *addr, int len, int type) 150 { 151 struct hostent *hp = 0; 152 int nserv = 0; 153 154 if (!service_done) 155 init_services(); 156 157 while (!hp) { 158 switch (service_order[nserv]) { 159 case SERVICE_NONE: 160 return 0; 161 case SERVICE_HOSTS: 162 hp = _gethostbyhtaddr(addr, len, type); 163 break; 164 case SERVICE_BIND: 165 hp = _gethostbydnsaddr(addr, len, type); 166 break; 167 case SERVICE_NIS: 168 hp = _gethostbynisaddr(addr, len, type); 169 break; 170 } 171 nserv++; 172 } 173 return hp; 174 } 175 176 void 177 sethostent(stayopen) 178 int stayopen; 179 { 180 _sethosthtent(stayopen); 181 _sethostdnsent(stayopen); 182 } 183 184 void 185 endhostent() 186 { 187 _endhosthtent(); 188 _endhostdnsent(); 189 } 190