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: getnetnamadr.c,v 1.1 1994/09/25 02:12:29 pst Exp $"; 28 static char rcsid[] = "$Id: getnetnamadr.c,v 1.1 1994/09/25 02:12:29 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 41 extern struct netent * _getnetbyhtname __P((const char *)); 42 extern struct netent * _getnetbydnsname __P((const char *)); 43 extern struct netent * _getnetbynisname __P((const char *)); 44 extern struct netent * _getnetbyhtaddr __P((long, int)); 45 extern struct netent * _getnetbydnsaddr __P((long, int)); 46 extern struct netent * _getnetbynisaddr __P((long, int)); 47 48 #ifndef _PATH_NETCONF 49 #define _PATH_NETCONF "/etc/host.conf" 50 #endif 51 52 enum service_type { 53 SERVICE_NONE = 0, 54 SERVICE_BIND, 55 SERVICE_TABLE, 56 SERVICE_NIS }; 57 #define SERVICE_MAX SERVICE_NIS 58 59 static struct { 60 const char *name; 61 enum service_type type; 62 } service_names[] = { 63 { "hosts", SERVICE_TABLE }, 64 { "/etc/hosts", SERVICE_TABLE }, 65 { "hosttable", SERVICE_TABLE }, 66 { "htable", SERVICE_TABLE }, 67 { "bind", SERVICE_BIND }, 68 { "dns", SERVICE_BIND }, 69 { "domain", SERVICE_BIND }, 70 { "yp", SERVICE_NIS }, 71 { "yellowpages", SERVICE_NIS }, 72 { "nis", SERVICE_NIS }, 73 { 0, SERVICE_NONE } 74 }; 75 76 static enum service_type service_order[SERVICE_MAX + 1]; 77 static int service_done = 0; 78 79 static enum service_type 80 get_service_name(const char *name) { 81 int i; 82 for(i = 0; service_names[i].type != SERVICE_NONE; i++) { 83 if(!strcasecmp(name, service_names[i].name)) { 84 return service_names[i].type; 85 } 86 } 87 return SERVICE_NONE; 88 } 89 90 static void 91 init_services() 92 { 93 char *cp, buf[BUFSIZ]; 94 register int cc = 0; 95 FILE *fd; 96 97 if ((fd = (FILE *)fopen(_PATH_NETCONF, "r")) == NULL) { 98 /* make some assumptions */ 99 service_order[0] = SERVICE_TABLE; 100 service_order[1] = SERVICE_NONE; 101 } else { 102 while (fgets(buf, BUFSIZ, fd) != NULL && cc < SERVICE_MAX) { 103 if(buf[0] == '#') 104 continue; 105 106 cp = strtok(buf, "\n \t,:;"); 107 do { 108 if(!isalpha(cp[0])) continue; 109 service_order[cc] = get_service_name(buf); 110 if(service_order[cc] != SERVICE_NONE) 111 cc++; 112 } while((cp = strtok((char *)0, "\n \t,:;")) 113 && (cc < SERVICE_MAX)); 114 } 115 service_order[cc] = SERVICE_NONE; 116 fclose(fd); 117 } 118 service_done = 1; 119 } 120 121 struct netent * 122 getnetbyname(const char *name) 123 { 124 struct netent *hp = 0; 125 int nserv = 0; 126 127 if (!service_done) 128 init_services(); 129 130 while (!hp) { 131 switch (service_order[nserv]) { 132 case SERVICE_NONE: 133 return NULL; 134 case SERVICE_TABLE: 135 hp = _getnetbyhtname(name); 136 break; 137 case SERVICE_BIND: 138 hp = _getnetbydnsname(name); 139 break; 140 case SERVICE_NIS: 141 hp = _getnetbynisname(name); 142 break; 143 } 144 nserv++; 145 } 146 return hp; 147 } 148 149 struct netent * 150 getnetbyaddr(addr, type) 151 long addr; 152 int type; 153 { 154 struct netent *hp = 0; 155 int nserv = 0; 156 157 if (!service_done) 158 init_services(); 159 160 while (!hp) { 161 switch (service_order[nserv]) { 162 case SERVICE_NONE: 163 return 0; 164 case SERVICE_TABLE: 165 hp = _getnetbyhtaddr(addr, type); 166 break; 167 case SERVICE_BIND: 168 hp = _getnetbydnsaddr(addr, type); 169 break; 170 case SERVICE_NIS: 171 hp = _getnetbynisaddr(addr, type); 172 break; 173 } 174 nserv++; 175 } 176 return hp; 177 } 178 179 void 180 setnetent(stayopen) 181 int stayopen; 182 { 183 _setnethtent(stayopen); 184 _setnetdnsent(stayopen); 185 } 186 187 void 188 endnetent() 189 { 190 _endnethtent(); 191 _endnetdnsent(); 192 } 193