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