getnetnamadr.c (52865) | getnetnamadr.c (65532) |
---|---|
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. --- 10 unchanged lines hidden (view full) --- 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) | 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. --- 10 unchanged lines hidden (view full) --- 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 rcsid[] = "$FreeBSD: head/lib/libc/net/getnetnamadr.c 52865 1999-11-04 05:01:28Z ache $"; | 27static char rcsid[] = "$FreeBSD: head/lib/libc/net/getnetnamadr.c 65532 2000-09-06 18:16:48Z nectar $"; |
28#endif /* LIBC_SCCS and not lint */ 29 30#include <sys/param.h> 31#include <sys/socket.h> 32#include <netinet/in.h> 33#include <arpa/inet.h> 34#include <netdb.h> 35#include <stdio.h> 36#include <ctype.h> 37#include <errno.h> 38#include <string.h> | 28#endif /* LIBC_SCCS and not lint */ 29 30#include <sys/param.h> 31#include <sys/socket.h> 32#include <netinet/in.h> 33#include <arpa/inet.h> 34#include <netdb.h> 35#include <stdio.h> 36#include <ctype.h> 37#include <errno.h> 38#include <string.h> |
39#include <stdarg.h> 40#include <nsswitch.h> |
|
39 | 41 |
40#ifndef _PATH_NETCONF 41#define _PATH_NETCONF "/etc/host.conf" 42#endif | 42extern int _ht_getnetbyname(void *, void *, va_list); 43extern int _dns_getnetbyname(void *, void *, va_list); 44extern int _nis_getnetbyname(void *, void *, va_list); 45extern int _ht_getnetbyaddr(void *, void *, va_list); 46extern int _dns_getnetbyaddr(void *, void *, va_list); 47extern int _nis_getnetbyaddr(void *, void *, va_list); |
43 | 48 |
44enum service_type { 45 SERVICE_NONE = 0, 46 SERVICE_BIND, 47 SERVICE_TABLE, 48 SERVICE_NIS }; 49#define SERVICE_MAX SERVICE_NIS 50 51static struct { 52 const char *name; 53 enum service_type type; 54} service_names[] = { 55 { "hosts", SERVICE_TABLE }, 56 { "/etc/hosts", SERVICE_TABLE }, 57 { "hosttable", SERVICE_TABLE }, 58 { "htable", SERVICE_TABLE }, 59 { "bind", SERVICE_BIND }, 60 { "dns", SERVICE_BIND }, 61 { "domain", SERVICE_BIND }, 62 { "yp", SERVICE_NIS }, 63 { "yellowpages", SERVICE_NIS }, 64 { "nis", SERVICE_NIS }, 65 { 0, SERVICE_NONE } | 49/* Network lookup order if nsswitch.conf is broken or nonexistant */ 50static const ns_src default_src[] = { 51 { NSSRC_FILES, NS_SUCCESS }, 52 { NSSRC_DNS, NS_SUCCESS }, 53 { 0 } |
66}; 67 | 54}; 55 |
68static enum service_type service_order[SERVICE_MAX + 1]; 69static int service_done = 0; 70 71static enum service_type 72get_service_name(const char *name) { 73 int i; 74 for(i = 0; service_names[i].type != SERVICE_NONE; i++) { 75 if(!strcasecmp(name, service_names[i].name)) { 76 return service_names[i].type; 77 } 78 } 79 return SERVICE_NONE; 80} 81 82static void 83init_services() 84{ 85 char *cp, *p, buf[BUFSIZ]; 86 register int cc = 0; 87 FILE *fd; 88 89 if ((fd = (FILE *)fopen(_PATH_NETCONF, "r")) == NULL) { 90 /* make some assumptions */ 91 service_order[0] = SERVICE_TABLE; 92 service_order[1] = SERVICE_NONE; 93 } else { 94 while (fgets(buf, BUFSIZ, fd) != NULL && cc < SERVICE_MAX) { 95 if(buf[0] == '#') 96 continue; 97 98 p = buf; 99 while ((cp = strsep(&p, "\n \t,:;")) != NULL && *cp == '\0') 100 ; 101 if (cp == NULL) 102 continue; 103 do { 104 if (isalpha((unsigned char)cp[0])) { 105 service_order[cc] = get_service_name(cp); 106 if(service_order[cc] != SERVICE_NONE) 107 cc++; 108 } 109 while ((cp = strsep(&p, "\n \t,:;")) != NULL && *cp == '\0') 110 ; 111 } while(cp != NULL && 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; | 56struct netent * 57getnetbyname(const char *name) 58{ 59 struct netent *hp = 0; |
123 int nserv = 0; | 60 int rval; |
124 | 61 |
125 if (!service_done) 126 init_services(); | |
127 | 62 |
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; | 63 static const ns_dtab dtab[] = { 64 NS_FILES_CB(_ht_getnetbyname, NULL) 65 { NSSRC_DNS, _dns_getnetbyname, NULL }, 66 NS_NIS_CB(_nis_getnetbyname, NULL) /* force -DHESIOD */ 67 { 0 } 68 }; 69 70 rval = nsdispatch((void *)&hp, dtab, NSDB_NETWORKS, "getnetbyname", 71 default_src, name); 72 73 if (rval != NS_SUCCESS) 74 return NULL; 75 else 76 return hp; |
145} 146 147struct netent * | 77} 78 79struct netent * |
148getnetbyaddr(addr, af) 149 u_long addr; 150 int af; | 80getnetbyaddr(u_long addr, int af) |
151{ 152 struct netent *hp = 0; | 81{ 82 struct netent *hp = 0; |
153 int nserv = 0; | 83 int rval; |
154 | 84 |
155 if (!service_done) 156 init_services(); | 85 static const ns_dtab dtab[] = { 86 NS_FILES_CB(_ht_getnetbyaddr, NULL) 87 { NSSRC_DNS, _dns_getnetbyaddr, NULL }, 88 NS_NIS_CB(_nis_getnetbyaddr, NULL) /* force -DHESIOD */ 89 { 0 } 90 }; |
157 | 91 |
158 while (!hp) { 159 switch (service_order[nserv]) { 160 case SERVICE_NONE: 161 return 0; 162 case SERVICE_TABLE: 163 hp = _getnetbyhtaddr(addr, af); 164 break; 165 case SERVICE_BIND: 166 hp = _getnetbydnsaddr(addr, af); 167 break; 168 case SERVICE_NIS: 169 hp = _getnetbynisaddr(addr, af); 170 break; 171 } 172 nserv++; 173 } 174 return hp; | 92 rval = nsdispatch((void *)&hp, dtab, NSDB_HOSTS, "getnetbyaddr", 93 default_src, addr, af); 94 95 if (rval != NS_SUCCESS) 96 return NULL; 97 else 98 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} | 99} 100 101void 102setnetent(stayopen) 103 int stayopen; 104{ 105 _setnethtent(stayopen); 106 _setnetdnsent(stayopen); 107} 108 109void 110endnetent() 111{ 112 _endnethtent(); 113 _endnetdnsent(); 114} |