1 | #include <machine/rtems-bsd-user-space.h> |
---|
2 | |
---|
3 | /*- |
---|
4 | * Copyright (c) 1994, Garrett Wollman |
---|
5 | * |
---|
6 | * Redistribution and use in source and binary forms, with or without |
---|
7 | * modification, are permitted provided that the following conditions |
---|
8 | * are met: |
---|
9 | * 1. Redistributions of source code must retain the above copyright |
---|
10 | * notice, this list of conditions and the following disclaimer. |
---|
11 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
12 | * notice, this list of conditions and the following disclaimer in the |
---|
13 | * documentation and/or other materials provided with the distribution. |
---|
14 | * |
---|
15 | * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND |
---|
16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
21 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
23 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
25 | * SUCH DAMAGE. |
---|
26 | */ |
---|
27 | |
---|
28 | #if HAVE_CONFIG_H |
---|
29 | #include "config.h" |
---|
30 | #endif |
---|
31 | |
---|
32 | #include <sys/param.h> |
---|
33 | #include <sys/socket.h> |
---|
34 | #include <netinet/in.h> |
---|
35 | #include <arpa/inet.h> |
---|
36 | #include <netdb.h> |
---|
37 | #include <rtems/rtems_netdb.h> |
---|
38 | #include <stdio.h> |
---|
39 | #include <ctype.h> |
---|
40 | #include <errno.h> |
---|
41 | #include <string.h> |
---|
42 | #ifdef HAVE_STRINGS_H |
---|
43 | #include <strings.h> /* for strcasecmp */ |
---|
44 | #endif |
---|
45 | |
---|
46 | #ifndef _PATH_NETCONF |
---|
47 | #define _PATH_NETCONF "/etc/host.conf" |
---|
48 | #endif |
---|
49 | |
---|
50 | enum service_type { |
---|
51 | SERVICE_NONE = 0, |
---|
52 | SERVICE_BIND, |
---|
53 | SERVICE_TABLE, |
---|
54 | SERVICE_NIS }; |
---|
55 | #define SERVICE_MAX SERVICE_NIS |
---|
56 | |
---|
57 | static struct { |
---|
58 | const char *name; |
---|
59 | enum service_type type; |
---|
60 | } service_names[] = { |
---|
61 | { "hosts", SERVICE_TABLE }, |
---|
62 | { "/etc/hosts", SERVICE_TABLE }, |
---|
63 | { "hosttable", SERVICE_TABLE }, |
---|
64 | { "htable", 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 | |
---|
74 | static enum service_type service_order[SERVICE_MAX + 1]; |
---|
75 | static int service_done = 0; |
---|
76 | |
---|
77 | static enum service_type |
---|
78 | get_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 | |
---|
88 | static void |
---|
89 | init_services(void) |
---|
90 | { |
---|
91 | char *cp, *p, 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 | p = buf; |
---|
105 | while ((cp = strsep(&p, "\n \t,:;")) != NULL && *cp == '\0') |
---|
106 | ; |
---|
107 | if (cp == NULL) |
---|
108 | continue; |
---|
109 | do { |
---|
110 | if (isalpha((unsigned char)cp[0])) { |
---|
111 | service_order[cc] = get_service_name(cp); |
---|
112 | if(service_order[cc] != SERVICE_NONE) |
---|
113 | cc++; |
---|
114 | } |
---|
115 | while ((cp = strsep(&p, "\n \t,:;")) != NULL && *cp == '\0') |
---|
116 | ; |
---|
117 | } while(cp != NULL && cc < SERVICE_MAX); |
---|
118 | } |
---|
119 | service_order[cc] = SERVICE_NONE; |
---|
120 | fclose(fd); |
---|
121 | } |
---|
122 | service_done = 1; |
---|
123 | } |
---|
124 | |
---|
125 | struct netent * |
---|
126 | getnetbyname(const char *name) |
---|
127 | { |
---|
128 | struct netent *hp = 0; |
---|
129 | int nserv = 0; |
---|
130 | |
---|
131 | if (!service_done) |
---|
132 | init_services(); |
---|
133 | |
---|
134 | while (!hp) { |
---|
135 | switch (service_order[nserv]) { |
---|
136 | case SERVICE_NONE: |
---|
137 | return NULL; |
---|
138 | case SERVICE_TABLE: |
---|
139 | hp = _getnetbyhtname(name); |
---|
140 | break; |
---|
141 | case SERVICE_BIND: |
---|
142 | hp = _getnetbydnsname(name); |
---|
143 | break; |
---|
144 | case SERVICE_NIS: |
---|
145 | hp = _getnetbynisname(name); |
---|
146 | break; |
---|
147 | } |
---|
148 | nserv++; |
---|
149 | } |
---|
150 | return hp; |
---|
151 | } |
---|
152 | |
---|
153 | struct netent * |
---|
154 | getnetbyaddr(uint32_t addr, int af) |
---|
155 | { |
---|
156 | struct netent *hp = 0; |
---|
157 | int nserv = 0; |
---|
158 | |
---|
159 | if (!service_done) |
---|
160 | init_services(); |
---|
161 | |
---|
162 | while (!hp) { |
---|
163 | switch (service_order[nserv]) { |
---|
164 | case SERVICE_NONE: |
---|
165 | return 0; |
---|
166 | case SERVICE_TABLE: |
---|
167 | hp = _getnetbyhtaddr(addr, af); |
---|
168 | break; |
---|
169 | case SERVICE_BIND: |
---|
170 | hp = _getnetbydnsaddr(addr, af); |
---|
171 | break; |
---|
172 | case SERVICE_NIS: |
---|
173 | hp = _getnetbynisaddr(addr, af); |
---|
174 | break; |
---|
175 | } |
---|
176 | nserv++; |
---|
177 | } |
---|
178 | return hp; |
---|
179 | } |
---|
180 | |
---|
181 | void |
---|
182 | setnetent(int stayopen) |
---|
183 | { |
---|
184 | _setnethtent(stayopen); |
---|
185 | _setnetdnsent(stayopen); |
---|
186 | } |
---|
187 | |
---|
188 | void |
---|
189 | endnetent(void) |
---|
190 | { |
---|
191 | _endnethtent(); |
---|
192 | _endnetdnsent(); |
---|
193 | } |
---|