getnetnamadr.c revision 3070
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$"; 28static 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 41extern struct netent * _getnetbyhtname __P((const char *)); 42extern struct netent * _getnetbydnsname __P((const char *)); 43extern struct netent * _getnetbynisname __P((const char *)); 44extern struct netent * _getnetbyhtaddr __P((long, int)); 45extern struct netent * _getnetbydnsaddr __P((long, int)); 46extern struct netent * _getnetbynisaddr __P((long, int)); 47 48#define _PATH_NETCONF "/etc/net.conf" 49 50enum service_type { 51 SERVICE_NONE = 0, 52 SERVICE_BIND, 53 SERVICE_TABLE, 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 { "nets", SERVICE_TABLE }, 62 { "/etc/nets", SERVICE_TABLE }, 63 { "nettable", SERVICE_TABLE }, 64 { "ntable", SERVICE_TABLE }, 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, buf[BUFSIZ]; 92 register int cc = 0; 93 FILE *fd; 94 95 if ((fd = (FILE *)fopen(_PATH_NETCONF, "r")) == NULL) { 96 /* make some assumptions */ 97 service_order[0] = SERVICE_TABLE; 98 service_order[1] = SERVICE_NONE; 99 } else { 100 while (fgets(buf, BUFSIZ, fd) != NULL && cc < SERVICE_MAX) { 101 if(buf[0] == '#') 102 continue; 103 104 cp = strtok(buf, "\n \t,:;"); 105 do { 106 if(!isalpha(cp[0])) continue; 107 service_order[cc] = get_service_name(buf); 108 if(service_order[cc] != SERVICE_NONE) 109 cc++; 110 } while((cp = strtok((char *)0, "\n \t,:;")) 111 && (cc < SERVICE_MAX)); 112 } 113 service_order[cc] = SERVICE_NONE; 114 fclose(fd); 115 } 116 service_done = 1; 117} 118 119struct netent * 120getnetbyname(const char *name) 121{ 122 struct netent *hp = 0; 123 int nserv = 0; 124 125 if (!service_done) 126 init_services(); 127 128 while (!hp) { 129 switch (service_order[nserv]) { 130 case SERVICE_NONE: 131 return NULL; 132 case SERVICE_TABLE: 133 hp = _getnetbyhtname(name); 134 break; 135 case SERVICE_BIND: 136 hp = _getnetbydnsname(name); 137 break; 138 case SERVICE_NIS: 139 hp = _getnetbynisname(name); 140 break; 141 } 142 nserv++; 143 } 144 return hp; 145} 146 147struct netent * 148getnetbyaddr(addr, type) 149 long addr; 150 int type; 151{ 152 struct netent *hp = 0; 153 int nserv = 0; 154 155 if (!service_done) 156 init_services(); 157 158 while (!hp) { 159 switch (service_order[nserv]) { 160 case SERVICE_NONE: 161 return 0; 162 case SERVICE_TABLE: 163 hp = _getnetbyhtaddr(addr, type); 164 break; 165 case SERVICE_BIND: 166 hp = _getnetbydnsaddr(addr, type); 167 break; 168 case SERVICE_NIS: 169 hp = _getnetbynisaddr(addr, type); 170 break; 171 } 172 nserv++; 173 } 174 return hp; 175} 176 177void 178setnetent(stayopen) 179 int stayopen; 180{ 181 _setnethtent(stayopen); 182 _setnetdnsent(stayopen); 183} 184 185void 186endnetent() 187{ 188 _endnethtent(); 189 _endnetdnsent(); 190} 191