]>
Commit | Line | Data |
---|---|---|
d7ce7006 | 1 | /*-\r |
2 | * Copyright (c) 1994, Garrett Wollman\r | |
3 | *\r | |
4 | * Redistribution and use in source and binary forms, with or without\r | |
5 | * modification, are permitted provided that the following conditions\r | |
6 | * are met:\r | |
7 | * 1. Redistributions of source code must retain the above copyright\r | |
8 | * notice, this list of conditions and the following disclaimer.\r | |
9 | * 2. Redistributions in binary form must reproduce the above copyright\r | |
10 | * notice, this list of conditions and the following disclaimer in the\r | |
11 | * documentation and/or other materials provided with the distribution.\r | |
12 | *\r | |
13 | * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND\r | |
14 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r | |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r | |
16 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE\r | |
17 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r | |
18 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r | |
19 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r | |
20 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r | |
21 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r | |
22 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r | |
23 | * SUCH DAMAGE.\r | |
24 | */\r | |
25 | \r | |
26 | #if defined(LIBC_SCCS) && !defined(lint)\r | |
27 | static char rcsid[] = "$Id: getnetnamadr.c,v 1.1.1.1 2003/11/19 01:51:28 kyu3 Exp $";\r | |
28 | #endif /* LIBC_SCCS and not lint */\r | |
29 | \r | |
30 | #include <sys/param.h>\r | |
31 | #include <sys/socket.h>\r | |
32 | #include <netinet/in.h>\r | |
33 | #include <arpa/inet.h>\r | |
34 | #include <netdb.h>\r | |
35 | #include <stdio.h>\r | |
36 | #include <ctype.h>\r | |
37 | #include <errno.h>\r | |
38 | #include <paths.h>\r | |
39 | #include <string.h>\r | |
40 | \r | |
41 | #include "Socklib_internals.h"\r | |
42 | \r | |
43 | enum service_type {\r | |
44 | SERVICE_NONE = 0,\r | |
45 | SERVICE_BIND,\r | |
46 | SERVICE_TABLE,\r | |
47 | SERVICE_NIS };\r | |
48 | #define SERVICE_MAX SERVICE_NIS\r | |
49 | \r | |
50 | static struct {\r | |
51 | const char *name;\r | |
52 | enum service_type type;\r | |
53 | } service_names[] = {\r | |
54 | { "hosts", SERVICE_TABLE },\r | |
55 | { _PATH_HOSTS, SERVICE_TABLE },\r | |
56 | { "hosttable", SERVICE_TABLE },\r | |
57 | { "htable", SERVICE_TABLE },\r | |
58 | { "bind", SERVICE_BIND },\r | |
59 | { "dns", SERVICE_BIND },\r | |
60 | { "domain", SERVICE_BIND },\r | |
61 | { "yp", SERVICE_NIS },\r | |
62 | { "yellowpages", SERVICE_NIS },\r | |
63 | { "nis", SERVICE_NIS },\r | |
64 | { 0, SERVICE_NONE }\r | |
65 | };\r | |
66 | \r | |
67 | static enum service_type service_order[SERVICE_MAX + 1];\r | |
68 | static int service_done = 0;\r | |
69 | \r | |
70 | static enum service_type\r | |
71 | get_service_name(const char *name) {\r | |
72 | int i;\r | |
73 | for(i = 0; service_names[i].type != SERVICE_NONE; i++) {\r | |
74 | if(!strcasecmp(name, service_names[i].name)) {\r | |
75 | return service_names[i].type;\r | |
76 | }\r | |
77 | }\r | |
78 | return SERVICE_NONE;\r | |
79 | }\r | |
80 | \r | |
81 | static void\r | |
82 | init_services()\r | |
83 | {\r | |
84 | char *cp, *p, buf[BUFSIZ];\r | |
85 | register int cc = 0;\r | |
86 | FILE *fd;\r | |
87 | \r | |
88 | if ((fd = (FILE *)fopen(_PATH_NETCONF, "r")) == NULL) {\r | |
89 | /* make some assumptions */\r | |
90 | service_order[0] = SERVICE_TABLE;\r | |
91 | service_order[1] = SERVICE_NONE;\r | |
92 | } else {\r | |
93 | while (fgets(buf, BUFSIZ, fd) != NULL && cc < SERVICE_MAX) {\r | |
94 | if(buf[0] == '#')\r | |
95 | continue;\r | |
96 | \r | |
97 | p = buf;\r | |
98 | while ((cp = strsep(&p, "\n \t,:;")) != NULL && *cp == '\0')\r | |
99 | ;\r | |
100 | if (cp == NULL)\r | |
101 | continue;\r | |
102 | do {\r | |
103 | if (isalpha(cp[0])) {\r | |
104 | service_order[cc] = get_service_name(cp);\r | |
105 | if(service_order[cc] != SERVICE_NONE)\r | |
106 | cc++;\r | |
107 | }\r | |
108 | while ((cp = strsep(&p, "\n \t,:;")) != NULL && *cp == '\0')\r | |
109 | ;\r | |
110 | } while(cp != NULL && cc < SERVICE_MAX);\r | |
111 | }\r | |
112 | service_order[cc] = SERVICE_NONE;\r | |
113 | fclose(fd);\r | |
114 | }\r | |
115 | service_done = 1;\r | |
116 | }\r | |
117 | \r | |
118 | struct netent *\r | |
119 | getnetbyname(const char *name)\r | |
120 | {\r | |
121 | struct netent *hp = 0;\r | |
122 | int nserv = 0;\r | |
123 | \r | |
124 | if (!service_done)\r | |
125 | init_services();\r | |
126 | \r | |
127 | while (!hp) {\r | |
128 | switch (service_order[nserv]) {\r | |
129 | case SERVICE_NONE:\r | |
130 | return NULL;\r | |
131 | case SERVICE_TABLE:\r | |
132 | hp = _getnetbyhtname(name);\r | |
133 | break;\r | |
134 | case SERVICE_BIND:\r | |
135 | hp = _getnetbydnsname(name);\r | |
136 | break;\r | |
137 | case SERVICE_NIS:\r | |
138 | hp = _getnetbynisname(name);\r | |
139 | break;\r | |
140 | }\r | |
141 | nserv++;\r | |
142 | }\r | |
143 | return hp;\r | |
144 | }\r | |
145 | \r | |
146 | struct netent *\r | |
147 | getnetbyaddr(uint32_t addr, int af)\r | |
148 | {\r | |
149 | struct netent *hp = 0;\r | |
150 | int nserv = 0;\r | |
151 | \r | |
152 | if (!service_done)\r | |
153 | init_services();\r | |
154 | \r | |
155 | while (!hp) {\r | |
156 | switch (service_order[nserv]) {\r | |
157 | case SERVICE_NONE:\r | |
158 | return 0;\r | |
159 | case SERVICE_TABLE:\r | |
160 | hp = _getnetbyhtaddr(addr, af);\r | |
161 | break;\r | |
162 | case SERVICE_BIND:\r | |
163 | hp = _getnetbydnsaddr(addr, af);\r | |
164 | break;\r | |
165 | case SERVICE_NIS:\r | |
166 | hp = _getnetbynisaddr(addr, af);\r | |
167 | break;\r | |
168 | }\r | |
169 | nserv++;\r | |
170 | }\r | |
171 | return hp;\r | |
172 | }\r | |
173 | \r | |
174 | void\r | |
175 | setnetent(int stayopen)\r | |
176 | {\r | |
177 | _setnethtent(stayopen);\r | |
178 | _setnetdnsent(stayopen);\r | |
179 | }\r | |
180 | \r | |
181 | void\r | |
182 | endnetent()\r | |
183 | {\r | |
184 | _endnethtent();\r | |
185 | _endnetdnsent();\r | |
186 | }\r |